@@ -2,11 +2,14 @@ package cli_test
22
33import (
44 "context"
5+ "fmt"
6+ "strings"
57 "testing"
68
79 "github.com/stretchr/testify/require"
810
911 "github.com/coder/coder/cli/clitest"
12+ "github.com/coder/coder/cli/cliui"
1013 "github.com/coder/coder/coderd/coderdtest"
1114 "github.com/coder/coder/codersdk"
1215 "github.com/coder/coder/pty/ptytest"
@@ -25,14 +28,27 @@ func TestTemplateDelete(t *testing.T) {
2528 template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
2629
2730 cmd , root := clitest .New (t , "templates" , "delete" , template .Name )
31+
2832 clitest .SetupConfig (t , client , root )
29- require .NoError (t , cmd .Execute ())
33+ pty := ptytest .New (t )
34+ cmd .SetIn (pty .Input ())
35+ cmd .SetOut (pty .Output ())
36+
37+ execDone := make (chan error )
38+ go func () {
39+ execDone <- cmd .Execute ()
40+ }()
41+
42+ pty .ExpectMatch (fmt .Sprintf ("Delete these templates: %s?" , cliui .Styles .Code .Render (template .Name )))
43+ pty .WriteLine ("yes" )
44+
45+ require .NoError (t , <- execDone )
3046
3147 _ , err := client .Template (context .Background (), template .ID )
3248 require .Error (t , err , "template should not exist" )
3349 })
3450
35- t .Run ("Multiple" , func (t * testing.T ) {
51+ t .Run ("Multiple --yes " , func (t * testing.T ) {
3652 t .Parallel ()
3753
3854 client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
@@ -49,7 +65,7 @@ func TestTemplateDelete(t *testing.T) {
4965 templateNames = append (templateNames , template .Name )
5066 }
5167
52- cmd , root := clitest .New (t , append ([]string {"templates" , "delete" }, templateNames ... )... )
68+ cmd , root := clitest .New (t , append ([]string {"templates" , "delete" , "--yes" }, templateNames ... )... )
5369 clitest .SetupConfig (t , client , root )
5470 require .NoError (t , cmd .Execute ())
5571
@@ -59,6 +75,45 @@ func TestTemplateDelete(t *testing.T) {
5975 }
6076 })
6177
78+ t .Run ("Multiple prompted" , func (t * testing.T ) {
79+ t .Parallel ()
80+
81+ client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
82+ user := coderdtest .CreateFirstUser (t , client )
83+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
84+ _ = coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
85+ templates := []codersdk.Template {
86+ coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID ),
87+ coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID ),
88+ coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID ),
89+ }
90+ templateNames := []string {}
91+ for _ , template := range templates {
92+ templateNames = append (templateNames , template .Name )
93+ }
94+
95+ cmd , root := clitest .New (t , append ([]string {"templates" , "delete" }, templateNames ... )... )
96+ clitest .SetupConfig (t , client , root )
97+ pty := ptytest .New (t )
98+ cmd .SetIn (pty .Input ())
99+ cmd .SetOut (pty .Output ())
100+
101+ execDone := make (chan error )
102+ go func () {
103+ execDone <- cmd .Execute ()
104+ }()
105+
106+ pty .ExpectMatch (fmt .Sprintf ("Delete these templates: %s?" , cliui .Styles .Code .Render (strings .Join (templateNames , ", " ))))
107+ pty .WriteLine ("yes" )
108+
109+ require .NoError (t , <- execDone )
110+
111+ for _ , template := range templates {
112+ _ , err := client .Template (context .Background (), template .ID )
113+ require .Error (t , err , "template should not exist" )
114+ }
115+ })
116+
62117 t .Run ("Selector" , func (t * testing.T ) {
63118 t .Parallel ()
64119
@@ -80,7 +135,7 @@ func TestTemplateDelete(t *testing.T) {
80135 execDone <- cmd .Execute ()
81136 }()
82137
83- pty .WriteLine ("docker-local " )
138+ pty .WriteLine ("yes " )
84139 require .NoError (t , <- execDone )
85140
86141 _ , err := client .Template (context .Background (), template .ID )
0 commit comments