Skip to content

Commit

Permalink
Fix diff edge case for trailing diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Feb 21, 2017
1 parent 58a8e94 commit 792bf52
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ func buildDiffLines(lString, rString string) []lineRecord {
}
}

if l != "" && r != "" {
if l == r {
results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l})
} else {
if l != "" && l == r {
results = append(results, lineRecord{Type: diffmatchpatch.DiffEqual, Line: l})
} else {
if l != "" {
results = append(results, lineRecord{Type: diffmatchpatch.DiffDelete, Line: l})
}
if r != "" {
results = append(results, lineRecord{Type: diffmatchpatch.DiffInsert, Line: r})
}
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,39 @@ Line3`
t.Fatalf("unexpected diff. expected=%v, actual=%v", expectedDiff, actual)
}
}

func Test_Diff_4(t *testing.T) {
l := `A
B
C
D
E
F
`
r := `A
B
C
D
E
F`
expectedDiff := `...
D
E
- F
+ F
`
{
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(l, r, false)

// We do need to cleanup, as otherwise we get some spurious changes on complex diffs
diffs = dmp.DiffCleanupSemantic(diffs)

t.Logf("diffs %v", diffs)
}

actual := FormatDiff(l, r)
if actual != expectedDiff {
t.Fatalf("unexpected diff. expected=%s, actual=%s", expectedDiff, actual)
}
}

0 comments on commit 792bf52

Please sign in to comment.