diff --git a/bin/d2l-test-runner.js b/bin/d2l-test-runner.js index 61a77b11..36b97234 100755 --- a/bin/d2l-test-runner.js +++ b/bin/d2l-test-runner.js @@ -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'; @@ -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(); diff --git a/src/server/cli/test-runner.js b/src/server/cli/test-runner.js index cc8f0876..1c807f7d 100755 --- a/src/server/cli/test-runner.js +++ b/src/server/cli/test-runner.js @@ -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.' }] } ]); diff --git a/src/server/cli/vdiff/migrate.js b/src/server/cli/vdiff/migrate.js index 430af78c..0e44cbce 100755 --- a/src/server/cli/vdiff/migrate.js +++ b/src/server/cli/vdiff/migrate.js @@ -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; @@ -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`);