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');

0 commit comments

Comments
 (0)