Skip to content
Myr

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

6 files+153 added−16 removed

added:fixtures/fail+4 added
1#!/usr/bin/env node2'use strict';34process.exit(2);
added:fixtures/forever+4 added
1#!/usr/bin/env node2'use strict';34setTimeout(function () {}, 20000);
modified:.travis.yml+2 added-1 removed
modified:index.js+17 added-12 removed
133 unmodified lines
134 });135}136137module.exports = function (cmd, args, opts) {138 var joinedCmd = cmd;139140 if (Array.isArray(args) && args.length) {141 joinedCmd += ' ' + args.join(' ');142 }143144 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 lines
159 var code = result.code;160 var signal = result.signal;161162 if (err || code !== 0 || signal !== null) {157 var joinedCmd = cmd;158159 if (Array.isArray(args) && args.length) {160 joinedCmd += ' ' + args.join(' ');161 }162163 if (!err) {164 err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);165166 // 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;170166 err.code = code < 0 ? errname(code) : code;167 }168169 // 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;173174 err.stdout = stdout;175 err.stderr = stderr;176 err.failed = true;177 err.signal = signal || null;178 err.cmd = joinedCmd;179180 if (!parsed.opts.reject) {181 return err;182 }
4 unmodified lines
187 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 });197198 crossSpawn._enoent.hookChildProcess(spawned, parsed);
47 unmodified lines
modified:package.json+12 added-2 removed
modified:test.js+114 added-1 removed
1import path from 'path';2import stream from 'stream';3import childProcess from 'child_process';4import test from 'ava';5import getStream from 'get-stream';6import m from './';7
25 unmodified lines
33 t.is(typeof err.stdout, 'string');34 t.is(typeof err.stderr, 'string');35});3636test('include stdout in errors for improved debugging', async t => {37test('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});4243test('execa.shell()', async t => {44 const {stdout} = await m.shell('node fixtures/noop foo');
119 unmodified lines
164 const pathViaParentDir = path.join('..', path.basename(__dirname), 'fixtures', 'noop');165 const {stdout} = await m(pathViaParentDir, ['foo']);166 t.is(stdout, 'foo');167});168169test('err.killed is true if process was killed directly', async t => {170 const cp = m('forever');171172 setTimeout(function () {173 cp.kill();174 }, 100);175176 const err = await t.throws(cp);177178 t.true(err.killed);179});180181// TODO: Should this really be the case, or should we improve on child_process?182test('err.killed is false if process was killed indirectly', async t => {183 const cp = m('forever');184185 setTimeout(function () {186 process.kill(cp.pid, 'SIGINT');187 }, 100);188189 const err = await t.throws(cp);190191 t.false(err.killed);192});193194if (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 });201202 setTimeout(function () {203 process.kill(cp.pid, 'SIGINT');204 }, 100);205 });206}207208if (process.platform !== 'win32') {209 test('err.signal is SIGINT', async t => {210 const cp = m('forever');211212 setTimeout(function () {213 process.kill(cp.pid, 'SIGINT');214 }, 100);215216 const err = await t.throws(cp);217218 t.is(err.signal, 'SIGINT');219 });220221 test('err.signal is SIGTERM', async t => {222 const cp = m('forever');223224 setTimeout(function () {225 process.kill(cp.pid, 'SIGTERM');226 }, 100);227228 const err = await t.throws(cp);229230 t.is(err.signal, 'SIGTERM');231 });232}233234test('result.signal is null for successful execution', async t => {235 t.is((await m('noop')).signal, null);236});237238test('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});242243async function code(t, num) {244 const err = await t.throws(m('exit', [`${num}`]));245246 t.is(err.code, num);247}248249test('err.code is 2', code, 2);250test('err.code is 3', code, 3);251test('err.code is 4', code, 4);252253async function errorMessage(t, expected, ...args) {254 const err = await t.throws(m('exit', args));255256 t.regex(err.message, expected);257}258259errorMessage.title = (message, expected) => `err.message matches: ${expected}`;260261test(errorMessage, /Command failed: exit 2 foo bar/, 2, 'foo', 'bar');262test(errorMessage, /Command failed: exit 3 baz quz/, 3, 'baz', 'quz');263264async function cmd(t, expected, ...args) {265 const err = await t.throws(m('fail', args));266267 t.is(err.cmd, `fail${expected}`);268269 const result = await m('noop', args);270271 t.is(result.cmd, `noop${expected}`);272}273274cmd.title = (message, expected) => `cmd is: ${JSON.stringify(expected)}`;275276test(cmd, ' foo bar', 'foo', 'bar');277test(cmd, ' baz quz', 'baz', 'quz');278test(cmd, '');