forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlintengine.test.ts
More file actions
171 lines (152 loc) · 7.53 KB
/
lintengine.test.ts
File metadata and controls
171 lines (152 loc) · 7.53 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as TypeMoq from 'typemoq';
import { OutputChannel, TextDocument, Uri } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../../client/common/application/types';
import { PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL } from '../../client/common/constants';
import '../../client/common/extensions';
import { IFileSystem } from '../../client/common/platform/types';
import { IConfigurationService, ILintingSettings, IOutputChannel, IPythonSettings } from '../../client/common/types';
import { IServiceContainer } from '../../client/ioc/types';
import { LintingEngine } from '../../client/linters/lintingEngine';
import { ILinterManager, ILintingEngine } from '../../client/linters/types';
import { initialize } from '../initialize';
suite('Linting - LintingEngine', () => {
let serviceContainer: TypeMoq.IMock<IServiceContainer>;
let lintManager: TypeMoq.IMock<ILinterManager>;
let settings: TypeMoq.IMock<IPythonSettings>;
let lintSettings: TypeMoq.IMock<ILintingSettings>;
let fileSystem: TypeMoq.IMock<IFileSystem>;
let lintingEngine: ILintingEngine;
suiteSetup(initialize);
setup(async () => {
serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
const docManager = TypeMoq.Mock.ofType<IDocumentManager>();
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IDocumentManager), TypeMoq.It.isAny()))
.returns(() => docManager.object);
const workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IWorkspaceService), TypeMoq.It.isAny()))
.returns(() => workspaceService.object);
fileSystem = TypeMoq.Mock.ofType<IFileSystem>();
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IFileSystem), TypeMoq.It.isAny()))
.returns(() => fileSystem.object);
lintSettings = TypeMoq.Mock.ofType<ILintingSettings>();
settings = TypeMoq.Mock.ofType<IPythonSettings>();
const configService = TypeMoq.Mock.ofType<IConfigurationService>();
configService.setup((x) => x.getSettings(TypeMoq.It.isAny())).returns(() => settings.object);
configService.setup((x) => x.isTestExecution()).returns(() => true);
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IConfigurationService), TypeMoq.It.isAny()))
.returns(() => configService.object);
const outputChannel = TypeMoq.Mock.ofType<OutputChannel>();
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(IOutputChannel), TypeMoq.It.isValue(STANDARD_OUTPUT_CHANNEL)))
.returns(() => outputChannel.object);
lintManager = TypeMoq.Mock.ofType<ILinterManager>();
lintManager.setup((x) => x.isLintingEnabled(TypeMoq.It.isAny())).returns(async () => true);
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(ILinterManager), TypeMoq.It.isAny()))
.returns(() => lintManager.object);
lintingEngine = new LintingEngine(serviceContainer.object);
serviceContainer
.setup((c) => c.get(TypeMoq.It.isValue(ILintingEngine), TypeMoq.It.isAny()))
.returns(() => lintingEngine);
});
test('Ensure document.uri is passed into isLintingEnabled', () => {
const doc = mockTextDocument('a.py', PYTHON_LANGUAGE, true);
try {
lintingEngine.lintDocument(doc, 'auto').ignoreErrors();
} catch {
lintManager.verify((l) => l.isLintingEnabled(TypeMoq.It.isValue(doc.uri)), TypeMoq.Times.once());
}
});
test('Ensure document.uri is passed into createLinter', () => {
const doc = mockTextDocument('a.py', PYTHON_LANGUAGE, true);
try {
lintingEngine.lintDocument(doc, 'auto').ignoreErrors();
} catch {
lintManager.verify(
(l) =>
l.createLinter(
TypeMoq.It.isAny(),
TypeMoq.It.isAny(),
TypeMoq.It.isAny(),
TypeMoq.It.isValue(doc.uri),
),
TypeMoq.Times.atLeastOnce(),
);
}
});
test('Verify files that match ignore pattern are not linted', async () => {
const doc = mockTextDocument('a1.py', PYTHON_LANGUAGE, true, ['a*.py']);
await lintingEngine.lintDocument(doc, 'auto');
lintManager.verify(
(l) => l.createLinter(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
test('Ensure non-Python files are not linted', async () => {
const doc = mockTextDocument('a.ts', 'typescript', true);
await lintingEngine.lintDocument(doc, 'auto');
lintManager.verify(
(l) => l.createLinter(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
test('Ensure files with git scheme are not linted', async () => {
const doc = mockTextDocument('a1.py', PYTHON_LANGUAGE, false, [], 'git');
await lintingEngine.lintDocument(doc, 'auto');
lintManager.verify(
(l) => l.createLinter(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
test('Ensure files with showModifications scheme are not linted', async () => {
const doc = mockTextDocument('a1.py', PYTHON_LANGUAGE, false, [], 'showModifications');
await lintingEngine.lintDocument(doc, 'auto');
lintManager.verify(
(l) => l.createLinter(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
test('Ensure files with svn scheme are not linted', async () => {
const doc = mockTextDocument('a1.py', PYTHON_LANGUAGE, false, [], 'svn');
await lintingEngine.lintDocument(doc, 'auto');
lintManager.verify(
(l) => l.createLinter(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
test('Ensure non-existing files are not linted', async () => {
const doc = mockTextDocument('file.py', PYTHON_LANGUAGE, false, []);
await lintingEngine.lintDocument(doc, 'auto');
lintManager.verify(
(l) => l.createLinter(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
TypeMoq.Times.never(),
);
});
function mockTextDocument(
fileName: string,
language: string,
exists: boolean,
ignorePattern: string[] = [],
scheme?: string,
): TextDocument {
fileSystem.setup((x) => x.fileExists(TypeMoq.It.isAnyString())).returns(() => Promise.resolve(exists));
lintSettings.setup((l) => l.ignorePatterns).returns(() => ignorePattern);
settings.setup((x) => x.linting).returns(() => lintSettings.object);
const doc = TypeMoq.Mock.ofType<TextDocument>();
if (scheme) {
doc.setup((d) => d.uri).returns(() => Uri.parse(`${scheme}:${fileName}`));
} else {
doc.setup((d) => d.uri).returns(() => Uri.file(fileName));
}
doc.setup((d) => d.fileName).returns(() => fileName);
doc.setup((d) => d.languageId).returns(() => language);
return doc.object;
}
});