Skip to content

Commit c714cda

Browse files
authored
test: add spawnSyncAndAssert util
PR-URL: #52132 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent f69946b commit c714cda

21 files changed

+77
-72
lines changed

test/common/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ gathering more information about test failures coming from child processes.
7070
* `stderr` [\<string>][<string>] The output from the child process to stderr.
7171
* `stdout` [\<string>][<string>] The output from the child process to stdout.
7272

73-
### `spawnSyncAndExitWithoutError(command[, args][, spawnOptions], expectations)`
73+
### `spawnSyncAndExitWithoutError(command[, args][, spawnOptions])`
7474

7575
Similar to `expectSyncExit()` with the `status` expected to be 0 and
76-
`signal` expected to be `null`. Any other optional options are passed
77-
into `expectSyncExit()`.
76+
`signal` expected to be `null`.
77+
78+
### `spawnSyncAndAssert(command[, args][, spawnOptions], expectations)`
79+
80+
Similar to `spawnSyncAndExitWithoutError()`, but with an additional
81+
`expectations` parameter.
7882

7983
## Common Module API
8084

test/common/child_process.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,15 @@ function spawnSyncAndExit(...args) {
119119
}
120120

121121
function spawnSyncAndExitWithoutError(...args) {
122-
const spawnArgs = args.slice(0, args.length);
123-
const expectations = args[args.length - 1];
124-
const child = spawnSync(...spawnArgs);
122+
return expectSyncExit(spawnSync(...args), {
123+
status: 0,
124+
signal: null,
125+
});
126+
}
127+
128+
function spawnSyncAndAssert(...args) {
129+
const expectations = args.pop();
130+
const child = spawnSync(...args);
125131
return expectSyncExit(child, {
126132
status: 0,
127133
signal: null,
@@ -134,6 +140,7 @@ module.exports = {
134140
logAfterTime,
135141
kExpiringChildRunTime,
136142
kExpiringParentTimer,
143+
spawnSyncAndAssert,
137144
spawnSyncAndExit,
138145
spawnSyncAndExitWithoutError,
139146
};

test/common/sea.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow
9292

9393
if (process.platform === 'darwin') {
9494
try {
95-
spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ], {});
96-
spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ], {});
95+
spawnSyncAndExitWithoutError('codesign', [ '--sign', '-', targetExecutable ]);
96+
spawnSyncAndExitWithoutError('codesign', [ '--verify', targetExecutable ]);
9797
} catch (e) {
9898
const message = `Cannot sign ${targetExecutable}: ${inspect(e)}`;
9999
if (verifyWorkflow) {
@@ -104,7 +104,7 @@ function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow
104104
console.log(`Signed ${targetExecutable}`);
105105
} else if (process.platform === 'win32') {
106106
try {
107-
spawnSyncAndExitWithoutError('where', [ 'signtool' ], {});
107+
spawnSyncAndExitWithoutError('where', [ 'signtool' ]);
108108
} catch (e) {
109109
const message = `Cannot find signtool: ${inspect(e)}`;
110110
if (verifyWorkflow) {
@@ -114,8 +114,8 @@ function generateSEA(targetExecutable, sourceExecutable, seaBlob, verifyWorkflow
114114
}
115115
let stderr;
116116
try {
117-
({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ], {}));
118-
spawnSyncAndExitWithoutError('signtool', 'verify', '/pa', 'SHA256', targetExecutable, {});
117+
({ stderr } = spawnSyncAndExitWithoutError('signtool', [ 'sign', '/fd', 'SHA256', targetExecutable ]));
118+
spawnSyncAndExitWithoutError('signtool', ['verify', '/pa', 'SHA256', targetExecutable]);
119119
} catch (e) {
120120
const message = `Cannot sign ${targetExecutable}: ${inspect(e)}\n${stderr}`;
121121
if (verifyWorkflow) {

test/common/wasi.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Test version set to preview1
22
'use strict';
33

4-
const { spawnSyncAndExitWithoutError } = require('./child_process');
4+
const { spawnSyncAndAssert } = require('./child_process');
55
const fixtures = require('./fixtures');
66
const childPath = fixtures.path('wasi-preview-1.js');
77

@@ -15,7 +15,7 @@ function testWasiPreview1(args, spawnArgs = {}, expectations = {}) {
1515
spawnArgs.env = newEnv;
1616

1717
console.log('Testing with --turbo-fast-api-calls:', ...args);
18-
spawnSyncAndExitWithoutError(
18+
spawnSyncAndAssert(
1919
process.execPath, [
2020
'--turbo-fast-api-calls',
2121
childPath,
@@ -26,7 +26,7 @@ function testWasiPreview1(args, spawnArgs = {}, expectations = {}) {
2626
);
2727

2828
console.log('Testing with --no-turbo-fast-api-calls:', ...args);
29-
spawnSyncAndExitWithoutError(
29+
spawnSyncAndAssert(
3030
process.execPath,
3131
[
3232
'--no-turbo-fast-api-calls',

test/embedding/test-embedding.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fixtures = require('../common/fixtures');
44
const tmpdir = require('../common/tmpdir');
55
const assert = require('assert');
66
const {
7+
spawnSyncAndAssert,
78
spawnSyncAndExit,
89
spawnSyncAndExitWithoutError,
910
} = require('../common/child_process');
@@ -23,15 +24,15 @@ function resolveBuiltBinary(binary) {
2324

2425
const binary = resolveBuiltBinary('embedtest');
2526

26-
spawnSyncAndExitWithoutError(
27+
spawnSyncAndAssert(
2728
binary,
2829
['console.log(42)'],
2930
{
3031
trim: true,
3132
stdout: '42',
3233
});
3334

34-
spawnSyncAndExitWithoutError(
35+
spawnSyncAndAssert(
3536
binary,
3637
['console.log(embedVars.nön_ascıı)'],
3738
{
@@ -111,9 +112,8 @@ for (const extraSnapshotArgs of [
111112
spawnSyncAndExitWithoutError(
112113
binary,
113114
[ '--', ...buildSnapshotArgs ],
114-
{ cwd: tmpdir.path },
115-
{});
116-
spawnSyncAndExitWithoutError(
115+
{ cwd: tmpdir.path });
116+
spawnSyncAndAssert(
117117
binary,
118118
[ '--', ...runSnapshotArgs ],
119119
{ cwd: tmpdir.path },
@@ -145,11 +145,9 @@ for (const extraSnapshotArgs of [
145145
spawnSyncAndExitWithoutError(
146146
binary,
147147
[ '--', ...buildSnapshotArgs ],
148-
{ cwd: tmpdir.path },
149-
{});
148+
{ cwd: tmpdir.path });
150149
spawnSyncAndExitWithoutError(
151150
binary,
152151
[ '--', ...runEmbeddedArgs ],
153-
{ cwd: tmpdir.path },
154-
{});
152+
{ cwd: tmpdir.path });
155153
}

test/parallel/test-error-prepare-stack-trace.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ if (process.argv[2] !== 'child') {
2626
// enabled.
2727
spawnSyncAndExitWithoutError(
2828
process.execPath,
29-
['--enable-source-maps', __filename, 'child'],
30-
{});
29+
['--enable-source-maps', __filename, 'child']);
3130
}

test/parallel/test-snapshot-api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require('../common');
66
const assert = require('assert');
77
const tmpdir = require('../common/tmpdir');
88
const fixtures = require('../common/fixtures');
9-
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
9+
const { spawnSyncAndAssert, spawnSyncAndExitWithoutError } = require('../common/child_process');
1010
const fs = require('fs');
1111

1212
const v8 = require('v8');
@@ -41,7 +41,7 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
4141
}
4242

4343
{
44-
spawnSyncAndExitWithoutError(process.execPath, [
44+
spawnSyncAndAssert(process.execPath, [
4545
'--snapshot-blob',
4646
blobPath,
4747
'book1',

test/parallel/test-snapshot-basic.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const assert = require('assert');
88
const tmpdir = require('../common/tmpdir');
99
const fixtures = require('../common/fixtures');
1010
const {
11-
spawnSyncAndExitWithoutError,
11+
spawnSyncAndAssert,
1212
spawnSyncAndExit,
13+
spawnSyncAndExitWithoutError,
1314
} = require('../common/child_process');
1415
const fs = require('fs');
1516

@@ -65,7 +66,7 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');
6566

6667
{
6768
// Check --help.
68-
spawnSyncAndExitWithoutError(process.execPath, [
69+
spawnSyncAndAssert(process.execPath, [
6970
'--snapshot-blob',
7071
blobPath,
7172
'--help',
@@ -78,7 +79,7 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');
7879

7980
{
8081
// Check -c.
81-
spawnSyncAndExitWithoutError(process.execPath, [
82+
spawnSyncAndAssert(process.execPath, [
8283
'--snapshot-blob',
8384
blobPath,
8485
'-c',

test/parallel/test-snapshot-child-process-sync.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// restoring state from a snapshot
55

66
require('../common');
7-
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
7+
const { spawnSyncAndAssert } = require('../common/child_process');
88
const tmpdir = require('../common/tmpdir');
99
const fixtures = require('../common/fixtures');
1010
const assert = require('assert');
@@ -20,7 +20,7 @@ const expected = [
2020

2121
{
2222
// Create the snapshot.
23-
spawnSyncAndExitWithoutError(process.execPath, [
23+
spawnSyncAndAssert(process.execPath, [
2424
'--snapshot-blob',
2525
blobPath,
2626
'--build-snapshot',
@@ -37,7 +37,7 @@ const expected = [
3737
}
3838

3939
{
40-
spawnSyncAndExitWithoutError(process.execPath, [
40+
spawnSyncAndAssert(process.execPath, [
4141
'--snapshot-blob',
4242
blobPath,
4343
file,

test/parallel/test-snapshot-config.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
require('../common');
66
const assert = require('assert');
77
const {
8-
spawnSyncAndExitWithoutError,
8+
spawnSyncAndAssert,
99
spawnSyncAndExit,
10+
spawnSyncAndExitWithoutError,
1011
} = require('../common/child_process');
1112
const tmpdir = require('../common/tmpdir');
1213
const fixtures = require('../common/fixtures');
@@ -84,7 +85,7 @@ let sizeWithCache;
8485
configPath,
8586
], {
8687
cwd: tmpdir.path
87-
}, {});
88+
});
8889
const stats = fs.statSync(blobPath);
8990
assert(stats.isFile());
9091
sizeWithCache = stats.size;
@@ -115,14 +116,14 @@ let sizeWithoutCache;
115116
NODE_DEBUG_NATIVE: 'CODE_CACHE'
116117
},
117118
cwd: tmpdir.path
118-
}, {});
119+
});
119120
const stats = fs.statSync(blobPath);
120121
assert(stats.isFile());
121122
sizeWithoutCache = stats.size;
122123
assert(sizeWithoutCache < sizeWithCache,
123124
`sizeWithoutCache = ${sizeWithoutCache} >= sizeWithCache ${sizeWithCache}`);
124125
// Check the snapshot.
125-
spawnSyncAndExitWithoutError(process.execPath, [
126+
spawnSyncAndAssert(process.execPath, [
126127
'--snapshot-blob',
127128
blobPath,
128129
checkFile,

test/parallel/test-snapshot-cwd.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// restoring state from a snapshot
55

66
require('../common');
7-
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
7+
const { spawnSyncAndAssert } = require('../common/child_process');
88
const tmpdir = require('../common/tmpdir');
99
const fixtures = require('../common/fixtures');
1010
const fs = require('fs');
@@ -18,7 +18,7 @@ fs.mkdirSync(subdir);
1818

1919
{
2020
// Create the snapshot.
21-
spawnSyncAndExitWithoutError(process.execPath, [
21+
spawnSyncAndAssert(process.execPath, [
2222
'--snapshot-blob',
2323
blobPath,
2424
'--build-snapshot',
@@ -32,7 +32,7 @@ fs.mkdirSync(subdir);
3232
}
3333

3434
{
35-
spawnSyncAndExitWithoutError(process.execPath, [
35+
spawnSyncAndAssert(process.execPath, [
3636
'--snapshot-blob',
3737
blobPath,
3838
file,

test/parallel/test-snapshot-warning.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require('../common');
99
const assert = require('assert');
1010
const tmpdir = require('../common/tmpdir');
1111
const fixtures = require('../common/fixtures');
12-
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
12+
const { spawnSyncAndAssert, spawnSyncAndExitWithoutError } = require('../common/child_process');
1313
const fs = require('fs');
1414

1515
const warningScript = fixtures.path('snapshot', 'warning.js');
@@ -30,7 +30,7 @@ tmpdir.refresh();
3030
const stats = fs.statSync(blobPath);
3131
assert(stats.isFile());
3232

33-
spawnSyncAndExitWithoutError(process.execPath, [
33+
spawnSyncAndAssert(process.execPath, [
3434
'--snapshot-blob',
3535
blobPath,
3636
warningScript,
@@ -49,7 +49,7 @@ tmpdir.refresh();
4949
{
5050
console.log('\n# Check snapshot scripts that emit ' +
5151
'warnings and --trace-warnings hint.');
52-
spawnSyncAndExitWithoutError(process.execPath, [
52+
spawnSyncAndAssert(process.execPath, [
5353
'--snapshot-blob',
5454
blobPath,
5555
'--build-snapshot',
@@ -68,7 +68,7 @@ tmpdir.refresh();
6868
const stats = fs.statSync(blobPath);
6969
assert(stats.isFile());
7070

71-
spawnSyncAndExitWithoutError(process.execPath, [
71+
spawnSyncAndAssert(process.execPath, [
7272
'--snapshot-blob',
7373
blobPath,
7474
warningScript,
@@ -92,7 +92,7 @@ tmpdir.refresh();
9292
const warningFile1 = tmpdir.resolve('warnings.txt');
9393
const warningFile2 = tmpdir.resolve('warnings2.txt');
9494

95-
spawnSyncAndExitWithoutError(process.execPath, [
95+
spawnSyncAndAssert(process.execPath, [
9696
'--snapshot-blob',
9797
blobPath,
9898
'--redirect-warnings',
@@ -120,7 +120,7 @@ tmpdir.refresh();
120120
maxRetries: 3, recursive: false, force: true
121121
});
122122

123-
spawnSyncAndExitWithoutError(process.execPath, [
123+
spawnSyncAndAssert(process.execPath, [
124124
'--snapshot-blob',
125125
blobPath,
126126
'--redirect-warnings',

test/sequential/test-single-executable-application-assets-raw.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'se
5151
...process.env,
5252
},
5353
cwd: tmpdir.path
54-
},
55-
{});
54+
});
5655

5756
assert(existsSync(seaPrepBlob));
5857

@@ -67,6 +66,5 @@ const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'se
6766
__TEST_PERSON_JPG: fixtures.path('person.jpg'),
6867
}
6968
},
70-
{ }
7169
);
7270
}

test/sequential/test-single-executable-application-assets.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const tmpdir = require('../common/tmpdir');
1414

1515
const { copyFileSync, writeFileSync, existsSync } = require('fs');
1616
const {
17+
spawnSyncAndAssert,
1718
spawnSyncAndExit,
1819
spawnSyncAndExitWithoutError,
1920
} = require('../common/child_process');
@@ -104,14 +105,13 @@ const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'se
104105
...process.env,
105106
},
106107
cwd: tmpdir.path
107-
},
108-
{});
108+
});
109109

110110
assert(existsSync(seaPrepBlob));
111111

112112
generateSEA(outputFile, process.execPath, seaPrepBlob);
113113

114-
spawnSyncAndExitWithoutError(
114+
spawnSyncAndAssert(
115115
outputFile,
116116
{
117117
env: {

0 commit comments

Comments
 (0)