Skip to content

Commit 48c9f20

Browse files
aduh95targos
authored andcommitted
test: fix common.mustNotCall error message
When using `util.inspect` as a callback, the array index (second argument) is picked up as the `showHidden` param, and the argument array (third argument) as the `depth` param. This fails with a seemingly unrelated error message when the argument array contains a `Symbol`-related property. PR-URL: #42917 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 85dfc07 commit 48c9f20

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

test/common/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const fs = require('fs');
2929
// Do not require 'os' until needed so that test-os-checked-function can
3030
// monkey patch it. If 'os' is required here, that test will fail.
3131
const path = require('path');
32-
const util = require('util');
32+
const { inspect } = require('util');
3333
const { isMainThread } = require('worker_threads');
3434

3535
const tmpdir = require('./tmpdir');
@@ -97,7 +97,7 @@ if (process.argv.length === 2 &&
9797
(process.features.inspector || !flag.startsWith('--inspect'))) {
9898
console.log(
9999
'NOTE: The test started as a child_process using these flags:',
100-
util.inspect(flags),
100+
inspect(flags),
101101
'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.'
102102
);
103103
const args = [...flags, ...process.execArgv, ...process.argv.slice(1)];
@@ -138,7 +138,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
138138
process.on('exit', () => {
139139
// Iterate through handles to make sure nothing crashes
140140
for (const k in initHandles)
141-
util.inspect(initHandles[k]);
141+
inspect(initHandles[k]);
142142
});
143143

144144
const _queueDestroyAsyncId = async_wrap.queueDestroyAsyncId;
@@ -148,7 +148,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
148148
process._rawDebug();
149149
throw new Error(`same id added to destroy list twice (${id})`);
150150
}
151-
destroyListList[id] = util.inspect(new Error());
151+
destroyListList[id] = inspect(new Error());
152152
_queueDestroyAsyncId(id);
153153
};
154154

@@ -162,7 +162,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
162162
}
163163
initHandles[id] = {
164164
resource,
165-
stack: util.inspect(new Error()).substr(6)
165+
stack: inspect(new Error()).substr(6)
166166
};
167167
},
168168
before() { },
@@ -173,7 +173,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
173173
process._rawDebug();
174174
throw new Error(`destroy called for same id (${id})`);
175175
}
176-
destroydIdsList[id] = util.inspect(new Error());
176+
destroydIdsList[id] = inspect(new Error());
177177
},
178178
}).enable();
179179
}
@@ -397,7 +397,7 @@ function _mustCallInner(fn, criteria = 1, field) {
397397
const context = {
398398
[field]: criteria,
399399
actual: 0,
400-
stack: util.inspect(new Error()),
400+
stack: inspect(new Error()),
401401
name: fn.name || '<anonymous>'
402402
};
403403

@@ -485,7 +485,7 @@ function mustNotCall(msg) {
485485
const callSite = getCallSite(mustNotCall);
486486
return function mustNotCall(...args) {
487487
const argsInfo = args.length > 0 ?
488-
`\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : '';
488+
`\ncalled with arguments: ${args.map((arg) => inspect(arg)).join(', ')}` : '';
489489
assert.fail(
490490
`${msg || 'function should not have been called'} at ${callSite}` +
491491
argsInfo);
@@ -587,7 +587,7 @@ function expectWarning(nameOrMap, expected, code) {
587587
if (!catchWarning[warning.name]) {
588588
throw new TypeError(
589589
`"${warning.name}" was triggered without being expected.\n` +
590-
util.inspect(warning)
590+
inspect(warning)
591591
);
592592
}
593593
catchWarning[warning.name](warning);
@@ -608,7 +608,7 @@ function expectsError(validator, exact) {
608608
if (args.length !== 1) {
609609
// Do not use `assert.strictEqual()` to prevent `inspect` from
610610
// always being called.
611-
assert.fail(`Expected one argument, got ${util.inspect(args)}`);
611+
assert.fail(`Expected one argument, got ${inspect(args)}`);
612612
}
613613
const error = args.pop();
614614
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
@@ -713,9 +713,9 @@ function invalidArgTypeHelper(input) {
713713
if (input.constructor && input.constructor.name) {
714714
return ` Received an instance of ${input.constructor.name}`;
715715
}
716-
return ` Received ${util.inspect(input, { depth: -1 })}`;
716+
return ` Received ${inspect(input, { depth: -1 })}`;
717717
}
718-
let inspected = util.inspect(input, { colors: false });
718+
let inspected = inspect(input, { colors: false });
719719
if (inspected.length > 25)
720720
inspected = `${inspected.slice(0, 25)}...`;
721721
return ` Received type ${typeof input} (${inspected})`;

test/parallel/test-common-must-not-call.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,18 @@ try {
3939
} catch (e) {
4040
validate2(e);
4141
}
42+
43+
assert.throws(
44+
() => new Proxy({ prop: Symbol() }, { get: common.mustNotCall() }).prop,
45+
{ code: 'ERR_ASSERTION' }
46+
);
47+
48+
{
49+
const { inspect } = util;
50+
delete util.inspect;
51+
assert.throws(
52+
() => common.mustNotCall()(null),
53+
{ code: 'ERR_ASSERTION' }
54+
);
55+
util.inspect = inspect;
56+
}

0 commit comments

Comments
 (0)