error.js
This file
Path
test/error.js
225 lines · 7.0 KiB · JavaScript
Last changed by
Add `cwd` error property (#565)homersimpsonsbefore adoptionf57fdec
History of this fileTake it
Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 1–3why, one step back1import process from 'node:process';2import childProcess from 'node:child_process';3import {promisify} from 'node:util';Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 4why, one step back4import test from 'ava';Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 5why, one step back5import {execa, execaSync} from '../index.js';Add `cwd` error property (#565)homersimpsonsbefore adoptionf57fdec
line 6why, one step back6import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 7why, one step back7Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 8why, one step back8const pExec = promisify(childProcess.exec);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 9why, one step back9Re-enable Windows in CI (#500)ehmickybefore adoption071d0a2
line 10why, one step back10setFixtureDir();Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 11why, one step back11Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 12–16why, one step back12const TIMEOUT_REGEXP = /timed out after/;1314const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);1516test('stdout/stderr/all available on errors', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 17why, one step back17 const {stdout, stderr, all} = await t.throwsAsync(execa('exit.js', ['2'], {all: true}), {message: getExitRegExp('2')});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 18–22why, one step back18 t.is(typeof stdout, 'string');19 t.is(typeof stderr, 'string');20 t.is(typeof all, 'string');21});22Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 23–25why, one step back23const WRONG_COMMAND = process.platform === 'win32'24 ? '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.'25 : '';Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 26–27why, one step back2627test('stdout/stderr/all on process errors', async t => {Add `all` option (#353)Tiago Nápolibefore adoption56b749d
line 28why, one step back28 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command', {all: true}));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 29–35why, one step back29 t.is(stdout, '');30 t.is(stderr, WRONG_COMMAND);31 t.is(all, WRONG_COMMAND);32});3334test('stdout/stderr/all on process errors, in sync mode', t => {35 const {stdout, stderr, all} = t.throws(() => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 36why, one step back36 execaSync('wrong command');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 37–42why, one step back37 });38 t.is(stdout, '');39 t.is(stderr, WRONG_COMMAND);40 t.is(all, undefined);41});42Fix error properties (#375)ehmickybefore adoptionc1b511b
line 43why, one step back43test('exitCode is 0 on success', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 44why, one step back44 const {exitCode} = await execa('noop.js', ['foo']);Fix error properties (#375)ehmickybefore adoptionc1b511b
line 45why, one step back45 t.is(exitCode, 0);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 46–47why, one step back46});47Upgrade dependenciesSindre Sorhusbefore adoption5d64878
line 48why, one step back48const testExitCode = async (t, number) => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 49why, one step back49 const {exitCode} = await t.throwsAsync(execa('exit.js', [`${number}`]), {message: getExitRegExp(number)});Upgrade dependenciesSindre Sorhusbefore adoption5d64878
line 50why, one step back50 t.is(exitCode, number);Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 51–57why, one step back51};5253test('exitCode is 2', testExitCode, 2);54test('exitCode is 3', testExitCode, 3);55test('exitCode is 4', testExitCode, 4);5657test('error.message contains the command', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 58why, one step back58 await t.throwsAsync(execa('exit.js', ['2', 'foo', 'bar']), {message: /exit.js 2 foo bar/});Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9
lines 59–61why, one step back59});6061test('error.message contains stdout/stderr if available', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 62why, one step back62 const {message} = await t.throwsAsync(execa('echo-fail.js'));Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9
lines 63–67why, one step back63 t.true(message.includes('stderr'));64 t.true(message.includes('stdout'));65});6667test('error.message does not contain stdout/stderr if not available', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 68why, one step back68 const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: 'ignore'}));Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9
lines 69–73why, one step back69 t.false(message.includes('stderr'));70 t.false(message.includes('stdout'));71});7273test('error.shortMessage does not contain stdout/stderr', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 74why, one step back74 const {shortMessage} = await t.throwsAsync(execa('echo-fail.js'));Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9
lines 75–76why, one step back75 t.false(shortMessage.includes('stderr'));76 t.false(shortMessage.includes('stdout'));Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 77–79why, one step back77});7879test('Original error.message is kept', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 80why, one step back80 const {originalMessage} = await t.throwsAsync(execa('noop.js', {cwd: 1}));Drop support for Node 12 (#497)ehmickybefore adoptiona09cbc0
line 81why, one step back81 t.true(originalMessage.startsWith('The "options.cwd" property must be of type string or an instance of Buffer or URL. Received type number'));Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 82–84why, one step back82});8384test('failed is false on success', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 85why, one step back85 const {failed} = await execa('noop.js', ['foo']);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 86–88why, one step back86 t.false(failed);87});88Fix error properties (#375)ehmickybefore adoptionc1b511b
line 89why, one step back89test('failed is true on failure', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 90why, one step back90 const {failed} = await t.throwsAsync(execa('exit.js', ['2']));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 91–94why, one step back91 t.true(failed);92});9394test('error.killed is true if process was killed directly', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 95why, one step back95 const subprocess = execa('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 96why, one step back96Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 97why, one step back97 subprocess.kill();Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 98why, one step back98Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 99why, one step back99 const {killed} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 100–103why, one step back100 t.true(killed);101});102103test('error.killed is false if process was killed indirectly', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 104why, one step back104 const subprocess = execa('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 105why, one step back105Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 106why, one step back106 process.kill(subprocess.pid, 'SIGINT');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 107–109why, one step back107108 // `process.kill()` is emulated by Node.js on Windows109 const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 110why, one step back110 const {killed} = await t.throwsAsync(subprocess, {message});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 111–114why, one step back111 t.false(killed);112});113114test('result.killed is false if not killed', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 115why, one step back115 const {killed} = await execa('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 116–119why, one step back116 t.false(killed);117});118119test('result.killed is false if not killed, in sync mode', t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 120why, one step back120 const {killed} = execaSync('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 121–130why, one step back121 t.false(killed);122});123124test('result.killed is false on process error', async t => {125 const {killed} = await t.throwsAsync(execa('wrong command'));126 t.false(killed);127});128129test('result.killed is false on process error, in sync mode', t => {130 const {killed} = t.throws(() => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 131why, one step back131 execaSync('wrong command');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 132–136why, one step back132 });133 t.false(killed);134});135136if (process.platform === 'darwin') {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 137–138why, one step back137 test('sanity check: child_process.exec also has killed.false if killed indirectly', async t => {138 const promise = pExec('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 139why, one step back139Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 140–144why, one step back140 process.kill(promise.child.pid, 'SIGINT');141142 const error = await t.throwsAsync(promise);143 t.truthy(error);144 t.false(error.killed);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 145–149why, one step back145 });146}147148if (process.platform !== 'win32') {149 test('error.signal is SIGINT', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 150why, one step back150 const subprocess = execa('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 151why, one step back151Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 152why, one step back152 process.kill(subprocess.pid, 'SIGINT');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 153why, one step back153Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 154why, one step back154 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGINT/});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 155–157why, one step back155 t.is(signal, 'SIGINT');156 });157Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f
line 158why, one step back158 test('error.signalDescription is defined', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 159why, one step back159 const subprocess = execa('noop.js');Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f
lines 160–166why, one step back160161 process.kill(subprocess.pid, 'SIGINT');162163 const {signalDescription} = await t.throwsAsync(subprocess, {message: /User interruption with CTRL-C/});164 t.is(signalDescription, 'User interruption with CTRL-C');165 });166Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 167why, one step back167 test('error.signal is SIGTERM', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 168why, one step back168 const subprocess = execa('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 169why, one step back169Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 170why, one step back170 process.kill(subprocess.pid, 'SIGTERM');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 171why, one step back171Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 172why, one step back172 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 173–176why, one step back173 t.is(signal, 'SIGTERM');174 });175176 test('custom error.signal', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 177why, one step back177 const {signal} = await t.throwsAsync(execa('noop.js', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 178–179why, one step back178 t.is(signal, 'SIGHUP');179 });Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 180–181why, one step back180181 test('exitCode is undefined on signal termination', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 182why, one step back182 const subprocess = execa('noop.js');Fix error properties (#375)ehmickybefore adoptionc1b511b
line 183why, one step back183Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 184why, one step back184 process.kill(subprocess.pid);Fix error properties (#375)ehmickybefore adoptionc1b511b
line 185why, one step back185Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 186why, one step back186 const {exitCode} = await t.throwsAsync(subprocess);Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 187–188why, one step back187 t.is(exitCode, undefined);188 });Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 189–191why, one step back189}190191test('result.signal is undefined for successful execution', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 192why, one step back192 const {signal} = await execa('noop.js');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 193–196why, one step back193 t.is(signal, undefined);194});195196test('result.signal is undefined if process failed, but was not killed', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 197why, one step back197 const {signal} = await t.throwsAsync(execa('exit.js', [2]), {message: getExitRegExp('2')});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 198why, one step back198 t.is(signal, undefined);Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f
lines 199–201why, one step back199});200201test('result.signalDescription is undefined for successful execution', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 202why, one step back202 const {signalDescription} = await execa('noop.js');Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f
line 203why, one step back203 t.is(signalDescription, undefined);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 204–205why, one step back204});205Fix error properties (#375)ehmickybefore adoptionc1b511b
line 206why, one step back206test('error.code is undefined on success', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 207why, one step back207 const {code} = await execa('noop.js');Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 208–209why, one step back208 t.is(code, undefined);209});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 210why, one step back210Fix error properties (#375)ehmickybefore adoptionc1b511b
line 211why, one step back211test('error.code is defined on failure if applicable', async t => {Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 212why, one step back212 const {code} = await t.throwsAsync(execa('noop.js', {cwd: 1}));Remove faulty emulated ENOENT error on Windows (#447)Mark Terrelbefore adoptionbdbd975
line 213why, one step back213 t.is(code, 'ERR_INVALID_ARG_TYPE');Add `cwd` error property (#565)homersimpsonsbefore adoptionf57fdec
lines 214–224why, one step back214});215216test('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});220221test('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);Add `error.originalMessage` property (#373)ehmickybefore adoption07d2b76
line 225why, one step back225});