Skip to content

Commit 19a65d5

Browse files
committed
Fix: Missing "No newline at end of file" when comparing two texts that do not end in newlines (kpdecker#94)
1 parent 04ea47c commit 19a65d5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/patch/create.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
7474
if (lines.length == 0 && !oldEOFNewline) {
7575
// special case: old has no eol and no trailing context; no-nl can end up before adds
7676
curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file');
77-
} else if (!oldEOFNewline || !newEOFNewline) {
77+
}
78+
if (!newEOFNewline) {
7879
curRange.push('\\ No newline at end of file');
7980
}
8081
}

test/patch/create.js

+16
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ describe('patch/create', function() {
8989
+ '+line4\n');
9090
});
9191

92+
it('should output "no newline" at end of file message on both missing nl', function() {
93+
expect(createPatch('test', 'line1\nline2\nline3\nline4', 'line1\nline2\nline3\nline44', 'header1', 'header2')).to.equal(
94+
'Index: test\n'
95+
+ '===================================================================\n'
96+
+ '--- test\theader1\n'
97+
+ '+++ test\theader2\n'
98+
+ '@@ -1,4 +1,4 @@\n'
99+
+ ' line1\n'
100+
+ ' line2\n'
101+
+ ' line3\n'
102+
+ '-line4\n'
103+
+ '\\ No newline at end of file\n'
104+
+ '+line44\n'
105+
+ '\\ No newline at end of file\n');
106+
});
107+
92108
it('should output "no newline" at end of file message on context missing nl', function() {
93109
expect(createPatch('test', 'line11\nline2\nline3\nline4', 'line1\nline2\nline3\nline4', 'header1', 'header2')).to.equal(
94110
'Index: test\n'

0 commit comments

Comments
 (0)