Skip to content

Commit cc24cf5

Browse files
committed
fix #121
1 parent fcbf27b commit cc24cf5

File tree

7 files changed

+117
-45
lines changed

7 files changed

+117
-45
lines changed

lib/tmp.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,24 @@ function _safely_install_listener() {
593593
}
594594
}
595595

596+
// windows does not support signals
597+
// it'd never had won if it wasn't a major PITA
598+
// with node v8.x and win 10 this is no longer an issue
599+
if (process.platform == 'win32') {
600+
var rl = require('readline').createInterface({
601+
input: process.stdin,
602+
output: process.stdout
603+
});
604+
605+
rl.on('SIGINT', function () {
606+
process.emit('SIGINT');
607+
});
608+
}
609+
610+
process.on('SIGINT', function () {
611+
process.exit(0);
612+
});
613+
596614
process.addListener(EVENT, function _tmp$safe_listener(data) {
597615
/* istanbul ignore else */
598616
if (existingListeners.length) {
@@ -606,7 +624,6 @@ function _safely_install_listener() {
606624

607625
_safely_install_listener();
608626

609-
610627
/**
611628
* Configuration options.
612629
*

test/child-process.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,81 @@
11
// vim: expandtab:ts=2:sw=2
22

3-
var
3+
const
44
fs = require('fs'),
55
path = require('path'),
6-
exists = fs.exists || path.exists,
6+
existsSync = fs.existsSync || path.existsSync,
77
spawn = require('child_process').spawn;
88

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

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

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

18-
var
19-
configFilePath = path.join(__dirname, 'outband', configFile),
20-
commandArgs = [path.join(__dirname, spawnFile), configFilePath];
19+
_do_spawn(command_args, cb);
20+
};
2121

22-
exists(configFilePath, function (configExists) {
23-
if (configExists) return _doSpawn(commandArgs, cb);
22+
module.exports.childProcess = function spawnChildProcess(configFile, cb, detach) {
23+
var
24+
configFilePath = path.join(__dirname, 'outband', configFile),
25+
command_args = [path.join(__dirname, 'spawn-custom.js'), configFilePath];
26+
27+
// make sure that the config file exists
28+
if (!existsSync(configFilePath))
29+
return cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));
30+
31+
if (arguments.length > 2) {
32+
for (var i=2; i < arguments.length; i++) {
33+
command_args.push(arguments[i]);
34+
}
35+
}
2436

25-
cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));
26-
});
27-
};
37+
_do_spawn(command_args, cb, detach);
2838
}
2939

30-
function _doSpawn(commandArgs, cb) {
31-
var
40+
function _do_spawn(command_args, cb, detach) {
41+
const
3242
node_path = process.argv[0],
3343
stdoutBufs = [],
34-
stderrBufs = [],
44+
stderrBufs = [];
45+
46+
var
3547
child,
3648
done = false,
3749
stderrDone = false,
3850
stdoutDone = false;
3951

40-
if (process.env.running_under_istanbul) {
41-
commandArgs = [
42-
ISTANBUL_PATH, 'cover', '--report' , 'none', '--print', 'none',
43-
'--dir', path.join('coverage', 'json'), '--include-pid',
44-
commandArgs[0], '--', commandArgs[1]
45-
];
46-
}
47-
4852
// spawn doesn’t have the quoting problems that exec does,
4953
// especially when going for Windows portability.
50-
child = spawn(node_path, commandArgs);
54+
child = spawn(node_path, command_args, detach ? { detached: true } : undefined);
5155
child.stdin.end();
52-
53-
// TODO we no longer support node 0.6
56+
// TODO:we no longer support node <0.10.0
5457
// Cannot use 'close' event because not on node-0.6.
5558
function _close() {
56-
var
59+
const
5760
stderr = _bufferConcat(stderrBufs).toString(),
5861
stdout = _bufferConcat(stdoutBufs).toString();
59-
6062
if (stderrDone && stdoutDone && !done) {
6163
done = true;
6264
cb(null, stderr, stdout);
6365
}
6466
}
65-
6667
child.on('error', function _spawnError(err) {
6768
if (!done) {
6869
done = true;
6970
cb(err);
7071
}
7172
});
72-
7373
child.stdout.on('data', function _stdoutData(data) {
7474
stdoutBufs.push(data);
7575
}).on('close', function _stdoutEnd() {
7676
stdoutDone = true;
7777
_close();
7878
});
79-
8079
child.stderr.on('data', function _stderrData(data) {
8180
stderrBufs.push(data);
8281
}).on('close', function _stderrEnd() {
@@ -88,13 +87,13 @@ function _doSpawn(commandArgs, cb) {
8887
function _bufferConcat(buffers) {
8988
if (Buffer.concat) {
9089
return Buffer.concat.apply(this, arguments);
90+
} else {
91+
return new Buffer(buffers.reduce(function (acc, buf) {
92+
for (var i = 0; i < buf.length; i++) {
93+
acc.push(buf[i]);
94+
}
95+
return acc;
96+
}, []));
9197
}
92-
93-
return new Buffer(buffers.reduce(function (acc, buf) {
94-
for (var i = 0; i < buf.length; i++) {
95-
acc.push(buf[i]);
96-
}
97-
return acc;
98-
}, []));
9998
}
10099

test/issue121-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-octal */
2+
// vim: expandtab:ts=2:sw=2
3+
4+
const
5+
assert = require('assert'),
6+
assertions = require('./assertions'),
7+
childProcess = require('./child-process').childProcess,
8+
signals = ['SIGINT', 'SIGTERM'];
9+
10+
describe('tmp', function () {
11+
describe('issue121 - clean up on terminating signals', function () {
12+
for (var i=0; i < signals.length; i++) {
13+
it(signals[i], issue121Tests(signals[i]));
14+
}
15+
});
16+
});
17+
18+
function issue121Tests(signal) {
19+
return function (done) {
20+
childProcess('issue121.json', function (err, stderr, stdout) {
21+
if (err) return done(err);
22+
else if (stderr) return done(new Error(stderr));
23+
else assertions.assertDoesNotExist(stdout);
24+
done();
25+
}, true);
26+
};
27+
}

test/outband/issue121.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable no-octal */
2+
// vim: expandtab:ts=2:sw=2
3+
4+
var
5+
fs = require('fs'),
6+
tmp = require('../../lib/tmp'),
7+
// we reuse the fixtures from issue62 here
8+
fixture = require('./issue62');
9+
10+
tmp.setGracefulCleanup();
11+
12+
// https://github.com/raszi/node-tmp/issues/121
13+
module.exports = function (signal) {
14+
fixture.apply(this, [tmp.dirSync({ unsafeCleanup: true }), tmp]);
15+
16+
// make sure that the process keeps running
17+
setTimeout(function () {}, 1000000);
18+
19+
this.kill(signal);
20+
};

test/outband/issue121.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"tc": "issue121"
3+
}

test/spawn-custom.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ var
88
var config = readJsonConfig(process.argv[2]);
99
spawn.graceful = !!config.graceful;
1010

11+
var args = [];
12+
13+
for (var i=3; i<process.argv.length; i++) {
14+
args[i-3] = process.argv[i];
15+
}
16+
1117
// import the test case function and execute it
1218
var fn = require(path.join(__dirname, 'outband', config.tc));
13-
fn.apply(spawn);
19+
fn.apply(spawn, args);
1420

test/spawn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
process.exit(code || 0);
3131
},
3232
kill: function (signal) {
33-
process.kill(signal || 'SIGINT');
33+
process.kill(process.pid, signal || 'SIGINT');
3434
}
3535
};
3636

0 commit comments

Comments
 (0)