-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathterminalManager.unit.test.ts
More file actions
321 lines (271 loc) · 13.5 KB
/
terminalManager.unit.test.ts
File metadata and controls
321 lines (271 loc) · 13.5 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
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as assert from 'assert';
import * as fsapi from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import { Disposable, Event, EventEmitter, Progress, Terminal, TerminalOptions, Uri, WorkspaceConfiguration } from 'vscode';
import { PythonEnvironment } from '../../../api';
import * as windowApis from '../../../common/window.apis';
import * as workspaceApis from '../../../common/workspace.apis';
import * as activationUtils from '../../../features/common/activation';
import * as shellDetector from '../../../features/common/shellDetector';
import {
ShellEnvsProvider,
ShellStartupScriptProvider,
} from '../../../features/terminal/shells/startupProvider';
import {
DidChangeTerminalActivationStateEvent,
TerminalActivationInternal,
} from '../../../features/terminal/terminalActivationState';
import { TerminalManagerImpl } from '../../../features/terminal/terminalManager';
import * as terminalUtils from '../../../features/terminal/utils';
/**
* Test implementation of TerminalActivationInternal that tracks method calls.
*/
class TestTerminalActivation implements TerminalActivationInternal {
public callOrder: string[] = [];
public activateCalls = 0;
public deactivateCalls = 0;
private onDidChangeEmitter = new EventEmitter<DidChangeTerminalActivationStateEvent>();
public onDidChangeTerminalActivationState: Event<DidChangeTerminalActivationStateEvent> =
this.onDidChangeEmitter.event;
isActivated(_terminal: Terminal, _environment?: PythonEnvironment): boolean {
return false;
}
async activate(_terminal: Terminal, _environment: PythonEnvironment): Promise<void> {
this.activateCalls += 1;
this.callOrder.push('activate');
}
async deactivate(_terminal: Terminal): Promise<void> {
this.deactivateCalls += 1;
}
getEnvironment(_terminal: Terminal): PythonEnvironment | undefined {
return undefined;
}
updateActivationState(_terminal: Terminal, _environment: PythonEnvironment, _activated: boolean): void {
// Not used in these tests
}
dispose(): void {
this.onDidChangeEmitter.dispose();
}
}
suite('TerminalManager - create()', () => {
let terminalActivation: TestTerminalActivation;
let mockGetAutoActivationType: sinon.SinonStub;
let terminalManager: TerminalManagerImpl;
let mockTerminal: Partial<Terminal> & { show: sinon.SinonStub };
const createMockEnvironment = (): PythonEnvironment => ({
envId: { id: 'test-env-id', managerId: 'test-manager' },
name: 'Test Environment',
displayName: 'Test Environment',
shortDisplayName: 'TestEnv',
displayPath: '/path/to/env',
version: '3.9.0',
environmentPath: Uri.file('/path/to/python'),
sysPrefix: '/path/to/env',
execInfo: {
run: { executable: '/path/to/python' },
activation: [{ executable: '/path/to/activate' }],
},
});
setup(() => {
terminalActivation = new TestTerminalActivation();
mockTerminal = {
name: 'Test Terminal',
creationOptions: {} as TerminalOptions,
shellIntegration: undefined,
show: sinon.stub().callsFake(() => {
terminalActivation.callOrder.push('show');
}),
sendText: sinon.stub(),
};
mockGetAutoActivationType = sinon.stub(terminalUtils, 'getAutoActivationType');
sinon.stub(terminalUtils, 'waitForShellIntegration').resolves(false);
sinon.stub(activationUtils, 'isActivatableEnvironment').returns(true);
sinon.stub(shellDetector, 'identifyTerminalShell').returns('bash');
sinon.stub(windowApis, 'createTerminal').returns(mockTerminal as Terminal);
sinon.stub(windowApis, 'onDidOpenTerminal').returns(new Disposable(() => {}));
sinon.stub(windowApis, 'onDidCloseTerminal').returns(new Disposable(() => {}));
sinon.stub(windowApis, 'onDidChangeWindowState').returns(new Disposable(() => {}));
sinon.stub(windowApis, 'terminals').returns([]);
sinon.stub(windowApis, 'withProgress').callsFake(async (_options, task) => {
const mockProgress: Progress<{ message?: string; increment?: number }> = { report: () => {} };
const mockCancellationToken = {
isCancellationRequested: false,
onCancellationRequested: () => new Disposable(() => {}),
};
return task(mockProgress, mockCancellationToken as never);
});
sinon.stub(workspaceApis, 'onDidChangeConfiguration').returns(new Disposable(() => {}));
});
teardown(() => {
sinon.restore();
terminalActivation.dispose();
});
function createTerminalManager(): TerminalManagerImpl {
const emptyEnvProviders: ShellEnvsProvider[] = [];
const emptyScriptProviders: ShellStartupScriptProvider[] = [];
return new TerminalManagerImpl(terminalActivation, emptyEnvProviders, emptyScriptProviders);
}
// Regression test for https://github.com/microsoft/vscode-python-environments/issues/640
// With ACT_TYPE_COMMAND, create() awaits activation which blocks returning the terminal.
// Without showing the terminal early, users wouldn't see it until activation completes (2-5 seconds).
test('ACT_TYPE_COMMAND: shows terminal before awaiting activation to prevent hidden terminal during activation', async () => {
mockGetAutoActivationType.returns(terminalUtils.ACT_TYPE_COMMAND);
terminalManager = createTerminalManager();
const env = createMockEnvironment();
await terminalManager.create(env, { cwd: '/workspace' });
const { callOrder } = terminalActivation;
assert.ok(callOrder.includes('show'), 'Terminal show() should be called');
assert.ok(callOrder.includes('activate'), 'Terminal activate() should be called');
const showIndex = callOrder.indexOf('show');
const activateIndex = callOrder.indexOf('activate');
assert.ok(
showIndex < activateIndex,
`show() at index ${showIndex} must precede activate() at index ${activateIndex}`,
);
});
// With ACT_TYPE_SHELL/OFF, create() returns immediately without blocking.
// The caller (runInTerminal) handles showing the terminal, so create() shouldn't call show().
test('ACT_TYPE_SHELL: does not call show() since create() returns immediately and caller handles visibility', async () => {
mockGetAutoActivationType.returns(terminalUtils.ACT_TYPE_SHELL);
terminalManager = createTerminalManager();
const env = createMockEnvironment();
await terminalManager.create(env, { cwd: '/workspace' });
const { callOrder } = terminalActivation;
assert.strictEqual(callOrder.includes('show'), false, 'show() deferred to caller');
assert.strictEqual(callOrder.includes('activate'), false, 'No command activation for shell startup mode');
});
test('ACT_TYPE_OFF: does not call show() since create() returns immediately and caller handles visibility', async () => {
mockGetAutoActivationType.returns(terminalUtils.ACT_TYPE_OFF);
terminalManager = createTerminalManager();
const env = createMockEnvironment();
await terminalManager.create(env, { cwd: '/workspace' });
const { callOrder } = terminalActivation;
assert.strictEqual(callOrder.includes('show'), false, 'show() deferred to caller');
assert.strictEqual(callOrder.includes('activate'), false, 'Activation disabled');
});
test('disableActivation option: skips both show() and activation, returns terminal immediately', async () => {
mockGetAutoActivationType.returns(terminalUtils.ACT_TYPE_COMMAND);
terminalManager = createTerminalManager();
const env = createMockEnvironment();
const terminal = await terminalManager.create(env, { cwd: '/workspace', disableActivation: true });
const { callOrder } = terminalActivation;
assert.ok(terminal, 'Terminal should be returned');
assert.strictEqual(callOrder.includes('show'), false, 'No show() when activation skipped');
assert.strictEqual(terminalActivation.activateCalls, 0, 'No activate() when disableActivation is true');
});
});
suite('TerminalManager - terminal naming', () => {
let terminalActivation: TestTerminalActivation;
let mockGetAutoActivationType: sinon.SinonStub;
let terminalManager: TerminalManagerImpl;
let mockTerminal: Partial<Terminal> & { show: sinon.SinonStub };
let createTerminalStub: sinon.SinonStub;
const createMockEnvironment = (): PythonEnvironment => ({
envId: { id: 'test-env-id', managerId: 'test-manager' },
name: 'Test Environment',
displayName: 'Test Environment',
shortDisplayName: 'TestEnv',
displayPath: '/path/to/env',
version: '3.9.0',
environmentPath: Uri.file('/path/to/python'),
sysPrefix: '/path/to/env',
execInfo: {
run: { executable: '/path/to/python' },
activation: [{ executable: '/path/to/activate' }],
},
});
setup(() => {
terminalActivation = new TestTerminalActivation();
mockTerminal = {
name: 'Test Terminal',
creationOptions: {} as TerminalOptions,
shellIntegration: undefined,
show: sinon.stub(),
sendText: sinon.stub(),
};
mockGetAutoActivationType = sinon.stub(terminalUtils, 'getAutoActivationType');
sinon.stub(terminalUtils, 'waitForShellIntegration').resolves(false);
sinon.stub(activationUtils, 'isActivatableEnvironment').returns(true);
sinon.stub(shellDetector, 'identifyTerminalShell').returns('bash');
createTerminalStub = sinon.stub(windowApis, 'createTerminal').returns(mockTerminal as Terminal);
sinon.stub(windowApis, 'onDidOpenTerminal').returns(new Disposable(() => {}));
sinon.stub(windowApis, 'onDidCloseTerminal').returns(new Disposable(() => {}));
sinon.stub(windowApis, 'onDidChangeWindowState').returns(new Disposable(() => {}));
sinon.stub(windowApis, 'terminals').returns([]);
sinon.stub(windowApis, 'withProgress').callsFake(async (_options, task) => {
const mockProgress: Progress<{ message?: string; increment?: number }> = { report: () => {} };
const mockCancellationToken = {
isCancellationRequested: false,
onCancellationRequested: () => new Disposable(() => {}),
};
return task(mockProgress, mockCancellationToken as never);
});
sinon.stub(workspaceApis, 'onDidChangeConfiguration').returns(new Disposable(() => {}));
});
teardown(() => {
sinon.restore();
terminalActivation.dispose();
});
function createTerminalManager(): TerminalManagerImpl {
const emptyEnvProviders: ShellEnvsProvider[] = [];
const emptyScriptProviders: ShellStartupScriptProvider[] = [];
return new TerminalManagerImpl(terminalActivation, emptyEnvProviders, emptyScriptProviders);
}
test('getDedicatedTerminal sets Python file name as terminal name', async () => {
mockGetAutoActivationType.returns(terminalUtils.ACT_TYPE_OFF);
terminalManager = createTerminalManager();
const env = createMockEnvironment();
const optionsList: TerminalOptions[] = [];
createTerminalStub.callsFake((options) => {
optionsList.push(options);
return mockTerminal as Terminal;
});
const tempRoot = await fsapi.mkdtemp(path.join(os.tmpdir(), 'py-envs-'));
const projectPath = path.join(tempRoot, 'project');
const filePath = path.join(projectPath, 'main.py');
await fsapi.ensureDir(projectPath);
await fsapi.writeFile(filePath, 'print("hello")');
const projectUri = Uri.file(projectPath);
const fileUri = Uri.file(filePath);
const config = { get: sinon.stub().returns(false) } as unknown as WorkspaceConfiguration;
sinon.stub(workspaceApis, 'getConfiguration').returns(config);
try {
await terminalManager.getDedicatedTerminal(fileUri, projectUri, env);
assert.strictEqual(
optionsList[0]?.name,
'Python: main',
'Dedicated terminal should use the file name in the title',
);
} finally {
await fsapi.remove(tempRoot);
}
});
test('getProjectTerminal sets Python as terminal name', async () => {
mockGetAutoActivationType.returns(terminalUtils.ACT_TYPE_OFF);
terminalManager = createTerminalManager();
const env = createMockEnvironment();
const optionsList: TerminalOptions[] = [];
createTerminalStub.callsFake((options) => {
optionsList.push(options);
return mockTerminal as Terminal;
});
const tempRoot = await fsapi.mkdtemp(path.join(os.tmpdir(), 'py-envs-'));
const projectPath = path.join(tempRoot, 'project');
await fsapi.ensureDir(projectPath);
const projectUri = Uri.file(projectPath);
try {
await terminalManager.getProjectTerminal(projectUri, env);
assert.strictEqual(
optionsList[0]?.name,
'Python',
'Project terminal should use the Python title',
);
} finally {
await fsapi.remove(tempRoot);
}
});
});