error.js
This file
Path
test/error.js
165 lines · 5.1 KiB · JavaScript
Last changed by
Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
History of this fileTake it
Separate error creation logic into its own file (#311)ehmickybefore adoption4dd258d
lines 1–165why, 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 => {13 const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});14 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 => {24 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));25 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});3839test('allow unknown exit code', async t => {40 const {exitCode, exitCodeName} = await t.throwsAsync(execa('exit', ['255']), {message: /exit code 255 \(Unknown system error -255\)/});41 t.is(exitCode, 255);42 t.is(exitCodeName, 'Unknown system error -255');43});4445test('execa() does not return code and failed properties on success', async t => {46 const {exitCode, exitCodeName, failed} = await execa('noop', ['foo']);47 t.is(exitCode, 0);48 t.is(exitCodeName, 'SUCCESS');49 t.false(failed);50});5152test('execa() returns code and failed properties', async t => {53 const {exitCode, exitCodeName, failed} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});54 t.is(exitCode, 2);55 const expectedName = process.platform === 'win32' ? 'Unknown system error -2' : 'ENOENT';56 t.is(exitCodeName, expectedName);57 t.true(failed);58});5960test('error.killed is true if process was killed directly', async t => {61 const cp = execa('forever');6263 cp.kill();6465 const {killed} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});66 t.true(killed);67});6869test('error.killed is false if process was killed indirectly', async t => {70 const cp = execa('forever');7172 process.kill(cp.pid, 'SIGINT');7374 // `process.kill()` is emulated by Node.js on Windows75 const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;76 const {killed} = await t.throwsAsync(cp, {message});77 t.false(killed);78});7980test('result.killed is false if not killed', async t => {81 const {killed} = await execa('noop');82 t.false(killed);83});8485test('result.killed is false if not killed, in sync mode', t => {86 const {killed} = execa.sync('noop');87 t.false(killed);88});8990test('result.killed is false on process error', async t => {91 const {killed} = await t.throwsAsync(execa('wrong command'));92 t.false(killed);93});9495test('result.killed is false on process error, in sync mode', t => {96 const {killed} = t.throws(() => {97 execa.sync('wrong command');98 });99 t.false(killed);100});101102if (process.platform === 'darwin') {103 test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {104 const {pid} = childProcess.exec('forever', error => {105 t.truthy(error);106 t.false(error.killed);107 t.end();108 });109110 process.kill(pid, 'SIGINT');111 });112}113114if (process.platform !== 'win32') {115 test('error.signal is SIGINT', async t => {116 const cp = execa('forever');117118 process.kill(cp.pid, 'SIGINT');119120 const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGINT/});121 t.is(signal, 'SIGINT');122 });123124 test('error.signal is SIGTERM', async t => {125 const cp = execa('forever');126127 process.kill(cp.pid, 'SIGTERM');128129 const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});130 t.is(signal, 'SIGTERM');131 });132133 test('custom error.signal', async t => {134 const {signal} = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));135 t.is(signal, 'SIGHUP');136 });137}138139test('result.signal is undefined for successful execution', async t => {140 const {signal} = await execa('noop');141 t.is(signal, undefined);142});143144test('result.signal is undefined if process failed, but was not killed', async t => {145 const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});146 t.is(signal, undefined);147});148149const testExitCode = async (t, num) => {150 const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});151 t.is(exitCode, num);152};153154test('error.exitCode is 2', testExitCode, 2);155test('error.exitCode is 3', testExitCode, 3);156test('error.exitCode is 4', testExitCode, 4);157158const errorMessage = async (t, expected, ...args) => {159 await t.throwsAsync(execa('exit', args), {message: expected});160};161162errorMessage.title = (message, expected) => `error.message matches: ${expected}`;163164test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');165test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');