forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceRegistry.ts
More file actions
192 lines (170 loc) · 8.68 KB
/
serviceRegistry.ts
File metadata and controls
192 lines (170 loc) · 8.68 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Container } from 'inversify';
import { anything, instance, mock, when } from 'ts-mockito';
import * as TypeMoq from 'typemoq';
import { Disposable, Memento } from 'vscode';
import { FileSystem } from '../client/common/platform/fileSystem';
import { PathUtils } from '../client/common/platform/pathUtils';
import { PlatformService, isWindows } from '../client/common/platform/platformService';
import { RegistryImplementation } from '../client/common/platform/registry';
import { registerTypes as platformRegisterTypes } from '../client/common/platform/serviceRegistry';
import { IFileSystem, IPlatformService, IRegistry } from '../client/common/platform/types';
import { ProcessService } from '../client/common/process/proc';
import { PythonExecutionFactory } from '../client/common/process/pythonExecutionFactory';
import { PythonToolExecutionService } from '../client/common/process/pythonToolService';
import { registerTypes as processRegisterTypes } from '../client/common/process/serviceRegistry';
import {
IProcessServiceFactory,
IPythonExecutionFactory,
IPythonToolExecutionService,
} from '../client/common/process/types';
import { registerTypes as commonRegisterTypes } from '../client/common/serviceRegistry';
import {
GLOBAL_MEMENTO,
ICurrentProcess,
IDisposableRegistry,
IMemento,
ILogOutputChannel,
IPathUtils,
IsWindows,
WORKSPACE_MEMENTO,
ITestOutputChannel,
} from '../client/common/types';
import { registerTypes as variableRegisterTypes } from '../client/common/variables/serviceRegistry';
import { EnvironmentActivationService } from '../client/interpreter/activation/service';
import { IEnvironmentActivationService } from '../client/interpreter/activation/types';
import {
IInterpreterAutoSelectionService,
IInterpreterAutoSelectionProxyService,
} from '../client/interpreter/autoSelection/types';
import { IInterpreterService } from '../client/interpreter/contracts';
import { InterpreterService } from '../client/interpreter/interpreterService';
import { registerInterpreterTypes } from '../client/interpreter/serviceRegistry';
import { ServiceContainer } from '../client/ioc/container';
import { ServiceManager } from '../client/ioc/serviceManager';
import { IServiceContainer, IServiceManager } from '../client/ioc/types';
import { registerTypes as unittestsRegisterTypes } from '../client/testing/serviceRegistry';
import { LegacyFileSystem } from './legacyFileSystem';
import { MockOutputChannel } from './mockClasses';
import { MockAutoSelectionService } from './mocks/autoSelector';
import { MockMemento } from './mocks/mementos';
import { MockProcessService } from './mocks/proc';
import { MockProcess } from './mocks/process';
import { registerForIOC } from './pythonEnvironments/legacyIOC';
export class IocContainer {
// This may be set (before any registration happens) to indicate
// whether or not IOC should depend on the VS Code API (e.g. the
// "vscode" module). So in "functional" tests, this should be set
// to "false".
public useVSCodeAPI = true;
public readonly serviceManager: IServiceManager;
public readonly serviceContainer: IServiceContainer;
private disposables: Disposable[] = [];
constructor() {
const cont = new Container({ skipBaseClassChecks: true });
this.serviceManager = new ServiceManager(cont);
this.serviceContainer = new ServiceContainer(cont);
this.serviceManager.addSingletonInstance<IServiceContainer>(IServiceContainer, this.serviceContainer);
this.serviceManager.addSingletonInstance<Disposable[]>(IDisposableRegistry, this.disposables);
this.serviceManager.addSingleton<Memento>(IMemento, MockMemento, GLOBAL_MEMENTO);
this.serviceManager.addSingleton<Memento>(IMemento, MockMemento, WORKSPACE_MEMENTO);
const stdOutputChannel = new MockOutputChannel('Python');
this.disposables.push(stdOutputChannel);
this.serviceManager.addSingletonInstance<ILogOutputChannel>(ILogOutputChannel, stdOutputChannel);
const testOutputChannel = new MockOutputChannel('Python Test - UnitTests');
this.disposables.push(testOutputChannel);
this.serviceManager.addSingletonInstance<ITestOutputChannel>(ITestOutputChannel, testOutputChannel);
this.serviceManager.addSingleton<IInterpreterAutoSelectionService>(
IInterpreterAutoSelectionService,
MockAutoSelectionService,
);
this.serviceManager.addSingleton<IInterpreterAutoSelectionProxyService>(
IInterpreterAutoSelectionProxyService,
MockAutoSelectionService,
);
}
public async dispose(): Promise<void> {
for (const disposable of this.disposables) {
if (disposable) {
const promise = disposable.dispose() as Promise<unknown>;
if (promise) {
await promise;
}
}
}
this.disposables = [];
this.serviceManager.dispose();
}
public registerCommonTypes(registerFileSystem = true): void {
commonRegisterTypes(this.serviceManager);
if (registerFileSystem) {
this.registerFileSystemTypes();
}
}
public registerFileSystemTypes(): void {
this.serviceManager.addSingleton<IPlatformService>(IPlatformService, PlatformService);
this.serviceManager.addSingleton<IFileSystem>(
IFileSystem,
// Maybe use fake vscode.workspace.filesystem API:
this.useVSCodeAPI ? FileSystem : LegacyFileSystem,
);
}
public registerProcessTypes(): void {
processRegisterTypes(this.serviceManager);
const mockEnvironmentActivationService = mock(EnvironmentActivationService);
when(mockEnvironmentActivationService.getActivatedEnvironmentVariables(anything())).thenResolve();
this.serviceManager.addSingletonInstance<IEnvironmentActivationService>(
IEnvironmentActivationService,
instance(mockEnvironmentActivationService),
);
}
public registerVariableTypes(): void {
variableRegisterTypes(this.serviceManager);
}
public registerUnitTestTypes(): void {
unittestsRegisterTypes(this.serviceManager);
}
public registerPlatformTypes(): void {
platformRegisterTypes(this.serviceManager);
}
public registerInterpreterTypes(): void {
// This method registers all interpreter types except `IInterpreterAutoSelectionProxyService` & `IEnvironmentActivationService`, as it's already registered in the constructor & registerMockProcessTypes() respectively
registerInterpreterTypes(this.serviceManager);
}
public registerMockProcessTypes(): void {
const processServiceFactory = TypeMoq.Mock.ofType<IProcessServiceFactory>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const processService = new MockProcessService(new ProcessService(process.env as any));
processServiceFactory.setup((f) => f.create(TypeMoq.It.isAny())).returns(() => Promise.resolve(processService));
this.serviceManager.addSingletonInstance<IProcessServiceFactory>(
IProcessServiceFactory,
processServiceFactory.object,
);
this.serviceManager.addSingleton<IPythonExecutionFactory>(IPythonExecutionFactory, PythonExecutionFactory);
this.serviceManager.addSingleton<IPythonToolExecutionService>(
IPythonToolExecutionService,
PythonToolExecutionService,
);
this.serviceManager.addSingleton<IEnvironmentActivationService>(
IEnvironmentActivationService,
EnvironmentActivationService,
);
const mockEnvironmentActivationService = mock(EnvironmentActivationService);
when(mockEnvironmentActivationService.getActivatedEnvironmentVariables(anything())).thenResolve();
this.serviceManager.rebindInstance<IEnvironmentActivationService>(
IEnvironmentActivationService,
instance(mockEnvironmentActivationService),
);
}
public async registerMockInterpreterTypes(): Promise<void> {
this.serviceManager.addSingleton<IInterpreterService>(IInterpreterService, InterpreterService);
this.serviceManager.addSingleton<IRegistry>(IRegistry, RegistryImplementation);
await registerForIOC(this.serviceManager, this.serviceContainer);
}
public registerMockProcess(): void {
this.serviceManager.addSingletonInstance<boolean>(IsWindows, isWindows());
this.serviceManager.addSingleton<IPathUtils>(IPathUtils, PathUtils);
this.serviceManager.addSingleton<ICurrentProcess>(ICurrentProcess, MockProcess);
}
}