forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint.args.test.ts
More file actions
216 lines (197 loc) · 11.5 KB
/
lint.args.test.ts
File metadata and controls
216 lines (197 loc) · 11.5 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-any max-func-body-length
import { expect } from 'chai';
import { Container } from 'inversify';
import * as path from 'path';
import * as TypeMoq from 'typemoq';
import { CancellationTokenSource, OutputChannel, TextDocument, Uri, WorkspaceFolder } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../../client/common/application/types';
import '../../client/common/extensions';
import { IFileSystem, IPlatformService } from '../../client/common/platform/types';
import {
IConfigurationService,
IInstaller,
ILintingSettings,
IOutputChannel,
IPythonSettings
} from '../../client/common/types';
import {
IInterpreterAutoSelectionService,
IInterpreterAutoSeletionProxyService
} from '../../client/interpreter/autoSelection/types';
import { IInterpreterService } from '../../client/interpreter/contracts';
import { ServiceContainer } from '../../client/ioc/container';
import { ServiceManager } from '../../client/ioc/serviceManager';
import { Bandit } from '../../client/linters/bandit';
import { BaseLinter } from '../../client/linters/baseLinter';
import { Flake8 } from '../../client/linters/flake8';
import { LinterManager } from '../../client/linters/linterManager';
import { MyPy } from '../../client/linters/mypy';
import { Prospector } from '../../client/linters/prospector';
import { Pycodestyle } from '../../client/linters/pycodestyle';
import { PyDocStyle } from '../../client/linters/pydocstyle';
import { PyLama } from '../../client/linters/pylama';
import { Pylint } from '../../client/linters/pylint';
import { ILinterManager, ILintingEngine } from '../../client/linters/types';
import { initialize } from '../initialize';
import { MockAutoSelectionService } from '../mocks/autoSelector';
suite('Linting - Arguments', () => {
[undefined, path.join('users', 'dev_user')].forEach(workspaceUri => {
[
Uri.file(path.join('users', 'dev_user', 'development path to', 'one.py')),
Uri.file(path.join('users', 'dev_user', 'development', 'one.py'))
].forEach(fileUri => {
suite(
`File path ${fileUri.fsPath.indexOf(' ') > 0 ? 'with' : 'without'} spaces and ${
workspaceUri ? 'without' : 'with'
} a workspace`,
() => {
let interpreterService: TypeMoq.IMock<IInterpreterService>;
let engine: TypeMoq.IMock<ILintingEngine>;
let configService: TypeMoq.IMock<IConfigurationService>;
let docManager: TypeMoq.IMock<IDocumentManager>;
let settings: TypeMoq.IMock<IPythonSettings>;
let lm: ILinterManager;
let serviceContainer: ServiceContainer;
let document: TypeMoq.IMock<TextDocument>;
let outputChannel: TypeMoq.IMock<OutputChannel>;
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
const cancellationToken = new CancellationTokenSource().token;
suiteSetup(initialize);
setup(async () => {
const cont = new Container();
const serviceManager = new ServiceManager(cont);
serviceContainer = new ServiceContainer(cont);
outputChannel = TypeMoq.Mock.ofType<OutputChannel>();
const fs = TypeMoq.Mock.ofType<IFileSystem>();
fs.setup(x => x.fileExists(TypeMoq.It.isAny())).returns(
() => new Promise<boolean>((resolve, _reject) => resolve(true))
);
fs.setup(x => x.arePathsSame(TypeMoq.It.isAnyString(), TypeMoq.It.isAnyString())).returns(
() => true
);
serviceManager.addSingletonInstance<IFileSystem>(IFileSystem, fs.object);
serviceManager.addSingletonInstance(IOutputChannel, outputChannel.object);
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
serviceManager.addSingletonInstance<IInterpreterService>(
IInterpreterService,
interpreterService.object
);
serviceManager.addSingleton<IInterpreterAutoSelectionService>(
IInterpreterAutoSelectionService,
MockAutoSelectionService
);
serviceManager.addSingleton<IInterpreterAutoSeletionProxyService>(
IInterpreterAutoSeletionProxyService,
MockAutoSelectionService
);
engine = TypeMoq.Mock.ofType<ILintingEngine>();
serviceManager.addSingletonInstance<ILintingEngine>(ILintingEngine, engine.object);
docManager = TypeMoq.Mock.ofType<IDocumentManager>();
serviceManager.addSingletonInstance<IDocumentManager>(IDocumentManager, docManager.object);
const lintSettings = TypeMoq.Mock.ofType<ILintingSettings>();
lintSettings.setup(x => x.enabled).returns(() => true);
lintSettings.setup(x => x.lintOnSave).returns(() => true);
settings = TypeMoq.Mock.ofType<IPythonSettings>();
settings.setup(x => x.linting).returns(() => lintSettings.object);
configService = TypeMoq.Mock.ofType<IConfigurationService>();
configService.setup(x => x.getSettings(TypeMoq.It.isAny())).returns(() => settings.object);
serviceManager.addSingletonInstance<IConfigurationService>(
IConfigurationService,
configService.object
);
const workspaceFolder: WorkspaceFolder | undefined = workspaceUri
? { uri: Uri.file(workspaceUri), index: 0, name: '' }
: undefined;
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
workspaceService
.setup(w => w.getWorkspaceFolder(TypeMoq.It.isAny()))
.returns(() => workspaceFolder);
serviceManager.addSingletonInstance<IWorkspaceService>(
IWorkspaceService,
workspaceService.object
);
const installer = TypeMoq.Mock.ofType<IInstaller>();
serviceManager.addSingletonInstance<IInstaller>(IInstaller, installer.object);
const platformService = TypeMoq.Mock.ofType<IPlatformService>();
serviceManager.addSingletonInstance<IPlatformService>(IPlatformService, platformService.object);
lm = new LinterManager(serviceContainer, workspaceService.object);
serviceManager.addSingletonInstance<ILinterManager>(ILinterManager, lm);
document = TypeMoq.Mock.ofType<TextDocument>();
});
async function testLinter(linter: BaseLinter, expectedArgs: string[]) {
document.setup(d => d.uri).returns(() => fileUri);
let invoked = false;
(linter as any).run = (args: string[]) => {
expect(args).to.deep.equal(expectedArgs);
invoked = true;
return Promise.resolve([]);
};
await linter.lint(document.object, cancellationToken);
expect(invoked).to.be.equal(true, 'method not invoked');
}
test('Flake8', async () => {
const linter = new Flake8(outputChannel.object, serviceContainer);
const expectedArgs = ['--format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s', fileUri.fsPath];
await testLinter(linter, expectedArgs);
});
test('Pycodestyle', async () => {
const linter = new Pycodestyle(outputChannel.object, serviceContainer);
const expectedArgs = ['--format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s', fileUri.fsPath];
await testLinter(linter, expectedArgs);
});
test('Prospector', async () => {
const linter = new Prospector(outputChannel.object, serviceContainer);
const expectedPath = workspaceUri
? fileUri.fsPath.substring(workspaceUri.length + 2)
: path.basename(fileUri.fsPath);
const expectedArgs = ['--absolute-paths', '--output-format=json', expectedPath];
await testLinter(linter, expectedArgs);
});
test('Pylama', async () => {
const linter = new PyLama(outputChannel.object, serviceContainer);
const expectedArgs = ['--format=parsable', fileUri.fsPath];
await testLinter(linter, expectedArgs);
});
test('MyPy', async () => {
const linter = new MyPy(outputChannel.object, serviceContainer);
const expectedArgs = [fileUri.fsPath];
await testLinter(linter, expectedArgs);
});
test('Pydocstyle', async () => {
const linter = new PyDocStyle(outputChannel.object, serviceContainer);
const expectedArgs = [fileUri.fsPath];
await testLinter(linter, expectedArgs);
});
test('Pylint', async () => {
const linter = new Pylint(outputChannel.object, serviceContainer);
document.setup(d => d.uri).returns(() => fileUri);
let invoked = false;
(linter as any).run = (args: any[], _doc: any, _token: any) => {
expect(args[args.length - 1]).to.equal(fileUri.fsPath);
invoked = true;
return Promise.resolve([]);
};
await linter.lint(document.object, cancellationToken);
expect(invoked).to.be.equal(true, 'method not invoked');
});
test('Bandit', async () => {
const linter = new Bandit(outputChannel.object, serviceContainer);
const expectedArgs = [
'-f',
'custom',
'--msg-template',
'{line},0,{severity},{test_id}:{msg}',
'-n',
'-1',
fileUri.fsPath
];
await testLinter(linter, expectedArgs);
});
}
);
});
});
});