UNPKG

9.06 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 fs = require("fs");
8var assert = require("assert");
9var windowsTerminal_1 = require("./windowsTerminal");
10var path = require("path");
11var psList = require("ps-list");
12function pollForProcessState(desiredState, intervalMs, timeoutMs) {
13 if (intervalMs === void 0) { intervalMs = 100; }
14 if (timeoutMs === void 0) { timeoutMs = 2000; }
15 return new Promise(function (resolve) {
16 var tries = 0;
17 var interval = setInterval(function () {
18 psList({ all: true }).then(function (ps) {
19 var success = true;
20 var pids = Object.keys(desiredState).map(function (k) { return parseInt(k, 10); });
21 pids.forEach(function (pid) {
22 if (desiredState[pid]) {
23 if (!ps.some(function (p) { return p.pid === pid; })) {
24 success = false;
25 }
26 }
27 else {
28 if (ps.some(function (p) { return p.pid === pid; })) {
29 success = false;
30 }
31 }
32 });
33 if (success) {
34 clearInterval(interval);
35 resolve();
36 return;
37 }
38 tries++;
39 if (tries * intervalMs >= timeoutMs) {
40 clearInterval(interval);
41 var processListing = pids.map(function (k) { return k + ": " + desiredState[k]; }).join('\n');
42 assert.fail("Bad process state, expected:\n" + processListing);
43 resolve();
44 }
45 });
46 }, intervalMs);
47 });
48}
49function pollForProcessTreeSize(pid, size, intervalMs, timeoutMs) {
50 if (intervalMs === void 0) { intervalMs = 100; }
51 if (timeoutMs === void 0) { timeoutMs = 2000; }
52 return new Promise(function (resolve) {
53 var tries = 0;
54 var interval = setInterval(function () {
55 psList({ all: true }).then(function (ps) {
56 var openList = [];
57 openList.push(ps.filter(function (p) { return p.pid === pid; }).map(function (p) {
58 return { name: p.name, pid: p.pid };
59 })[0]);
60 var list = [];
61 var _loop_1 = function () {
62 var current = openList.shift();
63 ps.filter(function (p) { return p.ppid === current.pid; }).map(function (p) {
64 return { name: p.name, pid: p.pid };
65 }).forEach(function (p) { return openList.push(p); });
66 list.push(current);
67 };
68 while (openList.length) {
69 _loop_1();
70 }
71 var success = list.length === size;
72 if (success) {
73 clearInterval(interval);
74 resolve(list);
75 return;
76 }
77 tries++;
78 if (tries * intervalMs >= timeoutMs) {
79 clearInterval(interval);
80 assert.fail("Bad process state, expected: " + size + ", actual: " + list.length);
81 resolve();
82 }
83 });
84 }, intervalMs);
85 });
86}
87if (process.platform === 'win32') {
88 describe('WindowsTerminal', function () {
89 describe('kill', function () {
90 it('should not crash parent process', function (done) {
91 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', [], {});
92 term.kill();
93 // Add done call to deferred function queue to ensure the kill call has completed
94 term._defer(done);
95 });
96 it('should kill the process tree', function (done) {
97 this.timeout(5000);
98 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', [], {});
99 // Start sub-processes
100 term.write('powershell.exe\r');
101 term.write('notepad.exe\r');
102 term.write('node.exe\r');
103 pollForProcessTreeSize(term.pid, 4, 500, 5000).then(function (list) {
104 assert.equal(list[0].name, 'cmd.exe');
105 assert.equal(list[1].name, 'powershell.exe');
106 assert.equal(list[2].name, 'notepad.exe');
107 assert.equal(list[3].name, 'node.exe');
108 term.kill();
109 var desiredState = {};
110 desiredState[list[0].pid] = false;
111 desiredState[list[1].pid] = false;
112 desiredState[list[2].pid] = true;
113 desiredState[list[3].pid] = false;
114 pollForProcessState(desiredState).then(function () {
115 // Kill notepad before done
116 process.kill(list[2].pid);
117 done();
118 });
119 });
120 });
121 });
122 describe('resize', function () {
123 it('should throw a non-native exception when resizing an invalid value', function () {
124 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', [], {});
125 assert.throws(function () { return term.resize(-1, -1); });
126 assert.throws(function () { return term.resize(0, 0); });
127 assert.doesNotThrow(function () { return term.resize(1, 1); });
128 });
129 it('should throw an non-native exception when resizing a killed terminal', function (done) {
130 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', [], {});
131 term._defer(function () {
132 term.on('exit', function () {
133 assert.throws(function () { return term.resize(1, 1); });
134 done();
135 });
136 term.destroy();
137 });
138 });
139 });
140 describe('Args as CommandLine', function () {
141 it('should not fail running a file containing a space in the path', function (done) {
142 var spaceFolder = path.resolve(__dirname, '..', 'fixtures', 'space folder');
143 if (!fs.existsSync(spaceFolder)) {
144 fs.mkdirSync(spaceFolder);
145 }
146 var cmdCopiedPath = path.resolve(spaceFolder, 'cmd.exe');
147 var data = fs.readFileSync(process.env.windir + "\\System32\\cmd.exe");
148 fs.writeFileSync(cmdCopiedPath, data);
149 if (!fs.existsSync(cmdCopiedPath)) {
150 // Skip test if git bash isn't installed
151 return;
152 }
153 var term = new windowsTerminal_1.WindowsTerminal(cmdCopiedPath, '/c echo "hello world"', {});
154 var result = '';
155 term.on('data', function (data) {
156 result += data;
157 });
158 term.on('exit', function () {
159 assert.ok(result.indexOf('hello world') >= 1);
160 done();
161 });
162 });
163 });
164 describe('env', function () {
165 it('should set environment variables of the shell', function (done) {
166 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', '/C echo %FOO%', { env: { FOO: 'BAR' } });
167 var result = '';
168 term.on('data', function (data) {
169 result += data;
170 });
171 term.on('exit', function () {
172 assert.ok(result.indexOf('BAR') >= 0);
173 done();
174 });
175 });
176 });
177 describe('On close', function () {
178 it('should return process zero exit codes', function (done) {
179 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', '/C exit');
180 term.on('exit', function (code) {
181 assert.equal(code, 0);
182 done();
183 });
184 });
185 it('should return process non-zero exit codes', function (done) {
186 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', '/C exit 2');
187 term.on('exit', function (code) {
188 assert.equal(code, 2);
189 done();
190 });
191 });
192 });
193 describe('winpty', function () {
194 it('should accept input', function (done) {
195 var term = new windowsTerminal_1.WindowsTerminal('cmd.exe', '', { useConpty: false });
196 term.write('exit\r');
197 term.on('exit', function () {
198 done();
199 });
200 });
201 });
202 });
203}
204//# sourceMappingURL=windowsTerminal.test.js.map
\No newline at end of file