-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feature: supertape: add support of worker_threads
1 parent
c19242f
commit 64c7acb
Showing
21 changed files
with
367 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
parentPort, | ||
workerData, | ||
} from 'node:worker_threads'; | ||
import {EventEmitter} from 'node:events'; | ||
|
||
const {assign} = Object; | ||
|
||
export const createCommunication = (argv) => { | ||
if (parentPort) | ||
return { | ||
parentPort, | ||
workerData, | ||
}; | ||
|
||
const {newWorker, newParentPort} = fakeWorkers(); | ||
|
||
return { | ||
worker: newWorker, | ||
parentPort: newParentPort, | ||
workerData: argv, | ||
}; | ||
}; | ||
|
||
export function fakeWorkers() { | ||
const newWorker = new EventEmitter(); | ||
const newParentPort = new EventEmitter(); | ||
|
||
assign(newWorker, { | ||
postMessage: (a) => { | ||
newParentPort.emit('message', a); | ||
}, | ||
}); | ||
|
||
assign(newParentPort, { | ||
postMessage: (a) => { | ||
newWorker.emit('message', a); | ||
}, | ||
}); | ||
|
||
return { | ||
newParentPort, | ||
newWorker, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {EventEmitter} from 'node:events'; | ||
|
||
export const createFormatter = (parentPort) => { | ||
const formatter = new EventEmitter(); | ||
|
||
formatter.on('start', ({total}) => { | ||
parentPort.postMessage(['start', { | ||
total, | ||
}]); | ||
}); | ||
|
||
formatter.on('test', ({test}) => { | ||
parentPort.postMessage(['test', { | ||
test, | ||
}]); | ||
}); | ||
|
||
formatter.on('test:end', ({count, total, failed, test}) => { | ||
parentPort.postMessage(['test:end', { | ||
total, | ||
count, | ||
failed, | ||
test, | ||
}]); | ||
}); | ||
|
||
formatter.on('comment', (message) => { | ||
parentPort.postMessage(['test:end', { | ||
message, | ||
}]); | ||
}); | ||
|
||
formatter.on('test:success', ({count, message}) => { | ||
parentPort.postMessage(['test:success', { | ||
count, | ||
message, | ||
}]); | ||
}); | ||
|
||
formatter.on('test:fail', ({at, count, message, operator, result, expected, output, errorStack}) => { | ||
parentPort.postMessage(['fail', { | ||
at, | ||
count, | ||
message, | ||
operator, | ||
result: JSON.stringify(result), | ||
expected: JSON.stringify(expected), | ||
output, | ||
errorStack, | ||
}]); | ||
}); | ||
|
||
formatter.on('end', ({count, passed, failed, skiped}) => { | ||
parentPort.postMessage(['end', { | ||
count, | ||
passed, | ||
failed, | ||
skiped, | ||
}]); | ||
}); | ||
|
||
return formatter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import keyPress from '@putout/cli-keypress'; | ||
import harnessCreator from '../lib/formatter/harness.js'; | ||
|
||
const {createHarness} = harnessCreator; | ||
|
||
const resolveFormatter = async (name) => await import(`@supertape/formatter-${name}`); | ||
|
||
export async function subscribe({name, quiet, exit, worker, stdout}) { | ||
const {isStop} = keyPress(); | ||
const harness = createHarness(await resolveFormatter(name)); | ||
|
||
if (!quiet) | ||
harness.pipe(stdout); | ||
|
||
worker.on('exit', (code) => { | ||
exit(code); | ||
}); | ||
|
||
worker.on('message', ([type, a]) => { | ||
harness.write({ | ||
type, | ||
...a, | ||
}); | ||
|
||
if (isStop()) | ||
worker.postMessage(['stop']); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,40 @@ | ||
#!/usr/bin/env node | ||
|
||
import process from 'node:process'; | ||
import cli from '../lib/cli.js'; | ||
import {createCommunication} from './communication.mjs'; | ||
import {subscribe} from './subscribe.mjs'; | ||
import {createFormatter} from './formatter.mjs'; | ||
import {parseArgs} from '../lib/cli/parse-args.js'; | ||
|
||
const { | ||
worker, | ||
parentPort, | ||
workerData, | ||
} = createCommunication(process.argv); | ||
|
||
const args = parseArgs(process.argv.slice(2)); | ||
|
||
const { | ||
stdout, | ||
stderr, | ||
exit, | ||
} = process; | ||
|
||
export default cli({ | ||
const workerFormatter = createFormatter(parentPort); | ||
|
||
if (worker) | ||
subscribe({ | ||
name: args.format, | ||
quiet: args.quiet, | ||
exit, | ||
worker, | ||
stdout, | ||
}); | ||
|
||
export default await cli({ | ||
stdout, | ||
stderr, | ||
exit, | ||
cwd: process.cwd(), | ||
argv: process.argv.slice(2), | ||
argv: workerData.slice(2), | ||
workerFormatter, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const createTrace = (parentPort) => (event, data) => { | ||
parentPort?.postMessage([event, data]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
test, | ||
stub, | ||
} from 'supertape'; | ||
import tryCatch from 'try-catch'; | ||
import {createTrace} from './trace.mjs'; | ||
|
||
test('supertape: bin: trace: parentPort', (t) => { | ||
const data = {}; | ||
const postMessage = stub(); | ||
const parentPort = { | ||
postMessage, | ||
}; | ||
|
||
const trace = createTrace(parentPort); | ||
|
||
trace('start', data); | ||
|
||
const args = [ | ||
['start', data], | ||
]; | ||
|
||
t.calledWith(postMessage, args); | ||
t.end(); | ||
}); | ||
|
||
test('supertape: bin: trace: no parentPort', (t) => { | ||
const data = {}; | ||
const trace = createTrace(); | ||
const [error] = tryCatch(trace, 'start', data); | ||
|
||
t.notOk(error); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env node | ||
|
||
import process from 'node:process'; | ||
import {Worker} from 'node:worker_threads'; | ||
import {parseArgs} from '../lib/cli/parse-args.js'; | ||
import {subscribe} from './subscribe.mjs'; | ||
|
||
const { | ||
cwd, | ||
exit, | ||
stdout, | ||
} = process; | ||
|
||
const args = parseArgs(process.argv.slice(2)); | ||
const write = stdout.write.bind(stdout); | ||
|
||
if (!args.worker) { | ||
await import('./supertape.mjs'); | ||
exit(); | ||
} | ||
|
||
const slave = new URL('./supertape.mjs', import.meta.url); | ||
|
||
const worker = new Worker(slave, { | ||
workerData: process.argv, | ||
stdin: true, | ||
}); | ||
|
||
await subscribe({ | ||
name: args.format, | ||
args, | ||
worker, | ||
exit, | ||
cwd, | ||
write, | ||
stdout, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const process = require('node:process'); | ||
const yargsParser = require('yargs-parser'); | ||
const {isArray} = Array; | ||
const maybeFirst = (a) => isArray(a) ? a.pop() : a; | ||
const maybeArray = (a) => isArray(a) ? a : [a]; | ||
|
||
const { | ||
SUPERTAPE_CHECK_DUPLICATES = '1', | ||
SUPERTAPE_CHECK_SCOPES = '1', | ||
SUPERTAPE_CHECK_ASSERTIONS_COUNT = '1', | ||
} = process.env; | ||
|
||
const yargsOptions = { | ||
configuration: { | ||
'strip-aliased': true, | ||
'strip-dashed': true, | ||
}, | ||
coerce: { | ||
require: maybeArray, | ||
format: maybeFirst, | ||
}, | ||
string: [ | ||
'format', | ||
'require', | ||
], | ||
boolean: [ | ||
'version', | ||
'help', | ||
'check-duplicates', | ||
'check-scopes', | ||
'check-assertions-count', | ||
'worker', | ||
], | ||
alias: { | ||
version: 'v', | ||
format: 'f', | ||
help: 'h', | ||
require: 'r', | ||
checkDuplicates: 'd', | ||
checkScopes: 's', | ||
checkAssertionsCount: 'a', | ||
}, | ||
default: { | ||
format: 'progress-bar', | ||
require: [], | ||
checkDuplicates: SUPERTAPE_CHECK_DUPLICATES !== '0', | ||
checkScopes: SUPERTAPE_CHECK_SCOPES !== '0', | ||
checkAssertionsCount: SUPERTAPE_CHECK_ASSERTIONS_COUNT !== '0', | ||
worker: true, | ||
}, | ||
}; | ||
|
||
module.exports.yargsOptions = yargsOptions; | ||
module.exports.parseArgs = (argv) => yargsParser(argv, yargsOptions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const {parentPort, workerData} = require('node:worker_threads'); | ||
|
||
const {EventEmitter} = require('node:events'); | ||
const process = require('node:process'); | ||
|
||
const {assign} = Object; | ||
|
||
module.exports.createCommunication = () => { | ||
if (parentPort) | ||
return { | ||
parentPort, | ||
workerData, | ||
}; | ||
|
||
const newWorker = new EventEmitter(); | ||
const newParentPort = new EventEmitter(); | ||
|
||
assign(newWorker, { | ||
postMessage: (a) => { | ||
newParentPort.emit('message', a); | ||
}, | ||
}); | ||
|
||
assign(newParentPort, { | ||
postMessage: (a) => { | ||
newWorker.emit('message', a); | ||
}, | ||
}); | ||
|
||
return { | ||
worker: newWorker, | ||
parentPort: newParentPort, | ||
workerData: process.argv, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters