Skip to content

Commit d76ac52

Browse files
vasekkpdecker
authored andcommitted
Fix: Missing "No newline at end of file" when comparing two texts that do not end in newlines (#94)
1 parent f27b899 commit d76ac52

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Diff for: src/patch/create.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
7171
// EOF is inside this hunk
7272
let oldEOFNewline = (/\n$/.test(oldStr));
7373
let newEOFNewline = (/\n$/.test(newStr));
74-
if (lines.length == 0 && !oldEOFNewline) {
74+
let noNlBeforeAdds = lines.length == 0 && curRange.length > hunk.oldLines;
75+
if (!oldEOFNewline && noNlBeforeAdds) {
7576
// special case: old has no eol and no trailing context; no-nl can end up before adds
7677
curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file');
77-
} else if (!oldEOFNewline || !newEOFNewline) {
78+
}
79+
if ((!oldEOFNewline && !noNlBeforeAdds) || !newEOFNewline) {
7880
curRange.push('\\ No newline at end of file');
7981
}
8082
}

Diff for: test/patch/create.js

+43
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'
@@ -104,6 +120,33 @@ describe('patch/create', function() {
104120
+ '\\ No newline at end of file\n');
105121
});
106122

123+
it('should output only one "no newline" at end of file message on empty file', function() {
124+
expect(createPatch('test', '', 'line1\nline2\nline3\nline4', 'header1', 'header2')).to.equal(
125+
'Index: test\n'
126+
+ '===================================================================\n'
127+
+ '--- test\theader1\n'
128+
+ '+++ test\theader2\n'
129+
+ '@@ -1,0 +1,4 @@\n'
130+
+ '\\ No newline at end of file\n'
131+
+ '+line1\n'
132+
+ '+line2\n'
133+
+ '+line3\n'
134+
+ '+line4\n'
135+
+ '\\ No newline at end of file\n');
136+
137+
expect(createPatch('test', 'line1\nline2\nline3\nline4', '', 'header1', 'header2')).to.equal(
138+
'Index: test\n'
139+
+ '===================================================================\n'
140+
+ '--- test\theader1\n'
141+
+ '+++ test\theader2\n'
142+
+ '@@ -1,4 +1,0 @@\n'
143+
+ '-line1\n'
144+
+ '-line2\n'
145+
+ '-line3\n'
146+
+ '-line4\n'
147+
+ '\\ No newline at end of file\n');
148+
});
149+
107150
it('should not output no newline at end of file message when eof outside hunk', function() {
108151
expect(createPatch('test', 'line11\nline2\nline3\nline4\nline4\nline4\nline4', 'line1\nline2\nline3\nline4\nline4\nline4\nline4', 'header1', 'header2')).to.equal(
109152
'Index: test\n'

0 commit comments

Comments
 (0)