Skip to content

Commit

Permalink
Fix: Missing "No newline at end of file" when comparing two texts tha…
Browse files Browse the repository at this point in the history
…t do not end in newlines (kpdecker#94)
  • Loading branch information
vasek committed Aug 30, 2018
1 parent 04ea47c commit 19a65d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
if (lines.length == 0 && !oldEOFNewline) {
// special case: old has no eol and no trailing context; no-nl can end up before adds
curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file');
} else if (!oldEOFNewline || !newEOFNewline) {
}
if (!newEOFNewline) {
curRange.push('\\ No newline at end of file');
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ describe('patch/create', function() {
+ '+line4\n');
});

it('should output "no newline" at end of file message on both missing nl', function() {
expect(createPatch('test', 'line1\nline2\nline3\nline4', 'line1\nline2\nline3\nline44', 'header1', 'header2')).to.equal(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,4 @@\n'
+ ' line1\n'
+ ' line2\n'
+ ' line3\n'
+ '-line4\n'
+ '\\ No newline at end of file\n'
+ '+line44\n'
+ '\\ No newline at end of file\n');
});

it('should output "no newline" at end of file message on context missing nl', function() {
expect(createPatch('test', 'line11\nline2\nline3\nline4', 'line1\nline2\nline3\nline4', 'header1', 'header2')).to.equal(
'Index: test\n'
Expand Down

0 comments on commit 19a65d5

Please sign in to comment.