Skip to content

Commit

Permalink
Fix killing PID directly (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva authored and sindresorhus committed Mar 22, 2018
1 parent 6c9d206 commit 1ba0732
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const cli = meow(`
Run without arguments to use the interactive interface.
The process name is case insensitive.
`, {
inferType: true,
flags: {
force: {
type: 'boolean',
Expand Down
8 changes: 8 additions & 0 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
const http = require('http');

const server = http.createServer((request, response) => {
response.end();
});

server.listen(process.argv.slice(2)[0]);
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
},
"devDependencies": {
"ava": "*",
"delay": "^2.0.0",
"execa": "^0.8.0",
"get-port": "^3.2.0",
"noop-process": "^3.1.0",
"process-exists": "^3.1.0",
"xo": "*"
}
}
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
import childProcess from 'child_process';
import test from 'ava';
import execa from 'execa';
import delay from 'delay';
import noopProcess from 'noop-process';
import processExists from 'process-exists';
import getPort from 'get-port';

const noopProcessKilled = async (t, pid) => {
// Ensure the noop process has time to exit
await delay(100);
t.false(await processExists(pid));
};

test(async t => {
const {stdout} = await execa('./cli.js', ['--version']);
t.true(stdout.length > 0);
});

test('pid', async t => {
const pid = await noopProcess();
await execa('./cli.js', ['--force', pid]);
await noopProcessKilled(t, pid);
});

test('kill from port', async t => {
const port = await getPort();
const pid = childProcess.spawn('node', ['fixture.js', port]).pid;
await execa('./cli.js', ['--force', pid]);
await noopProcessKilled(t, pid);
t.is(await getPort(port), port);
});

test('error when process is not found', async t => {
const err = await t.throws(execa('./cli.js', ['--force', 'notFoundProcess']));
t.regex(err.message, /Killing process notFoundProcess failed: Process doesn't exist/);
});

0 comments on commit 1ba0732

Please sign in to comment.