Skip to content

Commit bea0a6e

Browse files
cjihrigMylesBorins
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 <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
1 parent f2230cc commit bea0a6e

File tree

106 files changed

+178
-200
lines changed

Some content is hidden

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

106 files changed

+178
-200
lines changed

Diff for: test/common.js

+6
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ function fail(msg) {
464464
}
465465
exports.fail = fail;
466466

467+
exports.mustNotCall = function(msg) {
468+
return function mustNotCall() {
469+
fail(msg || 'function should not have been called');
470+
};
471+
};
472+
467473
exports.skip = function(msg) {
468474
console.log(`1..0 # Skipped: ${msg}`);
469475
};

Diff for: 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

Diff for: 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) {

Diff for: 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');

Diff for: 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());

Diff for: 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();

Diff for: 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) {

Diff for: 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);

Diff for: 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);

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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) => {

Diff for: 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();

Diff for: 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);

Diff for: 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
}));

Diff for: 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
}

Diff for: 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
}

Diff for: 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();

Diff for: 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;

Diff for: 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();

Diff for: 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();

Diff for: 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);

Diff for: 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

Diff for: 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();

Diff for: 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.

Diff for: test/parallel/test-dgram-unref.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ const dgram = require('dgram');
1616
s.close(common.mustCall(() => s.unref()));
1717
}
1818

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

Diff for: 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.

Diff for: 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
}

Diff for: 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

Diff for: test/parallel/test-fs-read-stream-err.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ fs.read = function() {
3838
};
3939

4040
stream.on('data', (buf) => {
41-
stream.on('data', () => common.fail("no more 'data' events should follow"));
41+
stream.on('data', common.mustNotCall("no more 'data' events should follow"));
4242
});

Diff for: test/parallel/test-http-abort-before-end.js

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

6-
const server = http.createServer(common.fail);
6+
const server = http.createServer(common.mustNotCall());
77

88
server.listen(0, function() {
99
const req = http.request({

Diff for: test/parallel/test-http-abort-queued-2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ server.listen(0, common.mustCall(() => {
2424

2525
http.get({agent, port}, (res) => res.resume());
2626

27-
const req = http.get({agent, port}, common.fail);
27+
const req = http.get({agent, port}, common.mustNotCall());
2828
req.abort();
2929

3030
http.get({agent, port}, common.mustCall((res) => {

0 commit comments

Comments
 (0)