Comparing version 0.10.0-beta6 to 0.10.0-beta7
@@ -9,2 +9,3 @@ "use strict"; | ||
var assert = require("assert"); | ||
var cp = require("child_process"); | ||
var path = require("path"); | ||
@@ -99,4 +100,105 @@ var testUtils_test_1 = require("./testUtils.test"); | ||
}); | ||
describe('signals in parent and child', function () { | ||
it('SIGINT - custom in parent and child', function (done) { | ||
// this test is cumbersome - we have to run it in a sub process to | ||
// see behavior of SIGINT handlers | ||
var data = "\n var pty = require('./lib/index');\n process.on('SIGINT', () => console.log('SIGINT in parent'));\n var ptyProcess = pty.spawn('node', ['-e', 'process.on(\"SIGINT\", ()=>console.log(\"SIGINT in child\"));setTimeout(() => null, 300);'], {\n name: 'xterm-color',\n cols: 80,\n rows: 30,\n cwd: process.env.HOME,\n env: process.env\n });\n ptyProcess.on('data', function (data) {\n console.log(data);\n });\n setTimeout(() => null, 500);\n console.log('ready', ptyProcess.pid);\n "; | ||
var buffer = []; | ||
var p = cp.spawn('node', ['-e', data]); | ||
var sub = ''; | ||
p.stdout.on('data', function (data) { | ||
if (!data.toString().indexOf('ready')) { | ||
sub = data.toString().split(' ')[1].slice(0, -1); | ||
setTimeout(function () { | ||
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child | ||
p.kill('SIGINT'); // SIGINT to parent | ||
}, 200); | ||
} | ||
else { | ||
buffer.push(data.toString().replace(/^\s+|\s+$/g, '')); | ||
} | ||
}); | ||
p.on('close', function () { | ||
// handlers in parent and child should have been triggered | ||
assert.equal(buffer.indexOf('SIGINT in child') !== -1, true); | ||
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true); | ||
done(); | ||
}); | ||
}); | ||
it('SIGINT - custom in parent, default in child', function (done) { | ||
// this tests the original idea of the signal(...) change in pty.cc: | ||
// to make sure the SIGINT handler of a pty child is reset to default | ||
// and does not interfere with the handler in the parent | ||
var data = "\n var pty = require('./lib/index');\n process.on('SIGINT', () => console.log('SIGINT in parent'));\n var ptyProcess = pty.spawn('node', ['-e', 'setTimeout(() => console.log(\"should not be printed\"), 300);'], {\n name: 'xterm-color',\n cols: 80,\n rows: 30,\n cwd: process.env.HOME,\n env: process.env\n });\n ptyProcess.on('data', function (data) {\n console.log(data);\n });\n setTimeout(() => null, 500);\n console.log('ready', ptyProcess.pid);\n "; | ||
var buffer = []; | ||
var p = cp.spawn('node', ['-e', data]); | ||
var sub = ''; | ||
p.stdout.on('data', function (data) { | ||
if (!data.toString().indexOf('ready')) { | ||
sub = data.toString().split(' ')[1].slice(0, -1); | ||
setTimeout(function () { | ||
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child | ||
p.kill('SIGINT'); // SIGINT to parent | ||
}, 200); | ||
} | ||
else { | ||
buffer.push(data.toString().replace(/^\s+|\s+$/g, '')); | ||
} | ||
}); | ||
p.on('close', function () { | ||
// handlers in parent and child should have been triggered | ||
assert.equal(buffer.indexOf('should not be printed') !== -1, false); | ||
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true); | ||
done(); | ||
}); | ||
}); | ||
it('SIGHUP default (child only)', function (done) { | ||
var term = new unixTerminal_1.UnixTerminal('node', ['-e', "\n console.log('ready');\n setTimeout(()=>console.log('timeout'), 200);" | ||
]); | ||
var buffer = ''; | ||
term.on('data', function (data) { | ||
if (data === 'ready\r\n') { | ||
term.kill(); | ||
} | ||
else { | ||
buffer += data; | ||
} | ||
}); | ||
term.on('exit', function () { | ||
// no timeout in buffer | ||
assert.equal(buffer, ''); | ||
done(); | ||
}); | ||
}); | ||
it('SIGUSR1 - custom in parent and child', function (done) { | ||
var pHandlerCalled = 0; | ||
var handleSigUsr = function (h) { | ||
return function () { | ||
pHandlerCalled += 1; | ||
process.removeListener('SIGUSR1', h); | ||
}; | ||
}; | ||
process.on('SIGUSR1', handleSigUsr(handleSigUsr)); | ||
var term = new unixTerminal_1.UnixTerminal('node', ['-e', "\n process.on('SIGUSR1', () => {\n console.log('SIGUSR1 in child');\n });\n console.log('ready');\n setTimeout(()=>null, 200);" | ||
]); | ||
var buffer = ''; | ||
term.on('data', function (data) { | ||
if (data === 'ready\r\n') { | ||
process.kill(process.pid, 'SIGUSR1'); | ||
term.kill('SIGUSR1'); | ||
} | ||
else { | ||
buffer += data; | ||
} | ||
}); | ||
term.on('exit', function () { | ||
// should have called both handlers and only once | ||
assert.equal(pHandlerCalled, 1); | ||
assert.equal(buffer, 'SIGUSR1 in child\r\n'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
//# sourceMappingURL=unixTerminal.test.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.10.0-beta6", | ||
"version": "0.10.0-beta7", | ||
"license": "MIT", | ||
@@ -10,0 +10,0 @@ "main": "./lib/index.js", |
@@ -8,2 +8,3 @@ /** | ||
import * as assert from 'assert'; | ||
import * as cp from 'child_process'; | ||
import * as path from 'path'; | ||
@@ -108,3 +109,138 @@ import { pollUntil } from './testUtils.test'; | ||
}); | ||
describe('signals in parent and child', () => { | ||
it('SIGINT - custom in parent and child', done => { | ||
// this test is cumbersome - we have to run it in a sub process to | ||
// see behavior of SIGINT handlers | ||
const data = ` | ||
var pty = require('./lib/index'); | ||
process.on('SIGINT', () => console.log('SIGINT in parent')); | ||
var ptyProcess = pty.spawn('node', ['-e', 'process.on("SIGINT", ()=>console.log("SIGINT in child"));setTimeout(() => null, 300);'], { | ||
name: 'xterm-color', | ||
cols: 80, | ||
rows: 30, | ||
cwd: process.env.HOME, | ||
env: process.env | ||
}); | ||
ptyProcess.on('data', function (data) { | ||
console.log(data); | ||
}); | ||
setTimeout(() => null, 500); | ||
console.log('ready', ptyProcess.pid); | ||
`; | ||
let buffer: string[] = []; | ||
const p = cp.spawn('node', ['-e', data]); | ||
let sub = ''; | ||
p.stdout.on('data', (data) => { | ||
if (!data.toString().indexOf('ready')) { | ||
sub = data.toString().split(' ')[1].slice(0, -1); | ||
setTimeout(() => { | ||
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child | ||
p.kill('SIGINT'); // SIGINT to parent | ||
}, 200); | ||
} else { | ||
buffer.push(data.toString().replace(/^\s+|\s+$/g, '')); | ||
} | ||
}); | ||
p.on('close', () => { | ||
// handlers in parent and child should have been triggered | ||
assert.equal(buffer.indexOf('SIGINT in child') !== -1, true); | ||
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true); | ||
done(); | ||
}); | ||
}); | ||
it('SIGINT - custom in parent, default in child', done => { | ||
// this tests the original idea of the signal(...) change in pty.cc: | ||
// to make sure the SIGINT handler of a pty child is reset to default | ||
// and does not interfere with the handler in the parent | ||
const data = ` | ||
var pty = require('./lib/index'); | ||
process.on('SIGINT', () => console.log('SIGINT in parent')); | ||
var ptyProcess = pty.spawn('node', ['-e', 'setTimeout(() => console.log("should not be printed"), 300);'], { | ||
name: 'xterm-color', | ||
cols: 80, | ||
rows: 30, | ||
cwd: process.env.HOME, | ||
env: process.env | ||
}); | ||
ptyProcess.on('data', function (data) { | ||
console.log(data); | ||
}); | ||
setTimeout(() => null, 500); | ||
console.log('ready', ptyProcess.pid); | ||
`; | ||
let buffer: string[] = []; | ||
const p = cp.spawn('node', ['-e', data]); | ||
let sub = ''; | ||
p.stdout.on('data', (data) => { | ||
if (!data.toString().indexOf('ready')) { | ||
sub = data.toString().split(' ')[1].slice(0, -1); | ||
setTimeout(() => { | ||
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child | ||
p.kill('SIGINT'); // SIGINT to parent | ||
}, 200); | ||
} else { | ||
buffer.push(data.toString().replace(/^\s+|\s+$/g, '')); | ||
} | ||
}); | ||
p.on('close', () => { | ||
// handlers in parent and child should have been triggered | ||
assert.equal(buffer.indexOf('should not be printed') !== -1, false); | ||
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true); | ||
done(); | ||
}); | ||
}); | ||
it('SIGHUP default (child only)', done => { | ||
const term = new UnixTerminal('node', [ '-e', ` | ||
console.log('ready'); | ||
setTimeout(()=>console.log('timeout'), 200);` | ||
]); | ||
let buffer = ''; | ||
term.on('data', (data) => { | ||
if (data === 'ready\r\n') { | ||
term.kill(); | ||
} else { | ||
buffer += data; | ||
} | ||
}); | ||
term.on('exit', () => { | ||
// no timeout in buffer | ||
assert.equal(buffer, ''); | ||
done(); | ||
}); | ||
}); | ||
it('SIGUSR1 - custom in parent and child', done => { | ||
let pHandlerCalled = 0; | ||
const handleSigUsr = function(h: any): any { | ||
return function(): void { | ||
pHandlerCalled += 1; | ||
process.removeListener('SIGUSR1', h); | ||
}; | ||
}; | ||
process.on('SIGUSR1', handleSigUsr(handleSigUsr)); | ||
const term = new UnixTerminal('node', [ '-e', ` | ||
process.on('SIGUSR1', () => { | ||
console.log('SIGUSR1 in child'); | ||
}); | ||
console.log('ready'); | ||
setTimeout(()=>null, 200);` | ||
]); | ||
let buffer = ''; | ||
term.on('data', (data) => { | ||
if (data === 'ready\r\n') { | ||
process.kill(process.pid, 'SIGUSR1'); | ||
term.kill('SIGUSR1'); | ||
} else { | ||
buffer += data; | ||
} | ||
}); | ||
term.on('exit', () => { | ||
// should have called both handlers and only once | ||
assert.equal(pHandlerCalled, 1); | ||
assert.equal(buffer, 'SIGUSR1 in child\r\n'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1105242
3849
7