Skip to content
Myr

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

6 files+167 added−2 removed

added:lib/stdio.js+41 added
added:test/stdio.js+50 added
modified:index.js+17 added-1 removed
7 unmodified lines
8const _getStream = require('get-stream');9const pFinally = require('p-finally');10const onExit = require('signal-exit');11const errname = require('./lib/errname');12const stdio = require('./lib/stdio');1314const TEN_MEGABYTES = 1000 * 1000 * 10;1516function handleArgs(cmd, args, opts) {
20 unmodified lines
37 reject: true,38 cleanup: true39 }, parsed.options);4041 opts.stdio = stdio(opts);4243 if (opts.preferLocal) {44 opts.env = npmRunPath.env(opts);45 }46
161 unmodified lines
208 }209210 if (err || code !== 0 || signal !== null) {211 if (!err) {209 const output = parsed.opts.stdio === 'inherit' ? '' : `\n${stderr}${stdout}`;212 let output = '';213214 if (Array.isArray(parsed.opts.stdio)) {215 if (parsed.opts.stdio[2] !== 'inherit') {216 output += output.length > 0 ? stderr : `\n${stderr}`;217 }218219 if (parsed.opts.stdio[1] !== 'inherit') {220 output += `\n${stdout}`;221 }222 } else if (parsed.opts.stdio !== 'inherit') {223 output = `\n${stderr}${stdout}`;224 }225226 err = new Error(`Command failed: ${joinedCmd}${output}`);227 err.code = code < 0 ? errname(code) : code;228 }229
75 unmodified lines
modified:package.json+1 added
modified:readme.md+21 added
144 unmodified lines
145Default: `true`146147Keep track of the spawned process and `kill` it when the parent process exits.148149#### stdin150151Type: `string` `number` `Stream` `undefined` `null`<br>152Default: `pipe`153154Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).155156#### stdout157158Type: `string` `number` `Stream` `undefined` `null`<br>159Default: `pipe`160161Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).162163#### stderr164165Type: `string` `number` `Stream` `undefined` `null`<br>166Default: `pipe`167168Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).169170171## Tips172173### Save and pipe output from a child process174
19 unmodified lines
modified:test.js+37 added-1 removed
1import path from 'path';2import fs from 'fs';3import stream from 'stream';4import childProcess from 'child_process';5import test from 'ava';6import getStream from 'get-stream';7import isRunning from 'is-running';8import delay from 'delay';9import tempfile from 'tempfile';10import m from './';1112process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;13
32 unmodified lines
46});4748test('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});5253test('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});5758test('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});6263test('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});6869test('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});7475test('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});8081test('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});8687test('execa.shell()', async t => {88 const {stdout} = await m.shell('node fixtures/noop foo');89 t.is(stdout, 'foo');90});
323 unmodified lines