-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
228 lines (170 loc) · 5.91 KB
/
manager.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package surveysteps
import (
"context"
"fmt"
"sync"
"time"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/Netflix/go-expect"
"github.com/cucumber/godog"
"github.com/stretchr/testify/assert"
"go.nhat.io/consolesteps"
"go.nhat.io/surveyexpect"
)
// Starter is a callback when survey starts.
type Starter func(sc *godog.Scenario, stdio terminal.Stdio)
// Manager is a wrapper around *surveyexpect.Survey to make it run with cucumber/godog.
type Manager struct {
console *consolesteps.Manager
surveys map[string]*Survey
current string
starters []Starter
test surveyexpect.TestingT
mu sync.Mutex
options []surveyexpect.ExpectOption
}
func (m *Manager) registerConsole(ctx *godog.ScenarioContext) {
if m.console != nil {
return
}
console := consolesteps.New(m.test)
m.attach(console)
// Manage state.
ctx.Before(func(_ context.Context, sc *godog.Scenario) (context.Context, error) {
console.NewConsole(sc)
return nil, nil
})
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
console.CloseConsole(sc)
return nil, nil
})
}
// RegisterContext register the survey to a *godog.ScenarioContext.
//
// Deprecated: Use Manager.RegisterSteps instead.
func (m *Manager) RegisterContext(s *godog.ScenarioContext) {
m.RegisterSteps(s)
}
// RegisterSteps register the survey to a *godog.ScenarioContext.
func (m *Manager) RegisterSteps(s *godog.ScenarioContext) {
m.registerConsole(s)
// Confirm prompt
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* answers? yes`, m.expectConfirmYes)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* answers? no`, m.expectConfirmNo)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* answers? "([^"]*)"`, m.expectConfirmAnswer)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* interrupts?`, m.expectConfirmInterrupt)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? confirm prompt "([^"]*)".* asks? for help and sees? "([^"]*)"`, m.expectConfirmHelp)
// Multiline prompt.
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? multiline prompt "([^"]*)".* answers? "([^"]*)"`, m.expectMultilineAnswer)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? multiline prompt "([^"]*)".* answers?:`, m.expectMultilineAnswerMultiline)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? multiline prompt "([^"]*)".* interrupts?`, m.expectMultilineInterrupt)
// Password prompt.
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? password prompt "([^"]*)".* answers? "([^"]*)"`, m.expectPasswordAnswer)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? password prompt "([^"]*)".* interrupts?`, m.expectPasswordInterrupt)
s.Step(`(?:(?:get)|(?:see))s? a(?:nother)? password prompt "([^"]*)".* asks? for help and sees? "([^"]*)"`, m.expectPasswordHelp)
}
func (m *Manager) start(sc *godog.Scenario, console *expect.Console) {
m.mu.Lock()
defer m.mu.Unlock()
s := NewSurvey(m.test, m.options...).Start(console)
m.current = sc.Id
m.surveys[m.current] = s
for _, start := range m.starters {
start(sc, terminal.Stdio{
In: console.Tty(),
Out: console.Tty(),
Err: console.Tty(),
})
}
}
func (m *Manager) close(sc *godog.Scenario) {
m.mu.Lock()
defer m.mu.Unlock()
if s := m.surveys[sc.Id]; s != nil {
s.Close()
delete(m.surveys, sc.Id)
assert.NoError(m.test, m.expectationsWereMet(sc.Name, s)) //nolint: testifylint
}
m.current = ""
}
func (m *Manager) survey() *Survey {
m.mu.Lock()
defer m.mu.Unlock()
return m.surveys[m.current]
}
func (m *Manager) expectConfirmYes(message string) error {
m.survey().ExpectConfirm(message).Yes()
return nil
}
func (m *Manager) expectConfirmNo(message string) error {
m.survey().ExpectConfirm(message).No()
return nil
}
func (m *Manager) expectConfirmAnswer(message, answer string) error {
m.survey().ExpectConfirm(message).Answer(answer)
return nil
}
func (m *Manager) expectConfirmInterrupt(message string) error {
m.survey().ExpectConfirm(message).Interrupt()
return nil
}
func (m *Manager) expectConfirmHelp(message, help string) error {
m.survey().ExpectConfirm(message).ShowHelp(help)
return nil
}
func (m *Manager) expectMultilineAnswer(message string, answer string) error {
m.survey().ExpectMultiline(message).Answer(answer)
return nil
}
func (m *Manager) expectMultilineAnswerMultiline(message string, answer *godog.DocString) error {
return m.expectMultilineAnswer(message, answer.Content)
}
func (m *Manager) expectMultilineInterrupt(message string) error {
m.survey().ExpectMultiline(message).Interrupt()
return nil
}
func (m *Manager) expectPasswordAnswer(message, answer string) error {
m.survey().ExpectPassword(message).Answer(answer)
return nil
}
func (m *Manager) expectPasswordInterrupt(message string) error {
m.survey().ExpectPassword(message).Interrupt()
return nil
}
func (m *Manager) expectPasswordHelp(message, help string) error {
m.survey().ExpectPassword(message).ShowHelp(help)
return nil
}
// expectationsWereMet checks whether all queued expectations were met in order.
// If any of them was not met - an error is returned.
func (m *Manager) expectationsWereMet(scenario string, s *Survey) error {
<-time.After(surveyexpect.ReactionTime)
err := s.ExpectationsWereMet()
if err == nil {
return nil
}
return fmt.Errorf("in scenario %q, %w", scenario, err)
}
func (m *Manager) attach(console *consolesteps.Manager) *consolesteps.Manager {
return console.
WithStarter(m.start).
WithCloser(m.close)
}
// WithConsole sets console manager.
func (m *Manager) WithConsole(console *consolesteps.Manager) *Manager {
m.console = m.attach(console)
return m
}
// WithStarter adds a mew Starter to Manager.
func (m *Manager) WithStarter(s Starter) *Manager {
m.starters = append(m.starters, s)
return m
}
// New initiates a new *surveysteps.Manager.
func New(t surveyexpect.TestingT, options ...surveyexpect.ExpectOption) *Manager {
return &Manager{
surveys: make(map[string]*Survey),
options: options,
test: t,
}
}