Skip to content

Commit dffe58f

Browse files
committed
test: add regression test for mode-only diff panic in checkDiffFile
Add a table entry to Test_checkDiffFile covering the case where OrigName and NewName are both empty strings — the shape go-diff produces for mode-only diffs (chmod changes with no content diff). Before the fix, calling checkDiffFile with empty names panicked: panic: runtime error: index out of range [1] with length 1 diff/diff.go:44 After the fix the function returns ("", true), skipping the entry.
1 parent efff7e0 commit dffe58f

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

diff/diff_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ func newProcessFlagAccEnv() *testProcessor {
6969

7070
func Test_checkDiffFile(t *testing.T) {
7171
cases := []struct {
72-
name string
73-
fileName string
74-
origName string
75-
newName string
76-
skip bool
72+
name string
73+
fileName string
74+
origName string
75+
newName string
76+
skip bool
77+
expectedFilePath string // when non-empty, overrides "../testdata/" + fileName
7778
}{
7879
{
7980
name: "basic",
@@ -89,6 +90,16 @@ func Test_checkDiffFile(t *testing.T) {
8990
newName: "b/.testignore",
9091
skip: true,
9192
},
93+
{
94+
// Mode-only diffs (e.g. chmod changes) produce no --- / +++ headers.
95+
// go-diff returns empty OrigName and NewName for these entries, which
96+
// caused a panic at diff.go:44 when SplitN("", "/", 2)[1] was accessed.
97+
name: "skip mode-only diff with empty OrigName and NewName",
98+
origName: "",
99+
newName: "",
100+
skip: true,
101+
expectedFilePath: "",
102+
},
92103
}
93104

94105
for _, tc := range cases {
@@ -100,13 +111,16 @@ func Test_checkDiffFile(t *testing.T) {
100111
OrigStartLine: 0,
101112
StartPosition: 1,
102113
}
103-
diff := diff.FileDiff{
114+
fileDiff := diff.FileDiff{
104115
OrigName: tc.origName,
105116
NewName: tc.newName,
106117
Hunks: []*diff.Hunk{hunk},
107118
}
108-
filePath, ignore := checkDiffFile(&diff, "../testdata")
109-
expectedFilePath := "../testdata/" + tc.fileName
119+
filePath, ignore := checkDiffFile(&fileDiff, "../testdata")
120+
expectedFilePath := tc.expectedFilePath
121+
if expectedFilePath == "" && tc.fileName != "" {
122+
expectedFilePath = "../testdata/" + tc.fileName
123+
}
110124
assert.Equal(t, expectedFilePath, filePath)
111125
assert.Equal(t, tc.skip, ignore)
112126
})
@@ -248,4 +262,4 @@ func TestProcessDiffs_BuildReferences(t *testing.T) {
248262
assert.Equal(t, tc.expected, flagsRef)
249263
})
250264
}
251-
}
265+
}

0 commit comments

Comments
 (0)