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

util: inspect() should not exceed breakLength #26914

Closed
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
9 changes: 6 additions & 3 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const kReadableOperator = {
equal: 'Expected values to be loosely equal:',
notDeepStrictEqual: 'Expected "actual" not to be strictly deep-equal to:',
notStrictEqual: 'Expected "actual" to be strictly unequal to:',
// eslint-disable-next-line max-len
notStrictEqualObject: 'Expected "actual" not to be reference-equal to "expected":',
notStrictEqualObject:
'Expected "actual" not to be reference-equal to "expected":',
notDeepEqual: 'Expected "actual" not to be loosely deep-equal to:',
notEqual: 'Expected "actual" to be loosely unequal to:',
notIdentical: 'Values identical but not reference-equal:',
Expand Down Expand Up @@ -52,7 +52,10 @@ function inspectValue(val) {
// Assert compares only enumerable properties (with a few exceptions).
showHidden: false,
// Having a long line as error is better than wrapping the line for
// comparison.
// comparison for now.
// TODO(BridgeAR): `breakLength` should be limited as soon as soon as we
// have meta information about the inspected properties (i.e., know where
// in what line the property starts and ends).
breakLength: Infinity,
// Assert does not detect proxies currently.
showProxy: false,
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ function groupArrayElements(ctx, output) {
approxCharHeights * (actualMax - bias) * output.length
) / (actualMax - bias)
),
// Do not exceed the breakLength.
Math.floor((ctx.breakLength - ctx.indentationLvl) / actualMax),
// Limit array grouping for small `compact` modes as the user requested
// minimal grouping.
ctx.compact * 3,
Expand Down
90 changes: 90 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2166,4 +2166,94 @@ assert.strictEqual(
'\u001b[33m3\u001b[39m, \u001b[33m4\u001b[39m ]';

assert.strictEqual(out, expected);

obj = [
'Object', 'Function', 'Array',
'Number', 'parseFloat', 'parseInt',
'Infinity', 'NaN', 'undefined',
'Boolean', 'String', 'Symbol',
'Date', 'Promise', 'RegExp',
'Error', 'EvalError', 'RangeError',
'ReferenceError', 'SyntaxError', 'TypeError',
'URIError', 'JSON', 'Math',
'console', 'Intl', 'ArrayBuffer',
'Uint8Array', 'Int8Array', 'Uint16Array',
'Int16Array', 'Uint32Array', 'Int32Array',
'Float32Array', 'Float64Array', 'Uint8ClampedArray',
'BigUint64Array', 'BigInt64Array', 'DataView',
'Map', 'BigInt', 'Set',
'WeakMap', 'WeakSet', 'Proxy',
'Reflect', 'decodeURI', 'decodeURIComponent',
'encodeURI', 'encodeURIComponent', 'escape',
'unescape', 'eval', 'isFinite',
'isNaN', 'SharedArrayBuffer', 'Atomics',
'globalThis', 'WebAssembly', 'global',
'process', 'GLOBAL', 'root',
'Buffer', 'URL', 'URLSearchParams',
'TextEncoder', 'TextDecoder', 'clearInterval',
'clearTimeout', 'setInterval', 'setTimeout',
'queueMicrotask', 'clearImmediate', 'setImmediate',
'module', 'require', 'assert',
'async_hooks', 'buffer', 'child_process',
'cluster', 'crypto', 'dgram',
'dns', 'domain', 'events',
'fs', 'http', 'http2',
'https', 'inspector', 'net',
'os', 'path', 'perf_hooks',
'punycode', 'querystring', 'readline',
'repl', 'stream', 'string_decoder',
'tls', 'trace_events', 'tty',
'url', 'v8', 'vm',
'worker_threads', 'zlib', '_',
'_error', 'util'
];

out = util.inspect(
obj,
{ compact: 3, breakLength: 80, maxArrayLength: 250 }
);
expected = [
'[',
" 'Object', 'Function', 'Array',",
" 'Number', 'parseFloat', 'parseInt',",
" 'Infinity', 'NaN', 'undefined',",
" 'Boolean', 'String', 'Symbol',",
" 'Date', 'Promise', 'RegExp',",
" 'Error', 'EvalError', 'RangeError',",
" 'ReferenceError', 'SyntaxError', 'TypeError',",
" 'URIError', 'JSON', 'Math',",
" 'console', 'Intl', 'ArrayBuffer',",
" 'Uint8Array', 'Int8Array', 'Uint16Array',",
" 'Int16Array', 'Uint32Array', 'Int32Array',",
" 'Float32Array', 'Float64Array', 'Uint8ClampedArray',",
" 'BigUint64Array', 'BigInt64Array', 'DataView',",
" 'Map', 'BigInt', 'Set',",
" 'WeakMap', 'WeakSet', 'Proxy',",
" 'Reflect', 'decodeURI', 'decodeURIComponent',",
" 'encodeURI', 'encodeURIComponent', 'escape',",
" 'unescape', 'eval', 'isFinite',",
" 'isNaN', 'SharedArrayBuffer', 'Atomics',",
" 'globalThis', 'WebAssembly', 'global',",
" 'process', 'GLOBAL', 'root',",
" 'Buffer', 'URL', 'URLSearchParams',",
" 'TextEncoder', 'TextDecoder', 'clearInterval',",
" 'clearTimeout', 'setInterval', 'setTimeout',",
" 'queueMicrotask', 'clearImmediate', 'setImmediate',",
" 'module', 'require', 'assert',",
" 'async_hooks', 'buffer', 'child_process',",
" 'cluster', 'crypto', 'dgram',",
" 'dns', 'domain', 'events',",
" 'fs', 'http', 'http2',",
" 'https', 'inspector', 'net',",
" 'os', 'path', 'perf_hooks',",
" 'punycode', 'querystring', 'readline',",
" 'repl', 'stream', 'string_decoder',",
" 'tls', 'trace_events', 'tty',",
" 'url', 'v8', 'vm',",
" 'worker_threads', 'zlib', '_',",
" '_error', 'util'",
']'
].join('\n');

assert.strictEqual(out, expected);
}