Skip to content

Commit c839d3b

Browse files
committed
Check for unused ask stubs at the end of the test
1 parent 8e64c14 commit c839d3b

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

pkg/cmd/gist/view/view_test.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ func Test_viewRun(t *testing.T) {
355355
)),
356356
)
357357

358-
as, surveyteardown := prompt.InitAskStubber()
359-
defer surveyteardown()
360-
as.StubOne(0)
358+
as, surveyteardown := prompt.NewAskStubber()
359+
defer surveyteardown(t)
360+
as.StubPrompt("Select a gist").AnswerDefault()
361361
}
362362

363363
if tt.opts == nil {
@@ -392,16 +392,18 @@ func Test_viewRun(t *testing.T) {
392392

393393
func Test_promptGists(t *testing.T) {
394394
tests := []struct {
395-
name string
396-
gistIndex int
397-
response string
398-
wantOut string
399-
gist *shared.Gist
400-
wantErr bool
395+
name string
396+
askStubs func(as *prompt.AskStubber)
397+
response string
398+
wantOut string
399+
gist *shared.Gist
400+
wantErr bool
401401
}{
402402
{
403-
name: "multiple files, select first gist",
404-
gistIndex: 0,
403+
name: "multiple files, select first gist",
404+
askStubs: func(as *prompt.AskStubber) {
405+
as.StubPrompt("Select a gist").AnswerWith("cool.txt about 6 hours ago")
406+
},
405407
response: `{ "data": { "viewer": { "gists": { "nodes": [
406408
{
407409
"name": "gistid1",
@@ -421,8 +423,10 @@ func Test_promptGists(t *testing.T) {
421423
wantOut: "gistid1",
422424
},
423425
{
424-
name: "multiple files, select second gist",
425-
gistIndex: 1,
426+
name: "multiple files, select second gist",
427+
askStubs: func(as *prompt.AskStubber) {
428+
as.StubPrompt("Select a gist").AnswerWith("gistfile0.txt about 6 hours ago")
429+
},
426430
response: `{ "data": { "viewer": { "gists": { "nodes": [
427431
{
428432
"name": "gistid1",
@@ -465,11 +469,13 @@ func Test_promptGists(t *testing.T) {
465469
)
466470
client := &http.Client{Transport: reg}
467471

468-
as, surveyteardown := prompt.InitAskStubber()
469-
defer surveyteardown()
470-
as.StubOne(tt.gistIndex)
471-
472472
t.Run(tt.name, func(t *testing.T) {
473+
as, surveyteardown := prompt.NewAskStubber()
474+
defer surveyteardown(t)
475+
if tt.askStubs != nil {
476+
tt.askStubs(as)
477+
}
478+
473479
gistID, err := promptGists(client, "github.com", io.ColorScheme())
474480
assert.NoError(t, err)
475481
assert.Equal(t, tt.wantOut, gistID)

pkg/cmd/pr/review/review_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,27 +309,11 @@ func TestPRReview_interactive_no_body(t *testing.T) {
309309

310310
shared.RunCommandFinder("", &api.PullRequest{ID: "THE-ID", Number: 123}, ghrepo.New("OWNER", "REPO"))
311311

312-
as, teardown := prompt.InitAskStubber()
313-
defer teardown()
312+
as, teardown := prompt.NewAskStubber()
313+
defer teardown(t)
314314

315-
as.Stub([]*prompt.QuestionStub{
316-
{
317-
Name: "reviewType",
318-
Value: "Request changes",
319-
},
320-
})
321-
as.Stub([]*prompt.QuestionStub{
322-
{
323-
Name: "body",
324-
Default: true,
325-
},
326-
})
327-
as.Stub([]*prompt.QuestionStub{
328-
{
329-
Name: "confirm",
330-
Value: true,
331-
},
332-
})
315+
as.StubPrompt("What kind of review do you want to give?").AnswerWith("Request changes")
316+
as.StubPrompt("Review body").AnswerWith("")
333317

334318
_, err := runCommand(http, nil, true, "")
335319
assert.EqualError(t, err, "this type of review cannot be blank")

pkg/cmd/workflow/enable/enable_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestEnableRun(t *testing.T) {
121121
httpmock.StatusStringResponse(204, "{}"))
122122
},
123123
askStubs: func(as *prompt.AskStubber) {
124-
as.StubOne(0)
124+
as.StubPrompt("Select a workflow").AnswerWith("a disabled workflow (disabled.yml)")
125125
},
126126
wantOut: "✓ Enabled a disabled workflow\n",
127127
},
@@ -176,7 +176,7 @@ func TestEnableRun(t *testing.T) {
176176
httpmock.StatusStringResponse(204, "{}"))
177177
},
178178
askStubs: func(as *prompt.AskStubber) {
179-
as.StubOne(1)
179+
as.StubPrompt("Which workflow do you mean?").AnswerWith("a disabled workflow (anotherDisabled.yml)")
180180
},
181181
wantOut: "✓ Enabled a disabled workflow\n",
182182
},
@@ -194,9 +194,6 @@ func TestEnableRun(t *testing.T) {
194194
httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"),
195195
httpmock.StatusStringResponse(204, "{}"))
196196
},
197-
askStubs: func(as *prompt.AskStubber) {
198-
as.StubOne(0)
199-
},
200197
wantOut: "✓ Enabled a disabled workflow\n",
201198
},
202199
{
@@ -279,13 +276,13 @@ func TestEnableRun(t *testing.T) {
279276
return ghrepo.FromFullName("OWNER/REPO")
280277
}
281278

282-
as, teardown := prompt.InitAskStubber()
283-
defer teardown()
284-
if tt.askStubs != nil {
285-
tt.askStubs(as)
286-
}
287-
288279
t.Run(tt.name, func(t *testing.T) {
280+
as, teardown := prompt.NewAskStubber()
281+
defer teardown(t)
282+
if tt.askStubs != nil {
283+
tt.askStubs(as)
284+
}
285+
289286
err := runEnable(tt.opts)
290287
if tt.wantErr {
291288
assert.Error(t, err)

pkg/prompt/stubber.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ type AskStubber struct {
1313
stubs []*QuestionStub
1414
}
1515

16-
func InitAskStubber() (*AskStubber, func()) {
16+
type testing interface {
17+
Errorf(format string, args ...interface{})
18+
}
19+
20+
func NewAskStubber() (*AskStubber, func(t testing)) {
1721
origSurveyAsk := SurveyAsk
1822
origSurveyAskOne := SurveyAskOne
1923
as := AskStubber{}
@@ -120,13 +124,29 @@ func InitAskStubber() (*AskStubber, func()) {
120124
return nil
121125
}
122126

123-
teardown := func() {
127+
teardown := func(t testing) {
124128
SurveyAsk = origSurveyAsk
125129
SurveyAskOne = origSurveyAskOne
130+
for _, s := range as.stubs {
131+
if !s.matched {
132+
if t == nil {
133+
panic(fmt.Sprintf("unmatched prompt stub: %+v", s))
134+
} else {
135+
t.Errorf("unmatched prompt stub: %+v", s)
136+
}
137+
}
138+
}
126139
}
127140
return &as, teardown
128141
}
129142

143+
func InitAskStubber() (*AskStubber, func()) {
144+
as, teardown := NewAskStubber()
145+
return as, func() {
146+
teardown(nil)
147+
}
148+
}
149+
130150
type QuestionStub struct {
131151
Name string
132152
Value interface{}

0 commit comments

Comments
 (0)