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

test: refactor common.expectWarning() #25251

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
50 changes: 42 additions & 8 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,51 @@ Indicates if there is more than 1gb of total memory.
returned function has not been called exactly `exact` number of times when the
test is complete, then the test will fail.

### expectWarning(name, expected, code)
### expectWarning(name, expected[, code])
* `name` [<string>]
* `expected` [<string>] | [<Array>]
* `expected` [<string>] | [<Array>] | [<Object>]
* `code` [<string>]

Tests whether `name`, `expected`, and `code` are part of a raised warning. If
an expected warning does not have a code then `common.noWarnCode` can be used
to indicate this.
Tests whether `name`, `expected`, and `code` are part of a raised warning.

The code is required in case the name is set to `'DeprecationWarning'`.

Examples:

```js
const { expectWarning } = require('../common');

expectWarning('Warning', 'Foobar is really bad');

expectWarning('DeprecationWarning', 'Foobar is deprecated', 'DEP0XXX');

expectWarning('DeprecationWarning', [
'Foobar is deprecated', 'DEP0XXX'
]);

expectWarning('DeprecationWarning', [
['Foobar is deprecated', 'DEP0XXX'],
['Baz is also deprecated']
]);

expectWarning('DeprecationWarning', {
DEP0XXX: 'Foobar is deprecated',
DEP0XX2: 'Baz is also deprecated'
});

expectWarning({
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
DeprecationWarning: {
DEP0XXX: 'Foobar is deprecated',
DEP0XX1: 'Baz is also deprecated'
},
Warning: [
['Multiple array entries are fine', 'DEP0XXX'],
['No code is also fine']
],
SingleEntry: ['This will also work', 'DEP0XXX'],
SingleString: 'Single string entries without code will also work'
});
```

### getArrayBufferViews(buf)
* `buf` [<Buffer>]
Expand Down Expand Up @@ -262,9 +299,6 @@ Returns `true` if the exit code `exitCode` and/or signal name `signal` represent
the exit code and/or signal name of a node process that aborted, `false`
otherwise.

### noWarnCode
See `common.expectWarning()` for usage.

### opensslCli
* [<boolean>]

Expand Down
66 changes: 27 additions & 39 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,54 +498,43 @@ function isAlive(pid) {
}
}

function _expectWarning(name, expected) {
const map = new Map(expected);
function _expectWarning(name, expected, code) {
if (typeof expected === 'string') {
expected = [[expected, code]];
} else if (!Array.isArray(expected)) {
expected = Object.entries(expected).map(([a, b]) => [b, a]);
} else if (!(Array.isArray(expected[0]))) {
expected = [[expected[0], expected[1]]];
}
// Deprecation codes are mandatory, everything else is not.
if (name === 'DeprecationWarning') {
expected.forEach(([_, code]) => assert(code, expected));
}
return mustCall((warning) => {
const [ message, code ] = expected.shift();
assert.strictEqual(warning.name, name);
assert.ok(map.has(warning.message),
`unexpected error message: "${warning.message}"`);
const code = map.get(warning.message);
assert.strictEqual(warning.message, message);
assert.strictEqual(warning.code, code);
// Remove a warning message after it is seen so that we guarantee that we
// get each message only once.
map.delete(expected);
}, expected.length);
}

function expectWarningByName(name, expected, code) {
if (typeof expected === 'string') {
expected = [[expected, code]];
}
process.on('warning', _expectWarning(name, expected));
}
let catchWarning;

function expectWarningByMap(warningMap) {
const catchWarning = {};
Object.keys(warningMap).forEach((name) => {
let expected = warningMap[name];
if (!Array.isArray(expected)) {
throw new Error('warningMap entries must be arrays consisting of two ' +
'entries: [message, warningCode]');
}
if (!(Array.isArray(expected[0]))) {
if (expected.length === 0) {
return;
}
expected = [[expected[0], expected[1]]];
}
catchWarning[name] = _expectWarning(name, expected);
});
process.on('warning', (warning) => catchWarning[warning.name](warning));
}

// Accepts a warning name and description or array of descriptions or a map
// of warning names to description(s)
// ensures a warning is generated for each name/description pair
// Accepts a warning name and description or array of descriptions or a map of
// warning names to description(s) ensures a warning is generated for each
// name/description pair.
// The expected messages have to be unique per `expectWarning()` call.
function expectWarning(nameOrMap, expected, code) {
if (catchWarning === undefined) {
catchWarning = {};
process.on('warning', (warning) => catchWarning[warning.name](warning));
}
if (typeof nameOrMap === 'string') {
expectWarningByName(nameOrMap, expected, code);
catchWarning[nameOrMap] = _expectWarning(nameOrMap, expected, code);
} else {
expectWarningByMap(nameOrMap);
Object.keys(nameOrMap).forEach((name) => {
catchWarning[name] = _expectWarning(name, nameOrMap[name]);
});
}
}

Expand Down Expand Up @@ -759,7 +748,6 @@ module.exports = {
mustCallAtLeast,
mustNotCall,
nodeProcessAborted,
noWarnCode: undefined,
PIPE,
platformTimeout,
printSkipMessage,
Expand Down
2 changes: 0 additions & 2 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const {
nodeProcessAborted,
busyLoop,
isAlive,
noWarnCode,
expectWarning,
expectsError,
skipIfInspectorDisabled,
Expand Down Expand Up @@ -84,7 +83,6 @@ export {
nodeProcessAborted,
busyLoop,
isAlive,
noWarnCode,
expectWarning,
expectsError,
skipIfInspectorDisabled,
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-atomics-notify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { expectWarning, noWarnCode } = require('../common');
const { expectWarning } = require('../common');

const assert = require('assert');
const { runInNewContext } = require('vm');
Expand All @@ -14,6 +14,6 @@ assert.strictEqual(runInNewContext('typeof Atomics.notify'), 'function');
expectWarning(
'Atomics',
'Atomics.wake will be removed in a future version, ' +
'use Atomics.notify instead.', noWarnCode);
'use Atomics.notify instead.');

Atomics.wake(new Int32Array(new SharedArrayBuffer(4)), 0, 0);
16 changes: 8 additions & 8 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ if (common.isMainThread) {
common.expectWarning(
'Warning',
[
['Count for \'noLabel\' does not exist', common.noWarnCode],
['No such label \'noLabel\' for console.timeLog()', common.noWarnCode],
['No such label \'noLabel\' for console.timeEnd()', common.noWarnCode],
['Count for \'default\' does not exist', common.noWarnCode],
['No such label \'default\' for console.timeLog()', common.noWarnCode],
['No such label \'default\' for console.timeEnd()', common.noWarnCode],
['Label \'default\' already exists for console.time()', common.noWarnCode],
['Label \'test\' already exists for console.time()', common.noWarnCode]
['Count for \'noLabel\' does not exist'],
['No such label \'noLabel\' for console.timeLog()'],
['No such label \'noLabel\' for console.timeEnd()'],
['Count for \'default\' does not exist'],
['No such label \'default\' for console.timeLog()'],
['No such label \'default\' for console.timeEnd()'],
['Label \'default\' already exists for console.time()'],
['Label \'test\' already exists for console.time()']
]
);

Expand Down
38 changes: 19 additions & 19 deletions test/parallel/test-crypto-authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ const ciphers = crypto.getCiphers();

const expectedWarnings = common.hasFipsCrypto ?
[] : [
['Use Cipheriv for counter mode of aes-192-gcm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-192-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-192-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-128-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-128-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-128-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode],
['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode]
['Use Cipheriv for counter mode of aes-192-gcm'],
['Use Cipheriv for counter mode of aes-192-ccm'],
['Use Cipheriv for counter mode of aes-192-ccm'],
['Use Cipheriv for counter mode of aes-128-ccm'],
['Use Cipheriv for counter mode of aes-128-ccm'],
['Use Cipheriv for counter mode of aes-128-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm'],
['Use Cipheriv for counter mode of aes-256-ccm']
];

const expectedDeprecationWarnings = [
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-cipher-decipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const assert = require('assert');

common.expectWarning({
Warning: [
['Use Cipheriv for counter mode of aes-256-gcm', common.noWarnCode]
['Use Cipheriv for counter mode of aes-256-gcm']
],
DeprecationWarning: [
['crypto.createCipher is deprecated.', 'DEP0106']
Expand Down
14 changes: 6 additions & 8 deletions test/parallel/test-dns-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ cares.getaddrinfo = () => internalBinding('uv').UV_ENOENT;
common.expectsError(() => dnsPromises.lookup(1, {}), err);
}

// This also verifies different expectWarning notations.
common.expectWarning({
// For 'internal/test/binding' module.
'internal/test/binding': [
'These APIs are for internal testing only. Do not use them.'
],
// For dns.promises.
'ExperimentalWarning': [
'The dns.promises API is experimental'
],
'ExperimentalWarning': 'The dns.promises API is experimental',
// For calling `dns.lookup` with falsy `hostname`.
'DeprecationWarning': [
'The provided hostname "false" is not a valid ' +
'hostname, and is supported in the dns module solely for compatibility.',
'DEP0118',
],
'DeprecationWarning': {
DEP0118: 'The provided hostname "false" is not a valid ' +
'hostname, and is supported in the dns module solely for compatibility.'
}
});

common.expectsError(() => {
Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-fs-filehandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ let fdnum;

common.expectWarning({
'internal/test/binding': [
'These APIs are for internal testing only. Do not use them.',
common.noWarnCode
'These APIs are for internal testing only. Do not use them.'
],
'Warning': [
`Closing file descriptor ${fdnum} on garbage collection`,
common.noWarnCode
`Closing file descriptor ${fdnum} on garbage collection`
]
});

Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-https-strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ common.expectWarning(
'Warning',
'Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to \'0\' ' +
'makes TLS connections and HTTPS requests insecure by disabling ' +
'certificate verification.',
common.noWarnCode
'certificate verification.'
);

const assert = require('assert');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-process-emit-warning-from-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const key = '0123456789';
['crypto.createCipher is deprecated.', 'DEP0106']
],
Warning: [
['Use Cipheriv for counter mode of aes-256-gcm', common.noWarnCode]
['Use Cipheriv for counter mode of aes-256-gcm']
]
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-promises-unhandled-proxy-rejections.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const expectedPromiseWarning = ['Unhandled promise rejection. ' +
'This error originated either by throwing ' +
'inside of an async function without a catch ' +
'block, or by rejecting a promise which was ' +
'not handled with .catch(). (rejection id: 1)', common.noWarnCode];
'not handled with .catch(). (rejection id: 1)'];

function throwErr() {
throw new Error('Error from proxy');
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-promises-unhandled-symbol-rejections.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const common = require('../common');

common.disableCrashOnUnhandledRejection();

const expectedValueWarning = ['Symbol()', common.noWarnCode];
const expectedValueWarning = ['Symbol()'];
const expectedDeprecationWarning = ['Unhandled promise rejections are ' +
'deprecated. In the future, promise ' +
'rejections that are not handled will ' +
Expand All @@ -13,13 +13,13 @@ const expectedPromiseWarning = ['Unhandled promise rejection. ' +
'This error originated either by throwing ' +
'inside of an async function without a catch ' +
'block, or by rejecting a promise which was ' +
'not handled with .catch(). (rejection id: 1)', common.noWarnCode];
'not handled with .catch(). (rejection id: 1)'];

common.expectWarning({
DeprecationWarning: expectedDeprecationWarning,
UnhandledPromiseRejectionWarning: [
expectedPromiseWarning,
expectedValueWarning
expectedValueWarning,
expectedPromiseWarning
],
});

Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-tls-dhe.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const ciphers = 'DHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';

// Test will emit a warning because the DH parameter size is < 2048 bits
common.expectWarning('SecurityWarning',
'DH parameter is less than 2048 bits',
common.noWarnCode);
'DH parameter is less than 2048 bits');

function loadDHParam(n) {
const params = [`dh${n}.pem`];
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-trace-events-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ if (isChild) {
common.expectWarning(
'Warning',
'Possible trace_events memory leak detected. There are more than ' +
'10 enabled Tracing objects.',
common.noWarnCode);
'10 enabled Tracing objects.');
for (let n = 0; n < 10; n++) {
const tracing = createTracing({ categories: [ `a${n}` ] });
tracing.enable();
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-warn-sigprof.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ if (common.isWindows)
common.skipIfWorker(); // Worker inspector never has a server running

common.expectWarning('Warning',
'process.on(SIGPROF) is reserved while debugging',
common.noWarnCode);
'process.on(SIGPROF) is reserved while debugging');

process.on('SIGPROF', () => {});
Loading