Skip to content

Commit b945d7b

Browse files
aduh95RafaelGSS
authored andcommitted
test: use spawn and spawnPromisified instead of exec
PR-URL: #48991 Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Debadree Chatterjee <[email protected]>
1 parent 821b11a commit b945d7b

7 files changed

+101
-128
lines changed

test/parallel/test-preload.js

+66-92
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ const childProcess = require('child_process');
1111

1212
const nodeBinary = process.argv[0];
1313

14-
const preloadOption = (preloads) => {
15-
let option = '';
16-
preloads.forEach(function(preload, index) {
17-
option += `-r "${preload}" `;
18-
});
19-
return option;
14+
const preloadOption = (...preloads) => {
15+
return preloads.flatMap((preload) => ['-r', preload]);
2016
};
2117

2218
const fixtureA = fixtures.path('printA.js');
@@ -34,75 +30,52 @@ assert(!module.isPreloading);
3430

3531
// Test that module.isPreloading is set in preloaded module
3632
// Test preloading a single module works
37-
childProcess.exec(
38-
`"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`,
39-
function(err, stdout, stderr) {
40-
assert.ifError(err);
41-
assert.strictEqual(stdout, 'B\n');
42-
});
33+
common.spawnPromisified(nodeBinary, [...preloadOption(fixtureIsPreloading), fixtureB]).then(common.mustCall(
34+
({ stdout }) => {
35+
assert.strictEqual(stdout.trim(), 'B');
36+
}
37+
));
4338

4439
// Test preloading a single module works
45-
childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`,
46-
function(err, stdout, stderr) {
47-
assert.ifError(err);
48-
assert.strictEqual(stdout, 'A\nB\n');
49-
});
40+
common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA), fixtureB]).then(common.mustCall(
41+
({ stdout }) => {
42+
assert.strictEqual(stdout, 'A\nB\n');
43+
}));
5044

5145
// Test preloading multiple modules works
52-
childProcess.exec(
53-
`"${nodeBinary}" ${preloadOption([fixtureA, fixtureB])} "${fixtureC}"`,
54-
function(err, stdout, stderr) {
55-
assert.ifError(err);
46+
common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA, fixtureB), fixtureC]).then(common.mustCall(
47+
({ stdout }) => {
5648
assert.strictEqual(stdout, 'A\nB\nC\n');
5749
}
58-
);
50+
));
5951

6052
// Test that preloading a throwing module aborts
61-
childProcess.exec(
62-
`"${nodeBinary}" ${preloadOption([fixtureA, fixtureThrows])} "${fixtureB}"`,
63-
function(err, stdout, stderr) {
64-
if (err) {
65-
assert.strictEqual(stdout, 'A\n');
66-
} else {
67-
throw new Error('Preload should have failed');
68-
}
53+
common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA, fixtureThrows), fixtureB]).then(common.mustCall(
54+
({ code, stdout }) => {
55+
assert.strictEqual(stdout, 'A\n');
56+
assert.strictEqual(code, 1);
6957
}
70-
);
58+
));
7159

7260
// Test that preload can be used with --eval
73-
childProcess.exec(
74-
`"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');"`,
75-
function(err, stdout, stderr) {
76-
assert.ifError(err);
61+
common.spawnPromisified(nodeBinary, [...preloadOption(fixtureA), '-e', 'console.log("hello");']).then(common.mustCall(
62+
({ stdout }) => {
7763
assert.strictEqual(stdout, 'A\nhello\n');
7864
}
79-
);
65+
));
8066

8167
// Test that preload can be used with --frozen-intrinsics
82-
childProcess.exec(
83-
`"${nodeBinary}" --frozen-intrinsics ${
84-
preloadOption([fixtureE])
85-
} ${
86-
fixtureF
87-
}`,
88-
function(err, stdout) {
89-
assert.ifError(err);
90-
assert.strictEqual(stdout, 'smoosh\n');
91-
}
92-
);
93-
childProcess.exec(
94-
`"${
95-
nodeBinary
96-
}" --frozen-intrinsics ${
97-
preloadOption([fixtureE])
98-
} ${
99-
fixtureG
100-
} ${fixtureF}`,
101-
function(err, stdout) {
102-
assert.ifError(err);
68+
common.spawnPromisified(nodeBinary, ['--frozen-intrinsics', ...preloadOption(fixtureE), fixtureF]).then(common.mustCall(
69+
({ stdout }) => {
10370
assert.strictEqual(stdout, 'smoosh\n');
10471
}
105-
);
72+
));
73+
common.spawnPromisified(nodeBinary, ['--frozen-intrinsics', ...preloadOption(fixtureE), fixtureG, fixtureF])
74+
.then(common.mustCall(
75+
({ stdout }) => {
76+
assert.strictEqual(stdout, 'smoosh\n');
77+
}
78+
));
10679

10780
// Test that preload can be used with stdin
10881
const stdinProc = childProcess.spawn(
@@ -143,61 +116,62 @@ replProc.on('close', function(code) {
143116

144117
// Test that preload placement at other points in the cmdline
145118
// also test that duplicated preload only gets loaded once
146-
childProcess.exec(
147-
`"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');" ${
148-
preloadOption([fixtureA, fixtureB])}`,
149-
function(err, stdout, stderr) {
150-
assert.ifError(err);
119+
common.spawnPromisified(nodeBinary, [
120+
...preloadOption(fixtureA),
121+
'-e', 'console.log("hello");',
122+
...preloadOption(fixtureA, fixtureB),
123+
]).then(common.mustCall(
124+
({ stdout }) => {
151125
assert.strictEqual(stdout, 'A\nB\nhello\n');
152126
}
153-
);
127+
));
154128

155129
// Test that preload works with -i
156-
const interactive = childProcess.exec(
157-
`"${nodeBinary}" ${preloadOption([fixtureD])}-i`,
158-
common.mustSucceed((stdout, stderr) => {
159-
assert.ok(stdout.endsWith("> 'test'\n> "));
160-
})
161-
);
130+
const interactive = childProcess.spawn(nodeBinary, [...preloadOption(fixtureD), '-i']);
131+
132+
{
133+
const stdout = [];
134+
interactive.stdout.on('data', (chunk) => {
135+
stdout.push(chunk);
136+
});
137+
interactive.on('close', common.mustCall(() => {
138+
assert.match(Buffer.concat(stdout).toString('utf8'), /> 'test'\r?\n> $/);
139+
}));
140+
}
162141

163142
interactive.stdin.write('a\n');
164143
interactive.stdin.write('process.exit()\n');
165144

166-
childProcess.exec(
167-
`"${nodeBinary}" --require "${fixtures.path('cluster-preload.js')}" "${
168-
fixtures.path('cluster-preload-test.js')}"`,
169-
function(err, stdout, stderr) {
170-
assert.ifError(err);
145+
common.spawnPromisified(nodeBinary,
146+
['--require', fixtures.path('cluster-preload.js'), fixtures.path('cluster-preload-test.js')])
147+
.then(common.mustCall(({ stdout }) => {
171148
assert.match(stdout, /worker terminated with code 43/);
172-
}
173-
);
149+
}));
174150

175151
// Test that preloading with a relative path works
176-
childProcess.exec(
177-
`"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
178-
{ cwd: fixtures.fixturesDir },
179-
common.mustSucceed((stdout, stderr) => {
152+
common.spawnPromisified(nodeBinary,
153+
[...preloadOption('./printA.js'), fixtureB],
154+
{ cwd: fixtures.fixturesDir }).then(common.mustCall(
155+
({ stdout }) => {
180156
assert.strictEqual(stdout, 'A\nB\n');
181157
})
182158
);
183159
if (common.isWindows) {
184160
// https://github.com/nodejs/node/issues/21918
185-
childProcess.exec(
186-
`"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
187-
{ cwd: fixtures.fixturesDir },
188-
common.mustSucceed((stdout, stderr) => {
161+
common.spawnPromisified(nodeBinary,
162+
[...preloadOption('.\\printA.js'), fixtureB],
163+
{ cwd: fixtures.fixturesDir }).then(common.mustCall(
164+
({ stdout }) => {
189165
assert.strictEqual(stdout, 'A\nB\n');
190166
})
191167
);
192168
}
193169

194170
// https://github.com/nodejs/node/issues/1691
195-
childProcess.exec(
196-
`"${nodeBinary}" --require ` +
197-
`"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`,
198-
{ cwd: fixtures.fixturesDir },
199-
function(err, stdout, stderr) {
200-
assert.ifError(err);
171+
common.spawnPromisified(nodeBinary,
172+
['--require', fixtures.path('cluster-preload.js'), 'cluster-preload-test.js'],
173+
{ cwd: fixtures.fixturesDir }).then(common.mustCall(
174+
({ stdout }) => {
201175
assert.match(stdout, /worker terminated with code 43/);
202176
}
203-
);
177+
));

test/parallel/test-timers-immediate-promisified.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ const common = require('../common');
44
const assert = require('assert');
55
const timers = require('timers');
66
const { promisify } = require('util');
7-
const child_process = require('child_process');
87

98
const { getEventListeners } = require('events');
109
const { NodeEventTarget } = require('internal/event_target');
1110

1211
const timerPromises = require('timers/promises');
1312

1413
const setPromiseImmediate = promisify(timers.setImmediate);
15-
const exec = promisify(child_process.exec);
1614

1715
assert.strictEqual(setPromiseImmediate, timerPromises.setImmediate);
1816

@@ -91,9 +89,9 @@ process.on('multipleResolves', common.mustNotCall());
9189
}
9290

9391
{
94-
exec(`${process.execPath} -pe "const assert = require('assert');` +
92+
common.spawnPromisified(process.execPath, ['-pe', "const assert = require('assert');" +
9593
'require(\'timers/promises\').setImmediate(null, { ref: false }).' +
96-
'then(assert.fail)"').then(common.mustCall(({ stderr }) => {
94+
'then(assert.fail)']).then(common.mustCall(({ stderr }) => {
9795
assert.strictEqual(stderr, '');
9896
}));
9997
}

test/parallel/test-timers-interval-promisified.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ const common = require('../common');
44
const assert = require('assert');
55
const timers = require('timers');
66
const { promisify } = require('util');
7-
const child_process = require('child_process');
87

98
const { getEventListeners } = require('events');
109
const { NodeEventTarget } = require('internal/event_target');
1110

1211
const timerPromises = require('timers/promises');
1312

1413
const setPromiseTimeout = promisify(timers.setTimeout);
15-
const exec = promisify(child_process.exec);
1614

1715
const { setInterval } = timerPromises;
1816

@@ -154,11 +152,11 @@ process.on('multipleResolves', common.mustNotCall());
154152
}
155153

156154
{
157-
exec(`${process.execPath} -pe "const assert = require('assert');` +
155+
common.spawnPromisified(process.execPath, ['-pe', "const assert = require('assert');" +
158156
'const interval = require(\'timers/promises\')' +
159157
'.setInterval(1000, null, { ref: false });' +
160158
'interval[Symbol.asyncIterator]().next()' +
161-
'.then(assert.fail)"').then(common.mustCall(({ stderr }) => {
159+
'.then(assert.fail)']).then(common.mustCall(({ stderr }) => {
162160
assert.strictEqual(stderr, '');
163161
}));
164162
}

test/parallel/test-timers-timeout-promisified.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ const common = require('../common');
44
const assert = require('assert');
55
const timers = require('timers');
66
const { promisify } = require('util');
7-
const child_process = require('child_process');
87

98
const { getEventListeners } = require('events');
109
const { NodeEventTarget } = require('internal/event_target');
1110

1211
const timerPromises = require('timers/promises');
1312

1413
const setPromiseTimeout = promisify(timers.setTimeout);
15-
const exec = promisify(child_process.exec);
1614

1715
assert.strictEqual(setPromiseTimeout, timerPromises.setTimeout);
1816

@@ -91,9 +89,9 @@ process.on('multipleResolves', common.mustNotCall());
9189
}
9290

9391
{
94-
exec(`${process.execPath} -pe "const assert = require('assert');` +
92+
common.spawnPromisified(process.execPath, ['-pe', 'const assert = require(\'assert\');' +
9593
'require(\'timers/promises\').setTimeout(1000, null, { ref: false }).' +
96-
'then(assert.fail)"').then(common.mustCall(({ stderr }) => {
94+
'then(assert.fail)']).then(common.mustCall(({ stderr }) => {
9795
assert.strictEqual(stderr, '');
9896
}));
9997
}

test/parallel/test-url-parse-invalid-input.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const childProcess = require('child_process');
54
const url = require('url');
65

76
// https://github.com/joyent/node/issues/568
@@ -82,13 +81,12 @@ if (common.hasIntl) {
8281
'git+ssh://[email protected]:npm/npm',
8382
];
8483
badURLs.forEach((badURL) => {
85-
childProcess.exec(`${process.execPath} -e "url.parse('${badURL}')"`,
86-
common.mustCall((err, stdout, stderr) => {
87-
assert.strictEqual(err, null);
88-
assert.strictEqual(stdout, '');
89-
assert.match(stderr, /\[DEP0170\] DeprecationWarning:/);
90-
})
91-
);
84+
common.spawnPromisified(process.execPath, ['-e', `url.parse(${JSON.stringify(badURL)})`])
85+
.then(common.mustCall(({ code, stdout, stderr }) => {
86+
assert.strictEqual(code, 0);
87+
assert.strictEqual(stdout, '');
88+
assert.match(stderr, /\[DEP0170\] DeprecationWarning:/);
89+
}));
9290
});
9391

9492
// Warning should only happen once per process.

test/sequential/test-cli-syntax-require.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5-
const { exec } = require('child_process');
5+
const { spawn } = require('child_process');
66
const fixtures = require('../common/fixtures');
77

88
const node = process.execPath;
@@ -17,20 +17,25 @@ const syntaxErrorRE = /^SyntaxError: \b/m;
1717
const preloadFile = fixtures.path('no-wrapper.js');
1818
const file = fixtures.path('syntax', 'illegal_if_not_wrapped.js');
1919
const args = [requireFlag, preloadFile, checkFlag, file];
20-
const cmd = [node, ...args].join(' ');
21-
exec(cmd, common.mustCall((err, stdout, stderr) => {
22-
assert.strictEqual(err instanceof Error, true);
23-
assert.strictEqual(err.code, 1,
24-
`code ${err.code} !== 1 for error:\n\n${err}`);
20+
const cp = spawn(node, args);
2521

26-
// No stdout should be produced
27-
assert.strictEqual(stdout, '');
22+
// No stdout should be produced
23+
cp.stdout.on('data', common.mustNotCall('stdout data event'));
2824

25+
const stderrBuffer = [];
26+
cp.stderr.on('data', (chunk) => stderrBuffer.push(chunk));
27+
28+
cp.on('exit', common.mustCall((code, signal) => {
29+
assert.strictEqual(code, 1);
30+
assert.strictEqual(signal, null);
31+
32+
const stderr = Buffer.concat(stderrBuffer).toString('utf-8');
2933
// stderr should have a syntax error message
3034
assert.match(stderr, syntaxErrorRE);
3135

3236
// stderr should include the filename
3337
assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
3438
}));
39+
3540
});
3641
});

0 commit comments

Comments
 (0)