Skip to content

Commit

Permalink
feature: supertape: isStop
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jan 26, 2024
1 parent 64c7acb commit 0e55c2f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/supertape/bin/is-stop.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fullstore from 'fullstore';

const noop = () => {};

export const createIsStop = (parentPort) => {
if (!parentPort)
return noop;

const isStop = fullstore(false);

parentPort?.on('message', ([event]) => {
if (event === 'stop')
isStop(true);
});

return isStop;
};
45 changes: 45 additions & 0 deletions packages/supertape/bin/is-stop.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
test,
stub,
} from 'supertape';
import {createIsStop} from './is-stop.mjs';

test('putout: bin: isStop: parentPort', (t) => {
const on = stub();
const parentPort = {
on,
};

const args = ['message', stub()];

createIsStop(parentPort);

t.calledWith(on, args);
t.end();
});

test('putout: bin: isStop: no parentPort', (t) => {
const isStop = createIsStop();
const result = isStop();

t.notOk(result);
t.end();
});

test('putout: bin: isStop: parentPort: isStop', (t) => {
const on = stub();
const parentPort = {
on,
};

const isStop = createIsStop(parentPort);
const [args] = on.args;
const [, listener] = args;

listener(['stop']);

const result = isStop();

t.ok(result);
t.end();
});
5 changes: 4 additions & 1 deletion packages/supertape/bin/supertape.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import process from 'node:process';
import cli from '../lib/cli.js';
import {createCommunication} from './communication.mjs';
import {subscribe} from './subscribe.mjs';
import {createIsStop} from './is-stop.mjs';
import {createFormatter} from './formatter.mjs';
import {parseArgs} from '../lib/cli/parse-args.js';
import cli from '../lib/cli.js';

const {
worker,
Expand All @@ -20,6 +21,7 @@ const {
} = process;

const workerFormatter = createFormatter(parentPort);
const isStop = createIsStop(parentPort);

if (worker)
subscribe({
Expand All @@ -37,4 +39,5 @@ export default await cli({
cwd: process.cwd(),
argv: workerData.slice(2),
workerFormatter,
isStop,
});
5 changes: 3 additions & 2 deletions packages/supertape/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ const filesCount = fullstore(0);

const {SUPERTAPE_CHECK_SKIPED = '0'} = process.env;

module.exports = async ({argv, cwd, stdout, stderr, exit, workerFormatter}) => {
const {isStop} = keypress();
module.exports = async ({argv, cwd, stdout, stderr, exit, isStop, workerFormatter}) => {
isStop = isStop || keypress().isStop;

const [error, result] = await tryToCatch(cli, {
argv,
cwd,
Expand Down

0 comments on commit 0e55c2f

Please sign in to comment.