test.js
This file
Path
test.js
413 lines · 11.3 KiB · JavaScript
Last changed by
Better stdio option handling - fixes #24Sam Verschuerenbefore adoption9cd5e2a
History of this fileTake it
fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 1why, one step back1import path from 'path';Better stdio option handling - fixes #24Sam Verschuerenbefore adoption9cd5e2a
line 2why, one step back2import fs from 'fs';add `input` optionJames Talmagebefore adoption605a674
line 3why, one step back3import stream from 'stream';add tests (#48)James Talmagebefore adoptionb15483a
line 4why, one step back4import childProcess from 'child_process';5import test from 'ava';make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
line 6why, one step back6import getStream from 'get-stream';Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 7–8why, one step back7import isRunning from 'is-running';8import delay from 'delay';Better stdio option handling - fixes #24Sam Verschuerenbefore adoption9cd5e2a
line 9why, one step back9import tempfile from 'tempfile';tweaksSindre Sorhusbefore adoptione3505a8
line 10why, one step back10import m from './';11fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 12–13why, one step back12process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;1314test('execa()', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 15why, one step back15 const {stdout} = await m('noop', ['foo']);initSindre Sorhusbefore adoptionbc16d6b
lines 16–1916 t.is(stdout, 'foo');17});1819test('buffer', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 20why, one step back20 const {stdout} = await m('noop', ['foo'], {encoding: null});initSindre Sorhusbefore adoptionbc16d6b
lines 21–2421 t.true(Buffer.isBuffer(stdout));22 t.is(stdout.toString(), 'foo');23});24add `execa.stdout()` shortcut (#14)Sindre Sorhusbefore adoption868f505
lines 25–34why, one step back25test('execa.stdout()', async t => {26 const stdout = await m.stdout('noop', ['foo']);27 t.is(stdout, 'foo');28});2930test('execa.stderr()', async t => {31 const stderr = await m.stderr('noop-err', ['foo']);32 t.is(stderr, 'foo');33});34add `stdout`/`stderr` to errorsSindre Sorhusbefore adoption0ea8aaf
line 35why, one step back35test('stdout/stderr available on errors', async t => {meta tweaksSindre Sorhusbefore adoption858af26
lines 36–38why, one step back36 const err = await t.throws(m('exit', ['2']));37 t.is(typeof err.stdout, 'string');38 t.is(typeof err.stderr, 'string');add `stdout`/`stderr` to errorsSindre Sorhusbefore adoption0ea8aaf
lines 39–40why, one step back39});40add tests (#48)James Talmagebefore adoptionb15483a
line 41why, one step back41test('include stdout and stderr in errors for improved debugging', async t => {Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 42–43why, one step back42 const err = await t.throws(m('fixtures/error-message.js'));43 t.regex(err.message, /stdout/);add tests (#48)James Talmagebefore adoptionb15483a
line 44why, one step back44 t.regex(err.message, /stderr/);Fix test for exit code 1 on windows (#78)Sam Verschuerenbefore adoption4231a10
line 45why, one step back45 t.is(err.code, 1);include stdout in errors for improved debuggingSindre Sorhusbefore adoptionaea8c9a
lines 46–47why, one step back46});47Do not output stderr and stdout on stdio inherit - fixes #70 (#79)Sam Verschuerenbefore adoption2a12986
lines 48–49why, one step back48test('do not include in errors when `stdio` is set to `inherit`', async t => {49 const err = await t.throws(m('fixtures/error-message.js', {stdio: 'inherit'}));Better stdio option handling - fixes #24Sam Verschuerenbefore adoption9cd5e2a
lines 50–84why, one step back50 t.notRegex(err.message, /\n/);51});5253test('do not include `stderr` and `stdout` in errors when set to `inherit`', async t => {54 const err = await t.throws(m('fixtures/error-message.js', {stdout: 'inherit', stderr: 'inherit'}));55 t.notRegex(err.message, /\n/);56});5758test('do not include `stderr` and `stdout` in errors when `stdio` is set to `inherit`', async t => {59 const err = await t.throws(m('fixtures/error-message.js', {stdio: [null, 'inherit', 'inherit']}));60 t.notRegex(err.message, /\n/);61});6263test('do not include `stdout` in errors when set to `inherit`', async t => {64 const err = await t.throws(m('fixtures/error-message.js', {stdout: 'inherit'}));65 t.notRegex(err.message, /stdout/);66 t.regex(err.message, /stderr/);67});6869test('do not include `stderr` in errors when set to `inherit`', async t => {70 const err = await t.throws(m('fixtures/error-message.js', {stderr: 'inherit'}));71 t.regex(err.message, /stdout/);72 t.notRegex(err.message, /stderr/);73});7475test('pass `stdout` to a file descriptor', async t => {76 const file = tempfile('.txt');77 await m('fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});78 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');79});8081test('pass `stderr` to a file descriptor', async t => {82 const file = tempfile('.txt');83 await m('fixtures/noop-err', ['foo bar'], {stderr: fs.openSync(file, 'w')});84 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');Do not output stderr and stdout on stdio inherit - fixes #70 (#79)Sam Verschuerenbefore adoption2a12986
lines 85–86why, one step back85});86Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 87why, one step back87test('execa.shell()', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 88why, one step back88 const {stdout} = await m.shell('node fixtures/noop foo');Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
lines 89–91why, one step back89 t.is(stdout, 'foo');90});91make `execa.spawn()` use the same argument normalization as `execa()`Sindre Sorhusbefore adoptiond6e3220
line 92why, one step back92test('execa.spawn()', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 93–94why, one step back93 t.is(typeof m.spawn('noop').pid, 'number');94 t.is((await getStream(m.spawn('noop', ['foo']).stdout)).trim(), 'foo');expose `.spawn`Sindre Sorhusbefore adoptionb65479c
lines 95–96why, one step back95});96add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
line 97why, one step back97test('execa.sync()', t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 98why, one step back98 const {stdout} = m.sync('noop', ['foo']);add `execa.sync()` (#7)Sindre Sorhusbefore adoption9345d2c
lines 99–101why, one step back99 t.is(stdout, 'foo');100});101Throw error in sync method - fixes #66 (#67)Sam Verschuerenbefore adoptionc8c9c7b
lines 102–105why, one step back102test('execa.sync() throws error if written to stderr', t => {103 t.throws(() => m.sync('foo'), process.platform === 'win32' ? /'foo' is not recognized as an internal or external command/ : 'spawnSync foo ENOENT');104});105add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
line 106why, one step back106test('execa.shellSync()', t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 107why, one step back107 const {stdout} = m.shellSync('node fixtures/noop foo');add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 108–110why, one step back108 t.is(stdout, 'foo');109});110111test('stripEof option', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 112why, one step back112 const {stdout} = await m('noop', ['foo'], {stripEof: false});initSindre Sorhusbefore adoptionbc16d6b
lines 113–115113 t.is(stdout, 'foo\n');114});115Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 116why, one step back116test.serial('preferLocal option', async t => {tweaksSindre Sorhusbefore adoptione3505a8
line 117why, one step back117 t.true((await m('cat-names')).stdout.length > 2);Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 118why, one step back118fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 119–123why, one step back119 if (process.platform === 'win32') {120 // TODO: figure out how to make the below not hang on Windows121 return;122 }123Fix linting (#77)Sam Verschuerenbefore adoption80a059d
line 124why, one step back124 // Account for npm adding local binaries to the PATHMerge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
lines 125–126why, one step back125 const _path = process.env.PATH;126 process.env.PATH = '';fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 127why, one step back127 await t.throws(m('cat-names', {preferLocal: false}), /spawn .* ENOENT/);Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 128why, one step back128 process.env.PATH = _path;add `input` optionJames Talmagebefore adoption605a674
lines 129–131why, one step back129});130131test('input option can be a String', async t => {fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 132why, one step back132 const {stdout} = await m('stdin', {input: 'foobar'});add `input` optionJames Talmagebefore adoption605a674
lines 133–136why, one step back133 t.is(stdout, 'foobar');134});135136test('input option can be a Buffer', async t => {fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 137why, one step back137 const {stdout} = await m('stdin', {input: 'testing12'});add `input` optionJames Talmagebefore adoption605a674
lines 138–141why, one step back138 t.is(stdout, 'testing12');139});140141test('input can be a Stream', async t => {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 142why, one step back142 const s = new stream.PassThrough();add `input` optionJames Talmagebefore adoption605a674
lines 143–144why, one step back143 s.write('howdy');144 s.end();fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 145why, one step back145 const {stdout} = await m('stdin', {input: s});add `input` optionJames Talmagebefore adoption605a674
lines 146–148why, one step back146 t.is(stdout, 'howdy');147});148return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba
lines 149–154why, one step back149test('you can write to child.stdin', async t => {150 const child = m('stdin');151 child.stdin.end('unicorns');152 t.is((await child).stdout, 'unicorns');153});154Fix linting (#77)Sam Verschuerenbefore adoption80a059d
line 155why, one step back155test('input option can be a String - sync', t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 156why, one step back156 const {stdout} = m.sync('stdin', {input: 'foobar'});add `input` optionJames Talmagebefore adoption605a674
lines 157–159why, one step back157 t.is(stdout, 'foobar');158});159Fix linting (#77)Sam Verschuerenbefore adoption80a059d
lines 160–161why, one step back160test('input option can be a Buffer - sync', t => {161 const {stdout} = m.sync('stdin', {input: Buffer.from('testing12', 'utf8')});add `input` optionJames Talmagebefore adoption605a674
line 162why, one step back162 t.is(stdout, 'testing12');Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 163–165why, one step back163});164165test('opts.stdout:ignore - stdout will not collect data', async t => {fix XO lint issuesSindre Sorhusbefore adoption342096c
lines 166–169why, one step back166 const {stdout} = await m('stdin', {167 input: 'hello',168 stdio: [null, 'ignore', null]169 });Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 170why, one step back170 t.is(stdout, null);add `input` optionJames Talmagebefore adoption605a674
lines 171–174why, one step back171});172173test('helpful error trying to provide an input stream in sync mode', t => {174 t.throws(fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 175why, one step back175 () => m.sync('stdin', {input: new stream.PassThrough()}),add `input` optionJames Talmagebefore adoption605a674
lines 176–177why, one step back176 /The `input` option cannot be a stream in sync mode/177 );Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 178–180why, one step back178});179180test('execa() returns a promise with kill() and pid', t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 181why, one step back181 const promise = m('noop', ['foo']);Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 182–183why, one step back182 t.is(typeof promise.kill, 'function');183 t.is(typeof promise.pid, 'number');184});add max-buffer support (#37)James Talmagebefore adoption6288806
lines 185–194why, one step back185186test('maxBuffer affects stdout', t => {187 t.throws(m('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /stdout maxBuffer exceeded/);188 t.notThrows(m('max-buffer', ['stdout', '10'], {maxBuffer: 10}));189});190191test('maxBuffer affects stderr', t => {192 t.throws(m('max-buffer', ['stderr', '13'], {maxBuffer: 12}), /stderr maxBuffer exceeded/);193 t.notThrows(m('max-buffer', ['stderr', '12'], {maxBuffer: 12}));194});Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
lines 195–209why, one step back195196test('skip throwing when using reject option', async t => {197 const err = await t.notThrows(m('exit', ['2'], {reject: false}));198 t.is(typeof err.stdout, 'string');199 t.is(typeof err.stderr, 'string');200});201202test('execa() returns code and failed properties', async t => {203 const {code, failed} = await m('noop', ['foo']);204 const err = await t.throws(m('exit', ['2']));205 t.is(code, 0);206 t.false(failed);207 t.is(err.code, 2);208 t.true(err.failed);209});Add test for `../` paths (#43)James Talmagebefore adoption56a20b1
lines 210–215why, one step back210211test(`use relative path with '..' chars`, async t => {212 const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');213 const {stdout} = await m(pathViaParentDir, ['foo']);214 t.is(stdout, 'foo');215});add tests (#48)James Talmagebefore adoptionb15483a
line 216why, one step back216add test case for throwing on non-executables (#49)Markus Wolfbefore adoptione10d263
line 217why, one step back217if (process.platform !== 'win32') {ensure `execa` rejects and not throws when the core spawn throws and errorSindre Sorhusbefore adoption03964c1
lines 218–220why, one step back218 test('execa() rejects if running non-executable', t => {219 const cp = m('non-executable');220 t.throws(cp);add test case for throwing on non-executables (#49)Markus Wolfbefore adoptione10d263
lines 221–223why, one step back221 });222}223add tests (#48)James Talmagebefore adoptionb15483a
lines 224–226why, one step back224test('err.killed is true if process was killed directly', async t => {225 const cp = m('forever');226[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 227why, one step back227 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 228–239why, one step back228 cp.kill();229 }, 100);230231 const err = await t.throws(cp);232233 t.true(err.killed);234});235236// TODO: Should this really be the case, or should we improve on child_process?237test('err.killed is false if process was killed indirectly', async t => {238 const cp = m('forever');239[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 240why, one step back240 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 241–256why, one step back241 process.kill(cp.pid, 'SIGINT');242 }, 100);243244 const err = await t.throws(cp);245246 t.false(err.killed);247});248249if (process.platform === 'darwin') {250 test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {251 const cp = childProcess.exec('forever', err => {252 t.truthy(err);253 t.false(err.killed);254 t.end();255 });256[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 257why, one step back257 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 258–266why, one step back258 process.kill(cp.pid, 'SIGINT');259 }, 100);260 });261}262263if (process.platform !== 'win32') {264 test('err.signal is SIGINT', async t => {265 const cp = m('forever');266[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 267why, one step back267 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 268–278why, one step back268 process.kill(cp.pid, 'SIGINT');269 }, 100);270271 const err = await t.throws(cp);272273 t.is(err.signal, 'SIGINT');274 });275276 test('err.signal is SIGTERM', async t => {277 const cp = m('forever');278[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 279why, one step back279 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 280–307why, one step back280 process.kill(cp.pid, 'SIGTERM');281 }, 100);282283 const err = await t.throws(cp);284285 t.is(err.signal, 'SIGTERM');286 });287}288289test('result.signal is null for successful execution', async t => {290 t.is((await m('noop')).signal, null);291});292293test('result.signal is null if process failed, but was not killed', async t => {294 const err = await t.throws(m('exit', [2]));295 t.is(err.signal, null);296});297298async function code(t, num) {299 const err = await t.throws(m('exit', [`${num}`]));300301 t.is(err.code, num);302}303304test('err.code is 2', code, 2);305test('err.code is 3', code, 3);306test('err.code is 4', code, 4);307Reimplement missing features. Fixes #30 & #32 (#68)James Talmagebefore adoptione96350d
lines 308–333why, one step back308test('timeout will kill the process early', async t => {309 const err = await t.throws(m('delay', ['3000', '0'], {timeout: 1500}));310311 t.true(err.timedOut);312 t.not(err.code, 22);313});314315test('timeout will not kill the process early', async t => {316 const err = await t.throws(m('delay', ['3000', '22'], {timeout: 9000}));317318 t.false(err.timedOut);319 t.is(err.code, 22);320});321322test('timedOut will be false if no timeout was set and zero exit code', async t => {323 const result = await m('delay', ['1000', '0']);324325 t.false(result.timedOut);326});327328test('timedOut will be false if no timeout was set and non-zero exit code', async t => {329 const err = await t.throws(m('delay', ['1000', '3']));330331 t.false(err.timedOut);332});333add tests (#48)James Talmagebefore adoptionb15483a
lines 334–359why, one step back334async function errorMessage(t, expected, ...args) {335 const err = await t.throws(m('exit', args));336337 t.regex(err.message, expected);338}339340errorMessage.title = (message, expected) => `err.message matches: ${expected}`;341342test(errorMessage, /Command failed: exit 2 foo bar/, 2, 'foo', 'bar');343test(errorMessage, /Command failed: exit 3 baz quz/, 3, 'baz', 'quz');344345async function cmd(t, expected, ...args) {346 const err = await t.throws(m('fail', args));347348 t.is(err.cmd, `fail${expected}`);349350 const result = await m('noop', args);351352 t.is(result.cmd, `noop${expected}`);353}354355cmd.title = (message, expected) => `cmd is: ${JSON.stringify(expected)}`;356357test(cmd, ' foo bar', 'foo', 'bar');358test(cmd, ' baz quz', 'baz', 'quz');359test(cmd, '');Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 360–392why, one step back360361async function spawnAndKill(t, signal, cleanup) {362 const name = cleanup ? 'sub-process' : 'sub-process-false';363 const cp = m(name);364 let pid;365366 cp.stdout.setEncoding('utf8');367 cp.stdout.on('data', chunk => {368 pid = parseInt(chunk, 10);369 t.is(typeof pid, 'number');370371 setTimeout(() => {372 process.kill(cp.pid, signal);373 }, 100);374 });375376 await t.throws(cp);377378 // Give everybody some time to breath and kill things379 await delay(200);380381 t.false(isRunning(cp.pid));382 t.is(isRunning(pid), !cleanup);383}384385test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);386test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);387388if (process.platform !== 'win32') {389 // On Windows the subprocesses are actually always killed390 test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);391 test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);392}fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
line 393why, one step back393Fix linting (#77)Sam Verschuerenbefore adoption80a059d
line 394why, one step back394// See: https://github.com/sindresorhus/execa/issues/56only set to failing test on WindowsSindre Sorhusbefore adoption7d88adc
lines 395–396why, one step back395const onlyWinFailing = test[process.platform === 'win32' ? 'failing' : 'serial'];396onlyWinFailing('execa.shell() supports the `shell` option', async t => {fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
lines 397–401why, one step back397 const {stdout} = await m.shell('noop foo', {398 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'399 });400 t.is(stdout, 'foo');401});Attach error listener to stdio stream (#74)Vsevolod Strukchinskybefore adoptiona71bef2
lines 402–404why, one step back402403if (process.platform !== 'win32') {404 test('write to fast-exit process', async t => {Fix linting (#77)Sam Verschuerenbefore adoption80a059d
lines 405–406why, one step back405 // Try-catch here is necessary, because this test is not 100% accurate406 // Sometimes process can manage to accept input before exitingAttach error listener to stdio stream (#74)Vsevolod Strukchinskybefore adoptiona71bef2
lines 407–413why, one step back407 try {408 await m(`fast-exit-${process.platform}`, [], {input: 'data'});409 } catch (err) {410 t.is(err.code, 'EPIPE');411 }412 });413}