-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey.go
211 lines (160 loc) · 5.03 KB
/
survey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package bootstrap
import (
"errors"
"fmt"
"sync"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/cucumber/godog"
"github.com/stretchr/testify/assert"
"go.nhat.io/surveyexpect/options"
)
// Prompt produces prompts and validate the answers.
type Prompt struct {
stdio terminal.Stdio
mu sync.Mutex
}
// RegisterSteps register Prompt to a *godog.ScenarioContext.
func (p *Prompt) RegisterSteps(s *godog.ScenarioContext) {
s.Step(`ask for confirm "([^"]*)" with help "([^"]*)", receive yes`, p.askConfirmWithHelpYes)
s.Step(`ask for confirm "([^"]*)" with help "([^"]*)", receive no`, p.askConfirmWithHelpNo)
s.Step(`ask for confirm "([^"]*)", receive yes`, p.askConfirmWithoutHelpYes)
s.Step(`ask for confirm "([^"]*)", receive no`, p.askConfirmWithoutHelpNo)
s.Step(`ask for confirm "([^"]*)", get interrupted`, p.askConfirmInterrupted)
s.Step(`ask for multiline "([^"]*)", receive:`, p.askMultilineWithAnswer)
s.Step(`ask for multiline "([^"]*)", get interrupted`, p.askMultilineInterrupted)
s.Step(`ask for password "([^"]*)" with help "([^"]*)", receive "([^"]*)"`, p.askPasswordWithHelp)
s.Step(`ask for password "([^"]*)", receive "([^"]*)"`, p.askPasswordWithoutHelp)
s.Step(`ask for password "([^"]*)", get interrupted`, p.askPasswordInterrupted)
}
// WithStdio configures stdio for a given scenario.
func (p *Prompt) WithStdio(_ *godog.Scenario, stdio terminal.Stdio) {
p.mu.Lock()
defer p.mu.Unlock()
p.stdio = stdio
}
// Stdio returns the current stdio.
func (p *Prompt) Stdio() terminal.Stdio {
p.mu.Lock()
defer p.mu.Unlock()
return p.stdio
}
func (p *Prompt) ask(prompt survey.Prompt, response interface{}) (err error) {
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
err = survey.AskOne(prompt, response, options.WithStdio(p.Stdio()))
}()
select {
case <-time.After(time.Second):
return errors.New("ask timed out") // nolint: goerr113
case <-doneCh:
return nil
}
}
func (p *Prompt) askConfirm(message, help string) (bool, error) {
prompt := &survey.Confirm{
Message: message,
Help: help,
}
var answer bool
err := p.ask(prompt, &answer)
return answer, err
}
func (p *Prompt) askConfirmWithHelp(message, help string, expectedAnswer bool) error {
answer, err := p.askConfirm(message, help)
if err != nil {
return err
}
if !assert.ObjectsAreEqual(expectedAnswer, answer) {
return fmt.Errorf("expected answer: %t, got %t", expectedAnswer, answer) // nolint: goerr113
}
return nil
}
func (p *Prompt) askConfirmWithHelpYes(message, help string) error {
return p.askConfirmWithHelp(message, help, true)
}
func (p *Prompt) askConfirmWithHelpNo(message, help string) error {
return p.askConfirmWithHelp(message, help, false)
}
func (p *Prompt) askConfirmWithoutHelpYes(message string) error {
return p.askConfirmWithHelp(message, "", true)
}
func (p *Prompt) askConfirmWithoutHelpNo(message string) error {
return p.askConfirmWithHelp(message, "", false)
}
func (p *Prompt) askConfirmInterrupted(message string) error {
answer, err := p.askConfirm(message, "")
if answer {
return fmt.Errorf("unexpected answer: %t", answer) // nolint: goerr113
}
if !errors.Is(err, terminal.InterruptErr) {
return err
}
return nil
}
func (p *Prompt) askMultiline(message string) (string, error) {
prompt := &survey.Multiline{
Message: message,
}
var answer string
err := p.ask(prompt, &answer)
return answer, err
}
func (p *Prompt) askMultilineWithAnswer(message string, expected *godog.DocString) error {
answer, err := p.askMultiline(message)
if err != nil {
return err
}
if !assert.ObjectsAreEqual(expected.Content, answer) {
return fmt.Errorf("expected answer: %s, got %s", expected.Content, answer) // nolint: goerr113
}
return nil
}
func (p *Prompt) askMultilineInterrupted(message string) error {
answer, err := p.askMultiline(message)
if answer != "" {
return fmt.Errorf("unexpected answer: %s", answer) // nolint: goerr113
}
if !errors.Is(err, terminal.InterruptErr) {
return err
}
return nil
}
func (p *Prompt) askPassword(message, help string) (string, error) {
prompt := &survey.Password{
Message: message,
Help: help,
}
var answer string
err := p.ask(prompt, &answer)
return answer, err
}
func (p *Prompt) askPasswordWithHelp(message, help, expectedAnswer string) error {
answer, err := p.askPassword(message, help)
if err != nil {
return err
}
if !assert.ObjectsAreEqual(expectedAnswer, answer) {
return fmt.Errorf("expected answer: %s, got %s", expectedAnswer, answer) // nolint: goerr113
}
return nil
}
func (p *Prompt) askPasswordWithoutHelp(message, expectedAnswer string) error {
return p.askPasswordWithHelp(message, "", expectedAnswer)
}
func (p *Prompt) askPasswordInterrupted(message string) error {
answer, err := p.askPassword(message, "")
if answer != "" {
return fmt.Errorf("unexpected answer: %s", answer) // nolint: goerr113
}
if !errors.Is(err, terminal.InterruptErr) {
return err
}
return nil
}
// NewPrompt initiates a new *Prompt.
func NewPrompt() *Prompt {
return &Prompt{}
}