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
197 lines (154 loc) · 8.38 KB
/
pythonStartup.test.ts
File metadata and controls
197 lines (154 loc) · 8.38 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
// 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,
} 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>();
// Question: Why do we have to set up environmentVariableCollection and globalEnvironmentVariableCollection in this flip-flop way?
// Reference: /vscode-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts
context.setup((c) => c.environmentVariableCollection).returns(() => globalEnvironmentVariableCollection.object);
context.setup((c) => c.storageUri).returns(() => Uri.parse('a'));
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 provideTerminalLinks returns links when context.line contains expectedNativeLink', () => {
const provider = new CustomTerminalLinkProvider();
const context: TerminalLinkContext = {
line: 'Some random string with VS Code Native REPL in it',
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('VS Code Native REPL'),
'Expected startIndex to be 0',
);
assert.equal(links[0].length, 'VS Code Native REPL'.length, 'Expected length to be 16');
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');
});
});