Skip to content

Commit 244df82

Browse files
Fix more logic around newlines at EOF - this time stuff I recently broke in (pre-release) changes to applyPatch (#536)
* Add test showing broken newline-at-EOF handling in applyPatch * Fix newline-at-EOF behaviour in applyPatch
1 parent 939bb45 commit 244df82

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/patch/apply.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ export function applyPatch(source, uniDiff, options = {}) {
5757
} else if (prevLine[0] == '-') {
5858
addEOFNL = true;
5959
}
60-
break;
6160
}
6261
prevLine = line;
6362
}
6463
if (removeEOFNL) {
65-
if (lines[lines.length - 1] == '') {
64+
if (addEOFNL) {
65+
// This means the final line gets changed but doesn't have a trailing newline in either the
66+
// original or patched version. In that case, we do nothing if fuzzFactor > 0, and if
67+
// fuzzFactor is 0, we simply validate that the source file has no trailing newline.
68+
if (!fuzzFactor && lines[lines.length - 1] == '') {
69+
return false;
70+
}
71+
} else if (lines[lines.length - 1] == '') {
6672
lines.pop();
6773
} else if (!fuzzFactor) {
6874
return false;

test/patch/apply.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,41 @@ describe('patch/apply', function() {
15521552
expect(applyPatch(oldFile, diffFile, {fuzzFactor: 1})).to.equal(oldFile);
15531553
});
15541554

1555+
describe('when the last line is changed but both old & new version have no trailing newline...', () => {
1556+
const diffFile = 'Index: file.txt\n' +
1557+
'===================================================================\n' +
1558+
'--- file.txt\n' +
1559+
'+++ file.txt\n' +
1560+
'@@ -1,4 +1,4 @@\n' +
1561+
' foo\n' +
1562+
' bar\n' +
1563+
' baz\n' +
1564+
'-banana\n' +
1565+
'\\ No newline at end of file\n' +
1566+
'+babaco\n' +
1567+
'\\ No newline at end of file\n';
1568+
1569+
it('correctly applies the patch to the original source file', () => {
1570+
const oldFile = 'foo\nbar\nbaz\nbanana';
1571+
expect(applyPatch(oldFile, diffFile)).to.equal('foo\nbar\nbaz\nbabaco');
1572+
});
1573+
1574+
it('fails if fuzzFactor=0 and the source file has an unexpected trailing newline', () => {
1575+
const oldFile = 'foo\nbar\nbaz\nbanana\n';
1576+
expect(applyPatch(oldFile, diffFile)).to.equal(false);
1577+
});
1578+
1579+
it('ignores an unexpected trailing newline if fuzzFactor > 0', () => {
1580+
const oldFile = 'foo\nbar\nbaz\nbanana\n';
1581+
expect(applyPatch(oldFile, diffFile, {fuzzFactor: 1})).to.equal('foo\nbar\nbaz\nbabaco\n');
1582+
});
1583+
1584+
it("ignores extra lines, even with fuzzFactor = 0, as long as there's no newline at EOF", () => {
1585+
const oldFile = 'foo\nbar\nbaz\nbanana\nqux';
1586+
expect(applyPatch(oldFile, diffFile)).to.equal('foo\nbar\nbaz\nbabaco\nqux');
1587+
});
1588+
});
1589+
15551590
it('rejects negative or non-integer fuzz factors', () => {
15561591
expect(() => {
15571592
applyPatch(

0 commit comments

Comments
 (0)