Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 373cb23

Browse files
committed
Add unit test for prompt skipping
1 parent 664ea38 commit 373cb23

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

cli/cliui/prompt_test.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cliui_test
22

33
import (
4+
"bytes"
45
"context"
6+
"io"
57
"os"
68
"os/exec"
79
"testing"
@@ -24,7 +26,7 @@ func TestPrompt(t *testing.T) {
2426
go func() {
2527
resp, err := newPrompt(ptty, cliui.PromptOptions{
2628
Text: "Example",
27-
})
29+
}, nil)
2830
require.NoError(t, err)
2931
msgChan <- resp
3032
}()
@@ -41,7 +43,7 @@ func TestPrompt(t *testing.T) {
4143
resp, err := newPrompt(ptty, cliui.PromptOptions{
4244
Text: "Example",
4345
IsConfirm: true,
44-
})
46+
}, nil)
4547
require.NoError(t, err)
4648
doneChan <- resp
4749
}()
@@ -50,14 +52,45 @@ func TestPrompt(t *testing.T) {
5052
require.Equal(t, "yes", <-doneChan)
5153
})
5254

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+
5386
t.Run("JSON", func(t *testing.T) {
5487
t.Parallel()
5588
ptty := ptytest.New(t)
5689
doneChan := make(chan string)
5790
go func() {
5891
resp, err := newPrompt(ptty, cliui.PromptOptions{
5992
Text: "Example",
60-
})
93+
}, nil)
6194
require.NoError(t, err)
6295
doneChan <- resp
6396
}()
@@ -71,9 +104,10 @@ func TestPrompt(t *testing.T) {
71104
ptty := ptytest.New(t)
72105
doneChan := make(chan string)
73106
go func() {
107+
74108
resp, err := newPrompt(ptty, cliui.PromptOptions{
75109
Text: "Example",
76-
})
110+
}, nil)
77111
require.NoError(t, err)
78112
doneChan <- resp
79113
}()
@@ -89,7 +123,7 @@ func TestPrompt(t *testing.T) {
89123
go func() {
90124
resp, err := newPrompt(ptty, cliui.PromptOptions{
91125
Text: "Example",
92-
})
126+
}, nil)
93127
require.NoError(t, err)
94128
doneChan <- resp
95129
}()
@@ -101,7 +135,7 @@ func TestPrompt(t *testing.T) {
101135
})
102136
}
103137

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) {
105139
value := ""
106140
cmd := &cobra.Command{
107141
RunE: func(cmd *cobra.Command, args []string) error {
@@ -110,6 +144,10 @@ func newPrompt(ptty *ptytest.PTY, opts cliui.PromptOptions) (string, error) {
110144
return err
111145
},
112146
}
147+
// Optionally modify the cmd
148+
if cmdOpt != nil {
149+
cmdOpt(cmd)
150+
}
113151
cmd.SetOutput(ptty.Output())
114152
cmd.SetIn(ptty.Input())
115153
return value, cmd.ExecuteContext(context.Background())

0 commit comments

Comments
 (0)