Skip to content

Commit

Permalink
Simplify tests for stdio (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 19, 2019
1 parent 977ad1d commit ab83d8d
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions test/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@ import util from 'util';
import test from 'ava';
import stdio from '../lib/stdio';

util.inspect.styles.name = 'magenta';
const macro = (t, input, expected, func) => {
if (expected instanceof Error) {
t.throws(() => {
stdio(input);
}, expected.message);
return;
}

function createMacro(func) {
const macro = (t, input, expected) => {
if (expected instanceof Error) {
t.throws(() => {
stdio(input);
}, expected.message);
return;
}
t.deepEqual(func(input), expected);
};

const result = func(input);
const macroTitle = name => (title, input) => `${name} ${(util.inspect(input))}`;

if (typeof expected === 'object' && expected !== null) {
t.deepEqual(result, expected);
} else {
t.is(result, expected);
}
};

macro.title = (providedTitle, input) => `${func.name} ${(providedTitle || util.inspect(input, {colors: true}))}`;

return macro;
}

const stdioMacro = createMacro(stdio);
const stdioMacro = (...args) => macro(...args, stdio);
stdioMacro.title = macroTitle('execa()');

test(stdioMacro, undefined, undefined);
test(stdioMacro, null, undefined);
Expand Down Expand Up @@ -57,7 +46,8 @@ test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possibl
test(stdioMacro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));
test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));

const forkMacro = createMacro(stdio.node);
const forkMacro = (...args) => macro(...args, stdio.node);
forkMacro.title = macroTitle('execa.fork()');

test(forkMacro, undefined, ['pipe', 'pipe', 'pipe', 'ipc']);
test(forkMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore', 'ipc']);
Expand Down

0 comments on commit ab83d8d

Please sign in to comment.