summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-10-22 12:26:32 -0700
committerRich Trott <rtrott@gmail.com>2017-11-16 22:00:24 -0800
commit07d39a2262dac233b5f86b06ecc16484ab0f7858 (patch)
tree14c1c29089ebb87a792c114c42d7f12f2d0ec187 /test
parentccc87ebb3393a7a4738ed20d9378857633e74c76 (diff)
downloadandroid-node-v8-07d39a2262dac233b5f86b06ecc16484ab0f7858.tar.gz
android-node-v8-07d39a2262dac233b5f86b06ecc16484ab0f7858.tar.bz2
android-node-v8-07d39a2262dac233b5f86b06ecc16484ab0f7858.zip
util: emit deprecation code only once
If another function has already emitted the deprecation warning with the same code as the warning that is about to be emitted, do not emit the warning. This is a breaking change. Previously, different functions could emit the same deprecation warning multiple times. This was a known bug rather than a feature, but this change is being treated as a breaking change out of caution. Identical deprecation warnings should not be emitted. PR-URL: https://github.com/nodejs/node/pull/16393 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-util-deprecate.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/parallel/test-util-deprecate.js b/test/parallel/test-util-deprecate.js
new file mode 100644
index 0000000000..1b4a5e7662
--- /dev/null
+++ b/test/parallel/test-util-deprecate.js
@@ -0,0 +1,57 @@
+'use strict';
+
+require('../common');
+
+// Tests basic functionality of util.deprecate().
+
+const assert = require('assert');
+const util = require('util');
+
+const expectedWarnings = new Map();
+
+// Emits deprecation only once if same function is called.
+{
+ const msg = 'fhqwhgads';
+ const fn = util.deprecate(() => {}, msg);
+ expectedWarnings.set(msg, { code: undefined, count: 1 });
+ fn();
+ fn();
+}
+
+// Emits deprecation twice for different functions.
+{
+ const msg = 'sterrance';
+ const fn1 = util.deprecate(() => {}, msg);
+ const fn2 = util.deprecate(() => {}, msg);
+ expectedWarnings.set(msg, { code: undefined, count: 2 });
+ fn1();
+ fn2();
+}
+
+// Emits deprecation only once if optional code is the same, even for different
+// functions.
+{
+ const msg = 'cannonmouth';
+ const code = 'deprecatesque';
+ const fn1 = util.deprecate(() => {}, msg, code);
+ const fn2 = util.deprecate(() => {}, msg, code);
+ expectedWarnings.set(msg, { code, count: 1 });
+ fn1();
+ fn2();
+ fn1();
+ fn2();
+}
+
+process.on('warning', (warning) => {
+ assert.strictEqual(warning.name, 'DeprecationWarning');
+ assert.ok(expectedWarnings.has(warning.message));
+ const expected = expectedWarnings.get(warning.message);
+ assert.strictEqual(warning.code, expected.code);
+ expected.count = expected.count - 1;
+ if (expected.count === 0)
+ expectedWarnings.delete(warning.message);
+});
+
+process.on('exit', () => {
+ assert.deepStrictEqual(expectedWarnings, new Map());
+});