Skip to content

Commit

Permalink
feature(supertape) add fail formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Dec 11, 2020
1 parent 751e688 commit 3a88908
Show file tree
Hide file tree
Showing 18 changed files with 813 additions and 402 deletions.
6 changes: 3 additions & 3 deletions bin/supertape.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

const cli = require('../lib/cli.js');

const {stdout} = process;
const write = stdout.write.bind(stdout);
const {stdout, exit} = process;

module.exports = cli({
write,
stdout,
exit,
cwd: process.cwd(),
argv: process.argv.slice(2),
});
Expand Down
25 changes: 20 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

const {resolve: resolvePath} = require('path');
const {once} = require('events');

const yargsParser = require('yargs-parser');
const glob = require('glob');
const fullstore = require('fullstore');

const supertape = require('../lib/supertape.js');
const supertape = require('..');

const {resolve} = require;
const {isArray} = Array;
Expand All @@ -15,39 +16,48 @@ const maybeArray = (a) => isArray(a) ? a : [a];
const removeDuplicates = (a) => Array.from(new Set(a));
const filesCount = fullstore(0);

module.exports = async ({argv, cwd, write}) => {
module.exports = async ({argv, cwd, stdout, exit}) => {
const args = yargsParser(argv, {
coerce: {
require: maybeArray,
},
string: [
'require',
'formatter',
],
boolean: [
'version',
],
alias: {
r: 'require',
v: 'version',
f: 'formatter',
},
default: {
require: [],
formatter: 'tap',
},
});

if (args.version)
return write(`v${require('../package').version}\n`);
return stdout.write(`v${require('../package').version}\n`);

for (const module of args.require) {
await import(resolve(module, {
paths: [cwd],
}));
}

const {formatter} = args;

supertape.init({
loop: false,
run: false,
quiet: true,
formatter,
});

supertape.createStream().pipe(stdout);

const allFiles = [];
for (const arg of args._) {
const files = glob.sync(arg);
Expand All @@ -63,10 +73,15 @@ module.exports = async ({argv, cwd, write}) => {

filesCount(files.length);

let code = 0;

if (promises.length) {
await Promise.all(promises);
supertape.loop();
const [{failed}] = await once(supertape.run(), 'end');
code = failed ? 1 : 0;
}

exit(code);
};

module.exports._filesCount = filesCount;
Expand Down
157 changes: 116 additions & 41 deletions lib/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

const {join} = require('path');
const {Transform} = require('stream');
const {EventEmitter} = require('events');

const stub = require('@cloudcmd/stub');
const mockRequire = require('mock-require');
const tryToCatch = require('try-to-catch');
const pullout = require('pullout');
const wait = require('@iocmd/wait');

const test = require('./supertape.js');
const cli = require('./cli');
Expand All @@ -15,9 +19,8 @@ const {assign} = Object;
test('supertape: cli: -r', async (t) => {
const argv = ['-r', 'hello'];

const [error] = await tryToCatch(cli, {
const [error] = await runCli({
argv,
cwd: __dirname,
});

t.ok(error.message.includes(`Cannot find module 'hello'`));
Expand All @@ -27,15 +30,17 @@ test('supertape: cli: -r', async (t) => {
test('supertape: cli: -v', async (t) => {
const {version} = require('../package');
const argv = ['-v'];
const write = stub();
const stdout = createStream();

await cli({
argv,
cwd: __dirname,
write,
stdout,
});

t.ok(write.calledWith(`v${version}\n`));
stdout.push(null);
const result = await pullout(stdout);

t.equal(result, `v${version}\n`);
t.end();
});

Expand All @@ -47,10 +52,8 @@ test('supertape: bin: cli: glob', async (t) => {
sync,
});

const cli = reRequire('./cli');
await cli({
await runCli({
argv,
cwd: __dirname,
});

stopAll();
Expand All @@ -67,10 +70,8 @@ test('supertape: bin: cli: glob: a couple', async (t) => {
sync,
});

const cli = reRequire('./cli');
await cli({
await runCli({
argv,
cwd: __dirname,
});

stopAll();
Expand All @@ -81,85 +82,159 @@ test('supertape: bin: cli: glob: a couple', async (t) => {
t.end();
});

test('supertape: bin: cli: loop', async (t) => {
test('supertape: bin: cli: run', async (t) => {
const name = join(__dirname, 'fixture/cli.js');
const argv = [name, name];

const test = stub();
const init = stub();
const loop = stub();
const run = stub();

const {createStream} = reRequire('..');

assign(test, {
init,
loop,
run,
createStream,
});
mockRequire('./supertape', test);

const cli = reRequire('./cli');
await cli({
mockRequire('..', test);

await runCli({
argv,
cwd: __dirname,
});

stopAll();

t.equal(loop.callCount, 1, 'loop always called once');
t.equal(run.callCount, 1, 'run always called once');
t.end();
});

test('supertape: bin: cli: loop', async (t) => {
test('supertape: bin: cli: files count', async (t) => {
const name = join(__dirname, 'fixture/cli.js');
const argv = [name, name];

const emitter = new EventEmitter();

const test = stub();
const init = stub();
const loop = stub();
const run = stub().returns(emitter);

assign(test, {
init,
loop,
run,
createStream,
});

mockRequire('./supertape', test);

const cli = reRequire('./cli');
await cli({
const emit = emitter.emit.bind(emitter);
const [[error, cli]] = await Promise.all([
runCli({
argv,
}),
wait(emit, 'end', {failed: 0}),
]);

const result = cli._filesCount();
const expected = 1;

stopAll();

t.equal(result, expected, 'should process 1 file');
t.end();
});

test('supertape: bin: cli: successs', async (t) => {
const name = join(__dirname, 'fixture/cli-success.js');
const argv = [name, name];

const supertape = reRequire('./supertape');
const init = stub();
const exit = stub();

mockRequire('supertape', {
...supertape,
init,
});

supertape.init({
quiet: true,
});

await runCli({
argv,
cwd: __dirname,
exit,
});

stopAll();

t.equal(loop.callCount, 1, 'test always called once');
t.ok(exit.calledWith(0), 'should call exit with 0');
t.end();
});

test('supertape: bin: cli: files count', async (t) => {
test('supertape: bin: cli: fail', async (t) => {
const name = join(__dirname, 'fixture/cli.js');
const argv = [name, name];

const test = stub();
const init = stub();
const loop = stub();
const exit = stub();

const test = stub();
const emitter = new EventEmitter();
const run = stub().returns(emitter);

assign(test, {
init,
loop,
createStream,
run,
});

mockRequire('./supertape', test);
mockRequire('..', test);

const cli = reRequire('./cli');
await cli({
argv,
cwd: __dirname,
});

const result = cli._filesCount();
const expected = 1;
const emit = emitter.emit.bind(emitter);
await Promise.all([
runCli({
argv,
exit,
}),
wait(emit, 'end', {
failed: 1,
}),
]);

stopAll();

t.equal(result, expected, 'should process 1 file');
t.ok(exit.calledWith(1), 'should call exit with 1');
t.end();
});

function createStream() {
return new Transform({
transform(chunk, encoding, callback) {
this.push(chunk);
callback();
},
});
}

async function runCli(options) {
const {
argv = [],
stdout = createStream(),
cwd = __dirname,
exit = stub(),
} = options;

const cli = reRequire('./cli');

const [error] = await tryToCatch(cli, {
argv,
stdout,
cwd,
exit,
});

return [error, cli];
}

2 changes: 1 addition & 1 deletion lib/diff.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('supertape: diff', (t) => {
const {length} = diffed.split('\n');
const expected = 2;

t.equal(expected, length);
t.equal(length, expected);
t.end();
});

7 changes: 7 additions & 0 deletions lib/fixture/cli-success.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test = require('../supertape');

test('hello world', (t) => {
t.equal(1, 1);
t.end();
});

1 change: 1 addition & 0 deletions lib/fixture/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require('../supertape');

test('hello world', (t) => {
t.equal(1, 2);
t.end();
});

Loading

0 comments on commit 3a88908

Please sign in to comment.