Skip to content

Commit

Permalink
fix(remove-unused-screens): check if screens exist in fs
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Mar 31, 2022
1 parent 1024e58 commit edca7a2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lib/cli-commands/remove-unused-screens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,38 @@ async function handleUnusedScreens(screenPaths, fsTests, opts = {}) {
}

async function handleScreens(allScreenPaths, screensToRemove, {spinner, cliOpts} = {}) {
const screenPathsToRemoveLen = screensToRemove.paths.length;
const existentScreenPaths = await Promise.filter(screensToRemove.paths, async screenPath => {
try {
await fs.access(screenPath);
return true;
} catch (err) {
if (err.code === 'ENOENT') {
logger.warn(
chalk.red(
`Screen by path: "${screenPath}" is not found in your file system. ` +
'Try to rebase your branch or download more recent report from CI.',
),
);
return false;
}

throw err;
}
});

const screenPathsToRemoveLen = existentScreenPaths.length;

logger.log(
`Found ${chalk[screenPathsToRemoveLen > 0 ? 'red' : 'green'](screenPathsToRemoveLen)} ` +
`${screensToRemove.type} reference images out of ${chalk.bold(allScreenPaths.length)}`
);

if (_.isEmpty(screensToRemove.paths)) {
if (_.isEmpty(existentScreenPaths)) {
return;
}

spinner.start(`Calculating total size of ${screensToRemove.type} reference images`);
const bytes = (await Promise.map(screensToRemove.paths, (screenPath) => fs.stat(screenPath))).reduce((acc, {size}) => acc + size, 0);
const bytes = (await Promise.map(existentScreenPaths, (screenPath) => fs.stat(screenPath))).reduce((acc, {size}) => acc + size, 0);
spinner.succeed();

logger.log(`Total size of ${screensToRemove.type} reference images = ${chalk.red(filesize(bytes))}`);
Expand All @@ -158,7 +177,7 @@ async function handleScreens(allScreenPaths, screensToRemove, {spinner, cliOpts}
}, cliOpts);

if (shouldShowList) {
logger.log(`List of ${screensToRemove.type} reference images:\n${screensToRemove.paths.map((p) => chalk.red(p)).join('\n')}`);
logger.log(`List of ${screensToRemove.type} reference images:\n${existentScreenPaths.map((p) => chalk.red(p)).join('\n')}`);
}

const shouldRemove = await askQuestion({
Expand All @@ -173,7 +192,7 @@ async function handleScreens(allScreenPaths, screensToRemove, {spinner, cliOpts}
}

spinner.start(`Removing ${screensToRemove.type} reference images`);
await removeScreens(screensToRemove.paths);
await removeScreens(existentScreenPaths);
spinner.succeed();

logger.log(`${chalk.green(screenPathsToRemoveLen)} reference images were removed`);
Expand Down
30 changes: 30 additions & 0 deletions test/unit/lib/cli-commands/remove-unused-screens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ describe('lib/cli-commands/remove-unused-screens', () => {

beforeEach(() => {
sandbox.stub(fs, 'pathExists').resolves(true);
sandbox.stub(fs, 'access').resolves(undefined);
sandbox.stub(fs, 'move').resolves();
sandbox.stub(fs, 'stat').resolves({size: 1});
sandbox.stub(logger, 'log');
sandbox.stub(logger, 'error');
sandbox.stub(logger, 'warn');
sandbox.stub(process, 'exit');

getTestsFromFs = sandbox.stub().resolves(mkTestsTreeFromFs_());
Expand Down Expand Up @@ -204,6 +206,20 @@ describe('lib/cli-commands/remove-unused-screens', () => {
assert.calledOnceWith(identifyOutdatedScreens, foundScreenPaths, screenPatterns);
});

it('should not throw if outdated screen does not exist on fs', async () => {
findScreens.resolves(['/root/broId/testId/1.png', '/root/broId/outdatedTestId/2.png']);
identifyOutdatedScreens.returns(['/root/broId/outdatedTestId/2.png']);

const accessError = new Error('file does not exist');
accessError.code = 'ENOENT';
fs.access.withArgs('/root/broId/outdatedTestId/2.png').rejects(accessError);

await removeUnusedScreens_({program: mkProgram_({pattern: ['broId/**/*.png'], skipQuestions})});

assert.calledOnceWith(logger.warn, sinon.match('Screen by path: "/root/broId/outdatedTestId/2.png" is not found in your file system'));
assert.calledWith(logger.log, `Found ${chalk.green('0')} outdated reference images out of ${chalk.bold('2')}`);
});

it('should inform user about the number of outdated screens', async () => {
const screenPatterns = ['/root/broId/testId/*.png'];
getTestsFromFs.resolves(mkTestsTreeFromFs_({screenPatterns}));
Expand Down Expand Up @@ -427,6 +443,20 @@ describe('lib/cli-commands/remove-unused-screens', () => {
assert.calledOnceWith(identifyUnusedScreens, testsTreeFromFs, {hermione, mergedDbPath});
});

it('should not throw if unused screen does not exist on fs', async () => {
findScreens.resolves(['/root/broId/testId/1.png', '/root/broId/unusedTestId/2.png']);
identifyUnusedScreens.returns(['/root/broId/unusedTestId/2.png']);

const accessError = new Error('file does not exist');
accessError.code = 'ENOENT';
fs.access.withArgs('/root/broId/unusedTestId/2.png').rejects(accessError);

await removeUnusedScreens_({hermione, program, pluginConfig});

assert.calledOnceWith(logger.warn, sinon.match('Screen by path: "/root/broId/unusedTestId/2.png" is not found in your file system'));
assert.calledWith(logger.log, `Found ${chalk.green('0')} unused reference images out of ${chalk.bold('2')}`);
});

it('should inform user about the number of unused screens', async () => {
findScreens.resolves(['/root/usedTestId/img.png', '/root/unusedTestId/img.png']);
identifyUnusedScreens.returns(['/root/unusedTestId/img.png']);
Expand Down

0 comments on commit edca7a2

Please sign in to comment.