From c839d3ba1def7cf396052f2f0cd672514673e426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 13 Jan 2022 12:23:42 +0100 Subject: [PATCH] Check for unused ask stubs at the end of the test --- pkg/cmd/gist/view/view_test.go | 40 +++++++++++++++----------- pkg/cmd/pr/review/review_test.go | 24 +++------------- pkg/cmd/workflow/enable/enable_test.go | 19 ++++++------ pkg/prompt/stubber.go | 24 ++++++++++++++-- 4 files changed, 57 insertions(+), 50 deletions(-) diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index cc58867cf..dbacd9101 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -355,9 +355,9 @@ func Test_viewRun(t *testing.T) { )), ) - as, surveyteardown := prompt.InitAskStubber() - defer surveyteardown() - as.StubOne(0) + as, surveyteardown := prompt.NewAskStubber() + defer surveyteardown(t) + as.StubPrompt("Select a gist").AnswerDefault() } if tt.opts == nil { @@ -392,16 +392,18 @@ func Test_viewRun(t *testing.T) { func Test_promptGists(t *testing.T) { tests := []struct { - name string - gistIndex int - response string - wantOut string - gist *shared.Gist - wantErr bool + name string + askStubs func(as *prompt.AskStubber) + response string + wantOut string + gist *shared.Gist + wantErr bool }{ { - name: "multiple files, select first gist", - gistIndex: 0, + name: "multiple files, select first gist", + askStubs: func(as *prompt.AskStubber) { + as.StubPrompt("Select a gist").AnswerWith("cool.txt about 6 hours ago") + }, response: `{ "data": { "viewer": { "gists": { "nodes": [ { "name": "gistid1", @@ -421,8 +423,10 @@ func Test_promptGists(t *testing.T) { wantOut: "gistid1", }, { - name: "multiple files, select second gist", - gistIndex: 1, + name: "multiple files, select second gist", + askStubs: func(as *prompt.AskStubber) { + as.StubPrompt("Select a gist").AnswerWith("gistfile0.txt about 6 hours ago") + }, response: `{ "data": { "viewer": { "gists": { "nodes": [ { "name": "gistid1", @@ -465,11 +469,13 @@ func Test_promptGists(t *testing.T) { ) client := &http.Client{Transport: reg} - as, surveyteardown := prompt.InitAskStubber() - defer surveyteardown() - as.StubOne(tt.gistIndex) - t.Run(tt.name, func(t *testing.T) { + as, surveyteardown := prompt.NewAskStubber() + defer surveyteardown(t) + if tt.askStubs != nil { + tt.askStubs(as) + } + gistID, err := promptGists(client, "github.com", io.ColorScheme()) assert.NoError(t, err) assert.Equal(t, tt.wantOut, gistID) diff --git a/pkg/cmd/pr/review/review_test.go b/pkg/cmd/pr/review/review_test.go index 75ae8c200..87ec8e984 100644 --- a/pkg/cmd/pr/review/review_test.go +++ b/pkg/cmd/pr/review/review_test.go @@ -309,27 +309,11 @@ func TestPRReview_interactive_no_body(t *testing.T) { shared.RunCommandFinder("", &api.PullRequest{ID: "THE-ID", Number: 123}, ghrepo.New("OWNER", "REPO")) - as, teardown := prompt.InitAskStubber() - defer teardown() + as, teardown := prompt.NewAskStubber() + defer teardown(t) - as.Stub([]*prompt.QuestionStub{ - { - Name: "reviewType", - Value: "Request changes", - }, - }) - as.Stub([]*prompt.QuestionStub{ - { - Name: "body", - Default: true, - }, - }) - as.Stub([]*prompt.QuestionStub{ - { - Name: "confirm", - Value: true, - }, - }) + as.StubPrompt("What kind of review do you want to give?").AnswerWith("Request changes") + as.StubPrompt("Review body").AnswerWith("") _, err := runCommand(http, nil, true, "") assert.EqualError(t, err, "this type of review cannot be blank") diff --git a/pkg/cmd/workflow/enable/enable_test.go b/pkg/cmd/workflow/enable/enable_test.go index fb0e703bb..7cc3fbd47 100644 --- a/pkg/cmd/workflow/enable/enable_test.go +++ b/pkg/cmd/workflow/enable/enable_test.go @@ -121,7 +121,7 @@ func TestEnableRun(t *testing.T) { httpmock.StatusStringResponse(204, "{}")) }, askStubs: func(as *prompt.AskStubber) { - as.StubOne(0) + as.StubPrompt("Select a workflow").AnswerWith("a disabled workflow (disabled.yml)") }, wantOut: "✓ Enabled a disabled workflow\n", }, @@ -176,7 +176,7 @@ func TestEnableRun(t *testing.T) { httpmock.StatusStringResponse(204, "{}")) }, askStubs: func(as *prompt.AskStubber) { - as.StubOne(1) + as.StubPrompt("Which workflow do you mean?").AnswerWith("a disabled workflow (anotherDisabled.yml)") }, wantOut: "✓ Enabled a disabled workflow\n", }, @@ -194,9 +194,6 @@ func TestEnableRun(t *testing.T) { httpmock.REST("PUT", "repos/OWNER/REPO/actions/workflows/456/enable"), httpmock.StatusStringResponse(204, "{}")) }, - askStubs: func(as *prompt.AskStubber) { - as.StubOne(0) - }, wantOut: "✓ Enabled a disabled workflow\n", }, { @@ -279,13 +276,13 @@ func TestEnableRun(t *testing.T) { return ghrepo.FromFullName("OWNER/REPO") } - as, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(as) - } - t.Run(tt.name, func(t *testing.T) { + as, teardown := prompt.NewAskStubber() + defer teardown(t) + if tt.askStubs != nil { + tt.askStubs(as) + } + err := runEnable(tt.opts) if tt.wantErr { assert.Error(t, err) diff --git a/pkg/prompt/stubber.go b/pkg/prompt/stubber.go index 3159540f3..f01a28dcf 100644 --- a/pkg/prompt/stubber.go +++ b/pkg/prompt/stubber.go @@ -13,7 +13,11 @@ type AskStubber struct { stubs []*QuestionStub } -func InitAskStubber() (*AskStubber, func()) { +type testing interface { + Errorf(format string, args ...interface{}) +} + +func NewAskStubber() (*AskStubber, func(t testing)) { origSurveyAsk := SurveyAsk origSurveyAskOne := SurveyAskOne as := AskStubber{} @@ -120,13 +124,29 @@ func InitAskStubber() (*AskStubber, func()) { return nil } - teardown := func() { + teardown := func(t testing) { SurveyAsk = origSurveyAsk SurveyAskOne = origSurveyAskOne + for _, s := range as.stubs { + if !s.matched { + if t == nil { + panic(fmt.Sprintf("unmatched prompt stub: %+v", s)) + } else { + t.Errorf("unmatched prompt stub: %+v", s) + } + } + } } return &as, teardown } +func InitAskStubber() (*AskStubber, func()) { + as, teardown := NewAskStubber() + return as, func() { + teardown(nil) + } +} + type QuestionStub struct { Name string Value interface{}