Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document and test options to createPatch #345

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ npm install diff --save
* `newStr` : New string value
* `oldHeader` : Additional information to include in the old file header
* `newHeader` : Additional information to include in the new file header
* `options` : An object with options. Currently, only `context` is supported and describes how many lines of context should be included.
* `options` : An object with options.
- `context` describes how many lines of context should be included.
- `ignoreWhitespace`: `true` to ignore leading and trailing whitespace.
- `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output.

* `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.

Expand Down
72 changes: 72 additions & 0 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,78 @@ describe('patch/create', function() {
const diffResult = createTwoFilesPatch('foo', 'bar', '', '');
expect(diffResult).to.equal(expectedResult);
});

describe('ignoreWhitespace', function() {
it('ignoreWhitespace: false', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,2 +1,2 @@\n'
+ '-line \n'
+ '- line\n'
+ '\\ No newline at end of file\n'
+ '+line\n'
+ '+line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: false});
expect(diffResult).to.equal(expectedResult);
});

it('ignoreWhitespace: true', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n';

const diffResult = createPatch('testFileName', 'line \n\ line', 'line\n\line', undefined, undefined, {ignoreWhitespace: true});
expect(diffResult).to.equal(expectedResult);
});
});

describe('newlineIsToken', function() {
it('newlineIsToken: false', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,2 +1,2 @@\n'

// Diff is shown as entire row, eventhough text is unchanged
+ '-line\n'
+ '+line\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: false});
expect(diffResult).to.equal(expectedResult);
});

it('newlineIsToken: true', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,3 +1,3 @@\n'
+ ' line\n'

// Newline change is shown as a single diff
+ '-\n'
+ '+\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: true});
expect(diffResult).to.equal(expectedResult);
});
});
});

describe('#structuredPatch', function() {
Expand Down