Skip to content

Commit 8fb4ea9

Browse files
committed
test: add deprecation code to expectWarning
This commit adds a deprecation code to expectWarning and updates the function to check the passed code against the code property on the warning object. Not all warnings have a deprecation code so for those that don't an explicit code of common.noWarnCode is required. Passing this skips the assertion of the code. PR-URL: #19474 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent ebbf393 commit 8fb4ea9

29 files changed

+101
-59
lines changed

test/common/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,17 @@ Indicates if there is more than 1gb of total memory.
108108
returned function has not been called exactly `exact` number of times when the
109109
test is complete, then the test will fail.
110110

111-
### expectWarning(name, expected)
111+
### expectWarning(name, expected, code)
112112
* `name` [&lt;string>]
113113
* `expected` [&lt;string>] | [&lt;Array>]
114+
* `code` [&lt;string>]
114115

115-
Tests whether `name` and `expected` are part of a raised warning.
116+
Tests whether `name`, `expected`, and `code` are part of a raised warning. If
117+
an expected warning does not have a code then `common.noWarnCode` can be used
118+
to indicate this.
119+
120+
### noWarnCode
121+
See `common.expectWarning()` for usage.
116122

117123
### fileExists(pathname)
118124
* pathname [&lt;string>]

test/common/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,20 +611,32 @@ exports.isAlive = function isAlive(pid) {
611611
}
612612
};
613613

614-
function expectWarning(name, expectedMessages) {
614+
exports.noWarnCode = 'no_expected_warning_code';
615+
616+
function expectWarning(name, expected) {
617+
const map = new Map(expected);
615618
return exports.mustCall((warning) => {
616619
assert.strictEqual(warning.name, name);
617-
assert.ok(expectedMessages.includes(warning.message),
620+
assert.ok(map.has(warning.message),
618621
`unexpected error message: "${warning.message}"`);
622+
const code = map.get(warning.message);
623+
if (code === undefined) {
624+
throw new Error('An error code must be specified or use ' +
625+
'common.noWarnCode if there is no error code. The error ' +
626+
`code for this warning was ${warning.code}`);
627+
}
628+
if (code !== exports.noWarnCode) {
629+
assert.strictEqual(warning.code, code);
630+
}
619631
// Remove a warning message after it is seen so that we guarantee that we
620632
// get each message only once.
621-
expectedMessages.splice(expectedMessages.indexOf(warning.message), 1);
622-
}, expectedMessages.length);
633+
map.delete(expected);
634+
}, map.size);
623635
}
624636

625-
function expectWarningByName(name, expected) {
637+
function expectWarningByName(name, expected, code) {
626638
if (typeof expected === 'string') {
627-
expected = [expected];
639+
expected = [[expected, code]];
628640
}
629641
process.on('warning', expectWarning(name, expected));
630642
}
@@ -633,8 +645,15 @@ function expectWarningByMap(warningMap) {
633645
const catchWarning = {};
634646
Object.keys(warningMap).forEach((name) => {
635647
let expected = warningMap[name];
636-
if (typeof expected === 'string') {
637-
expected = [expected];
648+
if (!Array.isArray(expected)) {
649+
throw new Error('warningMap entries must be arrays consisting of two ' +
650+
'entries: [message, warningCode]');
651+
}
652+
if (!(Array.isArray(expected[0]))) {
653+
if (expected.length === 0) {
654+
return;
655+
}
656+
expected = [[expected[0], expected[1]]];
638657
}
639658
catchWarning[name] = expectWarning(name, expected);
640659
});
@@ -644,9 +663,9 @@ function expectWarningByMap(warningMap) {
644663
// accepts a warning name and description or array of descriptions or a map
645664
// of warning names to description(s)
646665
// ensures a warning is generated for each name/description pair
647-
exports.expectWarning = function(nameOrMap, expected) {
666+
exports.expectWarning = function(nameOrMap, expected, code) {
648667
if (typeof nameOrMap === 'string') {
649-
expectWarningByName(nameOrMap, expected);
668+
expectWarningByName(nameOrMap, expected, code);
650669
} else {
651670
expectWarningByMap(nameOrMap);
652671
}

test/parallel/test-assert-fail-deprecation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const assert = require('assert');
66
common.expectWarning(
77
'DeprecationWarning',
88
'assert.fail() with more than one argument is deprecated. ' +
9-
'Please use assert.strictEqual() instead or only pass a message.'
9+
'Please use assert.strictEqual() instead or only pass a message.',
10+
'DEP0094'
1011
);
1112

1213
// Two args only, operator defaults to '!='

test/parallel/test-buffer-pending-deprecation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
99
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
1010
'methods instead.';
1111

12-
common.expectWarning('DeprecationWarning', bufferWarning);
12+
common.expectWarning('DeprecationWarning', bufferWarning, 'DEP0005');
1313

1414
// This is used to make sure that a warning is only emitted once even though
1515
// `new Buffer()` is called twice.

test/parallel/test-child-process-custom-fds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const oldSpawnSync = internalCp.spawnSync;
99
{
1010
const msg = 'child_process: options.customFds option is deprecated. ' +
1111
'Use options.stdio instead.';
12-
common.expectWarning('DeprecationWarning', msg);
12+
common.expectWarning('DeprecationWarning', msg, 'DEP0006');
1313

1414
const customFds = [-1, process.stdout.fd, process.stderr.fd];
1515
internalCp.spawnSync = common.mustCall(function(opts) {

test/parallel/test-crypto-authenticated.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,16 @@ const errMessages = {
336336
const ciphers = crypto.getCiphers();
337337

338338
const expectedWarnings = common.hasFipsCrypto ?
339-
[] : ['Use Cipheriv for counter mode of aes-192-gcm'];
339+
[] : [['Use Cipheriv for counter mode of aes-192-gcm',
340+
common.noWarnCode]];
340341

341342
const expectedDeprecationWarnings = [0, 1, 2, 6, 9, 10, 11, 17]
342-
.map((i) => `Permitting authentication tag lengths of ${i} bytes is ` +
343-
'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.');
343+
.map((i) => [`Permitting authentication tag lengths of ${i} bytes is ` +
344+
'deprecated. Valid GCM tag lengths are 4, 8, 12, 13, 14, 15, 16.',
345+
'DEP0090']);
344346

345-
expectedDeprecationWarnings.push('crypto.DEFAULT_ENCODING is deprecated.');
347+
expectedDeprecationWarnings.push(['crypto.DEFAULT_ENCODING is deprecated.',
348+
'DEP0091']);
346349

347350
common.expectWarning({
348351
Warning: expectedWarnings,

test/parallel/test-crypto-cipher-decipher.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ testCipher2(Buffer.from('0123456789abcdef'));
236236
const data = Buffer.from('test-crypto-cipher-decipher');
237237

238238
common.expectWarning('Warning',
239-
'Use Cipheriv for counter mode of aes-256-gcm');
239+
'Use Cipheriv for counter mode of aes-256-gcm',
240+
common.noWarnCode);
240241

241242
const cipher = crypto.createCipher('aes-256-gcm', key);
242243
cipher.setAAD(aadbuf);

test/parallel/test-crypto-deprecated.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ const crypto = require('crypto');
88
const tls = require('tls');
99

1010
common.expectWarning('DeprecationWarning', [
11-
'crypto.Credentials is deprecated. Use tls.SecureContext instead.',
12-
'crypto.createCredentials is deprecated. Use tls.createSecureContext ' +
13-
'instead.',
14-
'crypto.Decipher.finaltol is deprecated. Use crypto.Decipher.final instead.'
11+
['crypto.Credentials is deprecated. Use tls.SecureContext instead.',
12+
'DEP0011'],
13+
['crypto.createCredentials is deprecated. Use tls.createSecureContext ' +
14+
'instead.', 'DEP0010'],
15+
['crypto.Decipher.finaltol is deprecated. Use crypto.Decipher.final instead.',
16+
'DEP0105']
1517
]);
1618

1719
// Accessing the deprecated function is enough to trigger the warning event.

test/parallel/test-fs-filehandle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ let fdnum;
2020

2121
common.expectWarning(
2222
'Warning',
23-
`Closing file descriptor ${fdnum} on garbage collection`
23+
`Closing file descriptor ${fdnum} on garbage collection`,
24+
common.noWarnCode
2425
);
2526

2627
gc(); // eslint-disable-line no-undef

test/parallel/test-fs-truncate-fd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
1515
' Please use fs.ftruncate with a file descriptor instead.';
1616

1717

18-
common.expectWarning('DeprecationWarning', msg);
18+
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
1919
fs.truncate(fd, 5, common.mustCall(function(err) {
2020
assert.ok(!err);
2121
assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello');

test/parallel/test-fs-truncate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ fs.ftruncateSync(fd);
6464
stat = fs.statSync(filename);
6565
assert.strictEqual(stat.size, 0);
6666

67-
// Check truncateSync
68-
common.expectWarning('DeprecationWarning', msg);
67+
// truncateSync
68+
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
6969
fs.truncateSync(fd);
7070

7171
fs.closeSync(fd);

test/parallel/test-net-server-connections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const server = new net.Server();
3030
const expectedWarning = 'Server.connections property is deprecated. ' +
3131
'Use Server.getConnections method instead.';
3232

33-
common.expectWarning('DeprecationWarning', expectedWarning);
33+
common.expectWarning('DeprecationWarning', expectedWarning, 'DEP0020');
3434

3535
// test that server.connections property is no longer enumerable now that it
3636
// has been marked as deprecated

test/parallel/test-performance-warning.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ performance.maxEntries = 1;
2020
);
2121
});
2222

23-
common.expectWarning('Warning', [
24-
'Possible perf_hooks memory leak detected. There are 2 entries in the ' +
23+
common.expectWarning('Warning', 'Possible perf_hooks memory leak detected. ' +
24+
'There are 2 entries in the ' +
2525
'Performance Timeline. Use the clear methods to remove entries that are no ' +
2626
'longer needed or set performance.maxEntries equal to a higher value ' +
27-
'(currently the maxEntries is 1).']);
27+
'(currently the maxEntries is 1).', common.noWarnCode);
2828

2929
performance.mark('test');

test/parallel/test-process-assert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const assert = require('assert');
44

55
common.expectWarning(
66
'DeprecationWarning',
7-
'process.assert() is deprecated. Please use the `assert` module instead.'
7+
'process.assert() is deprecated. Please use the `assert` module instead.',
8+
'DEP0100'
89
);
910

1011
assert.strictEqual(process.assert(1, 'error'), undefined);

test/parallel/test-process-emit-warning-from-native.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const key = '0123456789';
1212

1313
{
1414
common.expectWarning('Warning',
15-
'Use Cipheriv for counter mode of aes-256-gcm');
15+
'Use Cipheriv for counter mode of aes-256-gcm',
16+
common.noWarnCode);
1617

1718
// Emits regular warning expected by expectWarning()
1819
crypto.createCipher('aes-256-gcm', key);

test/parallel/test-process-env-deprecation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ common.expectWarning(
88
'DeprecationWarning',
99
'Assigning any value other than a string, number, or boolean to a ' +
1010
'process.env property is deprecated. Please make sure to convert the value ' +
11-
'to a string before setting process.env with it.'
11+
'to a string before setting process.env with it.',
12+
'DEP0104'
1213
);
1314

1415
process.env.ABC = undefined;

test/parallel/test-promises-unhandled-proxy-rejections.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
'use strict';
22
const common = require('../common');
33

4-
const expectedDeprecationWarning = 'Unhandled promise rejections are ' +
4+
const expectedDeprecationWarning = ['Unhandled promise rejections are ' +
55
'deprecated. In the future, promise ' +
66
'rejections that are not handled will ' +
77
'terminate the Node.js process with a ' +
8-
'non-zero exit code.';
9-
const expectedPromiseWarning = 'Unhandled promise rejection. ' +
8+
'non-zero exit code.', 'DEP0018'];
9+
const expectedPromiseWarning = ['Unhandled promise rejection. ' +
1010
'This error originated either by throwing ' +
1111
'inside of an async function without a catch ' +
1212
'block, or by rejecting a promise which was ' +
13-
'not handled with .catch(). (rejection id: 1)';
13+
'not handled with .catch(). (rejection id: 1)', common.noWarnCode];
1414

1515
function throwErr() {
1616
throw new Error('Error from proxy');

test/parallel/test-promises-unhandled-symbol-rejections.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict';
22
const common = require('../common');
33

4-
const expectedValueWarning = 'Symbol()';
5-
const expectedDeprecationWarning = 'Unhandled promise rejections are ' +
4+
const expectedValueWarning = ['Symbol()', common.noWarnCode];
5+
const expectedDeprecationWarning = ['Unhandled promise rejections are ' +
66
'deprecated. In the future, promise ' +
77
'rejections that are not handled will ' +
88
'terminate the Node.js process with a ' +
9-
'non-zero exit code.';
10-
const expectedPromiseWarning = 'Unhandled promise rejection. ' +
9+
'non-zero exit code.', common.noWarnCode];
10+
const expectedPromiseWarning = ['Unhandled promise rejection. ' +
1111
'This error originated either by throwing ' +
1212
'inside of an async function without a catch ' +
1313
'block, or by rejecting a promise which was ' +
14-
'not handled with .catch(). (rejection id: 1)';
14+
'not handled with .catch(). (rejection id: 1)', common.noWarnCode];
1515

1616
common.expectWarning({
1717
DeprecationWarning: expectedDeprecationWarning,

test/parallel/test-repl-deprecations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function testParseREPLKeyword() {
99
const server = repl.start({ prompt: '> ' });
1010
const warn = 'REPLServer.parseREPLKeyword() is deprecated';
1111

12-
common.expectWarning('DeprecationWarning', warn);
12+
common.expectWarning('DeprecationWarning', warn, 'DEP0075');
1313
assert.ok(server.parseREPLKeyword('clear'));
1414
assert.ok(!server.parseREPLKeyword('tacos'));
1515
server.close();

test/parallel/test-repl-memory-deprecation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function testMemory() {
99
const server = repl.start({ prompt: '> ' });
1010
const warn = 'REPLServer.memory() is deprecated';
1111

12-
common.expectWarning('DeprecationWarning', warn);
12+
common.expectWarning('DeprecationWarning', warn, 'DEP0082');
1313
assert.strictEqual(server.memory(), undefined);
1414
server.close();
1515
}

test/parallel/test-repl-turn-off-editor-mode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function testTurnOffEditorMode() {
88
const server = repl.start({ prompt: '> ' });
99
const warn = 'REPLServer.turnOffEditorMode() is deprecated';
1010

11-
common.expectWarning('DeprecationWarning', warn);
11+
common.expectWarning('DeprecationWarning', warn, 'DEP0078');
1212
server.turnOffEditorMode();
1313
server.close();
1414
}

test/parallel/test-require-deps-deprecation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const deps = [
2929
];
3030

3131
common.expectWarning('DeprecationWarning', deprecatedModules.map((m) => {
32-
return `Requiring Node.js-bundled '${m}' module is deprecated. ` +
33-
'Please install the necessary module locally.';
32+
return [`Requiring Node.js-bundled '${m}' module is deprecated. ` +
33+
'Please install the necessary module locally.', 'DEP0084'];
3434
}));
3535

3636
for (const m of deprecatedModules) {

test/parallel/test-tls-dhe.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const ciphers = 'DHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
4141

4242
// Test will emit a warning because the DH parameter size is < 2048 bits
4343
common.expectWarning('SecurityWarning',
44-
'DH parameter is less than 2048 bits');
44+
'DH parameter is less than 2048 bits',
45+
common.noWarnCode);
4546

4647
function loadDHParam(n) {
4748
const params = [`dh${n}.pem`];

test/parallel/test-tls-ecdh-disable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const options = {
4848
};
4949

5050
common.expectWarning('DeprecationWarning',
51-
'{ ecdhCurve: false } is deprecated.');
51+
'{ ecdhCurve: false } is deprecated.',
52+
'DEP0083');
5253

5354
const server = tls.createServer(options, common.mustNotCall());
5455

test/parallel/test-tls-legacy-deprecated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const tls = require('tls');
88

99
common.expectWarning(
1010
'DeprecationWarning',
11-
'tls.createSecurePair() is deprecated. Please use tls.TLSSocket instead.'
11+
'tls.createSecurePair() is deprecated. Please use tls.TLSSocket instead.',
12+
'DEP0064'
1213
);
1314

1415
tls.createSecurePair();

test/parallel/test-tls-parse-cert-string.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ common.restoreStderr();
5959
{
6060
common.expectWarning('DeprecationWarning',
6161
'tls.parseCertString() is deprecated. ' +
62-
'Please use querystring.parse() instead.');
62+
'Please use querystring.parse() instead.',
63+
'DEP0076');
6364

6465
const ret = tls.parseCertString('foo=bar');
6566
assert.deepStrictEqual(ret, { __proto__: null, foo: 'bar' });

test/parallel/test-util-inspect-deprecated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const util = require('util');
1111
// `common.expectWarning` will expect the warning exactly one time only
1212
common.expectWarning(
1313
'DeprecationWarning',
14-
'Custom inspection function on Objects via .inspect() is deprecated'
14+
'Custom inspection function on Objects via .inspect() is deprecated',
15+
'DEP0079'
1516
);
1617
util.inspect(target); // should emit deprecation warning
1718
util.inspect(target); // should not emit deprecation warning

test/parallel/test-util.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ assert.strictEqual(util.isFunction(), false);
142142
assert.strictEqual(util.isFunction('string'), false);
143143

144144
common.expectWarning('DeprecationWarning', [
145-
'util.print is deprecated. Use console.log instead.',
146-
'util.puts is deprecated. Use console.log instead.',
147-
'util.debug is deprecated. Use console.error instead.',
148-
'util.error is deprecated. Use console.error instead.'
145+
['util.print is deprecated. Use console.log instead.', common.noWarnCode],
146+
['util.puts is deprecated. Use console.log instead.', common.noWarnCode],
147+
['util.debug is deprecated. Use console.error instead.', common.noWarnCode],
148+
['util.error is deprecated. Use console.error instead.', common.noWarnCode]
149149
]);
150150

151151
util.print('test');

0 commit comments

Comments
 (0)