Skip to content
Myr

Fix test for `options.cleanup` (#224)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by Sindre Sorhus · parent 4cd3d35 · Browse files · 4 files+26 added−35 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+26 added−35 removed

  • other:+26 added−35 removed

4 files+26 added−35 removed

modified:fixtures/sub-process+4 added-2 removed
1#!/usr/bin/env node2'use strict';3const m = require('../');3const execa = require('..');45console.log(m('forever').pid);5const cleanup = process.argv[2] === 'true';6const subprocess = execa('node', ['./fixtures/forever'], {cleanup});7process.send(subprocess.pid);
deleted:fixtures/sub-process-false-5 removed
1#!/usr/bin/env node2'use strict';3const m = require('../');45console.log(m('forever', {cleanup: false}).pid);
modified:package.json+1 added-1 removed
modified:test.js+21 added-27 removed
3 unmodified lines
4import childProcess from 'child_process';5import test from 'ava';6import getStream from 'get-stream';7import isRunning from 'is-running';8import delay from 'delay';8import tempfile from 'tempfile';9import pEvent from 'p-event';10import execa from '.';1112process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;13process.env.FOO = 'foo';
462 unmodified lines
476test(command, ' foo bar', 'foo', 'bar');477test(command, ' baz quz', 'baz', 'quz');478test(command, '');479480async function spawnAndKill(t, signal, cleanup) {481 const name = cleanup ? 'sub-process' : 'sub-process-false';482 const cp = execa(name);483 let pid;480async function spawnAndKill(t, signal, cleanup, isKilled) {481 const subprocess = execa('sub-process', [cleanup], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});482485 cp.stdout.setEncoding('utf8');486 cp.stdout.on('data', chunk => {487 pid = parseInt(chunk, 10);488 t.is(typeof pid, 'number');483 const pid = await pEvent(subprocess, 'message');484 t.true(Number.isInteger(pid));485 t.true(isRunning(pid));486490 setTimeout(() => {491 process.kill(cp.pid, signal);487 process.kill(subprocess.pid, signal);492 }, 100);493 });488495 await t.throwsAsync(cp);489 await t.throwsAsync(subprocess);490497 // Give everybody some time to breath and kill things498 await delay(200);499500 t.false(isRunning(cp.pid));501 t.is(isRunning(pid), !cleanup);491 t.false(isRunning(subprocess.pid));492 t.is(isRunning(pid), !isKilled);493}494504test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);505test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);495// Without `options.cleanup`:496// - on Windows subprocesses are killed if `options.detached: false`, but not497// if `options.detached: true`498// - on Linux subprocesses are never killed regardless of `options.detached`499// With `options.cleanup`, subprocesses are always killed500// - `options.cleanup` with SIGKILL is a noop, since it cannot be handled501const exitIfWindows = process.platform === 'win32';502test('cleanup true - SIGTERM', spawnAndKill, 'SIGTERM', 'true', true);503test('cleanup false - SIGTERM', spawnAndKill, 'SIGTERM', 'false', exitIfWindows);504test('cleanup true - SIGKILL', spawnAndKill, 'SIGKILL', 'true', exitIfWindows);505test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', 'false', exitIfWindows);506507if (process.platform !== 'win32') {508 // On Windows the subprocesses are actually always killed509 test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);510 test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);511}512507test('execa.shell() supports the `shell` option', async t => {508 const {stdout} = await execa.shell('node fixtures/noop foo', {509 shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash'510 });
172 unmodified lines