@@ -60,6 +60,14 @@ func TestTaskCreate(t *testing.T) {
6060Name :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+ })
6371case "/api/experimental/tasks/me" :
6472var req codersdk.CreateTaskRequest
6573if ! httpapi .Read (ctx ,w ,r ,& req ) {
@@ -88,71 +96,80 @@ func TestTaskCreate(t *testing.T) {
8896tests := []struct {
8997args []string
9098env []string
99+ stdin string
91100expectError string
92101expectOutput string
93102handler 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" },
97114expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
98115handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
99116return 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 ()},
105121expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
106122handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
107123return 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" },
113129expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
114130handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
115131return 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" },
120137expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
121138handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
122139return 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 ()},
127144expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
128145handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
129146return 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 ()},
134151expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
135152handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
136153return 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 " },
141158env : []string {"CODER_TASK_PRESET_NAME=my-preset" },
142159expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
143160handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
144161return 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" },
149166expectError :`preset "not-real-preset" not found` ,
150167handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
151168return 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 " },
156173expectError :httpapi .ResourceNotFoundResponse .Message ,
157174handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
158175return func (w http.ResponseWriter ,r * http.Request ) {
@@ -163,6 +180,11 @@ func TestTaskCreate(t *testing.T) {
163180ID :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+ })
166188case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template/versions/not-real-template-version" ,organizationID ):
167189httpapi .ResourceNotFound (w )
168190default :
@@ -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 ()},
176198expectError :httpapi .ResourceNotFoundResponse .Message ,
177199handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
178200return 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 ()},
196218expectError :httpapi .ResourceNotFoundResponse .Message ,
197219handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
198220return 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" },
216238expectError :"Must select an organization with --org=<org_name>" ,
217239handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
218240return 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
230295for _ ,tt := range tests {
@@ -244,6 +309,7 @@ func TestTaskCreate(t *testing.T) {
244309
245310inv ,root := clitest .New (t ,append (args ,tt .args ... )... )
246311inv .Environ = serpent .ParseEnviron (tt .env ,"" )
312+ inv .Stdin = strings .NewReader (tt .stdin )
247313inv .Stdout = & sb
248314inv .Stderr = & sb
249315clitest .SetupConfig (t ,client ,root )