Skip to content

Commit

Permalink
Test: add all tests for conflict pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 19, 2018
1 parent fb16830 commit ddf05f7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
7 changes: 5 additions & 2 deletions conflict/command.go
Expand Up @@ -9,15 +9,18 @@ import (
)

var execCommand = exec.Command
var cwdEnvFlag = "GO_MOCK_PROCESS_DIRECTORY="
var argsEnvFlag = "GO_MOCK_PROCESS_ARGS"

// 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 := execCommand(name, args...)
cmd.Dir = dir
cmd.Env = append(cmd.Env, cwdEnvFlag+dir)

// Save config for testing purposes
cmd.Env = append(cmd.Env, argsEnvFlag+"="+strings.Join(args, ","))

cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

Expand Down
37 changes: 27 additions & 10 deletions conflict/command_test.go
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"testing"
)

Expand All @@ -12,28 +14,43 @@ var mockInvalidDirectory = "/hello/world"

var commands = []struct {
command string
path string
ok bool
}{
{"time", mockValidDirectory, true},
{"ls", mockValidDirectory, true},
{"less", mockInvalidDirectory, false},
{"time", true},
{"ls", true},
{"foobar", false},
}

func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}

cwd := os.Getenv(cwdEnvFlag)
args := os.Getenv(argsEnvFlag)

if cwd == mockInvalidDirectory {
// Purposely fail test
if args == "false" {
fmt.Fprintf(os.Stderr, "Mock exec: Command tragically failed")
os.Exit(1)
} else {
fmt.Fprintf(os.Stdout, "Mock exec: Command succeeded")
}

// TopLevelPath arguements
if args == "rev-parse,--show-toplevel" {
fmt.Fprintf(os.Stdout, "testdata")
os.Exit(0)
}

// MarkerLocation arguements
if args == "--no-pager,diff,--check" {
allCheckOutput := append([]string{}, loremDiffCheck...)
allCheckOutput = append(allCheckOutput, ccDiffCheck...)
allCheckOutput = append(allCheckOutput, readmeDiffCheck...)
fmt.Fprintf(os.Stdout, strings.Join(allCheckOutput, "\n"))
os.Exit(0)
}

fmt.Fprintf(os.Stdout, "Mock exec: Command succeeded")
os.Exit(0)
}

// Allows us to mock exec.Command, thanks to
Expand All @@ -42,7 +59,7 @@ 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"}
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
return cmd
}

Expand All @@ -51,7 +68,7 @@ func TestRun(t *testing.T) {
defer func() { execCommand = exec.Command }()

for _, test := range commands {
stdout, stderr, exitCode := run(test.command, test.path)
stdout, stderr, exitCode := run(test.command, ".", strconv.FormatBool(test.ok))

if test.ok && exitCode != 0 {
t.Errorf("run failed: got %s with exit code %d, expected no errors", stderr, exitCode)
Expand Down
26 changes: 26 additions & 0 deletions conflict/parse_test.go
@@ -1,6 +1,7 @@
package conflict

import (
"os/exec"
"reflect"
"testing"
)
Expand Down Expand Up @@ -35,3 +36,28 @@ func TestParseConflictsIn(t *testing.T) {
}
}
}

func TestFind(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()

files, err := Find(".")
if err != nil {
t.Errorf("Find failed: %s", err.Error())
}

if len(files) != 3 {
t.Errorf("Find failed: got %d files, want 3", len(files))
}

for _, f := range files {
for _, test := range tests {
if f.AbsolutePath == test.path && test.parsable {
if len(f.Conflicts) != test.numConflicts {
t.Errorf("Find failed: got %d conflicts in %s, want %d",
len(f.Conflicts), test.path, test.numConflicts)
}
}
}
}
}
5 changes: 5 additions & 0 deletions conflict/test.go
Expand Up @@ -4,6 +4,7 @@ type test struct {
path string
diffCheck []string
markers []int
numConflicts int
highlightable bool
parsable bool
}
Expand All @@ -13,27 +14,31 @@ var tests = []test{
path: "testdata/assets/README.md",
diffCheck: readmeDiffCheck,
markers: []int{20, 22, 24, 26, 32, 35, 38, 41},
numConflicts: 2,
highlightable: true,
parsable: true,
},
{
path: "testdata/CircularCrownSelector.swift",
diffCheck: ccDiffCheck,
markers: []int{14, 22, 30, 38},
numConflicts: 1,
highlightable: true,
parsable: true,
},
{
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 6, 9},
numConflicts: 1,
highlightable: false,
parsable: true,
},
{
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 9},
numConflicts: 0,
highlightable: false,
parsable: false,
},
Expand Down

0 comments on commit ddf05f7

Please sign in to comment.