From a7b39a6c2284dff42b9b3ed20456dc1d69e13a58 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Fri, 1 Dec 2017 00:39:13 +0100 Subject: [PATCH] fix #121 --- lib/tmp.js | 19 ++++++++- test/child-process.js | 83 +++++++++++++++++++------------------- test/issue121-test.js | 27 +++++++++++++ test/outband/issue121.js | 20 +++++++++ test/outband/issue121.json | 3 ++ test/spawn-custom.js | 8 +++- test/spawn.js | 2 +- 7 files changed, 117 insertions(+), 45 deletions(-) create mode 100644 test/issue121-test.js create mode 100644 test/outband/issue121.js create mode 100644 test/outband/issue121.json diff --git a/lib/tmp.js b/lib/tmp.js index d47671a..ea7a95d 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -592,6 +592,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) { @@ -605,7 +623,6 @@ function _safely_install_listener() { _safely_install_listener(); - /** * Configuration options. * diff --git a/test/child-process.js b/test/child-process.js index 3dd0dde..c7f0e49 100644 --- a/test/child-process.js +++ b/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() { @@ -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; - }, [])); } diff --git a/test/issue121-test.js b/test/issue121-test.js new file mode 100644 index 0000000..e723720 --- /dev/null +++ b/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); + }; +} diff --git a/test/outband/issue121.js b/test/outband/issue121.js new file mode 100644 index 0000000..0dce377 --- /dev/null +++ b/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); +}; diff --git a/test/outband/issue121.json b/test/outband/issue121.json new file mode 100644 index 0000000..86429e8 --- /dev/null +++ b/test/outband/issue121.json @@ -0,0 +1,3 @@ +{ + "tc": "issue121" +} diff --git a/test/spawn-custom.js b/test/spawn-custom.js index bb1cc27..d450635 100644 --- a/test/spawn-custom.js +++ b/test/spawn-custom.js @@ -8,7 +8,13 @@ var var config = readJsonConfig(process.argv[2]); spawn.graceful = !!config.graceful; +var args = []; + +for (var i=3; i