Skip to content
Myr

file-path.js

This file

Path

test/stdio/file-path.js

203 lines · 11.6 KiB · JavaScript

Last changed by

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

History of this file

Take it

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 1–6why, one step back
1import {readFile, writeFile, rm} from 'node:fs/promises';
2import {relative, dirname, basename} from 'node:path';3import process from 'node:process';4import {pathToFileURL} from 'node:url';5import test from 'ava';6import tempfile from 'tempfile';

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 7why, one step back
7import {execa, execaSync} from '../../index.js';

Refactor tests (#628)ehmickybefore adoption30ecc81

line 8why, one step back
8import {setFixtureDir} from '../helpers/fixtures-dir.js';

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 9–10why, one step back
9import {identity, getStdio} from '../helpers/stdio.js';
10import {runExeca, runExecaSync, runScript, runScriptSync} from '../helpers/run.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 11why, one step back
11import {foobarUint8Array} from '../helpers/input.js';

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 12–16why, one step back
12
13setFixtureDir();1415const nonFileUrl = new URL('https://example.com');16

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 17–18why, one step back
17const getAbsolutePath = file => ({file});
18const getRelativePath = filePath => ({file: relative('.', filePath)});

Refactor tests (#628)ehmickybefore adoption30ecc81

line 19why, one step back
19

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 20–27why, one step back
20const getStdioFile = (index, file) => getStdio(index, index === 0 ? {file} : file);
2122const getStdioInput = (index, file) => {23 if (index === 'string') {24 return {input: 'foobar'};25 }2627 if (index === 'binary') {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 28why, one step back
28 return {input: foobarUint8Array};

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 29–34why, one step back
29 }
3031 return getStdioFile(index, file);32};3334const testStdinFile = async (t, mapFilePath, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 35–36why, one step back
35 const filePath = tempfile();
36 await writeFile(filePath, 'foobar');

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 37why, one step back
37 const {stdout} = await execaMethod('stdin.js', getStdio(index, mapFilePath(filePath)));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 38–41why, one step back
38 t.is(stdout, 'foobar');
39 await rm(filePath);40};41

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 42–53why, one step back
42test('inputFile can be a file URL', testStdinFile, pathToFileURL, 'inputFile', execa);
43test('stdin can be a file URL', testStdinFile, pathToFileURL, 0, execa);44test('inputFile can be an absolute file path', testStdinFile, identity, 'inputFile', execa);45test('stdin can be an absolute file path', testStdinFile, getAbsolutePath, 0, execa);46test('inputFile can be a relative file path', testStdinFile, identity, 'inputFile', execa);47test('stdin can be a relative file path', testStdinFile, getRelativePath, 0, execa);48test('inputFile can be a file URL - sync', testStdinFile, pathToFileURL, 'inputFile', execaSync);49test('stdin can be a file URL - sync', testStdinFile, pathToFileURL, 0, execaSync);50test('inputFile can be an absolute file path - sync', testStdinFile, identity, 'inputFile', execaSync);51test('stdin can be an absolute file path - sync', testStdinFile, getAbsolutePath, 0, execaSync);52test('inputFile can be a relative file path - sync', testStdinFile, identity, 'inputFile', execaSync);53test('stdin can be a relative file path - sync', testStdinFile, getRelativePath, 0, execaSync);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 54why, one step back
54

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 55why, one step back
55const testOutputFile = async (t, mapFile, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 56why, one step back
56 const filePath = tempfile();

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 57–58why, one step back
57 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, mapFile(filePath)));
58 t.is(await readFile(filePath, 'utf8'), 'foobar');

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 59–61why, one step back
59 await rm(filePath);
60};61

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 62–79why, one step back
62test('stdout can be a file URL', testOutputFile, pathToFileURL, 1, execa);
63test('stderr can be a file URL', testOutputFile, pathToFileURL, 2, execa);64test('stdio[*] can be a file URL', testOutputFile, pathToFileURL, 3, execa);65test('stdout can be an absolute file path', testOutputFile, getAbsolutePath, 1, execa);66test('stderr can be an absolute file path', testOutputFile, getAbsolutePath, 2, execa);67test('stdio[*] can be an absolute file path', testOutputFile, getAbsolutePath, 3, execa);68test('stdout can be a relative file path', testOutputFile, getRelativePath, 1, execa);69test('stderr can be a relative file path', testOutputFile, getRelativePath, 2, execa);70test('stdio[*] can be a relative file path', testOutputFile, getRelativePath, 3, execa);71test('stdout can be a file URL - sync', testOutputFile, pathToFileURL, 1, execaSync);72test('stderr can be a file URL - sync', testOutputFile, pathToFileURL, 2, execaSync);73test('stdio[*] can be a file URL - sync', testOutputFile, pathToFileURL, 3, execaSync);74test('stdout can be an absolute file path - sync', testOutputFile, getAbsolutePath, 1, execaSync);75test('stderr can be an absolute file path - sync', testOutputFile, getAbsolutePath, 2, execaSync);76test('stdio[*] can be an absolute file path - sync', testOutputFile, getAbsolutePath, 3, execaSync);77test('stdout can be a relative file path - sync', testOutputFile, getRelativePath, 1, execaSync);78test('stderr can be a relative file path - sync', testOutputFile, getRelativePath, 2, execaSync);79test('stdio[*] can be a relative file path - sync', testOutputFile, getRelativePath, 3, execaSync);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 80why, one step back
80

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 81why, one step back
81const testStdioNonFileUrl = (t, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 82why, one step back
82 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 83why, one step back
83 execaMethod('empty.js', getStdio(index, nonFileUrl));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 84–86why, one step back
84 }, {message: /pathToFileURL/});
85};86

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 87–96why, one step back
87test('inputFile cannot be a non-file URL', testStdioNonFileUrl, 'inputFile', execa);
88test('stdin cannot be a non-file URL', testStdioNonFileUrl, 0, execa);89test('stdout cannot be a non-file URL', testStdioNonFileUrl, 1, execa);90test('stderr cannot be a non-file URL', testStdioNonFileUrl, 2, execa);91test('stdio[*] cannot be a non-file URL', testStdioNonFileUrl, 3, execa);92test('inputFile cannot be a non-file URL - sync', testStdioNonFileUrl, 'inputFile', execaSync);93test('stdin cannot be a non-file URL - sync', testStdioNonFileUrl, 0, execaSync);94test('stdout cannot be a non-file URL - sync', testStdioNonFileUrl, 1, execaSync);95test('stderr cannot be a non-file URL - sync', testStdioNonFileUrl, 2, execaSync);96test('stdio[*] cannot be a non-file URL - sync', testStdioNonFileUrl, 3, execaSync);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 97why, one step back
97

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 98–99why, one step back
98const testInvalidInputFile = (t, execaMethod) => {
99 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 100why, one step back
100 execaMethod('empty.js', getStdio('inputFile', false));

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 101–106why, one step back
101 }, {message: /a file path string or a file URL/});
102};103104test('inputFile must be a file URL or string', testInvalidInputFile, execa);105test('inputFile must be a file URL or string - sync', testInvalidInputFile, execaSync);106

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 107why, one step back
107const testInputFileValidUrl = async (t, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 108–113why, one step back
108 const filePath = tempfile();
109 await writeFile(filePath, 'foobar');110 const currentCwd = process.cwd();111 process.chdir(dirname(filePath));112113 try {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 114why, one step back
114 const {stdout} = await execaMethod('stdin.js', getStdioFile(index, basename(filePath)));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 115–121why, one step back
115 t.is(stdout, 'foobar');
116 } finally {117 process.chdir(currentCwd);118 await rm(filePath);119 }120};121

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 122–125why, one step back
122test.serial('inputFile does not need to start with . when being a relative file path', testInputFileValidUrl, 'inputFile', execa);
123test.serial('stdin does not need to start with . when being a relative file path', testInputFileValidUrl, 0, execa);124test.serial('inputFile does not need to start with . when being a relative file path - sync', testInputFileValidUrl, 'inputFile', execaSync);125test.serial('stdin does not need to start with . when being a relative file path - sync', testInputFileValidUrl, 0, execaSync);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 126why, one step back
126

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 127why, one step back
127const testFilePathObject = (t, index, execaMethod) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 128why, one step back
128 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 129why, one step back
129 execaMethod('empty.js', getStdio(index, 'foobar'));

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 130why, one step back
130 }, {message: /must be used/});

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 131–132why, one step back
131};
132

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 133–140why, one step back
133test('stdin must be an object when it is a file path string', testFilePathObject, 0, execa);
134test('stdout must be an object when it is a file path string', testFilePathObject, 1, execa);135test('stderr must be an object when it is a file path string', testFilePathObject, 2, execa);136test('stdio[*] must be an object when it is a file path string', testFilePathObject, 3, execa);137test('stdin be an object when it is a file path string - sync', testFilePathObject, 0, execaSync);138test('stdout be an object when it is a file path string - sync', testFilePathObject, 1, execaSync);139test('stderr be an object when it is a file path string - sync', testFilePathObject, 2, execaSync);140test('stdio[*] must be an object when it is a file path string - sync', testFilePathObject, 3, execaSync);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 141why, one step back
141

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 142why, one step back
142const testFileError = async (t, mapFile, index) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 143why, one step back
143 await t.throwsAsync(

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 144why, one step back
144 execa('empty.js', getStdio(index, mapFile('./unknown/file'))),

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 145–148why, one step back
145 {code: 'ENOENT'},
146 );147};148

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 149–158why, one step back
149test('inputFile file URL errors should be handled', testFileError, pathToFileURL, 'inputFile');
150test('stdin file URL errors should be handled', testFileError, pathToFileURL, 0);151test('stdout file URL errors should be handled', testFileError, pathToFileURL, 1);152test('stderr file URL errors should be handled', testFileError, pathToFileURL, 2);153test('stdio[*] file URL errors should be handled', testFileError, pathToFileURL, 3);154test('inputFile file path errors should be handled', testFileError, identity, 'inputFile');155test('stdin file path errors should be handled', testFileError, getAbsolutePath, 0);156test('stdout file path errors should be handled', testFileError, getAbsolutePath, 1);157test('stderr file path errors should be handled', testFileError, getAbsolutePath, 2);158test('stdio[*] file path errors should be handled', testFileError, getAbsolutePath, 3);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 159why, one step back
159

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 160why, one step back
160const testFileErrorSync = (t, mapFile, index) => {

Refactor tests (#628)ehmickybefore adoption30ecc81

line 161why, one step back
161 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 162why, one step back
162 execaSync('empty.js', getStdio(index, mapFile('./unknown/file')));

Refactor tests (#628)ehmickybefore adoption30ecc81

lines 163–165why, one step back
163 }, {code: 'ENOENT'});
164};165

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 166–175why, one step back
166test('inputFile file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 'inputFile');
167test('stdin file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 0);168test('stdout file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 1);169test('stderr file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 2);170test('stdio[*] file URL errors should be handled - sync', testFileErrorSync, pathToFileURL, 3);171test('inputFile file path errors should be handled - sync', testFileErrorSync, identity, 'inputFile');172test('stdin file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 0);173test('stdout file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 1);174test('stderr file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 2);175test('stdio[*] file path errors should be handled - sync', testFileErrorSync, getAbsolutePath, 3);

Refactor tests (#628)ehmickybefore adoption30ecc81

line 176why, one step back
176

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 177why, one step back
177const testMultipleInputs = async (t, indices, execaMethod) => {

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 178–179why, one step back
178 const filePath = tempfile();
179 await writeFile(filePath, 'foobar');

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 180why, one step back
180 const options = Object.assign({}, ...indices.map(index => getStdioInput(index, filePath)));

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 181why, one step back
181 const {stdout} = await execaMethod('stdin.js', options);

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 182why, one step back
182 t.is(stdout, 'foobar'.repeat(indices.length));

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 183–185why, one step back
183 await rm(filePath);
184};185

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 186–203why, one step back
186test('inputFile can be set', testMultipleInputs, ['inputFile'], runExeca);
187test('inputFile can be set - sync', testMultipleInputs, ['inputFile'], runExecaSync);188test('inputFile can be set with $', testMultipleInputs, ['inputFile'], runScript);189test('inputFile can be set with $.sync', testMultipleInputs, ['inputFile'], runScriptSync);190test('input String and inputFile can be both set', testMultipleInputs, ['inputFile', 'string'], execa);191test('input String and stdin can be both set', testMultipleInputs, [0, 'string'], execa);192test('input Uint8Array and inputFile can be both set', testMultipleInputs, ['inputFile', 'binary'], execa);193test('input Uint8Array and stdin can be both set', testMultipleInputs, [0, 'binary'], execa);194test('stdin and inputFile can be both set', testMultipleInputs, [0, 'inputFile'], execa);195test('input String, stdin and inputFile can be all set', testMultipleInputs, ['inputFile', 0, 'string'], execa);196test('input Uint8Array, stdin and inputFile can be all set', testMultipleInputs, ['inputFile', 0, 'binary'], execa);197test('input String and inputFile can be both set - sync', testMultipleInputs, ['inputFile', 'string'], execaSync);198test('input String and stdin can be both set - sync', testMultipleInputs, [0, 'string'], execaSync);199test('input Uint8Array and inputFile can be both set - sync', testMultipleInputs, ['inputFile', 'binary'], execaSync);200test('input Uint8Array and stdin can be both set - sync', testMultipleInputs, [0, 'binary'], execaSync);201test('stdin and inputFile can be both set - sync', testMultipleInputs, [0, 'inputFile'], execaSync);202test('input String, stdin and inputFile can be all set - sync', testMultipleInputs, ['inputFile', 0, 'string'], execaSync);203test('input Uint8Array, stdin and inputFile can be all set - sync', testMultipleInputs, ['inputFile', 0, 'binary'], execaSync);