forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.test.ts
More file actions
180 lines (171 loc) · 10 KB
/
Copy pathinstaller.test.ts
File metadata and controls
180 lines (171 loc) · 10 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
import * as path from 'path';
import { instance, mock } from 'ts-mockito';
import * as TypeMoq from 'typemoq';
import { ConfigurationTarget, Uri } from 'vscode';
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../../client/common/application/types';
import { WorkspaceService } from '../../client/common/application/workspace';
import { ConfigurationService } from '../../client/common/configuration/service';
import { InstallationChannelManager } from '../../client/common/installer/channelManager';
import { ProductInstaller } from '../../client/common/installer/productInstaller';
import {
CTagsProductPathService,
DataScienceProductPathService,
FormatterProductPathService,
LinterProductPathService,
RefactoringLibraryProductPathService,
TestFrameworkProductPathService
} from '../../client/common/installer/productPath';
import { ProductService } from '../../client/common/installer/productService';
import { IInstallationChannelManager, IModuleInstaller, IProductPathService, IProductService } from '../../client/common/installer/types';
import { Logger } from '../../client/common/logger';
import { PersistentStateFactory } from '../../client/common/persistentState';
import { PathUtils } from '../../client/common/platform/pathUtils';
import { CurrentProcess } from '../../client/common/process/currentProcess';
import { ProcessLogger } from '../../client/common/process/logger';
import { IProcessLogger, IProcessServiceFactory } from '../../client/common/process/types';
import { TerminalHelper } from '../../client/common/terminal/helper';
import { ITerminalHelper } from '../../client/common/terminal/types';
import {
IConfigurationService,
ICurrentProcess,
IInstaller,
ILogger,
IPathUtils,
IPersistentStateFactory,
IsWindows,
ModuleNamePurpose,
Product,
ProductType
} from '../../client/common/types';
import { createDeferred } from '../../client/common/utils/async';
import { getNamesAndValues } from '../../client/common/utils/enum';
import { ICondaService } from '../../client/interpreter/contracts';
import { CondaService } from '../../client/interpreter/locators/services/condaService';
import { InterpreterHashProvider } from '../../client/interpreter/locators/services/hashProvider';
import { InterpeterHashProviderFactory } from '../../client/interpreter/locators/services/hashProviderFactory';
import { InterpreterFilter } from '../../client/interpreter/locators/services/interpreterFilter';
import { WindowsStoreInterpreter } from '../../client/interpreter/locators/services/windowsStoreInterpreter';
import { rootWorkspaceUri, updateSetting } from '../common';
import { MockModuleInstaller } from '../mocks/moduleInstaller';
import { MockProcessService } from '../mocks/proc';
import { UnitTestIocContainer } from '../testing/serviceRegistry';
import { closeActiveWindows, initializeTest, IS_MULTI_ROOT_TEST } from './../initialize';
// tslint:disable-next-line:max-func-body-length
suite('Installer', () => {
let ioc: UnitTestIocContainer;
const workspaceUri = Uri.file(path.join(__dirname, '..', '..', '..', 'src', 'test'));
const resource = IS_MULTI_ROOT_TEST ? workspaceUri : undefined;
suiteSetup(initializeTest);
setup(async () => {
await initializeTest();
await resetSettings();
initializeDI();
});
suiteTeardown(async () => {
await closeActiveWindows();
await resetSettings();
});
teardown(async () => {
await ioc.dispose();
await closeActiveWindows();
});
function initializeDI() {
ioc = new UnitTestIocContainer();
ioc.registerUnitTestTypes();
ioc.registerFileSystemTypes();
ioc.registerVariableTypes();
ioc.registerLinterTypes();
ioc.registerFormatterTypes();
ioc.serviceManager.addSingleton<IPersistentStateFactory>(IPersistentStateFactory, PersistentStateFactory);
ioc.serviceManager.addSingleton<ILogger>(ILogger, Logger);
ioc.serviceManager.addSingleton<IInstaller>(IInstaller, ProductInstaller);
ioc.serviceManager.addSingleton<IPathUtils>(IPathUtils, PathUtils);
ioc.serviceManager.addSingleton<IProcessLogger>(IProcessLogger, ProcessLogger);
ioc.serviceManager.addSingleton<ICurrentProcess>(ICurrentProcess, CurrentProcess);
ioc.serviceManager.addSingleton<IInstallationChannelManager>(IInstallationChannelManager, InstallationChannelManager);
ioc.serviceManager.addSingletonInstance<ICommandManager>(ICommandManager, TypeMoq.Mock.ofType<ICommandManager>().object);
ioc.serviceManager.addSingletonInstance<IApplicationShell>(IApplicationShell, TypeMoq.Mock.ofType<IApplicationShell>().object);
ioc.serviceManager.addSingleton<IConfigurationService>(IConfigurationService, ConfigurationService);
ioc.serviceManager.addSingleton<IWorkspaceService>(IWorkspaceService, WorkspaceService);
ioc.serviceManager.addSingleton<ICondaService>(ICondaService, CondaService);
ioc.registerMockInterpreterTypes();
ioc.registerMockProcessTypes();
ioc.serviceManager.addSingletonInstance<boolean>(IsWindows, false);
ioc.serviceManager.addSingletonInstance<IProductService>(IProductService, new ProductService());
ioc.serviceManager.addSingleton<IProductPathService>(IProductPathService, CTagsProductPathService, ProductType.WorkspaceSymbols);
ioc.serviceManager.addSingleton<IProductPathService>(IProductPathService, FormatterProductPathService, ProductType.Formatter);
ioc.serviceManager.addSingleton<IProductPathService>(IProductPathService, LinterProductPathService, ProductType.Linter);
ioc.serviceManager.addSingleton<IProductPathService>(IProductPathService, TestFrameworkProductPathService, ProductType.TestFramework);
ioc.serviceManager.addSingleton<IProductPathService>(IProductPathService, RefactoringLibraryProductPathService, ProductType.RefactoringLibrary);
ioc.serviceManager.addSingleton<IProductPathService>(IProductPathService, DataScienceProductPathService, ProductType.DataScience);
ioc.serviceManager.addSingleton<WindowsStoreInterpreter>(WindowsStoreInterpreter, WindowsStoreInterpreter);
ioc.serviceManager.addSingleton<InterpreterHashProvider>(InterpreterHashProvider, InterpreterHashProvider);
ioc.serviceManager.addSingleton<InterpeterHashProviderFactory>(InterpeterHashProviderFactory, InterpeterHashProviderFactory);
ioc.serviceManager.addSingleton<InterpreterFilter>(InterpreterFilter, InterpreterFilter);
}
async function resetSettings() {
await updateSetting('linting.pylintEnabled', true, rootWorkspaceUri, ConfigurationTarget.Workspace);
}
async function testCheckingIfProductIsInstalled(product: Product) {
const installer = ioc.serviceContainer.get<IInstaller>(IInstaller);
const processService = (await ioc.serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory).create()) as MockProcessService;
const checkInstalledDef = createDeferred<boolean>();
processService.onExec((_file, args, _options, callback) => {
const moduleName = installer.translateProductToModuleName(product, ModuleNamePurpose.run);
if (args.length > 1 && args[0] === '-c' && args[1] === `import ${moduleName}`) {
checkInstalledDef.resolve(true);
}
callback({ stdout: '' });
});
await installer.isInstalled(product, resource);
await checkInstalledDef.promise;
}
getNamesAndValues<Product>(Product).forEach(prod => {
test(`Ensure isInstalled for Product: '${prod.name}' executes the right command`, async () => {
ioc.serviceManager.addSingletonInstance<IModuleInstaller>(IModuleInstaller, new MockModuleInstaller('one', false));
ioc.serviceManager.addSingletonInstance<IModuleInstaller>(IModuleInstaller, new MockModuleInstaller('two', true));
ioc.serviceManager.addSingletonInstance<ITerminalHelper>(ITerminalHelper, instance(mock(TerminalHelper)));
if (
prod.value === Product.ctags ||
prod.value === Product.unittest ||
prod.value === Product.isort ||
prod.value === Product.jupyter ||
prod.value === Product.ipykernel
) {
return;
}
await testCheckingIfProductIsInstalled(prod.value);
});
});
async function testInstallingProduct(product: Product) {
const installer = ioc.serviceContainer.get<IInstaller>(IInstaller);
const checkInstalledDef = createDeferred<boolean>();
const moduleInstallers = ioc.serviceContainer.getAll<MockModuleInstaller>(IModuleInstaller);
const moduleInstallerOne = moduleInstallers.find(item => item.displayName === 'two')!;
moduleInstallerOne.on('installModule', moduleName => {
const installName = installer.translateProductToModuleName(product, ModuleNamePurpose.install);
if (installName === moduleName) {
checkInstalledDef.resolve();
}
});
await installer.install(product);
await checkInstalledDef.promise;
}
getNamesAndValues<Product>(Product).forEach(prod => {
test(`Ensure install for Product: '${prod.name}' executes the right command in IModuleInstaller`, async () => {
ioc.serviceManager.addSingletonInstance<IModuleInstaller>(IModuleInstaller, new MockModuleInstaller('one', false));
ioc.serviceManager.addSingletonInstance<IModuleInstaller>(IModuleInstaller, new MockModuleInstaller('two', true));
ioc.serviceManager.addSingletonInstance<ITerminalHelper>(ITerminalHelper, instance(mock(TerminalHelper)));
if (
prod.value === Product.unittest ||
prod.value === Product.ctags ||
prod.value === Product.isort ||
prod.value === Product.jupyter ||
prod.value === Product.ipykernel
) {
return;
}
await testInstallingProduct(prod.value);
});
});
});