Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 1, 2024
1 parent 21dcf74 commit 608311f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
6 changes: 2 additions & 4 deletions source/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ export const getOptions = ({
}) => {
const cwd = cwdOption instanceof URL ? fileURLToPath(cwdOption) : path.resolve(cwdOption);
const env = envOption === undefined ? undefined : {...process.env, ...envOption};
const [stdioOption, input] = stdio[0]?.string === undefined
? [stdio]
: [['pipe', ...stdio.slice(1)], stdio[0].string];
const input = stdio[0]?.string;
return {
...options,
stdio: stdioOption,
input,
stdio: input === undefined ? stdio : ['pipe', ...stdio.slice(1)],
env: preferLocal ? addLocalPath(env ?? process.env, cwd) : env,
cwd,
};
Expand Down
4 changes: 2 additions & 2 deletions test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {red} from 'yoctocolors';
import nanoSpawn from '../source/index.js';
import {testString} from './helpers/arguments.js';
import {assertDurationMs} from './helpers/assert.js';
import {nodePrint, nodePrintStdout} from './helpers/commands.js';
import {nodePrint, nodePrintFail, nodePrintStdout} from './helpers/commands.js';

test('result.command does not quote normal arguments', async t => {
const {command} = await nanoSpawn('node', ['--version']);
Expand All @@ -27,6 +27,6 @@ test('result.durationMs is set', async t => {
});

test('error.durationMs is set', async t => {
const {durationMs} = await t.throwsAsync(nanoSpawn('node', ['--unknown']));
const {durationMs} = await t.throwsAsync(nanoSpawn(...nodePrintFail));
assertDurationMs(t, durationMs);
});
11 changes: 4 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import test from 'ava';
import nanoSpawn from '../source/index.js';
import {assertSigterm} from './helpers/assert.js';
import {nodePrintStdout, nodeHanging} from './helpers/commands.js';
import {nodePrintStdout} from './helpers/commands.js';

test('Returns a promise', async t => {
const promise = nanoSpawn(...nodePrintStdout);
Expand All @@ -12,10 +11,8 @@ test('Returns a promise', async t => {
});

test('promise.nodeChildProcess is set', async t => {
const promise = nanoSpawn(...nodeHanging);
const promise = nanoSpawn(...nodePrintStdout);
const nodeChildProcess = await promise.nodeChildProcess;
nodeChildProcess.kill();

const error = await t.throwsAsync(promise);
assertSigterm(t, error);
t.true(Number.isInteger(nodeChildProcess.pid));
await promise;
});
20 changes: 13 additions & 7 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const testArgv0 = async (t, shell) => {
test('Can pass options.argv0', testArgv0, false);
test('Can pass options.argv0, shell', testArgv0, true);

const testCwd = async (t, cwd) => {
const {stdout} = await nanoSpawn(...nodePrint('process.cwd()'), {cwd});
t.is(stdout, fixturesPath.replace(/[\\/]$/, ''));
};

test('Can pass options.cwd string', testCwd, fixturesPath);
test('Can pass options.cwd URL', testCwd, FIXTURES_URL);

const testStdOption = async (t, optionName) => {
const promise = nanoSpawn(...nodePrintStdout, {[optionName]: 'ignore'});
const subprocess = await promise.nodeChildProcess;
Expand Down Expand Up @@ -90,15 +98,13 @@ test('options.stdio string has priority over options.stdout', async t => {
await promise;
});

test('options.stdin can be {string: string}', async t => {
const {stdout} = await nanoSpawn(...nodePassThrough, {stdin: {string: testString}});
const testInput = async (t, options) => {
const {stdout} = await nanoSpawn(...nodePassThrough, options);
t.is(stdout, testString);
});
};

test('options.stdio[0] can be {string: string}', async t => {
const {stdout} = await nanoSpawn(...nodePassThrough, {stdio: [{string: testString}, 'pipe', 'pipe']});
t.is(stdout, testString);
});
test('options.stdin can be {string: string}', testInput, {stdin: {string: testString}});
test('options.stdio[0] can be {string: string}', testInput, {stdio: [{string: testString}, 'pipe', 'pipe']});

const testLocalBinaryExec = async (t, cwd) => {
const {stdout} = await nanoSpawn(...localBinary, {preferLocal: true, cwd});
Expand Down

0 comments on commit 608311f

Please sign in to comment.