-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better stdio option handling - fixes #24
* Better stdio option handling - fixes #24 * Based on work started in #80 * tweak docs * add fd test * add number tests and fix the bugs
- Loading branch information
1 parent
4231a10
commit 9cd5e2a
Showing
6 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 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`')); |