Skip to content

Commit

Permalink
Add migrate-local subcommand (#144)
Browse files Browse the repository at this point in the history
* Add migrate-local subcommand

* Fix output path for migrating local goldens

* Use existing pattern when running old visual-diff tests

* Add some output (#148)

* Add migrate-local to --help

---------

Co-authored-by: Stacey Van Herk <[email protected]>
  • Loading branch information
bearfriend and svanherk committed Aug 24, 2023
1 parent 9ae0b76 commit 801b1f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
15 changes: 14 additions & 1 deletion bin/d2l-test-runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import commandLineArgs from 'command-line-args';
import { execSync } from 'node:child_process';
import process from 'node:process';
import { runner } from '../src/server/cli/test-runner.js';

Expand All @@ -21,8 +22,20 @@ if (cli.subcommand === 'vdiff') {
} else if (vdiff.subcommand === 'migrate') {
const { migrate } = await import('../src/server/cli/vdiff/migrate.js');
await migrate.start(vdiff._unknown);
} else if (vdiff.subcommand === 'migrate-local') {

const { pattern = './**' } = commandLineArgs({ name: 'pattern', type: String, defaultOption: true }, { partial: true, argv: vdiff._unknown || [] });

stdout.write('\nInstalling @brightspace-ui/visual-diff\n');
execSync('npm install @brightspace-ui/visual-diff@14 --no-save');
stdout.write(`\nRunning existing tests for pattern '${pattern}/*.visual-diff.js'\n`);
execSync(`npx mocha '${pattern}/*.visual-diff.js' -t 10000 --golden`);
stdout.write('\nTest run complete.\n');

const { migrate } = await import('../src/server/cli/vdiff/migrate.js');
await migrate.start(vdiff._unknown, true);
} else {
stdout.write(`\nfatal: unknown subcomamnd: ${vdiff.subcommand}\n`);
stdout.write(`\nfatal: unknown subcommand: ${vdiff.subcommand}\n`);
}
} else {
runTests();
Expand Down
4 changes: 4 additions & 0 deletions src/server/cli/test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ async function getTestRunnerOptions(argv = []) {
{
example: 'vdiff migrate [directory]',
desc: 'Migrate from @brightspace-ui/visual-diff. Restrict which goldens are migrated with a directory glob.'
},
{
example: 'vdiff migrate-local [directory]',
desc: 'Generate goldens with @brightspace-ui/visual-diff for local comparison while migrating tests. Restrict which goldens are generated with a directory glob.'
}]
}
]);
Expand Down
15 changes: 11 additions & 4 deletions src/server/cli/vdiff/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { glob } from 'glob';
import { PATHS } from '../../visual-diff-plugin.js';
import { stdout } from 'node:process';

async function start(argv = []) {
async function start(argv = [], local = false) {
const { pattern = './**' } = commandLineArgs({ name: 'pattern', type: String, defaultOption: true }, { partial: true, argv });
const oldSuffix = 'screenshots/ci/golden';
const oldSuffix = local ? 'screenshots/golden' : 'screenshots/ci/golden';

stdout.write(`\nMigrating goldens for pattern '${pattern}/${oldSuffix}'\n`);
const dirs = await glob(`${pattern}/${oldSuffix}`, { ignore: 'node_modules/**', posix: true });
let fileCount = 0;

Expand All @@ -30,12 +32,17 @@ async function start(argv = []) {
.replace(/^d2l-/, '')
.replace(new RegExp(`^${dirName}-`), '');

const newDir = dir.replace(`${oldSuffix}/${dirName}`, `${PATHS.GOLDEN}/${dirName}/chromium`);
const newDir = local ?
`${PATHS.VDIFF_ROOT}/${dir.replace(`${oldSuffix}/${dirName}`, `${dirName}/${PATHS.GOLDEN}/chromium`)}` :
dir.replace(`${oldSuffix}/${dirName}`, `${PATHS.GOLDEN}/${dirName}/chromium`);

await mkdir(newDir, { recursive: true });
return rename(file, join(newDir, newName));
}));
return rm(normalize(join(dir, '..', '..')), { recursive: true });

if (!local) {
return rm(normalize(join(dir, '..', '..')), { recursive: true });
}
}));

stdout.write(`\nMigrated ${fileCount} ${fileCount === 1 ? 'golden' : 'goldens'} found in ${dirs.length} test ${dirs.length === 1 ? 'directory' : 'directories'}\n`);
Expand Down

0 comments on commit 801b1f4

Please sign in to comment.