forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
341 lines (294 loc) · 12.4 KB
/
common.ts
File metadata and controls
341 lines (294 loc) · 12.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as os from 'os';
import * as TypeMoq from 'typemoq';
import { DiagnosticSeverity, TextDocument, Uri, WorkspaceFolder } from 'vscode';
import { LanguageServerType } from '../../client/activation/types';
import { IApplicationShell, IWorkspaceService } from '../../client/common/application/types';
import { Product } from '../../client/common/installer/productInstaller';
import { ProductNames } from '../../client/common/installer/productNames';
import { IFileSystem, IPlatformService } from '../../client/common/platform/types';
import { IPythonExecutionFactory, IPythonToolExecutionService } from '../../client/common/process/types';
import {
Flake8CategorySeverity,
IConfigurationService,
IInstaller,
IMypyCategorySeverity,
IOutputChannel,
IPycodestyleCategorySeverity,
IPylintCategorySeverity,
IPythonSettings,
} from '../../client/common/types';
import { IServiceContainer } from '../../client/ioc/types';
import { LINTERID_BY_PRODUCT } from '../../client/linters/constants';
import { LinterManager } from '../../client/linters/linterManager';
import { ILinter, ILinterManager, ILintMessage, LinterId } from '../../client/linters/types';
export function newMockDocument(filename: string): TypeMoq.IMock<TextDocument> {
const uri = Uri.file(filename);
const doc = TypeMoq.Mock.ofType<TextDocument>(undefined, TypeMoq.MockBehavior.Strict);
doc.setup((s) => s.uri).returns(() => uri);
return doc;
}
export function linterMessageAsLine(msg: ILintMessage): string {
switch (msg.provider) {
case 'pydocstyle': {
return `<filename>:${msg.line} spam:${os.EOL}\t${msg.code}: ${msg.message}`;
}
default: {
return `${msg.line},${msg.column},${msg.type},${msg.code}:${msg.message}`;
}
}
}
export function getLinterID(product: Product): LinterId {
const linterID = LINTERID_BY_PRODUCT.get(product);
if (!linterID) {
throwUnknownProduct(product);
}
return linterID!;
}
export function getProductName(product: Product, capitalize = true): string {
let prodName = ProductNames.get(product);
if (!prodName) {
prodName = Product[product];
}
if (capitalize) {
return prodName.charAt(0).toUpperCase() + prodName.slice(1);
} else {
return prodName;
}
}
export function throwUnknownProduct(product: Product) {
throw Error(`unsupported product ${Product[product]} (${product})`);
}
export class LintingSettings {
public enabled: boolean;
public cwd?: string;
public ignorePatterns: string[];
public prospectorEnabled: boolean;
public prospectorArgs: string[];
public pylintEnabled: boolean;
public pylintArgs: string[];
public pycodestyleEnabled: boolean;
public pycodestyleArgs: string[];
public pylamaEnabled: boolean;
public pylamaArgs: string[];
public flake8Enabled: boolean;
public flake8Args: string[];
public pydocstyleEnabled: boolean;
public pydocstyleArgs: string[];
public lintOnSave: boolean;
public maxNumberOfProblems: number;
public pylintCategorySeverity: IPylintCategorySeverity;
public pycodestyleCategorySeverity: IPycodestyleCategorySeverity;
public flake8CategorySeverity: Flake8CategorySeverity;
public mypyCategorySeverity: IMypyCategorySeverity;
public prospectorPath: string;
public pylintPath: string;
public pycodestylePath: string;
public pylamaPath: string;
public flake8Path: string;
public pydocstylePath: string;
public mypyEnabled: boolean;
public mypyArgs: string[];
public mypyPath: string;
public banditEnabled: boolean;
public banditArgs: string[];
public banditPath: string;
constructor() {
// mostly from configSettings.ts
this.enabled = true;
this.cwd = undefined;
this.ignorePatterns = [];
this.lintOnSave = false;
this.maxNumberOfProblems = 100;
this.flake8Enabled = false;
this.flake8Path = 'flake8';
this.flake8Args = [];
this.flake8CategorySeverity = {
E: DiagnosticSeverity.Error,
W: DiagnosticSeverity.Warning,
F: DiagnosticSeverity.Warning,
};
this.mypyEnabled = false;
this.mypyPath = 'mypy';
this.mypyArgs = [];
this.mypyCategorySeverity = {
error: DiagnosticSeverity.Error,
note: DiagnosticSeverity.Hint,
};
this.banditEnabled = false;
this.banditPath = 'bandit';
this.banditArgs = [];
this.pycodestyleEnabled = false;
this.pycodestylePath = 'pycodestyle';
this.pycodestyleArgs = [];
this.pycodestyleCategorySeverity = {
E: DiagnosticSeverity.Error,
W: DiagnosticSeverity.Warning,
};
this.pylamaEnabled = false;
this.pylamaPath = 'pylama';
this.pylamaArgs = [];
this.prospectorEnabled = false;
this.prospectorPath = 'prospector';
this.prospectorArgs = [];
this.pydocstyleEnabled = false;
this.pydocstylePath = 'pydocstyle';
this.pydocstyleArgs = [];
this.pylintEnabled = false;
this.pylintPath = 'pylint';
this.pylintArgs = [];
this.pylintCategorySeverity = {
convention: DiagnosticSeverity.Hint,
error: DiagnosticSeverity.Error,
fatal: DiagnosticSeverity.Error,
refactor: DiagnosticSeverity.Hint,
warning: DiagnosticSeverity.Warning,
};
}
}
export class BaseTestFixture {
public serviceContainer: TypeMoq.IMock<IServiceContainer>;
public linterManager: LinterManager;
// services
public workspaceService: TypeMoq.IMock<IWorkspaceService>;
public installer: TypeMoq.IMock<IInstaller>;
public appShell: TypeMoq.IMock<IApplicationShell>;
// config
public configService: TypeMoq.IMock<IConfigurationService>;
public pythonSettings: TypeMoq.IMock<IPythonSettings>;
public lintingSettings: LintingSettings;
// data
public outputChannel: TypeMoq.IMock<IOutputChannel>;
// artifacts
public output: string;
public logged: string[];
constructor(
platformService: IPlatformService,
filesystem: IFileSystem,
pythonToolExecService: IPythonToolExecutionService,
pythonExecFactory: IPythonExecutionFactory,
configService?: TypeMoq.IMock<IConfigurationService>,
serviceContainer?: TypeMoq.IMock<IServiceContainer>,
ignoreConfigUpdates = false,
public readonly workspaceDir = '.',
protected readonly printLogs = false,
) {
this.serviceContainer = serviceContainer
? serviceContainer
: TypeMoq.Mock.ofType<IServiceContainer>(undefined, TypeMoq.MockBehavior.Strict);
// services
this.workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>(undefined, TypeMoq.MockBehavior.Strict);
this.installer = TypeMoq.Mock.ofType<IInstaller>(undefined, TypeMoq.MockBehavior.Strict);
this.appShell = TypeMoq.Mock.ofType<IApplicationShell>(undefined, TypeMoq.MockBehavior.Strict);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IFileSystem), TypeMoq.It.isAny()))
.returns(() => filesystem);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IWorkspaceService), TypeMoq.It.isAny()))
.returns(() => this.workspaceService.object);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IInstaller), TypeMoq.It.isAny()))
.returns(() => this.installer.object);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IPlatformService), TypeMoq.It.isAny()))
.returns(() => platformService);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IPythonToolExecutionService), TypeMoq.It.isAny()))
.returns(() => pythonToolExecService);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IPythonExecutionFactory), TypeMoq.It.isAny()))
.returns(() => pythonExecFactory);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IApplicationShell), TypeMoq.It.isAny()))
.returns(() => this.appShell.object);
this.initServices();
// config
this.configService = configService
? configService
: TypeMoq.Mock.ofType<IConfigurationService>(undefined, TypeMoq.MockBehavior.Strict);
this.pythonSettings = TypeMoq.Mock.ofType<IPythonSettings>(undefined, TypeMoq.MockBehavior.Strict);
this.lintingSettings = new LintingSettings();
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IConfigurationService), TypeMoq.It.isAny()))
.returns(() => this.configService.object);
this.configService.setup((c) => c.getSettings(TypeMoq.It.isAny())).returns(() => this.pythonSettings.object);
this.pythonSettings.setup((s) => s.linting).returns(() => this.lintingSettings);
this.initConfig(ignoreConfigUpdates);
// data
this.outputChannel = TypeMoq.Mock.ofType<IOutputChannel>(undefined, TypeMoq.MockBehavior.Strict);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IOutputChannel), TypeMoq.It.isAny()))
.returns(() => this.outputChannel.object);
this.initData();
// artifacts
this.output = '';
this.logged = [];
// linting
this.linterManager = new LinterManager(this.configService.object);
this.serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(ILinterManager), TypeMoq.It.isAny()))
.returns(() => this.linterManager);
}
public async getLinter(product: Product, enabled = true): Promise<ILinter> {
const info = this.linterManager.getLinterInfo(product);
(this.lintingSettings as any)[info.enabledSettingName] = enabled;
await this.linterManager.setActiveLintersAsync([product]);
await this.linterManager.enableLintingAsync(enabled);
return this.linterManager.createLinter(product, this.outputChannel.object, this.serviceContainer.object);
}
public async getEnabledLinter(product: Product): Promise<ILinter> {
return this.getLinter(product, true);
}
public async getDisabledLinter(product: Product): Promise<ILinter> {
return this.getLinter(product, false);
}
protected newMockDocument(filename: string): TypeMoq.IMock<TextDocument> {
return newMockDocument(filename);
}
private initServices(): void {
const workspaceFolder = TypeMoq.Mock.ofType<WorkspaceFolder>(undefined, TypeMoq.MockBehavior.Strict);
workspaceFolder.setup((f) => f.uri).returns(() => Uri.file(this.workspaceDir));
this.workspaceService
.setup((s) => s.getWorkspaceFolder(TypeMoq.It.isAny()))
.returns(() => workspaceFolder.object);
this.appShell
.setup((a) => a.showErrorMessage(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => Promise.resolve(undefined));
}
private initConfig(ignoreUpdates = false): void {
this.configService
.setup((c) =>
c.updateSetting(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
)
.callback((setting, value) => {
if (ignoreUpdates) {
return;
}
const prefix = 'linting.';
if (setting.startsWith(prefix)) {
(this.lintingSettings as any)[setting.substring(prefix.length)] = value;
}
})
.returns(() => Promise.resolve(undefined));
this.pythonSettings.setup((s) => s.languageServer).returns(() => LanguageServerType.Jedi);
}
private initData(): void {
this.outputChannel
.setup((o) => o.appendLine(TypeMoq.It.isAny()))
.callback((line) => {
if (this.output === '') {
this.output = line;
} else {
this.output = `${this.output}${os.EOL}${line}`;
}
});
this.outputChannel
.setup((o) => o.append(TypeMoq.It.isAny()))
.callback((data) => {
this.output += data;
});
this.outputChannel.setup((o) => o.show());
}
}