forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpythonStartup.test.ts
More file actions
263 lines (214 loc) · 10.9 KB
/
pythonStartup.test.ts
File metadata and controls
263 lines (214 loc) · 10.9 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as sinon from 'sinon';
import * as TypeMoq from 'typemoq';
import {
GlobalEnvironmentVariableCollection,
Uri,
WorkspaceConfiguration,
Disposable,
CancellationToken,
TerminalLinkContext,
Terminal,
EventEmitter,
workspace,
} from 'vscode';
import { assert } from 'chai';
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis';
import { registerPythonStartup } from '../../../client/terminals/pythonStartup';
import { IExtensionContext } from '../../../client/common/types';
import * as pythonStartupLinkProvider from '../../../client/terminals/pythonStartupLinkProvider';
import { CustomTerminalLinkProvider } from '../../../client/terminals/pythonStartupLinkProvider';
import { Repl } from '../../../client/common/utils/localize';
suite('Terminal - Shell Integration with PYTHONSTARTUP', () => {
let getConfigurationStub: sinon.SinonStub;
let pythonConfig: TypeMoq.IMock<WorkspaceConfiguration>;
let editorConfig: TypeMoq.IMock<WorkspaceConfiguration>;
let context: TypeMoq.IMock<IExtensionContext>;
let createDirectoryStub: sinon.SinonStub;
let copyStub: sinon.SinonStub;
let globalEnvironmentVariableCollection: TypeMoq.IMock<GlobalEnvironmentVariableCollection>;
setup(() => {
context = TypeMoq.Mock.ofType<IExtensionContext>();
globalEnvironmentVariableCollection = TypeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>();
context.setup((c) => c.environmentVariableCollection).returns(() => globalEnvironmentVariableCollection.object);
context.setup((c) => c.storageUri).returns(() => Uri.parse('a'));
context.setup((c) => c.subscriptions).returns(() => []);
globalEnvironmentVariableCollection
.setup((c) => c.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => Promise.resolve());
globalEnvironmentVariableCollection.setup((c) => c.delete(TypeMoq.It.isAny())).returns(() => Promise.resolve());
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
createDirectoryStub = sinon.stub(workspaceApis, 'createDirectory');
copyStub = sinon.stub(workspaceApis, 'copy');
pythonConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
editorConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
getConfigurationStub.callsFake((section: string) => {
if (section === 'python') {
return pythonConfig.object;
}
return editorConfig.object;
});
createDirectoryStub.callsFake((_) => Promise.resolve());
copyStub.callsFake((_, __, ___) => Promise.resolve());
});
teardown(() => {
sinon.restore();
});
test('Verify createDirectory is called when shell integration is enabled', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => true);
await registerPythonStartup(context.object);
sinon.assert.calledOnce(createDirectoryStub);
});
test('Verify createDirectory is not called when shell integration is disabled', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => false);
await registerPythonStartup(context.object);
sinon.assert.notCalled(createDirectoryStub);
});
test('Verify copy is called when shell integration is enabled', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => true);
await registerPythonStartup(context.object);
sinon.assert.calledOnce(copyStub);
});
test('Verify copy is not called when shell integration is disabled', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => false);
await registerPythonStartup(context.object);
sinon.assert.notCalled(copyStub);
});
test('PYTHONSTARTUP is set when enableShellIntegration setting is true', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => true);
await registerPythonStartup(context.object);
globalEnvironmentVariableCollection.verify(
(c) => c.replace('PYTHONSTARTUP', TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.once(),
);
});
test('environmentCollection should not remove PYTHONSTARTUP when enableShellIntegration setting is true', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => true);
await registerPythonStartup(context.object);
globalEnvironmentVariableCollection.verify((c) => c.delete('PYTHONSTARTUP'), TypeMoq.Times.never());
});
test('PYTHONSTARTUP is not set when enableShellIntegration setting is false', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => false);
await registerPythonStartup(context.object);
globalEnvironmentVariableCollection.verify(
(c) => c.replace('PYTHONSTARTUP', TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
test('PYTHONSTARTUP is deleted when enableShellIntegration setting is false', async () => {
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => false);
await registerPythonStartup(context.object);
globalEnvironmentVariableCollection.verify((c) => c.delete('PYTHONSTARTUP'), TypeMoq.Times.once());
});
test('Ensure registering terminal link calls registerTerminalLinkProvider', async () => {
const registerTerminalLinkProviderStub = sinon.stub(
pythonStartupLinkProvider,
'registerCustomTerminalLinkProvider',
);
const disposableArray: Disposable[] = [];
pythonStartupLinkProvider.registerCustomTerminalLinkProvider(disposableArray);
sinon.assert.calledOnce(registerTerminalLinkProviderStub);
sinon.assert.calledWith(registerTerminalLinkProviderStub, disposableArray);
registerTerminalLinkProviderStub.restore();
});
test('Verify onDidChangeConfiguration is called when configuration changes', async () => {
const onDidChangeConfigurationSpy = sinon.spy(workspace, 'onDidChangeConfiguration');
pythonConfig.setup((p) => p.get('terminal.shellIntegration.enabled')).returns(() => true);
await registerPythonStartup(context.object);
assert.isTrue(onDidChangeConfigurationSpy.calledOnce);
onDidChangeConfigurationSpy.restore();
});
if (process.platform === 'darwin') {
test('Mac - Verify provideTerminalLinks returns links when context.line contains expectedNativeLink', () => {
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with Cmd click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};
const links = provider.provideTerminalLinks(context, token);
assert.isNotNull(links, 'Expected links to be not undefined');
assert.isArray(links, 'Expected links to be an array');
assert.isNotEmpty(links, 'Expected links to be not empty');
if (Array.isArray(links)) {
assert.equal(
links[0].command,
'python.startNativeREPL',
'Expected command to be python.startNativeREPL',
);
assert.equal(
links[0].startIndex,
context.line.indexOf('Cmd click to launch VS Code Native REPL'),
'start index should match',
);
assert.equal(
links[0].length,
'Cmd click to launch VS Code Native REPL'.length,
'Match expected length',
);
assert.equal(
links[0].tooltip,
Repl.launchNativeRepl,
'Expected tooltip to be Launch VS Code Native REPL',
);
}
});
}
if (process.platform !== 'darwin') {
test('Windows/Linux - Verify provideTerminalLinks returns links when context.line contains expectedNativeLink', () => {
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with Ctrl click to launch VS Code Native REPL',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};
const links = provider.provideTerminalLinks(context, token);
assert.isNotNull(links, 'Expected links to be not undefined');
assert.isArray(links, 'Expected links to be an array');
assert.isNotEmpty(links, 'Expected links to be not empty');
if (Array.isArray(links)) {
assert.equal(
links[0].command,
'python.startNativeREPL',
'Expected command to be python.startNativeREPL',
);
assert.equal(
links[0].startIndex,
context.line.indexOf('Ctrl click to launch VS Code Native REPL'),
'start index should match',
);
assert.equal(
links[0].length,
'Ctrl click to launch VS Code Native REPL'.length,
'Match expected Length',
);
assert.equal(
links[0].tooltip,
Repl.launchNativeRepl,
'Expected tooltip to be Launch VS Code Native REPL',
);
}
});
}
test('Verify provideTerminalLinks returns no links when context.line does not contain expectedNativeLink', () => {
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string without the expected link',
terminal: {} as Terminal,
};
const token: CancellationToken = {
isCancellationRequested: false,
onCancellationRequested: new EventEmitter<unknown>().event,
};
const links = provider.provideTerminalLinks(context, token);
assert.isArray(links, 'Expected links to be an array');
assert.isEmpty(links, 'Expected links to be empty');
});
});