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
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
import contextlib
import json
import re
import sys
import os
from os.path import join, dirname, abspath
import tempfile
import textwrap
import unittest
import six
from pylint.lint import Run
from pylint import __pkginfo__
from pylint.reporters import BaseReporter
from pylint.reporters.text import *
from pylint.reporters.html import HTMLReporter
from pylint.reporters.json import JSONReporter
HERE = abspath(dirname(__file__))
@contextlib.contextmanager
def _patch_streams(out):
sys.stderr = sys.stdout = out
try:
yield
finally:
sys.stderr = sys.__stderr__
sys.stdout = sys.__stdout__
class MultiReporter(BaseReporter):
def __init__(self, reporters):
self._reporters = reporters
self.path_strip_prefix = os.getcwd() + os.sep
def on_set_current_module(self, *args, **kwargs):
for rep in self._reporters:
rep.on_set_current_module(*args, **kwargs)
def handle_message(self, msg):
for rep in self._reporters:
rep.handle_message(msg)
def display_reports(self, layout):
pass
@property
def out(self):
return self._reporters[0].out
@property
def linter(self):
return self._linter
@linter.setter
def linter(self, value):
self._linter = value
for rep in self._reporters:
rep.linter = value
class RunTC(unittest.TestCase):
def _runtest(self, args, reporter=None, out=None, code=28):
if out is None:
out = six.StringIO()
pylint_code = self._run_pylint(args, reporter=reporter, out=out)
if reporter:
output = reporter.out.getvalue()
elif hasattr(out, 'getvalue'):
output = out.getvalue()
else:
output = None
msg = 'expected output status %s, got %s' % (code, pylint_code)
if output is not None:
msg = '%s. Below pylint output: \n%s' % (msg, output)
self.assertEqual(pylint_code, code, msg)
def _run_pylint(self, args, out, reporter=None):
args = args + ['--persistent=no']
with _patch_streams(out):
with self.assertRaises(SystemExit) as cm:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Run(args, reporter=reporter)
return cm.exception.code
def _test_output(self, args, expected_output):
out = six.StringIO()
self._run_pylint(args, out=out)
actual_output = out.getvalue()
self.assertIn(expected_output.strip(), actual_output.strip())
def test_pkginfo(self):
"""Make pylint check itself."""
self._runtest(['pylint.__pkginfo__'], reporter=TextReporter(six.StringIO()),
code=0)
def test_all(self):
"""Make pylint check itself."""
reporters = [
TextReporter(six.StringIO()),
HTMLReporter(six.StringIO()),
ColorizedTextReporter(six.StringIO()),
JSONReporter(six.StringIO())
]
self._runtest(['pylint/test/functional/arguments.py'],
reporter=MultiReporter(reporters), code=1)
def test_no_ext_file(self):
self._runtest([join(HERE, 'input', 'noext')], code=0)
def test_w0704_ignored(self):
self._runtest([join(HERE, 'input', 'ignore_except_pass_by_default.py')], code=0)
def test_generate_config_option(self):
self._runtest(['--generate-rcfile'], code=0)
def test_generate_config_disable_symbolic_names(self):
# Test that --generate-rcfile puts symbolic names in the --disable
# option.
out = six.StringIO()
self._run_pylint(["--generate-rcfile", "--rcfile="], out=out)
output = out.getvalue()
# Get rid of the pesky messages that pylint emits if the
# configuration file is not found.
master = re.search("\[MASTER", output)
out = six.StringIO(output[master.start():])
parser = six.moves.configparser.RawConfigParser()
parser.readfp(out)
messages = parser.get('MESSAGES CONTROL', 'disable').split(",")
self.assertIn('suppressed-message', messages)
def test_generate_rcfile_no_obsolete_methods(self):
out = six.StringIO()
self._run_pylint(["--generate-rcfile"], out=out)
output = out.getvalue()
self.assertNotIn("profile", output)
def _test_deprecated_options(self, option, expected):
out = six.StringIO()
self._run_pylint([option, "--rcfile=", "pylint.config"], out=out)
output = out.getvalue()
if __pkginfo__.numversion >= (1, 6, 0):
self.assertIn("no such option", output)
else:
self.assertIn(expected, output)
def test_deprecated_options_zope(self):
expected = "no such option"
self._test_deprecated_options("--zope=y", expected)
def test_deprecated_options_symbols(self):
expected = "no such option"
self._test_deprecated_options("--symbols=y", expected)
def test_deprecated_options_include_ids(self):
expected = "no such option"
self._test_deprecated_options("--include-ids=y", expected)
def test_deprecated_options_profile(self):
expected = "no such option"
self._test_deprecated_options("--profile=y", expected)
def test_help_message_option(self):
self._runtest(['--help-msg', 'W0101'], code=0)
def test_error_help_message_option(self):
self._runtest(['--help-msg', 'WX101'], code=0)
def test_error_missing_arguments(self):
self._runtest([], code=32)
def test_no_out_encoding(self):
"""test redirection of stdout with non ascii caracters
"""
#This test reproduces bug #48066 ; it happens when stdout is redirected
# through '>' : the sys.stdout.encoding becomes then None, and if the
# output contains non ascii, pylint will crash
if sys.version_info < (3, 0):
strio = tempfile.TemporaryFile()
else:
strio = six.StringIO()
assert strio.encoding is None
self._runtest([join(HERE, 'regrtest_data/no_stdout_encoding.py')],
out=strio)
def test_parallel_execution(self):
self._runtest(['-j 2', 'pylint/test/functional/arguments.py',
'pylint/test/functional/bad_continuation.py'], code=1)
def test_parallel_execution_missing_arguments(self):
self._runtest(['-j 2', 'not_here', 'not_here_too'], code=1)
def test_py3k_option(self):
# Test that --py3k flag works.
rc_code = 2 if six.PY2 else 0
self._runtest([join(HERE, 'functional', 'unpacked_exceptions.py'),
'--py3k'],
code=rc_code)
def test_py3k_jobs_option(self):
rc_code = 2 if six.PY2 else 0
self._runtest([join(HERE, 'functional', 'unpacked_exceptions.py'),
'--py3k', '-j 2'],
code=rc_code)
@unittest.skipIf(sys.version_info[0] > 2, "Requires the --py3k flag.")
def test_py3k_commutative_with_errors_only(self):
# Test what gets emitted with -E only
module = join(HERE, 'regrtest_data', 'py3k_error_flag.py')
expected = textwrap.dedent("""
No config file found, using default configuration
************* Module py3k_error_flag
Explicit return in __init__
""")
self._test_output([module, "-E", "--msg-template='{msg}'"],
expected_output=expected)
# Test what gets emitted with -E --py3k
expected = textwrap.dedent("""
No config file found, using default configuration
************* Module py3k_error_flag
Use raise ErrorClass(args) instead of raise ErrorClass, args.
""")
self._test_output([module, "-E", "--py3k", "--msg-template='{msg}'"],
expected_output=expected)
# Test what gets emitted with --py3k -E
self._test_output([module, "--py3k", "-E", "--msg-template='{msg}'"],
expected_output=expected)
def test_abbreviations_are_not_supported(self):
expected = "no such option: --load-plugin"
self._test_output([".", "--load-plugin"], expected_output=expected)
def test_enable_all_works(self):
module = join(HERE, 'data', 'clientmodule_test.py')
expected = textwrap.dedent("""
No config file found, using default configuration
************* Module data.clientmodule_test
W: 10, 8: Unused variable 'local_variable' (unused-variable)
C: 18, 4: Missing method docstring (missing-docstring)
C: 22, 0: Missing class docstring (missing-docstring)
""")
self._test_output([module, "--disable=all", "--enable=all", "-rn"],
expected_output=expected)
def test_html_crash_report(self):
out = six.StringIO()
module = join(HERE, 'regrtest_data', 'html_crash_420.py')
self._runtest([module], code=16, reporter=HTMLReporter(out))
def test_wrong_import_position_when_others_disabled(self):
expected_output = textwrap.dedent('''
No config file found, using default configuration
************* Module wrong_import_position
C: 11, 0: Import "import os" should be placed at the top of the module (wrong-import-position)
''')
module1 = join(HERE, 'regrtest_data', 'import_something.py')
module2 = join(HERE, 'regrtest_data', 'wrong_import_position.py')
args = [module2, module1,
"--disable=all", "--enable=wrong-import-position",
"-rn", "-sn"]
out = six.StringIO()
self._run_pylint(args, out=out)
actual_output = out.getvalue()
self.assertEqual(expected_output.strip(), actual_output.strip())
def test_import_itself_not_accounted_for_relative_imports(self):
expected = 'No config file found, using default configuration'
package = join(HERE, 'regrtest_data', 'dummy')
self._test_output([package, '--disable=locally-disabled', '-rn'],
expected_output=expected)
def test_reject_empty_indent_strings(self):
expected = "indent string can't be empty"
module = join(HERE, 'data', 'clientmodule_test.py')
self._test_output([module, '--indent-string='],
expected_output=expected)
def test_json_report_when_file_has_syntax_error(self):
out = six.StringIO()
module = join(HERE, 'regrtest_data', 'syntax_error.py')
self._runtest([module], code=2, reporter=JSONReporter(out))
output = json.loads(out.getvalue())
self.assertIsInstance(output, list)
self.assertEqual(len(output), 1)
self.assertIsInstance(output[0], dict)
expected = {
"obj": "",
"column": 0,
"line": 1,
"type": "error",
"symbol": "syntax-error",
"module": "syntax_error"
}
message = output[0]
for key, value in expected.items():
self.assertIn(key, message)
self.assertEqual(message[key], value)
self.assertIn('invalid syntax', message['message'].lower())
def test_json_report_when_file_is_missing(self):
out = six.StringIO()
module = join(HERE, 'regrtest_data', 'totally_missing.py')
self._runtest([module], code=1, reporter=JSONReporter(out))
output = json.loads(out.getvalue())
self.assertIsInstance(output, list)
self.assertEqual(len(output), 1)
self.assertIsInstance(output[0], dict)
expected = {
"obj": "",
"column": 0,
"line": 1,
"type": "fatal",
"symbol": "fatal",
"module": module
}
message = output[0]
for key, value in expected.items():
self.assertIn(key, message)
self.assertEqual(message[key], value)
self.assertTrue(message['message'].startswith("No module named"))
def test_information_category_disabled_by_default(self):
expected = 'No config file found, using default configuration'
path = join(HERE, 'regrtest_data', 'meta.py')
self._test_output([path], expected_output=expected)
def test_error_mode_shows_no_score(self):
expected_output = textwrap.dedent('''
No config file found, using default configuration
************* Module application_crash
E: 1, 6: Undefined variable 'something_undefined' (undefined-variable)
''')
module = join(HERE, 'regrtest_data', 'application_crash.py')
self._test_output([module, "-E"], expected_output=expected_output)
def test_evaluation_score_shown_by_default(self):
expected_output = 'Your code has been rated at -60.00/10'
module = join(HERE, 'regrtest_data', 'application_crash.py')
self._test_output([module], expected_output=expected_output)
def test_confidence_levels(self):
expected = 'No config file found, using default configuration'
path = join(HERE, 'regrtest_data', 'meta.py')
self._test_output([path, "--confidence=HIGH,INFERENCE"],
expected_output=expected)
def test_bom_marker(self):
path = join(HERE, 'regrtest_data', 'meta.py')
config_path = join(HERE, 'regrtest_data', '.pylintrc')
expected = 'Your code has been rated at 10.00/10'
self._test_output([path, "--rcfile=%s" % config_path, "-rn"],
expected_output=expected)
if __name__ == '__main__':
unittest.main()
|