Skip to content

Commit

Permalink
test: add deprecation code to expectWarning
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danbev committed Mar 26, 2018
1 parent d49661b commit 55a7d6a
Show file tree
Hide file tree
Showing 29 changed files with 101 additions and 59 deletions.
10 changes: 8 additions & 2 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ 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)
### expectWarning(name, expected, code)
* `name` [<string>]
* `expected` [<string>] | [<Array>]
* `code` [<string>]

Tests whether `name` and `expected` are part of a raised warning.
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.

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

### fileExists(pathname)
* pathname [<string>]
Expand Down
39 changes: 29 additions & 10 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,20 +611,32 @@ exports.isAlive = function isAlive(pid) {
}
};

function expectWarning(name, expectedMessages) {
exports.noWarnCode = 'no_expected_warning_code';

function expectWarning(name, expected) {
const map = new Map(expected);
return exports.mustCall((warning) => {
assert.strictEqual(warning.name, name);
assert.ok(expectedMessages.includes(warning.message),
assert.ok(map.has(warning.message),
`unexpected error message: "${warning.message}"`);
const code = map.get(warning.message);
if (code === undefined) {
throw new Error('An error code must be specified or use ' +
'common.noWarnCode if there is no error code. The error ' +
`code for this warning was ${warning.code}`);
}
if (code !== exports.noWarnCode) {
assert.strictEqual(warning.code, code);
}
// Remove a warning message after it is seen so that we guarantee that we
// get each message only once.
expectedMessages.splice(expectedMessages.indexOf(warning.message), 1);
}, expectedMessages.length);
map.delete(expected);
}, map.size);
}

function expectWarningByName(name, expected) {
function expectWarningByName(name, expected, code) {
if (typeof expected === 'string') {
expected = [expected];
expected = [[expected, code]];
}
process.on('warning', expectWarning(name, expected));
}
Expand All @@ -633,8 +645,15 @@ function expectWarningByMap(warningMap) {
const catchWarning = {};
Object.keys(warningMap).forEach((name) => {
let expected = warningMap[name];
if (typeof expected === 'string') {
expected = [expected];
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);
});
Expand All @@ -644,9 +663,9 @@ function expectWarningByMap(warningMap) {
// 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
exports.expectWarning = function(nameOrMap, expected) {
exports.expectWarning = function(nameOrMap, expected, code) {
if (typeof nameOrMap === 'string') {
expectWarningByName(nameOrMap, expected);
expectWarningByName(nameOrMap, expected, code);
} else {
expectWarningByMap(nameOrMap);
}
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-assert-fail-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const assert = require('assert');
common.expectWarning(
'DeprecationWarning',
'assert.fail() with more than one argument is deprecated. ' +
'Please use assert.strictEqual() instead or only pass a message.'
'Please use assert.strictEqual() instead or only pass a message.',
'DEP0094'
);

// Two args only, operator defaults to '!='
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-pending-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
'methods instead.';

common.expectWarning('DeprecationWarning', bufferWarning);
common.expectWarning('DeprecationWarning', bufferWarning, 'DEP0005');

// This is used to make sure that a warning is only emitted once even though
// `new Buffer()` is called twice.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-custom-fds.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const oldSpawnSync = internalCp.spawnSync;
{
const msg = 'child_process: options.customFds option is deprecated. ' +
'Use options.stdio instead.';
common.expectWarning('DeprecationWarning', msg);
common.expectWarning('DeprecationWarning', msg, 'DEP0006');

const customFds = [-1, process.stdout.fd, process.stderr.fd];
internalCp.spawnSync = common.mustCall(function(opts) {
Expand Down
11 changes: 7 additions & 4 deletions test/parallel/test-crypto-authenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,16 @@ const errMessages = {
const ciphers = crypto.getCiphers();

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

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

expectedDeprecationWarnings.push('crypto.DEFAULT_ENCODING is deprecated.');
expectedDeprecationWarnings.push(['crypto.DEFAULT_ENCODING is deprecated.',
'DEP0091']);

common.expectWarning({
Warning: expectedWarnings,
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-crypto-cipher-decipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ testCipher2(Buffer.from('0123456789abcdef'));
const data = Buffer.from('test-crypto-cipher-decipher');

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

const cipher = crypto.createCipher('aes-256-gcm', key);
cipher.setAAD(aadbuf);
Expand Down
10 changes: 6 additions & 4 deletions test/parallel/test-crypto-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const crypto = require('crypto');
const tls = require('tls');

common.expectWarning('DeprecationWarning', [
'crypto.Credentials is deprecated. Use tls.SecureContext instead.',
'crypto.createCredentials is deprecated. Use tls.createSecureContext ' +
'instead.',
'crypto.Decipher.finaltol is deprecated. Use crypto.Decipher.final instead.'
['crypto.Credentials is deprecated. Use tls.SecureContext instead.',
'DEP0011'],
['crypto.createCredentials is deprecated. Use tls.createSecureContext ' +
'instead.', 'DEP0010'],
['crypto.Decipher.finaltol is deprecated. Use crypto.Decipher.final instead.',
'DEP0105']
]);

// Accessing the deprecated function is enough to trigger the warning event.
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-fs-filehandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ let fdnum;

common.expectWarning(
'Warning',
`Closing file descriptor ${fdnum} on garbage collection`
`Closing file descriptor ${fdnum} on garbage collection`,
common.noWarnCode
);

gc(); // eslint-disable-line no-undef
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-truncate-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
' Please use fs.ftruncate with a file descriptor instead.';


common.expectWarning('DeprecationWarning', msg);
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
fs.truncate(fd, 5, common.mustCall(function(err) {
assert.ok(!err);
assert.strictEqual(fs.readFileSync(filename, 'utf8'), 'hello');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ fs.ftruncateSync(fd);
stat = fs.statSync(filename);
assert.strictEqual(stat.size, 0);

// Check truncateSync
common.expectWarning('DeprecationWarning', msg);
// truncateSync
common.expectWarning('DeprecationWarning', msg, 'DEP0081');
fs.truncateSync(fd);

fs.closeSync(fd);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-server-connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const server = new net.Server();
const expectedWarning = 'Server.connections property is deprecated. ' +
'Use Server.getConnections method instead.';

common.expectWarning('DeprecationWarning', expectedWarning);
common.expectWarning('DeprecationWarning', expectedWarning, 'DEP0020');

// test that server.connections property is no longer enumerable now that it
// has been marked as deprecated
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-performance-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ performance.maxEntries = 1;
);
});

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

performance.mark('test');
3 changes: 2 additions & 1 deletion test/parallel/test-process-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const assert = require('assert');

common.expectWarning(
'DeprecationWarning',
'process.assert() is deprecated. Please use the `assert` module instead.'
'process.assert() is deprecated. Please use the `assert` module instead.',
'DEP0100'
);

assert.strictEqual(process.assert(1, 'error'), undefined);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-process-emit-warning-from-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const key = '0123456789';

{
common.expectWarning('Warning',
'Use Cipheriv for counter mode of aes-256-gcm');
'Use Cipheriv for counter mode of aes-256-gcm',
common.noWarnCode);

// Emits regular warning expected by expectWarning()
crypto.createCipher('aes-256-gcm', key);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-process-env-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ common.expectWarning(
'DeprecationWarning',
'Assigning any value other than a string, number, or boolean to a ' +
'process.env property is deprecated. Please make sure to convert the value ' +
'to a string before setting process.env with it.'
'to a string before setting process.env with it.',
'DEP0104'
);

process.env.ABC = undefined;
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-promises-unhandled-proxy-rejections.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';
const common = require('../common');

const expectedDeprecationWarning = 'Unhandled promise rejections are ' +
const expectedDeprecationWarning = ['Unhandled promise rejections are ' +
'deprecated. In the future, promise ' +
'rejections that are not handled will ' +
'terminate the Node.js process with a ' +
'non-zero exit code.';
const expectedPromiseWarning = 'Unhandled promise rejection. ' +
'non-zero exit code.', 'DEP0018'];
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)';
'not handled with .catch(). (rejection id: 1)', common.noWarnCode];

function throwErr() {
throw new Error('Error from proxy');
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-promises-unhandled-symbol-rejections.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';
const common = require('../common');

const expectedValueWarning = 'Symbol()';
const expectedDeprecationWarning = 'Unhandled promise rejections are ' +
const expectedValueWarning = ['Symbol()', common.noWarnCode];
const expectedDeprecationWarning = ['Unhandled promise rejections are ' +
'deprecated. In the future, promise ' +
'rejections that are not handled will ' +
'terminate the Node.js process with a ' +
'non-zero exit code.';
const expectedPromiseWarning = 'Unhandled promise rejection. ' +
'non-zero exit code.', common.noWarnCode];
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)';
'not handled with .catch(). (rejection id: 1)', common.noWarnCode];

common.expectWarning({
DeprecationWarning: expectedDeprecationWarning,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-deprecations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function testParseREPLKeyword() {
const server = repl.start({ prompt: '> ' });
const warn = 'REPLServer.parseREPLKeyword() is deprecated';

common.expectWarning('DeprecationWarning', warn);
common.expectWarning('DeprecationWarning', warn, 'DEP0075');
assert.ok(server.parseREPLKeyword('clear'));
assert.ok(!server.parseREPLKeyword('tacos'));
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-memory-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function testMemory() {
const server = repl.start({ prompt: '> ' });
const warn = 'REPLServer.memory() is deprecated';

common.expectWarning('DeprecationWarning', warn);
common.expectWarning('DeprecationWarning', warn, 'DEP0082');
assert.strictEqual(server.memory(), undefined);
server.close();
}
2 changes: 1 addition & 1 deletion test/parallel/test-repl-turn-off-editor-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function testTurnOffEditorMode() {
const server = repl.start({ prompt: '> ' });
const warn = 'REPLServer.turnOffEditorMode() is deprecated';

common.expectWarning('DeprecationWarning', warn);
common.expectWarning('DeprecationWarning', warn, 'DEP0078');
server.turnOffEditorMode();
server.close();
}
4 changes: 2 additions & 2 deletions test/parallel/test-require-deps-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const deps = [
];

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

for (const m of deprecatedModules) {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-tls-dhe.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ 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');
'DH parameter is less than 2048 bits',
common.noWarnCode);

function loadDHParam(n) {
const params = [`dh${n}.pem`];
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-tls-ecdh-disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const options = {
};

common.expectWarning('DeprecationWarning',
'{ ecdhCurve: false } is deprecated.');
'{ ecdhCurve: false } is deprecated.',
'DEP0083');

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

Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-tls-legacy-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const tls = require('tls');

common.expectWarning(
'DeprecationWarning',
'tls.createSecurePair() is deprecated. Please use tls.TLSSocket instead.'
'tls.createSecurePair() is deprecated. Please use tls.TLSSocket instead.',
'DEP0064'
);

tls.createSecurePair();
3 changes: 2 additions & 1 deletion test/parallel/test-tls-parse-cert-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ common.restoreStderr();
{
common.expectWarning('DeprecationWarning',
'tls.parseCertString() is deprecated. ' +
'Please use querystring.parse() instead.');
'Please use querystring.parse() instead.',
'DEP0076');

const ret = tls.parseCertString('foo=bar');
assert.deepStrictEqual(ret, { __proto__: null, foo: 'bar' });
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-util-inspect-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const util = require('util');
// `common.expectWarning` will expect the warning exactly one time only
common.expectWarning(
'DeprecationWarning',
'Custom inspection function on Objects via .inspect() is deprecated'
'Custom inspection function on Objects via .inspect() is deprecated',
'DEP0079'
);
util.inspect(target); // should emit deprecation warning
util.inspect(target); // should not emit deprecation warning
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false);

common.expectWarning('DeprecationWarning', [
'util.print is deprecated. Use console.log instead.',
'util.puts is deprecated. Use console.log instead.',
'util.debug is deprecated. Use console.error instead.',
'util.error is deprecated. Use console.error instead.'
['util.print is deprecated. Use console.log instead.', common.noWarnCode],
['util.puts is deprecated. Use console.log instead.', common.noWarnCode],
['util.debug is deprecated. Use console.error instead.', common.noWarnCode],
['util.error is deprecated. Use console.error instead.', common.noWarnCode]
]);

util.print('test');
Expand Down
Loading

0 comments on commit 55a7d6a

Please sign in to comment.