Better stdio option handling - fixes #24
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Better stdio option handling - fixes #24 * Based on work started in #80 * tweak docs * add fd test * add number tests and fix the bugs
authored by Sam Verschueren · committed by James Talmage · parent 4231a10 · Browse files · 6 files·+167 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+167 added−2 removed
- tests:+50 added−0 removed
- other:+117 added−2 removed
Provenance
- Commit
9cd5e2a- Tree
c32d708- Author
Sam Verschueren <sam.verschueren@gmail.com>- Committer
James Talmage <james.talmage@jrtechnical.com>- Parent
4231a10
6 files changed+167 added−2 removed
added:lib/stdio.js+41 added
1234567891011121314151617181920212223242526272829303132333435363738394041'use strict';const alias = ['stdin', 'stdout', 'stderr'];
const hasAlias = opts => alias.some(x => Boolean(opts[x]));
module.exports = opts => { if (!opts) { return null; }
if (opts.stdio && hasAlias(opts)) { throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${alias.map(x => `\`${x}\``).join(', ')}`); }
if (typeof opts.stdio === 'string') { return opts.stdio; }
const stdio = opts.stdio || [];
if (!Array.isArray(stdio)) { throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); }
const result = []; const len = Math.max(stdio.length, alias.length);
for (let i = 0; i < len; i++) { let value = null;
if (stdio[i] !== undefined) { value = stdio[i]; } else if (opts[alias[i]] !== undefined) { value = opts[alias[i]]; }
result[i] = value; }
return result;};added:test/stdio.js+50 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import util from 'util';import test from 'ava';import stdio from '../lib/stdio';
util.inspect.styles.name = 'magenta';
function macro(t, input, expected) { if (expected instanceof Error) { t.throws(() => stdio(input), expected.message); return; }
const result = stdio(input);
if (typeof expected === 'object' && expected !== null) { t.deepEqual(result, expected); } else { t.is(result, expected); }}
macro.title = (providedTitle, input) => providedTitle || util.inspect(input, {colors: true});
test(macro, undefined, null);test(macro, null, null);
test(macro, {stdio: 'inherit'}, 'inherit');test(macro, {stdio: 'pipe'}, 'pipe');test(macro, {stdio: 'ignore'}, 'ignore');test(macro, {stdio: [0, 1, 2]}, [0, 1, 2]);
test(macro, {}, [null, null, null]);test(macro, {stdio: []}, [null, null, null]);test(macro, {stdin: 'pipe'}, ['pipe', null, null]);test(macro, {stdout: 'ignore'}, [null, 'ignore', null]);test(macro, {stderr: 'inherit'}, [null, null, 'inherit']);test(macro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);test(macro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', null]);test(macro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', null, 'inherit']);test(macro, {stdout: 'ignore', stderr: 'inherit'}, [null, 'ignore', 'inherit']);test(macro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2]);test(macro, {stdin: 0, stdout: 1}, [0, 1, null]);test(macro, {stdin: 0, stderr: 2}, [0, null, 2]);test(macro, {stdout: 1, stderr: 2}, [null, 1, 2]);
test(macro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));
test(macro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));test(macro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));test(macro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));modified:index.js+17 added-1 removed
7 unmodified lines8 const _getStream = require('get-stream');9 const pFinally = require('p-finally');10 const onExit = require('signal-exit');11 const errname = require('./lib/errname');12+const stdio = require('./lib/stdio');13 14 const TEN_MEGABYTES = 1000 * 1000 * 10;15 16 function handleArgs(cmd, args, opts) {20 unmodified lines37 reject: true,38 cleanup: true39 }, parsed.options);40 41+ opts.stdio = stdio(opts);42+43 if (opts.preferLocal) {44 opts.env = npmRunPath.env(opts);45 }46 161 unmodified lines208 }209 210 if (err || code !== 0 || signal !== null) {211 if (!err) {209- const output = parsed.opts.stdio === 'inherit' ? '' : `\n${stderr}${stdout}`;212+ let output = '';213+214+ if (Array.isArray(parsed.opts.stdio)) {215+ if (parsed.opts.stdio[2] !== 'inherit') {216+ output += output.length > 0 ? stderr : `\n${stderr}`;217+ }218+219+ if (parsed.opts.stdio[1] !== 'inherit') {220+ output += `\n${stdout}`;221+ }222+ } else if (parsed.opts.stdio !== 'inherit') {223+ output = `\n${stderr}${stdout}`;224+ }225+226 err = new Error(`Command failed: ${joinedCmd}${output}`);227 err.code = code < 0 ? errname(code) : code;228 }229 75 unmodified linesmodified:package.json+1 added
57 unmodified lines58596061626364656613 unmodified lines57 unmodified lines "coveralls": "^2.11.9", "delay": "^1.3.1", "is-running": "^2.0.0", "nyc": "^10.0.0", "tempfile": "^1.1.1", "xo": "*" }, "nyc": { "reporter": [13 unmodified linesmodified:readme.md+21 added
144 unmodified lines145 Default: `true`146 147 Keep track of the spawned process and `kill` it when the parent process exits.148 149+#### stdin150 151+Type: `string` `number` `Stream` `undefined` `null`<br>152+Default: `pipe`153+154+Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).155+156+#### stdout157+158+Type: `string` `number` `Stream` `undefined` `null`<br>159+Default: `pipe`160+161+Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).162+163+#### stderr164+165+Type: `string` `number` `Stream` `undefined` `null`<br>166+Default: `pipe`167+168+Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).169+170+171 ## Tips172 173 ### Save and pipe output from a child process174 19 unmodified linesmodified:test.js+37 added-1 removed
1 import path from 'path';2+import fs from 'fs';3 import stream from 'stream';4 import childProcess from 'child_process';5 import test from 'ava';6 import getStream from 'get-stream';7 import isRunning from 'is-running';8 import delay from 'delay';9+import tempfile from 'tempfile';10 import m from './';11 12 process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;13 32 unmodified lines46 });47 48 test('do not include in errors when `stdio` is set to `inherit`', async t => {49 const err = await t.throws(m('fixtures/error-message.js', {stdio: 'inherit'}));48- t.notRegex(err.message, /\\n/);50+ t.notRegex(err.message, /\n/);51 });52 53+test('do not include `stderr` and `stdout` in errors when set to `inherit`', async t => {54+ const err = await t.throws(m('fixtures/error-message.js', {stdout: 'inherit', stderr: 'inherit'}));55+ t.notRegex(err.message, /\n/);56+});57+58+test('do not include `stderr` and `stdout` in errors when `stdio` is set to `inherit`', async t => {59+ const err = await t.throws(m('fixtures/error-message.js', {stdio: [null, 'inherit', 'inherit']}));60+ t.notRegex(err.message, /\n/);61+});62+63+test('do not include `stdout` in errors when set to `inherit`', async t => {64+ const err = await t.throws(m('fixtures/error-message.js', {stdout: 'inherit'}));65+ t.notRegex(err.message, /stdout/);66+ t.regex(err.message, /stderr/);67+});68+69+test('do not include `stderr` in errors when set to `inherit`', async t => {70+ const err = await t.throws(m('fixtures/error-message.js', {stderr: 'inherit'}));71+ t.regex(err.message, /stdout/);72+ t.notRegex(err.message, /stderr/);73+});74+75+test('pass `stdout` to a file descriptor', async t => {76+ const file = tempfile('.txt');77+ await m('fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});78+ t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');79+});80+81+test('pass `stderr` to a file descriptor', async t => {82+ const file = tempfile('.txt');83+ await m('fixtures/noop-err', ['foo bar'], {stderr: fs.openSync(file, 'w')});84+ t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');85+});86+87 test('execa.shell()', async t => {88 const {stdout} = await m.shell('node fixtures/noop foo');89 t.is(stdout, 'foo');90 });323 unmodified lines