From db4e7c581402676a56ed243476f3170b594f8c1c Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Wed, 12 Sep 2018 18:11:08 +0200 Subject: [PATCH] fix test for #121 --- lib/tmp.js | 17 ++--------------- test/child-process.js | 24 +++++++++++------------- test/issue121-test.js | 22 ++++++++++++++-------- test/outband/issue121.js | 23 +++++++++++------------ test/outband/issue121.json | 1 + test/spawn-custom.js | 2 ++ test/spawn-generic.js | 2 +- test/spawn-signalled.js | 17 +++++++++++++++++ test/spawn.js | 2 +- 9 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 test/spawn-signalled.js diff --git a/lib/tmp.js b/lib/tmp.js index 8fe48a0..6a47c2e 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -575,7 +575,7 @@ function _is_legacy_listener(listener) { /** * Safely install process exit listeners. - * + * * @private */ function _safely_install_listener() { @@ -593,21 +593,8 @@ 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 () { + throw new Error('SIGINT RECEIVED'); process.exit(0); }); diff --git a/test/child-process.js b/test/child-process.js index ce189e2..b3f9aa9 100644 --- a/test/child-process.js +++ b/test/child-process.js @@ -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); @@ -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, @@ -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); } @@ -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; }, [])); } - diff --git a/test/issue121-test.js b/test/issue121-test.js index 69a24f3..9d004fc 100644 --- a/test/issue121-test.js +++ b/test/issue121-test.js @@ -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); - done(); - }, true); + + try { + assertions.assertDoesNotExist(stdout); + done(); + } catch (err) { + done(err); + } + }, signal); }; } diff --git a/test/outband/issue121.js b/test/outband/issue121.js index 0dce377..deba395 100644 --- a/test/outband/issue121.js +++ b/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); }; diff --git a/test/outband/issue121.json b/test/outband/issue121.json index 86429e8..2210135 100644 --- a/test/outband/issue121.json +++ b/test/outband/issue121.json @@ -1,3 +1,4 @@ { + "graceful": true, "tc": "issue121" } diff --git a/test/spawn-custom.js b/test/spawn-custom.js index bb1cc27..6d72f96 100644 --- a/test/spawn-custom.js +++ b/test/spawn-custom.js @@ -12,3 +12,5 @@ spawn.graceful = !!config.graceful; var fn = require(path.join(__dirname, 'outband', config.tc)); fn.apply(spawn); +spawn.kill('SIGINT'); + diff --git a/test/spawn-generic.js b/test/spawn-generic.js index 6b4cb5b..d557db0 100644 --- a/test/spawn-generic.js +++ b/test/spawn-generic.js @@ -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]); diff --git a/test/spawn-signalled.js b/test/spawn-signalled.js new file mode 100644 index 0000000..0ee23c4 --- /dev/null +++ b/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); + diff --git a/test/spawn.js b/test/spawn.js index 5dfd87e..81851ff 100644 --- a/test/spawn.js +++ b/test/spawn.js @@ -30,7 +30,7 @@ module.exports = { process.exit(code || 0); }, kill: function (signal) { - process.kill(signal || 'SIGINT'); + process.kill(process.pid, signal || 'SIGINT'); } };