Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Dec 9, 2022
1 parent 420926e commit 3ac1e2b
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 129 deletions.
7 changes: 3 additions & 4 deletions lib/internal/main/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ if (isUsingInspector()) {
inspectPort = process.debugPort;
}

const tapStream = run({ concurrency, inspectPort, watch: getOptionValue('--watch') });
const reporters = setupTestReporters(tapStream);
reporters.pipe(process.stdout);
tapStream.once('test:fail', () => {
const reporterStream = run({ concurrency, inspectPort, watch: getOptionValue('--watch') });
reporterStream.once('test:fail', () => {
process.exitCode = kGenericUserError;
});
setupTestReporters(reporterStream);
7 changes: 3 additions & 4 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function setup(root) {
}

root.startTime = hrtime();
root.reporter.version();
root.reporter.start();

wasRootSetup.add(root);
return root;
Expand All @@ -120,11 +120,10 @@ let globalRoot;
function getGlobalRoot() {
if (!globalRoot) {
globalRoot = createTestTree();
const reporter = setupTestReporters(globalRoot.reporter);
reporter.pipe(process.stdout);
reporter.once('test:fail', () => {
globalRoot.reporter.once('test:fail', () => {
process.exitCode = kGenericUserError;
});
setupTestReporters(globalRoot.reporter);
}
return globalRoot;
}
Expand Down
16 changes: 8 additions & 8 deletions lib/internal/test_runner/reporter_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ReporterStream extends Readable {
this.#canPush = true;

while (this.#buffer.length > 0) {
const chunk = ArrayPrototypeShift(this.#buffer);
const obj = ArrayPrototypeShift(this.#buffer);

if (!this.#tryPush(chunk)) {
if (!this.#tryPush(obj)) {
return;
}
}
Expand All @@ -35,8 +35,8 @@ class ReporterStream extends Readable {
this.#emit('test:pass', { __proto__: null, name, nesting, testNumber, details, ...directive });
}

plan() {
// NOT IMPLEMENTED
plan(nesting, count, explanation) {
this.#emit('test:plan', { __proto__: null, nesting, count, explanation });
}

getSkip(reason) {
Expand All @@ -48,15 +48,15 @@ class ReporterStream extends Readable {
}

subtest(nesting, name) {
this.#emit('test:subtest', { nesting, name });
this.#emit('test:subtest', { __proto__: null, nesting, name });
}

diagnostic(nesting, message) {
this.#emit('test:diagnostic', { nesting, message });
this.#emit('test:diagnostic', { __proto__: null, nesting, message });
}

version() {
// NOT IMPLEMENTED
start() {
this.#emit('test:start', { __proto__: null });
}

#emit(type, data) {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const { kEmptyObject } = require('internal/util');
const { createTestTree } = require('internal/test_runner/harness');
const { kSubtestsFailed, Test } = require('internal/test_runner/test');
const { TapParser } = require('internal/test_runner/tap_parser');
const { kDefaultIndent } = require('internal/test_runner/tap_stream');
const { TokenKind } = require('internal/test_runner/tap_lexer');

const {
Expand Down Expand Up @@ -130,6 +129,8 @@ function getRunArgs({ path, inspectPort }) {
return argv;
}

const kDefaultIndent = ' '; // 4 spaces

class FileTest extends Test {
#buffer = [];
#handleReportItem({ kind, node, nesting = 0 }) {
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeUnshift,
Boolean,
FunctionPrototype,
MathMax,
Number,
Expand All @@ -34,7 +33,6 @@ const {
} = require('internal/errors');
const { getOptionValue } = require('internal/options');
const { MockTracker } = require('internal/test_runner/mock');
const { TapStream } = require('internal/test_runner/tap_stream');
const { ReporterStream } = require('internal/test_runner/reporter_stream');
const {
convertStringToRegExp,
Expand Down Expand Up @@ -67,7 +65,6 @@ const kHookFailure = 'hookFailed';
const kDefaultTimeout = null;
const noop = FunctionPrototype;
const isTestRunner = getOptionValue('--test');
const hasReporters = Boolean(getOptionValue('--test-reporter'));
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only');
const testNamePatternFlag = isTestRunner ? null :
getOptionValue('--test-name-pattern');
Expand Down Expand Up @@ -191,7 +188,7 @@ class Test extends AsyncResource {
this.concurrency = 1;
this.nesting = 0;
this.only = testOnlyFlag;
this.reporter = hasReporters ? new ReporterStream() : new TapStream();
this.reporter = new ReporterStream();
this.runOnlySubtests = this.only;
this.testNumber = 0;
this.timeout = kDefaultTimeout;
Expand Down
64 changes: 55 additions & 9 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';
const { RegExp, RegExpPrototypeExec } = primordials;
const { ObjectGetOwnPropertyDescriptor, RegExp, RegExpPrototypeExec, SafeMap } = primordials;
const { basename } = require('path');
const { createWriteStream } = require('fs');
const { createDeferredPromise } = require('internal/util');
const { getOptionValue } = require('internal/options');
const specReporter = require('test/reporter/spec');
const dotReporter = require('test/reporter/dot');
const tapReporter = require('test/reporter/tap');

const {
codes: {
ERR_INVALID_ARG_VALUE,
Expand All @@ -11,7 +16,6 @@ const {
kIsNodeError,
} = require('internal/errors');
const { compose } = require('stream');
const { Module } = require('internal/modules/cjs/loader');

const kMultipleCallbackInvocations = 'multipleCallbackInvocations';
const kRegExpPattern = /^\/(.*)\/([a-z]*)$/;
Expand Down Expand Up @@ -77,14 +81,56 @@ function convertStringToRegExp(str, name) {
}
}

let _module;
function setupTestReporters(reporter) {
if (getOptionValue('--test-reporter')) {
_module ??= new Module('node:test');
const reporters = _module.require(getOptionValue('--test-reporter'));
return compose(reporter, reporters);
const kBuiltinDestinations = new SafeMap([
['stdout', process.stdout],
['stderr', process.stderr],
]);

const kBuiltinReporters = new SafeMap([
['spec', specReporter],
['dot', dotReporter],
['tap', tapReporter],
]);

const kDefaultReporter = 'tap';
const kDefaltDestination = 'stdout';

function getReportersMap(reporters, destinations) {
const result = [];
reporters.forEach((name, i) => {
const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]);
let reporter = kBuiltinReporters.get(name) ?? require(name);

if (ObjectGetOwnPropertyDescriptor(reporter.prototype, 'constructor')) {
reporter = new reporter();
}

result.push({ reporter, destination });
});
return result;
}


function setupTestReporters(reporterStream) {
const destinations = getOptionValue('--test-reporter-destination');
const reporters = getOptionValue('--test-reporter');

if (reporters.length === 0 && destinations.length === 0) {
reporters.push(kDefaultReporter);
}

if (reporters.length === 1 && destinations.length === 0) {
destinations.push(kDefaltDestination);
}

if (destinations.length !== reporters.length) {
throw new ERR_INVALID_ARG_VALUE('The number of reporters and destinations must match');
}

const reportersMap = getReportersMap(reporters, destinations);
for (const { reporter, destination } of reportersMap) {
compose(reporterStream, reporter).pipe(destination);
}
return reporter;
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/test/reporter/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ class SpecReporter extends Transform {
}
}

module.exports = new SpecReporter();
module.exports = SpecReporter;
Loading

0 comments on commit 3ac1e2b

Please sign in to comment.