Skip to content

Commit 3b82153

Browse files
committed
changes to linter tests
1 parent 985832a commit 3b82153

10 files changed

Lines changed: 410 additions & 60 deletions

File tree

src/test/extension.lint.test.ts

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,35 @@ import {initialize} from './initialize';
2222
import {execPythonFile} from '../client/common/utils';
2323

2424
let pythonSettings = settings.PythonSettings.getInstance();
25-
let ch = vscode.window.createOutputChannel('Lint');
26-
let pythoFilesPath = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'linting');
2725

28-
let targetFlake8ConfigFile = path.join(__dirname, '.flake8');
29-
let targetPep8ConfigFile = path.join(__dirname, '.pep8');
30-
let targetPydocstyleConfigFile = path.join(__dirname, '.pydocstyle');
26+
const pythoFilesPath = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'linting');
27+
const flake8ConfigPath = path.join(pythoFilesPath, 'flake8config');
28+
const pep8ConfigPath = path.join(pythoFilesPath, 'pep8config');
29+
const pydocstyleConfigPath = path.join(pythoFilesPath, 'pydocstyleconfig');
30+
const pylintConfigPath = path.join(pythoFilesPath, 'pylintconfig');
31+
const fileToLint = path.join(pythoFilesPath, 'file.py');
3132
let pylintFileToLintLines: string[] = [];
32-
let pyLintFileToLint = path.join(pythoFilesPath, 'pylintSample.py');
33-
let targetPythonFileToLint = path.join(__dirname, 'pythonFiles', 'linting', path.basename(pyLintFileToLint));
34-
let sourceFlake8ConfigFile = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'linting', 'pylintcfg', '.flake8');
35-
let sourcePep8ConfigFile = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'linting', 'pylintcfg', '.pep8');
36-
let sourcePydocstyleConfigFile = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'linting', 'pylintcfg', '.pydocstyle');
3733

38-
function deleteFile(file: string): Promise<any> {
39-
return new Promise<any>(resolve => {
40-
fs.exists(file, yes => {
41-
if (yes) {
42-
return fs.unlink(file, () => resolve());
43-
}
44-
resolve();
45-
});
46-
});
34+
class MockOutputChannel implements vscode.OutputChannel {
35+
constructor(name: string) {
36+
this.name = name;
37+
this.output = '';
38+
}
39+
name: string;
40+
output: string;
41+
append(value: string) {
42+
this.output += value;
43+
}
44+
appendLine(value: string) { this.append(value); this.append('\n'); }
45+
clear() { }
46+
show(preservceFocus?: boolean): void;
47+
show(column?: vscode.ViewColumn, preserveFocus?: boolean): void;
48+
show(x?: any, y?: any): void { }
49+
hide() { }
50+
dispose() { }
4751
}
4852
suiteSetup(done => {
49-
fs.copySync(pyLintFileToLint, targetPythonFileToLint);
50-
pylintFileToLintLines = fs.readFileSync(pyLintFileToLint).toString('utf-8').split(/\r?\n/g);
53+
pylintFileToLintLines = fs.readFileSync(fileToLint).toString('utf-8').split(/\r?\n/g);
5154
done();
5255
});
5356
suiteTeardown(done => {
@@ -57,7 +60,6 @@ suiteTeardown(done => {
5760

5861
suite('Linting', () => {
5962
let pylintMessagesToBeReturned: baseLinter.ILintMessage[] = [
60-
{ line: 17, column: 0, severity: baseLinter.LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling unused-argument (W0613)', possibleWord: '', provider: '', type: '' },
6163
{ line: 24, column: 0, severity: baseLinter.LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', possibleWord: '', provider: '', type: '' },
6264
{ line: 30, column: 0, severity: baseLinter.LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', possibleWord: '', provider: '', type: '' },
6365
{ line: 34, column: 0, severity: baseLinter.LintMessageSeverity.Information, code: 'I0012', message: 'Locally enabling no-member (E1101)', possibleWord: '', provider: '', type: '' },
@@ -69,7 +71,6 @@ suite('Linting', () => {
6971
{ line: 70, column: 0, severity: baseLinter.LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', possibleWord: '', provider: '', type: '' },
7072
{ line: 84, column: 0, severity: baseLinter.LintMessageSeverity.Information, code: 'I0011', message: 'Locally disabling no-member (E1101)', possibleWord: '', provider: '', type: '' },
7173
{ line: 87, column: 0, severity: baseLinter.LintMessageSeverity.Hint, code: 'C0304', message: 'Final newline missing', possibleWord: '', provider: '', type: '' },
72-
{ line: 1, column: 0, severity: baseLinter.LintMessageSeverity.Hint, code: 'C0103', message: 'Invalid module name \"pylintSample\"', possibleWord: '', provider: '', type: '' },
7374
{ line: 11, column: 20, severity: baseLinter.LintMessageSeverity.Warning, code: 'W0613', message: 'Unused argument \'arg\'', possibleWord: '', provider: '', type: '' },
7475
{ line: 26, column: 14, severity: baseLinter.LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blop\' member', possibleWord: '', provider: '', type: '' },
7576
{ line: 36, column: 14, severity: baseLinter.LintMessageSeverity.Error, code: 'E1101', message: 'Instance of \'Foo\' has no \'blip\' member', possibleWord: '', provider: '', type: '' },
@@ -82,7 +83,6 @@ suite('Linting', () => {
8283
];
8384
let flake8MessagesToBeReturned: baseLinter.ILintMessage[] = [
8485
{ line: 5, column: 1, severity: baseLinter.LintMessageSeverity.Information, code: 'E302', message: 'expected 2 blank lines, found 1', possibleWord: '', provider: '', type: '' },
85-
{ line: 13, column: 19, severity: baseLinter.LintMessageSeverity.Information, code: 'E901', message: 'SyntaxError: invalid syntax', possibleWord: '', provider: '', type: '' },
8686
{ line: 19, column: 15, severity: baseLinter.LintMessageSeverity.Information, code: 'E127', message: 'continuation line over-indented for visual indent', possibleWord: '', provider: '', type: '' },
8787
{ line: 24, column: 23, severity: baseLinter.LintMessageSeverity.Information, code: 'E261', message: 'at least two spaces before inline comment', possibleWord: '', provider: '', type: '' },
8888
{ line: 62, column: 30, severity: baseLinter.LintMessageSeverity.Information, code: 'E261', message: 'at least two spaces before inline comment', possibleWord: '', provider: '', type: '' },
@@ -153,13 +153,6 @@ suite('Linting', () => {
153153
}).then(done, done);
154154
});
155155

156-
teardown(done => {
157-
Promise.all([deleteFile(targetFlake8ConfigFile),
158-
deleteFile(targetPep8ConfigFile),
159-
deleteFile(targetPydocstyleConfigFile)
160-
]).then(() => done(), done);
161-
});
162-
163156
function testEnablingDisablingOfLinter(linter: baseLinter.BaseLinter, propertyName: string) {
164157
pythonSettings.linting[propertyName] = true;
165158
assert.equal(true, linter.isEnabled());
@@ -168,69 +161,78 @@ suite('Linting', () => {
168161
assert.equal(false, linter.isEnabled());
169162
}
170163
test('Enable and Disable Pylint', () => {
171-
testEnablingDisablingOfLinter(new pyLint.Linter(ch, __dirname), 'pylintEnabled');
164+
let ch = new MockOutputChannel('Lint');
165+
testEnablingDisablingOfLinter(new pyLint.Linter(ch, pythoFilesPath), 'pylintEnabled');
172166
});
173167
test('Enable and Disable Pep8', () => {
174-
testEnablingDisablingOfLinter(new pep8.Linter(ch, __dirname), 'pep8Enabled');
168+
let ch = new MockOutputChannel('Lint');
169+
testEnablingDisablingOfLinter(new pep8.Linter(ch, pythoFilesPath), 'pep8Enabled');
175170
});
176171
test('Enable and Disable Flake8', () => {
177-
testEnablingDisablingOfLinter(new flake8.Linter(ch, __dirname), 'flake8Enabled');
172+
let ch = new MockOutputChannel('Lint');
173+
testEnablingDisablingOfLinter(new flake8.Linter(ch, pythoFilesPath), 'flake8Enabled');
178174
});
179175
test('Enable and Disable Prospector', () => {
180-
testEnablingDisablingOfLinter(new prospector.Linter(ch, __dirname), 'prospectorEnabled');
176+
let ch = new MockOutputChannel('Lint');
177+
testEnablingDisablingOfLinter(new prospector.Linter(ch, pythoFilesPath), 'prospectorEnabled');
181178
});
182179
test('Enable and Disable Pydocstyle', () => {
183-
testEnablingDisablingOfLinter(new pydocstyle.Linter(ch, __dirname), 'pydocstyleEnabled');
180+
let ch = new MockOutputChannel('Lint');
181+
testEnablingDisablingOfLinter(new pydocstyle.Linter(ch, pythoFilesPath), 'pydocstyleEnabled');
184182
});
185183

186-
function testLinterMessages(linter: baseLinter.BaseLinter, pythonFile: string, pythonFileLines: string[], messagesToBeReceived: baseLinter.ILintMessage[]): Promise<any> {
184+
function testLinterMessages(linter: baseLinter.BaseLinter, outputChannel: MockOutputChannel, pythonFile: string, pythonFileLines: string[], messagesToBeReceived: baseLinter.ILintMessage[]): Promise<any> {
187185
return linter.runLinter(pythonFile, pythonFileLines).then(messages => {
188186
// Different versions of python return different errors,
189187
// Here we have errors for version 2.7
190-
assert.notEqual(messages.length, 0, 'No errors in linter');
188+
assert.notEqual(messages.length, 0, 'No errors in linter, Output - ' + outputChannel.output);
191189
messagesToBeReceived.forEach(msg => {
192190
let similarMessages = messages.filter(m => m.code === msg.code && m.column === msg.column &&
193191
m.line === msg.line && m.message === msg.message && m.severity === msg.severity);
194-
assert.equal(true, similarMessages.length > 0, 'Error not found, ' + JSON.stringify(msg));
192+
assert.equal(true, similarMessages.length > 0, 'Error not found, ' + JSON.stringify(msg) + '\n, Output - ' + outputChannel.output);
195193
});
196194
}, error => {
197-
assert.fail(error, null, 'Linter error');
195+
assert.fail(error, null, 'Linter error, Output - ' + outputChannel.output);
198196
});
199197
}
200198
test('PyLint', done => {
201-
let linter = new pyLint.Linter(ch, __dirname);
202-
return testLinterMessages(linter, pyLintFileToLint, pylintFileToLintLines, pylintMessagesToBeReturned).then(done, done);
199+
let ch = new MockOutputChannel('Lint');
200+
let linter = new pyLint.Linter(ch, pythoFilesPath);
201+
return testLinterMessages(linter, ch, fileToLint, pylintFileToLintLines, pylintMessagesToBeReturned).then(done, done);
203202
});
204203
test('Flake8', done => {
205-
let linter = new flake8.Linter(ch, __dirname);
206-
return testLinterMessages(linter, pyLintFileToLint, pylintFileToLintLines, flake8MessagesToBeReturned).then(done, done);
204+
let ch = new MockOutputChannel('Lint');
205+
let linter = new flake8.Linter(ch, pythoFilesPath);
206+
return testLinterMessages(linter, ch, fileToLint, pylintFileToLintLines, flake8MessagesToBeReturned).then(done, done);
207207
});
208208
test('Pep8', done => {
209-
let linter = new pep8.Linter(ch, __dirname);
210-
return testLinterMessages(linter, pyLintFileToLint, pylintFileToLintLines, pep8MessagesToBeReturned).then(done, done);
209+
let ch = new MockOutputChannel('Lint');
210+
let linter = new pep8.Linter(ch, pythoFilesPath);
211+
return testLinterMessages(linter, ch, fileToLint, pylintFileToLintLines, pep8MessagesToBeReturned).then(done, done);
211212
});
212213
test('Pydocstyle', done => {
213-
let linter = new pydocstyle.Linter(ch, __dirname);
214-
return testLinterMessages(linter, pyLintFileToLint, pylintFileToLintLines, pydocstyleMessagseToBeReturned).then(done, done);
214+
let ch = new MockOutputChannel('Lint');
215+
let linter = new pydocstyle.Linter(ch, pythoFilesPath);
216+
return testLinterMessages(linter, ch, fileToLint, pylintFileToLintLines, pydocstyleMessagseToBeReturned).then(done, done);
215217
});
216218
test('PyLint with config in root', done => {
217-
let rootDirContainingConfig = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'linting', 'pylintcfg');
218-
let linter = new pyLint.Linter(ch, rootDirContainingConfig);
219-
return testLinterMessages(linter, pyLintFileToLint, pylintFileToLintLines, filteredPylintMessagesToBeReturned).then(done, done);
219+
let ch = new MockOutputChannel('Lint');
220+
let linter = new pyLint.Linter(ch, pylintConfigPath);
221+
return testLinterMessages(linter, ch, path.join(pylintConfigPath, 'file.py'), pylintFileToLintLines, filteredPylintMessagesToBeReturned).then(done, done);
220222
});
221223
test('Flake8 with config in root', done => {
222-
fs.copySync(sourceFlake8ConfigFile, targetFlake8ConfigFile);
223-
let linter = new flake8.Linter(ch, __dirname);
224-
return testLinterMessages(linter, targetPythonFileToLint, pylintFileToLintLines, filteredFlake8MessagesToBeReturned).then(done, done);
224+
let ch = new MockOutputChannel('Lint');
225+
let linter = new flake8.Linter(ch, flake8ConfigPath);
226+
return testLinterMessages(linter, ch, path.join(flake8ConfigPath, 'file.py'), pylintFileToLintLines, filteredFlake8MessagesToBeReturned).then(done, done);
225227
});
226228
test('Pep8 with config in root', done => {
227-
fs.copySync(sourcePep8ConfigFile, targetPep8ConfigFile);
228-
let linter = new pep8.Linter(ch, __dirname);
229-
return testLinterMessages(linter, targetPythonFileToLint, pylintFileToLintLines, filteredPep88MessagesToBeReturned).then(done, done);
229+
let ch = new MockOutputChannel('Lint');
230+
let linter = new pep8.Linter(ch, pep8ConfigPath);
231+
return testLinterMessages(linter, ch, path.join(pep8ConfigPath, 'file.py'), pylintFileToLintLines, filteredPep88MessagesToBeReturned).then(done, done);
230232
});
231233
test('Pydocstyle with config in root', done => {
232-
fs.copySync(sourcePydocstyleConfigFile, targetPydocstyleConfigFile);
233-
let linter = new pydocstyle.Linter(ch, __dirname);
234-
return testLinterMessages(linter, targetPythonFileToLint, pylintFileToLintLines, fiteredPydocstyleMessagseToBeReturned).then(done, done);
234+
let ch = new MockOutputChannel('Lint');
235+
let linter = new pydocstyle.Linter(ch, pydocstyleConfigPath);
236+
return testLinterMessages(linter, ch, path.join(pydocstyleConfigPath, 'file.py'), pylintFileToLintLines, fiteredPydocstyleMessagseToBeReturned).then(done, done);
235237
});
236238
});
File renamed without changes.
File renamed without changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""pylint option block-disable"""
2+
3+
__revision__ = None
4+
5+
class Foo(object):
6+
"""block-disable test"""
7+
8+
def __init__(self):
9+
pass
10+
11+
def meth1(self, arg):
12+
"""this issues a message"""
13+
print self
14+
15+
def meth2(self, arg):
16+
"""and this one not"""
17+
# pylint: disable=unused-argument
18+
print self\
19+
+ "foo"
20+
21+
def meth3(self):
22+
"""test one line disabling"""
23+
# no error
24+
print self.bla # pylint: disable=no-member
25+
# error
26+
print self.blop
27+
28+
def meth4(self):
29+
"""test re-enabling"""
30+
# pylint: disable=no-member
31+
# no error
32+
print self.bla
33+
print self.blop
34+
# pylint: enable=no-member
35+
# error
36+
print self.blip
37+
38+
def meth5(self):
39+
"""test IF sub-block re-enabling"""
40+
# pylint: disable=no-member
41+
# no error
42+
print self.bla
43+
if self.blop:
44+
# pylint: enable=no-member
45+
# error
46+
print self.blip
47+
else:
48+
# no error
49+
print self.blip
50+
# no error
51+
print self.blip
52+
53+
def meth6(self):
54+
"""test TRY/EXCEPT sub-block re-enabling"""
55+
# pylint: disable=no-member
56+
# no error
57+
print self.bla
58+
try:
59+
# pylint: enable=no-member
60+
# error
61+
print self.blip
62+
except UndefinedName: # pylint: disable=undefined-variable
63+
# no error
64+
print self.blip
65+
# no error
66+
print self.blip
67+
68+
def meth7(self):
69+
"""test one line block opening disabling"""
70+
if self.blop: # pylint: disable=no-member
71+
# error
72+
print self.blip
73+
else:
74+
# error
75+
print self.blip
76+
# error
77+
print self.blip
78+
79+
80+
def meth8(self):
81+
"""test late disabling"""
82+
# error
83+
print self.blip
84+
# pylint: disable=no-member
85+
# no error
86+
print self.bla
87+
print self.blop
File renamed without changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""pylint option block-disable"""
2+
3+
__revision__ = None
4+
5+
class Foo(object):
6+
"""block-disable test"""
7+
8+
def __init__(self):
9+
pass
10+
11+
def meth1(self, arg):
12+
"""this issues a message"""
13+
print self
14+
15+
def meth2(self, arg):
16+
"""and this one not"""
17+
# pylint: disable=unused-argument
18+
print self\
19+
+ "foo"
20+
21+
def meth3(self):
22+
"""test one line disabling"""
23+
# no error
24+
print self.bla # pylint: disable=no-member
25+
# error
26+
print self.blop
27+
28+
def meth4(self):
29+
"""test re-enabling"""
30+
# pylint: disable=no-member
31+
# no error
32+
print self.bla
33+
print self.blop
34+
# pylint: enable=no-member
35+
# error
36+
print self.blip
37+
38+
def meth5(self):
39+
"""test IF sub-block re-enabling"""
40+
# pylint: disable=no-member
41+
# no error
42+
print self.bla
43+
if self.blop:
44+
# pylint: enable=no-member
45+
# error
46+
print self.blip
47+
else:
48+
# no error
49+
print self.blip
50+
# no error
51+
print self.blip
52+
53+
def meth6(self):
54+
"""test TRY/EXCEPT sub-block re-enabling"""
55+
# pylint: disable=no-member
56+
# no error
57+
print self.bla
58+
try:
59+
# pylint: enable=no-member
60+
# error
61+
print self.blip
62+
except UndefinedName: # pylint: disable=undefined-variable
63+
# no error
64+
print self.blip
65+
# no error
66+
print self.blip
67+
68+
def meth7(self):
69+
"""test one line block opening disabling"""
70+
if self.blop: # pylint: disable=no-member
71+
# error
72+
print self.blip
73+
else:
74+
# error
75+
print self.blip
76+
# error
77+
print self.blip
78+
79+
80+
def meth8(self):
81+
"""test late disabling"""
82+
# error
83+
print self.blip
84+
# pylint: disable=no-member
85+
# no error
86+
print self.bla
87+
print self.blop

src/test/pythonFiles/linting/pylintcfg/.pydocstyle renamed to src/test/pythonFiles/linting/pydocstyleconfig/.pydocstyle

File renamed without changes.

0 commit comments

Comments
 (0)