Skip to content

Commit

Permalink
fix #121
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Dec 2, 2017
1 parent cf037ec commit 5283167
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 9 deletions.
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ environment:
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "7"
- nodejs_version: "8"
- nodejs_version: "9"

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
19 changes: 18 additions & 1 deletion lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,24 @@ function _safely_install_listener() {
}
}

// windows does not support signals
// it'd never had won if it wasn't a major PITA
// with node v8.x and win 10 this is no longer an issue
if (process.platform == 'win32') {
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});

rl.on('SIGINT', function () {
process.emit('SIGINT');
});
}

process.on('SIGINT', function () {
process.exit(0);
});

process.addListener(EVENT, function _tmp$safe_listener(data) {
if (existingListeners.length) {
for (var i = 0, length = existingListeners.length; i < length; i++) {
Expand All @@ -559,7 +577,6 @@ function _safely_install_listener() {

_safely_install_listener();


/**
* Configuration options.
*
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions test/child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports.genericChildProcess = function spawnGenericChildProcess(configFil
_do_spawn(command_args, cb);
};

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

_do_spawn(command_args, cb);
if (arguments.length > 2) {
for (var i=2; i < arguments.length; i++) {
command_args.push(arguments[i]);
}
}

_do_spawn(command_args, cb, detach);
}

function _do_spawn(command_args, cb) {
function _do_spawn(command_args, cb, detach) {
var
node_path = process.argv[0],
stdoutBufs = [],
Expand All @@ -43,9 +49,9 @@ function _do_spawn(command_args, cb) {

// spawn doesn’t have the quoting problems that exec does,
// especially when going for Windows portability.
child = spawn(node_path, command_args);
child = spawn(node_path, command_args, detach ? { detached: true } : undefined);
child.stdin.end();
// TODO:we no longer support node 0.6
// TODO:we no longer support node <0.10.0
// Cannot use 'close' event because not on node-0.6.
function _close() {
var
Expand Down
27 changes: 27 additions & 0 deletions test/issue121-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-octal */
// vim: expandtab:ts=2:sw=2

const
assert = require('assert'),
assertions = require('./assertions'),
childProcess = require('./child-process').childProcess,
signals = ['SIGINT', 'SIGTERM'];

describe('tmp', function () {
describe('issue121 - clean up on terminating signals', function () {
for (var i=0; i < signals.length; i++) {
it(signals[i], issue121Tests(signals[i]));
}
});
});

function issue121Tests(signal) {
return function (done) {
childProcess('issue121.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) return done(new Error(stderr));
else assertions.assertDoesNotExist(stdout);
done();
}, true);
};
}
20 changes: 20 additions & 0 deletions test/outband/issue121.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable no-octal */
// vim: expandtab:ts=2:sw=2

var
fs = require('fs'),
tmp = require('../../lib/tmp'),
// we reuse the fixtures from issue62 here
fixture = require('./issue62');

tmp.setGracefulCleanup();

// https://github.com/raszi/node-tmp/issues/121
module.exports = function (signal) {
fixture.apply(this, [tmp.dirSync({ unsafeCleanup: true }), tmp]);

// make sure that the process keeps running
setTimeout(function () {}, 1000000);

this.kill(signal);
};
3 changes: 3 additions & 0 deletions test/outband/issue121.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tc": "issue121"
}
8 changes: 7 additions & 1 deletion test/spawn-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ var
var config = readJsonConfig(process.argv[2]);
spawn.graceful = !!config.graceful;

var args = [];

for (var i=3; i<process.argv.length; i++) {
args[i-3] = process.argv[i];
}

// import the test case function and execute it
var fn = require(path.join(__dirname, 'outband', config.tc));
fn.apply(spawn);
fn.apply(spawn, args);

2 changes: 1 addition & 1 deletion test/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
process.exit(code || 0);
},
kill: function (signal) {
process.kill(signal || 'SIGINT');
process.kill(process.pid, signal || 'SIGINT');
}
};

0 comments on commit 5283167

Please sign in to comment.