Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x backport] test_runner: align stdout and std error with and without --test #48684

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,16 @@ object, streaming a series of events representing the execution of the tests.

Emitted when code coverage is enabled and all tests have completed.

### Event: `'test:dequeue'`

* `data` {Object}
* `file` {string|undefined} The path of the test file,
undefined if test is not ran through a file.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.

Emitted when a test is dequeued, right before it is executed.

### Event: `'test:diagnostic'`

* `data` {Object}
Expand All @@ -1404,6 +1414,16 @@ Emitted when code coverage is enabled and all tests have completed.

Emitted when [`context.diagnostic`][] is called.

### Event: `'test:enqueue'`

* `data` {Object}
* `file` {string|undefined} The path of the test file,
undefined if test is not ran through a file.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.

Emitted when a test is enqueued for execution.

### Event: `'test:fail'`

* `data` {Object}
Expand Down Expand Up @@ -1453,7 +1473,9 @@ Emitted when all subtests have completed for a given test.
* `name` {string} The test name.
* `nesting` {number} The nesting level of the test.

Emitted when a test starts.
Emitted when a test starts reporting its own and its subtests status.
This event is guaranteed to be emitted in the same order as the tests are
defined.

### Event: `'test:stderr'`

Expand Down
11 changes: 7 additions & 4 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const kMaxGroupIndentation = 1000;
// Lazy loaded for startup performance.
let cliTable;

let utilColors;
function lazyUtilColors() {
utilColors ??= require('internal/util/colors');
return utilColors;
}

// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('kGroupIndent');
const kGroupIndentationWidth = Symbol('kGroupIndentWidth');
Expand All @@ -96,7 +102,6 @@ const kUseStdout = Symbol('kUseStdout');
const kUseStderr = Symbol('kUseStderr');

const optionsMap = new SafeWeakMap();

function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
// We have to test new.target here to see if this function is called
// with new, because we need to define a custom instanceof to accommodate
Expand Down Expand Up @@ -315,9 +320,7 @@ ObjectDefineProperties(Console.prototype, {
value: function(stream) {
let color = this[kColorMode];
if (color === 'auto') {
color = stream.isTTY && (
typeof stream.getColorDepth === 'function' ?
stream.getColorDepth() > 2 : true);
color = lazyUtilColors().shouldColorize(stream);
}

const options = optionsMap.get(this);
Expand Down
11 changes: 7 additions & 4 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ function lazyInternalUtilInspect() {
return internalUtilInspect;
}

let utilColors;
function lazyUtilColors() {
utilColors ??= require('internal/util/colors');
return utilColors;
}

let buffer;
function lazyBuffer() {
buffer ??= require('buffer').Buffer;
Expand Down Expand Up @@ -795,10 +801,7 @@ const fatalExceptionStackEnhancers = {
colors: defaultColors,
},
} = lazyInternalUtilInspect();
const colors = useColors &&
((internalBinding('util').guessHandleType(2) === 'TTY' &&
require('internal/tty').hasColors()) ||
defaultColors);
const colors = useColors && (lazyUtilColors().shouldColorize(process.stderr) || defaultColors);
try {
return inspect(error, {
colors,
Expand Down
1 change: 1 addition & 0 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ function setup(root) {
topLevel: 0,
suites: 0,
},
shouldColorizeTestFiles: false,
};
root.startTime = hrtime();
return root;
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/test_runner/reporter/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const {
const assert = require('assert');
const Transform = require('internal/streams/transform');
const { inspectWithNoCustomRetry } = require('internal/errors');
const { green, blue, red, white, gray, hasColors } = require('internal/util/colors');
const { green, blue, red, white, gray, shouldColorize } = require('internal/util/colors');
const { kSubtestsFailed } = require('internal/test_runner/test');
const { getCoverageReport } = require('internal/test_runner/utils');

const inspectOptions = { __proto__: null, colors: hasColors, breakLength: Infinity };
const inspectOptions = { __proto__: null, colors: shouldColorize(process.stdout), breakLength: Infinity };

const colors = {
'__proto__': null,
Expand Down Expand Up @@ -119,7 +119,7 @@ class SpecReporter extends Transform {
break;
case 'test:stderr':
case 'test:stdout':
return `${data.message}\n`;
return data.message;
case 'test:diagnostic':
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
case 'test:coverage':
Expand Down
16 changes: 11 additions & 5 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const {
ArrayPrototypePush,
ObjectEntries,
RegExpPrototypeSymbolReplace,
RegExpPrototypeSymbolSplit,
SafeMap,
SafeSet,
StringPrototypeReplaceAll,
StringPrototypeSplit,
StringPrototypeRepeat,
} = primordials;
const { inspectWithNoCustomRetry } = require('internal/errors');
Expand Down Expand Up @@ -46,8 +46,14 @@ async function * tapReporter(source) {
yield `${indent(data.nesting)}# Subtest: ${tapEscape(data.name)}\n`;
break;
case 'test:stderr':
case 'test:stdout':
case 'test:diagnostic':
case 'test:stdout': {
const lines = RegExpPrototypeSymbolSplit(kLineBreakRegExp, data.message);
for (let i = 0; i < lines.length; i++) {
if (lines[i].length === 0) continue;
yield `# ${tapEscape(lines[i])}\n`;
}
break;
} case 'test:diagnostic':
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
break;
case 'test:coverage':
Expand Down Expand Up @@ -124,7 +130,7 @@ function jsToYaml(indent, name, value, seen) {
return `${prefix}${inspectWithNoCustomRetry(value, inspectOptions)}\n`;
}

const lines = StringPrototypeSplit(value, kLineBreakRegExp);
const lines = RegExpPrototypeSymbolSplit(kLineBreakRegExp, value);

if (lines.length === 1) {
return `${prefix}${inspectWithNoCustomRetry(value, inspectOptions)}\n`;
Expand Down Expand Up @@ -224,7 +230,7 @@ function jsToYaml(indent, name, value, seen) {
const frames = [];

ArrayPrototypeForEach(
StringPrototypeSplit(errStack, kLineBreakRegExp),
RegExpPrototypeSymbolSplit(kLineBreakRegExp, errStack),
(frame) => {
const processed = RegExpPrototypeSymbolReplace(
kFrameStartRegExp,
Expand Down
22 changes: 9 additions & 13 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ const {
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeSort,
hardenRegExp,
ObjectAssign,
PromisePrototypeThen,
SafePromiseAll,
SafePromiseAllReturnVoid,
SafePromiseAllSettledReturnVoid,
PromiseResolve,
RegExpPrototypeSymbolSplit,
SafeMap,
SafeSet,
StringPrototypeIndexOf,
Expand Down Expand Up @@ -74,7 +72,6 @@ const {
const kFilterArgs = ['--test', '--experimental-test-coverage', '--watch'];
const kFilterArgValues = ['--test-reporter', '--test-reporter-destination'];
const kDiagnosticsFilterArgs = ['tests', 'suites', 'pass', 'fail', 'cancelled', 'skipped', 'todo', 'duration_ms'];
const kSplitLine = hardenRegExp(/\r?\n/);

const kCanceledTests = new SafeSet()
.add(kCancelledByParent).add(kAborted).add(kTestTimeoutFailure);
Expand Down Expand Up @@ -300,15 +297,11 @@ class FileTest extends Test {
}

if (TypedArrayPrototypeGetLength(nonSerialized) > 0) {
const messages = RegExpPrototypeSymbolSplit(kSplitLine, nonSerialized.toString('utf-8'));
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
this.addToReport({
__proto__: null,
type: 'test:stdout',
data: { __proto__: null, file: this.name, message },
});
}
this.addToReport({
__proto__: null,
type: 'test:stdout',
data: { __proto__: null, file: this.name, message: nonSerialized.toString('utf-8') },
});
}

while (bufferHead?.length >= kSerializedSizeHeader) {
Expand Down Expand Up @@ -351,6 +344,9 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
stdio.push('ipc');
env.WATCH_REPORT_DEPENDENCIES = '1';
}
if (root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
}

const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio });
if (watchMode) {
Expand Down Expand Up @@ -382,7 +378,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
subtest.addToReport({
__proto__: null,
type: 'test:stderr',
data: { __proto__: null, file: path, message: line },
data: { __proto__: null, file: path, message: line + '\n' },
});
});

Expand Down
6 changes: 5 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ class Test extends AsyncResource {
async processPendingSubtests() {
while (this.pendingSubtests.length > 0 && this.hasConcurrency()) {
const deferred = ArrayPrototypeShift(this.pendingSubtests);
await deferred.test.run();
const test = deferred.test;
this.reporter.dequeue(test.nesting, kFilename, test.name);
await test.run();
deferred.resolve();
}
}
Expand Down Expand Up @@ -471,6 +473,7 @@ class Test extends AsyncResource {
// If there is enough available concurrency to run the test now, then do
// it. Otherwise, return a Promise to the caller and mark the test as
// pending for later execution.
this.reporter.enqueue(this.nesting, kFilename, this.name);
if (!this.parent.hasConcurrency()) {
const deferred = createDeferredPromise();

Expand All @@ -479,6 +482,7 @@ class Test extends AsyncResource {
return deferred.promise;
}

this.reporter.dequeue(this.nesting, kFilename, this.name);
return this.run();
}

Expand Down
8 changes: 8 additions & 0 deletions lib/internal/test_runner/tests_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class TestsStream extends Readable {
return { __proto__: null, todo: reason ?? true };
}

enqueue(nesting, file, name) {
this[kEmitMessage]('test:enqueue', { __proto__: null, nesting, file, name });
}

dequeue(nesting, file, name) {
this[kEmitMessage]('test:dequeue', { __proto__: null, nesting, file, name });
}

start(nesting, file, name) {
this[kEmitMessage]('test:start', { __proto__: null, nesting, file, name });
}
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { createWriteStream } = require('fs');
const { pathToFileURL } = require('internal/url');
const { createDeferredPromise } = require('internal/util');
const { getOptionValue } = require('internal/options');
const { green, red, white } = require('internal/util/colors');
const { green, red, white, shouldColorize } = require('internal/util/colors');

const {
codes: {
Expand Down Expand Up @@ -116,9 +116,10 @@ function tryBuiltinReporter(name) {
return require(builtinPath);
}

async function getReportersMap(reporters, destinations) {
async function getReportersMap(reporters, destinations, rootTest) {
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]);
rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination);

// Load the test reporter passed to --test-reporter
let reporter = tryBuiltinReporter(name);
Expand Down Expand Up @@ -155,7 +156,7 @@ async function getReportersMap(reporters, destinations) {

async function setupTestReporters(rootTest) {
const { reporters, destinations } = parseCommandLine();
const reportersMap = await getReportersMap(reporters, destinations);
const reportersMap = await getReportersMap(reporters, destinations, rootTest);
for (let i = 0; i < reportersMap.length; i++) {
const { reporter, destination } = reportersMap[i];
compose(rootTest.reporter, reporter).pipe(destination);
Expand Down
16 changes: 15 additions & 1 deletion lib/internal/util/colors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

let internalTTy;
function lazyInternalTTY() {
internalTTy ??= require('internal/tty');
return internalTTy;
}

module.exports = {
blue: '',
green: '',
Expand All @@ -8,9 +14,17 @@ module.exports = {
gray: '',
clear: '',
hasColors: false,
shouldColorize(stream) {
if (process.env.FORCE_COLOR !== undefined) {
return lazyInternalTTY().getColorDepth() > 2;
}
return stream?.isTTY && (
typeof stream.getColorDepth === 'function' ?
stream.getColorDepth() > 2 : true);
},
refresh() {
if (process.stderr.isTTY) {
const hasColors = process.stderr.hasColors();
const hasColors = module.exports.shouldColorize(process.stderr);
module.exports.blue = hasColors ? '\u001b[34m' : '';
module.exports.green = hasColors ? '\u001b[32m' : '';
module.exports.white = hasColors ? '\u001b[39m' : '';
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/util/debuglog.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ function emitWarningIfNeeded(set) {

const noop = () => {};

let utilColors;
function lazyUtilColors() {
utilColors ??= require('internal/util/colors');
return utilColors;
}

function debuglogImpl(enabled, set) {
if (debugImpls[set] === undefined) {
if (enabled) {
const pid = process.pid;
emitWarningIfNeeded(set);
debugImpls[set] = function debug(...args) {
const colors = process.stderr.hasColors && process.stderr.hasColors();
const colors = lazyUtilColors().shouldColorize(process.stderr);
const msg = formatWithOptions({ colors }, ...args);
const coloredPID = inspect(pid, { colors });
process.stderr.write(format('%s %s: %s\n', set, coloredPID, msg));
Expand Down
7 changes: 2 additions & 5 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const {
commonPrefix,
} = require('internal/readline/utils');
const { Console } = require('console');
const { shouldColorize } = require('internal/util/colors');
const CJSModule = require('internal/modules/cjs/loader').Module;
let _builtinLibs = ArrayPrototypeFilter(
CJSModule.builtinModules,
Expand Down Expand Up @@ -272,11 +273,7 @@ function REPLServer(prompt,

if (options.terminal && options.useColors === undefined) {
// If possible, check if stdout supports colors or not.
if (options.output.hasColors) {
options.useColors = options.output.hasColors();
} else if (process.env.NODE_DISABLE_COLORS === undefined) {
options.useColors = true;
}
options.useColors = shouldColorize(options.output) || process.env.NODE_DISABLE_COLORS === undefined;
}

// TODO(devsnek): Add a test case for custom eval functions.
Expand Down
Loading