Skip to content

Commit f966269

Browse files
committed
fix #121
1 parent cf037ec commit f966269

File tree

8 files changed

+87
-8
lines changed

8 files changed

+87
-8
lines changed

lib/tmp.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,24 @@ function _safely_install_listener() {
547547
}
548548
}
549549

550+
// windows does not support signals
551+
// it'd never had won if it wasn't a major PITA
552+
// with node v8.x and win 10 this is no longer an issue
553+
if (process.platform == 'win32') {
554+
var rl = require("readline").createInterface({
555+
input: process.stdin,
556+
output: process.stdout
557+
});
558+
559+
rl.on("SIGINT", function () {
560+
process.emit("SIGINT");
561+
});
562+
}
563+
564+
process.on('SIGINT', function () {
565+
process.exit(0);
566+
});
567+
550568
process.addListener(EVENT, function _tmp$safe_listener(data) {
551569
if (existingListeners.length) {
552570
for (var i = 0, length = existingListeners.length; i < length; i++) {
@@ -559,7 +577,6 @@ function _safely_install_listener() {
559577

560578
_safely_install_listener();
561579

562-
563580
/**
564581
* Configuration options.
565582
*

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/child-process.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports.genericChildProcess = function spawnGenericChildProcess(configFil
1919
_do_spawn(command_args, cb);
2020
};
2121

22-
module.exports.childProcess = function spawnChildProcess(configFile, cb) {
22+
module.exports.childProcess = function spawnChildProcess(configFile, cb, detach) {
2323
var
2424
configFilePath = path.join(__dirname, 'outband', configFile),
2525
command_args = [path.join(__dirname, 'spawn-custom.js'), configFilePath];
@@ -28,10 +28,16 @@ module.exports.childProcess = function spawnChildProcess(configFile, cb) {
2828
if (!existsSync(configFilePath))
2929
return cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));
3030

31-
_do_spawn(command_args, cb);
31+
if (arguments.length > 2) {
32+
for (var i=2; i<arguments.length; i++) {
33+
command_args.push(arguments[i]);
34+
}
35+
}
36+
37+
_do_spawn(command_args, cb, detach);
3238
}
3339

34-
function _do_spawn(command_args, cb) {
40+
function _do_spawn(command_args, cb, detach) {
3541
var
3642
node_path = process.argv[0],
3743
stdoutBufs = [],
@@ -43,7 +49,7 @@ function _do_spawn(command_args, cb) {
4349

4450
// spawn doesn’t have the quoting problems that exec does,
4551
// especially when going for Windows portability.
46-
child = spawn(node_path, command_args);
52+
child = spawn(node_path, command_args, detach ? { detached:true } : undefined);
4753
child.stdin.end();
4854
// TODO:we no longer support node 0.6
4955
// Cannot use 'close' event because not on node-0.6.

test/issue121-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-octal */
2+
// vim: expandtab:ts=2:sw=2
3+
4+
const
5+
assert = require('assert'),
6+
assertions = require('./assertions'),
7+
childProcess = require('./child-process').childProcess,
8+
signals = ['SIGINT', 'SIGTERM'];
9+
10+
describe('tmp', function () {
11+
describe('issue121 - clean up on terminating signals', function () {
12+
for (var i=0; i<signals.length; i++) {
13+
it(signals[i], issue121Tests(signals[i]));
14+
}
15+
});
16+
});
17+
18+
function issue121Tests(signal) {
19+
return function (done) {
20+
childProcess('issue121.json', function (err, stderr, stdout) {
21+
if (err) return done(err);
22+
else if (stderr) return done(new Error(stderr));
23+
else assertions.assertDoesNotExist(stdout);
24+
done();
25+
}, true);
26+
};
27+
}

test/outband/issue121.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable no-octal */
2+
// vim: expandtab:ts=2:sw=2
3+
4+
var
5+
fs = require('fs'),
6+
tmp = require('../../lib/tmp'),
7+
// we reuse the fixtures from issue62 here
8+
fixture = require('./issue62');
9+
10+
tmp.setGracefulCleanup();
11+
12+
// https://github.com/raszi/node-tmp/issues/121
13+
module.exports = function (signal) {
14+
fixture.apply(this, [tmp.dirSync({ unsafeCleanup: true }), tmp]);
15+
16+
// make sure that the process keeps running
17+
setTimeout(function () {}, 100);
18+
19+
this.kill(signal);
20+
};

test/outband/issue121.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tc": "issue121"
3+
}

test/spawn-custom.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ var
88
var config = readJsonConfig(process.argv[2]);
99
spawn.graceful = !!config.graceful;
1010

11+
var args = [];
12+
13+
for (var i=3; i<process.argv.length; i++) {
14+
args[i-3] = process.argv[i];
15+
}
16+
1117
// import the test case function and execute it
1218
var fn = require(path.join(__dirname, 'outband', config.tc));
13-
fn.apply(spawn);
19+
fn.apply(spawn, args);
1420

test/spawn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
process.exit(code || 0);
3535
},
3636
kill: function (signal) {
37-
process.kill(signal || 'SIGINT');
37+
process.kill(process.pid, signal || 'SIGINT');
3838
}
3939
};
4040

0 commit comments

Comments
 (0)