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 23f22ec
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 8 deletions.
2 changes: 2 additions & 0 deletions appveyor.yml
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
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.

14 changes: 10 additions & 4 deletions test/child-process.js
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,7 +49,7 @@ 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
// Cannot use 'close' event because not on node-0.6.
Expand Down
27 changes: 27 additions & 0 deletions test/issue121-test.js
@@ -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
@@ -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
@@ -0,0 +1,3 @@
{
"tc": "issue121"
}
8 changes: 7 additions & 1 deletion test/spawn-custom.js
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
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 23f22ec

Please sign in to comment.