forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint.multiroot.test.ts
More file actions
146 lines (135 loc) · 6.42 KB
/
lint.multiroot.test.ts
File metadata and controls
146 lines (135 loc) · 6.42 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
import * as assert from 'assert';
import * as path from 'path';
import { CancellationTokenSource, ConfigurationTarget, OutputChannel, Uri, workspace } from 'vscode';
import { PythonSettings } from '../../client/common/configSettings';
import {
CTagsProductPathService,
DataScienceProductPathService,
FormatterProductPathService,
LinterProductPathService,
RefactoringLibraryProductPathService,
TestFrameworkProductPathService
} from '../../client/common/installer/productPath';
import { ProductService } from '../../client/common/installer/productService';
import { IProductPathService, IProductService } from '../../client/common/installer/types';
import { IConfigurationService, IOutputChannel, Product, ProductType } from '../../client/common/types';
import { ICondaService } from '../../client/interpreter/contracts';
import { CondaService } from '../../client/interpreter/locators/services/condaService';
import { ILinter, ILinterManager } from '../../client/linters/types';
import { TEST_OUTPUT_CHANNEL } from '../../client/testing/common/constants';
import { TEST_TIMEOUT } from '../constants';
import { closeActiveWindows, initialize, initializeTest, IS_MULTI_ROOT_TEST } from '../initialize';
import { UnitTestIocContainer } from '../testing/serviceRegistry';
// tslint:disable:max-func-body-length no-invalid-this
const multirootPath = path.join(__dirname, '..', '..', '..', 'src', 'testMultiRootWkspc');
suite('Multiroot Linting', () => {
const pylintSetting = 'linting.pylintEnabled';
const flake8Setting = 'linting.flake8Enabled';
let ioc: UnitTestIocContainer;
suiteSetup(function() {
if (!IS_MULTI_ROOT_TEST) {
this.skip();
}
return initialize();
});
setup(async () => {
initializeDI();
await initializeTest();
});
suiteTeardown(closeActiveWindows);
teardown(async () => {
await ioc.dispose();
await closeActiveWindows();
PythonSettings.dispose();
});
function initializeDI() {
ioc = new UnitTestIocContainer();
ioc.registerCommonTypes(false);
ioc.registerProcessTypes();
ioc.registerLinterTypes();
ioc.registerVariableTypes();
ioc.registerFileSystemTypes();
ioc.registerMockInterpreterTypes();
ioc.serviceManager.addSingletonInstance<IProductService>(IProductService, new ProductService());
ioc.serviceManager.addSingleton<ICondaService>(ICondaService, CondaService);
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
);
}
async function createLinter(product: Product): Promise<ILinter> {
const mockOutputChannel = ioc.serviceContainer.get<OutputChannel>(IOutputChannel, TEST_OUTPUT_CHANNEL);
const lm = ioc.serviceContainer.get<ILinterManager>(ILinterManager);
return lm.createLinter(product, mockOutputChannel, ioc.serviceContainer);
}
async function testLinterInWorkspaceFolder(
product: Product,
workspaceFolderRelativePath: string,
mustHaveErrors: boolean
): Promise<void> {
const fileToLint = path.join(multirootPath, workspaceFolderRelativePath, 'file.py');
const cancelToken = new CancellationTokenSource();
const document = await workspace.openTextDocument(fileToLint);
const linter = await createLinter(product);
const messages = await linter.lint(document, cancelToken.token);
const errorMessage = mustHaveErrors ? 'No errors returned by linter' : 'Errors returned by linter';
assert.equal(messages.length > 0, mustHaveErrors, errorMessage);
}
test('Enabling Pylint in root and also in Workspace, should return errors', async () => {
await runTest(Product.pylint, true, true, pylintSetting);
}).timeout(TEST_TIMEOUT * 2);
test('Enabling Pylint in root and disabling in Workspace, should not return errors', async () => {
await runTest(Product.pylint, true, false, pylintSetting);
}).timeout(TEST_TIMEOUT * 2);
test('Disabling Pylint in root and enabling in Workspace, should return errors', async () => {
await runTest(Product.pylint, false, true, pylintSetting);
}).timeout(TEST_TIMEOUT * 2);
test('Enabling Flake8 in root and also in Workspace, should return errors', async () => {
await runTest(Product.flake8, true, true, flake8Setting);
});
test('Enabling Flake8 in root and disabling in Workspace, should not return errors', async () => {
await runTest(Product.flake8, true, false, flake8Setting);
});
test('Disabling Flake8 in root and enabling in Workspace, should return errors', async () => {
await runTest(Product.flake8, false, true, flake8Setting);
});
async function runTest(product: Product, global: boolean, wks: boolean, setting: string): Promise<void> {
const config = ioc.serviceContainer.get<IConfigurationService>(IConfigurationService);
await Promise.all([
config.updateSetting(setting, global, Uri.file(multirootPath), ConfigurationTarget.Global),
config.updateSetting(setting, wks, Uri.file(multirootPath), ConfigurationTarget.Workspace)
]);
await testLinterInWorkspaceFolder(product, 'workspace1', wks);
await Promise.all(
[ConfigurationTarget.Global, ConfigurationTarget.Workspace].map(configTarget =>
config.updateSetting(setting, undefined, Uri.file(multirootPath), configTarget)
)
);
}
});