Have NewAskStubber perform auto-cleanup

This commit is contained in:
Mislav Marohnić 2022-01-14 18:52:00 +01:00
parent 30c5ef23ee
commit a33b5a55c4
4 changed files with 21 additions and 26 deletions

View file

@ -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)
}

View file

@ -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("")

View file

@ -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)
}

View file

@ -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{}