Skip to content

Commit d3bb741

Browse files
refacktargos
authored andcommitted
test: refacor spawn[Sync]Pwd
* extract the gist into common.pwdCommand * Merge test-child-process-buffering.js into test-child-process-stdio.js PR-URL: #22522 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 2937a79 commit d3bb741

7 files changed

+108
-148
lines changed

test/common/README.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,17 @@ A port number for tests to use if one is needed.
317317

318318
Logs '1..0 # Skipped: ' + `msg`
319319

320+
### pwdCommand
321+
* [&lt;array>] First two argument for the `spawn`/`exec` functions.
322+
323+
Platform normalized `pwd` command options. Usage example:
324+
```js
325+
const common = require('../common');
326+
const { spawn } = require('child_process');
327+
328+
spawn(...common.pwdCommand, { stdio: ['pipe'] });
329+
```
330+
320331
### rootDir
321332
* [&lt;string>]
322333

@@ -350,18 +361,6 @@ was disabled at compile time.
350361
Skip the rest of the tests in the current file when the Node.js executable
351362
was compiled with a pointer size smaller than 64 bits.
352363

353-
### spawnPwd(options)
354-
* `options` [&lt;Object>]
355-
* return [&lt;Object>]
356-
357-
Platform normalizes the `pwd` command.
358-
359-
### spawnSyncPwd(options)
360-
* `options` [&lt;Object>]
361-
* return [&lt;Object>]
362-
363-
Synchronous version of `spawnPwd`.
364-
365364
## ArrayStream Module
366365

367366
The `ArrayStream` module provides a simple `Stream` that pushes elements from

test/common/index.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const path = require('path');
2626
const fs = require('fs');
2727
const assert = require('assert');
2828
const os = require('os');
29-
const { exec, execSync, spawn, spawnSync } = require('child_process');
29+
const { exec, execSync, spawnSync } = require('child_process');
3030
const util = require('util');
3131
const Timer = process.binding('timer_wrap').Timer;
3232
const { fixturesDir } = require('./fixtures');
@@ -268,22 +268,10 @@ exports.ddCommand = function(filename, kilobytes) {
268268
};
269269

270270

271-
exports.spawnPwd = function(options) {
272-
if (exports.isWindows) {
273-
return spawn('cmd.exe', ['/d', '/c', 'cd'], options);
274-
} else {
275-
return spawn('pwd', [], options);
276-
}
277-
};
278-
271+
exports.pwdCommand = exports.isWindows ?
272+
['cmd.exe', ['/d', '/c', 'cd']] :
273+
['pwd', []];
279274

280-
exports.spawnSyncPwd = function(options) {
281-
if (exports.isWindows) {
282-
return spawnSync('cmd.exe', ['/d', '/c', 'cd'], options);
283-
} else {
284-
return spawnSync('pwd', [], options);
285-
}
286-
};
287275

288276
exports.platformTimeout = function(ms) {
289277
if (process.features.debug)

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

-51
This file was deleted.

test/parallel/test-child-process-custom-fds.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
'use strict';
33
const common = require('../common');
44
const assert = require('assert');
5+
const { spawnSync } = require('child_process');
56
const internalCp = require('internal/child_process');
6-
const oldSpawnSync = internalCp.spawnSync;
77

88
if (!common.isMainThread)
99
common.skip('stdio is not associated with file descriptors in Workers');
1010

11+
// This test uses the deprecated `customFds` option. We expect a deprecation
12+
// warning, but only once (per node process).
13+
const msg = 'child_process: options.customFds option is deprecated. ' +
14+
'Use options.stdio instead.';
15+
common.expectWarning('DeprecationWarning', msg, 'DEP0006');
16+
1117
// Verify that customFds is used if stdio is not provided.
1218
{
13-
const msg = 'child_process: options.customFds option is deprecated. ' +
14-
'Use options.stdio instead.';
15-
common.expectWarning('DeprecationWarning', msg, 'DEP0006');
16-
1719
const customFds = [-1, process.stdout.fd, process.stderr.fd];
20+
const oldSpawnSync = internalCp.spawnSync;
1821
internalCp.spawnSync = common.mustCall(function(opts) {
1922
assert.deepStrictEqual(opts.options.customFds, customFds);
2023
assert.deepStrictEqual(opts.options.stdio, [
@@ -23,13 +26,14 @@ if (!common.isMainThread)
2326
{ type: 'fd', fd: process.stderr.fd }
2427
]);
2528
});
26-
common.spawnSyncPwd({ customFds });
29+
spawnSync(...common.pwdCommand, { customFds });
2730
internalCp.spawnSync = oldSpawnSync;
2831
}
2932

3033
// Verify that customFds is ignored when stdio is present.
3134
{
3235
const customFds = [0, 1, 2];
36+
const oldSpawnSync = internalCp.spawnSync;
3337
internalCp.spawnSync = common.mustCall(function(opts) {
3438
assert.deepStrictEqual(opts.options.customFds, customFds);
3539
assert.deepStrictEqual(opts.options.stdio, [
@@ -38,6 +42,6 @@ if (!common.isMainThread)
3842
{ type: 'pipe', readable: false, writable: true }
3943
]);
4044
});
41-
common.spawnSyncPwd({ customFds, stdio: 'pipe' });
45+
spawnSync(...common.pwdCommand, { customFds, stdio: 'pipe' });
4246
internalCp.spawnSync = oldSpawnSync;
4347
}

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

+21-34
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,37 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
26-
let returns = 0;
25+
const { spawn } = require('child_process');
2726

2827
/*
2928
Spawns 'pwd' with given options, then test
30-
- whether the exit code equals forCode,
31-
- optionally whether the stdout result matches forData
32-
(after removing trailing whitespace)
29+
- whether the exit code equals expectCode,
30+
- optionally whether the trimmed stdout result matches expectData
3331
*/
34-
function testCwd(options, forCode, forData) {
35-
let data = '';
36-
37-
const child = common.spawnPwd(options);
32+
function testCwd(options, expectCode = 0, expectData) {
33+
const child = spawn(...common.pwdCommand, options);
3834

3935
child.stdout.setEncoding('utf8');
4036

37+
// No need to assert callback since `data` is asserted.
38+
let data = '';
4139
child.stdout.on('data', function(chunk) {
4240
data += chunk;
4341
});
4442

43+
// Can't assert callback, as stayed in to API:
44+
// _The 'exit' event may or may not fire after an error has occurred._
4545
child.on('exit', function(code, signal) {
46-
assert.strictEqual(forCode, code);
47-
});
48-
49-
child.on('close', function() {
50-
forData && assert.strictEqual(forData, data.replace(/[\s\r\n]+$/, ''));
51-
returns--;
46+
assert.strictEqual(expectCode, code);
5247
});
5348

54-
returns++;
49+
child.on('close', common.mustCall(function() {
50+
expectData && assert.strictEqual(data.trim(), expectData);
51+
}));
5552

5653
return child;
5754
}
5855

59-
// Assume these exist, and 'pwd' gives us the right directory back
60-
testCwd({ cwd: common.rootDir }, 0, common.rootDir);
61-
if (common.isWindows) {
62-
testCwd({ cwd: process.env.windir }, 0, process.env.windir);
63-
} else {
64-
testCwd({ cwd: '/dev' }, 0, '/dev');
65-
}
6656

6757
// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
6858
{
@@ -72,15 +62,12 @@ if (common.isWindows) {
7262
}));
7363
}
7464

75-
// Spawn() shouldn't try to chdir() so this should just work
76-
testCwd(undefined, 0);
77-
testCwd({}, 0);
78-
testCwd({ cwd: '' }, 0);
79-
testCwd({ cwd: undefined }, 0);
80-
testCwd({ cwd: null }, 0);
65+
// Assume these exist, and 'pwd' gives us the right directory back
66+
testCwd({ cwd: common.rootDir }, 0, common.rootDir);
67+
const shouldExistDir = common.isWindows ? process.env.windir : '/dev';
68+
testCwd({ cwd: shouldExistDir }, 0, shouldExistDir);
8169

82-
// Check whether all tests actually returned
83-
assert.notStrictEqual(returns, 0);
84-
process.on('exit', function() {
85-
assert.strictEqual(returns, 0);
86-
});
70+
// Spawn() shouldn't try to chdir() to invalid arg, so this should just work
71+
testCwd({ cwd: '' });
72+
testCwd({ cwd: undefined });
73+
testCwd({ cwd: null });

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25+
const { spawnSync } = require('child_process');
2526

26-
const spawnSync = require('child_process').spawnSync;
27-
28-
// Echo does different things on Windows and Unix, but in both cases, it does
27+
// `sleep` does different things on Windows and Unix, but in both cases, it does
2928
// more-or-less nothing if there are no parameters
3029
const ret = spawnSync('sleep', ['0']);
3130
assert.strictEqual(ret.status, 0);
@@ -42,21 +41,23 @@ assert.deepStrictEqual(ret_err.spawnargs, ['bar']);
4241
{
4342
// Test the cwd option
4443
const cwd = common.rootDir;
45-
const response = common.spawnSyncPwd({ cwd });
44+
const response = spawnSync(...common.pwdCommand, { cwd });
4645

4746
assert.strictEqual(response.stdout.toString().trim(), cwd);
4847
}
4948

49+
5050
{
51-
// Test the encoding option
52-
const noEncoding = common.spawnSyncPwd();
53-
const bufferEncoding = common.spawnSyncPwd({ encoding: 'buffer' });
54-
const utf8Encoding = common.spawnSyncPwd({ encoding: 'utf8' });
51+
// Assert Buffer is the default encoding
52+
const retDefault = spawnSync(...common.pwdCommand);
53+
const retBuffer = spawnSync(...common.pwdCommand, { encoding: 'buffer' });
54+
assert.deepStrictEqual(retDefault.output, retBuffer.output);
5555

56-
assert.deepStrictEqual(noEncoding.output, bufferEncoding.output);
57-
assert.deepStrictEqual([
56+
const retUTF8 = spawnSync(...common.pwdCommand, { encoding: 'utf8' });
57+
const stringifiedDefault = [
5858
null,
59-
noEncoding.stdout.toString(),
60-
noEncoding.stderr.toString()
61-
], utf8Encoding.output);
59+
retDefault.stdout.toString(),
60+
retDefault.stderr.toString()
61+
];
62+
assert.deepStrictEqual(retUTF8.output, stringifiedDefault);
6263
}

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

+47-15
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,56 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const spawnSync = require('child_process').spawnSync;
25+
const { spawn } = require('child_process');
2626

27-
let options = { stdio: ['pipe'] };
28-
let child = common.spawnPwd(options);
27+
// Test stdio piping.
28+
{
29+
const child = spawn(...common.pwdCommand, { stdio: ['pipe'] });
30+
assert.notStrictEqual(child.stdout, null);
31+
assert.notStrictEqual(child.stderr, null);
32+
}
2933

30-
assert.notStrictEqual(child.stdout, null);
31-
assert.notStrictEqual(child.stderr, null);
34+
// Test stdio ignoring.
35+
{
36+
const child = spawn(...common.pwdCommand, { stdio: 'ignore' });
37+
assert.strictEqual(child.stdout, null);
38+
assert.strictEqual(child.stderr, null);
39+
}
3240

33-
options = { stdio: 'ignore' };
34-
child = common.spawnPwd(options);
41+
// Asset options invariance.
42+
{
43+
const options = { stdio: 'ignore' };
44+
spawn(...common.pwdCommand, options);
45+
assert.deepStrictEqual(options, { stdio: 'ignore' });
46+
}
3547

36-
assert.strictEqual(child.stdout, null);
37-
assert.strictEqual(child.stderr, null);
48+
// Test stdout buffering.
49+
{
50+
let output = '';
51+
const child = spawn(...common.pwdCommand);
3852

39-
options = { stdio: 'ignore' };
40-
child = spawnSync('cat', [], options);
41-
assert.deepStrictEqual(options, { stdio: 'ignore' });
53+
child.stdout.setEncoding('utf8');
54+
child.stdout.on('data', function(s) {
55+
output += s;
56+
});
4257

43-
common.expectsError(() => {
44-
common.spawnPwd({ stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'ipc'] });
45-
}, { code: 'ERR_IPC_ONE_PIPE', type: Error });
58+
child.on('exit', common.mustCall(function(code) {
59+
assert.strictEqual(code, 0);
60+
}));
61+
62+
child.on('close', common.mustCall(function() {
63+
assert.strictEqual(true, output.length > 1);
64+
assert.strictEqual('\n', output[output.length - 1]);
65+
}));
66+
}
67+
68+
// Assert only one IPC pipe allowed.
69+
common.expectsError(
70+
() => {
71+
spawn(
72+
...common.pwdCommand,
73+
{ stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'ipc'] }
74+
);
75+
},
76+
{ code: 'ERR_IPC_ONE_PIPE', type: Error }
77+
);

0 commit comments

Comments
 (0)