Skip to content

Commit

Permalink
Add final tests for git header parsing functions
Browse files Browse the repository at this point in the history
This gets 100% test coverage for the functions involved in parsing
Git-style file headers. Whether that actually makes it correct remains
to be seen.
  • Loading branch information
bluekeyes committed Mar 20, 2019
1 parent a1f92fe commit cbd3915
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gitdiff/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func parseGitHeaderName(header string) (string, error) {
return "", err
}

if header[n] == ' ' || header[n] == '\t' {
if n < len(header) && (header[n] == ' ' || header[n] == '\t') {
n++
}

Expand Down
60 changes: 60 additions & 0 deletions gitdiff/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ func TestParseGitHeaderData(t *testing.T) {
Line: "--- a/dir/file.txt\n",
Err: true,
},
"oldFileNameMissing": {
Line: "--- \n",
Err: true,
},
"newFileName": {
Line: "+++ b/dir/file.txt\n",
OutputFile: &File{
Expand Down Expand Up @@ -325,6 +329,10 @@ func TestParseGitHeaderData(t *testing.T) {
Line: "+++ b/dir/file.txt\n",
Err: true,
},
"newFileNameMissing": {
Line: "+++ \n",
Err: true,
},
"oldMode": {
Line: "old mode 100644\n",
OutputFile: &File{
Expand Down Expand Up @@ -403,6 +411,10 @@ func TestParseGitHeaderData(t *testing.T) {
Score: 0,
},
},
"similarityIndexInvalid": {
Line: "similarity index 12ab\n",
Err: true,
},
"indexFullSHA1AndMode": {
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14..04fab916d8f938173cbb8b93469855f0e838f098 100644\n",
OutputFile: &File{
Expand All @@ -426,6 +438,10 @@ func TestParseGitHeaderData(t *testing.T) {
OldMode: os.FileMode(0100644),
},
},
"indexInvalid": {
Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14\n",
Err: true,
},
}

for name, test := range tests {
Expand Down Expand Up @@ -455,3 +471,47 @@ func TestParseGitHeaderData(t *testing.T) {
})
}
}

func TestParseGitHeaderName(t *testing.T) {
tests := map[string]struct {
Input string
Output string
Err bool
}{
"twoMatchingNames": {
Input: "a/dir/file.txt b/dir/file.txt",
Output: "dir/file.txt",
},
"twoDifferentNames": {
Input: "a/dir/foo.txt b/dir/bar.txt",
Output: "",
},
"missingSecondName": {
Input: "a/dir/foo.txt",
Err: true,
},
"invalidName": {
Input: `"a/dir/file.txt b/dir/file.txt`,
Err: true,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
output, err := parseGitHeaderName(test.Input)
if test.Err {
if err == nil {
t.Fatalf("expected error parsing header name, but got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing header name: %v", err)
}

if output != test.Output {
t.Errorf("incorrect output: expected %q, actual %q", test.Output, output)
}
})
}
}

0 comments on commit cbd3915

Please sign in to comment.