diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index dbacd9101..952490d7d 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -355,8 +355,7 @@ func Test_viewRun(t *testing.T) { )), ) - as, surveyteardown := prompt.NewAskStubber() - defer surveyteardown(t) + as := prompt.NewAskStubber(t) as.StubPrompt("Select a gist").AnswerDefault() } @@ -470,8 +469,7 @@ func Test_promptGists(t *testing.T) { client := &http.Client{Transport: reg} t.Run(tt.name, func(t *testing.T) { - as, surveyteardown := prompt.NewAskStubber() - defer surveyteardown(t) + as := prompt.NewAskStubber(t) if tt.askStubs != nil { tt.askStubs(as) } diff --git a/pkg/cmd/pr/review/review_test.go b/pkg/cmd/pr/review/review_test.go index 87ec8e984..ced1736d9 100644 --- a/pkg/cmd/pr/review/review_test.go +++ b/pkg/cmd/pr/review/review_test.go @@ -309,8 +309,7 @@ func TestPRReview_interactive_no_body(t *testing.T) { shared.RunCommandFinder("", &api.PullRequest{ID: "THE-ID", Number: 123}, ghrepo.New("OWNER", "REPO")) - as, teardown := prompt.NewAskStubber() - defer teardown(t) + as := prompt.NewAskStubber(t) as.StubPrompt("What kind of review do you want to give?").AnswerWith("Request changes") as.StubPrompt("Review body").AnswerWith("") diff --git a/pkg/cmd/workflow/enable/enable_test.go b/pkg/cmd/workflow/enable/enable_test.go index 7cc3fbd47..cfc9ea79c 100644 --- a/pkg/cmd/workflow/enable/enable_test.go +++ b/pkg/cmd/workflow/enable/enable_test.go @@ -277,8 +277,7 @@ func TestEnableRun(t *testing.T) { } t.Run(tt.name, func(t *testing.T) { - as, teardown := prompt.NewAskStubber() - defer teardown(t) + as := prompt.NewAskStubber(t) if tt.askStubs != nil { tt.askStubs(as) } diff --git a/pkg/prompt/stubber.go b/pkg/prompt/stubber.go index f01a28dcf..8d9e22520 100644 --- a/pkg/prompt/stubber.go +++ b/pkg/prompt/stubber.go @@ -15,9 +15,24 @@ type AskStubber struct { type testing interface { Errorf(format string, args ...interface{}) + Cleanup(func()) } -func NewAskStubber() (*AskStubber, func(t testing)) { +func NewAskStubber(t testing) *AskStubber { + as, teardown := InitAskStubber() + t.Cleanup(func() { + teardown() + for _, s := range as.stubs { + if !s.matched { + t.Errorf("unmatched prompt stub: %+v", s) + } + } + }) + return as +} + +// Deprecated: use NewAskStubber +func InitAskStubber() (*AskStubber, func()) { origSurveyAsk := SurveyAsk origSurveyAskOne := SurveyAskOne as := AskStubber{} @@ -124,29 +139,13 @@ func NewAskStubber() (*AskStubber, func(t testing)) { return nil } - teardown := func(t testing) { + teardown := func() { 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{}