Skip to content

Commit

Permalink
feat: adds support for a custom diff message (#439)
Browse files Browse the repository at this point in the history
Co-authored-by: sagiv.bengiat <[email protected]>
  • Loading branch information
sag1v and sagiv.bengiat authored Mar 6, 2022
1 parent a2f0d51 commit 3acc498
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const cli = meow(
-A, --enableAntialias. Enable antialias. If omitted false.
-X, --additionalDetection. Enable additional difference detection(highly experimental). Select "none" or "client" (default: "none").
-F, --from Generate report from json. Please specify json file. If set, only report will be output without comparing images.
-D, --customDiffMessage Pass custom massage that will logged to the terminal when there is a diff.
Examples
$ reg-cli /path/to/actual-dir /path/to/expected-dir /path/to/diff-dir -U -D ./reg.json
`,
Expand All @@ -52,6 +53,7 @@ const cli = meow(
A: 'enableAntialias',
X: 'additionalDetection',
F: 'from',
D: 'customDiffMessage'
},
},
);
Expand All @@ -77,6 +79,7 @@ const extendedErrors = !!cli.flags.extendedErrors;
const ignoreChange = !!cli.flags.ignoreChange;
const enableClientAdditionalDetection = cli.flags.additionalDetection === 'client';
const from = String(cli.flags.from || '');
const customDiffMessage = String(cli.flags.customDiffMessage || `\nInspect your code changes, re-run with \`-U\` to update them. `);

// If from option specified, generate report from json and exit.
if (from) {
Expand Down Expand Up @@ -151,7 +154,7 @@ observer.once('complete', ({ failedItems, deletedItems, newItems, passedItems })
if (newItems.length) log.info(`${GREEK_CROSS} ${newItems.length} file(s) appended.`);
if (passedItems.length) log.success(`${CHECK_MARK} ${passedItems.length} file(s) passed.`);
if (!update && (failedItems.length > 0 || (extendedErrors && (newItems.length > 0 || deletedItems.length > 0)))) {
log.fail(`\nInspect your code changes, re-run with \`-U\` to update them. `);
log.fail(customDiffMessage);
if (!ignoreChange) process.exit(1);
}
return process.exit(0);
Expand Down
49 changes: 48 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@ test.beforeEach(async t => {
await new Promise(resolve => copyfiles([`${RESOURCE}/reg.json`, WORKSPACE], resolve));
});

test.serial('should display default diff message', async t => {
const stdout = await new Promise(async resolve => {
const chunks = [];
rimraf(`${WORKSPACE}/resource/expected`, () => {
const p = spawn('./dist/cli.js', [
`${WORKSPACE}/resource/actual`,
`${WORKSPACE}/resource/expected`,
`${WORKSPACE}/diff`,
'-E',
]);
p.stdout.on('data', chunk => {
chunks.push(Buffer.from(chunk));
});
p.on('close', () => {
resolve(Buffer.concat(chunks).toString('utf8'));
});
});
});
t.true(stdout.indexOf('Inspect your code changes, re-run with `-U` to update them') !== -1);
});

test.serial.only('should display custom diff message', async t => {
const stdout = await new Promise(async resolve => {
const chunks = [];
rimraf(`${WORKSPACE}/resource/expected`, () => {
const p = spawn('./dist/cli.js', [
`${WORKSPACE}/resource/actual`,
`${WORKSPACE}/resource/expected`,
`${WORKSPACE}/diff`,
'-E',
'-D Custom diff message',
]);
p.stdout.on('data', chunk => {
chunks.push(Buffer.from(chunk));
});
p.on('close', () => {
resolve(Buffer.concat(chunks).toString('utf8'));
});
});
});
t.true(stdout.indexOf('Custom diff message') !== -1);
});

test.serial('should display error message when passing only 1 argument', async t => {
const stdout = await new Promise(resolve => {
const p = spawn('./dist/cli.js', ['./sample/actual']);
Expand Down Expand Up @@ -553,7 +596,11 @@ test.serial('perf', async t => {
copy(`${WORKSPACE}/resource/actual/sample(cal).png`, `${WORKSPACE}/resource/actual/sample${i}.png`, resolve),
),
new Promise(resolve =>
copy(`${WORKSPACE}/resource/expected/sample(cal).png`, `${WORKSPACE}/resource/expected/sample${i}.png`, resolve),
copy(
`${WORKSPACE}/resource/expected/sample(cal).png`,
`${WORKSPACE}/resource/expected/sample${i}.png`,
resolve,
),
),
]);
}
Expand Down

0 comments on commit 3acc498

Please sign in to comment.