test.js
This file
Path
test.js
359 lines · 9.4 KiB · JavaScript
Last changed by
Throw error in sync method - fixes #66 (#67)Sam Verschuerenbefore adoptionc8c9c7b
History of this fileTake it
fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 1why, one step back1import path from 'path';add `input` optionJames Talmagebefore adoption605a674
line 2why, one step back2import stream from 'stream';add tests (#48)James Talmagebefore adoptionb15483a
line 3why, one step back3import childProcess from 'child_process';4import test from 'ava';make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
line 5why, one step back5import getStream from 'get-stream';Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 6–7why, one step back6import isRunning from 'is-running';7import delay from 'delay';tweaksSindre Sorhusbefore adoptione3505a8
line 8why, one step back8import m from './';9fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 10–11why, one step back10process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;1112test('execa()', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 13why, one step back13 const {stdout} = await m('noop', ['foo']);initSindre Sorhusbefore adoptionbc16d6b
lines 14–1714 t.is(stdout, 'foo');15});1617test('buffer', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 18why, one step back18 const {stdout} = await m('noop', ['foo'], {encoding: null});initSindre Sorhusbefore adoptionbc16d6b
lines 19–2219 t.true(Buffer.isBuffer(stdout));20 t.is(stdout.toString(), 'foo');21});22add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505
lines 23–32why, one step back23test('execa.stdout()', async t => {24 const stdout = await m.stdout('noop', ['foo']);25 t.is(stdout, 'foo');26});2728test('execa.stderr()', async t => {29 const stderr = await m.stderr('noop-err', ['foo']);30 t.is(stderr, 'foo');31});32add `stdout`/`stderr` to errorsSindre Sorhusbefore adoption0ea8aaf
line 33why, one step back33test('stdout/stderr available on errors', async t => {meta tweaksSindre Sorhusbefore adoption858af26
lines 34–36why, one step back34 const err = await t.throws(m('exit', ['2']));35 t.is(typeof err.stdout, 'string');36 t.is(typeof err.stderr, 'string');add `stdout`/`stderr` to errorsSindre Sorhusbefore adoption0ea8aaf
lines 37–38why, one step back37});38add tests (#48)James Talmagebefore adoptionb15483a
line 39why, one step back39test('include stdout and stderr in errors for improved debugging', async t => {Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 40–41why, one step back40 const err = await t.throws(m('fixtures/error-message.js'));41 t.regex(err.message, /stdout/);add tests (#48)James Talmagebefore adoptionb15483a
line 42why, one step back42 t.regex(err.message, /stderr/);include stdout in errors for improved debuggingSindre Sorhusbefore adoptionaea8c9a
lines 43–44why, one step back43});44Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 45why, one step back45test('execa.shell()', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 46why, one step back46 const {stdout} = await m.shell('node fixtures/noop foo');Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
lines 47–49why, one step back47 t.is(stdout, 'foo');48});49make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
line 50why, one step back50test('execa.spawn()', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 51–52why, one step back51 t.is(typeof m.spawn('noop').pid, 'number');52 t.is((await getStream(m.spawn('noop', ['foo']).stdout)).trim(), 'foo');expose `.spawn`Sindre Sorhusbefore adoptionb65479c
lines 53–54why, one step back53});54add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
line 55why, one step back55test('execa.sync()', t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 56why, one step back56 const {stdout} = m.sync('noop', ['foo']);add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 57–59why, one step back57 t.is(stdout, 'foo');58});59Throw error in sync method - fixes #66 (#67)Sam Verschuerenbefore adoptionc8c9c7b
lines 60–63why, one step back60test('execa.sync() throws error if written to stderr', t => {61 t.throws(() => m.sync('foo'), process.platform === 'win32' ? /'foo' is not recognized as an internal or external command/ : 'spawnSync foo ENOENT');62});63add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
line 64why, one step back64test('execa.shellSync()', t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 65why, one step back65 const {stdout} = m.shellSync('node fixtures/noop foo');add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 66–68why, one step back66 t.is(stdout, 'foo');67});6869test('stripEof option', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 70why, one step back70 const {stdout} = await m('noop', ['foo'], {stripEof: false});initSindre Sorhusbefore adoptionbc16d6b
lines 71–7371 t.is(stdout, 'foo\n');72});73Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 74why, one step back74test.serial('preferLocal option', async t => {tweaksSindre Sorhusbefore adoptione3505a8
line 75why, one step back75 t.true((await m('cat-names')).stdout.length > 2);Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 76why, one step back76fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 77–81why, one step back77 if (process.platform === 'win32') {78 // TODO: figure out how to make the below not hang on Windows79 return;80 }81Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
lines 82–84why, one step back82 // account for npm adding local binaries to the PATH83 const _path = process.env.PATH;84 process.env.PATH = '';fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 85why, one step back85 await t.throws(m('cat-names', {preferLocal: false}), /spawn .* ENOENT/);Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 86why, one step back86 process.env.PATH = _path;add `input` optionJames Talmagebefore adoption605a674
lines 87–89why, one step back87});8889test('input option can be a String', async t => {fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 90why, one step back90 const {stdout} = await m('stdin', {input: 'foobar'});add `input` optionJames Talmagebefore adoption605a674
lines 91–94why, one step back91 t.is(stdout, 'foobar');92});9394test('input option can be a Buffer', async t => {fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 95why, one step back95 const {stdout} = await m('stdin', {input: 'testing12'});add `input` optionJames Talmagebefore adoption605a674
lines 96–99why, one step back96 t.is(stdout, 'testing12');97});9899test('input can be a Stream', async t => {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 100why, one step back100 const s = new stream.PassThrough();add `input` optionJames Talmagebefore adoption605a674
lines 101–102why, one step back101 s.write('howdy');102 s.end();fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 103why, one step back103 const {stdout} = await m('stdin', {input: s});add `input` optionJames Talmagebefore adoption605a674
lines 104–106why, one step back104 t.is(stdout, 'howdy');105});106return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba
lines 107–112why, one step back107test('you can write to child.stdin', async t => {108 const child = m('stdin');109 child.stdin.end('unicorns');110 t.is((await child).stdout, 'unicorns');111});112add `input` optionJames Talmagebefore adoption605a674
line 113why, one step back113test('input option can be a String - sync', async t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 114why, one step back114 const {stdout} = m.sync('stdin', {input: 'foobar'});add `input` optionJames Talmagebefore adoption605a674
lines 115–118why, one step back115 t.is(stdout, 'foobar');116});117118test('input option can be a Buffer - sync', async t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 119why, one step back119 const {stdout} = m.sync('stdin', {input: new Buffer('testing12', 'utf8')});add `input` optionJames Talmagebefore adoption605a674
line 120why, one step back120 t.is(stdout, 'testing12');Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 121–123why, one step back121});122123test('opts.stdout:ignore - stdout will not collect data', async t => {fix XO lint issuesSindre Sorhusbefore adoption342096c
lines 124–127why, one step back124 const {stdout} = await m('stdin', {125 input: 'hello',126 stdio: [null, 'ignore', null]127 });Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 128why, one step back128 t.is(stdout, null);add `input` optionJames Talmagebefore adoption605a674
lines 129–132why, one step back129});130131test('helpful error trying to provide an input stream in sync mode', t => {132 t.throws(fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 133why, one step back133 () => m.sync('stdin', {input: new stream.PassThrough()}),add `input` optionJames Talmagebefore adoption605a674
lines 134–135why, one step back134 /The `input` option cannot be a stream in sync mode/135 );Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 136–138why, one step back136});137138test('execa() returns a promise with kill() and pid', t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 139why, one step back139 const promise = m('noop', ['foo']);Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 140–141why, one step back140 t.is(typeof promise.kill, 'function');141 t.is(typeof promise.pid, 'number');142});add max-buffer support (#37)James Talmagebefore adoption6288806
lines 143–152why, one step back143144test('maxBuffer affects stdout', t => {145 t.throws(m('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /stdout maxBuffer exceeded/);146 t.notThrows(m('max-buffer', ['stdout', '10'], {maxBuffer: 10}));147});148149test('maxBuffer affects stderr', t => {150 t.throws(m('max-buffer', ['stderr', '13'], {maxBuffer: 12}), /stderr maxBuffer exceeded/);151 t.notThrows(m('max-buffer', ['stderr', '12'], {maxBuffer: 12}));152});Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
lines 153–167why, one step back153154test('skip throwing when using reject option', async t => {155 const err = await t.notThrows(m('exit', ['2'], {reject: false}));156 t.is(typeof err.stdout, 'string');157 t.is(typeof err.stderr, 'string');158});159160test('execa() returns code and failed properties', async t => {161 const {code, failed} = await m('noop', ['foo']);162 const err = await t.throws(m('exit', ['2']));163 t.is(code, 0);164 t.false(failed);165 t.is(err.code, 2);166 t.true(err.failed);167});Add test for `../` paths (#43)James Talmagebefore adoption56a20b1
lines 168–173why, one step back168169test(`use relative path with '..' chars`, async t => {170 const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');171 const {stdout} = await m(pathViaParentDir, ['foo']);172 t.is(stdout, 'foo');173});add tests (#48)James Talmagebefore adoptionb15483a
line 174why, one step back174add test case for throwing on non-executables (#49)Markus Wolfbefore adoptione10d263
line 175why, one step back175if (process.platform !== 'win32') {ensure `execa` rejects and not throws when the core spawn throws and errorSindre Sorhusbefore adoption03964c1
lines 176–178why, one step back176 test('execa() rejects if running non-executable', t => {177 const cp = m('non-executable');178 t.throws(cp);add test case for throwing on non-executables (#49)Markus Wolfbefore adoptione10d263
lines 179–181why, one step back179 });180}181add tests (#48)James Talmagebefore adoptionb15483a
lines 182–184why, one step back182test('err.killed is true if process was killed directly', async t => {183 const cp = m('forever');184[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 185why, one step back185 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 186–197why, one step back186 cp.kill();187 }, 100);188189 const err = await t.throws(cp);190191 t.true(err.killed);192});193194// TODO: Should this really be the case, or should we improve on child_process?195test('err.killed is false if process was killed indirectly', async t => {196 const cp = m('forever');197[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 198why, one step back198 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 199–214why, one step back199 process.kill(cp.pid, 'SIGINT');200 }, 100);201202 const err = await t.throws(cp);203204 t.false(err.killed);205});206207if (process.platform === 'darwin') {208 test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {209 const cp = childProcess.exec('forever', err => {210 t.truthy(err);211 t.false(err.killed);212 t.end();213 });214[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 215why, one step back215 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 216–224why, one step back216 process.kill(cp.pid, 'SIGINT');217 }, 100);218 });219}220221if (process.platform !== 'win32') {222 test('err.signal is SIGINT', async t => {223 const cp = m('forever');224[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 225why, one step back225 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 226–236why, one step back226 process.kill(cp.pid, 'SIGINT');227 }, 100);228229 const err = await t.throws(cp);230231 t.is(err.signal, 'SIGINT');232 });233234 test('err.signal is SIGTERM', async t => {235 const cp = m('forever');236[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 237why, one step back237 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 238–265why, one step back238 process.kill(cp.pid, 'SIGTERM');239 }, 100);240241 const err = await t.throws(cp);242243 t.is(err.signal, 'SIGTERM');244 });245}246247test('result.signal is null for successful execution', async t => {248 t.is((await m('noop')).signal, null);249});250251test('result.signal is null if process failed, but was not killed', async t => {252 const err = await t.throws(m('exit', [2]));253 t.is(err.signal, null);254});255256async function code(t, num) {257 const err = await t.throws(m('exit', [`${num}`]));258259 t.is(err.code, num);260}261262test('err.code is 2', code, 2);263test('err.code is 3', code, 3);264test('err.code is 4', code, 4);265Reimplement missing features. Fixes #30 & #32 (#68)James Talmagebefore adoptione96350d
lines 266–291why, one step back266test('timeout will kill the process early', async t => {267 const err = await t.throws(m('delay', ['3000', '0'], {timeout: 1500}));268269 t.true(err.timedOut);270 t.not(err.code, 22);271});272273test('timeout will not kill the process early', async t => {274 const err = await t.throws(m('delay', ['3000', '22'], {timeout: 9000}));275276 t.false(err.timedOut);277 t.is(err.code, 22);278});279280test('timedOut will be false if no timeout was set and zero exit code', async t => {281 const result = await m('delay', ['1000', '0']);282283 t.false(result.timedOut);284});285286test('timedOut will be false if no timeout was set and non-zero exit code', async t => {287 const err = await t.throws(m('delay', ['1000', '3']));288289 t.false(err.timedOut);290});291add tests (#48)James Talmagebefore adoptionb15483a
lines 292–317why, one step back292async function errorMessage(t, expected, ...args) {293 const err = await t.throws(m('exit', args));294295 t.regex(err.message, expected);296}297298errorMessage.title = (message, expected) => `err.message matches: ${expected}`;299300test(errorMessage, /Command failed: exit 2 foo bar/, 2, 'foo', 'bar');301test(errorMessage, /Command failed: exit 3 baz quz/, 3, 'baz', 'quz');302303async function cmd(t, expected, ...args) {304 const err = await t.throws(m('fail', args));305306 t.is(err.cmd, `fail${expected}`);307308 const result = await m('noop', args);309310 t.is(result.cmd, `noop${expected}`);311}312313cmd.title = (message, expected) => `cmd is: ${JSON.stringify(expected)}`;314315test(cmd, ' foo bar', 'foo', 'bar');316test(cmd, ' baz quz', 'baz', 'quz');317test(cmd, '');Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 318–350why, one step back318319async function spawnAndKill(t, signal, cleanup) {320 const name = cleanup ? 'sub-process' : 'sub-process-false';321 const cp = m(name);322 let pid;323324 cp.stdout.setEncoding('utf8');325 cp.stdout.on('data', chunk => {326 pid = parseInt(chunk, 10);327 t.is(typeof pid, 'number');328329 setTimeout(() => {330 process.kill(cp.pid, signal);331 }, 100);332 });333334 await t.throws(cp);335336 // Give everybody some time to breath and kill things337 await delay(200);338339 t.false(isRunning(cp.pid));340 t.is(isRunning(pid), !cleanup);341}342343test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);344test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);345346if (process.platform !== 'win32') {347 // On Windows the subprocesses are actually always killed348 test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);349 test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);350}fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
line 351why, one step back351mark failing Windows test as known failingSindre Sorhusbefore adoption9699ea2
line 352why, one step back352// see: https://github.com/sindresorhus/execa/issues/56only set to failing test on WindowsSindre Sorhusbefore adoption7d88adc
lines 353–354why, one step back353const onlyWinFailing = test[process.platform === 'win32' ? 'failing' : 'serial'];354onlyWinFailing('execa.shell() supports the `shell` option', async t => {fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
lines 355–359why, one step back355 const {stdout} = await m.shell('noop foo', {356 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'357 });358 t.is(stdout, 'foo');359});