Skip to content

Commit 8cae858

Browse files
committed
test: ensure assertions are reached on more tests
PR-URL: #60498 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 759d693 commit 8cae858

File tree

63 files changed

+454
-449
lines changed

Some content is hidden

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

63 files changed

+454
-449
lines changed

test/eslint.config_partial.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ export default [
193193
'wpt',
194194
].join(',')}}/**/*.{js,mjs,cjs}`,
195195
`test/parallel/test-{${
196+
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'a*,b*,…'
197+
Array.from({ length: 3 }, (_, i) => String.fromCharCode(0x61 + i, 42)).join(',')
198+
},${
196199
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'z*,y*,…'
197200
Array.from({ length: 2 }, (_, i) => String.fromCharCode(0x61 + 25 - i, 42)).join(',')
198-
},${
199-
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'a*,b*,…'
200-
Array.from({ length: 2 }, (_, i) => String.fromCharCode(0x61 + i, 42)).join(',')
201201
}}.{js,mjs,cjs}`,
202202
],
203203
rules: {

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-abort-signal.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { mustCall, mustNotCall } = require('../common');
4-
const { strictEqual } = require('assert');
4+
const assert = require('assert');
55
const fixtures = require('../common/fixtures');
66
const { fork } = require('child_process');
77

@@ -13,11 +13,11 @@ const { fork } = require('child_process');
1313
signal
1414
});
1515
cp.on('exit', mustCall((code, killSignal) => {
16-
strictEqual(code, null);
17-
strictEqual(killSignal, 'SIGTERM');
16+
assert.strictEqual(code, null);
17+
assert.strictEqual(killSignal, 'SIGTERM');
1818
}));
1919
cp.on('error', mustCall((err) => {
20-
strictEqual(err.name, 'AbortError');
20+
assert.strictEqual(err.name, 'AbortError');
2121
}));
2222
process.nextTick(() => ac.abort());
2323
}
@@ -30,13 +30,13 @@ const { fork } = require('child_process');
3030
signal
3131
});
3232
cp.on('exit', mustCall((code, killSignal) => {
33-
strictEqual(code, null);
34-
strictEqual(killSignal, 'SIGTERM');
33+
assert.strictEqual(code, null);
34+
assert.strictEqual(killSignal, 'SIGTERM');
3535
}));
3636
cp.on('error', mustCall((err) => {
37-
strictEqual(err.name, 'AbortError');
38-
strictEqual(err.cause.name, 'Error');
39-
strictEqual(err.cause.message, 'boom');
37+
assert.strictEqual(err.name, 'AbortError');
38+
assert.strictEqual(err.cause.name, 'Error');
39+
assert.strictEqual(err.cause.message, 'boom');
4040
}));
4141
process.nextTick(() => ac.abort(new Error('boom')));
4242
}
@@ -48,11 +48,11 @@ const { fork } = require('child_process');
4848
signal
4949
});
5050
cp.on('exit', mustCall((code, killSignal) => {
51-
strictEqual(code, null);
52-
strictEqual(killSignal, 'SIGTERM');
51+
assert.strictEqual(code, null);
52+
assert.strictEqual(killSignal, 'SIGTERM');
5353
}));
5454
cp.on('error', mustCall((err) => {
55-
strictEqual(err.name, 'AbortError');
55+
assert.strictEqual(err.name, 'AbortError');
5656
}));
5757
}
5858

@@ -63,13 +63,13 @@ const { fork } = require('child_process');
6363
signal
6464
});
6565
cp.on('exit', mustCall((code, killSignal) => {
66-
strictEqual(code, null);
67-
strictEqual(killSignal, 'SIGTERM');
66+
assert.strictEqual(code, null);
67+
assert.strictEqual(killSignal, 'SIGTERM');
6868
}));
6969
cp.on('error', mustCall((err) => {
70-
strictEqual(err.name, 'AbortError');
71-
strictEqual(err.cause.name, 'Error');
72-
strictEqual(err.cause.message, 'boom');
70+
assert.strictEqual(err.name, 'AbortError');
71+
assert.strictEqual(err.cause.name, 'Error');
72+
assert.strictEqual(err.cause.message, 'boom');
7373
}));
7474
}
7575

@@ -81,11 +81,11 @@ const { fork } = require('child_process');
8181
killSignal: 'SIGKILL',
8282
});
8383
cp.on('exit', mustCall((code, killSignal) => {
84-
strictEqual(code, null);
85-
strictEqual(killSignal, 'SIGKILL');
84+
assert.strictEqual(code, null);
85+
assert.strictEqual(killSignal, 'SIGKILL');
8686
}));
8787
cp.on('error', mustCall((err) => {
88-
strictEqual(err.name, 'AbortError');
88+
assert.strictEqual(err.name, 'AbortError');
8989
}));
9090
}
9191

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

0 commit comments

Comments
 (0)