Skip to content

Commit

Permalink
Add binary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bearfriend committed Aug 3, 2023
1 parent 436634c commit 716cf93
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
10 changes: 6 additions & 4 deletions bin/d2l-test-runner.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env node
import { argv, stdout } from 'node:process';
import commandLineArgs from 'command-line-args';
import process from 'node:process';
import { runner } from '../src/server/cli/test-runner.js';

const cli = commandLineArgs({ name: 'subcommand', defaultOption: true }, { stopAtFirstUnknown: true });
const { argv, stdout } = process;

const cli = commandLineArgs({ name: 'subcommand', defaultOption: true }, { stopAtFirstUnknown: true, argv });

if (cli.subcommand === 'vdiff') {
const vdiff = commandLineArgs({ name: 'subcommand', defaultOption: true }, { stopAtFirstUnknown: true, argv: cli._unknown || [] });
Expand All @@ -15,10 +17,10 @@ if (cli.subcommand === 'vdiff') {
stdout.write('\nGenerating vdiff goldens...\n');
runTests();
} else if (vdiff.subcommand === 'report') {
import('../src/server/cli/vdiff/report.js');
await import('../src/server/cli/vdiff/report.js');
} else if (vdiff.subcommand === 'migrate') {
const { migrate } = await import('../src/server/cli/vdiff/migrate.js');
await migrate(vdiff._unknown);
await migrate.start(vdiff._unknown);
} else {
stdout.write(`\nfatal: unknown subcomamnd: ${vdiff.subcommand}\n`);
}
Expand Down
7 changes: 7 additions & 0 deletions src/server/bail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const bail = new Set();
const bailOn = key => bail.delete(key);

export {
bail,
bailOn
};
6 changes: 5 additions & 1 deletion src/server/cli/vdiff/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { glob } from 'glob';
import { PATHS } from '../../visual-diff-plugin.js';
import { stdout } from 'node:process';

export async function migrate(argv = []) {
async function start(argv = []) {
const { pattern = './**' } = commandLineArgs({ name: 'pattern', type: String, defaultOption: true }, { partial: true, argv });
const oldSuffix = 'screenshots/ci/golden';
const dirs = await glob(`${pattern}/${oldSuffix}`, { ignore: 'node_modules/**', posix: true });
Expand Down Expand Up @@ -40,3 +40,7 @@ export async function migrate(argv = []) {

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

export const migrate = {
start
};
3 changes: 3 additions & 0 deletions src/server/cli/vdiff/report.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env node
import { bailOn } from '../../bail.js';
import { PATHS } from '../../visual-diff-plugin.js';
import { startDevServer } from '@web/dev-server';

bailOn('report') ||

await startDevServer({
config: {
nodeResolve: false,
Expand Down
65 changes: 63 additions & 2 deletions test/bin/d2l-test-runner.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,79 @@
import { assert, restore, stub } from 'sinon';
import { argv } from 'node:process';
import { bail } from '../../src/server/bail.js';
import { expect } from 'chai';
import { migrate } from '../../src/server/cli/vdiff/migrate.js';
import process from 'node:process';
import { runner } from '../../src/server/cli/test-runner.js';

const { argv, stdout } = process;

const run = async() => {
await import(`../../bin/d2l-test-runner.js?${Math.random()}`);
};

describe('d2l-test-runner', () => {

beforeEach(() => {
bail.clear();
});

it('starts test runner with options', async() => {
const opts = { my: 'options' };
const optionsStub = stub(runner, 'getOptions').returns(opts);
const startStub = stub(runner, 'start');
await import('../../bin/d2l-test-runner.js');
await run();

assert.calledOnceWithExactly(optionsStub, argv);
assert.calledOnceWithExactly(startStub, opts);

restore();
});

it('runs report.js', async() => {
const optionsStub = stub(runner, 'getOptions');
const startStub = stub(runner, 'start');

bail.add('report');
argv.splice(0, argv.length, 'fake-node', 'fake-test-runner', 'vdiff', 'report');
await run();

expect(bail).to.not.include('report');

assert.notCalled(optionsStub);
assert.notCalled(startStub);

restore();
});

it('generates goldens', async() => {
const optionsStub = stub(runner, 'getOptions');
const startStub = stub(runner, 'start');
const stdoutStub = stub(stdout, 'write');

argv.splice(0, argv.length, 'fake-node', 'fake-test-runner', 'vdiff', 'golden');
await run();

expect(argv).to.deep.equal(['fake-node', 'fake-test-runner', 'vdiff', '--golden']);
assert.calledOnceWithExactly(optionsStub, argv);
assert.calledOnce(startStub);
assert.calledOnceWithExactly(stdoutStub, '\nGenerating vdiff goldens...\n');

restore();
});

it('calls migrate()', async() => {
const migrateStub = stub(migrate, 'start');
const optionsStub = stub(runner, 'getOptions');
const startStub = stub(runner, 'start');

argv.splice(0, argv.length, 'fake-node', 'fake-test-runner', 'vdiff', 'migrate', './test/**/dir');
await run();

assert.calledOnceWithExactly(migrateStub, ['./test/**/dir']);
assert.notCalled(optionsStub);
assert.notCalled(startStub);

restore();
});

});

0 comments on commit 716cf93

Please sign in to comment.