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 87dc50e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 49 deletions.
16 changes: 1 addition & 15 deletions lib/tmp.js
Expand Up @@ -575,7 +575,7 @@ function _is_legacy_listener(listener) {

/**
* Safely install process exit listeners.
*
*
* @private
*/
function _safely_install_listener() {
Expand All @@ -593,20 +593,6 @@ 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);
});
Expand Down
24 changes: 11 additions & 13 deletions test/child-process.js
Expand Up @@ -10,12 +10,13 @@ const ISTANBUL_PATH = path.join(__dirname, '..', 'node_modules', 'istanbul', 'li

module.exports.genericChildProcess = _spawnProcess('spawn-generic.js');
module.exports.childProcess = _spawnProcess('spawn-custom.js');
module.exports.signalledProcess = _spawnProcess('spawn-signalled.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];
commandArgs = [path.join(__dirname, spawnFile), configFilePath, signal];

exists(configFilePath, function (configExists) {
if (configExists) return _doSpawn(commandArgs, cb);
Expand All @@ -26,10 +27,11 @@ function _spawnProcess(spawnFile) {
}

function _doSpawn(commandArgs, cb) {
var
const
node_path = process.argv[0],
stdoutBufs = [],
stderrBufs = [],
stderrBufs = [];
let
child,
done = false,
stderrDone = false,
Expand All @@ -48,14 +50,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 Down Expand Up @@ -89,10 +88,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;
}, []));
}

20 changes: 13 additions & 7 deletions test/issue121-test.js
Expand Up @@ -2,26 +2,32 @@
// vim: expandtab:ts=2:sw=2

const
assert = require('assert'),
assertions = require('./assertions'),
childProcess = require('./child-process').childProcess,
signalledProcess = require('./child-process').signalledProcess,
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 (var i = 0; i < signals.length; i++) {
it('for signal ' + signals[i], function (done) {
issue121Tests(signals[i])(done);
});
}
});
});

function issue121Tests(signal) {
return function (done) {
childProcess('issue121.json', function (err, stderr, stdout) {
signalledProcess(this, 'issue121.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) return done(new Error(stderr));
else assertions.assertDoesNotExist(stdout);

try {
assertions.assertDoesNotExist(stdout);
} catch (err) {
done(err);
}
done();
}, true);
}, 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"
}
2 changes: 2 additions & 0 deletions test/spawn-custom.js
Expand Up @@ -12,3 +12,5 @@ spawn.graceful = !!config.graceful;
var fn = require(path.join(__dirname, 'outband', config.tc));
fn.apply(spawn);

spawn.kill('SIGINT');

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);

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 87dc50e

Please sign in to comment.