| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,7 @@ const { | |||
| 32 | 32 | ArrayPrototypeSort, | |
| 33 | 33 | ArrayPrototypeSplice, | |
| 34 | 34 | ArrayPrototypeUnshift, | |
| 35 | + ArrayPrototypePushApply, | ||
| 35 | 36 | NumberIsInteger, | |
| 36 | 37 | ObjectAssign, | |
| 37 | 38 | ObjectDefineProperty, | |
@@ -249,6 +250,45 @@ ObjectDefineProperty(exec, promisify.custom, { | |||
| 249 | 250 | value: customPromiseExecFunction(exec) | |
| 250 | 251 | }); | |
| 251 | 252 | ||
| 253 | + function normalizeExecFileArgs(file, args, options, callback) { | ||
| 254 | + if (ArrayIsArray(args)) { | ||
| 255 | + args = ArrayPrototypeSlice(args); | ||
| 256 | + } else if (args != null && typeof args === 'object') { | ||
| 257 | + callback = options; | ||
| 258 | + options = args; | ||
| 259 | + args = null; | ||
| 260 | + } else if (typeof args === 'function') { | ||
| 261 | + callback = args; | ||
| 262 | + options = null; | ||
| 263 | + args = null; | ||
| 264 | + } | ||
| 265 | + | ||
| 266 | + if (args == null) { | ||
| 267 | + args = []; | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + if (typeof options === 'function') { | ||
| 271 | + callback = options; | ||
| 272 | + } else if (options != null) { | ||
| 273 | + validateObject(options, 'options'); | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + if (options == null) { | ||
| 277 | + options = kEmptyObject; | ||
| 278 | + } | ||
| 279 | + | ||
| 280 | + if (callback != null) { | ||
| 281 | + validateFunction(callback, 'callback'); | ||
| 282 | + } | ||
| 283 | + | ||
| 284 | + // Validate argv0, if present. | ||
| 285 | + if (options.argv0 != null) { | ||
| 286 | + validateString(options.argv0, 'options.argv0'); | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + return { file, args, options, callback }; | ||
| 290 | + } | ||
| 291 | + | ||
| 252 | 292 | /** | |
| 253 | 293 | * Spawns the specified file as a shell. | |
| 254 | 294 | * @param {string} file | |
@@ -274,27 +314,8 @@ ObjectDefineProperty(exec, promisify.custom, { | |||
| 274 | 314 | * ) => any} [callback] | |
| 275 | 315 | * @returns {ChildProcess} | |
| 276 | 316 | */ | |
| 277 | - function execFile(file, args = [], options, callback) { | ||
| 278 | - if (args != null && typeof args === 'object' && !ArrayIsArray(args)) { | ||
| 279 | - callback = options; | ||
| 280 | - options = args; | ||
| 281 | - args = null; | ||
| 282 | - } else if (typeof args === 'function') { | ||
| 283 | - callback = args; | ||
| 284 | - options = null; | ||
| 285 | - args = null; | ||
| 286 | - } | ||
| 287 | - | ||
| 288 | - if (typeof options === 'function') { | ||
| 289 | - callback = options; | ||
| 290 | - options = null; | ||
| 291 | - } else if (options != null) { | ||
| 292 | - validateObject(options, 'options'); | ||
| 293 | - } | ||
| 294 | - | ||
| 295 | - if (callback != null) { | ||
| 296 | - validateFunction(callback, 'callback'); | ||
| 297 | - } | ||
| 317 | + function execFile(file, args, options, callback) { | ||
| 318 | + ({ file, args, options, callback } = normalizeExecFileArgs(file, args, options, callback)); | ||
| 298 | 319 | ||
| 299 | 320 | options = { | |
| 300 | 321 | encoding: 'utf8', | |
@@ -824,7 +845,7 @@ function checkExecSyncError(ret, args, cmd) { | |||
| 824 | 845 | ||
| 825 | 846 | /** | |
| 826 | 847 | * Spawns a file as a shell synchronously. | |
| 827 | - * @param {string} command | ||
| 848 | + * @param {string} file | ||
| 828 | 849 | * @param {string[]} [args] | |
| 829 | 850 | * @param {{ | |
| 830 | 851 | * cwd?: string; | |
@@ -842,17 +863,18 @@ function checkExecSyncError(ret, args, cmd) { | |||
| 842 | 863 | * }} [options] | |
| 843 | 864 | * @returns {Buffer | string} | |
| 844 | 865 | */ | |
| 845 | - function execFileSync(command, args, options) { | ||
| 846 | - options = normalizeSpawnArguments(command, args, options); | ||
| 866 | + function execFileSync(file, args, options) { | ||
| 867 | + ({ file, args, options } = normalizeExecFileArgs(file, args, options)); | ||
| 847 | 868 | ||
| 848 | 869 | const inheritStderr = !options.stdio; | |
| 849 | - const ret = spawnSync(options.file, | ||
| 850 | - ArrayPrototypeSlice(options.args, 1), options); | ||
| 870 | + const ret = spawnSync(file, args, options); | ||
| 851 | 871 | ||
| 852 | 872 | if (inheritStderr && ret.stderr) | |
| 853 | 873 | process.stderr.write(ret.stderr); | |
| 854 | 874 | ||
| 855 | - const err = checkExecSyncError(ret, options.args, undefined); | ||
| 875 | + const errArgs = [options.argv0 || file]; | ||
| 876 | + ArrayPrototypePushApply(errArgs, args); | ||
| 877 | + const err = checkExecSyncError(ret, errArgs); | ||
| 856 | 878 | ||
| 857 | 879 | if (err) | |
| 858 | 880 | throw err; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,10 +2,11 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | const assert = require('assert'); | |
| 5 | - const execFile = require('child_process').execFile; | ||
| 5 | + const { execFile, execFileSync } = require('child_process'); | ||
| 6 | 6 | const { getEventListeners } = require('events'); | |
| 7 | 7 | const { getSystemErrorName } = require('util'); | |
| 8 | 8 | const fixtures = require('../common/fixtures'); | |
| 9 | + const os = require('os'); | ||
| 9 | 10 | ||
| 10 | 11 | const fixture = fixtures.path('exit.js'); | |
| 11 | 12 | const echoFixture = fixtures.path('echo.js'); | |
@@ -99,3 +100,23 @@ const execOpts = { encoding: 'utf8', shell: true }; | |||
| 99 | 100 | }); | |
| 100 | 101 | execFile(process.execPath, [fixture, 0], { signal }, callback); | |
| 101 | 102 | } | |
| 103 | + | ||
| 104 | + // Verify the execFile() stdout is the same as execFileSync(). | ||
| 105 | + { | ||
| 106 | + const file = 'echo'; | ||
| 107 | + const args = ['foo', 'bar']; | ||
| 108 | + | ||
| 109 | + // Test with and without `{ shell: true }` | ||
| 110 | + [ | ||
| 111 | + // Skipping shell-less test on Windows because its echo command is a shell built-in command. | ||
| 112 | + ...(common.isWindows ? [] : [{ encoding: 'utf8' }]), | ||
| 113 | + { shell: true, encoding: 'utf8' }, | ||
| 114 | + ].forEach((options) => { | ||
| 115 | + const execFileSyncStdout = execFileSync(file, args, options); | ||
| 116 | + assert.strictEqual(execFileSyncStdout, `foo bar${os.EOL}`); | ||
| 117 | + | ||
| 118 | + execFile(file, args, options, common.mustCall((_, stdout) => { | ||
| 119 | + assert.strictEqual(stdout, execFileSyncStdout); | ||
| 120 | + })); | ||
| 121 | + }); | ||
| 122 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments