Add `cwd` error property (#565)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by homersimpsons · committed by GitHub · parent a228eda · Browse files · 5 files·+34 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+34 added−4 removed
- tests:+12 added−1 removed
- other:+22 added−3 removed
Provenance
- Commit
f57fdec- Tree
4f6593f- Author
homersimpsons <guillaume.alabre@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a228eda
5 files changed+34 added−4 removed
modified:lib/error.js+3 added-1 removed
1234530 unmodified lines3637383939404142434422 unmodified lines67686970717273747512 unmodified linesimport process from 'node:process';import {signalsByName} from 'human-signals';
const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => { if (timedOut) {30 unmodified lines escapedCommand, timedOut, isCanceled, killed, parsed: {options: {timeout}}, parsed: {options: {timeout, cwd = process.cwd()}},}) => { // `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`. // We normalize them to `undefined` exitCode = exitCode === null ? undefined : exitCode;22 unmodified lines error.signal = signal; error.signalDescription = signalDescription; error.stdout = stdout; error.stderr = stderr; error.cwd = cwd;
if (all !== undefined) { error.all = all; }12 unmodified linesmodified:test/error.js+12 added-1 removed
1 unmodified line2 import childProcess from 'node:child_process';3 import {promisify} from 'node:util';4 import test from 'ava';5 import {execa, execaSync} from '../index.js';6-import {setFixtureDir} from './helpers/fixtures-dir.js';6+import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';7 8 const pExec = promisify(childProcess.exec);9 10 setFixtureDir();200 unmodified lines211 test('error.code is defined on failure if applicable', async t => {212 const {code} = await t.throwsAsync(execa('noop.js', {cwd: 1}));213 t.is(code, 'ERR_INVALID_ARG_TYPE');214 });215+216+test('error.cwd is defined on failure if applicable', async t => {217+ const {cwd} = await t.throwsAsync(execa('noop-throw.js', [], {cwd: FIXTURES_DIR}));218+ t.is(cwd, FIXTURES_DIR);219+});220+221+test('error.cwd is undefined on failure if not passed as options', async t => {222+ const expectedCwd = process.cwd();223+ const {cwd} = await t.throwsAsync(execa('noop-throw.js'));224+ t.is(cwd, expectedCwd);225+});modified:index.d.ts+9 added-2 removed
362 unmodified lines363 364 If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.365 */366 signalDescription?: string;367+368+ /**369+ The `cwd` of the command if provided in the command options. Otherwise it is `process.cwd()`.370+ */371+ cwd: string;372 };373 374 export type ExecaSyncReturnValue<StdoutStderrType extends StdoutStderrAll = string> = {375 } & ExecaReturnBase<StdoutStderrType>;224 unmodified lines600 stderr: '',601 failed: true,602 timedOut: false,603 isCanceled: false,599- killed: false604+ killed: false,605+ cwd: '/path/to/cwd'606 }607 \*\/608 }609 ```74 unmodified lines684 stderr: '',685 failed: true,686 timedOut: false,687 isCanceled: false,682- killed: false688+ killed: false,689+ cwd: '/path/to/cwd'690 }691 \*\/692 }693 ```244 unmodified linesmodified:index.test-d.ts+4 added
69 unmodified lines70 expectType<boolean>(unicornsResult.isCanceled);71 expectType<boolean>(unicornsResult.killed);72 expectType<string | undefined>(unicornsResult.signal);73 expectType<string | undefined>(unicornsResult.signalDescription);74+ expectType<string>(unicornsResult.cwd);75 } catch (error: unknown) {76 const execaError = error as ExecaError;77 78 expectType<string>(execaError.message);6 unmodified lines85 expectType<boolean>(execaError.isCanceled);86 expectType<boolean>(execaError.killed);87 expectType<string | undefined>(execaError.signal);88 expectType<string | undefined>(execaError.signalDescription);89+ expectType<string>(execaError.cwd);90 expectType<string>(execaError.shortMessage);91 expectType<string | undefined>(execaError.originalMessage);92 }93 13 unmodified lines107 expectError(unicornsResult.isCanceled);108 expectType<boolean>(unicornsResult.killed);109 expectType<string | undefined>(unicornsResult.signal);110 expectType<string | undefined>(unicornsResult.signalDescription);111+ expectType<string>(unicornsResult.cwd);112 } catch (error: unknown) {113 const execaError = error as ExecaSyncError;114 115 expectType<string>(execaError.message);6 unmodified lines122 expectError(execaError.isCanceled);123 expectType<boolean>(execaError.killed);124 expectType<string | undefined>(execaError.signal);125 expectType<string | undefined>(execaError.signalDescription);126+ expectType<string>(execaError.cwd);127 expectType<string>(execaError.shortMessage);128 expectType<string | undefined>(execaError.originalMessage);129 }130 190 unmodified linesmodified:readme.md+6 added
459 unmodified lines460 A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`.461 462 If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.463 464+#### cwd465+466+Type: `string`467+468+The `cwd` of the command if provided in the [command options](#cwd-1). Otherwise it is `process.cwd()`.469+470 #### message471 472 Type: `string`473 351 unmodified lines