Skip to content

Commit 87dc50e

Browse files
committed
fix test for #121
1 parent 0c5bc5c commit 87dc50e

File tree

9 files changed

+58
-49
lines changed

9 files changed

+58
-49
lines changed

lib/tmp.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ function _is_legacy_listener(listener) {
575575

576576
/**
577577
* Safely install process exit listeners.
578-
*
578+
*
579579
* @private
580580
*/
581581
function _safely_install_listener() {
@@ -593,20 +593,6 @@ 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-
610596
process.on('SIGINT', function () {
611597
process.exit(0);
612598
});

test/child-process.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ const ISTANBUL_PATH = path.join(__dirname, '..', 'node_modules', 'istanbul', 'li
1010

1111
module.exports.genericChildProcess = _spawnProcess('spawn-generic.js');
1212
module.exports.childProcess = _spawnProcess('spawn-custom.js');
13+
module.exports.signalledProcess = _spawnProcess('spawn-signalled.js');
1314

1415
function _spawnProcess(spawnFile) {
15-
return function (testCase, configFile, cb) {
16-
var
16+
return function (testCase, configFile, cb, signal) {
17+
const
1718
configFilePath = path.join(__dirname, 'outband', configFile),
18-
commandArgs = [path.join(__dirname, spawnFile), configFilePath];
19+
commandArgs = [path.join(__dirname, spawnFile), configFilePath, signal];
1920

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

2829
function _doSpawn(commandArgs, cb) {
29-
var
30+
const
3031
node_path = process.argv[0],
3132
stdoutBufs = [],
32-
stderrBufs = [],
33+
stderrBufs = [];
34+
let
3335
child,
3436
done = false,
3537
stderrDone = false,
@@ -48,14 +50,11 @@ function _doSpawn(commandArgs, cb) {
4850
child = spawn(node_path, commandArgs);
4951
child.stdin.end();
5052

51-
// TODO we no longer support node 0.6
52-
// Cannot use 'close' event because not on node-0.6.
5353
function _close() {
54-
var
55-
stderr = _bufferConcat(stderrBufs).toString(),
56-
stdout = _bufferConcat(stdoutBufs).toString();
57-
5854
if (stderrDone && stdoutDone && !done) {
55+
const
56+
stderr = _bufferConcat(stderrBufs).toString(),
57+
stdout = _bufferConcat(stdoutBufs).toString();
5958
done = true;
6059
cb(null, stderr, stdout);
6160
}
@@ -89,10 +88,9 @@ function _bufferConcat(buffers) {
8988
}
9089

9190
return new Buffer(buffers.reduce(function (acc, buf) {
92-
for (var i = 0; i < buf.length; i++) {
91+
for (let i = 0; i < buf.length; i++) {
9392
acc.push(buf[i]);
9493
}
9594
return acc;
9695
}, []));
9796
}
98-

test/issue121-test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22
// vim: expandtab:ts=2:sw=2
33

44
const
5-
assert = require('assert'),
65
assertions = require('./assertions'),
7-
childProcess = require('./child-process').childProcess,
6+
signalledProcess = require('./child-process').signalledProcess,
87
signals = ['SIGINT', 'SIGTERM'];
98

109
describe('tmp', function () {
1110
describe('issue121 - clean up on terminating signals', function () {
12-
for (var i=0; i < signals.length; i++) {
13-
issue121Tests(signals[i]);
11+
for (var i = 0; i < signals.length; i++) {
12+
it('for signal ' + signals[i], function (done) {
13+
issue121Tests(signals[i])(done);
14+
});
1415
}
1516
});
1617
});
1718

1819
function issue121Tests(signal) {
1920
return function (done) {
20-
childProcess('issue121.json', function (err, stderr, stdout) {
21+
signalledProcess(this, 'issue121.json', function (err, stderr, stdout) {
2122
if (err) return done(err);
2223
else if (stderr) return done(new Error(stderr));
23-
else assertions.assertDoesNotExist(stdout);
24+
25+
try {
26+
assertions.assertDoesNotExist(stdout);
27+
} catch (err) {
28+
done(err);
29+
}
2430
done();
25-
}, true);
31+
}, signal);
2632
};
2733
}

test/outband/issue121.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/* eslint-disable no-octal */
22
// vim: expandtab:ts=2:sw=2
33

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();
4+
const
5+
tmp = require('../../lib/tmp');
116

127
// https://github.com/raszi/node-tmp/issues/121
13-
module.exports = function (signal) {
14-
fixture.apply(this, [tmp.dirSync({ unsafeCleanup: true }), tmp]);
8+
module.exports = function () {
9+
10+
tmp.setGracefulCleanup();
11+
12+
const result = tmp.dirSync({ unsafeCleanup: true });
1513

16-
// make sure that the process keeps running
17-
setTimeout(function () {}, 1000000);
14+
this.out(result.name, function () { });
1815

19-
this.kill(signal);
16+
setTimeout(function () {
17+
throw new Error('ran into timeout');
18+
}, 10000);
2019
};

test/outband/issue121.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"graceful": true,
23
"tc": "issue121"
34
}

test/spawn-custom.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ spawn.graceful = !!config.graceful;
1212
var fn = require(path.join(__dirname, 'outband', config.tc));
1313
fn.apply(spawn);
1414

15+
spawn.kill('SIGINT');
16+

test/spawn-generic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (config.async)
2323
fnUnderTest(config.options, function (err, name, fdOrCallback, cb) {
2424
if (err) spawn.err(err);
2525
else {
26-
var result = null;
26+
var result = null;
2727
if (config.file) result = { name: name, fd: fdOrCallback, removeCallback: cb };
2828
else result = { name: name, removeCallback: fdOrCallback };
2929
fn.apply(spawn, [result, tmp]);

test/spawn-signalled.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// vim: expandtab:ts=2:sw=2
2+
3+
var
4+
path = require('path'),
5+
readJsonConfig = require('./util').readJsonConfig,
6+
spawn = require('./spawn');
7+
8+
var config = readJsonConfig(process.argv[2]);
9+
var signal = process.argv[3];
10+
spawn.graceful = !!config.graceful;
11+
12+
// import the test case function and execute it
13+
var fn = require(path.join(__dirname, 'outband', config.tc));
14+
fn.apply(spawn);
15+
16+
spawn.kill(signal);
17+

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)