5
5
"testing"
6
6
7
7
"github.com/stretchr/testify/assert"
8
+ "github.com/stretchr/testify/require"
8
9
9
10
"github.com/coder/coder/cli/clitest"
10
11
"github.com/coder/coder/cli/config"
@@ -21,8 +22,8 @@ func TestLogout(t *testing.T) {
21
22
config := login (t , pty )
22
23
23
24
// ensure session files exist
24
- assert .FileExists (t , string (config .URL ()))
25
- assert .FileExists (t , string (config .Session ()))
25
+ require .FileExists (t , string (config .URL ()))
26
+ require .FileExists (t , string (config .Session ()))
26
27
27
28
logoutChan := make (chan struct {})
28
29
logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
@@ -37,6 +38,34 @@ func TestLogout(t *testing.T) {
37
38
assert .NoFileExists (t , string (config .Session ()))
38
39
}()
39
40
41
+ pty .ExpectMatch ("Are you sure you want to logout?" )
42
+ pty .WriteLine ("yes" )
43
+ pty .ExpectMatch ("Successfully logged out" )
44
+ <- logoutChan
45
+ })
46
+ t .Run ("SkipPrompt" , func (t * testing.T ) {
47
+ t .Parallel ()
48
+
49
+ pty := ptytest .New (t )
50
+ config := login (t , pty )
51
+
52
+ // ensure session files exist
53
+ require .FileExists (t , string (config .URL ()))
54
+ require .FileExists (t , string (config .Session ()))
55
+
56
+ logoutChan := make (chan struct {})
57
+ logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ), "-y" )
58
+ logout .SetIn (pty .Input ())
59
+ logout .SetOut (pty .Output ())
60
+
61
+ go func () {
62
+ defer close (logoutChan )
63
+ err := logout .Execute ()
64
+ assert .NoError (t , err )
65
+ assert .NoFileExists (t , string (config .URL ()))
66
+ assert .NoFileExists (t , string (config .Session ()))
67
+ }()
68
+
40
69
pty .ExpectMatch ("Successfully logged out" )
41
70
<- logoutChan
42
71
})
@@ -47,10 +76,11 @@ func TestLogout(t *testing.T) {
47
76
config := login (t , pty )
48
77
49
78
// ensure session files exist
50
- assert .FileExists (t , string (config .URL ()))
51
- assert .FileExists (t , string (config .Session ()))
79
+ require .FileExists (t , string (config .URL ()))
80
+ require .FileExists (t , string (config .Session ()))
52
81
53
- os .RemoveAll (string (config .URL ()))
82
+ err := os .Remove (string (config .URL ()))
83
+ require .NoError (t , err )
54
84
55
85
logoutChan := make (chan struct {})
56
86
logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
@@ -66,8 +96,9 @@ func TestLogout(t *testing.T) {
66
96
assert .NoFileExists (t , string (config .Session ()))
67
97
}()
68
98
99
+ pty .ExpectMatch ("Are you sure you want to logout?" )
100
+ pty .WriteLine ("yes" )
69
101
pty .ExpectMatch ("You are not logged in. Try logging in using 'coder login <url>'." )
70
- pty .ExpectMatch ("Successfully logged out" )
71
102
<- logoutChan
72
103
})
73
104
t .Run ("NoSessionFile" , func (t * testing.T ) {
@@ -77,10 +108,11 @@ func TestLogout(t *testing.T) {
77
108
config := login (t , pty )
78
109
79
110
// ensure session files exist
80
- assert .FileExists (t , string (config .URL ()))
81
- assert .FileExists (t , string (config .Session ()))
111
+ require .FileExists (t , string (config .URL ()))
112
+ require .FileExists (t , string (config .Session ()))
82
113
83
- os .RemoveAll (string (config .Session ()))
114
+ err := os .Remove (string (config .Session ()))
115
+ require .NoError (t , err )
84
116
85
117
logoutChan := make (chan struct {})
86
118
logout , _ := clitest .New (t , "logout" , "--global-config" , string (config ))
@@ -90,14 +122,15 @@ func TestLogout(t *testing.T) {
90
122
91
123
go func () {
92
124
defer close (logoutChan )
93
- err : = logout .Execute ()
125
+ err = logout .Execute ()
94
126
assert .NoError (t , err )
95
127
assert .NoFileExists (t , string (config .URL ()))
96
128
assert .NoFileExists (t , string (config .Session ()))
97
129
}()
98
130
131
+ pty .ExpectMatch ("Are you sure you want to logout?" )
132
+ pty .WriteLine ("yes" )
99
133
pty .ExpectMatch ("You are not logged in. Try logging in using 'coder login <url>'." )
100
- pty .ExpectMatch ("Successfully logged out" )
101
134
<- logoutChan
102
135
})
103
136
}
0 commit comments