Check for unused ask stubs at the end of the test

This commit is contained in:
Mislav Marohnić 2022-01-13 12:23:42 +01:00
parent 8e64c149e1
commit c839d3ba1d
4 changed files with 57 additions and 50 deletions

View file

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

View file

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

View file

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

View file

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