GeezerVison is showing github.com — test: refacor spawn[Sync]Pwd · nodejs/node@d3bb741
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/common/README.md +11-12Lines changed: 11 additions & 12 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -317,6 +317,17 @@ A port number for tests to use if one is needed. | 317 | 317 |
| 318 | 318 | Logs '1..0 # Skipped: ' + `msg` | 319 | 319 |
| | 320 | +### pwdCommand | | 321 | +* [<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 | + | 320 | 331 | ### rootDir | 321 | 332 | * [<string>] | 322 | 333 |
| @@ -350,18 +361,6 @@ was disabled at compile time. | 350 | 361 | Skip the rest of the tests in the current file when the Node.js executable | 351 | 362 | was compiled with a pointer size smaller than 64 bits. | 352 | 363 |
| 353 | | -### spawnPwd(options) | 354 | | -* `options` [<Object>] | 355 | | -* return [<Object>] | 356 | | - | 357 | | -Platform normalizes the `pwd` command. | 358 | | - | 359 | | -### spawnSyncPwd(options) | 360 | | -* `options` [<Object>] | 361 | | -* return [<Object>] | 362 | | - | 363 | | -Synchronous version of `spawnPwd`. | 364 | | - | 365 | 364 | ## ArrayStream Module | 366 | 365 |
| 367 | 366 | The `ArrayStream` module provides a simple `Stream` that pushes elements from | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.js +4-16Lines changed: 4 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,7 @@ const path = require('path'); | 26 | 26 | const fs = require('fs'); | 27 | 27 | const assert = require('assert'); | 28 | 28 | const os = require('os'); | 29 | | -const { exec, execSync, spawn, spawnSync } = require('child_process'); | | 29 | +const { exec, execSync, spawnSync } = require('child_process'); | 30 | 30 | const util = require('util'); | 31 | 31 | const Timer = process.binding('timer_wrap').Timer; | 32 | 32 | const { fixturesDir } = require('./fixtures'); | @@ -268,22 +268,10 @@ exports.ddCommand = function(filename, kilobytes) { | 268 | 268 | }; | 269 | 269 |
| 270 | 270 |
| 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', []]; | 279 | 274 |
| 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 | | -}; | 287 | 275 |
| 288 | 276 | exports.platformTimeout = function(ms) { | 289 | 277 | if (process.features.debug) | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-buffering.js -51Lines changed: 0 additions & 51 deletions Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-custom-fds.js +11-7Lines changed: 11 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | 'use strict'; | 3 | 3 | const common = require('../common'); | 4 | 4 | const assert = require('assert'); | | 5 | +const { spawnSync } = require('child_process'); | 5 | 6 | const internalCp = require('internal/child_process'); | 6 | | -const oldSpawnSync = internalCp.spawnSync; | 7 | 7 |
| 8 | 8 | if (!common.isMainThread) | 9 | 9 | common.skip('stdio is not associated with file descriptors in Workers'); | 10 | 10 |
| | 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 | + | 11 | 17 | // Verify that customFds is used if stdio is not provided. | 12 | 18 | { | 13 | | - const msg = 'child_process: options.customFds option is deprecated. ' + | 14 | | - 'Use options.stdio instead.'; | 15 | | - common.expectWarning('DeprecationWarning', msg, 'DEP0006'); | 16 | | - | 17 | 19 | const customFds = [-1, process.stdout.fd, process.stderr.fd]; | | 20 | + const oldSpawnSync = internalCp.spawnSync; | 18 | 21 | internalCp.spawnSync = common.mustCall(function(opts) { | 19 | 22 | assert.deepStrictEqual(opts.options.customFds, customFds); | 20 | 23 | assert.deepStrictEqual(opts.options.stdio, [ | @@ -23,13 +26,14 @@ if (!common.isMainThread) | 23 | 26 | { type: 'fd', fd: process.stderr.fd } | 24 | 27 | ]); | 25 | 28 | }); | 26 | | - common.spawnSyncPwd({ customFds }); | | 29 | + spawnSync(...common.pwdCommand, { customFds }); | 27 | 30 | internalCp.spawnSync = oldSpawnSync; | 28 | 31 | } | 29 | 32 |
| 30 | 33 | // Verify that customFds is ignored when stdio is present. | 31 | 34 | { | 32 | 35 | const customFds = [0, 1, 2]; | | 36 | + const oldSpawnSync = internalCp.spawnSync; | 33 | 37 | internalCp.spawnSync = common.mustCall(function(opts) { | 34 | 38 | assert.deepStrictEqual(opts.options.customFds, customFds); | 35 | 39 | assert.deepStrictEqual(opts.options.stdio, [ | @@ -38,6 +42,6 @@ if (!common.isMainThread) | 38 | 42 | { type: 'pipe', readable: false, writable: true } | 39 | 43 | ]); | 40 | 44 | }); | 41 | | - common.spawnSyncPwd({ customFds, stdio: 'pipe' }); | | 45 | + spawnSync(...common.pwdCommand, { customFds, stdio: 'pipe' }); | 42 | 46 | internalCp.spawnSync = oldSpawnSync; | 43 | 47 | } |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-cwd.js +21-34Lines changed: 21 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 | const common = require('../common'); | 24 | 24 | const assert = require('assert'); | 25 | | - | 26 | | -let returns = 0; | | 25 | +const { spawn } = require('child_process'); | 27 | 26 |
| 28 | 27 | /* | 29 | 28 | 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 | 33 | 31 | */ | 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); | 38 | 34 |
| 39 | 35 | child.stdout.setEncoding('utf8'); | 40 | 36 |
| | 37 | + // No need to assert callback since `data` is asserted. | | 38 | + let data = ''; | 41 | 39 | child.stdout.on('data', function(chunk) { | 42 | 40 | data += chunk; | 43 | 41 | }); | 44 | 42 |
| | 43 | + // Can't assert callback, as stayed in to API: | | 44 | + // _The 'exit' event may or may not fire after an error has occurred._ | 45 | 45 | 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); | 52 | 47 | }); | 53 | 48 |
| 54 | | - returns++; | | 49 | + child.on('close', common.mustCall(function() { | | 50 | + expectData && assert.strictEqual(data.trim(), expectData); | | 51 | + })); | 55 | 52 |
| 56 | 53 | return child; | 57 | 54 | } | 58 | 55 |
| 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 | | -} | 66 | 56 |
| 67 | 57 | // Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT | 68 | 58 | { | @@ -72,15 +62,12 @@ if (common.isWindows) { | 72 | 62 | })); | 73 | 63 | } | 74 | 64 |
| 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); | 81 | 69 |
| 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 }); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-spawnsync.js +14-13Lines changed: 14 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 | const common = require('../common'); | 24 | 24 | const assert = require('assert'); | | 25 | +const { spawnSync } = require('child_process'); | 25 | 26 |
| 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 | 29 | 28 | // more-or-less nothing if there are no parameters | 30 | 29 | const ret = spawnSync('sleep', ['0']); | 31 | 30 | assert.strictEqual(ret.status, 0); | @@ -42,21 +41,23 @@ assert.deepStrictEqual(ret_err.spawnargs, ['bar']); | 42 | 41 | { | 43 | 42 | // Test the cwd option | 44 | 43 | const cwd = common.rootDir; | 45 | | - const response = common.spawnSyncPwd({ cwd }); | | 44 | + const response = spawnSync(...common.pwdCommand, { cwd }); | 46 | 45 |
| 47 | 46 | assert.strictEqual(response.stdout.toString().trim(), cwd); | 48 | 47 | } | 49 | 48 |
| | 49 | + | 50 | 50 | { | 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); | 55 | 55 |
| 56 | | - assert.deepStrictEqual(noEncoding.output, bufferEncoding.output); | 57 | | - assert.deepStrictEqual([ | | 56 | + const retUTF8 = spawnSync(...common.pwdCommand, { encoding: 'utf8' }); | | 57 | + const stringifiedDefault = [ | 58 | 58 | 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); | 62 | 63 | } |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-stdio.js +47-15Lines changed: 47 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 | const common = require('../common'); | 24 | 24 | const assert = require('assert'); | 25 | | -const spawnSync = require('child_process').spawnSync; | | 25 | +const { spawn } = require('child_process'); | 26 | 26 |
| 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 | +} | 29 | 33 |
| 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 | +} | 32 | 40 |
| 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 | +} | 35 | 47 |
| 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); | 38 | 52 |
| 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 | + }); | 42 | 57 |
| 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 | +); |
|