Skip to content

Commit 3995a07

Browse files
committed
fixup! test: ensure assertions are reached on more tests
1 parent 4bad62e commit 3995a07

File tree

45 files changed

+173
-163
lines changed

Some content is hidden

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

45 files changed

+173
-163
lines changed

test/parallel/test-child-process-bad-stdio.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,31 @@ function createChild(options, callback) {
3636
}
3737

3838
test('normal execution of a child process is handled', (_, done) => {
39-
createChild({}, (err, stdout, stderr) => {
39+
createChild({}, common.mustCall((err, stdout, stderr) => {
4040
assert.strictEqual(err, null);
4141
assert.strictEqual(stdout, '');
4242
assert.strictEqual(stderr, '');
4343
done();
44-
});
44+
}));
4545
});
4646

4747
test('execution with an error event is handled', (_, done) => {
4848
const error = new Error('foo');
49-
const child = createChild({}, (err, stdout, stderr) => {
49+
const child = createChild({}, common.mustCall((err, stdout, stderr) => {
5050
assert.strictEqual(err, error);
5151
assert.strictEqual(stdout, '');
5252
assert.strictEqual(stderr, '');
5353
done();
54-
});
54+
}));
5555

5656
child.emit('error', error);
5757
});
5858

5959
test('execution with a killed process is handled', (_, done) => {
60-
createChild({ timeout: 1 }, (err, stdout, stderr) => {
60+
createChild({ timeout: 1 }, common.mustCall((err, stdout, stderr) => {
6161
assert.strictEqual(err.killed, true);
6262
assert.strictEqual(stdout, '');
6363
assert.strictEqual(stderr, '');
6464
done();
65-
});
65+
}));
6666
});

test/parallel/test-child-process-can-write-to-stdout.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Tests that a spawned child process can write to stdout without throwing.
33
// See https://github.com/nodejs/node-v0.x-archive/issues/1899.
44

5-
require('../common');
5+
const common = require('../common');
66
const fixtures = require('../common/fixtures');
77
const assert = require('assert');
88
const spawn = require('child_process').spawn;
@@ -16,7 +16,7 @@ child.stdout.on('data', function(data) {
1616
output += data;
1717
});
1818

19-
child.on('exit', function(code, signal) {
19+
child.on('exit', common.mustCall((code) => {
2020
assert.strictEqual(code, 0);
2121
assert.strictEqual(output, 'hello, world!\n');
22-
});
22+
}));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const { spawn } = require('child_process');
3232
// - whether the child pid is undefined or number,
3333
// - whether the exit code equals expectCode,
3434
// - optionally whether the trimmed stdout result matches expectData
35-
function testCwd(options, expectPidType, expectCode = 0, expectData) {
35+
function testCwd(options, expectPidType, expectCode = 0, expectData, shouldCallExit = true) {
3636
const child = spawn(...common.pwdCommand, options);
3737

3838
assert.strictEqual(typeof child.pid, expectPidType);
@@ -47,9 +47,9 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
4747

4848
// Can't assert callback, as stayed in to API:
4949
// _The 'exit' event may or may not fire after an error has occurred._
50-
child.on('exit', function(code, signal) {
50+
child.on('exit', shouldCallExit ? common.mustCall((code) => {
5151
assert.strictEqual(code, expectCode);
52-
});
52+
}) : common.mustNotCall());
5353

5454
child.on('close', common.mustCall(function() {
5555
if (expectData) {
@@ -68,7 +68,7 @@ function testCwd(options, expectPidType, expectCode = 0, expectData) {
6868

6969
// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
7070
{
71-
testCwd({ cwd: 'does-not-exist' }, 'undefined', -1)
71+
testCwd({ cwd: 'does-not-exist' }, 'undefined', -1, undefined, false)
7272
.on('error', common.mustCall(function(e) {
7373
assert.strictEqual(e.code, 'ENOENT');
7474
}));

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ if (process.argv[2] === 'child') {
3030

3131
// Check that the 'disconnect' event is deferred to the next event loop tick.
3232
const disconnect = process.disconnect;
33-
process.disconnect = function() {
33+
process.disconnect = common.mustCall(function() {
3434
disconnect.apply(this, arguments);
3535
// If the event is emitted synchronously, we're too late by now.
3636
process.once('disconnect', common.mustCall(disconnectIsNotAsync));
3737
// The funky function name makes it show up legible in mustCall errors.
3838
function disconnectIsNotAsync() {}
39-
};
39+
});
4040

4141
const server = net.createServer();
4242

@@ -81,13 +81,13 @@ if (process.argv[2] === 'child') {
8181
child.on('exit', common.mustCall());
8282

8383
// When child is listening
84-
child.on('message', function(obj) {
84+
child.on('message', common.mustCallAtLeast((obj) => {
8585
if (obj && obj.msg === 'ready') {
8686

8787
// Connect to child using TCP to know if disconnect was emitted
8888
const socket = net.connect(obj.port);
8989

90-
socket.on('data', function(data) {
90+
socket.on('data', common.mustCallAtLeast((data) => {
9191
data = data.toString();
9292

9393
// Ready to be disconnected
@@ -103,10 +103,10 @@ if (process.argv[2] === 'child') {
103103

104104
// 'disconnect' is emitted
105105
childFlag = (data === 'true');
106-
});
106+
}));
107107

108108
}
109-
});
109+
}));
110110

111111
process.on('exit', function() {
112112
assert.strictEqual(childFlag, false);

test/parallel/test-child-process-exec-encoding.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ if (process.argv[2] === 'child') {
2222
}
2323

2424
// Test default encoding, which should be utf8.
25-
run({}, (stdout, stderr) => {
25+
run({}, common.mustCall((stdout, stderr) => {
2626
assert.strictEqual(typeof stdout, 'string');
2727
assert.strictEqual(typeof stderr, 'string');
2828
assert.strictEqual(stdout, expectedStdout);
2929
assert.strictEqual(stderr, expectedStderr);
30-
});
30+
}));
3131

3232
// Test explicit utf8 encoding.
33-
run({ encoding: 'utf8' }, (stdout, stderr) => {
33+
run({ encoding: 'utf8' }, common.mustCall((stdout, stderr) => {
3434
assert.strictEqual(typeof stdout, 'string');
3535
assert.strictEqual(typeof stderr, 'string');
3636
assert.strictEqual(stdout, expectedStdout);
3737
assert.strictEqual(stderr, expectedStderr);
38-
});
38+
}));
3939

4040
// Test cases that result in buffer encodings.
4141
[undefined, null, 'buffer', 'invalid'].forEach((encoding) => {
42-
run({ encoding }, (stdout, stderr) => {
42+
run({ encoding }, common.mustCall((stdout, stderr) => {
4343
assert(stdout instanceof Buffer);
4444
assert(stdout instanceof Buffer);
4545
assert.strictEqual(stdout.toString(), expectedStdout);
4646
assert.strictEqual(stderr.toString(), expectedStderr);
47-
});
47+
}));
4848
});
4949
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ common.expectWarning(
6565
const ac = new AbortController();
6666
const { signal } = ac;
6767

68-
const test = () => {
68+
const test = common.mustCall(() => {
6969
const check = common.mustCall((err) => {
7070
assert.strictEqual(err.code, 'ABORT_ERR');
7171
assert.strictEqual(err.name, 'AbortError');
7272
assert.strictEqual(err.signal, undefined);
7373
});
7474
execFile(process.execPath, [echoFixture, 0], { signal }, check);
75-
};
75+
});
7676

7777
// Verify that it still works the same way now that the signal is aborted.
7878
test();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ p.on('close', common.mustCall((code, signal) => {
1717

1818
p.stdout.read();
1919

20-
const spawnWithReadable = () => {
20+
const spawnWithReadable = common.mustCall(() => {
2121
const buffer = [];
2222
const p = cp.spawn('echo', ['123'], opts);
2323
p.on('close', common.mustCall((code, signal) => {
@@ -30,4 +30,4 @@ const spawnWithReadable = () => {
3030
while ((buf = p.stdout.read()) !== null)
3131
buffer.push(buf);
3232
});
33-
};
33+
});

test/parallel/test-child-process-fork-closed-channel-segfault.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const server = net
2828
s.destroy();
2929
}, 100);
3030
})
31-
.listen(0, function() {
31+
.listen(0, common.mustCall(() => {
3232
const worker = cluster.fork();
3333

3434
worker.on('error', function(err) {
@@ -70,9 +70,8 @@ const server = net
7070
})
7171
);
7272

73-
worker.on('online', function() {
74-
send(function(err) {
75-
assert.ifError(err);
73+
worker.on('online', common.mustCall(() => {
74+
send(common.mustSucceed(() => {
7675
send(function(err) {
7776
// Ignore errors when sending the second handle because the worker
7877
// may already have exited.
@@ -83,6 +82,6 @@ const server = net
8382
throw err;
8483
}
8584
});
86-
});
87-
});
88-
});
85+
}));
86+
}));
87+
}));

test/parallel/test-child-process-fork-dgram.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ if (process.argv[2] === 'child') {
9292
msg.length,
9393
serverPort,
9494
'127.0.0.1',
95-
(err) => {
96-
assert.ifError(err);
97-
}
95+
common.mustSucceed(),
9896
);
9997
}
10098
}, 1);

test/parallel/test-child-process-fork-net-server.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ if (process.argv[2] === 'child') {
6060
// TODO(@jasnell): The close event is not called consistently
6161
// across platforms. Need to investigate if it can be made
6262
// more consistent.
63-
const onClose = (msg) => {
63+
const onClose = common.mustCallAtLeast((msg) => {
6464
if (msg.what !== 'close') return;
6565
process.removeListener('message', onClose);
6666

6767
serverScope.on('close', common.mustCall(() => {
6868
process.send({ what: 'close' });
6969
}));
7070
serverScope.close();
71-
};
71+
});
7272

7373
process.on('message', onClose);
7474

@@ -86,13 +86,13 @@ if (process.argv[2] === 'child') {
8686
function testServer(callback) {
8787

8888
// Destroy server execute callback when done.
89-
const countdown = new Countdown(2, () => {
89+
const countdown = new Countdown(2, common.mustCall(() => {
9090
server.on('close', common.mustCall(() => {
9191
debug('PARENT: server closed');
9292
child.send({ what: 'close' });
9393
}));
9494
server.close();
95-
});
95+
}));
9696

9797
// We expect 4 connections and close events.
9898
const connections = new Countdown(4, () => countdown.dec());
@@ -122,7 +122,7 @@ if (process.argv[2] === 'child') {
122122
// event is emitted appears to be variable across platforms.
123123
// Need to investigate why and whether it can be made
124124
// more consistent.
125-
const messageHandlers = (msg) => {
125+
const messageHandlers = common.mustCallAtLeast((msg) => {
126126
if (msg.what === 'listening') {
127127
// Make connections.
128128
let socket;
@@ -143,7 +143,7 @@ if (process.argv[2] === 'child') {
143143
child.removeListener('message', messageHandlers);
144144
callback();
145145
}
146-
};
146+
});
147147

148148
child.on('message', messageHandlers);
149149
}

0 commit comments

Comments
 (0)