Skip to content

Commit

Permalink
fix test for #121
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Mar 16, 2019
1 parent 0c5bc5c commit d9ed736
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 60 deletions.
40 changes: 18 additions & 22 deletions lib/tmp.js
Expand Up @@ -575,46 +575,42 @@ function _is_legacy_listener(listener) {

/**
* Safely install process exit listeners.
*
*
* @private
*/
function _safely_install_listener() {
var listeners = process.listeners(EVENT);
const exit_listeners = process.listeners(EVENT);

// collect any existing listeners
var existingListeners = [];
for (var i = 0, length = listeners.length; i < length; i++) {
var lstnr = listeners[i];
const existingListeners = [];
for (let i = 0, length = exit_listeners.length; i < length; i++) {
const lstnr = exit_listeners[i];
/* istanbul ignore else */
if (lstnr.name == '_tmp$safe_listener' || _is_legacy_listener(lstnr)) {
if (lstnr.name === '_tmp$safe_listener' || _is_legacy_listener(lstnr)) {
// we must forget about the uncaughtException listener
if (lstnr.name != '_uncaughtExceptionThrown') existingListeners.push(lstnr);
if (lstnr.name !== '_uncaughtExceptionThrown') existingListeners.push(lstnr);
process.removeListener(EVENT, lstnr);
}
}

// 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');
});
const sigint_listeners = process.listeners('SIGINT');
for (let i = 0, length = sigint_listeners.length; i < length; i++) {
const lstnr = sigint_listeners[i];
/* istanbul ignore else */
if (lstnr.name === '_tmp$signal_listener') {
process.removeListener(EVENT, lstnr);
}
}

process.on('SIGINT', function () {
process.on('SIGINT', function _tmp$signal_listener() {
throw new Error('SIGINT RECEIVED');
_garbageCollector();
process.exit(0);
});

process.addListener(EVENT, function _tmp$safe_listener(data) {
/* istanbul ignore else */
if (existingListeners.length) {
for (var i = 0, length = existingListeners.length; i < length; i++) {
for (let i = 0, length = existingListeners.length; i < length; i++) {
existingListeners[i](data);
}
}
Expand Down
31 changes: 17 additions & 14 deletions test/child-process.js
Expand Up @@ -12,24 +12,25 @@ module.exports.genericChildProcess = _spawnProcess('spawn-generic.js');
module.exports.childProcess = _spawnProcess('spawn-custom.js');

function _spawnProcess(spawnFile) {
return function (testCase, configFile, cb) {
var
return function (testCase, configFile, cb, signal) {
const
configFilePath = path.join(__dirname, 'outband', configFile),
commandArgs = [path.join(__dirname, spawnFile), configFilePath];

exists(configFilePath, function (configExists) {
if (configExists) return _doSpawn(commandArgs, cb);
if (configExists) return _doSpawn(commandArgs, cb, signal);

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

function _doSpawn(commandArgs, cb) {
var
function _doSpawn(commandArgs, cb, signal) {
const
node_path = process.argv[0],
stdoutBufs = [],
stderrBufs = [],
stderrBufs = [];
let
child,
done = false,
stderrDone = false,
Expand All @@ -48,14 +49,11 @@ function _doSpawn(commandArgs, cb) {
child = spawn(node_path, commandArgs);
child.stdin.end();

// TODO we no longer support node 0.6
// Cannot use 'close' event because not on node-0.6.
function _close() {
var
stderr = _bufferConcat(stderrBufs).toString(),
stdout = _bufferConcat(stdoutBufs).toString();

if (stderrDone && stdoutDone && !done) {
const
stderr = _bufferConcat(stderrBufs).toString(),
stdout = _bufferConcat(stdoutBufs).toString();
done = true;
cb(null, stderr, stdout);
}
Expand All @@ -81,6 +79,12 @@ function _doSpawn(commandArgs, cb) {
stderrDone = true;
_close();
});

if (signal) {
setTimeout(function () {
child.kill(signal);
}, 2000);
}
}

function _bufferConcat(buffers) {
Expand All @@ -89,10 +93,9 @@ function _bufferConcat(buffers) {
}

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

21 changes: 14 additions & 7 deletions test/issue121-test.js
Expand Up @@ -2,26 +2,33 @@
// 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++) {
issue121Tests(signals[i]);
for (let i = 0; i < signals.length; i++) {
it('for signal ' + signals[i], function (done) {
this.timeout(5000);
issue121Tests(signals[i])(done);
});
}
});
});

function issue121Tests(signal) {
return function (done) {
childProcess('issue121.json', function (err, stderr, stdout) {
childProcess(this, '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);

try {
assertions.assertDoesNotExist(stdout);
done();
} catch (err) {
done(err);
}
}, signal);
};
}
23 changes: 11 additions & 12 deletions test/outband/issue121.js
@@ -1,20 +1,19 @@
/* 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();
const
tmp = require('../../lib/tmp');

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

tmp.setGracefulCleanup();

const result = tmp.dirSync({ unsafeCleanup: true });

// make sure that the process keeps running
setTimeout(function () {}, 1000000);
this.out(result.name, function () { });

this.kill(signal);
setTimeout(function () {
throw new Error('ran into timeout');
}, 10000);
};
1 change: 1 addition & 0 deletions test/outband/issue121.json
@@ -1,3 +1,4 @@
{
"graceful": true,
"tc": "issue121"
}
1 change: 0 additions & 1 deletion test/spawn-custom.js
Expand Up @@ -11,4 +11,3 @@ spawn.graceful = !!config.graceful;
// import the test case function and execute it
var fn = require(path.join(__dirname, 'outband', config.tc));
fn.apply(spawn);

2 changes: 1 addition & 1 deletion test/spawn-generic.js
Expand Up @@ -23,7 +23,7 @@ if (config.async)
fnUnderTest(config.options, function (err, name, fdOrCallback, cb) {
if (err) spawn.err(err);
else {
var result = null;
var result = null;
if (config.file) result = { name: name, fd: fdOrCallback, removeCallback: cb };
else result = { name: name, removeCallback: fdOrCallback };
fn.apply(spawn, [result, tmp]);
Expand Down
17 changes: 17 additions & 0 deletions test/spawn-signalled.js
@@ -0,0 +1,17 @@
// vim: expandtab:ts=2:sw=2

var
path = require('path'),
readJsonConfig = require('./util').readJsonConfig,
spawn = require('./spawn');

var config = readJsonConfig(process.argv[2]);
var signal = process.argv[3];
spawn.graceful = !!config.graceful;

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

spawn.kill(signal);

3 changes: 0 additions & 3 deletions test/spawn.js
Expand Up @@ -28,9 +28,6 @@ module.exports = {
},
exit: function (code) {
process.exit(code || 0);
},
kill: function (signal) {
process.kill(signal || 'SIGINT');
}
};

0 comments on commit d9ed736

Please sign in to comment.