From fb16830d97abc1d325c9b97cf123d83e78fbaba1 Mon Sep 17 00:00:00 2001 From: "Mike JS. Choi" Date: Fri, 18 May 2018 21:13:25 -0500 Subject: [PATCH] Test: Add tests for command.go --- conflict/command.go | 10 +++-- conflict/command_test.go | 82 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/conflict/command.go b/conflict/command.go index bd273c6..bc83b6e 100644 --- a/conflict/command.go +++ b/conflict/command.go @@ -8,12 +8,16 @@ import ( "syscall" ) -// RunCommand runs the given command with arguments and returns the output +var execCommand = exec.Command +var cwdEnvFlag = "GO_MOCK_PROCESS_DIRECTORY=" + +// run runs the given command with arguments and returns the output // Refer to https://stackoverflow.com/questions/10385551/get-exit-code-go func run(name string, dir string, args ...string) (stdout string, stderr string, exitCode int) { var outbuf, errbuf bytes.Buffer - cmd := exec.Command(name, args...) + cmd := execCommand(name, args...) cmd.Dir = dir + cmd.Env = append(cmd.Env, cwdEnvFlag+dir) cmd.Stdout = &outbuf cmd.Stderr = &errbuf @@ -59,8 +63,6 @@ func TopLevelPath(cwd string) (string, error) { if len(stderr) != 0 { return "", errors.New(stderr) - } else if len(stdout) == 0 { - return "", errors.New(stderr) } return string(strings.Split(stdout, "\n")[0]), nil diff --git a/conflict/command_test.go b/conflict/command_test.go index f26f5da..c7b58a6 100644 --- a/conflict/command_test.go +++ b/conflict/command_test.go @@ -1,16 +1,94 @@ package conflict import ( + "fmt" + "os" + "os/exec" "testing" ) +var mockValidDirectory = "/" +var mockInvalidDirectory = "/hello/world" + var commands = []struct { command string + path string ok bool }{ - {"time", true}, - {"foobar", false}, + {"time", mockValidDirectory, true}, + {"ls", mockValidDirectory, true}, + {"less", mockInvalidDirectory, false}, +} + +func TestHelperProcess(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + + cwd := os.Getenv(cwdEnvFlag) + + if cwd == mockInvalidDirectory { + fmt.Fprintf(os.Stderr, "Mock exec: Command tragically failed") + os.Exit(1) + } else { + fmt.Fprintf(os.Stdout, "Mock exec: Command succeeded") + os.Exit(0) + } +} + +// Allows us to mock exec.Command, thanks to +// https://npf.io/2015/06/testing-exec-command/ +func mockExecCommand(command string, args ...string) *exec.Cmd { + cs := []string{"-test.run=TestHelperProcess", "--", command} + cs = append(cs, args...) + cmd := exec.Command(os.Args[0], cs...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + return cmd } func TestRun(t *testing.T) { + execCommand = mockExecCommand + defer func() { execCommand = exec.Command }() + + for _, test := range commands { + stdout, stderr, exitCode := run(test.command, test.path) + + if test.ok && exitCode != 0 { + t.Errorf("run failed: got %s with exit code %d, expected no errors", stderr, exitCode) + } else if !(test.ok) && exitCode == 0 { + t.Errorf("run failed: got %s with exit code %d, expected errors", stdout, exitCode) + } + } +} + +func TestTopLevelPath(t *testing.T) { + execCommand = mockExecCommand + defer func() { execCommand = exec.Command }() + + tests := []string{mockValidDirectory, mockInvalidDirectory} + for _, test := range tests { + out, err := TopLevelPath(test) + + if test == mockValidDirectory && err != nil { + t.Errorf("TopLevelPath failed: got %s, expected no errors", err.Error()) + } else if test == mockInvalidDirectory && err == nil { + t.Errorf("TopLevelPath failed: got %s, expected errors", out) + } + } +} + +func TestMarkerLocations(t *testing.T) { + execCommand = mockExecCommand + defer func() { execCommand = exec.Command }() + + tests := []string{mockValidDirectory, mockInvalidDirectory} + for _, test := range tests { + out, err := MarkerLocations(test) + + if test == mockValidDirectory && err != nil { + t.Errorf("TopLevelPath failed: got %s, expected no errors", err.Error()) + } else if test == mockInvalidDirectory && err == nil { + t.Errorf("TopLevelPath failed: got %s, expected errors", out) + } + } }