error.js
This file
Path
test/error.js
210 lines · 6.4 KiB · JavaScript
Last changed by
Remove faulty emulated ENOENT error on Windows (#447)Mark Terrelbefore adoptionbdbd975
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–53why, 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 => {Add stdout/stderr to error messages (#397)ehmickybefore adoptionbc520f9
lines 54–72why, one step back54 await t.throwsAsync(execa('exit', ['2', 'foo', 'bar']), {message: /exit 2 foo bar/});55});5657test('error.message contains stdout/stderr if available', async t => {58 const {message} = await t.throwsAsync(execa('echo-fail'));59 t.true(message.includes('stderr'));60 t.true(message.includes('stdout'));61});6263test('error.message does not contain stdout/stderr if not available', async t => {64 const {message} = await t.throwsAsync(execa('echo-fail', {stdio: 'ignore'}));65 t.false(message.includes('stderr'));66 t.false(message.includes('stdout'));67});6869test('error.shortMessage does not contain stdout/stderr', async t => {70 const {shortMessage} = await t.throwsAsync(execa('echo-fail'));71 t.false(shortMessage.includes('stderr'));72 t.false(shortMessage.includes('stdout'));Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 73–75why, one step back73});7475test('Original error.message is kept', async t => {Remove faulty emulated ENOENT error on Windows (#447)Mark Terrelbefore adoptionbdbd975
lines 76–77why, one step back76 const {originalMessage} = await t.throwsAsync(execa('noop', {cwd: 1}));77 t.true(originalMessage.startsWith('The "options.cwd" property must be of type string. Received type number'));Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 78–81why, one step back78});7980test('failed is false on success', async t => {81 const {failed} = await execa('noop', ['foo']);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 82–84why, one step back82 t.false(failed);83});84Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 85–86why, one step back85test('failed is true on failure', async t => {86 const {failed} = await t.throwsAsync(execa('exit', ['2']));Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 87–90why, one step back87 t.true(failed);88});8990test('error.killed is true if process was killed directly', async t => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 91why, one step back91 const subprocess = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 92why, one step back92Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 93why, one step back93 subprocess.kill();Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 94why, one step back94Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 95why, one step back95 const {killed} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 96–99why, one step back96 t.true(killed);97});9899test('error.killed is false if process was killed indirectly', async t => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 100why, one step back100 const subprocess = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 101why, one step back101Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 102why, one step back102 process.kill(subprocess.pid, 'SIGINT');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 103–105why, one step back103104 // `process.kill()` is emulated by Node.js on Windows105 const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 106why, one step back106 const {killed} = await t.throwsAsync(subprocess, {message});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 107–133why, one step back107 t.false(killed);108});109110test('result.killed is false if not killed', async t => {111 const {killed} = await execa('noop');112 t.false(killed);113});114115test('result.killed is false if not killed, in sync mode', t => {116 const {killed} = execa.sync('noop');117 t.false(killed);118});119120test('result.killed is false on process error', async t => {121 const {killed} = await t.throwsAsync(execa('wrong command'));122 t.false(killed);123});124125test('result.killed is false on process error, in sync mode', t => {126 const {killed} = t.throws(() => {127 execa.sync('wrong command');128 });129 t.false(killed);130});131132if (process.platform === 'darwin') {133 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 134why, one step back134 const {pid} = childProcess.exec('noop', error => {Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 135–145why, one step back135 t.truthy(error);136 t.false(error.killed);137 t.end();138 });139140 process.kill(pid, 'SIGINT');141 });142}143144if (process.platform !== 'win32') {145 test('error.signal is SIGINT', async t => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 146why, one step back146 const subprocess = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 147why, one step back147Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 148why, one step back148 process.kill(subprocess.pid, 'SIGINT');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 149why, one step back149Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 150why, one step back150 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGINT/});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 151–153why, one step back151 t.is(signal, 'SIGINT');152 });153Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f
lines 154–162why, one step back154 test('error.signalDescription is defined', async t => {155 const subprocess = execa('noop');156157 process.kill(subprocess.pid, 'SIGINT');158159 const {signalDescription} = await t.throwsAsync(subprocess, {message: /User interruption with CTRL-C/});160 t.is(signalDescription, 'User interruption with CTRL-C');161 });162Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 163why, one step back163 test('error.signal is SIGTERM', async t => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 164why, one step back164 const subprocess = execa('noop');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 165why, one step back165Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 166why, one step back166 process.kill(subprocess.pid, 'SIGTERM');Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 167why, one step back167Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 168why, one step back168 const {signal} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 169–172why, one step back169 t.is(signal, 'SIGTERM');170 });171172 test('custom error.signal', async t => {Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815
line 173why, one step back173 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 174–175why, one step back174 t.is(signal, 'SIGHUP');175 });Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 176–177why, one step back176177 test('exitCode is undefined on signal termination', async t => {Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 178why, one step back178 const subprocess = execa('noop');Fix error properties (#375)ehmickybefore adoptionc1b511b
line 179why, one step back179Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 180why, one step back180 process.kill(subprocess.pid);Fix error properties (#375)ehmickybefore adoptionc1b511b
line 181why, one step back181Rename internal variables (#379)ehmickybefore adoption1ac56ea
line 182why, one step back182 const {exitCode} = await t.throwsAsync(subprocess);Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 183–184why, one step back183 t.is(exitCode, undefined);184 });Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 185–194why, one step back185}186187test('result.signal is undefined for successful execution', async t => {188 const {signal} = await execa('noop');189 t.is(signal, undefined);190});191192test('result.signal is undefined if process failed, but was not killed', async t => {193 const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});194 t.is(signal, undefined);Add `error.signalDescription` (#378)ehmickybefore adoptiond8cb08f
lines 195–199why, one step back195});196197test('result.signalDescription is undefined for successful execution', async t => {198 const {signalDescription} = await execa('noop');199 t.is(signalDescription, undefined);Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 200–201why, one step back200});201Fix error properties (#375)ehmickybefore adoptionc1b511b
lines 202–205why, one step back202test('error.code is undefined on success', async t => {203 const {code} = await execa('noop');204 t.is(code, undefined);205});Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
line 206why, one step back206Fix error properties (#375)ehmickybefore adoptionc1b511b
line 207why, one step back207test('error.code is defined on failure if applicable', async t => {Remove faulty emulated ENOENT error on Windows (#447)Mark Terrelbefore adoptionbdbd975
lines 208–209why, one step back208 const {code} = await t.throwsAsync(execa('noop', {cwd: 1}));209 t.is(code, 'ERR_INVALID_ARG_TYPE');Add `error.originalMessage` property (#373)ehmickybefore adoption07d2b76
line 210why, one step back210});