Skip to content

Commit f0eba78

Browse files
cjihrigitaloacasas
authored andcommitted
test: add common.mustNotCall()
This commit adds a mustNotCall() helper for testing. This provides an alternative to using common.fail() as a callback, or creating a callback function for the sole purpose of calling common.fail(). PR-URL: #11152 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent 44b17a2 commit f0eba78

File tree

124 files changed

+243
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+243
-234
lines changed

test/common.js

+6
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ function fail(msg) {
494494
}
495495
exports.fail = fail;
496496

497+
exports.mustNotCall = function(msg) {
498+
return function mustNotCall() {
499+
fail(msg || 'function should not have been called');
500+
};
501+
};
502+
497503
exports.skip = function(msg) {
498504
console.log(`1..0 # Skipped: ${msg}`);
499505
};

test/debugger/test-debugger-remote.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const interfacer = spawn(process.execPath, ['debug', `localhost:${PORT}`]);
1515
interfacer.stdout.setEncoding('utf-8');
1616

1717
// fail the test if either of the processes exit normally
18-
const debugBreakExit = common.fail.bind(null, 'child should not exit normally');
19-
const debugExit = common.fail.bind(null, 'interfacer should not exit normally');
18+
const debugBreakExit = common.mustNotCall('child should not exit normally');
19+
const debugExit = common.mustNotCall('interfacer should not exit normally');
2020
child.on('exit', debugBreakExit);
2121
interfacer.on('exit', debugExit);
2222

test/inspector/inspector-helper.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ function makeBufferingDataCallback(dataCallback) {
116116
}
117117

118118
function timeout(message, multiplicator) {
119-
return setTimeout(() => common.fail(message), TIMEOUT * (multiplicator || 1));
119+
return setTimeout(common.mustNotCall(message),
120+
TIMEOUT * (multiplicator || 1));
120121
}
121122

122123
const TestSession = function(socket, harness) {
@@ -398,7 +399,7 @@ Harness.prototype.wsHandshake = function(devtoolsUrl, tests, readyCallback) {
398399
});
399400
}
400401
enqueue(tests);
401-
}).on('response', () => common.fail('Upgrade was not received'));
402+
}).on('response', common.mustNotCall('Upgrade was not received'));
402403
};
403404

404405
Harness.prototype.runFrontendSession = function(tests) {

test/internet/test-dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function checkWrap(req) {
4545

4646
TEST(function test_reverse_bogus(done) {
4747
assert.throws(() => {
48-
dns.reverse('bogus ip', common.fail);
48+
dns.reverse('bogus ip', common.mustNotCall());
4949
}, /^Error: getHostByAddr EINVAL$/);
5050
done();
5151
});

test/internet/test-http-dns-fail.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function httpreq(count) {
1616
port: 80,
1717
path: '/',
1818
method: 'GET'
19-
}, common.fail);
19+
}, common.mustNotCall());
2020

2121
req.on('error', common.mustCall((e) => {
2222
assert.strictEqual(e.code, 'ENOTFOUND');

test/internet/test-net-connect-timeout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ socket.on('timeout', common.mustCall(function() {
2626
socket.destroy();
2727
}));
2828

29-
socket.on('connect', common.fail);
29+
socket.on('connect', common.mustNotCall());

test/internet/test-net-connect-unref.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ const client = net.createConnection(53, '8.8.8.8', function() {
88
client.unref();
99
});
1010

11-
client.on('close', common.fail);
11+
client.on('close', common.mustNotCall());
1212

13-
setTimeout(common.fail, TIMEOUT).unref();
13+
setTimeout(common.mustNotCall(), TIMEOUT).unref();

test/parallel/test-async-wrap-throw-from-callback.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ if (typeof process.argv[2] === 'string') {
3737
async_wrap.setupHooks({ init, pre, post, destroy });
3838
async_wrap.enable();
3939

40-
process.on('uncaughtException', common.fail);
40+
process.on('uncaughtException', common.mustNotCall());
4141

4242
const d = domain.create();
43-
d.on('error', common.fail);
43+
d.on('error', common.mustNotCall());
4444
d.run(() => {
4545
// Using randomBytes because timers are not yet supported.
4646
crypto.randomBytes(0, () => { });

test/parallel/test-child-process-kill.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const spawn = require('child_process').spawn;
55
const cat = spawn(common.isWindows ? 'cmd' : 'cat');
66

77
cat.stdout.on('end', common.mustCall(function() {}));
8-
cat.stderr.on('data', common.fail);
8+
cat.stderr.on('data', common.mustNotCall());
99
cat.stderr.on('end', common.mustCall(function() {}));
1010

1111
cat.on('exit', common.mustCall(function(code, signal) {

test/parallel/test-child-process-recv-handle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function master() {
2424
});
2525
proc.stdout.on('data', common.mustCall((data) => {
2626
assert.strictEqual(data.toString(), 'ok\r\n');
27-
net.createServer(common.fail).listen(0, function() {
27+
net.createServer(common.mustNotCall()).listen(0, function() {
2828
handle = this._handle;
2929
proc.send('one');
3030
proc.send('two', handle);

test/parallel/test-child-process-send-returns-boolean.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ s.on('exit', function() {
2020
handle.close();
2121
});
2222

23-
net.createServer(common.fail).listen(0, function() {
23+
net.createServer(common.mustNotCall()).listen(0, function() {
2424
handle = this._handle;
2525
assert.strictEqual(s.send('one', handle), true);
2626
assert.strictEqual(s.send('two', handle), true);

test/parallel/test-child-process-spawn-shell.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const cp = require('child_process');
77
const doesNotExist = cp.spawn('does-not-exist', {shell: true});
88

99
assert.notStrictEqual(doesNotExist.spawnfile, 'does-not-exist');
10-
doesNotExist.on('error', common.fail);
10+
doesNotExist.on('error', common.mustNotCall());
1111
doesNotExist.on('exit', common.mustCall((code, signal) => {
1212
assert.strictEqual(signal, null);
1313

test/parallel/test-child-process-spawn-typeerror.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const empty = common.fixturesDir + '/empty.js';
1313

1414
assert.throws(function() {
1515
const child = spawn(invalidcmd, 'this is not an array');
16-
child.on('error', common.fail);
16+
child.on('error', common.mustNotCall());
1717
}, TypeError);
1818

1919
// verify that valid argument combinations do not throw

test/parallel/test-child-process-stdin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cat.stdout.on('data', function(chunk) {
2424

2525
cat.stdout.on('end', common.mustCall(function() {}));
2626

27-
cat.stderr.on('data', common.fail);
27+
cat.stderr.on('data', common.mustNotCall());
2828

2929
cat.stderr.on('end', common.mustCall(function() {}));
3030

test/parallel/test-child-process-stdout-flush.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const child = spawn(process.argv[0], [sub, n]);
1212
let count = 0;
1313

1414
child.stderr.setEncoding('utf8');
15-
child.stderr.on('data', common.fail);
15+
child.stderr.on('data', common.mustNotCall());
1616

1717
child.stdout.setEncoding('utf8');
1818
child.stdout.on('data', (data) => {

test/parallel/test-cluster-bind-privileged-port.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ if (cluster.isMaster) {
1919
assert.strictEqual(exitCode, 0);
2020
}));
2121
} else {
22-
const s = net.createServer(common.fail);
23-
s.listen(42, common.fail.bind(null, 'listen should have failed'));
22+
const s = net.createServer(common.mustNotCall());
23+
s.listen(42, common.mustNotCall('listen should have failed'));
2424
s.on('error', common.mustCall((err) => {
2525
assert.strictEqual(err.code, 'EACCES');
2626
process.disconnect();

test/parallel/test-cluster-bind-twice.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,22 @@ if (!id) {
6060
} else if (id === 'one') {
6161
if (cluster.isMaster) return startWorker();
6262

63-
http.createServer(common.fail).listen(common.PORT, common.mustCall(() => {
64-
process.send('READY');
65-
}));
63+
http.createServer(common.mustNotCall())
64+
.listen(common.PORT, common.mustCall(() => {
65+
process.send('READY');
66+
}));
6667

6768
process.on('message', common.mustCall((m) => {
6869
if (m === 'QUIT') process.exit();
6970
}));
7071
} else if (id === 'two') {
7172
if (cluster.isMaster) return startWorker();
7273

73-
const server = http.createServer(common.fail);
74+
const server = http.createServer(common.mustNotCall());
7475
process.on('message', common.mustCall((m) => {
7576
if (m === 'QUIT') process.exit();
7677
assert.strictEqual(m, 'START');
77-
server.listen(common.PORT, common.fail);
78+
server.listen(common.PORT, common.mustNotCall());
7879
server.on('error', common.mustCall((e) => {
7980
assert.strictEqual(e.code, 'EADDRINUSE');
8081
process.send(e.code);

test/parallel/test-cluster-eaddrinuse.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const net = require('net');
1111
const id = '' + process.argv[2];
1212

1313
if (id === 'undefined') {
14-
const server = net.createServer(common.fail);
14+
const server = net.createServer(common.mustNotCall());
1515
server.listen(common.PORT, function() {
1616
const worker = fork(__filename, ['worker']);
1717
worker.on('message', function(msg) {
@@ -22,14 +22,14 @@ if (id === 'undefined') {
2222
});
2323
});
2424
} else if (id === 'worker') {
25-
let server = net.createServer(common.fail);
26-
server.listen(common.PORT, common.fail);
25+
let server = net.createServer(common.mustNotCall());
26+
server.listen(common.PORT, common.mustNotCall());
2727
server.on('error', common.mustCall(function(e) {
2828
assert(e.code, 'EADDRINUSE');
2929
process.send('stop-listening');
3030
process.once('message', function(msg) {
3131
if (msg !== 'stopped-listening') return;
32-
server = net.createServer(common.fail);
32+
server = net.createServer(common.mustNotCall());
3333
server.listen(common.PORT, common.mustCall(function() {
3434
server.close();
3535
}));

test/parallel/test-cluster-listening-port.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ if (cluster.isMaster) {
1515
worker.kill();
1616
}));
1717
} else {
18-
net.createServer(common.fail).listen(0);
18+
net.createServer(common.mustNotCall()).listen(0);
1919
}

test/parallel/test-cluster-net-listen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ if (cluster.isMaster) {
1111
}));
1212
} else {
1313
// listen() without port should not trigger a libuv assert
14-
net.createServer(common.fail).listen(process.exit);
14+
net.createServer(common.mustNotCall()).listen(process.exit);
1515
}

test/parallel/test-cluster-rr-ref.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (cluster.isMaster) {
99
if (msg === 'done') this.kill();
1010
});
1111
} else {
12-
const server = net.createServer(common.fail);
12+
const server = net.createServer(common.mustNotCall());
1313
server.listen(common.PORT, function() {
1414
server.unref();
1515
server.ref();

test/parallel/test-cluster-setup-master-argv.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
const assert = require('assert');
44
const cluster = require('cluster');
55

6-
setTimeout(common.fail.bind(assert, 'setup not emitted'), 1000).unref();
6+
setTimeout(common.mustNotCall('setup not emitted'), 1000).unref();
77

88
cluster.on('setup', common.mustCall(function() {
99
const clusterArgs = cluster.settings.args;

test/parallel/test-cluster-shared-handle-bind-error.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (cluster.isMaster) {
88
// Master opens and binds the socket and shares it with the worker.
99
cluster.schedulingPolicy = cluster.SCHED_NONE;
1010
// Hog the TCP port so that when the worker tries to bind, it'll fail.
11-
const server = net.createServer(common.fail);
11+
const server = net.createServer(common.mustNotCall());
1212

1313
server.listen(common.PORT, common.mustCall(() => {
1414
const worker = cluster.fork();
@@ -18,8 +18,8 @@ if (cluster.isMaster) {
1818
}));
1919
}));
2020
} else {
21-
const s = net.createServer(common.fail);
22-
s.listen(common.PORT, common.fail.bind(null, 'listen should have failed'));
21+
const s = net.createServer(common.mustNotCall());
22+
s.listen(common.PORT, common.mustNotCall('listen should have failed'));
2323
s.on('error', common.mustCall((err) => {
2424
assert.strictEqual(err.code, 'EADDRINUSE');
2525
process.disconnect();

test/parallel/test-cluster-shared-handle-bind-privileged-port.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ if (cluster.isMaster) {
2121
assert.strictEqual(exitCode, 0);
2222
}));
2323
} else {
24-
const s = net.createServer(common.fail);
25-
s.listen(42, common.fail.bind(null, 'listen should have failed'));
24+
const s = net.createServer(common.mustNotCall());
25+
s.listen(42, common.mustNotCall('listen should have failed'));
2626
s.on('error', common.mustCall(function(err) {
2727
assert.strictEqual(err.code, 'EACCES');
2828
process.disconnect();

test/parallel/test-console-instance.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const err = new Stream();
99

1010
// ensure the Console instance doesn't write to the
1111
// process' "stdout" or "stderr" streams
12-
process.stdout.write = process.stderr.write = common.fail;
12+
process.stdout.write = process.stderr.write = common.mustNotCall();
1313

1414
// make sure that the "Console" function exists
1515
assert.strictEqual('function', typeof Console);

test/parallel/test-crypto-pbkdf2.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,30 @@ assert.throws(function() {
6363

6464
// Should not work with Infinity key length
6565
assert.throws(function() {
66-
crypto.pbkdf2('password', 'salt', 1, Infinity, 'sha256', common.fail);
66+
crypto.pbkdf2('password', 'salt', 1, Infinity, 'sha256',
67+
common.mustNotCall());
6768
}, /^TypeError: Bad key length$/);
6869

6970
// Should not work with negative Infinity key length
7071
assert.throws(function() {
71-
crypto.pbkdf2('password', 'salt', 1, -Infinity, 'sha256', common.fail);
72+
crypto.pbkdf2('password', 'salt', 1, -Infinity, 'sha256',
73+
common.mustNotCall());
7274
}, /^TypeError: Bad key length$/);
7375

7476
// Should not work with NaN key length
7577
assert.throws(function() {
76-
crypto.pbkdf2('password', 'salt', 1, NaN, 'sha256', common.fail);
78+
crypto.pbkdf2('password', 'salt', 1, NaN, 'sha256', common.mustNotCall());
7779
}, /^TypeError: Bad key length$/);
7880

7981
// Should not work with negative key length
8082
assert.throws(function() {
81-
crypto.pbkdf2('password', 'salt', 1, -1, 'sha256', common.fail);
83+
crypto.pbkdf2('password', 'salt', 1, -1, 'sha256', common.mustNotCall());
8284
}, /^TypeError: Bad key length$/);
8385

8486
// Should not work with key length that does not fit into 32 signed bits
8587
assert.throws(function() {
86-
crypto.pbkdf2('password', 'salt', 1, 4073741824, 'sha256', common.fail);
88+
crypto.pbkdf2('password', 'salt', 1, 4073741824, 'sha256',
89+
common.mustNotCall());
8790
}, /^TypeError: Bad key length$/);
8891

8992
// Should not get FATAL ERROR with empty password and salt

test/parallel/test-crypto-verify-failure.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ server.listen(0, common.mustCall(() => {
4141
}, common.mustCall(() => {
4242
verify();
4343
}))
44-
.on('error', common.fail)
44+
.on('error', common.mustNotCall())
4545
.on('close', common.mustCall(() => {
4646
server.close();
4747
})).resume();

test/parallel/test-dgram-error-message-address.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const dgram = require('dgram');
66
// IPv4 Test
77
const socket_ipv4 = dgram.createSocket('udp4');
88

9-
socket_ipv4.on('listening', common.fail);
9+
socket_ipv4.on('listening', common.mustNotCall());
1010

1111
socket_ipv4.on('error', common.mustCall(function(e) {
1212
assert.strictEqual(e.port, undefined);
@@ -21,7 +21,7 @@ socket_ipv4.bind(0, '1.1.1.1');
2121
// IPv6 Test
2222
const socket_ipv6 = dgram.createSocket('udp6');
2323

24-
socket_ipv6.on('listening', common.fail);
24+
socket_ipv6.on('listening', common.mustNotCall());
2525

2626
socket_ipv6.on('error', common.mustCall(function(e) {
2727
// EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system.

test/parallel/test-dgram-unref.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ const s = dgram.createSocket('udp4');
66
s.bind();
77
s.unref();
88

9-
setTimeout(common.fail, 1000).unref();
9+
setTimeout(common.mustNotCall(), 1000).unref();

test/parallel/test-domain-multi.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const http = require('http');
88
const a = domain.create();
99
a.enter(); // this will be our "root" domain
1010

11-
a.on('error', common.fail);
11+
a.on('error', common.mustNotCall());
1212

1313
const server = http.createServer((req, res) => {
1414
// child domain of a.

test/parallel/test-event-emitter-remove-listeners.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function listener2() {}
2020
{
2121
const ee = new EventEmitter();
2222
ee.on('hello', listener1);
23-
ee.on('removeListener', common.fail);
23+
ee.on('removeListener', common.mustNotCall());
2424
ee.removeListener('hello', listener2);
2525
assert.deepStrictEqual([listener1], ee.listeners('hello'));
2626
}

test/parallel/test-force-repl.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const spawn = require('child_process').spawn;
55

66
// spawn a node child process in "interactive" mode (force the repl)
77
const cp = spawn(process.execPath, ['-i']);
8-
const timeoutId = setTimeout(function() {
9-
common.fail('timeout!');
10-
}, common.platformTimeout(5000)); // give node + the repl 5 seconds to start
8+
// give node + the repl 5 seconds to start
9+
const timeoutId = setTimeout(common.mustNotCall(),
10+
common.platformTimeout(5000));
1111

1212
cp.stdout.setEncoding('utf8');
1313

test/parallel/test-fs-access.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fs.access(readOnlyFile, fs.W_OK, common.mustCall((err) => {
8282
}));
8383

8484
assert.throws(() => {
85-
fs.access(100, fs.F_OK, () => { common.fail('callback should not run'); });
85+
fs.access(100, fs.F_OK, common.mustNotCall());
8686
}, /^TypeError: path must be a string or Buffer$/);
8787

8888
assert.throws(() => {

0 commit comments

Comments
 (0)