@@ -60,6 +60,14 @@ func TestTaskCreate(t *testing.T) {
6060 Name : presetName ,
6161 },
6262 })
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+ })
6371 case "/api/experimental/tasks/me" :
6472 var req codersdk.CreateTaskRequest
6573 if ! httpapi .Read (ctx , w , r , & req ) {
@@ -88,71 +96,80 @@ func TestTaskCreate(t *testing.T) {
8896 tests := []struct {
8997 args []string
9098 env []string
99+ stdin string
91100 expectError string
92101 expectOutput string
93102 handler func (t * testing.T , ctx context.Context ) http.HandlerFunc
94103 }{
95104 {
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" },
97114 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
98115 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
99116 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" )
100117 },
101118 },
102119 {
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 ()},
105121 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
106122 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
107123 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" )
108124 },
109125 },
110126 {
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" },
113129 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
114130 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
115131 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" )
116132 },
117133 },
118134 {
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" },
120137 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
121138 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
122139 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "my-template-version" , "" , "my custom prompt" )
123140 },
124141 },
125142 {
126- args : []string {"my-template " , "--input " , "my custom prompt " , "--org" , organizationID .String ()},
143+ args : []string {"my custom prompt " , "--template " , "my-template " , "--org" , organizationID .String ()},
127144 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
128145 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
129146 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "" , "" , "my custom prompt" )
130147 },
131148 },
132149 {
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 ()},
134151 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
135152 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
136153 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "" , "my-preset" , "my custom prompt" )
137154 },
138155 },
139156 {
140- args : []string {"my-template " , "--input " , "my custom prompt " },
157+ args : []string {"my custom prompt " , "--template " , "my-template " },
141158 env : []string {"CODER_TASK_PRESET_NAME=my-preset" },
142159 expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
143160 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
144161 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "" , "my-preset" , "my custom prompt" )
145162 },
146163 },
147164 {
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" },
149166 expectError : `preset "not-real-preset" not found` ,
150167 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
151168 return templateAndVersionFoundHandler (t , ctx , organizationID , "my-template" , "" , "my-preset" , "my custom prompt" )
152169 },
153170 },
154171 {
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 " },
156173 expectError : httpapi .ResourceNotFoundResponse .Message ,
157174 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
158175 return func (w http.ResponseWriter , r * http.Request ) {
@@ -163,6 +180,11 @@ func TestTaskCreate(t *testing.T) {
163180 ID : organizationID ,
164181 }},
165182 })
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+ })
166188 case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template/versions/not-real-template-version" , organizationID ):
167189 httpapi .ResourceNotFound (w )
168190 default :
@@ -172,7 +194,7 @@ func TestTaskCreate(t *testing.T) {
172194 },
173195 },
174196 {
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 ()},
176198 expectError : httpapi .ResourceNotFoundResponse .Message ,
177199 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
178200 return func (w http.ResponseWriter , r * http.Request ) {
@@ -192,7 +214,7 @@ func TestTaskCreate(t *testing.T) {
192214 },
193215 },
194216 {
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 ()},
196218 expectError : httpapi .ResourceNotFoundResponse .Message ,
197219 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
198220 return func (w http.ResponseWriter , r * http.Request ) {
@@ -212,7 +234,7 @@ func TestTaskCreate(t *testing.T) {
212234 },
213235 },
214236 {
215- args : []string {"no-org" , "--input" , "my-custom -prompt" },
237+ args : []string {"no-org-prompt" },
216238 expectError : "Must select an organization with --org=<org_name>" ,
217239 handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
218240 return func (w http.ResponseWriter , r * http.Request ) {
@@ -225,6 +247,49 @@ func TestTaskCreate(t *testing.T) {
225247 }
226248 },
227249 },
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+ },
228293 }
229294
230295 for _ , tt := range tests {
@@ -244,6 +309,7 @@ func TestTaskCreate(t *testing.T) {
244309
245310 inv , root := clitest .New (t , append (args , tt .args ... )... )
246311 inv .Environ = serpent .ParseEnviron (tt .env , "" )
312+ inv .Stdin = strings .NewReader (tt .stdin )
247313 inv .Stdout = & sb
248314 inv .Stderr = & sb
249315 clitest .SetupConfig (t , client , root )
0 commit comments