1
1
package cliui_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "io"
5
7
"os"
6
8
"os/exec"
7
9
"testing"
@@ -24,7 +26,7 @@ func TestPrompt(t *testing.T) {
24
26
go func () {
25
27
resp , err := newPrompt (ptty , cliui.PromptOptions {
26
28
Text : "Example" ,
27
- })
29
+ }, nil )
28
30
require .NoError (t , err )
29
31
msgChan <- resp
30
32
}()
@@ -41,7 +43,7 @@ func TestPrompt(t *testing.T) {
41
43
resp , err := newPrompt (ptty , cliui.PromptOptions {
42
44
Text : "Example" ,
43
45
IsConfirm : true ,
44
- })
46
+ }, nil )
45
47
require .NoError (t , err )
46
48
doneChan <- resp
47
49
}()
@@ -50,14 +52,45 @@ func TestPrompt(t *testing.T) {
50
52
require .Equal (t , "yes" , <- doneChan )
51
53
})
52
54
55
+ t .Run ("Skip" , func (t * testing.T ) {
56
+ t .Parallel ()
57
+ ptty := ptytest .New (t )
58
+ var buf bytes.Buffer
59
+
60
+ // Copy all data written out to a buffer. When we close the ptty, we can
61
+ // no longer read from the ptty.Output(), but we can read what was
62
+ // written to the buffer.
63
+ go func () {
64
+ _ , err := io .Copy (& buf , ptty .Output ())
65
+ require .NoError (t , err , "copy" )
66
+ }()
67
+
68
+ doneChan := make (chan string )
69
+ go func () {
70
+ resp , err := newPrompt (ptty , cliui.PromptOptions {
71
+ Text : "ShouldNotSeeThis" ,
72
+ IsConfirm : true ,
73
+ }, func (cmd * cobra.Command ) {
74
+ cliui .AllowSkipPrompt (cmd )
75
+ cmd .SetArgs ([]string {"-y" })
76
+ })
77
+ require .NoError (t , err )
78
+ doneChan <- resp
79
+ }()
80
+
81
+ require .Equal (t , "yes" , <- doneChan )
82
+ require .NoError (t , ptty .Close (), "close ptty" )
83
+ require .Len (t , buf .Bytes (), 0 , "expect no output" )
84
+ })
85
+
53
86
t .Run ("JSON" , func (t * testing.T ) {
54
87
t .Parallel ()
55
88
ptty := ptytest .New (t )
56
89
doneChan := make (chan string )
57
90
go func () {
58
91
resp , err := newPrompt (ptty , cliui.PromptOptions {
59
92
Text : "Example" ,
60
- })
93
+ }, nil )
61
94
require .NoError (t , err )
62
95
doneChan <- resp
63
96
}()
@@ -71,9 +104,10 @@ func TestPrompt(t *testing.T) {
71
104
ptty := ptytest .New (t )
72
105
doneChan := make (chan string )
73
106
go func () {
107
+
74
108
resp , err := newPrompt (ptty , cliui.PromptOptions {
75
109
Text : "Example" ,
76
- })
110
+ }, nil )
77
111
require .NoError (t , err )
78
112
doneChan <- resp
79
113
}()
@@ -89,7 +123,7 @@ func TestPrompt(t *testing.T) {
89
123
go func () {
90
124
resp , err := newPrompt (ptty , cliui.PromptOptions {
91
125
Text : "Example" ,
92
- })
126
+ }, nil )
93
127
require .NoError (t , err )
94
128
doneChan <- resp
95
129
}()
@@ -101,7 +135,7 @@ func TestPrompt(t *testing.T) {
101
135
})
102
136
}
103
137
104
- func newPrompt (ptty * ptytest.PTY , opts cliui.PromptOptions ) (string , error ) {
138
+ func newPrompt (ptty * ptytest.PTY , opts cliui.PromptOptions , cmdOpt func ( cmd * cobra. Command ) ) (string , error ) {
105
139
value := ""
106
140
cmd := & cobra.Command {
107
141
RunE : func (cmd * cobra.Command , args []string ) error {
@@ -110,6 +144,10 @@ func newPrompt(ptty *ptytest.PTY, opts cliui.PromptOptions) (string, error) {
110
144
return err
111
145
},
112
146
}
147
+ // Optionally modify the cmd
148
+ if cmdOpt != nil {
149
+ cmdOpt (cmd )
150
+ }
113
151
cmd .SetOutput (ptty .Output ())
114
152
cmd .SetIn (ptty .Input ())
115
153
return value , cmd .ExecuteContext (context .Background ())
0 commit comments