@@ -3,6 +3,7 @@ package httpapi_test
3
3
import (
4
4
"testing"
5
5
6
+ "github.com/moby/moby/pkg/namesgenerator"
6
7
"github.com/stretchr/testify/require"
7
8
8
9
"github.com/coder/coder/coderd/httpapi"
@@ -120,6 +121,57 @@ func TestTemplateDisplayNameValid(t *testing.T) {
120
121
}
121
122
}
122
123
124
+ func TestTemplateVersionNameValid (t * testing.T ) {
125
+ t .Parallel ()
126
+
127
+ testCases := []struct {
128
+ Name string
129
+ Valid bool
130
+ }{
131
+ {"1" , true },
132
+ {"12" , true },
133
+ {"1_2" , true },
134
+ {"1-2" , true },
135
+ {"cray" , true },
136
+ {"123_456" , true },
137
+ {"123-456" , true },
138
+ {"1234_678901234567890" , true },
139
+ {"1234-678901234567890" , true },
140
+ {"S" , true },
141
+ {"a1" , true },
142
+ {"a1K2" , true },
143
+ {"fuzzy_bear3" , true },
144
+ {"fuzzy-bear3" , true },
145
+ {"v1.0.0" , true },
146
+ {"heuristic_cray2" , true },
147
+
148
+ {"" , false },
149
+ {".v1" , false },
150
+ {"v1..0" , false },
151
+ {"4--4" , false },
152
+ {"<b> </b>" , false },
153
+ {"!!!!1 ?????" , false },
154
+ }
155
+ for _ , testCase := range testCases {
156
+ testCase := testCase
157
+ t .Run (testCase .Name , func (t * testing.T ) {
158
+ t .Parallel ()
159
+ valid := httpapi .TemplateVersionNameValid (testCase .Name )
160
+ require .Equal (t , testCase .Valid , valid == nil )
161
+ })
162
+ }
163
+ }
164
+
165
+ func TestGeneratedTemplateVersionNameValid (t * testing.T ) {
166
+ t .Parallel ()
167
+
168
+ for i := 0 ; i < 1000 ; i ++ {
169
+ name := namesgenerator .GetRandomName (1 )
170
+ err := httpapi .TemplateVersionNameValid (name )
171
+ require .NoError (t , err , "invalid template version name: %s" , name )
172
+ }
173
+ }
174
+
123
175
func TestFrom (t * testing.T ) {
124
176
t .Parallel ()
125
177
testCases := []struct {
0 commit comments