Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] stdio option handling #36

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/stdio.js
Original file line number Diff line number Diff line change
@@ -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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`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;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"files": [
"index.js"
"index.js",
"lib"
],
"keywords": [
"exec",
Expand Down
10 changes: 5 additions & 5 deletions test.js → test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required because: avajs/ava#32 (comment)

t.is(stdout, 'foo');
});

Expand All @@ -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');
});

Expand Down
35 changes: 35 additions & 0 deletions test/stdio.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need test macros.


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']);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also do one with stdout and stderr at the same time?


// precedence
t({stdin: 'inherit', stdio: 'pipe'}, 'pipe');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just throw if they're used together?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's probably the way to go.

t({stdin: 'inherit', stdio: ['pipe']}, ['pipe', null, null]);
t({stdin: 'inherit', stdio: [undefined, 'pipe']}, ['inherit', 'pipe', null]);