getScript('wait').then(script => { let child = execFile(script, { timeout: 100 }, (err, stdout, stderr) => { assert.equal(err.killed, true, 'error has `killed` property as true'); assert.equal(err.code, null, 'error has `code` as null'); assert.equal(err.signal, 'SIGTERM', 'error has `signal` as SIGTERM by default'); assert.equal(stdout, '', 'stdout is empty'); assert.equal(stderr, '', 'stderr is empty'); if (++count === 3) complete(); }); function exitHandler (code, signal) { assert.equal(code, null, 'error has `code` as null'); assert.equal(signal, 'SIGTERM', 'error has `signal` as SIGTERM by default'); if (++count === 3) complete(); } function closeHandler (code, signal) { assert.equal(code, null, 'error has `code` as null'); assert.equal(signal, 'SIGTERM', 'error has `signal` as SIGTERM by default'); if (++count === 3) complete(); } child.on('exit', exitHandler); child.on('close', closeHandler); function complete () { child.off('exit', exitHandler); child.off('close', closeHandler); done(); } }).catch(assert.fail);
getScript('wait').then(script => { execFile(script, { killSignal: 'beepbeep', timeout: 10 }, function (err, stdout, stderr) { assert.equal(err.signal, 'beepbeep', 'correctly used custom killSignal'); done(); }); }).catch(assert.fail);
getScript('args').then(script => { execFile(script, ['--myargs', '-j', '-s'], { cwd: PROFILE_DIR }, function (err, stdout, stderr) { assert.ok(!err, 'no errors found'); assert.equal(stderr, '', 'stderr is empty'); // Trim output since different systems have different new line output assert.equal(stdout.trim(), '--myargs -j -s'.trim(), 'passes in correct arguments'); done(); }); }).catch(assert.fail);
getScript('large-err').then(script => { stderrChild = execFile(script, ['60'], { maxBuffer: 50 }, (err, stdout, stderr) => { assert.ok(/stderr maxBuffer exceeded/.test(err.toString()), 'error contains stderr maxBuffer exceeded message'); assert.ok(stderr.length >= 50, 'stderr has full buffer'); assert.equal(stdout, '', 'stdout is empty'); if (++count === 3) complete(); }); stderrChild.on('exit', exitHandler); stderrChild.on('close', closeHandler); }).catch(assert.fail);
exports.testExecFileCallbackError = function (assert, done) { execFile('not-real-command', { cwd: PROFILE_DIR }, function (err, stdout, stderr) { assert.ok(/Executable not found/.test(err.message), `error '${err.message}' contains error message`); assert.ok(err.lineNumber >= 0, 'error contains lineNumber'); assert.ok(/resource:\/\//.test(err.fileName), 'error contains fileName'); assert.equal(stdout, '', 'stdout is empty'); assert.equal(stderr, '', 'stdout is empty'); done(); }); };
getScript('check-env').then(script => { execFile(script, { cwd: PROFILE_DIR, env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' } }, function (err, stdout, stderr) { assert.equal(stderr, '', 'stderr is empty'); assert.ok(!err, 'received `cwd` option'); assert.ok(/my-value-test/.test(stdout), 'receives environment option'); done(); }); }).catch(assert.fail);
this.exec = function(command, opts = {}, callback = function() {}) { var _opts = { cwd: this.getCwd(), env: this.getEnv() }; // Resolve binary command = command.trim().split(" "); var binary = command.shift(); binary = this.lookup(binary) || binary; command.unshift(binary); command = command.join(" "); var _ = require("contrib/underscore"); _opts = _.extend(_opts, opts); // Prepare platform command var platform = require("sdk/system").platform; var file, cmdArgs; if (opts.argv) { file = command; cmdArgs = opts.argv; } else { if (platform.indexOf('win') === 0) { file = 'C:\\Windows\\System32\\cmd.exe'; cmdArgs = ['/s', '/c', command]; } else { file = '/bin/sh'; cmdArgs = ['-c', command]; } } // Undocumented option from node being able to specify shell if (_opts && _opts.shell) file = _opts.shell; var proc = require("sdk/system/child_process"); var process = proc.execFile(file, cmdArgs, _opts, callback); this.mediator(process, opts, command); return process; }
exports.testChildEventsSpawningError= function (assert, done) { let handlersCalled = 0; let child = execFile('i-do-not-exist', (err, stdout, stderr) => { assert.ok(err, 'error was passed into callback'); assert.equal(stdout, '', 'stdout is empty') assert.equal(stderr, '', 'stderr is empty'); if (++handlersCalled === 3) complete(); }); child.on('error', handleError); child.on('exit', handleExit); child.on('close', handleClose); function handleError (e) { assert.ok(e, 'error passed into error handler'); if (++handlersCalled === 3) complete(); } function handleClose (code, signal) { assert.equal(code, -1, 'process was never spawned, therefore exit code is -1'); assert.equal(signal, null, 'signal should be null'); if (++handlersCalled === 3) complete(); } function handleExit (code, signal) { assert.fail('Close event should not be called on init failure'); } function complete () { child.off('error', handleError); child.off('exit', handleExit); child.off('close', handleClose); done(); } };