Make tests more consistent (code refactoring) (#247)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent bc0984e · Browse files · 1 file·+85 added−95 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+85 added−95 removed
- other:+85 added−95 removed
Provenance
- Commit
f5172cd- Tree
986f434- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
bc0984e
1 file changed+85 added−95 removed
modified:test.js+85 added-95 removed
38 unmodified lines39 t.is(stdout.toString(), 'foo');40 });41 42 test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {43- const result = await execa('noop-132');44- t.is(result.all, '132');43+ const {all} = await execa('noop-132');44+ t.is(all, '132');45 });46 47 test('stdout/stderr/all available on errors', async t => {48- const error = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});49- t.is(typeof error.stdout, 'string');50- t.is(typeof error.stderr, 'string');51- t.is(typeof error.all, 'string');48+ const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});49+ t.is(typeof stdout, 'string');50+ t.is(typeof stderr, 'string');51+ t.is(typeof all, 'string');52 });53 54 test('pass `stdout` to a file descriptor', async t => {55 const file = tempfile('.txt');54 unmodified lines110 }, process.platform === 'win32' ? /failed with exit code 1/ : /spawnSync foo ENOENT/);111 });112 113 test('skip throwing when using reject option', async t => {114- const error = await execa('fail', {reject: false});115- t.is(error.exitCode, 2);114+ const {exitCode} = await execa('fail', {reject: false});115+ t.is(exitCode, 2);116 });117 118 test('skip throwing when using reject option in sync mode', t => {119- const error = execa.sync('fail', {reject: false});120- t.is(error.exitCode, 2);119+ const {exitCode} = execa.sync('fail', {reject: false});120+ t.is(exitCode, 2);121 });122 123 test('stripFinalNewline: true', async t => {124 const {stdout} = await execa('noop', ['foo']);88 unmodified lines213 );214 });215 216 test('execa() returns a promise with kill() and pid', t => {217- const promise = execa('noop', ['foo']);218- t.is(typeof promise.kill, 'function');219- t.is(typeof promise.pid, 'number');217+ const {kill, pid} = execa('noop', ['foo']);218+ t.is(typeof kill, 'function');219+ t.is(typeof pid, 'number');220 });221 222 test('maxBuffer affects stdout', async t => {223 await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /stdout maxBuffer exceeded/);34 unmodified lines258 t.is(exitCode, 255);259 t.is(exitCodeName, 'Unknown system error -255');260 });261 262-test('execa() returns code and failed properties', async t => {262+test('execa() does not return code and failed properties on success', async t => {263 const {exitCode, exitCodeName, failed} = await execa('noop', ['foo']);264 t.is(exitCode, 0);265 t.is(exitCodeName, 'SUCCESS');266 t.false(failed);267+});268 269+test('execa() returns code and failed properties', async t => {268- const error = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});269- t.is(error.exitCode, 2);270+ const {exitCode, exitCodeName, failed} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});271+ t.is(exitCode, 2);272 const expectedName = process.platform === 'win32' ? 'Unknown system error -2' : 'ENOENT';271- t.is(error.exitCodeName, expectedName);272- t.true(error.failed);273+ t.is(exitCodeName, expectedName);274+ t.true(failed);275 });276 277 test('use relative path with \'..\' chars', async t => {278 const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');16 unmodified lines295 const cp = execa('forever');296 297 cp.kill();298 297- const error = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});298- t.true(error.killed);299+ const {killed} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});300+ t.true(killed);301 });302 303 test('error.killed is false if process was killed indirectly', async t => {304 const cp = execa('forever');1 unmodified line306 process.kill(cp.pid, 'SIGINT');307 308 // `process.kill()` is emulated by Node.js on Windows309 const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;308- const error = await t.throwsAsync(cp, {message});309- t.false(error.killed);310+ const {killed} = await t.throwsAsync(cp, {message});311+ t.false(killed);312 });313 314 test('result.killed is false if not killed', async t => {313- const result = await execa('noop');314- t.false(result.killed);315+ const {killed} = await execa('noop');316+ t.false(killed);317 });318 319 test('result.killed is false if not killed, in sync mode', t => {318- const result = execa.sync('noop');319- t.false(result.killed);320+ const {killed} = execa.sync('noop');321+ t.false(killed);322 });323 324 test('result.killed is false on process error', async t => {325 const {killed} = await t.throwsAsync(execa('wrong command'));8 unmodified lines334 });335 336 if (process.platform === 'darwin') {337 test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {336- const cp = childProcess.exec('forever', error => {338+ const {pid} = childProcess.exec('forever', error => {339 t.truthy(error);340 t.false(error.killed);341 t.end();342 });343 342- process.kill(cp.pid, 'SIGINT');344+ process.kill(pid, 'SIGINT');345 });346 }347 348 if (process.platform !== 'win32') {1 unmodified line350 const cp = execa('forever');351 352 process.kill(cp.pid, 'SIGINT');353 352- const error = await t.throwsAsync(cp, {message: /was killed with SIGINT/});353- t.is(error.signal, 'SIGINT');354+ const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGINT/});355+ t.is(signal, 'SIGINT');356 });357 358 test('error.signal is SIGTERM', async t => {359 const cp = execa('forever');360 361 process.kill(cp.pid, 'SIGTERM');362 361- const error = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});362- t.is(error.signal, 'SIGTERM');363+ const {signal} = await t.throwsAsync(cp, {message: /was killed with SIGTERM/});364+ t.is(signal, 'SIGTERM');365 });366 367 test('custom error.signal', async t => {366- const error = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));367- t.is(error.signal, 'SIGHUP');368+ const {signal} = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));369+ t.is(signal, 'SIGHUP');370 });371 }372 373 test('result.signal is undefined for successful execution', async t => {372- t.is((await execa('noop')).signal, undefined);374+ const {signal} = await execa('noop');375+ t.is(signal, undefined);376 });377 378 test('result.signal is undefined if process failed, but was not killed', async t => {376- const error = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});377- t.is(error.signal, undefined);379+ const {signal} = await t.throwsAsync(execa('exit', [2]), {message: getExitRegExp('2')});380+ t.is(signal, undefined);381 });382 383 async function testExitCode(t, num) {384 const {exitCode} = await t.throwsAsync(execa('exit', [`${num}`]), {message: getExitRegExp(num)});4 unmodified lines389 test('error.exitCode is 3', testExitCode, 3);390 test('error.exitCode is 4', testExitCode, 4);391 392 test('timeout kills the process if it times out', async t => {390- const error = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP}));391- t.false(error.killed);392- t.true(error.timedOut);393+ const {killed, timedOut} = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP}));394+ t.false(killed);395+ t.true(timedOut);396 });397 398 test('timeout does not kill the process if it does not time out', async t => {396- const error = await execa('delay', ['500'], {timeout: 1e8});397- t.false(error.timedOut);399+ const {timedOut} = await execa('delay', ['500'], {timeout: 1e8});400+ t.false(timedOut);401 });402 403 test('timedOut is false if no timeout was set', async t => {401- const result = await execa('noop');402- t.false(result.timedOut);404+ const {timedOut} = await execa('noop');405+ t.false(timedOut);406 });407 408 test('timedOut will be false if no timeout was set and zero exit code in sync mode', t => {406- const result = execa.sync('noop');407- t.false(result.timedOut);409+ const {timedOut} = execa.sync('noop');410+ t.false(timedOut);411 });412 413 async function errorMessage(t, expected, ...args) {414 await t.throwsAsync(execa('exit', args), {message: expected});4 unmodified lines419 test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');420 test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');421 422 async function command(t, expected, ...args) {420- const error = await t.throwsAsync(execa('fail', args));421- t.is(error.command, `fail${expected}`);423+ const {command: failCommand} = await t.throwsAsync(execa('fail', args));424+ t.is(failCommand, `fail${expected}`);425 423- const result = await execa('noop', args);424- t.is(result.command, `noop${expected}`);426+ const {command} = await execa('noop', args);427+ t.is(command, `noop${expected}`);428 }429 430 command.title = (message, expected) => `command is: ${JSON.stringify(expected)}`;431 61 unmodified lines493 }494 495 test('use environment variables by default', async t => {496 const {stdout} = await execa('environment');494-495- t.deepEqual(stdout.split('\n'), [497+ t.deepEqual(stdout.split('\n'), ['foo', 'undefined']);496- 'foo',497- 'undefined'498- ]);498 });499 500 test('extend environment variables by default', async t => {501 const {stdout} = await execa('environment', [], {env: {BAR: 'bar'}});503-504- t.deepEqual(stdout.split('\n'), [502+ t.deepEqual(stdout.split('\n'), ['foo', 'bar']);505- 'foo',506- 'bar'507- ]);503 });504 505 test('do not extend environment with `extendEnv: false`', async t => {506 const {stdout} = await execa('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});512-513- t.deepEqual(stdout.split('\n'), [507+ t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);514- 'undefined',515- 'bar'516- ]);508 });509 510 test('can use `options.shell: true`', async t => {511 const {stdout} = await execa('node fixtures/noop foo', {shell: true});512 t.is(stdout, 'foo');513 });514 515 test('can use `options.shell: string`', async t => {525- const {stdout} = await execa('node fixtures/noop foo', {526- shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'527- });516+ const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/bash';517+ const {stdout} = await execa('node fixtures/noop foo', {shell});518 t.is(stdout, 'foo');519 });520 521 test('use extend environment with `extendEnv: true` and `shell: true`', async t => {4 unmodified lines526 delete process.env.TEST;527 });528 529 test('do not buffer when streaming', async t => {540- const result = await getStream(execa('max-buffer', ['stdout', '21'], {maxBuffer: 10}).stdout);541-530+ const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10});531+ const result = await getStream(stdout);532 t.is(result, '....................\n');533 });534 535 test('detach child process', async t => {34 unmodified lines570 });571 572 test('finally function is executed on failure', async t => {573 let isError = false;584- const error = await t.throwsAsync(execa('exit', ['2']).finally(() => {574+ const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => {575 isError = true;576 }));577 t.is(isError, true);588- t.is(typeof error.stdout, 'string');589- t.is(typeof error.stderr, 'string');578+ t.is(typeof stdout, 'string');579+ t.is(typeof stderr, 'string');580 });581 582 test('throw in finally function bubbles up on success', async t => {593- const result = await t.throwsAsync(execa('noop', ['foo']).finally(() => {583+ const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => {584 throw new Error('called');585 }));596- t.is(result.message, 'called');586+ t.is(message, 'called');587 });588 589 test('throw in finally bubbles up on error', async t => {600- const result = await t.throwsAsync(execa('exit', ['2']).finally(() => {590+ const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => {591 throw new Error('called');592 }));603- t.is(result.message, 'called');593+ t.is(message, 'called');594 });595 }596 597 test('cancel method kills the subprocess', t => {2 unmodified lines600 t.true(subprocess.killed);601 });602 603 test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {614- const result = await execa('noop');615- t.false(result.isCanceled);604+ const {isCanceled} = await execa('noop');605+ t.false(isCanceled);606 });607 608 test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {619- const error = await t.throwsAsync(execa('fail'));620- t.false(error.isCanceled);609+ const {isCanceled} = await t.throwsAsync(execa('fail'));610+ t.false(isCanceled);611 });612 613 test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {624- const result = execa.sync('noop');625- t.false(result.isCanceled);614+ const {isCanceled} = execa.sync('noop');615+ t.false(isCanceled);616 });617 618 test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {629- const error = t.throws(() => {619+ const {isCanceled} = t.throws(() => {620 execa.sync('fail');621 });632- t.false(error.isCanceled);622+ t.false(isCanceled);623 });624 625 test('calling cancel method throws an error with message "Command was canceled"', async t => {626 const subprocess = execa('noop');3 unmodified lines630 631 test('error.isCanceled is true when cancel method is used', async t => {632 const subprocess = execa('noop');633 subprocess.cancel();644- const error = await t.throwsAsync(subprocess);645- t.true(error.isCanceled);634+ const {isCanceled} = await t.throwsAsync(subprocess);635+ t.true(isCanceled);636 });637 638 test('error.isCanceled is false when kill method is used', async t => {639 const subprocess = execa('noop');640 subprocess.kill();651- const error = await t.throwsAsync(subprocess);652- t.false(error.isCanceled);641+ const {isCanceled} = await t.throwsAsync(subprocess);642+ t.false(isCanceled);643 });644 645 test('calling cancel method twice should show the same behaviour as calling it once', async t => {646 const subprocess = execa('noop');647 subprocess.cancel();648 subprocess.cancel();659- const error = await t.throwsAsync(subprocess);660- t.true(error.isCanceled);649+ const {isCanceled} = await t.throwsAsync(subprocess);650+ t.true(isCanceled);651 t.true(subprocess.killed);652 });653 654 test('calling cancel method on a successfuly completed process does not make result.isCanceled true', async t => {655 const subprocess = execa('noop');666- const result = await subprocess;656+ const {isCanceled} = await subprocess;657 subprocess.cancel();668- t.false(result.isCanceled);658+ t.false(isCanceled);659 });660 661 test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {662 const subprocess = execa('noop');663 subprocess.kill();674- const error = await t.throwsAsync(subprocess);675- t.false(error.isCanceled);664+ const {isCanceled} = await t.throwsAsync(subprocess);665+ t.false(isCanceled);666 });