Skip to content
Myr

file-path-main.js

This file

Path

test/stdio/file-path-main.js

211 lines · 13.0 KiB · JavaScript

Last changed by

Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 2why, one step back
2import {readFile, writeFile, rm} from 'node:fs/promises';

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 3why, one step back
3import path from 'node:path';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 4–5why, one step back
4import process from 'node:process';
5import {pathToFileURL} from 'node:url';

TweaksSindre Sorhusbefore adoption698a1b8

line 6why, one step back
6import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 7why, one step back
7import tempfile from 'tempfile';

TweaksSindre Sorhusbefore adoption698a1b8

line 8why, one step back
8import {assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 9–27why, one step back
9import {execa, execaSync} from '../../index.js';
10import {setFixtureDirectory} from '../helpers/fixtures-directory.js';11import {identity, getStdio} from '../helpers/stdio.js';12import {13 runExeca,14 runExecaSync,15 runScript,16 runScriptSync,17} from '../helpers/run.js';18import {foobarString, foobarUint8Array} from '../helpers/input.js';19import {getAbsolutePath, getRelativePath} from '../helpers/file-path.js';2021setFixtureDirectory();2223const getStdioInput = (fdNumberOrName, file) => {24 if (fdNumberOrName === 'string') {25 return {input: foobarString};26 }27

TweaksSindre Sorhusbefore adoption698a1b8

line 28why, one step back
28 return fdNumberOrName === 'binary' ? {input: foobarUint8Array} : getStdioInputFile(fdNumberOrName, file);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 29–32why, one step back
29};
3031const getStdioInputFile = (fdNumberOrName, file) => getStdio(fdNumberOrName, typeof fdNumberOrName === 'string' ? file : {file});32

TweaksSindre Sorhusbefore adoption698a1b8

line 33why, one step back
33const testStdinFile = async (mapFilePath, fdNumber, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 34–36why, one step back
34 const filePath = tempfile();
35 await writeFile(filePath, foobarString);36 const {stdout} = await execaMethod('stdin.js', getStdio(fdNumber, mapFilePath(filePath)));

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37 assert.equal(stdout, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–40why, one step back
38 await rm(filePath);
39};40

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 53why, one step back
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54const testOutputFile = async (mapFile, fdNumber, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 55–56why, one step back
55 const filePath = tempfile();
56 await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, mapFile(filePath)));

TweaksSindre Sorhusbefore adoption698a1b8

line 57why, one step back
57 assert.equal(await readFile(filePath, 'utf8'), foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 79why, one step back
79

TweaksSindre Sorhusbefore adoption698a1b8

line 80why, one step back
80const testInputFileValidUrl = async (fdNumber, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 81–83why, one step back
81 const filePath = tempfile();
82 await writeFile(filePath, foobarString);83 const currentCwd = process.cwd();

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 84why, one step back
84 process.chdir(path.dirname(filePath));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 85–86why, one step back
85
86 try {

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 87why, one step back
87 const {stdout} = await execaMethod('stdin.js', getStdioInputFile(fdNumber, path.basename(filePath)));

TweaksSindre Sorhusbefore adoption698a1b8

line 88why, one step back
88 assert.equal(stdout, foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 89–94why, one step back
89 } finally {
90 process.chdir(currentCwd);91 await rm(filePath);92 }93};94

TweaksSindre Sorhusbefore adoption698a1b8

lines 95–98why, one step back
95test('inputFile does not need to start with . when being a relative file path', () => testInputFileValidUrl('inputFile', execa));
96test('stdin does not need to start with . when being a relative file path', () => testInputFileValidUrl(0, execa));97test('inputFile does not need to start with . when being a relative file path - sync', () => testInputFileValidUrl('inputFile', execaSync));98test('stdin does not need to start with . when being a relative file path - sync', () => testInputFileValidUrl(0, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 99why, one step back
99

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 101–104why, one step back
101 const filePath = tempfile();
102 await writeFile(filePath, foobarString);103 const options = Object.assign({}, ...indices.map(fdNumber => getStdioInput(fdNumber, filePath)));104 const {stdout} = await execaMethod('stdin.js', options);

TweaksSindre Sorhusbefore adoption698a1b8

line 105why, one step back
105 assert.equal(stdout, foobarString.repeat(indices.length));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 106–108why, one step back
106 await rm(filePath);
107};108

TweaksSindre Sorhusbefore adoption698a1b8

lines 109–126why, one step back
109test('inputFile can be set', () => testMultipleInputs(['inputFile'], runExeca));
110test('inputFile can be set - sync', () => testMultipleInputs(['inputFile'], runExecaSync));111test('inputFile can be set with $', () => testMultipleInputs(['inputFile'], runScript));112test('inputFile can be set with $.sync', () => testMultipleInputs(['inputFile'], runScriptSync));113test('input String and inputFile can be both set', () => testMultipleInputs(['inputFile', 'string'], execa));114test('input String and stdin can be both set', () => testMultipleInputs([0, 'string'], execa));115test('input Uint8Array and inputFile can be both set', () => testMultipleInputs(['inputFile', 'binary'], execa));116test('input Uint8Array and stdin can be both set', () => testMultipleInputs([0, 'binary'], execa));117test('stdin and inputFile can be both set', () => testMultipleInputs([0, 'inputFile'], execa));118test('input String, stdin and inputFile can be all set', () => testMultipleInputs(['inputFile', 0, 'string'], execa));119test('input Uint8Array, stdin and inputFile can be all set', () => testMultipleInputs(['inputFile', 0, 'binary'], execa));120test('input String and inputFile can be both set - sync', () => testMultipleInputs(['inputFile', 'string'], execaSync));121test('input String and stdin can be both set - sync', () => testMultipleInputs([0, 'string'], execaSync));122test('input Uint8Array and inputFile can be both set - sync', () => testMultipleInputs(['inputFile', 'binary'], execaSync));123test('input Uint8Array and stdin can be both set - sync', () => testMultipleInputs([0, 'binary'], execaSync));124test('stdin and inputFile can be both set - sync', () => testMultipleInputs([0, 'inputFile'], execaSync));125test('input String, stdin and inputFile can be all set - sync', () => testMultipleInputs(['inputFile', 0, 'string'], execaSync));126test('input Uint8Array, stdin and inputFile can be all set - sync', () => testMultipleInputs(['inputFile', 0, 'binary'], execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 127why, one step back
127

TweaksSindre Sorhusbefore adoption698a1b8

line 128why, one step back
128const testMultipleOutputs = async (mapFile, fdNumber, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 129–131why, one step back
129 const filePath = tempfile();
130 const filePathTwo = tempfile();131 await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, [mapFile(filePath), mapFile(filePathTwo)]));

TweaksSindre Sorhusbefore adoption698a1b8

lines 132–133why, one step back
132 assert.equal(await readFile(filePath, 'utf8'), foobarString);
133 assert.equal(await readFile(filePathTwo, 'utf8'), foobarString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 134–136why, one step back
134 await Promise.all([rm(filePath), rm(filePathTwo)]);
135};136

TweaksSindre Sorhusbefore adoption698a1b8

lines 137–148why, one step back
137test('stdout can be two file URLs', () => testMultipleOutputs(pathToFileURL, 1, execa));
138test('stdout can be two file paths', () => testMultipleOutputs(getAbsolutePath, 1, execa));139test('stdout can be two file URLs - sync', () => testMultipleOutputs(pathToFileURL, 1, execaSync));140test('stdout can be two file paths - sync', () => testMultipleOutputs(getAbsolutePath, 1, execaSync));141test('stderr can be two file URLs', () => testMultipleOutputs(pathToFileURL, 2, execa));142test('stderr can be two file paths', () => testMultipleOutputs(getAbsolutePath, 2, execa));143test('stderr can be two file URLs - sync', () => testMultipleOutputs(pathToFileURL, 2, execaSync));144test('stderr can be two file paths - sync', () => testMultipleOutputs(getAbsolutePath, 2, execaSync));145test('stdio[*] can be two file URLs', () => testMultipleOutputs(pathToFileURL, 3, execa));146test('stdio[*] can be two file paths', () => testMultipleOutputs(getAbsolutePath, 3, execa));147test('stdio[*] can be two file URLs - sync', () => testMultipleOutputs(pathToFileURL, 3, execaSync));148test('stdio[*] can be two file paths - sync', () => testMultipleOutputs(getAbsolutePath, 3, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 149why, one step back
149

TweaksSindre Sorhusbefore adoption698a1b8

line 150why, one step back
150const testInputFileHanging = async mapFilePath => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 151–152why, one step back
151 const filePath = tempfile();
152 await writeFile(filePath, foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 153why, one step back
153 await assertRejects(execa('stdin.js', {stdin: mapFilePath(filePath), timeout: 1}), {message: /timed out/});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 154–156why, one step back
154 await rm(filePath);
155};156

TweaksSindre Sorhusbefore adoption698a1b8

lines 157–158why, one step back
157test('Passing an input file path when subprocess exits does not make promise hang', () => testInputFileHanging(getAbsolutePath));
158test('Passing an input file URL when subprocess exits does not make promise hang', () => testInputFileHanging(pathToFileURL));

Allow appending to files (#1166)ehmickybefore adoption94a75df

line 159why, one step back
159

TweaksSindre Sorhusbefore adoption698a1b8

line 160why, one step back
160const testOverwriteFile = async (fdNumber, execaMethod, append) => {

Allow appending to files (#1166)ehmickybefore adoption94a75df

lines 161–163why, one step back
161 const filePath = tempfile();
162 await writeFile(filePath, '.');163 await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, {file: filePath, append}));

TweaksSindre Sorhusbefore adoption698a1b8

line 164why, one step back
164 assert.equal(await readFile(filePath, 'utf8'), foobarString);

Allow appending to files (#1166)ehmickybefore adoption94a75df

lines 165–167why, one step back
165 await rm(filePath);
166};167

TweaksSindre Sorhusbefore adoption698a1b8

lines 168–179why, one step back
168test('Overwrite by default to stdout', () => testOverwriteFile(1, execa, undefined));
169test('Overwrite by default to stderr', () => testOverwriteFile(2, execa, undefined));170test('Overwrite by default to stdio[*]', () => testOverwriteFile(3, execa, undefined));171test('Overwrite by default to stdout - sync', () => testOverwriteFile(1, execaSync, undefined));172test('Overwrite by default to stderr - sync', () => testOverwriteFile(2, execaSync, undefined));173test('Overwrite by default to stdio[*] - sync', () => testOverwriteFile(3, execaSync, undefined));174test('Overwrite with append false to stdout', () => testOverwriteFile(1, execa, false));175test('Overwrite with append false to stderr', () => testOverwriteFile(2, execa, false));176test('Overwrite with append false to stdio[*]', () => testOverwriteFile(3, execa, false));177test('Overwrite with append false to stdout - sync', () => testOverwriteFile(1, execaSync, false));178test('Overwrite with append false to stderr - sync', () => testOverwriteFile(2, execaSync, false));179test('Overwrite with append false to stdio[*] - sync', () => testOverwriteFile(3, execaSync, false));

Allow appending to files (#1166)ehmickybefore adoption94a75df

line 180why, one step back
180

TweaksSindre Sorhusbefore adoption698a1b8

line 181why, one step back
181const testAppendFile = async (fdNumber, execaMethod) => {

Allow appending to files (#1166)ehmickybefore adoption94a75df

lines 182–184why, one step back
182 const filePath = tempfile();
183 await writeFile(filePath, '.');184 await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, {file: filePath, append: true}));

TweaksSindre Sorhusbefore adoption698a1b8

line 185why, one step back
185 assert.equal(await readFile(filePath, 'utf8'), `.${foobarString}`);

Allow appending to files (#1166)ehmickybefore adoption94a75df

lines 186–188why, one step back
186 await rm(filePath);
187};188

TweaksSindre Sorhusbefore adoption698a1b8

lines 189–194why, one step back
189test('Can append to stdout', () => testAppendFile(1, execa));
190test('Can append to stderr', () => testAppendFile(2, execa));191test('Can append to stdio[*]', () => testAppendFile(3, execa));192test('Can append to stdout - sync', () => testAppendFile(1, execaSync));193test('Can append to stderr - sync', () => testAppendFile(2, execaSync));194test('Can append to stdio[*] - sync', () => testAppendFile(3, execaSync));

Fix `execaSync()` writing decoded text instead of the output's bytes to filesSindre Sorhusbefore adoptionaf6264a

lines 195–211why, one step back
195
196// The file receives the subprocess' bytes as is, whatever the `encoding` option, which only applies to `result.stdout`197const nonUtf8Bytes = new Uint8Array([0x61, 0x00, 0xE9, 0x00, 0xFF, 0x0A]);198199const testOutputFileEncoding = async (encoding, execaMethod) => {200 const filePath = tempfile();201 await execaMethod('stdin.js', {input: nonUtf8Bytes, stdout: {file: filePath}, encoding});202 assert.deepEqual(new Uint8Array(await readFile(filePath)), nonUtf8Bytes);203 await rm(filePath);204};205206test('Output files are not modified by encoding "utf8"', () => testOutputFileEncoding('utf8', execa));207test('Output files are not modified by encoding "utf16le"', () => testOutputFileEncoding('utf16le', execa));208test('Output files are not modified by encoding "hex"', () => testOutputFileEncoding('hex', execa));209test('Output files are not modified by encoding "utf8" - sync', () => testOutputFileEncoding('utf8', execaSync));210test('Output files are not modified by encoding "utf16le" - sync', () => testOutputFileEncoding('utf16le', execaSync));211test('Output files are not modified by encoding "hex" - sync', () => testOutputFileEncoding('hex', execaSync));