Skip to content

Commit 60de9f8

Browse files
Myles Borinsrvagg
Myles Borins
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: #3453 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 7added3 commit 60de9f8

37 files changed

+81
-77
lines changed

test/common.js

+4
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

+1-1
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

+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
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

+2-2
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

+3-3
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

+4-4
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

+1-1
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

+1-1
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

+1-1
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

+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-
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();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ if (cluster.isMaster) {
2222
}));
2323
}
2424
else {
25-
var s = net.createServer(assert.fail);
26-
s.listen(42, assert.fail.bind(null, 'listen should have failed'));
25+
var s = net.createServer(common.fail);
26+
s.listen(42, common.fail.bind(null, 'listen should have failed'));
2727
s.on('error', common.mustCall(function(err) {
2828
assert.equal(err.code, 'EACCES');
2929
process.disconnect();

test/parallel/test-crypto-pbkdf2.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,28 @@ assert.throws(function() {
6262

6363
// Should not work with Infinity key length
6464
assert.throws(function() {
65-
crypto.pbkdf2('password', 'salt', 1, Infinity, assert.fail);
65+
crypto.pbkdf2('password', 'salt', 1, Infinity, common.fail);
6666
}, function(err) {
6767
return err instanceof Error && err.message === 'Bad key length';
6868
});
6969

7070
// Should not work with negative Infinity key length
7171
assert.throws(function() {
72-
crypto.pbkdf2('password', 'salt', 1, -Infinity, assert.fail);
72+
crypto.pbkdf2('password', 'salt', 1, -Infinity, common.fail);
7373
}, function(err) {
7474
return err instanceof Error && err.message === 'Bad key length';
7575
});
7676

7777
// Should not work with NaN key length
7878
assert.throws(function() {
79-
crypto.pbkdf2('password', 'salt', 1, NaN, assert.fail);
79+
crypto.pbkdf2('password', 'salt', 1, NaN, common.fail);
8080
}, function(err) {
8181
return err instanceof Error && err.message === 'Bad key length';
8282
});
8383

8484
// Should not work with negative key length
8585
assert.throws(function() {
86-
crypto.pbkdf2('password', 'salt', 1, -1, assert.fail);
86+
crypto.pbkdf2('password', 'salt', 1, -1, common.fail);
8787
}, function(err) {
8888
return err instanceof Error && err.message === 'Bad key length';
8989
});

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

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

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

1111
socket_ipv4.on('error', common.mustCall(function(e) {
1212
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1:' + common.PORT);
@@ -22,7 +22,7 @@ socket_ipv4.bind(common.PORT, '1.1.1.1');
2222
var socket_ipv6 = dgram.createSocket('udp6');
2323
var family_ipv6 = 'IPv6';
2424

25-
socket_ipv6.on('listening', assert.fail);
25+
socket_ipv6.on('listening', common.fail);
2626

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

test/parallel/test-dgram-oob-buffer.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ socket.send(buf, 3, 1, common.PORT, '127.0.0.1', ok);
1919
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', ok);
2020

2121
assert.throws(function() {
22-
socket.send(buf, 0, 5, common.PORT, '127.0.0.1', assert.fail);
22+
socket.send(buf, 0, 5, common.PORT, '127.0.0.1', common.fail);
2323
});
2424
assert.throws(function() {
25-
socket.send(buf, 2, 3, common.PORT, '127.0.0.1', assert.fail);
25+
socket.send(buf, 2, 3, common.PORT, '127.0.0.1', common.fail);
2626
});
2727
assert.throws(function() {
28-
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail);
28+
socket.send(buf, 4, 4, common.PORT, '127.0.0.1', common.fail);
2929
});
3030
assert.throws(function() {
31-
socket.send('abc', 4, 1, common.PORT, '127.0.0.1', assert.fail);
31+
socket.send('abc', 4, 1, common.PORT, '127.0.0.1', common.fail);
3232
});
3333
assert.throws(function() {
34-
socket.send('abc', 0, 4, common.PORT, '127.0.0.1', assert.fail);
34+
socket.send('abc', 0, 4, common.PORT, '127.0.0.1', common.fail);
3535
});
3636
assert.throws(function() {
37-
socket.send('abc', -1, 2, common.PORT, '127.0.0.1', assert.fail);
37+
socket.send('abc', -1, 2, common.PORT, '127.0.0.1', common.fail);
3838
});
3939

4040
socket.close(); // FIXME should not be necessary

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ assert.deepEqual([], e1.listeners('hello'));
3939

4040
var e2 = new events.EventEmitter();
4141
e2.on('hello', listener1);
42-
e2.on('removeListener', assert.fail);
42+
e2.on('removeListener', common.fail);
4343
e2.removeListener('hello', listener2);
4444
assert.deepEqual([listener1], e2.listeners('hello'));
4545

test/parallel/test-fs-null-bytes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ check(fs.symlink, fs.symlinkSync, 'foo\u0000bar', 'foobar');
4343
check(fs.symlink, fs.symlinkSync, 'foobar', 'foo\u0000bar');
4444
check(fs.truncate, fs.truncateSync, 'foo\u0000bar');
4545
check(fs.unlink, fs.unlinkSync, 'foo\u0000bar');
46-
check(null, fs.unwatchFile, 'foo\u0000bar', assert.fail);
46+
check(null, fs.unwatchFile, 'foo\u0000bar', common.fail);
4747
check(fs.utimes, fs.utimesSync, 'foo\u0000bar', 0, 0);
48-
check(null, fs.watch, 'foo\u0000bar', assert.fail);
49-
check(null, fs.watchFile, 'foo\u0000bar', assert.fail);
48+
check(null, fs.watch, 'foo\u0000bar', common.fail);
49+
check(null, fs.watchFile, 'foo\u0000bar', common.fail);
5050
check(fs.writeFile, fs.writeFileSync, 'foo\u0000bar');
5151

5252
// an 'error' for exists means that it doesn't exist.

test/parallel/test-fs-read-stream-err.js

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

4141
stream.on('data', function(buf) {
42-
stream.on('data', assert.fail); // no more 'data' events should follow
42+
stream.on('data', common.fail); // no more 'data' events should follow
4343
});

test/parallel/test-http-client-unescaped-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ var http = require('http');
55

66
assert.throws(function() {
77
// Path with spaces in it should throw.
8-
http.get({ path: 'bad path' }, assert.fail);
8+
http.get({ path: 'bad path' }, common.fail);
99
}, /contains unescaped characters/);

test/parallel/test-http-set-trailers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ server.on('listening', function() {
5353
c.on('connect', function() {
5454
outstanding_reqs++;
5555
c.write('GET / HTTP/1.1\r\n\r\n');
56-
tid = setTimeout(assert.fail, 2000, 'Couldn\'t find last chunk.');
56+
tid = setTimeout(common.fail, 2000, 'Couldn\'t find last chunk.');
5757
});
5858

5959
c.on('data', function(chunk) {

test/parallel/test-https-timeout-server-2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var options = {
1818
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
1919
};
2020

21-
var server = https.createServer(options, assert.fail);
21+
var server = https.createServer(options, common.fail);
2222

2323
server.on('secureConnection', function(cleartext) {
2424
var s = cleartext.setTimeout(50, function() {

test/parallel/test-https-timeout-server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var options = {
2424
handshakeTimeout: 50
2525
};
2626

27-
var server = https.createServer(options, assert.fail);
27+
var server = https.createServer(options, common.fail);
2828

2929
server.on('clientError', function(err, conn) {
3030
// Don't hesitate to update the asserts if the internal structure of

test/parallel/test-listen-fd-ebadf.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ process.on('exit', function() {
99
assert.equal(gotError, 2);
1010
});
1111

12-
net.createServer(assert.fail).listen({fd:2}).on('error', onError);
13-
net.createServer(assert.fail).listen({fd:42}).on('error', onError);
12+
net.createServer(common.fail).listen({fd:2}).on('error', onError);
13+
net.createServer(common.fail).listen({fd:42}).on('error', onError);
1414

1515
function onError(ex) {
1616
assert.equal(ex.code, 'EINVAL');

test/parallel/test-net-better-error-messages-listen-path.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var common = require('../common');
33
var assert = require('assert');
44
var net = require('net');
55
var fp = '/blah/fadfa';
6-
var server = net.createServer(assert.fail);
7-
server.listen(fp, assert.fail);
6+
var server = net.createServer(common.fail);
7+
server.listen(fp, common.fail);
88
server.on('error', common.mustCall(function(e) {
99
assert.equal(e.address, fp);
1010
}));

test/parallel/test-net-better-error-messages-listen.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var common = require('../common');
33
var assert = require('assert');
44
var net = require('net');
55

6-
var server = net.createServer(assert.fail);
7-
server.listen(1, '1.1.1.1', assert.fail);
6+
var server = net.createServer(common.fail);
7+
server.listen(1, '1.1.1.1', common.fail);
88
server.on('error', common.mustCall(function(e) {
99
assert.equal(e.address, '1.1.1.1');
1010
assert.equal(e.port, 1);

test/parallel/test-net-better-error-messages-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var assert = require('assert');
55
var fp = '/tmp/fadagagsdfgsdf';
66
var c = net.connect(fp);
77

8-
c.on('connect', assert.fail);
8+
c.on('connect', common.fail);
99

1010
c.on('error', common.mustCall(function(e) {
1111
assert.equal(e.code, 'ENOENT');

test/parallel/test-net-better-error-messages-port-hostname.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var assert = require('assert');
55

66
var c = net.createConnection(common.PORT, '...');
77

8-
c.on('connect', assert.fail);
8+
c.on('connect', common.fail);
99

1010
c.on('error', common.mustCall(function(e) {
1111
assert.equal(e.code, 'ENOTFOUND');

test/parallel/test-net-better-error-messages-port.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var assert = require('assert');
55

66
var c = net.createConnection(common.PORT);
77

8-
c.on('connect', assert.fail);
8+
c.on('connect', common.fail);
99

1010
c.on('error', common.mustCall(function(e) {
1111
assert.equal(e.code, 'ECONNREFUSED');

test/parallel/test-net-dns-lookup-skip.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function check(addressType) {
1111

1212
var address = addressType === 4 ? '127.0.0.1' : '::1';
1313
server.listen(common.PORT, address, function() {
14-
net.connect(common.PORT, address).on('lookup', assert.fail);
14+
net.connect(common.PORT, address).on('lookup', common.fail);
1515
});
1616
}
1717

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ process.on('exit', function() {
1010
});
1111

1212
// this should fail with an async EINVAL error, not throw an exception
13-
net.createServer(assert.fail).listen({fd:0}).on('error', function(e) {
13+
net.createServer(common.fail).listen({fd:0}).on('error', function(e) {
1414
switch (e.code) {
1515
case 'EINVAL':
1616
case 'ENOTSOCK':

0 commit comments

Comments
 (0)