forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonPathUpdater.test.ts
More file actions
131 lines (120 loc) · 8.67 KB
/
pythonPathUpdater.test.ts
File metadata and controls
131 lines (120 loc) · 8.67 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
import * as path from 'path';
import * as TypeMoq from 'typemoq';
import { ConfigurationTarget, Uri, WorkspaceConfiguration } from 'vscode';
import { IWorkspaceService } from '../../client/common/application/types';
import { PythonPathUpdaterServiceFactory } from '../../client/interpreter/configuration/pythonPathUpdaterServiceFactory';
import { IPythonPathUpdaterServiceFactory } from '../../client/interpreter/configuration/types';
import { IServiceContainer } from '../../client/ioc/types';
// tslint:disable:no-invalid-template-strings max-func-body-length
suite('Python Path Settings Updater', () => {
let serviceContainer: TypeMoq.IMock<IServiceContainer>;
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
let updaterServiceFactory: IPythonPathUpdaterServiceFactory;
function setupMocks() {
serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IWorkspaceService))).returns(() => workspaceService.object);
updaterServiceFactory = new PythonPathUpdaterServiceFactory(serviceContainer.object);
}
function setupConfigProvider(resource?: Uri): TypeMoq.IMock<WorkspaceConfiguration> {
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
workspaceService.setup(w => w.getConfiguration(TypeMoq.It.isValue('python'), TypeMoq.It.isValue(resource))).returns(() => workspaceConfig.object);
return workspaceConfig;
}
suite('Global', () => {
setup(setupMocks);
test('Python Path should not be updated when current pythonPath is the same', async () => {
const updater = updaterServiceFactory.getGlobalPythonPathConfigurationService();
const pythonPath = `xGlobalPythonPath${new Date().getMilliseconds()}`;
const workspaceConfig = setupConfigProvider();
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => {
// tslint:disable-next-line:no-any
return { globalValue: pythonPath } as any;
});
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.never());
});
test('Python Path should be updated when current pythonPath is different', async () => {
const updater = updaterServiceFactory.getGlobalPythonPathConfigurationService();
const pythonPath = `xGlobalPythonPath${new Date().getMilliseconds()}`;
const workspaceConfig = setupConfigProvider();
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => undefined);
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isValue('pythonPath'), TypeMoq.It.isValue(pythonPath), TypeMoq.It.isValue(true)), TypeMoq.Times.once());
});
});
suite('WorkspaceFolder', () => {
setup(setupMocks);
test('Python Path should not be updated when current pythonPath is the same', async () => {
const workspaceFolderPath = path.join('user', 'desktop', 'development');
const workspaceFolder = Uri.file(workspaceFolderPath);
const updater = updaterServiceFactory.getWorkspaceFolderPythonPathConfigurationService(workspaceFolder);
const pythonPath = `xWorkspaceFolderPythonPath${new Date().getMilliseconds()}`;
const workspaceConfig = setupConfigProvider(workspaceFolder);
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => {
// tslint:disable-next-line:no-any
return { workspaceFolderValue: pythonPath } as any;
});
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.never());
});
test('Python Path should be updated when current pythonPath is different', async () => {
const workspaceFolderPath = path.join('user', 'desktop', 'development');
const workspaceFolder = Uri.file(workspaceFolderPath);
const updater = updaterServiceFactory.getWorkspaceFolderPythonPathConfigurationService(workspaceFolder);
const pythonPath = `xWorkspaceFolderPythonPath${new Date().getMilliseconds()}`;
const workspaceConfig = setupConfigProvider(workspaceFolder);
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => undefined);
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isValue('pythonPath'), TypeMoq.It.isValue(pythonPath), TypeMoq.It.isValue(ConfigurationTarget.WorkspaceFolder)), TypeMoq.Times.once());
});
test('Python Path should be truncated for worspace-relative paths', async () => {
const workspaceFolderPath = path.join('user', 'desktop', 'development');
const workspaceFolder = Uri.file(workspaceFolderPath);
const updater = updaterServiceFactory.getWorkspaceFolderPythonPathConfigurationService(workspaceFolder);
const pythonPath = Uri.file(path.join(workspaceFolderPath, 'env', 'bin', 'python')).fsPath;
const expectedPythonPath = path.join('env', 'bin', 'python');
const workspaceConfig = setupConfigProvider(workspaceFolder);
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => undefined);
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isValue('pythonPath'), TypeMoq.It.isValue(expectedPythonPath), TypeMoq.It.isValue(ConfigurationTarget.WorkspaceFolder)), TypeMoq.Times.once());
});
});
suite('Workspace (multiroot scenario)', () => {
setup(setupMocks);
test('Python Path should not be updated when current pythonPath is the same', async () => {
const workspaceFolderPath = path.join('user', 'desktop', 'development');
const workspaceFolder = Uri.file(workspaceFolderPath);
const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder);
const pythonPath = `xWorkspaceFolderPythonPath${new Date().getMilliseconds()}`;
const workspaceConfig = setupConfigProvider(workspaceFolder);
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => {
// tslint:disable-next-line:no-any
return { workspaceValue: pythonPath } as any;
});
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.never());
});
test('Python Path should be updated when current pythonPath is different', async () => {
const workspaceFolderPath = path.join('user', 'desktop', 'development');
const workspaceFolder = Uri.file(workspaceFolderPath);
const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder);
const pythonPath = `xWorkspaceFolderPythonPath${new Date().getMilliseconds()}`;
const workspaceConfig = setupConfigProvider(workspaceFolder);
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => undefined);
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isValue('pythonPath'), TypeMoq.It.isValue(pythonPath), TypeMoq.It.isValue(false)), TypeMoq.Times.once());
});
test('Python Path should be truncated for workspace-relative paths', async () => {
const workspaceFolderPath = path.join('user', 'desktop', 'development');
const workspaceFolder = Uri.file(workspaceFolderPath);
const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder);
const pythonPath = Uri.file(path.join(workspaceFolderPath, 'env', 'bin', 'python')).fsPath;
const expectedPythonPath = path.join('env', 'bin', 'python');
const workspaceConfig = setupConfigProvider(workspaceFolder);
workspaceConfig.setup(w => w.inspect(TypeMoq.It.isValue('pythonPath'))).returns(() => undefined);
await updater.updatePythonPath(pythonPath);
workspaceConfig.verify(w => w.update(TypeMoq.It.isValue('pythonPath'), TypeMoq.It.isValue(expectedPythonPath), TypeMoq.It.isValue(false)), TypeMoq.Times.once());
});
});
});