Skip to content

Commit

Permalink
Fix more logic around newlines at EOF - this time stuff I recently br…
Browse files Browse the repository at this point in the history
…oke in (pre-release) changes to applyPatch (#536)

* Add test showing broken newline-at-EOF handling in applyPatch

* Fix newline-at-EOF behaviour in applyPatch
  • Loading branch information
ExplodingCabbage authored Jul 29, 2024
1 parent 939bb45 commit 244df82
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ export function applyPatch(source, uniDiff, options = {}) {
} else if (prevLine[0] == '-') {
addEOFNL = true;
}
break;
}
prevLine = line;
}
if (removeEOFNL) {
if (lines[lines.length - 1] == '') {
if (addEOFNL) {
// This means the final line gets changed but doesn't have a trailing newline in either the
// original or patched version. In that case, we do nothing if fuzzFactor > 0, and if
// fuzzFactor is 0, we simply validate that the source file has no trailing newline.
if (!fuzzFactor && lines[lines.length - 1] == '') {
return false;
}
} else if (lines[lines.length - 1] == '') {
lines.pop();
} else if (!fuzzFactor) {
return false;
Expand Down
35 changes: 35 additions & 0 deletions test/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,41 @@ describe('patch/apply', function() {
expect(applyPatch(oldFile, diffFile, {fuzzFactor: 1})).to.equal(oldFile);
});

describe('when the last line is changed but both old & new version have no trailing newline...', () => {
const diffFile = 'Index: file.txt\n' +
'===================================================================\n' +
'--- file.txt\n' +
'+++ file.txt\n' +
'@@ -1,4 +1,4 @@\n' +
' foo\n' +
' bar\n' +
' baz\n' +
'-banana\n' +
'\\ No newline at end of file\n' +
'+babaco\n' +
'\\ No newline at end of file\n';

it('correctly applies the patch to the original source file', () => {
const oldFile = 'foo\nbar\nbaz\nbanana';
expect(applyPatch(oldFile, diffFile)).to.equal('foo\nbar\nbaz\nbabaco');
});

it('fails if fuzzFactor=0 and the source file has an unexpected trailing newline', () => {
const oldFile = 'foo\nbar\nbaz\nbanana\n';
expect(applyPatch(oldFile, diffFile)).to.equal(false);
});

it('ignores an unexpected trailing newline if fuzzFactor > 0', () => {
const oldFile = 'foo\nbar\nbaz\nbanana\n';
expect(applyPatch(oldFile, diffFile, {fuzzFactor: 1})).to.equal('foo\nbar\nbaz\nbabaco\n');
});

it("ignores extra lines, even with fuzzFactor = 0, as long as there's no newline at EOF", () => {
const oldFile = 'foo\nbar\nbaz\nbanana\nqux';
expect(applyPatch(oldFile, diffFile)).to.equal('foo\nbar\nbaz\nbabaco\nqux');
});
});

it('rejects negative or non-integer fuzz factors', () => {
expect(() => {
applyPatch(
Expand Down

0 comments on commit 244df82

Please sign in to comment.