Skip to content

Commit 4a85f70

Browse files
joyeecheungUlisesGascon
authored andcommitted
test: add spawnSyncAndExit() and spawnSyncAndExitWithoutError()
Replaces expectSyncExit() and expectSyncExitWithoutError(). Since we usually just check the child process right after its spawned, these shorthands also takes care of the spawning. This makes the tests more concise. PR-URL: #49200 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 9610008 commit 4a85f70

File tree

5 files changed

+66
-73
lines changed

5 files changed

+66
-73
lines changed

test/common/README.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,16 @@ The `benchmark` module is used by tests to run benchmarks.
4040

4141
The `child_process` module is used by tests that launch child processes.
4242

43-
### `expectSyncExit(child, options)`
43+
### `spawnSyncAndExit(command[, args][, spawnOptions], expectations)`
4444

45-
Checks if a _synchronous_ child process runs in the way expected. If it does
46-
not, print the stdout and stderr output from the child process and additional
47-
information about it to the stderr of the current process before throwing
48-
and error. This helps gathering more information about test failures
49-
coming from child processes.
45+
Spawns a child process synchronously using [`child_process.spawnSync()`][] and
46+
check if it runs in the way expected. If it does not, print the stdout and
47+
stderr output from the child process and additional information about it to
48+
the stderr of the current process before throwing and error. This helps
49+
gathering more information about test failures coming from child processes.
5050

51-
* `child` [\<ChildProcess>][<ChildProcess>]: a `ChildProcess` instance
52-
returned by `child_process.spawnSync()`.
53-
* `options` [\<Object>][<Object>]
51+
* `command`, `args`, `spawnOptions` See [`child_process.spawnSync()`][]
52+
* `expectations` [\<Object>][<Object>]
5453
* `status` [\<number>][<number>] Expected `child.status`
5554
* `signal` [\<string>][<string>] | `null` Expected `child.signal`
5655
* `stderr` [\<string>][<string>] | [\<RegExp>][<RegExp>] |
@@ -65,8 +64,13 @@ coming from child processes.
6564
* `trim` [\<boolean>][<boolean>] Optional. Whether this method should trim
6665
out the whitespace characters when checking `stderr` and `stdout` outputs.
6766
Defaults to `false`.
67+
* return [\<Object>][<Object>]
68+
* `child` [\<ChildProcess>][<ChildProcess>] The child process returned by
69+
[`child_process.spawnSync()`][].
70+
* `stderr` [\<string>][<string>] The output from the child process to stderr.
71+
* `stdout` [\<string>][<string>] The output from the child process to stdout.
6872

69-
### `expectSyncExitWithoutError(child[, options])`
73+
### `spawnSyncAndExitWithoutError(command[, args][, spawnOptions], expectations)`
7074

7175
Similar to `expectSyncExit()` with the `status` expected to be 0 and
7276
`signal` expected to be `null`. Any other optional options are passed
@@ -1160,6 +1164,7 @@ See [the WPT tests README][] for details.
11601164
[<number>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
11611165
[<string>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type
11621166
[Web Platform Tests]: https://github.com/web-platform-tests/wpt
1167+
[`child_process.spawnSync()`]: ../../doc/api/child_process.md#child_processspawnsynccommand-args-options
11631168
[`hijackstdio.hijackStdErr()`]: #hijackstderrlistener
11641169
[`hijackstdio.hijackStdOut()`]: #hijackstdoutlistener
11651170
[internationalization]: ../../doc/api/intl.md

test/common/child_process.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const assert = require('assert');
4+
const { spawnSync, execFileSync } = require('child_process');
45
const common = require('./');
56
const util = require('util');
67

@@ -14,14 +15,13 @@ function cleanupStaleProcess(filename) {
1415
process.once('beforeExit', () => {
1516
const basename = filename.replace(/.*[/\\]/g, '');
1617
try {
17-
require('child_process')
18-
.execFileSync(`${process.env.SystemRoot}\\System32\\wbem\\WMIC.exe`, [
19-
'process',
20-
'where',
21-
`commandline like '%${basename}%child'`,
22-
'delete',
23-
'/nointeractive',
24-
]);
18+
execFileSync(`${process.env.SystemRoot}\\System32\\wbem\\WMIC.exe`, [
19+
'process',
20+
'where',
21+
`commandline like '%${basename}%child'`,
22+
'delete',
23+
'/nointeractive',
24+
]);
2525
} catch {
2626
// Ignore failures, there might not be any stale process to clean up.
2727
}
@@ -111,11 +111,21 @@ function expectSyncExit(child, {
111111
return { child, stderr: stderrStr, stdout: stdoutStr };
112112
}
113113

114-
function expectSyncExitWithoutError(child, options) {
114+
function spawnSyncAndExit(...args) {
115+
const spawnArgs = args.slice(0, args.length - 1);
116+
const expectations = args[args.length - 1];
117+
const child = spawnSync(...spawnArgs);
118+
return expectSyncExit(child, expectations);
119+
}
120+
121+
function spawnSyncAndExitWithoutError(...args) {
122+
const spawnArgs = args.slice(0, args.length);
123+
const expectations = args[args.length - 1];
124+
const child = spawnSync(...spawnArgs);
115125
return expectSyncExit(child, {
116126
status: 0,
117127
signal: null,
118-
...options,
128+
...expectations,
119129
});
120130
}
121131

@@ -124,6 +134,6 @@ module.exports = {
124134
logAfterTime,
125135
kExpiringChildRunTime,
126136
kExpiringParentTimer,
127-
expectSyncExit,
128-
expectSyncExitWithoutError,
137+
spawnSyncAndExit,
138+
spawnSyncAndExitWithoutError,
129139
};

test/parallel/test-snapshot-api.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
require('../common');
66
const assert = require('assert');
7-
const { spawnSync } = require('child_process');
87
const tmpdir = require('../common/tmpdir');
98
const fixtures = require('../common/fixtures');
10-
const { expectSyncExitWithoutError } = require('../common/child_process');
9+
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
1110
const fs = require('fs');
1211

1312
const v8 = require('v8');
@@ -29,22 +28,20 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
2928
fs.writeFileSync(tmpdir.resolve(book), content, 'utf8');
3029
}
3130
fs.copyFileSync(entry, tmpdir.resolve('entry.js'));
32-
const child = spawnSync(process.execPath, [
31+
spawnSyncAndExitWithoutError(process.execPath, [
3332
'--snapshot-blob',
3433
blobPath,
3534
'--build-snapshot',
3635
'entry.js',
3736
], {
3837
cwd: tmpdir.path
3938
});
40-
41-
expectSyncExitWithoutError(child);
4239
const stats = fs.statSync(tmpdir.resolve('snapshot.blob'));
4340
assert(stats.isFile());
4441
}
4542

4643
{
47-
const child = spawnSync(process.execPath, [
44+
spawnSyncAndExitWithoutError(process.execPath, [
4845
'--snapshot-blob',
4946
blobPath,
5047
'book1',
@@ -54,9 +51,7 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
5451
...process.env,
5552
BOOK_LANG: 'en_US',
5653
}
57-
});
58-
59-
expectSyncExitWithoutError(child, {
54+
}, {
6055
stderr: 'Reading book1.en_US.txt',
6156
stdout: 'This is book1.en_US.txt',
6257
trim: true

test/parallel/test-snapshot-basic.js

+13-19
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
require('../common');
77
const assert = require('assert');
8-
const { spawnSync } = require('child_process');
98
const tmpdir = require('../common/tmpdir');
109
const fixtures = require('../common/fixtures');
11-
const { expectSyncExitWithoutError, expectSyncExit } = require('../common/child_process');
10+
const {
11+
spawnSyncAndExitWithoutError,
12+
spawnSyncAndExit,
13+
} = require('../common/child_process');
1214
const fs = require('fs');
1315

1416
tmpdir.refresh();
@@ -18,14 +20,12 @@ if (!process.config.variables.node_use_node_snapshot) {
1820
// Check that Node.js built without an embedded snapshot
1921
// exits with 9 when node:embedded_snapshot_main is specified
2022
// as snapshot entry point.
21-
const child = spawnSync(process.execPath, [
23+
spawnSyncAndExit(process.execPath, [
2224
'--build-snapshot',
2325
snapshotScript,
2426
], {
2527
cwd: tmpdir.path
26-
});
27-
28-
expectSyncExit(child, {
28+
}, {
2929
status: 9,
3030
signal: null,
3131
stderr: /Node\.js was built without embedded snapshot/
@@ -37,13 +37,12 @@ if (!process.config.variables.node_use_node_snapshot) {
3737
// By default, the snapshot blob path is cwd/snapshot.blob.
3838
{
3939
// Create the snapshot.
40-
const child = spawnSync(process.execPath, [
40+
spawnSyncAndExitWithoutError(process.execPath, [
4141
'--build-snapshot',
4242
snapshotScript,
4343
], {
4444
cwd: tmpdir.path
4545
});
46-
expectSyncExitWithoutError(child);
4746
const stats = fs.statSync(tmpdir.resolve('snapshot.blob'));
4847
assert(stats.isFile());
4948
}
@@ -52,46 +51,41 @@ tmpdir.refresh();
5251
const blobPath = tmpdir.resolve('my-snapshot.blob');
5352
{
5453
// Create the snapshot.
55-
const child = spawnSync(process.execPath, [
54+
spawnSyncAndExitWithoutError(process.execPath, [
5655
'--snapshot-blob',
5756
blobPath,
5857
'--build-snapshot',
5958
snapshotScript,
6059
], {
6160
cwd: tmpdir.path
6261
});
63-
expectSyncExitWithoutError(child);
6462
const stats = fs.statSync(blobPath);
6563
assert(stats.isFile());
6664
}
6765

6866
{
6967
// Check --help.
70-
const child = spawnSync(process.execPath, [
68+
spawnSyncAndExitWithoutError(process.execPath, [
7169
'--snapshot-blob',
7270
blobPath,
7371
'--help',
7472
], {
7573
cwd: tmpdir.path
74+
}, {
75+
stdout: /--help/
7676
});
77-
expectSyncExitWithoutError(child);
78-
79-
assert(child.stdout.toString().includes('--help'));
8077
}
8178

8279
{
8380
// Check -c.
84-
const child = spawnSync(process.execPath, [
81+
spawnSyncAndExitWithoutError(process.execPath, [
8582
'--snapshot-blob',
8683
blobPath,
8784
'-c',
8885
fixtures.path('snapshot', 'marked.js'),
8986
], {
9087
cwd: tmpdir.path
91-
});
92-
93-
// Check that it is a noop.
94-
expectSyncExitWithoutError(child, {
88+
}, {
9589
stderr: '',
9690
stdout: '',
9791
trim: true

test/parallel/test-snapshot-warning.js

+12-23
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
require('../common');
88

99
const assert = require('assert');
10-
const { spawnSync } = require('child_process');
1110
const tmpdir = require('../common/tmpdir');
1211
const fixtures = require('../common/fixtures');
13-
const { expectSyncExitWithoutError } = require('../common/child_process');
12+
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
1413
const fs = require('fs');
1514

1615
const warningScript = fixtures.path('snapshot', 'warning.js');
@@ -20,48 +19,44 @@ const empty = fixtures.path('empty.js');
2019
tmpdir.refresh();
2120
{
2221
console.log('\n# Check snapshot scripts that do not emit warnings.');
23-
let child = spawnSync(process.execPath, [
22+
spawnSyncAndExitWithoutError(process.execPath, [
2423
'--snapshot-blob',
2524
blobPath,
2625
'--build-snapshot',
2726
empty,
2827
], {
2928
cwd: tmpdir.path
3029
});
31-
expectSyncExitWithoutError(child);
3230
const stats = fs.statSync(blobPath);
3331
assert(stats.isFile());
3432

35-
child = spawnSync(process.execPath, [
33+
spawnSyncAndExitWithoutError(process.execPath, [
3634
'--snapshot-blob',
3735
blobPath,
3836
warningScript,
3937
], {
4038
cwd: tmpdir.path
41-
});
42-
expectSyncExitWithoutError(child, {
39+
}, {
4340
stderr(output) {
4441
const match = output.match(/Warning: test warning/g);
4542
assert.strictEqual(match.length, 1);
4643
return true;
4744
}
4845
});
49-
5046
}
5147

5248
tmpdir.refresh();
5349
{
5450
console.log('\n# Check snapshot scripts that emit ' +
5551
'warnings and --trace-warnings hint.');
56-
let child = spawnSync(process.execPath, [
52+
spawnSyncAndExitWithoutError(process.execPath, [
5753
'--snapshot-blob',
5854
blobPath,
5955
'--build-snapshot',
6056
warningScript,
6157
], {
6258
cwd: tmpdir.path
63-
});
64-
expectSyncExitWithoutError(child, {
59+
}, {
6560
stderr(output) {
6661
let match = output.match(/Warning: test warning/g);
6762
assert.strictEqual(match.length, 1);
@@ -73,15 +68,13 @@ tmpdir.refresh();
7368
const stats = fs.statSync(blobPath);
7469
assert(stats.isFile());
7570

76-
child = spawnSync(process.execPath, [
71+
spawnSyncAndExitWithoutError(process.execPath, [
7772
'--snapshot-blob',
7873
blobPath,
7974
warningScript,
8075
], {
8176
cwd: tmpdir.path
82-
});
83-
84-
expectSyncExitWithoutError(child, {
77+
}, {
8578
stderr(output) {
8679
// Warnings should not be handled more than once.
8780
let match = output.match(/Warning: test warning/g);
@@ -99,7 +92,7 @@ tmpdir.refresh();
9992
const warningFile1 = tmpdir.resolve('warnings.txt');
10093
const warningFile2 = tmpdir.resolve('warnings2.txt');
10194

102-
let child = spawnSync(process.execPath, [
95+
spawnSyncAndExitWithoutError(process.execPath, [
10396
'--snapshot-blob',
10497
blobPath,
10598
'--redirect-warnings',
@@ -108,9 +101,7 @@ tmpdir.refresh();
108101
warningScript,
109102
], {
110103
cwd: tmpdir.path
111-
});
112-
113-
expectSyncExitWithoutError(child, {
104+
}, {
114105
stderr(output) {
115106
assert.doesNotMatch(output, /Warning: test warning/);
116107
}
@@ -129,17 +120,15 @@ tmpdir.refresh();
129120
maxRetries: 3, recursive: false, force: true
130121
});
131122

132-
child = spawnSync(process.execPath, [
123+
spawnSyncAndExitWithoutError(process.execPath, [
133124
'--snapshot-blob',
134125
blobPath,
135126
'--redirect-warnings',
136127
warningFile2,
137128
warningScript,
138129
], {
139130
cwd: tmpdir.path
140-
});
141-
142-
expectSyncExitWithoutError(child, {
131+
}, {
143132
stderr(output) {
144133
assert.doesNotMatch(output, /Warning: test warning/);
145134
return true;

0 commit comments

Comments
 (0)