UNPKG

10.4 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) 2017, Daniel Imms (MIT License).
4 * Copyright (c) 2018, Microsoft Corporation (MIT License).
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7var unixTerminal_1 = require("./unixTerminal");
8var assert = require("assert");
9var cp = require("child_process");
10var path = require("path");
11var testUtils_test_1 = require("./testUtils.test");
12var FIXTURES_PATH = path.normalize(path.join(__dirname, '..', 'fixtures', 'utf8-character.txt'));
13if (process.platform !== 'win32') {
14 describe('UnixTerminal', function () {
15 describe('Constructor', function () {
16 it('should set a valid pts name', function () {
17 var term = new unixTerminal_1.UnixTerminal('/bin/bash', [], {});
18 var regExp;
19 if (process.platform === 'linux') {
20 // https://linux.die.net/man/4/pts
21 regExp = /^\/dev\/pts\/\d+$/;
22 }
23 if (process.platform === 'darwin') {
24 // https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man4/pty.4.html
25 regExp = /^\/dev\/tty[p-sP-S][a-z0-9]+$/;
26 }
27 if (regExp) {
28 assert.ok(regExp.test(term._pty), '"' + term._pty + '" should match ' + regExp.toString());
29 }
30 });
31 });
32 describe('PtyForkEncodingOption', function () {
33 it('should default to utf8', function (done) {
34 var term = new unixTerminal_1.UnixTerminal('/bin/bash', ['-c', "cat \"" + FIXTURES_PATH + "\""]);
35 term.on('data', function (data) {
36 assert.equal(typeof data, 'string');
37 assert.equal(data, '\u00E6');
38 done();
39 });
40 });
41 it('should return a Buffer when encoding is null', function (done) {
42 var term = new unixTerminal_1.UnixTerminal('/bin/bash', ['-c', "cat \"" + FIXTURES_PATH + "\""], {
43 encoding: null
44 });
45 term.on('data', function (data) {
46 assert.equal(typeof data, 'object');
47 assert.ok(data instanceof Buffer);
48 assert.equal(0xC3, data[0]);
49 assert.equal(0xA6, data[1]);
50 done();
51 });
52 });
53 it('should support other encodings', function (done) {
54 var text = 'test æ!';
55 var term = new unixTerminal_1.UnixTerminal(null, ['-c', 'echo "' + text + '"'], {
56 encoding: 'base64'
57 });
58 var buffer = '';
59 term.on('data', function (data) {
60 assert.equal(typeof data, 'string');
61 buffer += data;
62 });
63 term.on('exit', function () {
64 assert.equal(Buffer.alloc(8, buffer, 'base64').toString().replace('\r', '').replace('\n', ''), text);
65 done();
66 });
67 });
68 });
69 describe('open', function () {
70 var term;
71 afterEach(function () {
72 if (term) {
73 term.slave.destroy();
74 term.master.destroy();
75 }
76 });
77 it('should open a pty with access to a master and slave socket', function (done) {
78 term = unixTerminal_1.UnixTerminal.open({});
79 var slavebuf = '';
80 term.slave.on('data', function (data) {
81 slavebuf += data;
82 });
83 var masterbuf = '';
84 term.master.on('data', function (data) {
85 masterbuf += data;
86 });
87 testUtils_test_1.pollUntil(function () {
88 if (masterbuf === 'slave\r\nmaster\r\n' && slavebuf === 'master\n') {
89 done();
90 return true;
91 }
92 return false;
93 }, 200, 10);
94 term.slave.write('slave\n');
95 term.master.write('master\n');
96 });
97 });
98 describe('signals in parent and child', function () {
99 it('SIGINT - custom in parent and child', function (done) {
100 // this test is cumbersome - we have to run it in a sub process to
101 // see behavior of SIGINT handlers
102 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 ";
103 var buffer = [];
104 var p = cp.spawn('node', ['-e', data]);
105 var sub = '';
106 p.stdout.on('data', function (data) {
107 if (!data.toString().indexOf('ready')) {
108 sub = data.toString().split(' ')[1].slice(0, -1);
109 setTimeout(function () {
110 process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child
111 p.kill('SIGINT'); // SIGINT to parent
112 }, 200);
113 }
114 else {
115 buffer.push(data.toString().replace(/^\s+|\s+$/g, ''));
116 }
117 });
118 p.on('close', function () {
119 // handlers in parent and child should have been triggered
120 assert.equal(buffer.indexOf('SIGINT in child') !== -1, true);
121 assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true);
122 done();
123 });
124 });
125 it('SIGINT - custom in parent, default in child', function (done) {
126 // this tests the original idea of the signal(...) change in pty.cc:
127 // to make sure the SIGINT handler of a pty child is reset to default
128 // and does not interfere with the handler in the parent
129 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 ";
130 var buffer = [];
131 var p = cp.spawn('node', ['-e', data]);
132 var sub = '';
133 p.stdout.on('data', function (data) {
134 if (!data.toString().indexOf('ready')) {
135 sub = data.toString().split(' ')[1].slice(0, -1);
136 setTimeout(function () {
137 process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child
138 p.kill('SIGINT'); // SIGINT to parent
139 }, 200);
140 }
141 else {
142 buffer.push(data.toString().replace(/^\s+|\s+$/g, ''));
143 }
144 });
145 p.on('close', function () {
146 // handlers in parent and child should have been triggered
147 assert.equal(buffer.indexOf('should not be printed') !== -1, false);
148 assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true);
149 done();
150 });
151 });
152 it('SIGHUP default (child only)', function (done) {
153 var term = new unixTerminal_1.UnixTerminal('node', ['-e', "\n console.log('ready');\n setTimeout(()=>console.log('timeout'), 200);"
154 ]);
155 var buffer = '';
156 term.on('data', function (data) {
157 if (data === 'ready\r\n') {
158 term.kill();
159 }
160 else {
161 buffer += data;
162 }
163 });
164 term.on('exit', function () {
165 // no timeout in buffer
166 assert.equal(buffer, '');
167 done();
168 });
169 });
170 it('SIGUSR1 - custom in parent and child', function (done) {
171 var pHandlerCalled = 0;
172 var handleSigUsr = function (h) {
173 return function () {
174 pHandlerCalled += 1;
175 process.removeListener('SIGUSR1', h);
176 };
177 };
178 process.on('SIGUSR1', handleSigUsr(handleSigUsr));
179 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);"
180 ]);
181 var buffer = '';
182 term.on('data', function (data) {
183 if (data === 'ready\r\n') {
184 process.kill(process.pid, 'SIGUSR1');
185 term.kill('SIGUSR1');
186 }
187 else {
188 buffer += data;
189 }
190 });
191 term.on('exit', function () {
192 // should have called both handlers and only once
193 assert.equal(pHandlerCalled, 1);
194 assert.equal(buffer, 'SIGUSR1 in child\r\n');
195 done();
196 });
197 });
198 });
199 });
200}
201//# sourceMappingURL=unixTerminal.test.js.map
\No newline at end of file