GeezerVison is showing github.com — test: refactor `common.expectWarning()` · nodejs/node@201a8d9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/common/README.md +43-9Lines changed: 43 additions & 9 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -109,14 +109,51 @@ Indicates if there is more than 1gb of total memory. | 109 | 109 | returned function has not been called exactly `exact` number of times when the | 110 | 110 | test is complete, then the test will fail. | 111 | 111 |
| 112 | | -### expectWarning(name, expected, code) | 113 | | -* `name` [<string>] | 114 | | -* `expected` [<string>] | [<Array>] | | 112 | +### expectWarning(name[, expected[, code]]) | | 113 | +* `name` [<string>] | [<Object>] | | 114 | +* `expected` [<string>] | [<Array>] | [<Object>] | 115 | 115 | * `code` [<string>] | 116 | 116 |
| 117 | | -Tests whether `name`, `expected`, and `code` are part of a raised warning. If | 118 | | -an expected warning does not have a code then `common.noWarnCode` can be used | 119 | | -to indicate this. | | 117 | +Tests whether `name`, `expected`, and `code` are part of a raised warning. | | 118 | + | | 119 | +The code is required in case the name is set to `'DeprecationWarning'`. | | 120 | + | | 121 | +Examples: | | 122 | + | | 123 | +```js | | 124 | +const { expectWarning } = require('../common'); | | 125 | + | | 126 | +expectWarning('Warning', 'Foobar is really bad'); | | 127 | + | | 128 | +expectWarning('DeprecationWarning', 'Foobar is deprecated', 'DEP0XXX'); | | 129 | + | | 130 | +expectWarning('DeprecationWarning', [ | | 131 | + 'Foobar is deprecated', 'DEP0XXX' | | 132 | +]); | | 133 | + | | 134 | +expectWarning('DeprecationWarning', [ | | 135 | + ['Foobar is deprecated', 'DEP0XXX'], | | 136 | + ['Baz is also deprecated', 'DEP0XX2'] | | 137 | +]); | | 138 | + | | 139 | +expectWarning('DeprecationWarning', { | | 140 | + DEP0XXX: 'Foobar is deprecated', | | 141 | + DEP0XX2: 'Baz is also deprecated' | | 142 | +}); | | 143 | + | | 144 | +expectWarning({ | | 145 | + DeprecationWarning: { | | 146 | + DEP0XXX: 'Foobar is deprecated', | | 147 | + DEP0XX1: 'Baz is also deprecated' | | 148 | + }, | | 149 | + Warning: [ | | 150 | + ['Multiple array entries are fine', 'SpecialWarningCode'], | | 151 | + ['No code is also fine'] | | 152 | + ], | | 153 | + SingleEntry: ['This will also work', 'WarningCode'], | | 154 | + SingleString: 'Single string entries without code will also work' | | 155 | +}); | | 156 | +``` | 120 | 157 |
| 121 | 158 | ### getArrayBufferViews(buf) | 122 | 159 | * `buf` [<Buffer>] | @@ -262,9 +299,6 @@ Returns `true` if the exit code `exitCode` and/or signal name `signal` represent | 262 | 299 | the exit code and/or signal name of a node process that aborted, `false` | 263 | 300 | otherwise. | 264 | 301 |
| 265 | | -### noWarnCode | 266 | | -See `common.expectWarning()` for usage. | 267 | | - | 268 | 302 | ### opensslCli | 269 | 303 | * [<boolean>] | 270 | 304 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.js +27-39Lines changed: 27 additions & 39 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -508,54 +508,43 @@ function isAlive(pid) { | 508 | 508 | } | 509 | 509 | } | 510 | 510 |
| 511 | | -function _expectWarning(name, expected) { | 512 | | - const map = new Map(expected); | | 511 | +function _expectWarning(name, expected, code) { | | 512 | + if (typeof expected === 'string') { | | 513 | + expected = [[expected, code]]; | | 514 | + } else if (!Array.isArray(expected)) { | | 515 | + expected = Object.entries(expected).map(([a, b]) => [b, a]); | | 516 | + } else if (!(Array.isArray(expected[0]))) { | | 517 | + expected = [[expected[0], expected[1]]]; | | 518 | + } | | 519 | + // Deprecation codes are mandatory, everything else is not. | | 520 | + if (name === 'DeprecationWarning') { | | 521 | + expected.forEach(([_, code]) => assert(code, expected)); | | 522 | + } | 513 | 523 | return mustCall((warning) => { | | 524 | + const [ message, code ] = expected.shift(); | 514 | 525 | assert.strictEqual(warning.name, name); | 515 | | - assert.ok(map.has(warning.message), | 516 | | - `unexpected error message: "${warning.message}"`); | 517 | | - const code = map.get(warning.message); | | 526 | + assert.strictEqual(warning.message, message); | 518 | 527 | assert.strictEqual(warning.code, code); | 519 | | - // Remove a warning message after it is seen so that we guarantee that we | 520 | | - // get each message only once. | 521 | | - map.delete(expected); | 522 | 528 | }, expected.length); | 523 | 529 | } | 524 | 530 |
| 525 | | -function expectWarningByName(name, expected, code) { | 526 | | - if (typeof expected === 'string') { | 527 | | - expected = [[expected, code]]; | 528 | | - } | 529 | | - process.on('warning', _expectWarning(name, expected)); | 530 | | -} | | 531 | +let catchWarning; | 531 | 532 |
| 532 | | -function expectWarningByMap(warningMap) { | 533 | | - const catchWarning = {}; | 534 | | - Object.keys(warningMap).forEach((name) => { | 535 | | - let expected = warningMap[name]; | 536 | | - if (!Array.isArray(expected)) { | 537 | | - throw new Error('warningMap entries must be arrays consisting of two ' + | 538 | | - 'entries: [message, warningCode]'); | 539 | | - } | 540 | | - if (!(Array.isArray(expected[0]))) { | 541 | | - if (expected.length === 0) { | 542 | | - return; | 543 | | - } | 544 | | - expected = [[expected[0], expected[1]]]; | 545 | | - } | 546 | | - catchWarning[name] = _expectWarning(name, expected); | 547 | | - }); | 548 | | - process.on('warning', (warning) => catchWarning[warning.name](warning)); | 549 | | -} | 550 | | - | 551 | | -// Accepts a warning name and description or array of descriptions or a map | 552 | | -// of warning names to description(s) | 553 | | -// ensures a warning is generated for each name/description pair | | 533 | +// Accepts a warning name and description or array of descriptions or a map of | | 534 | +// warning names to description(s) ensures a warning is generated for each | | 535 | +// name/description pair. | | 536 | +// The expected messages have to be unique per `expectWarning()` call. | 554 | 537 | function expectWarning(nameOrMap, expected, code) { | | 538 | + if (catchWarning === undefined) { | | 539 | + catchWarning = {}; | | 540 | + process.on('warning', (warning) => catchWarning[warning.name](warning)); | | 541 | + } | 555 | 542 | if (typeof nameOrMap === 'string') { | 556 | | - expectWarningByName(nameOrMap, expected, code); | | 543 | + catchWarning[nameOrMap] = _expectWarning(nameOrMap, expected, code); | 557 | 544 | } else { | 558 | | - expectWarningByMap(nameOrMap); | | 545 | + Object.keys(nameOrMap).forEach((name) => { | | 546 | + catchWarning[name] = _expectWarning(name, nameOrMap[name]); | | 547 | + }); | 559 | 548 | } | 560 | 549 | } | 561 | 550 |
| @@ -769,7 +758,6 @@ module.exports = { | 769 | 758 | mustCallAtLeast, | 770 | 759 | mustNotCall, | 771 | 760 | nodeProcessAborted, | 772 | | - noWarnCode: undefined, | 773 | 761 | PIPE, | 774 | 762 | platformTimeout, | 775 | 763 | printSkipMessage, | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.mjs -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,6 @@ const { | 37 | 37 | nodeProcessAborted, | 38 | 38 | busyLoop, | 39 | 39 | isAlive, | 40 | | - noWarnCode, | 41 | 40 | expectWarning, | 42 | 41 | expectsError, | 43 | 42 | skipIfInspectorDisabled, | @@ -84,7 +83,6 @@ export { | 84 | 83 | nodeProcessAborted, | 85 | 84 | busyLoop, | 86 | 85 | isAlive, | 87 | | - noWarnCode, | 88 | 86 | expectWarning, | 89 | 87 | expectsError, | 90 | 88 | skipIfInspectorDisabled, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-atomics-notify.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -const { expectWarning, noWarnCode } = require('../common'); | | 3 | +const { expectWarning } = require('../common'); | 4 | 4 |
| 5 | 5 | const assert = require('assert'); | 6 | 6 | const { runInNewContext } = require('vm'); | @@ -14,6 +14,6 @@ assert.strictEqual(runInNewContext('typeof Atomics.notify'), 'function'); | 14 | 14 | expectWarning( | 15 | 15 | 'Atomics', | 16 | 16 | 'Atomics.wake will be removed in a future version, ' + | 17 | | - 'use Atomics.notify instead.', noWarnCode); | | 17 | + 'use Atomics.notify instead.'); | 18 | 18 |
| 19 | 19 | Atomics.wake(new Int32Array(new SharedArrayBuffer(4)), 0, 0); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-console.js +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,14 +42,14 @@ if (common.isMainThread) { | 42 | 42 | common.expectWarning( | 43 | 43 | 'Warning', | 44 | 44 | [ | 45 | | - ['Count for \'noLabel\' does not exist', common.noWarnCode], | 46 | | - ['No such label \'noLabel\' for console.timeLog()', common.noWarnCode], | 47 | | - ['No such label \'noLabel\' for console.timeEnd()', common.noWarnCode], | 48 | | - ['Count for \'default\' does not exist', common.noWarnCode], | 49 | | - ['No such label \'default\' for console.timeLog()', common.noWarnCode], | 50 | | - ['No such label \'default\' for console.timeEnd()', common.noWarnCode], | 51 | | - ['Label \'default\' already exists for console.time()', common.noWarnCode], | 52 | | - ['Label \'test\' already exists for console.time()', common.noWarnCode] | | 45 | + ['Count for \'noLabel\' does not exist'], | | 46 | + ['No such label \'noLabel\' for console.timeLog()'], | | 47 | + ['No such label \'noLabel\' for console.timeEnd()'], | | 48 | + ['Count for \'default\' does not exist'], | | 49 | + ['No such label \'default\' for console.timeLog()'], | | 50 | + ['No such label \'default\' for console.timeEnd()'], | | 51 | + ['Label \'default\' already exists for console.time()'], | | 52 | + ['Label \'test\' already exists for console.time()'] | 53 | 53 | ] | 54 | 54 | ); | 55 | 55 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-authenticated.js +19-19Lines changed: 19 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,25 +50,25 @@ const ciphers = crypto.getCiphers(); | 50 | 50 |
| 51 | 51 | const expectedWarnings = common.hasFipsCrypto ? | 52 | 52 | [] : [ | 53 | | - ['Use Cipheriv for counter mode of aes-192-gcm', common.noWarnCode], | 54 | | - ['Use Cipheriv for counter mode of aes-192-ccm', common.noWarnCode], | 55 | | - ['Use Cipheriv for counter mode of aes-192-ccm', common.noWarnCode], | 56 | | - ['Use Cipheriv for counter mode of aes-128-ccm', common.noWarnCode], | 57 | | - ['Use Cipheriv for counter mode of aes-128-ccm', common.noWarnCode], | 58 | | - ['Use Cipheriv for counter mode of aes-128-ccm', common.noWarnCode], | 59 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 60 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 61 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 62 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 63 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 64 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 65 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 66 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 67 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 68 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 69 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 70 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode], | 71 | | - ['Use Cipheriv for counter mode of aes-256-ccm', common.noWarnCode] | | 53 | + ['Use Cipheriv for counter mode of aes-192-gcm'], | | 54 | + ['Use Cipheriv for counter mode of aes-192-ccm'], | | 55 | + ['Use Cipheriv for counter mode of aes-192-ccm'], | | 56 | + ['Use Cipheriv for counter mode of aes-128-ccm'], | | 57 | + ['Use Cipheriv for counter mode of aes-128-ccm'], | | 58 | + ['Use Cipheriv for counter mode of aes-128-ccm'], | | 59 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 60 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 61 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 62 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 63 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 64 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 65 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 66 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 67 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 68 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 69 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 70 | + ['Use Cipheriv for counter mode of aes-256-ccm'], | | 71 | + ['Use Cipheriv for counter mode of aes-256-ccm'] | 72 | 72 | ]; | 73 | 73 |
| 74 | 74 | const expectedDeprecationWarnings = [ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-cipher-decipher.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,7 @@ const assert = require('assert'); | 12 | 12 |
| 13 | 13 | common.expectWarning({ | 14 | 14 | Warning: [ | 15 | | - ['Use Cipheriv for counter mode of aes-256-gcm', common.noWarnCode] | | 15 | + ['Use Cipheriv for counter mode of aes-256-gcm'] | 16 | 16 | ], | 17 | 17 | DeprecationWarning: [ | 18 | 18 | ['crypto.createCipher is deprecated.', 'DEP0106'] | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-dns-lookup.js +6-8Lines changed: 6 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,21 +21,19 @@ cares.getaddrinfo = () => internalBinding('uv').UV_ENOENT; | 21 | 21 | common.expectsError(() => dnsPromises.lookup(1, {}), err); | 22 | 22 | } | 23 | 23 |
| | 24 | +// This also verifies different expectWarning notations. | 24 | 25 | common.expectWarning({ | 25 | 26 | // For 'internal/test/binding' module. | 26 | 27 | 'internal/test/binding': [ | 27 | 28 | 'These APIs are for internal testing only. Do not use them.' | 28 | 29 | ], | 29 | 30 | // For dns.promises. | 30 | | - 'ExperimentalWarning': [ | 31 | | - 'The dns.promises API is experimental' | 32 | | - ], | | 31 | + 'ExperimentalWarning': 'The dns.promises API is experimental', | 33 | 32 | // For calling `dns.lookup` with falsy `hostname`. | 34 | | - 'DeprecationWarning': [ | 35 | | - 'The provided hostname "false" is not a valid ' + | 36 | | - 'hostname, and is supported in the dns module solely for compatibility.', | 37 | | - 'DEP0118', | 38 | | - ], | | 33 | + 'DeprecationWarning': { | | 34 | + DEP0118: 'The provided hostname "false" is not a valid ' + | | 35 | + 'hostname, and is supported in the dns module solely for compatibility.' | | 36 | + } | 39 | 37 | }); | 40 | 38 |
| 41 | 39 | common.expectsError(() => { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-filehandle.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,12 +21,10 @@ let fdnum; | 21 | 21 |
| 22 | 22 | common.expectWarning({ | 23 | 23 | 'internal/test/binding': [ | 24 | | - 'These APIs are for internal testing only. Do not use them.', | 25 | | - common.noWarnCode | | 24 | + 'These APIs are for internal testing only. Do not use them.' | 26 | 25 | ], | 27 | 26 | 'Warning': [ | 28 | | - `Closing file descriptor ${fdnum} on garbage collection`, | 29 | | - common.noWarnCode | | 27 | + `Closing file descriptor ${fdnum} on garbage collection` | 30 | 28 | ] | 31 | 29 | }); | 32 | 30 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-https-strict.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,8 +32,7 @@ common.expectWarning( | 32 | 32 | 'Warning', | 33 | 33 | 'Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to \'0\' ' + | 34 | 34 | 'makes TLS connections and HTTPS requests insecure by disabling ' + | 35 | | - 'certificate verification.', | 36 | | - common.noWarnCode | | 35 | + 'certificate verification.' | 37 | 36 | ); | 38 | 37 |
| 39 | 38 | const assert = require('assert'); | |
|