Skip to content

Commit 12737b3

Browse files
BridgeARBethGriggs
authored andcommitted
util: inspect() should not exceed breakLength
Using `util.inspect()` with the `compact` option set to a number could result in output that exceeded the `breakLength` option. This change makes sure that limit is taken into account. PR-URL: #26914 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent 2d780f8 commit 12737b3

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

lib/internal/assert/assertion_error.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const kReadableOperator = {
1818
equal: 'Expected values to be loosely equal:',
1919
notDeepStrictEqual: 'Expected "actual" not to be strictly deep-equal to:',
2020
notStrictEqual: 'Expected "actual" to be strictly unequal to:',
21-
// eslint-disable-next-line max-len
22-
notStrictEqualObject: 'Expected "actual" not to be reference-equal to "expected":',
21+
notStrictEqualObject:
22+
'Expected "actual" not to be reference-equal to "expected":',
2323
notDeepEqual: 'Expected "actual" not to be loosely deep-equal to:',
2424
notEqual: 'Expected "actual" to be loosely unequal to:',
2525
notIdentical: 'Values identical but not reference-equal:',
@@ -52,7 +52,10 @@ function inspectValue(val) {
5252
// Assert compares only enumerable properties (with a few exceptions).
5353
showHidden: false,
5454
// Having a long line as error is better than wrapping the line for
55-
// comparison.
55+
// comparison for now.
56+
// TODO(BridgeAR): `breakLength` should be limited as soon as soon as we
57+
// have meta information about the inspected properties (i.e., know where
58+
// in what line the property starts and ends).
5659
breakLength: Infinity,
5760
// Assert does not detect proxies currently.
5861
showProxy: false,

lib/internal/util/inspect.js

+2
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ function groupArrayElements(ctx, output) {
890890
approxCharHeights * (actualMax - bias) * output.length
891891
) / (actualMax - bias)
892892
),
893+
// Do not exceed the breakLength.
894+
Math.floor((ctx.breakLength - ctx.indentationLvl) / actualMax),
893895
// Limit array grouping for small `compact` modes as the user requested
894896
// minimal grouping.
895897
ctx.compact * 3,

test/parallel/test-util-inspect.js

+90
Original file line numberDiff line numberDiff line change
@@ -2213,4 +2213,94 @@ assert.strictEqual(
22132213
'\u001b[33m3\u001b[39m, \u001b[33m4\u001b[39m ]';
22142214

22152215
assert.strictEqual(out, expected);
2216+
2217+
obj = [
2218+
'Object', 'Function', 'Array',
2219+
'Number', 'parseFloat', 'parseInt',
2220+
'Infinity', 'NaN', 'undefined',
2221+
'Boolean', 'String', 'Symbol',
2222+
'Date', 'Promise', 'RegExp',
2223+
'Error', 'EvalError', 'RangeError',
2224+
'ReferenceError', 'SyntaxError', 'TypeError',
2225+
'URIError', 'JSON', 'Math',
2226+
'console', 'Intl', 'ArrayBuffer',
2227+
'Uint8Array', 'Int8Array', 'Uint16Array',
2228+
'Int16Array', 'Uint32Array', 'Int32Array',
2229+
'Float32Array', 'Float64Array', 'Uint8ClampedArray',
2230+
'BigUint64Array', 'BigInt64Array', 'DataView',
2231+
'Map', 'BigInt', 'Set',
2232+
'WeakMap', 'WeakSet', 'Proxy',
2233+
'Reflect', 'decodeURI', 'decodeURIComponent',
2234+
'encodeURI', 'encodeURIComponent', 'escape',
2235+
'unescape', 'eval', 'isFinite',
2236+
'isNaN', 'SharedArrayBuffer', 'Atomics',
2237+
'globalThis', 'WebAssembly', 'global',
2238+
'process', 'GLOBAL', 'root',
2239+
'Buffer', 'URL', 'URLSearchParams',
2240+
'TextEncoder', 'TextDecoder', 'clearInterval',
2241+
'clearTimeout', 'setInterval', 'setTimeout',
2242+
'queueMicrotask', 'clearImmediate', 'setImmediate',
2243+
'module', 'require', 'assert',
2244+
'async_hooks', 'buffer', 'child_process',
2245+
'cluster', 'crypto', 'dgram',
2246+
'dns', 'domain', 'events',
2247+
'fs', 'http', 'http2',
2248+
'https', 'inspector', 'net',
2249+
'os', 'path', 'perf_hooks',
2250+
'punycode', 'querystring', 'readline',
2251+
'repl', 'stream', 'string_decoder',
2252+
'tls', 'trace_events', 'tty',
2253+
'url', 'v8', 'vm',
2254+
'worker_threads', 'zlib', '_',
2255+
'_error', 'util'
2256+
];
2257+
2258+
out = util.inspect(
2259+
obj,
2260+
{ compact: 3, breakLength: 80, maxArrayLength: 250 }
2261+
);
2262+
expected = [
2263+
'[',
2264+
" 'Object', 'Function', 'Array',",
2265+
" 'Number', 'parseFloat', 'parseInt',",
2266+
" 'Infinity', 'NaN', 'undefined',",
2267+
" 'Boolean', 'String', 'Symbol',",
2268+
" 'Date', 'Promise', 'RegExp',",
2269+
" 'Error', 'EvalError', 'RangeError',",
2270+
" 'ReferenceError', 'SyntaxError', 'TypeError',",
2271+
" 'URIError', 'JSON', 'Math',",
2272+
" 'console', 'Intl', 'ArrayBuffer',",
2273+
" 'Uint8Array', 'Int8Array', 'Uint16Array',",
2274+
" 'Int16Array', 'Uint32Array', 'Int32Array',",
2275+
" 'Float32Array', 'Float64Array', 'Uint8ClampedArray',",
2276+
" 'BigUint64Array', 'BigInt64Array', 'DataView',",
2277+
" 'Map', 'BigInt', 'Set',",
2278+
" 'WeakMap', 'WeakSet', 'Proxy',",
2279+
" 'Reflect', 'decodeURI', 'decodeURIComponent',",
2280+
" 'encodeURI', 'encodeURIComponent', 'escape',",
2281+
" 'unescape', 'eval', 'isFinite',",
2282+
" 'isNaN', 'SharedArrayBuffer', 'Atomics',",
2283+
" 'globalThis', 'WebAssembly', 'global',",
2284+
" 'process', 'GLOBAL', 'root',",
2285+
" 'Buffer', 'URL', 'URLSearchParams',",
2286+
" 'TextEncoder', 'TextDecoder', 'clearInterval',",
2287+
" 'clearTimeout', 'setInterval', 'setTimeout',",
2288+
" 'queueMicrotask', 'clearImmediate', 'setImmediate',",
2289+
" 'module', 'require', 'assert',",
2290+
" 'async_hooks', 'buffer', 'child_process',",
2291+
" 'cluster', 'crypto', 'dgram',",
2292+
" 'dns', 'domain', 'events',",
2293+
" 'fs', 'http', 'http2',",
2294+
" 'https', 'inspector', 'net',",
2295+
" 'os', 'path', 'perf_hooks',",
2296+
" 'punycode', 'querystring', 'readline',",
2297+
" 'repl', 'stream', 'string_decoder',",
2298+
" 'tls', 'trace_events', 'tty',",
2299+
" 'url', 'v8', 'vm',",
2300+
" 'worker_threads', 'zlib', '_',",
2301+
" '_error', 'util'",
2302+
']'
2303+
].join('\n');
2304+
2305+
assert.strictEqual(out, expected);
22162306
}

0 commit comments

Comments
 (0)