error.js
This file
Path
test/error.js
178 lines · 5.0 KiB · JavaScript
Last changed by
Fix error properties (#375)ehmickybefore adoptionc1b511b
History of this fileTake it
Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 1–12why, one step back1import path from 'path';2import childProcess from 'child_process';3import test from 'ava';4import execa from '..';56process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;78const TIMEOUT_REGEXP = /timed out after/;910const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);1112test('stdout/stderr/all available on errors', async t => {Add `all` option (#353)Tiago Nápolibefore adoption56b749d
line 13why, one step back13 const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2'], {all: true}), {message: getExitRegExp('2')});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 14–23why, one step back14 t.is(typeof stdout, 'string');15 t.is(typeof stderr, 'string');16 t.is(typeof all, 'string');17});1819const WRONG_COMMAND = process.platform === 'win32' ?20 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :21 '';2223test('stdout/stderr/all on process errors', async t => {Add `all` option (#353)Tiago Nápolibefore adoption56b749d
line 24why, one step back24 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command', {all: true}));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 25–38why, one step back25 t.is(stdout, '');26 t.is(stderr, WRONG_COMMAND);27 t.is(all, WRONG_COMMAND);28});2930test('stdout/stderr/all on process errors, in sync mode', t => {31 const {stdout, stderr, all} = t.throws(() => {32 execa.sync('wrong command');33 });34 t.is(stdout, '');35 t.is(stderr, WRONG_COMMAND);36 t.is(all, undefined);37});38Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 39–41why, one step back39test('exitCode is 0 on success', async t => {40 const {exitCode} = await execa('noop', ['foo']);41 t.is(exitCode, 0);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 42–43why, one step back42});43Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 44–63why, one step back44const testExitCode = async (t, num) => {45 const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});46 t.is(exitCode, num);47};4849test('exitCode is 2', testExitCode, 2);50test('exitCode is 3', testExitCode, 3);51test('exitCode is 4', testExitCode, 4);5253test('error.message contains the command', async t => {54 await t.throwsAsync(execa('exit', ['2', 'foo', 'bar'], {message: /exit 2 foo bar/}));55});5657test('Original error.message is kept', async t => {58 const {originalMessage} = await t.throwsAsync(execa('wrong command'));59 t.is(originalMessage, 'spawn wrong command ENOENT');60});6162test('failed is false on success', async t => {63 const {failed} = await execa('noop', ['foo']);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 64–66why, one step back64 t.false(failed);65});66Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 67–68why, one step back67test('failed is true on failure', async t => {68 const {failed} = await t.throwsAsync(execa('exit', ['2']));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 69–72why, one step back69 t.true(failed);70});7172test('error.killed is true if process was killed directly', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 73why, one step back73 const cp = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 74–81why, one step back7475 cp.kill();7677 const {killed} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});78 t.true(killed);79});8081test('error.killed is false if process was killed indirectly', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 82why, one step back82 const cp = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 83–115why, one step back8384 process.kill(cp.pid, 'SIGINT');8586 // `process.kill()` is emulated by Node.js on Windows87 const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;88 const {killed} = await t.throwsAsync(cp, {message});89 t.false(killed);90});9192test('result.killed is false if not killed', async t => {93 const {killed} = await execa('noop');94 t.false(killed);95});9697test('result.killed is false if not killed, in sync mode', t => {98 const {killed} = execa.sync('noop');99 t.false(killed);100});101102test('result.killed is false on process error', async t => {103 const {killed} = await t.throwsAsync(execa('wrong command'));104 t.false(killed);105});106107test('result.killed is false on process error, in sync mode', t => {108 const {killed} = t.throws(() => {109 execa.sync('wrong command');110 });111 t.false(killed);112});113114if (process.platform === 'darwin') {115 test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 116why, one step back116 const {pid} = childProcess.exec('noop', error => {Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 117–127why, one step back117 t.truthy(error);118 t.false(error.killed);119 t.end();120 });121122 process.kill(pid, 'SIGINT');123 });124}125126if (process.platform !== 'win32') {127 test('error.signal is SIGINT', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 128why, one step back128 const cp = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 129–136why, one step back129130 process.kill(cp.pid, 'SIGINT');131132 const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGINT/});133 t.is(signal, 'SIGINT');134 });135136 test('error.signal is SIGTERM', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 137why, one step back137 const cp = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 138–145why, one step back138139 process.kill(cp.pid, 'SIGTERM');140141 const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});142 t.is(signal, 'SIGTERM');143 });144145 test('custom error.signal', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 146why, one step back146 const {signal} = await t.throwsAsync(execa('noop', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 147–148why, one step back147 t.is(signal, 'SIGHUP');148 });Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 149–157why, one step back149150 test('exitCode is undefined on signal termination', async t => {151 const cp = execa('noop');152153 process.kill(cp.pid);154155 const {exitCode} = await t.throwsAsync(cp);156 t.is(exitCode, undefined);157 });Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 158–169why, one step back158}159160test('result.signal is undefined for successful execution', async t => {161 const {signal} = await execa('noop');162 t.is(signal, undefined);163});164165test('result.signal is undefined if process failed, but was not killed', async t => {166 const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});167 t.is(signal, undefined);168});169Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 170–173why, one step back170test('error.code is undefined on success', async t => {171 const {code} = await execa('noop');172 t.is(code, undefined);173});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 174why, one step back174Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 175–177why, one step back175test('error.code is defined on failure if applicable', async t => {176 const {code} = await t.throwsAsync(execa('invalid'));177 t.is(code, 'ENOENT');Add `error.originalMessage` property (#373)ehmickybefore adoption07d2b76
line 178why, one step back178});