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 (#94)
  • Loading branch information
vasek authored and kpdecker committed Jan 5, 2019
1 parent f27b899 commit d76ac52
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
// EOF is inside this hunk
let oldEOFNewline = (/\n$/.test(oldStr));
let newEOFNewline = (/\n$/.test(newStr));
if (lines.length == 0 && !oldEOFNewline) {
let noNlBeforeAdds = lines.length == 0 && curRange.length > hunk.oldLines;
if (!oldEOFNewline && noNlBeforeAdds) {
// 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 ((!oldEOFNewline && !noNlBeforeAdds) || !newEOFNewline) {
curRange.push('\\ No newline at end of file');
}
}
Expand Down
43 changes: 43 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 All @@ -104,6 +120,33 @@ describe('patch/create', function() {
+ '\\ No newline at end of file\n');
});

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

expect(createPatch('test', 'line1\nline2\nline3\nline4', '', 'header1', 'header2')).to.equal(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,4 +1,0 @@\n'
+ '-line1\n'
+ '-line2\n'
+ '-line3\n'
+ '-line4\n'
+ '\\ No newline at end of file\n');
});

it('should not output no newline at end of file message when eof outside hunk', function() {
expect(createPatch('test', 'line11\nline2\nline3\nline4\nline4\nline4\nline4', 'line1\nline2\nline3\nline4\nline4\nline4\nline4', 'header1', 'header2')).to.equal(
'Index: test\n'
Expand Down

0 comments on commit d76ac52

Please sign in to comment.