-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathextHostTerminalShellIntegration.test.ts
More file actions
308 lines (270 loc) · 12.2 KB
/
extHostTerminalShellIntegration.test.ts
File metadata and controls
308 lines (270 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type Terminal, type TerminalShellExecution, type TerminalShellExecutionCommandLine, type TerminalShellExecutionStartEvent } from 'vscode';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
import { InternalTerminalShellIntegration } from '../../common/extHostTerminalShellIntegration.js';
import { Emitter } from '../../../../base/common/event.js';
import { TerminalShellExecutionCommandLineConfidence } from '../../common/extHostTypes.js';
import { deepStrictEqual, notStrictEqual, strictEqual } from 'assert';
import type { URI } from '../../../../base/common/uri.js';
import { DeferredPromise } from '../../../../base/common/async.js';
function cmdLine(value: string): TerminalShellExecutionCommandLine {
return Object.freeze({
confidence: TerminalShellExecutionCommandLineConfidence.High,
value,
isTrusted: true,
});
}
function asCmdLine(value: string | TerminalShellExecutionCommandLine): TerminalShellExecutionCommandLine {
if (typeof value === 'string') {
return cmdLine(value);
}
return value;
}
function vsc(data: string) {
return `\x1b]633;${data}\x07`;
}
const testCommandLine = 'echo hello world';
const testCommandLine2 = 'echo goodbye world';
interface ITrackedEvent {
type: 'start' | 'data' | 'end';
commandLine: string;
data?: string;
}
suite('InternalTerminalShellIntegration', () => {
const store = ensureNoDisposablesAreLeakedInTestSuite();
let si: InternalTerminalShellIntegration;
let terminal: Terminal;
let onDidStartTerminalShellExecution: Emitter<TerminalShellExecutionStartEvent>;
let trackedEvents: ITrackedEvent[];
let readIteratorsFlushed: Promise<void>[];
async function startExecutionAwaitObject(commandLine: string | TerminalShellExecutionCommandLine, cwd?: URI): Promise<TerminalShellExecution> {
return await new Promise<TerminalShellExecution>(r => {
store.add(onDidStartTerminalShellExecution.event(e => {
r(e.execution);
}));
si.startShellExecution(asCmdLine(commandLine), cwd);
});
}
async function endExecutionAwaitObject(commandLine: string | TerminalShellExecutionCommandLine): Promise<TerminalShellExecution> {
return await new Promise<TerminalShellExecution>(r => {
store.add(si.onDidRequestEndExecution(e => r(e.execution)));
si.endShellExecution(asCmdLine(commandLine), 0);
});
}
async function emitData(data: string): Promise<void> {
// AsyncIterableProducers are initialized in a microtask, this doesn't matter in practice
// since the events will always come through in different events.
await new Promise<void>(r => queueMicrotask(r));
si.emitData(data);
}
function assertTrackedEvents(expected: ITrackedEvent[]) {
deepStrictEqual(trackedEvents, expected);
}
function assertNonDataTrackedEvents(expected: ITrackedEvent[]) {
deepStrictEqual(trackedEvents.filter(e => e.type !== 'data'), expected);
}
function assertDataTrackedEvents(expected: ITrackedEvent[]) {
deepStrictEqual(trackedEvents.filter(e => e.type === 'data'), expected);
}
setup(() => {
// eslint-disable-next-line local/code-no-any-casts
terminal = Symbol('testTerminal') as any;
onDidStartTerminalShellExecution = store.add(new Emitter());
si = store.add(new InternalTerminalShellIntegration(terminal, true, onDidStartTerminalShellExecution));
trackedEvents = [];
readIteratorsFlushed = [];
store.add(onDidStartTerminalShellExecution.event(async e => {
trackedEvents.push({
type: 'start',
commandLine: e.execution.commandLine.value,
});
const stream = e.execution.read();
const readIteratorsFlushedDeferred = new DeferredPromise<void>();
readIteratorsFlushed.push(readIteratorsFlushedDeferred.p);
for await (const data of stream) {
trackedEvents.push({
type: 'data',
commandLine: e.execution.commandLine.value,
data,
});
}
readIteratorsFlushedDeferred.complete();
}));
store.add(si.onDidRequestEndExecution(e => trackedEvents.push({
type: 'end',
commandLine: e.execution.commandLine.value,
})));
});
test('simple execution', async () => {
const execution = await startExecutionAwaitObject(testCommandLine);
deepStrictEqual(execution.commandLine.value, testCommandLine);
const execution2 = await endExecutionAwaitObject(testCommandLine);
strictEqual(execution2, execution);
assertTrackedEvents([
{ commandLine: testCommandLine, type: 'start' },
{ commandLine: testCommandLine, type: 'end' },
]);
});
test('different execution unexpectedly ended', async () => {
const execution1 = await startExecutionAwaitObject(testCommandLine);
const execution2 = await endExecutionAwaitObject(testCommandLine2);
strictEqual(execution1, execution2, 'when a different execution is ended, the one that started first should end');
assertTrackedEvents([
{ commandLine: testCommandLine, type: 'start' },
// This looks weird, but it's the same execution behind the scenes, just the command
// line was updated
{ commandLine: testCommandLine2, type: 'end' },
]);
});
test('no end event', async () => {
const execution1 = await startExecutionAwaitObject(testCommandLine);
const endedExecution = await new Promise<TerminalShellExecution>(r => {
store.add(si.onDidRequestEndExecution(e => r(e.execution)));
startExecutionAwaitObject(testCommandLine2);
});
strictEqual(execution1, endedExecution, 'when no end event is fired, the current execution should end');
// Clean up disposables
await endExecutionAwaitObject(testCommandLine2);
await Promise.all(readIteratorsFlushed);
assertTrackedEvents([
{ commandLine: testCommandLine, type: 'start' },
{ commandLine: testCommandLine, type: 'end' },
{ commandLine: testCommandLine2, type: 'start' },
{ commandLine: testCommandLine2, type: 'end' },
]);
});
suite('executeCommand', () => {
test('^C to clear previous command', async () => {
const commandLine = 'foo';
const apiRequestedExecution = si.requestNewShellExecution(cmdLine(commandLine), undefined);
const firstExecution = await startExecutionAwaitObject('^C');
notStrictEqual(firstExecution, apiRequestedExecution.value);
si.emitData('SIGINT');
si.endShellExecution(cmdLine('^C'), 0);
si.startShellExecution(cmdLine(commandLine), undefined);
await emitData('1');
await endExecutionAwaitObject(commandLine);
// IMPORTANT: We cannot reliably assert the order of data events here because flushing
// of the async iterator is asynchronous and could happen after the execution's end
// event fires if an execution is started immediately afterwards.
await Promise.all(readIteratorsFlushed);
assertNonDataTrackedEvents([
{ commandLine: '^C', type: 'start' },
{ commandLine: '^C', type: 'end' },
{ commandLine, type: 'start' },
{ commandLine, type: 'end' },
]);
assertDataTrackedEvents([
{ commandLine: '^C', type: 'data', data: 'SIGINT' },
{ commandLine, type: 'data', data: '1' },
]);
});
test('multi-line command line', async () => {
const commandLine = 'foo\nbar';
const apiRequestedExecution = si.requestNewShellExecution(cmdLine(commandLine), undefined);
const startedExecution = await startExecutionAwaitObject('foo');
strictEqual(startedExecution, apiRequestedExecution.value);
si.emitData('1');
si.emitData('2');
si.endShellExecution(cmdLine('foo'), 0);
si.startShellExecution(cmdLine('bar'), undefined);
si.emitData('3');
si.emitData('4');
const endedExecution = await endExecutionAwaitObject('bar');
strictEqual(startedExecution, endedExecution);
assertTrackedEvents([
{ commandLine, type: 'start' },
{ commandLine, type: 'data', data: '1' },
{ commandLine, type: 'data', data: '2' },
{ commandLine, type: 'data', data: '3' },
{ commandLine, type: 'data', data: '4' },
{ commandLine, type: 'end' },
]);
});
test('multi-line command with long second command', async () => {
const commandLine = 'echo foo\ncat << EOT\nline1\nline2\nline3\nEOT';
const subCommandLine1 = 'echo foo';
const subCommandLine2 = 'cat << EOT\nline1\nline2\nline3\nEOT';
const apiRequestedExecution = si.requestNewShellExecution(cmdLine(commandLine), undefined);
const startedExecution = await startExecutionAwaitObject(subCommandLine1);
strictEqual(startedExecution, apiRequestedExecution.value);
si.emitData(`${vsc('C')}foo`);
si.endShellExecution(cmdLine(subCommandLine1), 0);
si.startShellExecution(cmdLine(subCommandLine2), undefined);
si.emitData(`${vsc('C')}line1`);
si.emitData('line2');
si.emitData('line3');
const endedExecution = await endExecutionAwaitObject(subCommandLine2);
strictEqual(startedExecution, endedExecution);
assertTrackedEvents([
{ commandLine, type: 'start' },
{ commandLine, type: 'data', data: `${vsc('C')}foo` },
{ commandLine, type: 'data', data: `${vsc('C')}line1` },
{ commandLine, type: 'data', data: 'line2' },
{ commandLine, type: 'data', data: 'line3' },
{ commandLine, type: 'end' },
]);
});
test('multi-line command comment followed by long second command', async () => {
const commandLine = '# comment: foo\ncat << EOT\nline1\nline2\nline3\nEOT';
const subCommandLine1 = '# comment: foo';
const subCommandLine2 = 'cat << EOT\nline1\nline2\nline3\nEOT';
const apiRequestedExecution = si.requestNewShellExecution(cmdLine(commandLine), undefined);
const startedExecution = await startExecutionAwaitObject(subCommandLine1);
strictEqual(startedExecution, apiRequestedExecution.value);
si.emitData(`${vsc('C')}`);
si.endShellExecution(cmdLine(subCommandLine1), 0);
si.startShellExecution(cmdLine(subCommandLine2), undefined);
si.emitData(`${vsc('C')}line1`);
si.emitData('line2');
si.emitData('line3');
const endedExecution = await endExecutionAwaitObject(subCommandLine2);
strictEqual(startedExecution, endedExecution);
assertTrackedEvents([
{ commandLine, type: 'start' },
{ commandLine, type: 'data', data: `${vsc('C')}` },
{ commandLine, type: 'data', data: `${vsc('C')}line1` },
{ commandLine, type: 'data', data: 'line2' },
{ commandLine, type: 'data', data: 'line3' },
{ commandLine, type: 'end' },
]);
});
test('4 multi-line commands with output', async () => {
const commandLine = 'echo "\nfoo"\ngit commit -m "hello\n\nworld"\ncat << EOT\nline1\nline2\nline3\nEOT\n{\necho "foo"\n}';
const subCommandLine1 = 'echo "\nfoo"';
const subCommandLine2 = 'git commit -m "hello\n\nworld"';
const subCommandLine3 = 'cat << EOT\nline1\nline2\nline3\nEOT';
const subCommandLine4 = '{\necho "foo"\n}';
const apiRequestedExecution = si.requestNewShellExecution(cmdLine(commandLine), undefined);
const startedExecution = await startExecutionAwaitObject(subCommandLine1);
strictEqual(startedExecution, apiRequestedExecution.value);
si.emitData(`${vsc('C')}foo`);
si.endShellExecution(cmdLine(subCommandLine1), 0);
si.startShellExecution(cmdLine(subCommandLine2), undefined);
si.emitData(`${vsc('C')} 2 files changed, 61 insertions(+), 2 deletions(-)`);
si.endShellExecution(cmdLine(subCommandLine2), 0);
si.startShellExecution(cmdLine(subCommandLine3), undefined);
si.emitData(`${vsc('C')}line1`);
si.emitData('line2');
si.emitData('line3');
si.endShellExecution(cmdLine(subCommandLine3), 0);
si.emitData(`${vsc('C')}foo`);
si.startShellExecution(cmdLine(subCommandLine4), undefined);
const endedExecution = await endExecutionAwaitObject(subCommandLine4);
strictEqual(startedExecution, endedExecution);
assertTrackedEvents([
{ commandLine, type: 'start' },
{ commandLine, type: 'data', data: `${vsc('C')}foo` },
{ commandLine, type: 'data', data: `${vsc('C')} 2 files changed, 61 insertions(+), 2 deletions(-)` },
{ commandLine, type: 'data', data: `${vsc('C')}line1` },
{ commandLine, type: 'data', data: 'line2' },
{ commandLine, type: 'data', data: 'line3' },
{ commandLine, type: 'data', data: `${vsc('C')}foo` },
{ commandLine, type: 'end' },
]);
});
});
});