Skip to content

Commit

Permalink
Don't die on diffing file deletions (again)
Browse files Browse the repository at this point in the history
Makes `null` content (file deletion):

1. not identify as binary (so default diff style is nicer)
2. not break binary diff if it is

Addresses regression(?) of yeoman#950
  • Loading branch information
dbushong committed Jul 5, 2017
1 parent edc2bf2 commit 730125c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/util/binary-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const Table = require('cli-table');

exports.isBinary = (existingFilePath, newFileContents) => {
const existingHeader = readChunk.sync(existingFilePath, 0, 512);
return istextorbinary.isBinarySync(undefined, existingHeader) || istextorbinary.isBinarySync(undefined, newFileContents);
return istextorbinary.isBinarySync(undefined, existingHeader) ||
(newFileContents && istextorbinary.isBinarySync(undefined, newFileContents));
};

exports.diff = (existingFilePath, newFileContents) => {
Expand All @@ -19,6 +20,10 @@ exports.diff = (existingFilePath, newFileContents) => {

let sizeDiff;

if (!newFileContents) {
newFileContents = Buffer.from([]);
}

if (existingStat.size > newFileContents.length) {
sizeDiff = '-';
} else {
Expand Down
52 changes: 52 additions & 0 deletions test/conflicter.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ describe('Conflicter', () => {
});
});

it('shows old content for deleted text files', done => {
const testAdapter = new TestAdapter({action: 'diff'});
const conflicter = new Conflicter(testAdapter);
const _prompt = testAdapter.prompt.bind(testAdapter);
const promptStub = sinon.stub(testAdapter, 'prompt', (prompts, resultHandler) => {
if (promptStub.calledTwice) {
const stubbedResultHandler = result => {
result.action = 'write';
return resultHandler(result);
};

return _prompt(prompts, stubbedResultHandler);
}
return _prompt(prompts, resultHandler);
});

conflicter.collision({
path: path.join(__dirname, 'fixtures/foo.js'),
contents: null
}, () => {
sinon.assert.neverCalledWithMatch(testAdapter.log.writeln, /Existing.*Replacement.*Diff/);
sinon.assert.called(testAdapter.diff);
done();
});
});

it('displays custom diff for binary files', done => {
const testAdapter = new TestAdapter({action: 'diff'});
const conflicter = new Conflicter(testAdapter);
Expand All @@ -180,5 +206,31 @@ describe('Conflicter', () => {
done();
});
});

it('displays custom diff for deleted binary files', done => {
const testAdapter = new TestAdapter({action: 'diff'});
const conflicter = new Conflicter(testAdapter);
const _prompt = testAdapter.prompt.bind(testAdapter);
const promptStub = sinon.stub(testAdapter, 'prompt', (prompts, resultHandler) => {
if (promptStub.calledTwice) {
const stubbedResultHandler = result => {
result.action = 'write';
return resultHandler(result);
};

return _prompt(prompts, stubbedResultHandler);
}
return _prompt(prompts, resultHandler);
});

conflicter.collision({
path: path.join(__dirname, 'fixtures/yeoman-logo.png'),
contents: null
}, () => {
sinon.assert.calledWithMatch(testAdapter.log.writeln, /Existing.*Replacement.*Diff/);
sinon.assert.notCalled(testAdapter.diff);
done();
});
});
});
});

0 comments on commit 730125c

Please sign in to comment.