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

Simplify tests for stdio #300

Merged
merged 1 commit into from Jun 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 14 additions & 24 deletions test/stdio.js
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