terminal.test.ts 19.4 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { window, Pseudoterminal, EventEmitter, TerminalDimensions, workspace, ConfigurationTarget, Disposable, UIKind, env } from 'vscode';
7
import { doesNotThrow, equal, ok, deepEqual, throws } from 'assert';
8

9 10
// TODO@Daniel flaky tests (https://github.com/microsoft/vscode/issues/92826)
((env.uiKind === UIKind.Web) ? suite.skip : suite)('vscode API - terminal', () => {
11 12 13 14 15
	suiteSetup(async () => {
		// Disable conpty in integration tests because of https://github.com/microsoft/vscode/issues/76548
		await workspace.getConfiguration('terminal.integrated').update('windowsEnableConpty', false, ConfigurationTarget.Global);
	});
	suite('Terminal', () => {
16 17 18 19 20 21 22
		let disposables: Disposable[] = [];

		teardown(() => {
			disposables.forEach(d => d.dispose());
			disposables.length = 0;
		});

23 24

		test('sendText immediately after createTerminal should not throw', (done) => {
25
			disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
26 27 28 29 30
				try {
					equal(terminal, term);
				} catch (e) {
					done(e);
				}
31
				terminal.dispose();
32 33
				disposables.push(window.onDidCloseTerminal(() => done()));
			}));
34 35 36 37
			const terminal = window.createTerminal();
			doesNotThrow(terminal.sendText.bind(terminal, 'echo "foo"'));
		});

38
		test('onDidCloseTerminal event fires when terminal is disposed', (done) => {
39
			disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
40 41 42 43 44
				try {
					equal(terminal, term);
				} catch (e) {
					done(e);
				}
45
				terminal.dispose();
46 47
				disposables.push(window.onDidCloseTerminal(() => done()));
			}));
48
			const terminal = window.createTerminal();
49 50 51
		});

		test('processId immediately after createTerminal should fetch the pid', (done) => {
52
			disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
53 54 55 56 57
				try {
					equal(terminal, term);
				} catch (e) {
					done(e);
				}
58
				terminal.processId.then(id => {
D
Daniel Imms 已提交
59
					try {
D
Daniel Imms 已提交
60
						ok(id && id > 0);
D
Daniel Imms 已提交
61 62 63
					} catch (e) {
						done(e);
					}
64
					terminal.dispose();
65
					disposables.push(window.onDidCloseTerminal(() => done()));
66
				});
67
			}));
68
			const terminal = window.createTerminal();
69 70
		});

71
		test('name in constructor should set terminal.name', (done) => {
72
			disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
73 74 75 76 77
				try {
					equal(terminal, term);
				} catch (e) {
					done(e);
				}
78
				terminal.dispose();
79 80
				disposables.push(window.onDidCloseTerminal(() => done()));
			}));
81
			const terminal = window.createTerminal('a');
D
Daniel Imms 已提交
82 83 84 85 86
			try {
				equal(terminal.name, 'a');
			} catch (e) {
				done(e);
			}
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
		test('creationOptions should be set and readonly for TerminalOptions terminals', (done) => {
			disposables.push(window.onDidOpenTerminal(term => {
				try {
					equal(terminal, term);
				} catch (e) {
					done(e);
				}
				terminal.dispose();
				disposables.push(window.onDidCloseTerminal(() => done()));
			}));
			const options = {
				name: 'foo',
				hideFromUser: true
			};
			const terminal = window.createTerminal(options);
			try {
				equal(terminal.name, 'foo');
				deepEqual(terminal.creationOptions, options);
				throws(() => (<any>terminal.creationOptions).name = 'bad', 'creationOptions should be readonly at runtime');
			} catch (e) {
				done(e);
			}
		});

113
		test('onDidOpenTerminal should fire when a terminal is created', (done) => {
114
			disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
115 116 117 118 119
				try {
					equal(term.name, 'b');
				} catch (e) {
					done(e);
				}
120
				disposables.push(window.onDidCloseTerminal(() => done()));
121
				terminal.dispose();
122
			}));
123 124
			const terminal = window.createTerminal('b');
		});
125

D
Daniel Imms 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
		test('exitStatus.code should be set to undefined after a terminal is disposed', (done) => {
			disposables.push(window.onDidOpenTerminal(term => {
				try {
					equal(term, terminal);
				} catch (e) {
					done(e);
				}
				disposables.push(window.onDidCloseTerminal(t => {
					try {
						deepEqual(t.exitStatus, { code: undefined });
					} catch (e) {
						done(e);
						return;
					}
					done();
				}));
				terminal.dispose();
			}));
			const terminal = window.createTerminal();
		});

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		// test('onDidChangeActiveTerminal should fire when new terminals are created', (done) => {
		// 	const reg1 = window.onDidChangeActiveTerminal((active: Terminal | undefined) => {
		// 		equal(active, terminal);
		// 		equal(active, window.activeTerminal);
		// 		reg1.dispose();
		// 		const reg2 = window.onDidChangeActiveTerminal((active: Terminal | undefined) => {
		// 			equal(active, undefined);
		// 			equal(active, window.activeTerminal);
		// 			reg2.dispose();
		// 			done();
		// 		});
		// 		terminal.dispose();
		// 	});
		// 	const terminal = window.createTerminal();
		// 	terminal.show();
		// });
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
		// test('onDidChangeTerminalDimensions should fire when new terminals are created', (done) => {
		// 	const reg1 = window.onDidChangeTerminalDimensions(async (event: TerminalDimensionsChangeEvent) => {
		// 		equal(event.terminal, terminal1);
		// 		equal(typeof event.dimensions.columns, 'number');
		// 		equal(typeof event.dimensions.rows, 'number');
		// 		ok(event.dimensions.columns > 0);
		// 		ok(event.dimensions.rows > 0);
		// 		reg1.dispose();
		// 		let terminal2: Terminal;
		// 		const reg2 = window.onDidOpenTerminal((newTerminal) => {
		// 			// This is guarantees to fire before dimensions change event
		// 			if (newTerminal !== terminal1) {
		// 				terminal2 = newTerminal;
		// 				reg2.dispose();
		// 			}
		// 		});
		// 		let firstCalled = false;
		// 		let secondCalled = false;
		// 		const reg3 = window.onDidChangeTerminalDimensions((event: TerminalDimensionsChangeEvent) => {
		// 			if (event.terminal === terminal1) {
		// 				// The original terminal should fire dimension change after a split
		// 				firstCalled = true;
		// 			} else if (event.terminal !== terminal1) {
		// 				// The new split terminal should fire dimension change
		// 				secondCalled = true;
		// 			}
		// 			if (firstCalled && secondCalled) {
		// 				let firstDisposed = false;
		// 				let secondDisposed = false;
		// 				const reg4 = window.onDidCloseTerminal(term => {
		// 					if (term === terminal1) {
		// 						firstDisposed = true;
		// 					}
		// 					if (term === terminal2) {
		// 						secondDisposed = true;
		// 					}
		// 					if (firstDisposed && secondDisposed) {
		// 						reg4.dispose();
		// 						done();
		// 					}
		// 				});
		// 				terminal1.dispose();
		// 				terminal2.dispose();
		// 				reg3.dispose();
		// 			}
		// 		});
		// 		await timeout(500);
		// 		commands.executeCommand('workbench.action.terminal.split');
		// 	});
		// 	const terminal1 = window.createTerminal({ name: 'test' });
		// 	terminal1.show();
		// });
216

217 218 219
		suite('hideFromUser', () => {
			test('should be available to terminals API', done => {
				const terminal = window.createTerminal({ name: 'bg', hideFromUser: true });
220
				disposables.push(window.onDidOpenTerminal(t => {
D
Daniel Imms 已提交
221 222 223 224 225 226 227
					try {
						equal(t, terminal);
						equal(t.name, 'bg');
						ok(window.terminals.indexOf(terminal) !== -1);
					} catch (e) {
						done(e);
					}
228 229
					disposables.push(window.onDidCloseTerminal(() => {
						// reg3.dispose();
230
						done();
231
					}));
232
					terminal.dispose();
233
				}));
234 235
			});
		});
236

237
		suite('window.onDidWriteTerminalData', () => {
238
			test('should listen to all future terminal data events', (done) => {
239 240 241
				const openEvents: string[] = [];
				const dataEvents: { name: string, data: string }[] = [];
				const closeEvents: string[] = [];
242
				disposables.push(window.onDidOpenTerminal(e => openEvents.push(e.name)));
243 244

				let resolveOnceDataWritten: (() => void) | undefined;
E
Eric Amodio 已提交
245
				let resolveOnceClosed: (() => void) | undefined;
246

247
				disposables.push(window.onDidWriteTerminalData(e => {
248 249 250
					dataEvents.push({ name: e.terminal.name, data: e.data });

					resolveOnceDataWritten!();
251
				}));
252

253
				disposables.push(window.onDidCloseTerminal(e => {
254
					closeEvents.push(e.name);
D
Daniel Imms 已提交
255 256 257 258 259 260 261 262 263 264
					try {
						if (closeEvents.length === 1) {
							deepEqual(openEvents, ['test1']);
							deepEqual(dataEvents, [{ name: 'test1', data: 'write1' }]);
							deepEqual(closeEvents, ['test1']);
						} else if (closeEvents.length === 2) {
							deepEqual(openEvents, ['test1', 'test2']);
							deepEqual(dataEvents, [{ name: 'test1', data: 'write1' }, { name: 'test2', data: 'write2' }]);
							deepEqual(closeEvents, ['test1', 'test2']);
						}
E
Eric Amodio 已提交
265
						resolveOnceClosed!();
D
Daniel Imms 已提交
266 267
					} catch (e) {
						done(e);
268
					}
269
				}));
270 271 272

				const term1Write = new EventEmitter<string>();
				const term1Close = new EventEmitter<void>();
E
Eric Amodio 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286
				window.createTerminal({
					name: 'test1', pty: {
						onDidWrite: term1Write.event,
						onDidClose: term1Close.event,
						open: async () => {
							term1Write.fire('write1');

							// Wait until the data is written
							await new Promise(resolve => { resolveOnceDataWritten = resolve; });

							term1Close.fire();

							// Wait until the terminal is closed
							await new Promise<void>(resolve => { resolveOnceClosed = resolve; });
D
Daniel Imms 已提交
287

E
Eric Amodio 已提交
288 289 290 291 292 293 294 295
							const term2Write = new EventEmitter<string>();
							const term2Close = new EventEmitter<void>();
							window.createTerminal({
								name: 'test2', pty: {
									onDidWrite: term2Write.event,
									onDidClose: term2Close.event,
									open: async () => {
										term2Write.fire('write2');
D
Daniel Imms 已提交
296

E
Eric Amodio 已提交
297 298
										// Wait until the data is written
										await new Promise<void>(resolve => { resolveOnceDataWritten = resolve; });
D
Daniel Imms 已提交
299

E
Eric Amodio 已提交
300
										term2Close.fire();
D
Daniel Imms 已提交
301

E
Eric Amodio 已提交
302 303 304 305 306 307 308 309 310 311 312 313
										// Wait until the terminal is closed
										await new Promise<void>(resolve => { resolveOnceClosed = resolve; });

										done();
									},
									close: () => { }
								}
							});
						},
						close: () => { }
					}
				});
314 315 316
			});
		});

D
Daniel Imms 已提交
317
		suite('Extension pty terminals', () => {
318
			test('should fire onDidOpenTerminal and onDidCloseTerminal', (done) => {
319
				disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
320 321 322 323 324
					try {
						equal(term.name, 'c');
					} catch (e) {
						done(e);
					}
325
					disposables.push(window.onDidCloseTerminal(() => done()));
326
					term.dispose();
327
				}));
328
				const pty: Pseudoterminal = {
D
Daniel Imms 已提交
329
					onDidWrite: new EventEmitter<string>().event,
330 331
					open: () => { },
					close: () => { }
332
				};
333
				window.createTerminal({ name: 'c', pty });
334 335
			});

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
			// The below tests depend on global UI state and each other
			// test('should not provide dimensions on start as the terminal has not been shown yet', (done) => {
			// 	const reg1 = window.onDidOpenTerminal(term => {
			// 		equal(terminal, term);
			// 		reg1.dispose();
			// 	});
			// 	const pty: Pseudoterminal = {
			// 		onDidWrite: new EventEmitter<string>().event,
			// 		open: (dimensions) => {
			// 			equal(dimensions, undefined);
			// 			const reg3 = window.onDidCloseTerminal(() => {
			// 				reg3.dispose();
			// 				done();
			// 			});
			// 			// Show a terminal and wait a brief period before dispose, this will cause
			// 			// the panel to init it's dimenisons and be provided to following terminals.
			// 			// The following test depends on this.
			// 			terminal.show();
			// 			setTimeout(() => terminal.dispose(), 200);
			// 		},
			// 		close: () => {}
			// 	};
			// 	const terminal = window.createTerminal({ name: 'foo', pty });
			// });
			// test('should provide dimensions on start as the terminal has been shown', (done) => {
			// 	const reg1 = window.onDidOpenTerminal(term => {
			// 		equal(terminal, term);
			// 		reg1.dispose();
			// 	});
			// 	const pty: Pseudoterminal = {
			// 		onDidWrite: new EventEmitter<string>().event,
			// 		open: (dimensions) => {
			// 			// This test depends on Terminal.show being called some time before such
			// 			// that the panel dimensions are initialized and cached.
			// 			ok(dimensions!.columns > 0);
			// 			ok(dimensions!.rows > 0);
			// 			const reg3 = window.onDidCloseTerminal(() => {
			// 				reg3.dispose();
			// 				done();
			// 			});
			// 			terminal.dispose();
			// 		},
			// 		close: () => {}
			// 	};
			// 	const terminal = window.createTerminal({ name: 'foo', pty });
			// });
D
Daniel Imms 已提交
382

B
Benjamin Pasero 已提交
383 384
			// https://github.com/microsoft/vscode/issues/90437
			test.skip('should respect dimension overrides', (done) => {
385
				disposables.push(window.onDidOpenTerminal(term => {
D
Daniel Imms 已提交
386 387 388 389 390
					try {
						equal(terminal, term);
					} catch (e) {
						done(e);
					}
D
Daniel Imms 已提交
391
					term.show();
392
					disposables.push(window.onDidChangeTerminalDimensions(e => {
393 394 395 396
						if (e.dimensions.columns === 0 || e.dimensions.rows === 0) {
							// HACK: Ignore the event if dimension(s) are zero (#83778)
							return;
						}
D
Daniel Imms 已提交
397 398 399 400 401 402 403
						try {
							equal(e.dimensions.columns, 10);
							equal(e.dimensions.rows, 5);
							equal(e.terminal, terminal);
						} catch (e) {
							done(e);
						}
404
						disposables.push(window.onDidCloseTerminal(() => done()));
D
Daniel Imms 已提交
405
						terminal.dispose();
406 407
					}));
				}));
D
Daniel Imms 已提交
408 409
				const writeEmitter = new EventEmitter<string>();
				const overrideDimensionsEmitter = new EventEmitter<TerminalDimensions>();
410
				const pty: Pseudoterminal = {
411
					onDidWrite: writeEmitter.event,
D
Daniel Imms 已提交
412
					onDidOverrideDimensions: overrideDimensionsEmitter.event,
D
Daniel Imms 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
					open: () => overrideDimensionsEmitter.fire({ columns: 10, rows: 5 }),
					close: () => { }
				};
				const terminal = window.createTerminal({ name: 'foo', pty });
			});

			test('exitStatus.code should be set to the exit code (undefined)', (done) => {
				disposables.push(window.onDidOpenTerminal(term => {
					try {
						equal(terminal, term);
						equal(terminal.exitStatus, undefined);
					} catch (e) {
						done(e);
					}
					disposables.push(window.onDidCloseTerminal(t => {
						try {
							equal(terminal, t);
							deepEqual(terminal.exitStatus, { code: undefined });
						} catch (e) {
							done(e);
							return;
						}
						done();
					}));
				}));
				const writeEmitter = new EventEmitter<string>();
				const closeEmitter = new EventEmitter<number | undefined>();
				const pty: Pseudoterminal = {
					onDidWrite: writeEmitter.event,
					onDidClose: closeEmitter.event,
					open: () => closeEmitter.fire(),
					close: () => { }
				};
				const terminal = window.createTerminal({ name: 'foo', pty });
			});

			test('exitStatus.code should be set to the exit code (zero)', (done) => {
				disposables.push(window.onDidOpenTerminal(term => {
					try {
						equal(terminal, term);
						equal(terminal.exitStatus, undefined);
					} catch (e) {
						done(e);
					}
					disposables.push(window.onDidCloseTerminal(t => {
						try {
							equal(terminal, t);
							deepEqual(terminal.exitStatus, { code: 0 });
						} catch (e) {
							done(e);
							return;
						}
						done();
					}));
				}));
				const writeEmitter = new EventEmitter<string>();
				const closeEmitter = new EventEmitter<number | undefined>();
				const pty: Pseudoterminal = {
					onDidWrite: writeEmitter.event,
					onDidClose: closeEmitter.event,
					open: () => closeEmitter.fire(0),
					close: () => { }
				};
				const terminal = window.createTerminal({ name: 'foo', pty });
			});

			test('exitStatus.code should be set to the exit code (non-zero)', (done) => {
				disposables.push(window.onDidOpenTerminal(term => {
					try {
						equal(terminal, term);
						equal(terminal.exitStatus, undefined);
					} catch (e) {
						done(e);
					}
					disposables.push(window.onDidCloseTerminal(t => {
						try {
							equal(terminal, t);
							deepEqual(terminal.exitStatus, { code: 22 });
						} catch (e) {
							done(e);
							return;
						}
						done();
					}));
				}));
				const writeEmitter = new EventEmitter<string>();
				const closeEmitter = new EventEmitter<number | undefined>();
				const pty: Pseudoterminal = {
					onDidWrite: writeEmitter.event,
					onDidClose: closeEmitter.event,
					open: () => closeEmitter.fire(22),
504
					close: () => { }
D
Daniel Imms 已提交
505
				};
506
				const terminal = window.createTerminal({ name: 'foo', pty });
D
Daniel Imms 已提交
507
			});
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

			test('creationOptions should be set and readonly for ExtensionTerminalOptions terminals', (done) => {
				disposables.push(window.onDidOpenTerminal(term => {
					try {
						equal(terminal, term);
					} catch (e) {
						done(e);
					}
					terminal.dispose();
					disposables.push(window.onDidCloseTerminal(() => done()));
				}));
				const writeEmitter = new EventEmitter<string>();
				const pty: Pseudoterminal = {
					onDidWrite: writeEmitter.event,
					open: () => { },
					close: () => { }
				};
				const options = { name: 'foo', pty };
				const terminal = window.createTerminal(options);
				try {
					equal(terminal.name, 'foo');
					deepEqual(terminal.creationOptions, options);
					throws(() => (<any>terminal.creationOptions).name = 'bad', 'creationOptions should be readonly at runtime');
				} catch (e) {
					done(e);
				}
			});
535
		});
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

		suite('getEnvironmentVariableCollection', () => {
			test('should have collection variables apply to terminals immediately after setting', (done) => {
				// Text to match on before passing the test
				const expectedText = [
					'~a2~',
					'b1~b2~',
					'~c2~c1'
				];
				disposables.push(window.onDidWriteTerminalData(e => {
					try {
						equal(terminal, e.terminal);
					} catch (e) {
						done(e);
					}
					// Multiple expected could show up in the same data event
					while (expectedText.length > 0 && e.data.indexOf(expectedText[0]) >= 0) {
						expectedText.shift();
						// Check if all string are found, if so finish the test
						if (expectedText.length === 0) {
							disposables.push(window.onDidCloseTerminal(() => done()));
							terminal.dispose();
						}
					}
				}));
				const collection = window.getEnvironmentVariableCollection();
				disposables.push(collection);
				collection.replace('A', '~a2~');
				collection.append('B', '~b2~');
				collection.prepend('C', '~c2~');
				const isWindows = process.platform === 'win32';
				const terminal = window.createTerminal({
					shellPath: isWindows ? 'powershell.exe' : 'sh',
					env: {
						A: 'a1',
						B: 'b1',
						C: 'c1'
					}
				});
				terminal.sendText(isWindows ? '$env:A' : 'echo $A');
				terminal.sendText(isWindows ? '$env:B' : 'echo $B');
				terminal.sendText(isWindows ? '$env:C' : 'echo $C');
			});

			test('should have collection variables apply to environment variables that don\'t exist', (done) => {
				// Text to match on before passing the test
				const expectedText = [
					'~a2~',
					'~b2~',
					'~c2~'
				];
				disposables.push(window.onDidWriteTerminalData(e => {
					try {
						equal(terminal, e.terminal);
					} catch (e) {
						done(e);
					}
					// Multiple expected could show up in the same data event
					while (expectedText.length > 0 && e.data.indexOf(expectedText[0]) >= 0) {
						expectedText.shift();
						// Check if all string are found, if so finish the test
						if (expectedText.length === 0) {
							disposables.push(window.onDidCloseTerminal(() => done()));
							terminal.dispose();
						}
					}
				}));
				const collection = window.getEnvironmentVariableCollection();
				disposables.push(collection);
				collection.replace('A', '~a2~');
				collection.append('B', '~b2~');
				collection.prepend('C', '~c2~');
				const isWindows = process.platform === 'win32';
				const terminal = window.createTerminal({
					shellPath: isWindows ? 'powershell.exe' : 'sh',
					env: {
						A: null,
						B: null,
						C: null
					}
				});
				terminal.sendText(isWindows ? '$env:A' : 'echo $A');
				terminal.sendText(isWindows ? '$env:B' : 'echo $B');
				terminal.sendText(isWindows ? '$env:C' : 'echo $C');
			});
		});
622 623
	});
});