Skip to content

Commit 953267f

Browse files
committed
Improved test coverage (and slightly stricter error handling)
1 parent 9721ba5 commit 953267f

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

diff/diff_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func init() {
2929
time.Local = time.UTC
3030
}
3131

32-
func TestReadQuotedFilename(t *testing.T) {
32+
func TestReadQuotedFilename_Success(t *testing.T) {
3333
tests := []string{
3434
`""`, "", "",
3535
`"aaa"`, "aaa", "",
@@ -50,7 +50,21 @@ func TestReadQuotedFilename(t *testing.T) {
5050
}
5151
}
5252

53-
func TestParseDiffGitArgs(t *testing.T) {
53+
func TestReadQuotedFilename_Error(t *testing.T) {
54+
tests := []string{
55+
`"`,
56+
`"\"`,
57+
`"\xxx"`,
58+
}
59+
for _, input := range tests {
60+
_, _, err := readQuotedFilename(input)
61+
if err == nil {
62+
t.Errorf("readQuotedFilename(`%s`): expected error", input)
63+
}
64+
}
65+
}
66+
67+
func TestParseDiffGitArgs_Success(t *testing.T) {
5468
tests := []string{
5569
`aaa bbb`, "aaa", "bbb",
5670
`"aaa" bbb`, "aaa", "bbb",
@@ -72,6 +86,28 @@ func TestParseDiffGitArgs(t *testing.T) {
7286
}
7387
}
7488

89+
func TestParseDiffGitArgs_Unsuccessful(t *testing.T) {
90+
tests := []string{
91+
``,
92+
`hello_world.txt`,
93+
`word `,
94+
` word`,
95+
`"a/bad_quoting b/bad_quoting`,
96+
`a/bad_quoting "b/bad_quoting`,
97+
`a/bad_quoting b/bad_quoting"`,
98+
`"a/bad_quoting b/bad_quoting"`,
99+
`"a/bad""b/bad"`,
100+
`"a/bad" "b/bad" "c/bad"`,
101+
`a/bad "b/bad" "c/bad"`,
102+
}
103+
for _, input := range tests {
104+
success, first, second := parseDiffGitArgs(input)
105+
if success {
106+
t.Errorf("`diff --git %s`: expected unsuccessful; got `%s` and `%s`", input, first, second)
107+
}
108+
}
109+
}
110+
75111
func TestParseHunkNoChunksize(t *testing.T) {
76112
filename := "sample_no_chunksize.diff"
77113
diffData, err := ioutil.ReadFile(filepath.Join("testdata", filename))

diff/parse.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ func parseDiffGitArgs(diffArgs string) (bool, string, string) {
417417

418418
secondSpace := strings.IndexByte(diffArgs[firstSpace+1:], ' ')
419419
if secondSpace == -1 {
420+
if diffArgs[firstSpace+1] == '"' {
421+
return false, "", ""
422+
}
420423
return true, diffArgs[:firstSpace], diffArgs[firstSpace+1:]
421424
}
422425

0 commit comments

Comments
 (0)