Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-03-26 05:59:28 +0300
committerBeth Griggs <Bethany.Griggs@uk.ibm.com>2019-04-10 17:30:27 +0300
commite40bfd899e18fdc843047ad2defd655aefe5cfbe (patch)
treecfd6a941c2a351d930e44f1a0f46e45d72a94b8a
parent00eef26fb63a34ea029e2b9c8aeb03bea5255497 (diff)
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: https://github.com/nodejs/node/pull/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>
-rw-r--r--lib/internal/assert/assertion_error.js9
-rw-r--r--lib/internal/util/inspect.js2
-rw-r--r--test/parallel/test-util-inspect.js90
3 files changed, 98 insertions, 3 deletions
diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js
index 2d272907f6b..87a69042af6 100644
--- a/lib/internal/assert/assertion_error.js
+++ b/lib/internal/assert/assertion_error.js
@@ -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:',
@@ -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,
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js
index 6cbae6f7397..c1cdb5c8f9e 100644
--- a/lib/internal/util/inspect.js
+++ b/lib/internal/util/inspect.js
@@ -890,6 +890,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,
diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js
index 5f4779095c9..f72deaab575 100644
--- a/test/parallel/test-util-inspect.js
+++ b/test/parallel/test-util-inspect.js
@@ -2213,4 +2213,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);
}