Skip to content
Myr

Improve copying previous Execa errors (#806)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent c999241 · Browse files · 2 files+73 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+73 added−33 removed

  • tests:+44 added−29 removed
  • other:+29 added−4 removed

2 files+73 added−33 removed

modified:lib/error.js+29 added-4 removed
modified:test/error.js+44 added-29 removed
33 unmodified lines
34 const error = await t.throwsAsync(execa('fail.js', {...fullStdio, all: true}));35 t.deepEqual(Reflect.ownKeys(error), [36 'stack',37 'message',38 'originalMessage',38 'shortMessage',39 'originalMessage',40 'command',41 'escapedCommand',42 'cwd',43 'failed',
309 unmodified lines
353 childProcess.emit('error', {message: foobarString});354 await t.throwsAsync(childProcess, {message: new RegExp(foobarString)});355});356357test('error instance can be shared', async t => {358 const originalMessage = foobarString;359 const error = new Error(originalMessage);357const runAndFail = (t, fixtureName, argument, error) => {358 const childProcess = execa(fixtureName, [argument]);359 childProcess.emit('error', error);360 return t.throwsAsync(childProcess);361};362363const testErrorCopy = async (t, getPreviousArgument) => {364 const fixtureName = 'empty.js';365 const argument = 'two';366362 const firstArgument = 'one';363 const childProcess = execa(fixtureName, [firstArgument]);364 childProcess.emit('error', error);365 const firstError = await t.throwsAsync(childProcess);367 const previousArgument = await getPreviousArgument(t, fixtureName);368 const previousError = await runAndFail(t, fixtureName, 'foo', previousArgument);369 const error = await runAndFail(t, fixtureName, argument, previousError);370 const message = `Command failed: ${fixtureName} ${argument}\n${foobarString}`;371367 const secondArgument = 'two';368 const secondChildProcess = execa(fixtureName, [secondArgument]);369 secondChildProcess.emit('error', error);370 const secondError = await t.throwsAsync(secondChildProcess);372 t.not(error, previousError);373 t.is(error.command, `${fixtureName} ${argument}`);374 t.is(error.message, message);375 t.true(error.stack.includes(message));376 t.is(error.shortMessage, message);377 t.is(error.originalMessage, foobarString);378};379372 const firstCommand = `${fixtureName} ${firstArgument}`;373 const firstMessage = `Command failed: ${firstCommand}\n${foobarString}`;374 t.is(firstError, error);375 t.is(firstError.command, firstCommand);376 t.is(firstError.message, firstMessage);377 t.true(firstError.stack.includes(firstMessage));378 t.is(firstError.shortMessage, firstMessage);379 t.is(firstError.originalMessage, originalMessage);380test('error instance can be shared', testErrorCopy, () => new Error(foobarString));381test('error string can be shared', testErrorCopy, () => foobarString);382test('error copy can be shared', testErrorCopy, (t, fixtureName) => runAndFail(t, fixtureName, 'bar', new Error(foobarString)));383381 const secondCommand = `${fixtureName} ${secondArgument}`;382 const secondMessage = `Command failed: ${secondCommand}\n${foobarString}`;383 t.not(secondError, error);384 t.is(secondError.command, secondCommand);385 t.is(secondError.message, secondMessage);386 t.true(secondError.stack.includes(secondMessage));387 t.is(secondError.shortMessage, secondMessage);388 t.is(secondError.originalMessage, originalMessage);389});384const testErrorCopyProperty = async (t, propertyName, isCopied) => {385 const propertyValue = 'test';386 const initialError = new Error(foobarString);387 initialError[propertyName] = propertyValue;388389 const previousError = await runAndFail(t, 'empty.js', 'foo', initialError);390 t.is(previousError, initialError);391392 const error = await runAndFail(t, 'empty.js', 'bar', previousError);393 t.is(error[propertyName] === propertyValue, isCopied);394};395396test('error.code can be copied', testErrorCopyProperty, 'code', true);397test('error.errno can be copied', testErrorCopyProperty, 'errno', true);398test('error.syscall can be copied', testErrorCopyProperty, 'syscall', true);399test('error.path can be copied', testErrorCopyProperty, 'path', true);400test('error.dest can be copied', testErrorCopyProperty, 'dest', true);401test('error.address can be copied', testErrorCopyProperty, 'address', true);402test('error.port can be copied', testErrorCopyProperty, 'port', true);403test('error.info can be copied', testErrorCopyProperty, 'info', true);404test('error.other cannot be copied', testErrorCopyProperty, 'other', false);