Skip to content

Commit 28e9a02

Browse files
Myles BorinsTrott
authored andcommitted
test: wrap assert.fail when passed to callback
Currently there are many instances where assert.fail is directly passed to a callback for error handling. Unfortunately this will swallow the error as it is the third argument of assert.fail that sets the message not the first. This commit adds a new function to test/common.js that simply wraps assert.fail and calls it with the provided message. Tip of the hat to @Trott for pointing me in the direction of this. PR-URL: nodejs#3453 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent b620790 commit 28e9a02

37 files changed

+81
-77
lines changed

test/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,7 @@ exports.fileExists = function(pathname) {
442442
return false;
443443
}
444444
};
445+
446+
exports.fail = function(msg) {
447+
assert.fail(null, null, msg);
448+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function master() {
2424
});
2525
proc.stdout.on('data', function(data) {
2626
assert.equal(data, 'ok\r\n');
27-
net.createServer(assert.fail).listen(common.PORT, function() {
27+
net.createServer(common.fail).listen(common.PORT, function() {
2828
handle = this._handle;
2929
proc.send('one');
3030
proc.send('two', handle);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const empty = common.fixturesDir + '/empty.js';
1313

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

1919
// verify that valid argument combinations do not throw

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ if (cluster.isMaster) {
2020
}));
2121
}
2222
else {
23-
var s = net.createServer(assert.fail);
24-
s.listen(42, assert.fail.bind(null, 'listen should have failed'));
23+
var s = net.createServer(common.fail);
24+
s.listen(42, common.fail.bind(null, 'listen should have failed'));
2525
s.on('error', common.mustCall(function(err) {
2626
assert.equal(err.code, 'EACCES');
2727
process.disconnect();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ if (!id) {
6868
else if (id === 'one') {
6969
if (cluster.isMaster) return startWorker();
7070

71-
var server = http.createServer(assert.fail).listen(common.PORT, function() {
71+
var server = http.createServer(common.fail).listen(common.PORT, function() {
7272
process.send('READY');
7373
});
7474

@@ -84,12 +84,12 @@ else if (id === 'two') {
8484
assert(ok);
8585
});
8686

87-
var server = http.createServer(assert.fail);
87+
var server = http.createServer(common.fail);
8888
process.on('message', function(m) {
8989
if (typeof m === 'object') return; // ignore system messages
9090
if (m === 'QUIT') process.exit();
9191
assert.equal(m, 'START');
92-
server.listen(common.PORT, assert.fail);
92+
server.listen(common.PORT, common.fail);
9393
server.on('error', function(e) {
9494
assert.equal(e.code, 'EADDRINUSE');
9595
process.send(e.code);

test/parallel/test-cluster-eaddrinuse.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var net = require('net');
1212
var id = '' + process.argv[2];
1313

1414
if (id === 'undefined') {
15-
var server = net.createServer(assert.fail);
15+
var server = net.createServer(common.fail);
1616
server.listen(common.PORT, function() {
1717
var worker = fork(__filename, ['worker']);
1818
worker.on('message', function(msg) {
@@ -24,14 +24,14 @@ if (id === 'undefined') {
2424
});
2525
}
2626
else if (id === 'worker') {
27-
var server = net.createServer(assert.fail);
28-
server.listen(common.PORT, assert.fail);
27+
var server = net.createServer(common.fail);
28+
server.listen(common.PORT, common.fail);
2929
server.on('error', common.mustCall(function(e) {
3030
assert(e.code, 'EADDRINUSE');
3131
process.send('stop-listening');
3232
process.once('message', function(msg) {
3333
if (msg !== 'stopped-listening') return;
34-
server = net.createServer(assert.fail);
34+
server = net.createServer(common.fail);
3535
server.listen(common.PORT, common.mustCall(function() {
3636
server.close();
3737
}));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ if (cluster.isMaster) {
1717
}
1818
else {
1919
// listen() without port should not trigger a libuv assert
20-
net.createServer(assert.fail).listen(process.exit);
20+
net.createServer(common.fail).listen(process.exit);
2121
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (cluster.isMaster) {
1010
if (msg === 'done') this.kill();
1111
});
1212
} else {
13-
const server = net.createServer(assert.fail);
13+
const server = net.createServer(common.fail);
1414
server.listen(common.PORT, function() {
1515
server.unref();
1616
server.ref();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var common = require('../common');
33
var assert = require('assert');
44
var cluster = require('cluster');
55

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

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

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

Lines changed: 3 additions & 3 deletions
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-
net.createServer(assert.fail).listen(common.PORT, function() {
11+
net.createServer(common.fail).listen(common.PORT, function() {
1212
var server = this;
1313
var worker = cluster.fork();
1414
worker.on('exit', common.mustCall(function(exitCode) {
@@ -18,8 +18,8 @@ if (cluster.isMaster) {
1818
});
1919
}
2020
else {
21-
var s = net.createServer(assert.fail);
22-
s.listen(common.PORT, assert.fail.bind(null, 'listen should have failed'));
21+
var s = net.createServer(common.fail);
22+
s.listen(common.PORT, common.fail.bind(null, 'listen should have failed'));
2323
s.on('error', common.mustCall(function(err) {
2424
assert.equal(err.code, 'EADDRINUSE');
2525
process.disconnect();

0 commit comments

Comments
 (0)