Skip to content

Commit 396e86f

Browse files
test: add applyPatch regression test for Unix patch with literal CR on CRLF source; update release notes
Co-authored-by: ExplodingCabbage <2358339+ExplodingCabbage@users.noreply.github.com>
1 parent 0eaa834 commit 396e86f

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [#697](https://github.com/kpdecker/jsdiff/pull/697) *`diffJson` now correctly handles JSON objects containing a key named `__proto__`*. (Previously, the returned diff would be as if the `__proto__` key did not exist on either of the objects being diffed.)
66
- [#700](https://github.com/kpdecker/jsdiff/pull/700) *`diffJson` now correctly handles JSON objects containing a non-callable property named `toJSON`* - i.e. it gives such a property no special behaviour whatsoever, just as `JSON.stringify` doesn't. Previously, such properties caused an error to be thrown. (*Callable* `toJSON` properties continue to get the same special behaviour that `JSON.stringify` gives them.)
7+
- [#701](https://github.com/kpdecker/jsdiff/pull/701) *`applyPatch` with `autoConvertLineEndings` on will no longer consider a stray `\r` character occurring at the end of a file without a terminating `\n` character to be a Windows line ending*, and so will no longer strip it when converting from Windows to Unix line endings or fail to apply a Unix-style patch to a Windows file when the patch introduces such a stray `\r`.
78

89
## 9.0.0
910

test/patch/apply.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,19 @@ describe('patch/apply', function() {
14221422
.to.equal('');
14231423
});
14241424

1425+
it('should correctly apply a Unix patch whose final added line ends with a literal \\r (no newline at EOF) to a Windows file', () => {
1426+
// The patch is Unix-style (no \\r\\n line endings), but the added line's content ends with a
1427+
// literal '\\r' because the new file has no trailing newline. autoConvertLineEndings must
1428+
// recognise the patch as Unix (not Windows), convert it to match the CRLF source, and apply
1429+
// it correctly — without dropping the literal '\\r'. Previously, isUnix() returned false for
1430+
// such a patch, so no conversion was attempted and applyPatch returned false.
1431+
const oldFileUnix = 'line1\nline2\n';
1432+
const newFileUnix = 'line1\nline3\r'; // final line has literal CR and no trailing newline
1433+
const patch = structuredPatch('test', 'test', oldFileUnix, newFileUnix, undefined, undefined, {context: 0});
1434+
const oldFileWin = 'line1\r\nline2\r\n';
1435+
expect(applyPatch(oldFileWin, patch)).to.equal('line1\r\nline3\r');
1436+
});
1437+
14251438
it('should automatically convert a patch with Unix file endings to Windows when patching a Windows file', () => {
14261439
const oldFile = 'foo\r\nbar\r\nbaz\r\nqux\r\n';
14271440
const diffFile =

0 commit comments

Comments
 (0)