@@ -2,11 +2,14 @@ package cli_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "strings"
5
7
"testing"
6
8
7
9
"github.com/stretchr/testify/require"
8
10
9
11
"github.com/coder/coder/cli/clitest"
12
+ "github.com/coder/coder/cli/cliui"
10
13
"github.com/coder/coder/coderd/coderdtest"
11
14
"github.com/coder/coder/codersdk"
12
15
"github.com/coder/coder/pty/ptytest"
@@ -25,14 +28,27 @@ func TestTemplateDelete(t *testing.T) {
25
28
template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
26
29
27
30
cmd , root := clitest .New (t , "templates" , "delete" , template .Name )
31
+
28
32
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 )
30
46
31
47
_ , err := client .Template (context .Background (), template .ID )
32
48
require .Error (t , err , "template should not exist" )
33
49
})
34
50
35
- t .Run ("Multiple" , func (t * testing.T ) {
51
+ t .Run ("Multiple --yes " , func (t * testing.T ) {
36
52
t .Parallel ()
37
53
38
54
client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerD : true })
@@ -49,7 +65,7 @@ func TestTemplateDelete(t *testing.T) {
49
65
templateNames = append (templateNames , template .Name )
50
66
}
51
67
52
- cmd , root := clitest .New (t , append ([]string {"templates" , "delete" }, templateNames ... )... )
68
+ cmd , root := clitest .New (t , append ([]string {"templates" , "delete" , "--yes" }, templateNames ... )... )
53
69
clitest .SetupConfig (t , client , root )
54
70
require .NoError (t , cmd .Execute ())
55
71
@@ -59,6 +75,45 @@ func TestTemplateDelete(t *testing.T) {
59
75
}
60
76
})
61
77
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
+
62
117
t .Run ("Selector" , func (t * testing.T ) {
63
118
t .Parallel ()
64
119
@@ -80,7 +135,7 @@ func TestTemplateDelete(t *testing.T) {
80
135
execDone <- cmd .Execute ()
81
136
}()
82
137
83
- pty .WriteLine ("docker-local " )
138
+ pty .WriteLine ("yes " )
84
139
require .NoError (t , <- execDone )
85
140
86
141
_ , err := client .Template (context .Background (), template .ID )
0 commit comments