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

More passing console tests #15676

Merged
merged 12 commits into from
Dec 12, 2024
4 changes: 3 additions & 1 deletion src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4199,7 +4199,9 @@ static void populateStackFramePosition(const JSC::StackFrame* stackFrame, BunStr
// It is key to not clone this data because source code strings are large.
// Usage of toStringView (non-owning) is safe as we ref the provider.
provider->ref();
ASSERT(*referenced_source_provider == nullptr);
if (*referenced_source_provider != nullptr) {
(*referenced_source_provider)->deref();
}
*referenced_source_provider = provider;
source_lines[0] = Bun::toStringView(sourceString.substring(lineStart, lineEnd - lineStart));
source_line_numbers[0] = location.line();
Expand Down
43 changes: 29 additions & 14 deletions src/js/builtins/ConsoleObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
const { inspect, formatWithOptions, stripVTControlCharacters } = require("node:util");
const { isBuffer } = require("node:buffer");

const { validateObject, validateInteger, validateArray } = require("internal/validators");
const kMaxGroupIndentation = 1000;

const StringPrototypeIncludes = String.prototype.includes;
const RegExpPrototypeSymbolReplace = RegExp.prototype[Symbol.replace];
const ArrayPrototypeUnshift = Array.prototype.unshift;
Expand Down Expand Up @@ -289,28 +292,35 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
} = options;

if (!stdout || typeof stdout.write !== "function") {
// throw new ERR_CONSOLE_WRITABLE_STREAM("stdout");
throw new TypeError("stdout is not a writable stream");
const err = new TypeError("stdout is not a writable stream");
err.code = "ERR_CONSOLE_WRITABLE_STREAM";
pfgithub marked this conversation as resolved.
Show resolved Hide resolved
throw err;
}
if (!stderr || typeof stderr.write !== "function") {
// throw new ERR_CONSOLE_WRITABLE_STREAM("stderr");
throw new TypeError("stderr is not a writable stream");
const err = new TypeError("stderr is not a writable stream");
err.code = "ERR_CONSOLE_WRITABLE_STREAM";
throw err;
}

if (typeof colorMode !== "boolean" && colorMode !== "auto") {
// throw new ERR_INVALID_ARG_VALUE("colorMode", colorMode);
throw new TypeError("colorMode must be a boolean or 'auto'");
throw $ERR_INVALID_ARG_VALUE(
"The argument 'colorMode' must be one of: 'auto', true, false. Received " + inspect(colorMode),
);
}

if (groupIndentation !== undefined) {
// validateInteger(groupIndentation, "groupIndentation", 0, kMaxGroupIndentation);
validateInteger(groupIndentation, "groupIndentation", 0, kMaxGroupIndentation);
}

if (inspectOptions !== undefined) {
// validateObject(inspectOptions, "options.inspectOptions");
validateObject(inspectOptions, "options.inspectOptions");

if (inspectOptions.colors !== undefined && options.colorMode !== undefined) {
// throw new ERR_INCOMPATIBLE_OPTION_PAIR("options.inspectOptions.color", "colorMode");
const err = new TypeError(
pfgithub marked this conversation as resolved.
Show resolved Hide resolved
'Option "options.inspectOptions.color" cannot be used in combination with option "colorMode"',
);
err.code = "ERR_INCOMPATIBLE_OPTION_PAIR";
throw err;
}
optionsMap.set(this, inspectOptions);
}
Expand Down Expand Up @@ -455,12 +465,17 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
// Add and later remove a noop error handler to catch synchronous
// errors.
if (stream.listenerCount("error") === 0) stream.once("error", noop);

stream.write(string, errorHandler);
} catch (e) {
// Console is a debugging utility, so it swallowing errors is not
// desirable even in edge cases such as low stack space.
// if (isStackOverflowError(e)) throw e;
if (
e != null &&
typeof e === "object" &&
e.name === "RangeError" &&
e.message === "Maximum call stack size exceeded."
)
throw e;
// Sorry, there's no proper way to pass along the error here.
} finally {
stream.removeListener("error", noop);
Expand All @@ -472,7 +487,7 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
value: function (stream) {
let color = this[kColorMode];
if (color === "auto") {
if (process.env.FORCE_COLOR !== undefined) {
if (process.env[["FORCE_", "COLOR"].join("")] !== undefined) {
color = Bun.enableANSIColors;
} else {
color = stream.isTTY && (typeof stream.getColorDepth === "function" ? stream.getColorDepth() > 2 : true);
Expand Down Expand Up @@ -595,7 +610,7 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
clear() {
// It only makes sense to clear if _stdout is a TTY.
// Otherwise, do nothing.
if (this._stdout.isTTY && process.env.TERM !== "dumb") {
if (this._stdout.isTTY && process.env[["TE", "RM"].join("")] !== "dumb") {
this._stdout.write("\x1B[2J\x1B[3J\x1B[H");
}
},
Expand Down Expand Up @@ -643,7 +658,7 @@ export function createConsoleConstructor(console: typeof globalThis.console) {
// https://console.spec.whatwg.org/#table
table(tabularData, properties) {
if (properties !== undefined) {
// validateArray(properties, "properties");
validateArray(properties, "properties");
}

if (tabularData === null || typeof tabularData !== "object") return this.log(tabularData);
Expand Down
241 changes: 241 additions & 0 deletions test/js/node/test/parallel/test-console-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
'use strict';
require('../common');
const {
hijackStdout,
hijackStderr,
restoreStdout,
restoreStderr
} = require('../common/hijackstdio');

const assert = require('assert');
const Console = require('console').Console;

let c, stdout, stderr;

function setup(groupIndentation) {
stdout = '';
hijackStdout(function(data) {
stdout += data;
});

stderr = '';
hijackStderr(function(data) {
stderr += data;
});

c = new Console({ stdout: process.stdout,
stderr: process.stderr,
colorMode: false,
groupIndentation: groupIndentation });
}

function teardown() {
restoreStdout();
restoreStderr();
}

// Basic group() functionality
{
setup();
const expectedOut = 'This is the outer level\n' +
' Level 2\n' +
' Level 3\n' +
' Back to level 2\n' +
'Back to the outer level\n' +
'Still at the outer level\n';


const expectedErr = ' More of level 3\n';

c.log('This is the outer level');
c.group();
c.log('Level 2');
c.group();
c.log('Level 3');
c.warn('More of level 3');
c.groupEnd();
c.log('Back to level 2');
c.groupEnd();
c.log('Back to the outer level');
c.groupEnd();
c.log('Still at the outer level');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Group indentation is tracked per Console instance.
{
setup();
const expectedOut = 'No indentation\n' +
'None here either\n' +
' Now the first console is indenting\n' +
'But the second one does not\n';
const expectedErr = '';

const c2 = new Console(process.stdout, process.stderr);
c.log('No indentation');
c2.log('None here either');
c.group();
c.log('Now the first console is indenting');
c2.log('But the second one does not');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Make sure labels work.
{
setup();
const expectedOut = 'This is a label\n' +
' And this is the data for that label\n';
const expectedErr = '';

c.group('This is a label');
c.log('And this is the data for that label');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Check that console.groupCollapsed() is an alias of console.group()
{
setup();
const expectedOut = 'Label\n' +
' Level 2\n' +
' Level 3\n';
const expectedErr = '';

c.groupCollapsed('Label');
c.log('Level 2');
c.groupCollapsed();
c.log('Level 3');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Check that multiline strings and object output are indented properly.
{
setup();
const expectedOut = 'not indented\n' +
' indented\n' +
' also indented\n' +
' {\n' +
" also: 'a',\n" +
" multiline: 'object',\n" +
" should: 'be',\n" +
" indented: 'properly',\n" +
" kthx: 'bai'\n" +
' }\n';
const expectedErr = '';

c.log('not indented');
c.group();
c.log('indented\nalso indented');
c.log({ also: 'a',
multiline: 'object',
should: 'be',
indented: 'properly',
kthx: 'bai' });

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Check that the kGroupIndent symbol property is not enumerable
{
const keys = Reflect.ownKeys(console)
.filter((val) => Object.prototype.propertyIsEnumerable.call(console, val))
.map((val) => val.toString());
assert(!keys.includes('Symbol(groupIndent)'),
'groupIndent should not be enumerable');
}

// Check custom groupIndentation.
{
setup(3);
const expectedOut = 'Set the groupIndentation parameter to 3\n' +
'This is the outer level\n' +
' Level 2\n' +
' Level 3\n' +
' Back to level 2\n' +
'Back to the outer level\n' +
'Still at the outer level\n';


const expectedErr = ' More of level 3\n';

c.log('Set the groupIndentation parameter to 3');
c.log('This is the outer level');
c.group();
c.log('Level 2');
c.group();
c.log('Level 3');
c.warn('More of level 3');
c.groupEnd();
c.log('Back to level 2');
c.groupEnd();
c.log('Back to the outer level');
c.groupEnd();
c.log('Still at the outer level');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Check the correctness of the groupIndentation parameter.
{
// TypeError
[null, 'str', [], false, true, {}].forEach((e) => {
assert.throws(
() => {
new Console({ stdout: process.stdout,
stderr: process.stderr,
groupIndentation: e });
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
}
);
});

// RangeError for integer
[NaN, 1.01].forEach((e) => {
assert.throws(
() => {
new Console({ stdout: process.stdout,
stderr: process.stderr,
groupIndentation: e });
},
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /an integer/,
}
);
});

// RangeError
[-1, 1001].forEach((e) => {
assert.throws(
() => {
new Console({ stdout: process.stdout,
stderr: process.stderr,
groupIndentation: e });
},
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: />= 0 and <= 1000/,
}
);
});
}
Loading
Loading