Skip to content

Commit a33b5a5

Browse files
committed
Have NewAskStubber perform auto-cleanup
1 parent 30c5ef2 commit a33b5a5

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

pkg/cmd/gist/view/view_test.go

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

358-
as, surveyteardown := prompt.NewAskStubber()
359-
defer surveyteardown(t)
358+
as := prompt.NewAskStubber(t)
360359
as.StubPrompt("Select a gist").AnswerDefault()
361360
}
362361

@@ -470,8 +469,7 @@ func Test_promptGists(t *testing.T) {
470469
client := &http.Client{Transport: reg}
471470

472471
t.Run(tt.name, func(t *testing.T) {
473-
as, surveyteardown := prompt.NewAskStubber()
474-
defer surveyteardown(t)
472+
as := prompt.NewAskStubber(t)
475473
if tt.askStubs != nil {
476474
tt.askStubs(as)
477475
}

pkg/cmd/pr/review/review_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ 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.NewAskStubber()
313-
defer teardown(t)
312+
as := prompt.NewAskStubber(t)
314313

315314
as.StubPrompt("What kind of review do you want to give?").AnswerWith("Request changes")
316315
as.StubPrompt("Review body").AnswerWith("")

pkg/cmd/workflow/enable/enable_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ func TestEnableRun(t *testing.T) {
277277
}
278278

279279
t.Run(tt.name, func(t *testing.T) {
280-
as, teardown := prompt.NewAskStubber()
281-
defer teardown(t)
280+
as := prompt.NewAskStubber(t)
282281
if tt.askStubs != nil {
283282
tt.askStubs(as)
284283
}

pkg/prompt/stubber.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@ type AskStubber struct {
1515

1616
type testing interface {
1717
Errorf(format string, args ...interface{})
18+
Cleanup(func())
1819
}
1920

20-
func NewAskStubber() (*AskStubber, func(t testing)) {
21+
func NewAskStubber(t testing) *AskStubber {
22+
as, teardown := InitAskStubber()
23+
t.Cleanup(func() {
24+
teardown()
25+
for _, s := range as.stubs {
26+
if !s.matched {
27+
t.Errorf("unmatched prompt stub: %+v", s)
28+
}
29+
}
30+
})
31+
return as
32+
}
33+
34+
// Deprecated: use NewAskStubber
35+
func InitAskStubber() (*AskStubber, func()) {
2136
origSurveyAsk := SurveyAsk
2237
origSurveyAskOne := SurveyAskOne
2338
as := AskStubber{}
@@ -124,29 +139,13 @@ func NewAskStubber() (*AskStubber, func(t testing)) {
124139
return nil
125140
}
126141

127-
teardown := func(t testing) {
142+
teardown := func() {
128143
SurveyAsk = origSurveyAsk
129144
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-
}
139145
}
140146
return &as, teardown
141147
}
142148

143-
func InitAskStubber() (*AskStubber, func()) {
144-
as, teardown := NewAskStubber()
145-
return as, func() {
146-
teardown(nil)
147-
}
148-
}
149-
150149
type QuestionStub struct {
151150
Name string
152151
Value interface{}

0 commit comments

Comments
 (0)