@@ -106,91 +106,33 @@ func TestPromptForConfirmation(t *testing.T) {
106
106
}()
107
107
108
108
for _ , tc := range []struct {
109
- desc string
110
- f func (* testing.T , context.Context , chan promptResult )
109
+ desc string
110
+ f func () error
111
+ expected promptResult
111
112
}{
112
- {"SIGINT" , func (t * testing.T , ctx context.Context , c chan promptResult ) {
113
- t .Helper ()
114
-
113
+ {"SIGINT" , func () error {
115
114
syscall .Kill (syscall .Getpid (), syscall .SIGINT )
116
-
117
- select {
118
- case <- ctx .Done ():
119
- t .Fatal ("PromptForConfirmation did not return after SIGINT" )
120
- case r := <- c :
121
- assert .Check (t , ! r .result )
122
- assert .ErrorContains (t , r .err , "prompt terminated" )
123
- }
124
- }},
125
- {"no" , func (t * testing.T , ctx context.Context , c chan promptResult ) {
126
- t .Helper ()
127
-
115
+ return nil
116
+ }, promptResult {false , command .ErrPromptTerminated }},
117
+ {"no" , func () error {
128
118
_ , err := fmt .Fprint (promptWriter , "n\n " )
129
- assert .NilError (t , err )
130
-
131
- select {
132
- case <- ctx .Done ():
133
- t .Fatal ("PromptForConfirmation did not return after user input `n`" )
134
- case r := <- c :
135
- assert .Check (t , ! r .result )
136
- assert .NilError (t , r .err )
137
- }
138
- }},
139
- {"yes" , func (t * testing.T , ctx context.Context , c chan promptResult ) {
140
- t .Helper ()
141
-
119
+ return err
120
+ }, promptResult {false , nil }},
121
+ {"yes" , func () error {
142
122
_ , err := fmt .Fprint (promptWriter , "y\n " )
143
- assert .NilError (t , err )
144
-
145
- select {
146
- case <- ctx .Done ():
147
- t .Fatal ("PromptForConfirmation did not return after user input `y`" )
148
- case r := <- c :
149
- assert .Check (t , r .result )
150
- assert .NilError (t , r .err )
151
- }
152
- }},
153
- {"any" , func (t * testing.T , ctx context.Context , c chan promptResult ) {
154
- t .Helper ()
155
-
123
+ return err
124
+ }, promptResult {true , nil }},
125
+ {"any" , func () error {
156
126
_ , err := fmt .Fprint (promptWriter , "a\n " )
157
- assert .NilError (t , err )
158
-
159
- select {
160
- case <- ctx .Done ():
161
- t .Fatal ("PromptForConfirmation did not return after user input `a`" )
162
- case r := <- c :
163
- assert .Check (t , ! r .result )
164
- assert .NilError (t , r .err )
165
- }
166
- }},
167
- {"with space" , func (t * testing.T , ctx context.Context , c chan promptResult ) {
168
- t .Helper ()
169
-
127
+ return err
128
+ }, promptResult {false , nil }},
129
+ {"with space" , func () error {
170
130
_ , err := fmt .Fprint (promptWriter , " y\n " )
171
- assert .NilError (t , err )
172
-
173
- select {
174
- case <- ctx .Done ():
175
- t .Fatal ("PromptForConfirmation did not return after user input ` y`" )
176
- case r := <- c :
177
- assert .Check (t , r .result )
178
- assert .NilError (t , r .err )
179
- }
180
- }},
181
- {"reader closed" , func (t * testing.T , ctx context.Context , c chan promptResult ) {
182
- t .Helper ()
183
-
184
- assert .NilError (t , promptReader .Close ())
185
-
186
- select {
187
- case <- ctx .Done ():
188
- t .Fatal ("PromptForConfirmation did not return after promptReader was closed" )
189
- case r := <- c :
190
- assert .Check (t , ! r .result )
191
- assert .NilError (t , r .err )
192
- }
193
- }},
131
+ return err
132
+ }, promptResult {true , nil }},
133
+ {"reader closed" , func () error {
134
+ return promptReader .Close ()
135
+ }, promptResult {false , nil }},
194
136
} {
195
137
t .Run ("case=" + tc .desc , func (t * testing.T ) {
196
138
buf .Reset ()
@@ -207,17 +149,25 @@ func TestPromptForConfirmation(t *testing.T) {
207
149
result <- promptResult {r , err }
208
150
}()
209
151
210
- // wait for the Prompt to write to the buffer
211
- pollForPromptOutput (ctx , t , wroteHook )
212
- drainChannel (ctx , wroteHook )
152
+ select {
153
+ case <- time .After (100 * time .Millisecond ):
154
+ case <- wroteHook :
155
+ }
213
156
214
157
assert .NilError (t , bufioWriter .Flush ())
215
158
assert .Equal (t , strings .TrimSpace (buf .String ()), "Are you sure you want to proceed? [y/N]" )
216
159
217
- resultCtx , resultCancel := context .WithTimeout (ctx , 500 * time .Millisecond )
218
- defer resultCancel ()
160
+ // wait for the Prompt to write to the buffer
161
+ drainChannel (ctx , wroteHook )
162
+
163
+ assert .NilError (t , tc .f ())
219
164
220
- tc .f (t , resultCtx , result )
165
+ select {
166
+ case <- time .After (500 * time .Millisecond ):
167
+ t .Fatal ("timeout waiting for prompt result" )
168
+ case r := <- result :
169
+ assert .Equal (t , r , tc .expected )
170
+ }
221
171
})
222
172
}
223
173
}
@@ -233,20 +183,3 @@ func drainChannel(ctx context.Context, ch <-chan struct{}) {
233
183
}
234
184
}()
235
185
}
236
-
237
- func pollForPromptOutput (ctx context.Context , t * testing.T , wroteHook <- chan struct {}) {
238
- t .Helper ()
239
-
240
- ctx , cancel := context .WithTimeout (ctx , 100 * time .Millisecond )
241
- defer cancel ()
242
-
243
- for {
244
- select {
245
- case <- ctx .Done ():
246
- t .Fatal ("Prompt output was not written to before ctx was cancelled" )
247
- return
248
- case <- wroteHook :
249
- return
250
- }
251
- }
252
- }
0 commit comments