Skip to content

Commit

Permalink
Test: Add tests for command.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 19, 2018
1 parent 46606a1 commit fb16830
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
10 changes: 6 additions & 4 deletions conflict/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
82 changes: 80 additions & 2 deletions conflict/command_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit fb16830

Please sign in to comment.