add tests (#48)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by James Talmage · committed by Sindre Sorhus · parent ac7a103 · Browse files · 6 files·+153 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+153 added−16 removed
- other:+153 added−16 removed
Provenance
- Commit
b15483a- Tree
ac11483- Author
James Talmage <james.talmage@jrtechnical.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
ac7a103
6 files changed+153 added−16 removed
added:fixtures/fail+4 added
1+#!/usr/bin/env node2+'use strict';3+4+process.exit(2);added:fixtures/forever+4 added
1+#!/usr/bin/env node2+'use strict';3+4+setTimeout(function () {}, 20000);modified:.travis.yml+2 added-1 removed
2 unmodified lines34567782 unmodified lines - '6' - '5' - '4' - '0.12'after_success: npm run coverallsafter_script: - 'cat coverage/lcov.info | ./node_modules/.bin/coveralls'modified:index.js+17 added-12 removed
133 unmodified lines134 });135 }136 137 module.exports = function (cmd, args, opts) {138+ var joinedCmd = cmd;139+140+ if (Array.isArray(args) && args.length) {141+ joinedCmd += ' ' + args.join(' ');142+ }143+144 var parsed = handleArgs(cmd, args, opts);145 var encoding = parsed.opts.encoding;146 var maxBuffer = parsed.opts.maxBuffer;147 var spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);11 unmodified lines159 var code = result.code;160 var signal = result.signal;161 162 if (err || code !== 0 || signal !== null) {157- var joinedCmd = cmd;158-159- if (Array.isArray(args) && args.length) {160- joinedCmd += ' ' + args.join(' ');161- }162-163 if (!err) {164 err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);165 166- // TODO: missing some timeout logic for killed167- // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203168- // err.killed = spawned.killed || killed;169- err.killed = spawned.killed;170-166 err.code = code < 0 ? errname(code) : code;167 }168 169+ // TODO: missing some timeout logic for killed170+ // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203171+ // err.killed = spawned.killed || killed;172+ err.killed = err.killed || spawned.killed;173+174 err.stdout = stdout;175 err.stderr = stderr;176 err.failed = true;177+ err.signal = signal || null;178+ err.cmd = joinedCmd;179 180 if (!parsed.opts.reject) {181 return err;182 }4 unmodified lines187 return {188 stdout: handleOutput(parsed.opts, stdout),189 stderr: handleOutput(parsed.opts, stderr),190 code: 0,189- failed: false191+ failed: false,192+ killed: false,193+ signal: null,194+ cmd: joinedCmd195 };196 });197 198 crossSpawn._enoent.hookChildProcess(spawned, parsed);47 unmodified linesmodified:package.json+12 added-2 removed
18 unmodified lines192021222324232425262729 unmodified lines575859606162636465666768697071727318 unmodified lines "engines": { "node": ">=0.12" }, "scripts": { "test": "xo && nyc ava", "coveralls": "nyc report --reporter=text-lcov | coveralls" "test": "xo && nyc ava" }, "files": [ "index.js", "lib"29 unmodified lines "cat-names": "^1.0.2", "coveralls": "^2.11.9", "nyc": "^6.4.0", "xo": "*" }, "nyc": { "reporter": [ "text", "lcov" ], "exclude": [ "**/fixtures/**", "**/test.js", "**/test/**" ] }}modified:test.js+114 added-1 removed
1 import path from 'path';2 import stream from 'stream';3+import childProcess from 'child_process';4 import test from 'ava';5 import getStream from 'get-stream';6 import m from './';7 25 unmodified lines33 t.is(typeof err.stdout, 'string');34 t.is(typeof err.stderr, 'string');35 });36 36-test('include stdout in errors for improved debugging', async t => {37+test('include stdout and stderr in errors for improved debugging', async t => {38 const err = await t.throws(m('fixtures/error-message.js'));39 t.regex(err.message, /stdout/);40+ t.regex(err.message, /stderr/);41 });42 43 test('execa.shell()', async t => {44 const {stdout} = await m.shell('node fixtures/noop foo');119 unmodified lines164 const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');165 const {stdout} = await m(pathViaParentDir, ['foo']);166 t.is(stdout, 'foo');167 });168+169+test('err.killed is true if process was killed directly', async t => {170+ const cp = m('forever');171+172+ setTimeout(function () {173+ cp.kill();174+ }, 100);175+176+ const err = await t.throws(cp);177+178+ t.true(err.killed);179+});180+181+// TODO: Should this really be the case, or should we improve on child_process?182+test('err.killed is false if process was killed indirectly', async t => {183+ const cp = m('forever');184+185+ setTimeout(function () {186+ process.kill(cp.pid, 'SIGINT');187+ }, 100);188+189+ const err = await t.throws(cp);190+191+ t.false(err.killed);192+});193+194+if (process.platform === 'darwin') {195+ test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {196+ const cp = childProcess.exec('forever', err => {197+ t.truthy(err);198+ t.false(err.killed);199+ t.end();200+ });201+202+ setTimeout(function () {203+ process.kill(cp.pid, 'SIGINT');204+ }, 100);205+ });206+}207+208+if (process.platform !== 'win32') {209+ test('err.signal is SIGINT', async t => {210+ const cp = m('forever');211+212+ setTimeout(function () {213+ process.kill(cp.pid, 'SIGINT');214+ }, 100);215+216+ const err = await t.throws(cp);217+218+ t.is(err.signal, 'SIGINT');219+ });220+221+ test('err.signal is SIGTERM', async t => {222+ const cp = m('forever');223+224+ setTimeout(function () {225+ process.kill(cp.pid, 'SIGTERM');226+ }, 100);227+228+ const err = await t.throws(cp);229+230+ t.is(err.signal, 'SIGTERM');231+ });232+}233+234+test('result.signal is null for successful execution', async t => {235+ t.is((await m('noop')).signal, null);236+});237+238+test('result.signal is null if process failed, but was not killed', async t => {239+ const err = await t.throws(m('exit', [2]));240+ t.is(err.signal, null);241+});242+243+async function code(t, num) {244+ const err = await t.throws(m('exit', [`${num}`]));245+246+ t.is(err.code, num);247+}248+249+test('err.code is 2', code, 2);250+test('err.code is 3', code, 3);251+test('err.code is 4', code, 4);252+253+async function errorMessage(t, expected, ...args) {254+ const err = await t.throws(m('exit', args));255+256+ t.regex(err.message, expected);257+}258+259+errorMessage.title = (message, expected) => `err.message matches: ${expected}`;260+261+test(errorMessage, /Command failed: exit 2 foo bar/, 2, 'foo', 'bar');262+test(errorMessage, /Command failed: exit 3 baz quz/, 3, 'baz', 'quz');263+264+async function cmd(t, expected, ...args) {265+ const err = await t.throws(m('fail', args));266+267+ t.is(err.cmd, `fail${expected}`);268+269+ const result = await m('noop', args);270+271+ t.is(result.cmd, `noop${expected}`);272+}273+274+cmd.title = (message, expected) => `cmd is: ${JSON.stringify(expected)}`;275+276+test(cmd, ' foo bar', 'foo', 'bar');277+test(cmd, ' baz quz', 'baz', 'quz');278+test(cmd, '');