Skip to content

Fix: Missing "No newline at end of file" when comparing two texts that do not end in newlines #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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