@@ -60,6 +60,14 @@ func TestTaskCreate(t *testing.T) {
60
60
Name :presetName ,
61
61
},
62
62
})
63
+ case "/api/v2/templates" :
64
+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Template {
65
+ {
66
+ ID :templateID ,
67
+ Name :templateName ,
68
+ ActiveVersionID :templateVersionID ,
69
+ },
70
+ })
63
71
case "/api/experimental/tasks/me" :
64
72
var req codersdk.CreateTaskRequest
65
73
if ! httpapi .Read (ctx ,w ,r ,& req ) {
@@ -88,71 +96,80 @@ func TestTaskCreate(t *testing.T) {
88
96
tests := []struct {
89
97
args []string
90
98
env []string
99
+ stdin string
91
100
expectError string
92
101
expectOutput string
93
102
handler func (t * testing.T ,ctx context.Context ) http.HandlerFunc
94
103
}{
95
104
{
96
- args : []string {"my-template@my-template-version" ,"--input" ,"my custom prompt" ,"--org" ,organizationID .String ()},
105
+ args : []string {"--stdin" },
106
+ stdin :"reads prompt from stdin" ,
107
+ expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
108
+ handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
109
+ return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"my-template-version" ,"" ,"reads prompt from stdin" )
110
+ },
111
+ },
112
+ {
113
+ args : []string {"my custom prompt" },
97
114
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
98
115
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
99
116
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
100
117
},
101
118
},
102
119
{
103
- args : []string {"my-template" ,"--input" ,"my custom prompt" ,"--org" ,organizationID .String ()},
104
- env : []string {"CODER_TASK_TEMPLATE_VERSION=my-template-version" },
120
+ args : []string {"my custom prompt" ,"--template" ,"my-template" ,"--template-version" ,"my-template-version" ,"--org" ,organizationID .String ()},
105
121
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
106
122
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
107
123
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
108
124
},
109
125
},
110
126
{
111
- args : []string {"--input " ,"my custom prompt " ,"--org" ,organizationID .String ()},
112
- env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" , " CODER_TASK_TEMPLATE_VERSION=my-template-version" },
127
+ args : []string {"my custom prompt" , "--template " ,"my-template " ,"--org" ,organizationID .String ()},
128
+ env : []string {"CODER_TASK_TEMPLATE_VERSION=my-template-version" },
113
129
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
114
130
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
115
131
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
116
132
},
117
133
},
118
134
{
119
- env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" ,"CODER_TASK_TEMPLATE_VERSION=my-template-version" ,"CODER_TASK_INPUT=my custom prompt" ,"CODER_ORGANIZATION=" + organizationID .String ()},
135
+ args : []string {"my custom prompt" ,"--org" ,organizationID .String ()},
136
+ env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" ,"CODER_TASK_TEMPLATE_VERSION=my-template-version" },
120
137
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
121
138
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
122
139
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
123
140
},
124
141
},
125
142
{
126
- args : []string {"my-template " ,"--input " ,"my custom prompt " ,"--org" ,organizationID .String ()},
143
+ args : []string {"my custom prompt " ,"--template " ,"my-template " ,"--org" ,organizationID .String ()},
127
144
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
128
145
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
129
146
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"" ,"" ,"my custom prompt" )
130
147
},
131
148
},
132
149
{
133
- args : []string {"my-template " ,"--input " ,"my custom prompt " ,"--preset" ,"my-preset" ,"--org" ,organizationID .String ()},
150
+ args : []string {"my custom prompt " ,"--template " ,"my-template " ,"--preset" ,"my-preset" ,"--org" ,organizationID .String ()},
134
151
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
135
152
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
136
153
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"" ,"my-preset" ,"my custom prompt" )
137
154
},
138
155
},
139
156
{
140
- args : []string {"my-template " ,"--input " ,"my custom prompt " },
157
+ args : []string {"my custom prompt " ,"--template " ,"my-template " },
141
158
env : []string {"CODER_TASK_PRESET_NAME=my-preset" },
142
159
expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
143
160
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
144
161
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"" ,"my-preset" ,"my custom prompt" )
145
162
},
146
163
},
147
164
{
148
- args : []string {"my-template " ,"--input " ,"my custom prompt " ,"--preset" ,"not-real-preset" },
165
+ args : []string {"my custom prompt " ,"--template " ,"my-template " ,"--preset" ,"not-real-preset" },
149
166
expectError :`preset "not-real-preset" not found` ,
150
167
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
151
168
return templateAndVersionFoundHandler (t ,ctx ,organizationID ,"my-template" ,"" ,"my-preset" ,"my custom prompt" )
152
169
},
153
170
},
154
171
{
155
- args : []string {"my-template@not-real- template-version " ,"--input " ,"my custom prompt " },
172
+ args : []string {"my custom prompt" , "-- template" , "my-template " ,"--template-version " ,"not-real-template-version " },
156
173
expectError :httpapi .ResourceNotFoundResponse .Message ,
157
174
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
158
175
return func (w http.ResponseWriter ,r * http.Request ) {
@@ -163,6 +180,11 @@ func TestTaskCreate(t *testing.T) {
163
180
ID :organizationID ,
164
181
}},
165
182
})
183
+ case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template" ,organizationID ):
184
+ httpapi .Write (ctx ,w ,http .StatusOK , codersdk.Template {
185
+ ID :templateID ,
186
+ ActiveVersionID :templateVersionID ,
187
+ })
166
188
case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template/versions/not-real-template-version" ,organizationID ):
167
189
httpapi .ResourceNotFound (w )
168
190
default :
@@ -172,7 +194,7 @@ func TestTaskCreate(t *testing.T) {
172
194
},
173
195
},
174
196
{
175
- args : []string {"not-real-template " ,"--input " ,"my custom prompt " ,"--org" ,organizationID .String ()},
197
+ args : []string {"my custom prompt " ,"--template " ,"not-real-template " ,"--org" ,organizationID .String ()},
176
198
expectError :httpapi .ResourceNotFoundResponse .Message ,
177
199
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
178
200
return func (w http.ResponseWriter ,r * http.Request ) {
@@ -192,7 +214,7 @@ func TestTaskCreate(t *testing.T) {
192
214
},
193
215
},
194
216
{
195
- args : []string {"template-in-different-org " ,"--input " ,"my-custom-prompt " ,"--org" ,anotherOrganizationID .String ()},
217
+ args : []string {"my-custom-prompt " ,"--template " ,"template-in-different-org " ,"--org" ,anotherOrganizationID .String ()},
196
218
expectError :httpapi .ResourceNotFoundResponse .Message ,
197
219
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
198
220
return func (w http.ResponseWriter ,r * http.Request ) {
@@ -212,7 +234,7 @@ func TestTaskCreate(t *testing.T) {
212
234
},
213
235
},
214
236
{
215
- args : []string {"no-org" , "--input" , "my-custom -prompt" },
237
+ args : []string {"no-org-prompt" },
216
238
expectError :"Must select an organization with --org=<org_name>" ,
217
239
handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
218
240
return func (w http.ResponseWriter ,r * http.Request ) {
@@ -225,6 +247,49 @@ func TestTaskCreate(t *testing.T) {
225
247
}
226
248
},
227
249
},
250
+ {
251
+ args : []string {"no task templates" },
252
+ expectError :"no task templates configured" ,
253
+ handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
254
+ return func (w http.ResponseWriter ,r * http.Request ) {
255
+ switch r .URL .Path {
256
+ case "/api/v2/users/me/organizations" :
257
+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Organization {
258
+ {MinimalOrganization : codersdk.MinimalOrganization {
259
+ ID :organizationID ,
260
+ }},
261
+ })
262
+ case "/api/v2/templates" :
263
+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Template {})
264
+ default :
265
+ t .Errorf ("unexpected path: %s" ,r .URL .Path )
266
+ }
267
+ }
268
+ },
269
+ },
270
+ {
271
+ args : []string {"no template name provided" },
272
+ expectError :"template name not provided, available templates: wibble, wobble" ,
273
+ handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
274
+ return func (w http.ResponseWriter ,r * http.Request ) {
275
+ switch r .URL .Path {
276
+ case "/api/v2/users/me/organizations" :
277
+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Organization {
278
+ {MinimalOrganization : codersdk.MinimalOrganization {
279
+ ID :organizationID ,
280
+ }},
281
+ })
282
+ case "/api/v2/templates" :
283
+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Template {
284
+ {Name :"wibble" },
285
+ {Name :"wobble" },
286
+ })
287
+ default :
288
+ t .Errorf ("unexpected path: %s" ,r .URL .Path )
289
+ }
290
+ }
291
+ },
292
+ },
228
293
}
229
294
230
295
for _ ,tt := range tests {
@@ -244,6 +309,7 @@ func TestTaskCreate(t *testing.T) {
244
309
245
310
inv ,root := clitest .New (t ,append (args ,tt .args ... )... )
246
311
inv .Environ = serpent .ParseEnviron (tt .env ,"" )
312
+ inv .Stdin = strings .NewReader (tt .stdin )
247
313
inv .Stdout = & sb
248
314
inv .Stderr = & sb
249
315
clitest .SetupConfig (t ,client ,root )