cwd.js
This file
Path
test/arguments/cwd.js
137 lines · 6.3 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 2why, one step back2import {mkdir, rmdir} from 'node:fs/promises';Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 3why, one step back3import path from 'node:path';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 4–5why, one step back4import process from 'node:process';5import {pathToFileURL, fileURLToPath} from 'node:url';TweaksSindre Sorhusbefore adoption698a1b8
line 6why, one step back6import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 7why, one step back7import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 8why, one step back8import {assertThrows} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 9–10why, one step back9import {execa, execaSync} from '../../index.js';10import {FIXTURES_DIRECTORY, setFixtureDirectory} from '../helpers/fixtures-directory.js';Fix CI tests (#1207)ehmickybefore adoption5587ae1
line 11why, one step back11import {majorNodeVersion} from '../helpers/node-version.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–16why, one step back1213setFixtureDirectory();1415const isWindows = process.platform === 'win32';16TweaksSindre Sorhusbefore adoption698a1b8
line 17why, one step back17const testOptionCwdString = async execaMethod => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 18–19why, one step back18 const cwd = '/';19 const {stdout} = await execaMethod('node', ['-p', 'process.cwd()'], {cwd});TweaksSindre Sorhusbefore adoption698a1b8
line 20why, one step back20 assert.equal(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–22why, one step back21};22TweaksSindre Sorhusbefore adoption698a1b8
lines 23–24why, one step back23test('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 back25TweaksSindre Sorhusbefore adoption698a1b8
line 26why, one step back26const testOptionCwdUrl = async execaMethod => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–29why, one step back27 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 back30 assert.equal(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 31–32why, one step back31};32TweaksSindre Sorhusbefore adoption698a1b8
lines 33–34why, one step back33test('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 back35TweaksSindre Sorhusbefore adoption698a1b8
lines 36–37why, one step back36const testOptionCwdInvalid = execaMethod => {37 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 38–41why, one step back38 execaMethod('empty.js', {cwd: true});39 }, {message: /The "cwd" option must be a string or a file URL: true/});40};41TweaksSindre Sorhusbefore adoption698a1b8
lines 42–43why, one step back42test('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 back44TweaksSindre Sorhusbefore adoption698a1b8
line 45why, one step back45const testErrorCwdDefault = async execaMethod => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 46why, one step back46 const {cwd} = await execaMethod('empty.js');TweaksSindre Sorhusbefore adoption698a1b8
line 47why, one step back47 assert.equal(cwd, process.cwd());100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 48–49why, one step back48};49TweaksSindre Sorhusbefore adoption698a1b8
lines 50–51why, one step back50test('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 back5253// 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 back55 // Deletes the current directory, which makes `process.cwd()` throw56 const chdirToMissingDirectory = async t => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 57why, one step back57 const currentCwd = process.cwd();TweaksSindre Sorhusbefore adoption698a1b8
line 58why, one step back58 t.after(() => {Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded
lines 59–61why, one step back59 process.chdir(currentCwd);60 });61100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 62–65why, one step back62 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 back66 };100% test coverage (#1018)ehmickybefore adoption9f65b86
line 67why, one step back67Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded
lines 68–70why, one step back68 const testCwdPreSpawn = async (t, execaMethod) => {69 await chdirToMissingDirectory(t);70TweaksSindre Sorhusbefore adoption698a1b8
line 71why, one step back71 assertThrows(() => {Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded
lines 72–73why, one step back72 execaMethod('empty.js');73 }, {message: /The current directory does not exist/});100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 74–75why, one step back74 };75TweaksSindre Sorhusbefore adoption698a1b8
lines 76–77why, one step back76 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 back7879 // 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 back85 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 back88 };89TweaksSindre Sorhusbefore adoption698a1b8
lines 90–91why, one step back90 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 back9293 // 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 back98 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 back99 };100TweaksSindre Sorhusbefore adoption698a1b8
lines 101–102why, one step back101 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 back103}104105const cwdNotExisting = {cwd: 'does_not_exist', expectedCode: 'ENOENT', expectedMessage: 'The "cwd" option is invalid'};Fix CI tests (#1207)ehmickybefore adoption5587ae1
line 106why, one step back106const 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 back107// @todo: use import.meta.dirname after dropping support for Node <20.11.0108const cwdNotDirectory = {cwd: fileURLToPath(import.meta.url), expectedCode: isWindows ? 'ENOENT' : 'ENOTDIR', expectedMessage: 'The "cwd" option is not a directory'};109TweaksSindre Sorhusbefore adoption698a1b8
line 110why, one step back110const testCwdPostSpawn = async ({cwd, expectedCode, expectedMessage}, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 111why, one step back111 const {failed, code, message} = await execaMethod('empty.js', {cwd, reject: false});TweaksSindre Sorhusbefore adoption698a1b8
lines 112–115why, one step back112 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 back116};117TweaksSindre Sorhusbefore adoption698a1b8
lines 118–123why, one step back118test('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 back124125const successProperties = {fixtureName: 'empty.js', expectedFailed: false};126const errorProperties = {fixtureName: 'fail.js', expectedFailed: true};127TweaksSindre Sorhusbefore adoption698a1b8
line 128why, one step back128const testErrorCwd = async (execaMethod, {fixtureName, expectedFailed}) => {Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 129why, one step back129 const {failed, cwd} = await execaMethod(fixtureName, {cwd: path.relative('.', FIXTURES_DIRECTORY), reject: false});TweaksSindre Sorhusbefore adoption698a1b8
lines 130–131why, one step back130 assert.equal(failed, expectedFailed);131 assert.equal(cwd, FIXTURES_DIRECTORY);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 132–133why, one step back132};133TweaksSindre Sorhusbefore adoption698a1b8
lines 134–137why, one step back134test('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));