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
125 lines (91 loc) · 5.33 KB
/
pythonStartup.test.ts
File metadata and controls
125 lines (91 loc) · 5.33 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
// 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 } from 'vscode';
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis';
import { registerPythonStartup } from '../../../client/terminals/pythonStartup';
import { IExtensionContext } from '../../../client/common/types';
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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).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('REPL.enableShellIntegration')).returns(() => false);
await registerPythonStartup(context.object);
globalEnvironmentVariableCollection.verify((c) => c.delete('PYTHONSTARTUP'), TypeMoq.Times.once());
});
});