Skip to content

Commit

Permalink
Refactor test helpers for the verbose option (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Jun 19, 2024
1 parent 37e0024 commit 18d320f
Show file tree
Hide file tree
Showing 41 changed files with 720 additions and 717 deletions.
9 changes: 4 additions & 5 deletions test/fixtures/nested-double.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa} from '../../index.js';
import {execa, getOneMessage} from '../../index.js';

const [options, file, ...commandArguments] = process.argv.slice(2);
const {file, commandArguments, options} = await getOneMessage();
const firstArguments = commandArguments.slice(0, -1);
const lastArgument = commandArguments.at(-1);
await Promise.all([
execa(file, [...firstArguments, lastArgument], JSON.parse(options)),
execa(file, [...firstArguments, lastArgument.toUpperCase()], JSON.parse(options)),
execa(file, [...firstArguments, lastArgument], options),
execa(file, [...firstArguments, lastArgument.toUpperCase()], options),
]);
7 changes: 3 additions & 4 deletions test/fixtures/nested-fail.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa} from '../../index.js';
import {execa, getOneMessage} from '../../index.js';

const [options, file, ...commandArguments] = process.argv.slice(2);
const subprocess = execa(file, commandArguments, JSON.parse(options));
const {file, commandArguments, options} = await getOneMessage();
const subprocess = execa(file, commandArguments, options);
subprocess.kill(new Error(commandArguments[0]));
await subprocess;
8 changes: 0 additions & 8 deletions test/fixtures/nested-file-url.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/fixtures/nested-input.js

This file was deleted.

25 changes: 13 additions & 12 deletions test/fixtures/nested-pipe-file.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa} from '../../index.js';
import {execa, getOneMessage} from '../../index.js';

const [
sourceOptions,
sourceFile,
sourceArgument,
destinationOptions,
destinationFile,
destinationArgument,
] = process.argv.slice(2);
await execa(sourceFile, [sourceArgument], JSON.parse(sourceOptions))
.pipe(destinationFile, destinationArgument === undefined ? [] : [destinationArgument], JSON.parse(destinationOptions));
const {
file,
commandArguments = [],
options: {
sourceOptions = {},
destinationFile,
destinationArguments = [],
destinationOptions = {},
},
} = await getOneMessage();
await execa(file, commandArguments, sourceOptions)
.pipe(destinationFile, destinationArguments, destinationOptions);
25 changes: 13 additions & 12 deletions test/fixtures/nested-pipe-script.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env node
import process from 'node:process';
import {$} from '../../index.js';
import {$, getOneMessage} from '../../index.js';

const [
sourceOptions,
sourceFile,
sourceArgument,
destinationOptions,
destinationFile,
destinationArgument,
] = process.argv.slice(2);
await $(JSON.parse(sourceOptions))`${sourceFile} ${sourceArgument}`
.pipe(JSON.parse(destinationOptions))`${destinationFile} ${destinationArgument === undefined ? [] : [destinationArgument]}`;
const {
file,
commandArguments = [],
options: {
sourceOptions = {},
destinationFile,
destinationArguments = [],
destinationOptions = {},
},
} = await getOneMessage();
await $(sourceOptions)`${file} ${commandArguments}`
.pipe(destinationOptions)`${destinationFile} ${destinationArguments}`;
8 changes: 4 additions & 4 deletions test/fixtures/nested-pipe-stream.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa} from '../../index.js';
import {execa, getOneMessage} from '../../index.js';

const [options, file, commandArgument, unpipe] = process.argv.slice(2);
const subprocess = execa(file, [commandArgument], JSON.parse(options));
const {file, commandArguments, options: {unpipe, ...options}} = await getOneMessage();
const subprocess = execa(file, commandArguments, options);
subprocess.stdout.pipe(process.stdout);
if (unpipe === 'true') {
if (unpipe) {
subprocess.stdout.unpipe(process.stdout);
}

Expand Down
15 changes: 8 additions & 7 deletions test/fixtures/nested-pipe-subprocess.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa} from '../../index.js';
import {execa, getOneMessage} from '../../index.js';

const [options, file, commandArgument, unpipe] = process.argv.slice(2);
const source = execa(file, [commandArgument], JSON.parse(options));
const {file, commandArguments, options: {unpipe, ...options}} = await getOneMessage();
const source = execa(file, commandArguments, options);
const destination = execa('stdin.js');
const controller = new AbortController();
const pipePromise = source.pipe(destination, {unpipeSignal: controller.signal});
if (unpipe === 'true') {
const subprocess = source.pipe(destination, {unpipeSignal: controller.signal});
if (unpipe) {
controller.abort();
destination.stdin.end();
}

await Promise.allSettled([source, destination, pipePromise]);
try {
await subprocess;
} catch {}
25 changes: 13 additions & 12 deletions test/fixtures/nested-pipe-subprocesses.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa} from '../../index.js';
import {execa, getOneMessage} from '../../index.js';

const [
sourceOptions,
sourceFile,
sourceArgument,
destinationOptions,
destinationFile,
destinationArgument,
] = process.argv.slice(2);
await execa(sourceFile, [sourceArgument], JSON.parse(sourceOptions))
.pipe(execa(destinationFile, destinationArgument === undefined ? [] : [destinationArgument], JSON.parse(destinationOptions)));
const {
file,
commandArguments = [],
options: {
sourceOptions = {},
destinationFile,
destinationArguments = [],
destinationOptions = {},
},
} = await getOneMessage();
await execa(file, commandArguments, sourceOptions)
.pipe(execa(destinationFile, destinationArguments, destinationOptions));
11 changes: 0 additions & 11 deletions test/fixtures/nested-sync.js

This file was deleted.

34 changes: 0 additions & 34 deletions test/fixtures/nested-transform.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/nested-writable-web.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/nested-writable.js

This file was deleted.

35 changes: 29 additions & 6 deletions test/fixtures/nested.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
#!/usr/bin/env node
import process from 'node:process';
import {execa, sendMessage} from '../../index.js';
import {
execa,
execaSync,
getOneMessage,
sendMessage,
} from '../../index.js';

const {
isSync,
file,
commandArguments,
options,
optionsFixture,
optionsInput,
} = await getOneMessage();

let commandOptions = options;

// Some subprocess options cannot be serialized between processes.
// For those, we pass a fixture filename instead, which dynamically creates the options.
if (optionsFixture !== undefined) {
const {getOptions} = await import(`./nested/${optionsFixture}`);
commandOptions = {...commandOptions, ...getOptions({...commandOptions, ...optionsInput})};
}

const [options, file, ...commandArguments] = process.argv.slice(2);
try {
const result = await execa(file, commandArguments, JSON.parse(options));
await sendMessage({result});
const result = isSync
? execaSync(file, commandArguments, commandOptions)
: await execa(file, commandArguments, commandOptions);
await sendMessage(result);
} catch (error) {
await sendMessage({error});
await sendMessage(error);
}
3 changes: 3 additions & 0 deletions test/fixtures/nested/file-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {pathToFileURL} from 'node:url';

export const getOptions = ({stdout: {file}}) => ({stdout: pathToFileURL(file)});
4 changes: 4 additions & 0 deletions test/fixtures/nested/generator-big-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {getOutputGenerator} from '../../helpers/generator.js';

const bigArray = Array.from({length: 100}, (_, index) => index);
export const getOptions = () => ({stdout: getOutputGenerator(bigArray)(true)});
3 changes: 3 additions & 0 deletions test/fixtures/nested/generator-duplex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {uppercaseBufferDuplex} from '../../helpers/duplex.js';

export const getOptions = () => ({stdout: uppercaseBufferDuplex()});
3 changes: 3 additions & 0 deletions test/fixtures/nested/generator-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {outputObjectGenerator} from '../../helpers/generator.js';

export const getOptions = () => ({stdout: outputObjectGenerator()});
4 changes: 4 additions & 0 deletions test/fixtures/nested/generator-string-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {getOutputGenerator} from '../../helpers/generator.js';
import {simpleFull} from '../../helpers/lines.js';

export const getOptions = () => ({stdout: getOutputGenerator(simpleFull)(true)});
3 changes: 3 additions & 0 deletions test/fixtures/nested/generator-uppercase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {uppercaseGenerator} from '../../helpers/generator.js';

export const getOptions = () => ({stdout: uppercaseGenerator()});
1 change: 1 addition & 0 deletions test/fixtures/nested/writable-web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getOptions = () => ({stdout: new WritableStream()});
3 changes: 3 additions & 0 deletions test/fixtures/nested/writable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import process from 'node:process';

export const getOptions = () => ({stdout: process.stdout});
16 changes: 6 additions & 10 deletions test/fixtures/worker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env node
import {workerData, parentPort} from 'node:worker_threads';
import {execa} from '../../index.js';
import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
import {spawnParentProcess} from '../helpers/nested.js';

setFixtureDirectory();

const {nodeFile, commandArguments, options} = workerData;
try {
const subprocess = execa(nodeFile, commandArguments, options);
const [parentResult, {result, error}] = await Promise.all([subprocess, subprocess.getOneMessage()]);
parentPort.postMessage({parentResult, result, error});
} catch (parentError) {
parentPort.postMessage({parentError});
const result = await spawnParentProcess(workerData);
parentPort.postMessage(result);
} catch (error) {
parentPort.postMessage(error);
}
Loading

0 comments on commit 18d320f

Please sign in to comment.