summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike JS. Choi <mkchoi212@icloud.com>2018-05-19 08:59:24 -0500
committerMike JS. Choi <mkchoi212@icloud.com>2018-05-19 08:59:24 -0500
commitddf05f7f9a86d54cbc31273015e8a1265fa3537d (patch)
tree282d95c87363667300fb5476b4d2aa13afbe782c
parentfb16830d97abc1d325c9b97cf123d83e78fbaba1 (diff)
Test: add all tests for conflict pkg
-rw-r--r--conflict/command.go7
-rw-r--r--conflict/command_test.go37
-rw-r--r--conflict/parse_test.go26
-rw-r--r--conflict/test.go5
4 files changed, 63 insertions, 12 deletions
diff --git a/conflict/command.go b/conflict/command.go
index bc83b6e..a782902 100644
--- a/conflict/command.go
+++ b/conflict/command.go
@@ -9,7 +9,7 @@ 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
@@ -17,7 +17,10 @@ func run(name string, dir string, args ...string) (stdout string, stderr string,
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
diff --git a/conflict/command_test.go b/conflict/command_test.go
index c7b58a6..5446fcd 100644
--- a/conflict/command_test.go
+++ b/conflict/command_test.go
@@ -4,6 +4,8 @@ import (
"fmt"
"os"
"os/exec"
+ "strconv"
+ "strings"
"testing"
)
@@ -12,12 +14,11 @@ 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) {
@@ -25,15 +26,31 @@ func TestHelperProcess(t *testing.T) {
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
@@ -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
}
@@ -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)
diff --git a/conflict/parse_test.go b/conflict/parse_test.go
index a1c33b0..8d453ed 100644
--- a/conflict/parse_test.go
+++ b/conflict/parse_test.go
@@ -1,6 +1,7 @@
package conflict
import (
+ "os/exec"
"reflect"
"testing"
)
@@ -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)
+ }
+ }
+ }
+ }
+}
diff --git a/conflict/test.go b/conflict/test.go
index cfa0717..cba051e 100644
--- a/conflict/test.go
+++ b/conflict/test.go
@@ -4,6 +4,7 @@ type test struct {
path string
diffCheck []string
markers []int
+ numConflicts int
highlightable bool
parsable bool
}
@@ -13,6 +14,7 @@ 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,
},
@@ -20,6 +22,7 @@ var tests = []test{
path: "testdata/CircularCrownSelector.swift",
diffCheck: ccDiffCheck,
markers: []int{14, 22, 30, 38},
+ numConflicts: 1,
highlightable: true,
parsable: true,
},
@@ -27,6 +30,7 @@ var tests = []test{
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 6, 9},
+ numConflicts: 1,
highlightable: false,
parsable: true,
},
@@ -34,6 +38,7 @@ var tests = []test{
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 9},
+ numConflicts: 0,
highlightable: false,
parsable: false,
},