forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint.functional.test.ts
More file actions
392 lines (363 loc) · 21.9 KB
/
Copy pathlint.functional.test.ts
File metadata and controls
392 lines (363 loc) · 21.9 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as assert from 'assert';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as TypeMoq from 'typemoq';
import {
CancellationTokenSource,
TextDocument,
TextLine,
Uri
} from 'vscode';
import { Product } from '../../client/common/installer/productInstaller';
import { FileSystem } from '../../client/common/platform/fileSystem';
import { PlatformService } from '../../client/common/platform/platformService';
import { BufferDecoder } from '../../client/common/process/decoder';
import { ProcessServiceFactory } from '../../client/common/process/processFactory';
import { PythonExecutionFactory } from '../../client/common/process/pythonExecutionFactory';
import { PythonToolExecutionService } from '../../client/common/process/pythonToolService';
import {
IBufferDecoder,
IProcessLogger,
IPythonExecutionFactory,
IPythonToolExecutionService
} from '../../client/common/process/types';
import {
IConfigurationService, IDisposableRegistry
} from '../../client/common/types';
import {
IEnvironmentVariablesProvider
} from '../../client/common/variables/types';
import {
IEnvironmentActivationService
} from '../../client/interpreter/activation/types';
import { IServiceContainer } from '../../client/ioc/types';
import { LINTERID_BY_PRODUCT } from '../../client/linters/constants';
import {
ILintMessage,
LinterId,
LintMessageSeverity
} from '../../client/linters/types';
import { deleteFile, PYTHON_PATH } from '../common';
import {
BaseTestFixture,
getLinterID,
getProductName,
newMockDocument,
throwUnknownProduct
} from './common';
const workspaceDir = path.join(__dirname, '..', '..', '..', 'src', 'test');
const workspaceUri = Uri.file(workspaceDir);
const pythonFilesDir = path.join(workspaceDir, 'pythonFiles', 'linting');
const fileToLint = path.join(pythonFilesDir, 'file.py');
const linterConfigDirs = new Map<LinterId, string>([
['flake8', path.join(pythonFilesDir, 'flake8config')],
['pycodestyle', path.join(pythonFilesDir, 'pycodestyleconfig')],
['pydocstyle', path.join(pythonFilesDir, 'pydocstyleconfig27')],
['pylint', path.join(pythonFilesDir, 'pylintconfig')]
]);
const linterConfigRCFiles = new Map<LinterId, string>([
['pylint', '.pylintrc'],
['pydocstyle', '.pydocstyle']
]);
const pylintMessagesToBeReturned: ILintMessage[] = [
{ line: 24, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 30, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 34, column: 0, severity: LintMessageSeverity.Information, code: 'I0012', message: 'Locally enabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 40, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 44, column: 0, severity: LintMessageSeverity.Information, code: 'I0012', message: 'Locally enabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 55, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 59, column: 0, severity: LintMessageSeverity.Information, code: 'I0012', message: 'Locally enabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 62, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling undefined-variable (E0602)', provider: '', type: 'warning' },
{ line: 70, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 84, column: 0, severity: LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', provider: '', type: 'warning' },
{ line: 87, column: 0, severity: LintMessageSeverity.Hint, code: 'C0304', message: 'Final newline missing', provider: '', type: 'warning' },
{ line: 11, column: 20, severity: LintMessageSeverity.Warning, code: 'W0613', message: 'Unused argument \'arg\'', provider: '', type: 'warning' },
{ line: 26, column: 14, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blop\' member', provider: '', type: 'warning' },
{ line: 36, column: 14, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' },
{ line: 46, column: 18, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' },
{ line: 61, column: 18, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' },
{ line: 72, column: 18, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' },
{ line: 75, column: 18, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' },
{ line: 77, column: 14, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' },
{ line: 83, column: 14, severity: LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', provider: '', type: 'warning' }
];
const flake8MessagesToBeReturned: ILintMessage[] = [
{ line: 5, column: 1, severity: LintMessageSeverity.Error, code: 'E302', message: 'expected 2 blank lines, found 1', provider: '', type: 'E' },
{ line: 19, column: 15, severity: LintMessageSeverity.Error, code: 'E127', message: 'continuation line over-indented for visual indent', provider: '', type: 'E' },
{ line: 24, column: 23, severity: LintMessageSeverity.Error, code: 'E261', message: 'at least two spaces before inline comment', provider: '', type: 'E' },
{ line: 62, column: 30, severity: LintMessageSeverity.Error, code: 'E261', message: 'at least two spaces before inline comment', provider: '', type: 'E' },
{ line: 70, column: 22, severity: LintMessageSeverity.Error, code: 'E261', message: 'at least two spaces before inline comment', provider: '', type: 'E' },
{ line: 80, column: 5, severity: LintMessageSeverity.Error, code: 'E303', message: 'too many blank lines (2)', provider: '', type: 'E' },
{ line: 87, column: 24, severity: LintMessageSeverity.Warning, code: 'W292', message: 'no newline at end of file', provider: '', type: 'E' }
];
const pycodestyleMessagesToBeReturned: ILintMessage[] = [
{ line: 5, column: 1, severity: LintMessageSeverity.Error, code: 'E302', message: 'expected 2 blank lines, found 1', provider: '', type: 'E' },
{ line: 19, column: 15, severity: LintMessageSeverity.Error, code: 'E127', message: 'continuation line over-indented for visual indent', provider: '', type: 'E' },
{ line: 24, column: 23, severity: LintMessageSeverity.Error, code: 'E261', message: 'at least two spaces before inline comment', provider: '', type: 'E' },
{ line: 62, column: 30, severity: LintMessageSeverity.Error, code: 'E261', message: 'at least two spaces before inline comment', provider: '', type: 'E' },
{ line: 70, column: 22, severity: LintMessageSeverity.Error, code: 'E261', message: 'at least two spaces before inline comment', provider: '', type: 'E' },
{ line: 80, column: 5, severity: LintMessageSeverity.Error, code: 'E303', message: 'too many blank lines (2)', provider: '', type: 'E' },
{ line: 87, column: 24, severity: LintMessageSeverity.Warning, code: 'W292', message: 'no newline at end of file', provider: '', type: 'E' }
];
const pydocstyleMessagesToBeReturned: ILintMessage[] = [
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'e\')', column: 0, line: 1, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'t\')', column: 0, line: 5, type: '', provider: 'pydocstyle' },
{ code: 'D102', severity: LintMessageSeverity.Information, message: 'Missing docstring in public method', column: 4, line: 8, type: '', provider: 'pydocstyle' },
{ code: 'D401', severity: LintMessageSeverity.Information, message: 'First line should be in imperative mood (\'thi\', not \'this\')', column: 4, line: 11, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'This\', not \'this\')', column: 4, line: 11, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'e\')', column: 4, line: 11, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'And\', not \'and\')', column: 4, line: 15, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'t\')', column: 4, line: 15, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'Test\', not \'test\')', column: 4, line: 21, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'g\')', column: 4, line: 21, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'Test\', not \'test\')', column: 4, line: 28, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'g\')', column: 4, line: 28, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'Test\', not \'test\')', column: 4, line: 38, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'g\')', column: 4, line: 38, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'Test\', not \'test\')', column: 4, line: 53, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'g\')', column: 4, line: 53, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'Test\', not \'test\')', column: 4, line: 68, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'g\')', column: 4, line: 68, type: '', provider: 'pydocstyle' },
{ code: 'D403', severity: LintMessageSeverity.Information, message: 'First word of the first line should be properly capitalized (\'Test\', not \'test\')', column: 4, line: 80, type: '', provider: 'pydocstyle' },
{ code: 'D400', severity: LintMessageSeverity.Information, message: 'First line should end with a period (not \'g\')', column: 4, line: 80, type: '', provider: 'pydocstyle' }
];
const filteredFlake8MessagesToBeReturned: ILintMessage[] = [
{ line: 87, column: 24, severity: LintMessageSeverity.Warning, code: 'W292', message: 'no newline at end of file', provider: '', type: '' }
];
const filteredPycodestyleMessagesToBeReturned: ILintMessage[] = [
{ line: 87, column: 24, severity: LintMessageSeverity.Warning, code: 'W292', message: 'no newline at end of file', provider: '', type: '' }
];
function getMessages(product: Product): ILintMessage[] {
switch (product) {
case Product.pylint: {
return pylintMessagesToBeReturned;
}
case Product.flake8: {
return flake8MessagesToBeReturned;
}
case Product.pycodestyle: {
return pycodestyleMessagesToBeReturned;
}
case Product.pydocstyle: {
return pydocstyleMessagesToBeReturned;
}
default: {
throwUnknownProduct(product);
return []; // to quiet tslint
}
}
}
async function getInfoForConfig(product: Product) {
const prodID = getLinterID(product);
const dirname = linterConfigDirs.get(prodID);
assert.notEqual(dirname, undefined, `tests not set up for ${Product[product]}`);
const filename = path.join(dirname!, product === Product.pylint ? 'file2.py' : 'file.py');
let messagesToBeReceived: ILintMessage[] = [];
switch (product) {
case Product.flake8: {
messagesToBeReceived = filteredFlake8MessagesToBeReturned;
break;
}
case Product.pycodestyle: {
messagesToBeReceived = filteredPycodestyleMessagesToBeReturned;
break;
}
default: { break; }
}
const basename = linterConfigRCFiles.get(prodID);
return {
filename,
messagesToBeReceived,
origRCFile: basename ? path.join(dirname!, basename) : ''
};
}
class TestFixture extends BaseTestFixture {
constructor(
printLogs = false
) {
const serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>(undefined, TypeMoq.MockBehavior.Strict);
const configService = TypeMoq.Mock.ofType<IConfigurationService>(undefined, TypeMoq.MockBehavior.Strict);
const processLogger = TypeMoq.Mock.ofType<IProcessLogger>(undefined, TypeMoq.MockBehavior.Strict);
processLogger.setup(p => p.logProcess(TypeMoq.It.isAnyString(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return; });
serviceContainer.setup(s => s.get(TypeMoq.It.isValue(IProcessLogger), TypeMoq.It.isAny())).returns(() => processLogger.object);
const platformService = new PlatformService();
const filesystem = new FileSystem(platformService);
super(
platformService,
filesystem,
TestFixture.newPythonToolExecService(
serviceContainer.object
),
TestFixture.newPythonExecFactory(
serviceContainer,
configService.object
),
configService,
serviceContainer,
false,
workspaceDir,
printLogs
);
this.pythonSettings.setup(s => s.pythonPath)
.returns(() => PYTHON_PATH);
}
private static newPythonToolExecService(
serviceContainer: IServiceContainer
): IPythonToolExecutionService {
// We do not worry about the IProcessServiceFactory possibly
// needed by PythonToolExecutionService.
return new PythonToolExecutionService(
serviceContainer
);
}
private static newPythonExecFactory(
serviceContainer: TypeMoq.IMock<IServiceContainer>,
configService: IConfigurationService
): IPythonExecutionFactory {
const envVarsService = TypeMoq.Mock.ofType<IEnvironmentVariablesProvider>(undefined, TypeMoq.MockBehavior.Strict);
envVarsService.setup(e => e.getEnvironmentVariables(TypeMoq.It.isAny())).returns(() => Promise.resolve({}));
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IEnvironmentVariablesProvider), TypeMoq.It.isAny())).returns(() => envVarsService.object);
const disposableRegistry: IDisposableRegistry = [];
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IDisposableRegistry), TypeMoq.It.isAny())).returns(() => disposableRegistry);
const envActivationService = TypeMoq.Mock.ofType<IEnvironmentActivationService>(undefined, TypeMoq.MockBehavior.Strict);
const decoder = new BufferDecoder();
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IBufferDecoder), TypeMoq.It.isAny()))
.returns(() => decoder);
const processLogger = TypeMoq.Mock.ofType<IProcessLogger>(undefined, TypeMoq.MockBehavior.Strict);
processLogger
.setup(p => p.logProcess(TypeMoq.It.isAnyString(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => {
return;
});
const procServiceFactory = new ProcessServiceFactory(envVarsService.object, processLogger.object, decoder, disposableRegistry);
return new PythonExecutionFactory(
serviceContainer.object,
envActivationService.object,
procServiceFactory,
configService,
decoder
);
}
public makeDocument(filename: string): TextDocument {
const doc = newMockDocument(filename);
doc.setup(d => d.lineAt(TypeMoq.It.isAny()))
.returns(lno => {
const lines = fs.readFileSync(filename)
.toString()
.split(os.EOL);
const textline = TypeMoq.Mock.ofType<TextLine>(undefined, TypeMoq.MockBehavior.Strict);
textline.setup(t => t.text)
.returns(() => lines[lno]);
return textline.object;
});
return doc.object;
}
}
// tslint:disable-next-line:max-func-body-length
suite('Linting Functional Tests', () => {
// These are integration tests that mock out everything except
// the filesystem and process execution.
// tslint:disable-next-line:no-any
async function testLinterMessages(
fixture: TestFixture,
product: Product,
pythonFile: string,
messagesToBeReceived: ILintMessage[]
) {
const doc = fixture.makeDocument(pythonFile);
await fixture.linterManager.setActiveLintersAsync([product], doc.uri);
const linter = await fixture.linterManager.createLinter(
product,
fixture.outputChannel.object,
fixture.serviceContainer.object
);
const messages = await linter.lint(
doc,
(new CancellationTokenSource()).token
);
if (messagesToBeReceived.length === 0) {
assert.equal(messages.length, 0, `No errors in linter, Output - ${fixture.output}`);
} else {
if (fixture.output.indexOf('ENOENT') === -1) {
// Pylint for Python Version 2.7 could return 80 linter messages, where as in 3.5 it might only return 1.
// Looks like pylint stops linting as soon as it comes across any ERRORS.
assert.notEqual(messages.length, 0, `No errors in linter, Output - ${fixture.output}`);
}
}
}
for (const product of LINTERID_BY_PRODUCT.keys()) {
test(getProductName(product), async function () {
if ([Product.bandit, Product.mypy, Product.pylama, Product.prospector].some(p => p === product)) {
// tslint:disable-next-line:no-invalid-this
return this.skip();
}
const fixture = new TestFixture();
const messagesToBeReturned = getMessages(product);
await testLinterMessages(fixture, product, fileToLint, messagesToBeReturned);
});
}
for (const product of LINTERID_BY_PRODUCT.keys()) {
// tslint:disable-next-line:max-func-body-length
test(`${getProductName(product)} with config in root`, async function () {
if ([Product.bandit, Product.mypy, Product.pylama, Product.prospector].some(p => p === product)) {
// tslint:disable-next-line:no-invalid-this
return this.skip();
}
const fixture = new TestFixture();
if (product === Product.pydocstyle) {
fixture.lintingSettings.pylintUseMinimalCheckers = false;
}
const { filename, messagesToBeReceived, origRCFile } = await getInfoForConfig(product);
let rcfile = '';
async function cleanUp() {
if (rcfile !== '') {
await deleteFile(rcfile);
}
}
if (origRCFile !== '') {
rcfile = path.join(workspaceUri.fsPath, path.basename(origRCFile));
await fs.copy(origRCFile, rcfile);
}
try {
await testLinterMessages(fixture, product, filename, messagesToBeReceived);
} finally {
await cleanUp();
}
});
}
async function testLinterMessageCount(
fixture: TestFixture,
product: Product,
pythonFile: string,
messageCountToBeReceived: number
) {
const doc = fixture.makeDocument(pythonFile);
await fixture.linterManager.setActiveLintersAsync([product], doc.uri);
const linter = await fixture.linterManager.createLinter(
product,
fixture.outputChannel.object,
fixture.serviceContainer.object
);
const messages = await linter.lint(
doc,
(new CancellationTokenSource()).token
);
assert.equal(messages.length, messageCountToBeReceived,
'Expected number of lint errors does not match lint error count');
}
test('Three line output counted as one message', async () => {
const maxErrors = 5;
const fixture = new TestFixture();
fixture.lintingSettings.maxNumberOfProblems = maxErrors;
await testLinterMessageCount(
fixture,
Product.pylint,
path.join(pythonFilesDir, 'threeLineLints.py'),
maxErrors
);
});
});