test.js
This file
Path
test.js
328 lines · 8.4 KiB · JavaScript
Last changed by
mark failing Windows test as known failingSindre Sorhusbefore adoption9699ea2
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});59add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
line 60why, one step back60test('execa.shellSync()', t => {[BREAKING] for consistency return a result object from `execa.sync()` instead of string (#22)James Talmagebefore adoptiona8f2100
line 61why, one step back61 const {stdout} = m.shellSync('node fixtures/noop foo');add `execa.shellSync()`Sindre Sorhusbefore adoption71a58ee
lines 62–64why, one step back62 t.is(stdout, 'foo');63});6465test('stripEof option', async t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 66why, one step back66 const {stdout} = await m('noop', ['foo'], {stripEof: false});initSindre Sorhusbefore adoptionbc16d6b
lines 67–6967 t.is(stdout, 'foo\n');68});69Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 70why, one step back70test.serial('preferLocal option', async t => {tweaksSindre Sorhusbefore adoptione3505a8
line 71why, one step back71 t.true((await m('cat-names')).stdout.length > 2);Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 72why, one step back72fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
lines 73–77why, one step back73 if (process.platform === 'win32') {74 // TODO: figure out how to make the below not hang on Windows75 return;76 }77Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
lines 78–80why, one step back78 // account for npm adding local binaries to the PATH79 const _path = process.env.PATH;80 process.env.PATH = '';fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 81why, one step back81 await t.throws(m('cat-names', {preferLocal: false}), /spawn .* ENOENT/);Merge pull request #3 from sindresorhus/local-binsSindre Sorhusbefore adoptiona0c035c
line 82why, one step back82 process.env.PATH = _path;add `input` optionJames Talmagebefore adoption605a674
lines 83–85why, one step back83});8485test('input option can be a String', async t => {fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 86why, one step back86 const {stdout} = await m('stdin', {input: 'foobar'});add `input` optionJames Talmagebefore adoption605a674
lines 87–90why, one step back87 t.is(stdout, 'foobar');88});8990test('input option can be a Buffer', async t => {fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 91why, one step back91 const {stdout} = await m('stdin', {input: 'testing12'});add `input` optionJames Talmagebefore adoption605a674
lines 92–95why, one step back92 t.is(stdout, 'testing12');93});9495test('input can be a Stream', async t => {[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 96why, one step back96 const s = new stream.PassThrough();add `input` optionJames Talmagebefore adoption605a674
lines 97–98why, one step back97 s.write('howdy');98 s.end();fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 99why, one step back99 const {stdout} = await m('stdin', {input: s});add `input` optionJames Talmagebefore adoption605a674
lines 100–102why, one step back100 t.is(stdout, 'howdy');101});102return the child process instead of a promise. (#18)James Talmagebefore adoption99f10ba
lines 103–108why, one step back103test('you can write to child.stdin', async t => {104 const child = m('stdin');105 child.stdin.end('unicorns');106 t.is((await child).stdout, 'unicorns');107});108add `input` optionJames Talmagebefore adoption605a674
line 109why, one step back109test('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 110why, one step back110 const {stdout} = m.sync('stdin', {input: 'foobar'});add `input` optionJames Talmagebefore adoption605a674
lines 111–114why, one step back111 t.is(stdout, 'foobar');112});113114test('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 115why, one step back115 const {stdout} = m.sync('stdin', {input: new Buffer('testing12', 'utf8')});add `input` optionJames Talmagebefore adoption605a674
line 116why, one step back116 t.is(stdout, 'testing12');Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
lines 117–119why, one step back117});118119test('opts.stdout:ignore - stdout will not collect data', async t => {fix XO lint issuesSindre Sorhusbefore adoption342096c
lines 120–123why, one step back120 const {stdout} = await m('stdin', {121 input: 'hello',122 stdio: [null, 'ignore', null]123 });Use spawn instead of execFile (#27)James Talmagebefore adoption31a8d87
line 124why, one step back124 t.is(stdout, null);add `input` optionJames Talmagebefore adoption605a674
lines 125–128why, one step back125});126127test('helpful error trying to provide an input stream in sync mode', t => {128 t.throws(fix option handling in #15Sindre Sorhusbefore adoption3d31e8d
line 129why, one step back129 () => m.sync('stdin', {input: new stream.PassThrough()}),add `input` optionJames Talmagebefore adoption605a674
lines 130–131why, one step back130 /The `input` option cannot be a stream in sync mode/131 );Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 132–134why, one step back132});133134test('execa() returns a promise with kill() and pid', t => {fix tests on Windows 💩Sindre Sorhusbefore adoption14f16df
line 135why, one step back135 const promise = m('noop', ['foo']);Attach pid and kill() to returned promise (#11)Luke Horvatbefore adoption9da5484
lines 136–137why, one step back136 t.is(typeof promise.kill, 'function');137 t.is(typeof promise.pid, 'number');138});add max-buffer support (#37)James Talmagebefore adoption6288806
lines 139–148why, one step back139140test('maxBuffer affects stdout', t => {141 t.throws(m('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /stdout maxBuffer exceeded/);142 t.notThrows(m('max-buffer', ['stdout', '10'], {maxBuffer: 10}));143});144145test('maxBuffer affects stderr', t => {146 t.throws(m('max-buffer', ['stderr', '13'], {maxBuffer: 12}), /stderr maxBuffer exceeded/);147 t.notThrows(m('max-buffer', ['stderr', '12'], {maxBuffer: 12}));148});Add `reject` option (#40)Kevin Mårtenssonbefore adoption9bafd76
lines 149–163why, one step back149150test('skip throwing when using reject option', async t => {151 const err = await t.notThrows(m('exit', ['2'], {reject: false}));152 t.is(typeof err.stdout, 'string');153 t.is(typeof err.stderr, 'string');154});155156test('execa() returns code and failed properties', async t => {157 const {code, failed} = await m('noop', ['foo']);158 const err = await t.throws(m('exit', ['2']));159 t.is(code, 0);160 t.false(failed);161 t.is(err.code, 2);162 t.true(err.failed);163});Add test for `../` paths (#43)James Talmagebefore adoption56a20b1
lines 164–169why, one step back164165test(`use relative path with '..' chars`, async t => {166 const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');167 const {stdout} = await m(pathViaParentDir, ['foo']);168 t.is(stdout, 'foo');169});add tests (#48)James Talmagebefore adoptionb15483a
line 170why, one step back170add test case for throwing on non-executables (#49)Markus Wolfbefore adoptione10d263
line 171why, one step back171if (process.platform !== 'win32') {ensure `execa` rejects and not throws when the core spawn throws and errorSindre Sorhusbefore adoption03964c1
lines 172–174why, one step back172 test('execa() rejects if running non-executable', t => {173 const cp = m('non-executable');174 t.throws(cp);add test case for throwing on non-executables (#49)Markus Wolfbefore adoptione10d263
lines 175–177why, one step back175 });176}177add tests (#48)James Talmagebefore adoptionb15483a
lines 178–180why, one step back178test('err.killed is true if process was killed directly', async t => {179 const cp = m('forever');180[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 181why, one step back181 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 182–193why, one step back182 cp.kill();183 }, 100);184185 const err = await t.throws(cp);186187 t.true(err.killed);188});189190// TODO: Should this really be the case, or should we improve on child_process?191test('err.killed is false if process was killed indirectly', async t => {192 const cp = m('forever');193[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 194why, one step back194 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 195–210why, one step back195 process.kill(cp.pid, 'SIGINT');196 }, 100);197198 const err = await t.throws(cp);199200 t.false(err.killed);201});202203if (process.platform === 'darwin') {204 test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {205 const cp = childProcess.exec('forever', err => {206 t.truthy(err);207 t.false(err.killed);208 t.end();209 });210[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 211why, one step back211 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 212–220why, one step back212 process.kill(cp.pid, 'SIGINT');213 }, 100);214 });215}216217if (process.platform !== 'win32') {218 test('err.signal is SIGINT', async t => {219 const cp = m('forever');220[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 221why, one step back221 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 222–232why, one step back222 process.kill(cp.pid, 'SIGINT');223 }, 100);224225 const err = await t.throws(cp);226227 t.is(err.signal, 'SIGINT');228 });229230 test('err.signal is SIGTERM', async t => {231 const cp = m('forever');232[BREAKING] ES2015ify and require Node.js 4Sindre Sorhusbefore adoption8a7a51c
line 233why, one step back233 setTimeout(() => {add tests (#48)James Talmagebefore adoptionb15483a
lines 234–287why, one step back234 process.kill(cp.pid, 'SIGTERM');235 }, 100);236237 const err = await t.throws(cp);238239 t.is(err.signal, 'SIGTERM');240 });241}242243test('result.signal is null for successful execution', async t => {244 t.is((await m('noop')).signal, null);245});246247test('result.signal is null if process failed, but was not killed', async t => {248 const err = await t.throws(m('exit', [2]));249 t.is(err.signal, null);250});251252async function code(t, num) {253 const err = await t.throws(m('exit', [`${num}`]));254255 t.is(err.code, num);256}257258test('err.code is 2', code, 2);259test('err.code is 3', code, 3);260test('err.code is 4', code, 4);261262async function errorMessage(t, expected, ...args) {263 const err = await t.throws(m('exit', args));264265 t.regex(err.message, expected);266}267268errorMessage.title = (message, expected) => `err.message matches: ${expected}`;269270test(errorMessage, /Command failed: exit 2 foo bar/, 2, 'foo', 'bar');271test(errorMessage, /Command failed: exit 3 baz quz/, 3, 'baz', 'quz');272273async function cmd(t, expected, ...args) {274 const err = await t.throws(m('fail', args));275276 t.is(err.cmd, `fail${expected}`);277278 const result = await m('noop', args);279280 t.is(result.cmd, `noop${expected}`);281}282283cmd.title = (message, expected) => `cmd is: ${JSON.stringify(expected)}`;284285test(cmd, ' foo bar', 'foo', 'bar');286test(cmd, ' baz quz', 'baz', 'quz');287test(cmd, '');Add `cleanup` option (#53)Friedel Ziegelmayerbefore adoptiondbd9acd
lines 288–320why, one step back288289async function spawnAndKill(t, signal, cleanup) {290 const name = cleanup ? 'sub-process' : 'sub-process-false';291 const cp = m(name);292 let pid;293294 cp.stdout.setEncoding('utf8');295 cp.stdout.on('data', chunk => {296 pid = parseInt(chunk, 10);297 t.is(typeof pid, 'number');298299 setTimeout(() => {300 process.kill(cp.pid, signal);301 }, 100);302 });303304 await t.throws(cp);305306 // Give everybody some time to breath and kill things307 await delay(200);308309 t.false(isRunning(cp.pid));310 t.is(isRunning(pid), !cleanup);311}312313test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);314test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);315316if (process.platform !== 'win32') {317 // On Windows the subprocesses are actually always killed318 test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);319 test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);320}fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
line 321why, one step back321mark failing Windows test as known failingSindre Sorhusbefore adoption9699ea2
lines 322–323why, one step back322// see: https://github.com/sindresorhus/execa/issues/56323test.failing('execa.shell() supports the `shell` option', async t => {fix support for the `shell` option in `execa.shell()`Sindre Sorhusbefore adoptiona80487b
lines 324–328why, one step back324 const {stdout} = await m.shell('noop foo', {325 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'326 });327 t.is(stdout, 'foo');328});