diff --git a/lib/stdio.js b/lib/stdio.js new file mode 100644 index 0000000000..a429123e98 --- /dev/null +++ b/lib/stdio.js @@ -0,0 +1,29 @@ +'use strict'; +var util = require('util'); + +var alias = ['stdin', 'stdout', 'stderr']; + +module.exports = function (opts) { + if (!opts) { + return null; + } + + if (typeof opts.stdio === 'string') { + return opts.stdio; + } + + var stdio = opts.stdio || []; + + if (!Array.isArray(stdio)) { + throw new TypeError('Incorrect value of stdio option: ' + util.inspect(stdio)); + } + + var result = []; + var len = Math.max(stdio.length, alias.length); + + for (var i = 0; i < len; i++) { + result[i] = stdio[i] || opts[alias[i]] || null; + } + + return result; +}; diff --git a/package.json b/package.json index 73b57d6f8e..f98416a693 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "coveralls": "nyc report --reporter=text-lcov | coveralls" }, "files": [ - "index.js" + "index.js", + "lib" ], "keywords": [ "exec", diff --git a/test.js b/test/index.js similarity index 91% rename from test.js rename to test/index.js index 91bb418a93..9896b06aae 100644 --- a/test.js +++ b/test/index.js @@ -2,9 +2,9 @@ import path from 'path'; import stream from 'stream'; import test from 'ava'; import getStream from 'get-stream'; -import m from './'; +import m from '../'; -process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH; +process.env.PATH = path.join(__dirname, '..', 'fixtures') + path.delimiter + process.env.PATH; test('execa()', async t => { const {stdout} = await m('noop', ['foo']); @@ -34,12 +34,12 @@ test('stdout/stderr available on errors', async t => { }); test('include stdout in errors for improved debugging', async t => { - const err = await t.throws(m('fixtures/error-message.js')); + const err = await t.throws(m('../fixtures/error-message.js')); t.regex(err.message, /stdout/); }); test('execa.shell()', async t => { - const {stdout} = await m.shell('node fixtures/noop foo'); + const {stdout} = await m.shell('node ../fixtures/noop foo'); t.is(stdout, 'foo'); }); @@ -54,7 +54,7 @@ test('execa.sync()', t => { }); test('execa.shellSync()', t => { - const {stdout} = m.shellSync('node fixtures/noop foo'); + const {stdout} = m.shellSync('node ../fixtures/noop foo'); t.is(stdout, 'foo'); }); diff --git a/test/stdio.js b/test/stdio.js new file mode 100644 index 0000000000..7da1085fd0 --- /dev/null +++ b/test/stdio.js @@ -0,0 +1,35 @@ +import util from 'util'; +import test from 'ava'; +import stdio from '../lib/stdio'; + +util.inspect.styles.name = 'magenta'; + +function t(input, expected) { + test(util.inspect(input, {colors: true}), t => { + const result = stdio(input); + + if (typeof expected === 'object' && expected !== null) { + t.deepEqual(result, expected); + } else { + t.is(result, expected); + } + }); +} + +t(undefined, null); +t(null, null); + +t({stdio: 'inherit'}, 'inherit'); +t({stdio: 'pipe'}, 'pipe'); +t({stdio: 'ignore'}, 'ignore'); + +t({}, [null, null, null]); +t({stdio: []}, [null, null, null]); +t({stdin: 'pipe'}, ['pipe', null, null]); +t({stdout: 'ignore'}, [null, 'ignore', null]); +t({stderr: 'inherit'}, [null, null, 'inherit']); + +// precedence +t({stdin: 'inherit', stdio: 'pipe'}, 'pipe'); +t({stdin: 'inherit', stdio: ['pipe']}, ['pipe', null, null]); +t({stdin: 'inherit', stdio: [undefined, 'pipe']}, ['inherit', 'pipe', null]);