consolidate CmdStubber into test package

This commit is contained in:
vilmibm 2020-03-23 16:36:24 -05:00
parent 5187ad4431
commit 88cf6ce16e
3 changed files with 48 additions and 47 deletions

View file

@ -8,6 +8,7 @@ import (
"testing"
"github.com/cli/cli/context"
"github.com/cli/cli/test"
)
func TestPRCreate(t *testing.T) {
@ -24,7 +25,7 @@ func TestPRCreate(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -68,7 +69,7 @@ func TestPRCreate_alreadyExists(t *testing.T) {
] } } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -88,7 +89,7 @@ func TestPRCreate_web(t *testing.T) {
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -123,7 +124,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub(" M git/git.go") // git status
@ -193,7 +194,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -242,7 +243,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -312,7 +313,7 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -383,7 +384,7 @@ func TestPRCreate_survey_autofill(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -426,7 +427,7 @@ func TestPRCreate_defaults_error_autofill(t *testing.T) {
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -442,7 +443,7 @@ func TestPRCreate_defaults_error_web(t *testing.T) {
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
@ -463,7 +464,7 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) {
} } } }
`))
cs, cmdTeardown := initCmdStubber()
cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status

View file

@ -3,7 +3,6 @@ package command
import (
"errors"
"fmt"
"os/exec"
"reflect"
"github.com/AlecAivazis/survey/v2"
@ -11,43 +10,8 @@ import (
"github.com/cli/cli/api"
"github.com/cli/cli/context"
"github.com/cli/cli/internal/run"
"github.com/cli/cli/test"
)
// TODO this is split between here and test/helpers.go. I did that because otherwise our test
// package would have to import utils (for utils.Runnable) which felt wrong. while utils_test
// currently doesn't import test helpers, i don't see why that ought to be precluded.
// I'm wondering if this is a case for having a global package just for storing Interfaces.
type CmdStubber struct {
Stubs []*test.OutputStub
Count int
Calls []*exec.Cmd
}
func initCmdStubber() (*CmdStubber, func()) {
cs := CmdStubber{}
teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs))
return &cs, teardown
}
func (cs *CmdStubber) Stub(desiredOutput string) {
// TODO maybe have some kind of command mapping but going simple for now
cs.Stubs = append(cs.Stubs, &test.OutputStub{[]byte(desiredOutput)})
}
func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
return func(cmd *exec.Cmd) run.Runnable {
cs.Calls = append(cs.Calls, cmd)
call := cs.Count
cs.Count += 1
if call >= len(cs.Stubs) {
panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd))
}
return cs.Stubs[call]
}
}
type askStubber struct {
Asks [][]*survey.Question
Count int

View file

@ -1,5 +1,12 @@
package test
import (
"fmt"
"os/exec"
"github.com/cli/cli/internal/run"
)
// OutputStub implements a simple utils.Runnable
type OutputStub struct {
Out []byte
@ -12,3 +19,32 @@ func (s OutputStub) Output() ([]byte, error) {
func (s OutputStub) Run() error {
return nil
}
type CmdStubber struct {
Stubs []*OutputStub
Count int
Calls []*exec.Cmd
}
func InitCmdStubber() (*CmdStubber, func()) {
cs := CmdStubber{}
teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs))
return &cs, teardown
}
func (cs *CmdStubber) Stub(desiredOutput string) {
// TODO maybe have some kind of command mapping but going simple for now
cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput)})
}
func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
return func(cmd *exec.Cmd) run.Runnable {
cs.Calls = append(cs.Calls, cmd)
call := cs.Count
cs.Count += 1
if call >= len(cs.Stubs) {
panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd))
}
return cs.Stubs[call]
}
}