From 7555aa9be33ea9e1adbc44afdeaba2ac7455f526 Mon Sep 17 00:00:00 2001 From: nate smith Date: Thu, 31 Oct 2019 11:10:57 -0500 Subject: [PATCH] first pass at generalizing process stubbing --- git/git_test.go | 61 ++++++++++++------------------------------------- test/helpers.go | 37 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/git/git_test.go b/git/git_test.go index 5402d51b4..ad60ef256 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -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) diff --git a/test/helpers.go b/test/helpers.go index d9276565c..e2f059175 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -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()