Skip to content
Myr

test.js

This file

Path

test/test.js

218 lines · 6.6 KiB · JavaScript

Last changed by

Fix error properties (#375)ehmickybefore adoptionc1b511b

History of this file

Take it

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 1–8why, one step back
1import path from 'path';
2import test from 'ava';3import isRunning from 'is-running';4import execa from '..';56process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;7process.env.FOO = 'foo';8

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

line 9why, one step back
9const ENOENT_REGEXP = process.platform === 'win32' ? /failed with exit code 1/ : /spawn.* ENOENT/;

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 10–35why, one step back
10
11test('execa()', async t => {12 const {stdout} = await execa('noop', ['foo']);13 t.is(stdout, 'foo');14});1516if (process.platform === 'win32') {17 test('execa() - cmd file', async t => {18 const {stdout} = await execa('hello.cmd');19 t.is(stdout, 'Hello World');20 });2122 test('execa() - run cmd command', async t => {23 const {stdout} = await execa('cmd', ['/c', 'hello.cmd']);24 t.is(stdout, 'Hello World');25 });26}2728test('execa.sync()', t => {29 const {stdout} = execa.sync('noop', ['foo']);30 t.is(stdout, 'foo');31});3233test('execa.sync() throws error if written to stderr', t => {34 t.throws(() => {35 execa.sync('foo');

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

line 36why, one step back
36 }, ENOENT_REGEXP);

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 37–75why, one step back
37});
3839test('skip throwing when using reject option', async t => {40 const {exitCode} = await execa('fail', {reject: false});41 t.is(exitCode, 2);42});4344test('skip throwing when using reject option in sync mode', t => {45 const {exitCode} = execa.sync('fail', {reject: false});46 t.is(exitCode, 2);47});4849test('stripFinalNewline: true', async t => {50 const {stdout} = await execa('noop', ['foo']);51 t.is(stdout, 'foo');52});5354test('stripFinalNewline: false', async t => {55 const {stdout} = await execa('noop', ['foo'], {stripFinalNewline: false});56 t.is(stdout, 'foo\n');57});5859test('stripFinalNewline on failure', async t => {60 const {stderr} = await t.throwsAsync(execa('noop-throw', ['foo'], {stripFinalNewline: true}));61 t.is(stderr, 'foo');62});6364test('stripFinalNewline in sync mode', t => {65 const {stdout} = execa.sync('noop', ['foo'], {stripFinalNewline: true});66 t.is(stdout, 'foo');67});6869test('stripFinalNewline in sync mode on failure', t => {70 const {stderr} = t.throws(() => {71 execa.sync('noop-throw', ['foo'], {stripFinalNewline: true});72 });73 t.is(stderr, 'foo');74});75

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

line 76why, one step back
76test('preferLocal: true', async t => {

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

line 77why, one step back
77 await t.notThrowsAsync(execa('ava', ['--version'], {preferLocal: true, env: {Path: '', PATH: ''}}));

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 78–79why, one step back
78});
79

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

line 80why, one step back
80test('preferLocal: false', async t => {

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

line 81why, one step back
81 await t.throwsAsync(execa('ava', ['--version'], {preferLocal: false, env: {Path: '', PATH: ''}}), ENOENT_REGEXP);

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

lines 82–84why, one step back
82});
8384test('preferLocal: undefined', async t => {

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

line 85why, one step back
85 await t.throwsAsync(execa('ava', ['--version'], {env: {Path: '', PATH: ''}}), ENOENT_REGEXP);

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

lines 86–87why, one step back
86});
87

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 88–89why, one step back
88test('localDir option', async t => {
89 const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';

Change default value of the `preferLocal` option to `false` (#314)ehmickybefore adoptioneb22ff7

line 90why, one step back
90 const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: '/test'});

Fix errors being thrown when `detached: true` or `cleanup: false` is used (#360)ehmickybefore adoptionc3526cd

lines 91–92why, one step back
91 const envPaths = stdout.split(path.delimiter);
92 t.true(envPaths.some(envPath => envPath.endsWith('.bin')));

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 93–107why, one step back
93});
9495test('stdin errors are handled', async t => {96 const child = execa('noop');97 child.stdin.emit('error', new Error('test'));98 await t.throwsAsync(child, /test/);99});100101test('child process errors are handled', async t => {102 const child = execa('noop');103 child.emit('error', new Error('test'));104 await t.throwsAsync(child, /test/);105});106107test('child process errors rejects promise right away', async t => {

Properly clean up child processes created during tests (#349)ehmickybefore adoption071a815

line 108why, one step back
108 const child = execa('noop');

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 109–112why, one step back
109 child.emit('error', new Error('test'));
110 await t.throwsAsync(child, /test/);111});112

Separate spawned.kill() features into own file (#312)ehmickybefore adoption230c7a7

lines 113–114why, one step back
113test('execa() returns a promise with pid', t => {
114 const {pid} = execa('noop', ['foo']);

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 115–138why, one step back
115 t.is(typeof pid, 'number');
116});117118test('child_process.spawn() propagated errors have correct shape', t => {119 const cp = execa('noop', {uid: -1});120 t.notThrows(() => {121 cp.catch(() => {});122 cp.unref();123 cp.on('error', () => {});124 });125});126127test('child_process.spawn() errors are propagated', async t => {128 const {failed} = await t.throwsAsync(execa('noop', {uid: -1}));129 t.true(failed);130});131132test('child_process.spawnSync() errors are propagated with a correct shape', t => {133 const {failed} = t.throws(() => {134 execa.sync('noop', {timeout: -1});135 });136 t.true(failed);137});138

Do not read streams more than once (#330)ehmickybefore adoption6a8e9ac

lines 139–144why, one step back
139test('do not try to consume streams twice', async t => {
140 const cp = execa('noop', ['foo']);141 t.is((await cp).stdout, 'foo');142 t.is((await cp).stdout, 'foo');143});144

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 145–157why, one step back
145test('use relative path with \'..\' chars', async t => {
146 const pathViaParentDir = path.join('..', path.basename(path.dirname(__dirname)), 'test', 'fixtures', 'noop');147 const {stdout} = await execa(pathViaParentDir, ['foo']);148 t.is(stdout, 'foo');149});150151if (process.platform !== 'win32') {152 test('execa() rejects if running non-executable', async t => {153 const cp = execa('non-executable');154 await t.throwsAsync(cp);155 });156157 test('execa() rejects with correct error and doesn\'t throw if running non-executable with input', async t => {

Fix test for Node `>= 12.6.0` (#348)ehmickybefore adoptionb46cba1

lines 158–160why, one step back
158 // On Node <12.6.0, `EACCESS` is emitted on `childProcess`.
159 // On Node >=12.6.0, `EPIPE` is emitted on `childProcess.stdin`.160 await t.throwsAsync(execa('non-executable', {input: 'Hey!'}), /EACCES|EPIPE/);

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 161–171why, one step back
161 });
162}163164if (process.platform !== 'win32') {165 test('write to fast-exit process', async t => {166 // Try-catch here is necessary, because this test is not 100% accurate167 // Sometimes process can manage to accept input before exiting168 try {169 await execa(`fast-exit-${process.platform}`, [], {input: 'data'});170 t.pass();171 } catch (error) {

Fix error properties (#375)ehmickybefore adoptionc1b511b

line 172why, one step back
172 t.is(error.code, 'EPIPE');

Add `.node()` method (#200)Guillaume Martignybefore adoptionc2f5edf

lines 173–218why, one step back
173 }
174 });175}176177test('use environment variables by default', async t => {178 const {stdout} = await execa('environment');179 t.deepEqual(stdout.split('\n'), ['foo', 'undefined']);180});181182test('extend environment variables by default', async t => {183 const {stdout} = await execa('environment', [], {env: {BAR: 'bar'}});184 t.deepEqual(stdout.split('\n'), ['foo', 'bar']);185});186187test('do not extend environment with `extendEnv: false`', async t => {188 const {stdout} = await execa('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});189 t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);190});191192test('can use `options.shell: true`', async t => {193 const {stdout} = await execa('node test/fixtures/noop foo', {shell: true});194 t.is(stdout, 'foo');195});196197test('can use `options.shell: string`', async t => {198 const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/bash';199 const {stdout} = await execa('node test/fixtures/noop foo', {shell});200 t.is(stdout, 'foo');201});202203test('use extend environment with `extendEnv: true` and `shell: true`', async t => {204 process.env.TEST = 'test';205 const command = process.platform === 'win32' ? 'echo %TEST%' : 'echo $TEST';206 const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});207 t.is(stdout, 'test');208 delete process.env.TEST;209});210211test('detach child process', async t => {212 const {stdout} = await execa('detach');213 const pid = Number(stdout);214 t.true(Number.isInteger(pid));215 t.true(isRunning(pid));216217 process.kill(pid, 'SIGKILL');218});