Skip to content

Commit

Permalink
fix: files deleted from /actual are not deleted from /expected
Browse files Browse the repository at this point in the history
  • Loading branch information
jusio committed Nov 28, 2020
1 parent b6239c7 commit 4a458a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/index.js
Expand Up @@ -101,7 +101,8 @@ const compareImages = (

const cleanupExpectedDir = (expectedDir, changedFiles) => {
const paths = changedFiles.map(image => path.join(expectedDir, image));
del(paths);
// force: true needed to allow deleting outside working directory
return del(paths, { force: true });
};

const aggregate = result => {
Expand All @@ -112,14 +113,17 @@ const aggregate = result => {
};

const updateExpected = ({ actualDir, expectedDir, diffDir, deletedImages, newImages, diffItems }) => {
cleanupExpectedDir(expectedDir, [...deletedImages, ...diffItems]);
return copyImages([...newImages, ...diffItems], {
actualDir,
expectedDir,
diffDir,
}).then(() => {
log.success(`\nAll images are updated. `);
});
return cleanupExpectedDir(expectedDir, [...deletedImages, ...diffItems])
.then(() =>
copyImages([...newImages, ...diffItems], {
actualDir,
expectedDir,
diffDir,
}),
)
.then(() => {
log.success(`\nAll images are updated. `);
});
};

module.exports = (params: RegParams) => {
Expand Down
33 changes: 33 additions & 0 deletions test/cli.test.js
Expand Up @@ -392,6 +392,39 @@ test.serial('should update images with `-U` option', async t => {
t.deepEqual(report, expected);
});

test.serial('should delete from /expected when they are missing from /actual with `-U` option', async t => {
const code = await new Promise(resolve => {
rimraf(`${WORKSPACE}/resource/actual/${SAMPLE_IMAGE}`, () => {
const p = spawn('./dist/cli.js', [
`${WORKSPACE}/resource/actual`,
`${WORKSPACE}/resource/expected`,
`${WORKSPACE}/diff`,
'-U',
]);
p.on('close', code => resolve(code));
// p.stdout.on('data', data => console.log(data.toString()));
p.stderr.on('data', data => console.error(data));
});
});
t.true(code === 0);
const report = replaceReportPath(JSON.parse(fs.readFileSync(`./reg.json`, 'utf8')));
const expected = {
actualItems: [],
expectedItems: [],
diffItems: [],
failedItems: [],
newItems: [],
deletedItems: [`${SAMPLE_IMAGE}`],
passedItems: [],
actualDir: `./${WORKSPACE}/resource/actual`,
expectedDir: `./${WORKSPACE}/resource/expected`,
diffDir: `./${WORKSPACE}/diff`,
};
t.deepEqual(report, expected);
const wasDeleted = !fs.existsSync(`${WORKSPACE}/resource/expected/${SAMPLE_IMAGE}`);
t.true(wasDeleted, `File ${SAMPLE_IMAGE} was not deleted from /expected`);
});

test.serial('should generate success report', async t => {
const stdout = await new Promise(resolve => {
const p = spawn('./dist/cli.js', [
Expand Down

0 comments on commit 4a458a1

Please sign in to comment.