From 6aa0c071d6fd2e60e2467bfb273286b402f73896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 11 Nov 2019 17:04:26 +0100 Subject: [PATCH] Simplify tests that use StubExecCommand --- command/pr_create_test.go | 21 ++++++++++++--------- git/git.go | 4 ++-- git/git_test.go | 29 ++++++++++++++++------------- test/helpers.go | 10 ---------- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/command/pr_create_test.go b/command/pr_create_test.go index e25e3c582..7453a0e1f 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -18,20 +18,23 @@ func TestPrCreateHelperProcess(*testing.T) { return } - statusOutputs := map[string]string{ - "clean": "", - "dirty": " M git/git.go", - } - args := test.GetTestHelperProcessArgs() switch args[1] { case "status": - fmt.Println(statusOutputs[args[0]]) + switch args[0] { + case "clean": + case "dirty": + fmt.Println(" M git/git.go") + default: + fmt.Fprintf(os.Stderr, "unknown scenario: %q", args[0]) + os.Exit(1) + } case "push": - fmt.Println() + default: + fmt.Fprintf(os.Stderr, "unknown command: %q", args[1]) + os.Exit(1) } - - defer os.Exit(0) + os.Exit(0) } func TestReportsUncommittedChanges(t *testing.T) { diff --git a/git/git.go b/git/git.go index 24297a7a8..411863b31 100644 --- a/git/git.go +++ b/git/git.go @@ -212,9 +212,9 @@ var GitCommand = func(args ...string) *exec.Cmd { func UncommittedChangeCount() (int, error) { statusCmd := GitCommand("status", "--porcelain") - output, err := statusCmd.Output() + output, err := utils.PrepareCmd(statusCmd).Output() if err != nil { - return 0, fmt.Errorf("failed to run git status: %s", err) + return 0, err } lines := strings.Split(string(output), "\n") diff --git a/git/git_test.go b/git/git_test.go index ad60ef256..dc1446c33 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -2,27 +2,30 @@ package git import ( "fmt" - "github.com/github/gh-cli/test" "os" + "strings" "testing" + + "github.com/github/gh-cli/test" ) func TestGitStatusHelperProcess(*testing.T) { if test.SkipTestHelperProcess() { return } - 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}, + + args := test.GetTestHelperProcessArgs() + switch args[0] { + case "no changes": + case "one change": + fmt.Println(" M poem.txt") + case "untracked file": + fmt.Println(" M poem.txt") + fmt.Println("?? new.txt") + case "boom": + os.Exit(1) } - output := test.GetExecStub(outputs) - defer os.Exit(output.ExitCode) - fmt.Println(output.Stdout) + os.Exit(0) } func Test_UncommittedChangeCount(t *testing.T) { @@ -48,7 +51,7 @@ func Test_UncommittedChangeCount(t *testing.T) { GitCommand = test.StubExecCommand("TestGitStatusHelperProcess", "boom") _, err := UncommittedChangeCount() - if err.Error() != "failed to run git status: exit status 1" { + if !strings.HasSuffix(err.Error(), "git.test: exit status 1") { t.Errorf("got unexpected error message: %s", err) } } diff --git a/test/helpers.go b/test/helpers.go index 8c2b5f416..dca930be5 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -11,11 +11,6 @@ import ( "github.com/spf13/cobra" ) -type ExecStub struct { - Stdout string - ExitCode int -} - func GetTestHelperProcessArgs() []string { args := os.Args for len(args) > 0 { @@ -28,11 +23,6 @@ func GetTestHelperProcessArgs() []string { return args } -func GetExecStub(outputs map[string]ExecStub) ExecStub { - args := GetTestHelperProcessArgs() - return outputs[args[0]] -} - func SkipTestHelperProcess() bool { return os.Getenv("GO_WANT_HELPER_PROCESS") != "1" }