first pass at generalizing process stubbing

This commit is contained in:
nate smith 2019-10-31 11:10:57 -05:00
parent ee0fe61b04
commit 7555aa9be3
2 changed files with 51 additions and 47 deletions

View file

@ -2,62 +2,29 @@ package git
import (
"fmt"
"github.com/github/gh-cli/test"
"os"
"os/exec"
"testing"
)
type outputSpec struct {
Stdout string
ExitCode int
}
var _outputs map[string]outputSpec
func init() {
_outputs = map[string]outputSpec{
"no changes": outputSpec{"", 0},
"one change": outputSpec{` M poem.txt
`, 0},
"untracked file": outputSpec{` M poem.txt
?? new.txt
`, 0},
"boom": outputSpec{"", 1},
}
}
func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
func TestGitStatusHelperProcess(*testing.T) {
if test.SkipTestHelperProcess() {
return
}
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
outputs := map[string]test.ExecStub{
"no changes": test.ExecStub{"", 0},
"one change": test.ExecStub{` M poem.txt
`, 0},
"untracked file": test.ExecStub{` M poem.txt
?? new.txt
`, 0},
"boom": test.ExecStub{"", 1},
}
output := _outputs[args[0]]
output := test.GetExecStub(outputs)
defer os.Exit(output.ExitCode)
fmt.Println(output.Stdout)
}
func StubGit(desiredOutput string) func(...string) *exec.Cmd {
return func(args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", desiredOutput}
cs = append(cs, args...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
}
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(env, os.Environ()...)
return cmd
}
}
func Test_UncommittedChangeCount(t *testing.T) {
origGitCommand := GitCommand
defer func() {
@ -71,7 +38,7 @@ func Test_UncommittedChangeCount(t *testing.T) {
}
for k, v := range cases {
GitCommand = StubGit(k)
GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", k)
ucc, _ := UncommittedChangeCount()
if ucc != v {
@ -79,7 +46,7 @@ func Test_UncommittedChangeCount(t *testing.T) {
}
}
GitCommand = StubGit("boom")
GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", "boom")
_, err := UncommittedChangeCount()
if err.Error() != "failed to run git status: exit status 1" {
t.Errorf("got unexpected error message: %s", err)

View file

@ -11,6 +11,43 @@ import (
"github.com/spf13/cobra"
)
type ExecStub struct {
Stdout string
ExitCode int
}
func GetExecStub(outputs map[string]ExecStub) ExecStub {
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
return outputs[args[0]]
}
func SkipTestHelperProcess() bool {
return os.Getenv("GO_WANT_HELPER_PROCESS") != "1"
}
func StubExecCommand(testHelper string, desiredOutput string) func(...string) *exec.Cmd {
return func(args ...string) *exec.Cmd {
cs := []string{
fmt.Sprintf("-test.run=%s", testHelper),
"--", desiredOutput}
cs = append(cs, args...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
}
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(env, os.Environ()...)
return cmd
}
}
type TempGitRepo struct {
Remote string
TearDown func()