Skip to content
Myr

cwd.js

This file

Path

test/arguments/cwd.js

137 lines · 6.3 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

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 {mkdir, rmdir} 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, fileURLToPath} 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 {assertThrows} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 9–10why, one step back
9import {execa, execaSync} from '../../index.js';
10import {FIXTURES_DIRECTORY, setFixtureDirectory} from '../helpers/fixtures-directory.js';

Fix CI tests (#1207)ehmickybefore adoption5587ae1

line 11why, one step back
11import {majorNodeVersion} from '../helpers/node-version.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–16why, one step back
12
13setFixtureDirectory();1415const isWindows = process.platform === 'win32';16

TweaksSindre Sorhusbefore adoption698a1b8

line 17why, one step back
17const testOptionCwdString = async execaMethod => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 18–19why, one step back
18 const cwd = '/';
19 const {stdout} = await execaMethod('node', ['-p', 'process.cwd()'], {cwd});

TweaksSindre Sorhusbefore adoption698a1b8

line 20why, one step back
20 assert.equal(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–22why, one step back
21};
22

TweaksSindre Sorhusbefore adoption698a1b8

lines 23–24why, one step back
23test('The "cwd" option can be a string', () => testOptionCwdString(execa));
24test('The "cwd" option can be a string - sync', () => testOptionCwdString(execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 25why, one step back
25

TweaksSindre Sorhusbefore adoption698a1b8

line 26why, one step back
26const testOptionCwdUrl = async execaMethod => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 27–29why, one step back
27 const cwd = '/';
28 const cwdUrl = pathToFileURL(cwd);29 const {stdout} = await execaMethod('node', ['-p', 'process.cwd()'], {cwd: cwdUrl});

TweaksSindre Sorhusbefore adoption698a1b8

line 30why, one step back
30 assert.equal(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 31–32why, one step back
31};
32

TweaksSindre Sorhusbefore adoption698a1b8

lines 33–34why, one step back
33test('The "cwd" option can be a URL', () => testOptionCwdUrl(execa));
34test('The "cwd" option can be a URL - sync', () => testOptionCwdUrl(execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 35why, one step back
35

TweaksSindre Sorhusbefore adoption698a1b8

lines 36–37why, one step back
36const testOptionCwdInvalid = execaMethod => {
37 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–41why, one step back
38 execaMethod('empty.js', {cwd: true});
39 }, {message: /The "cwd" option must be a string or a file URL: true/});40};41

TweaksSindre Sorhusbefore adoption698a1b8

lines 42–43why, one step back
42test('The "cwd" option cannot be an invalid type', () => testOptionCwdInvalid(execa));
43test('The "cwd" option cannot be an invalid type - sync', () => testOptionCwdInvalid(execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 44why, one step back
44

TweaksSindre Sorhusbefore adoption698a1b8

line 45why, one step back
45const testErrorCwdDefault = async execaMethod => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 46why, one step back
46 const {cwd} = await execaMethod('empty.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 47why, one step back
47 assert.equal(cwd, process.cwd());

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 48–49why, one step back
48};
49

TweaksSindre Sorhusbefore adoption698a1b8

lines 50–51why, one step back
50test('The "cwd" option defaults to process.cwd()', () => testErrorCwdDefault(execa));
51test('The "cwd" option defaults to process.cwd() - sync', () => testErrorCwdDefault(execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 52–54why, one step back
52
53// Windows does not allow removing a directory used as `cwd` of a running subprocess54if (!isWindows) {

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 55–56why, one step back
55 // Deletes the current directory, which makes `process.cwd()` throw
56 const chdirToMissingDirectory = async t => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 57why, one step back
57 const currentCwd = process.cwd();

TweaksSindre Sorhusbefore adoption698a1b8

line 58why, one step back
58 t.after(() => {

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 59–61why, one step back
59 process.chdir(currentCwd);
60 });61

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 62–65why, one step back
62 const filePath = tempfile();
63 await mkdir(filePath);64 process.chdir(filePath);65 await rmdir(filePath);

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

line 66why, one step back
66 };

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 67why, one step back
67

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 68–70why, one step back
68 const testCwdPreSpawn = async (t, execaMethod) => {
69 await chdirToMissingDirectory(t);70

TweaksSindre Sorhusbefore adoption698a1b8

line 71why, one step back
71 assertThrows(() => {

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 72–73why, one step back
72 execaMethod('empty.js');
73 }, {message: /The current directory does not exist/});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 74–75why, one step back
74 };
75

TweaksSindre Sorhusbefore adoption698a1b8

lines 76–77why, one step back
76 test('The "cwd" option default fails if current cwd is missing', t => testCwdPreSpawn(t, execa));
77 test('The "cwd" option default fails if current cwd is missing - sync', t => testCwdPreSpawn(t, execaSync));

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 78–84why, one step back
78
79 // The error message mentions the current directory, but computing it must not fail when it has been deleted.80 // Otherwise, this would hide the subprocess' own error, and would throw even with the `reject: false` option.81 const testCwdMissingError = async (t, execaMethod) => {82 await chdirToMissingDirectory(t);8384 const {failed, code, message} = await execaMethod('does_not_exist', {cwd: FIXTURES_DIRECTORY, reject: false});

TweaksSindre Sorhusbefore adoption698a1b8

lines 85–87why, one step back
85 assert.equal(failed, true);
86 assert.equal(code, 'ENOENT');87 assert.ok(message.includes('does_not_exist'));

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 88–89why, one step back
88 };
89

TweaksSindre Sorhusbefore adoption698a1b8

lines 90–91why, one step back
90 test('The subprocess error is kept if current cwd is missing', t => testCwdMissingError(t, execa));
91 test('The subprocess error is kept if current cwd is missing - sync', t => testCwdMissingError(t, execaSync));

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 92–97why, one step back
92
93 // The `cwd` option is valid, so the error message must not mention it at all94 const testCwdMissingValidOption = async (t, execaMethod) => {95 await chdirToMissingDirectory(t);9697 const {message} = await execaMethod('does_not_exist', {cwd: FIXTURES_DIRECTORY, reject: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 98why, one step back
98 assert.ok(!message.includes('The "cwd" option'));

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 99–100why, one step back
99 };
100

TweaksSindre Sorhusbefore adoption698a1b8

lines 101–102why, one step back
101 test('The "cwd" option is not reported as invalid if current cwd is missing', t => testCwdMissingValidOption(t, execa));
102 test('The "cwd" option is not reported as invalid if current cwd is missing - sync', t => testCwdMissingValidOption(t, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 103–105why, one step back
103}
104105const cwdNotExisting = {cwd: 'does_not_exist', expectedCode: 'ENOENT', expectedMessage: 'The "cwd" option is invalid'};

Fix CI tests (#1207)ehmickybefore adoption5587ae1

line 106why, one step back
106const cwdTooLong = {cwd: '.'.repeat(1e5), expectedCode: isWindows && majorNodeVersion >= 20 ? 'ENOENT' : 'ENAMETOOLONG', expectedMessage: 'The "cwd" option is invalid'};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 107–109why, one step back
107// @todo: use import.meta.dirname after dropping support for Node <20.11.0
108const cwdNotDirectory = {cwd: fileURLToPath(import.meta.url), expectedCode: isWindows ? 'ENOENT' : 'ENOTDIR', expectedMessage: 'The "cwd" option is not a directory'};109

TweaksSindre Sorhusbefore adoption698a1b8

line 110why, one step back
110const testCwdPostSpawn = async ({cwd, expectedCode, expectedMessage}, execaMethod) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 111why, one step back
111 const {failed, code, message} = await execaMethod('empty.js', {cwd, reject: false});

TweaksSindre Sorhusbefore adoption698a1b8

lines 112–115why, one step back
112 assert.equal(failed, true);
113 assert.equal(code, expectedCode);114 assert.ok(message.includes(expectedMessage));115 assert.ok(message.includes(cwd));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 116–117why, one step back
116};
117

TweaksSindre Sorhusbefore adoption698a1b8

lines 118–123why, one step back
118test('The "cwd" option must be an existing file', () => testCwdPostSpawn(cwdNotExisting, execa));
119test('The "cwd" option must be an existing file - sync', () => testCwdPostSpawn(cwdNotExisting, execaSync));120test('The "cwd" option must not be too long', () => testCwdPostSpawn(cwdTooLong, execa));121test('The "cwd" option must not be too long - sync', () => testCwdPostSpawn(cwdTooLong, execaSync));122test('The "cwd" option must be a directory', () => testCwdPostSpawn(cwdNotDirectory, execa));123test('The "cwd" option must be a directory - sync', () => testCwdPostSpawn(cwdNotDirectory, execaSync));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 124–127why, one step back
124
125const successProperties = {fixtureName: 'empty.js', expectedFailed: false};126const errorProperties = {fixtureName: 'fail.js', expectedFailed: true};127

TweaksSindre Sorhusbefore adoption698a1b8

line 128why, one step back
128const testErrorCwd = async (execaMethod, {fixtureName, expectedFailed}) => {

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 129why, one step back
129 const {failed, cwd} = await execaMethod(fixtureName, {cwd: path.relative('.', FIXTURES_DIRECTORY), reject: false});

TweaksSindre Sorhusbefore adoption698a1b8

lines 130–131why, one step back
130 assert.equal(failed, expectedFailed);
131 assert.equal(cwd, FIXTURES_DIRECTORY);

100% test coverage (#1018)ehmickybefore adoption9f65b86

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 134–137why, one step back
134test('result.cwd is defined', () => testErrorCwd(execa, successProperties));
135test('result.cwd is defined - sync', () => testErrorCwd(execaSync, successProperties));136test('error.cwd is defined', () => testErrorCwd(execa, errorProperties));137test('error.cwd is defined - sync', () => testErrorCwd(execaSync, errorProperties));