Skip to content

Commit

Permalink
fix #121
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Apr 30, 2018
1 parent 6c3db68 commit c960a36
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 45 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 @@ -577,6 +577,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) {
/* istanbul ignore else */
if (existingListeners.length) {
Expand All @@ -590,7 +608,6 @@ function _safely_install_listener() {

_safely_install_listener();


/**
* Configuration options.
*
Expand Down
83 changes: 41 additions & 42 deletions test/child-process.js
@@ -1,82 +1,81 @@
// vim: expandtab:ts=2:sw=2

var
const
fs = require('fs'),
path = require('path'),
exists = fs.exists || path.exists,
existsSync = fs.existsSync || path.existsSync,
spawn = require('child_process').spawn;

const ISTANBUL_PATH = path.join(__dirname, '..', 'node_modules', 'istanbul', 'lib', 'cli.js');

module.exports.genericChildProcess = _spawnProcess('spawn-generic.js');
module.exports.childProcess = _spawnProcess('spawn-custom.js');
module.exports.genericChildProcess = function spawnGenericChildProcess(configFile, cb) {
const
configFilePath = path.join(__dirname, 'outband', configFile),
command_args = [path.join(__dirname, 'spawn-generic.js'), configFilePath];

function _spawnProcess(spawnFile) {
return function (testCase, configFile, cb) {
testCase.timeout(5000);
// make sure that the config file exists
if (!existsSync(configFilePath))
return cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));

var
configFilePath = path.join(__dirname, 'outband', configFile),
commandArgs = [path.join(__dirname, spawnFile), configFilePath];
_do_spawn(command_args, cb);
};

exists(configFilePath, function (configExists) {
if (configExists) return _doSpawn(commandArgs, 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];

// make sure that the config file exists
if (!existsSync(configFilePath))
return cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));

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

cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));
});
};
_do_spawn(command_args, cb, detach);
}

function _doSpawn(commandArgs, cb) {
var
function _do_spawn(command_args, cb, detach) {
const
node_path = process.argv[0],
stdoutBufs = [],
stderrBufs = [],
stderrBufs = [];

var
child,
done = false,
stderrDone = false,
stdoutDone = false;

if (process.env.running_under_istanbul) {
commandArgs = [
ISTANBUL_PATH, 'cover', '--report' , 'none', '--print', 'none',
'--dir', path.join('coverage', 'json'), '--include-pid',
commandArgs[0], '--', commandArgs[1]
];
}

// spawn doesn’t have the quoting problems that exec does,
// especially when going for Windows portability.
child = spawn(node_path, commandArgs);
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
const
stderr = _bufferConcat(stderrBufs).toString(),
stdout = _bufferConcat(stdoutBufs).toString();

if (stderrDone && stdoutDone && !done) {
done = true;
cb(null, stderr, stdout);
}
}

child.on('error', function _spawnError(err) {
if (!done) {
done = true;
cb(err);
}
});

child.stdout.on('data', function _stdoutData(data) {
stdoutBufs.push(data);
}).on('close', function _stdoutEnd() {
stdoutDone = true;
_close();
});

child.stderr.on('data', function _stderrData(data) {
stderrBufs.push(data);
}).on('close', function _stderrEnd() {
Expand All @@ -88,13 +87,13 @@ function _doSpawn(commandArgs, cb) {
function _bufferConcat(buffers) {
if (Buffer.concat) {
return Buffer.concat.apply(this, arguments);
} else {
return new Buffer(buffers.reduce(function (acc, buf) {
for (var i = 0; i < buf.length; i++) {
acc.push(buf[i]);
}
return acc;
}, []));
}

return new Buffer(buffers.reduce(function (acc, buf) {
for (var i = 0; i < buf.length; i++) {
acc.push(buf[i]);
}
return acc;
}, []));
}

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 @@ -30,7 +30,7 @@ module.exports = {
process.exit(code || 0);
},
kill: function (signal) {
process.kill(signal || 'SIGINT');
process.kill(process.pid, signal || 'SIGINT');
}
};

0 comments on commit c960a36

Please sign in to comment.