Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_self.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  3. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  4. # Copyright (c) 2014 Vlad Temian <vladtemian@gmail.com>
  5. # Copyright (c) 2014 Google, Inc.
  6. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  7. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  8. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  9. # Copyright (c) 2016 Moises Lopez <moylop260@vauxoo.com>
  10. # Copyright (c) 2017 hippo91 <guillaume.peillex@gmail.com>
  11. # Copyright (c) 2017 Daniel Miller <millerdev@gmail.com>
  12. # Copyright (c) 2017 Bryce Guinta <bryce.paul.guinta@gmail.com>
  13. # Copyright (c) 2017 Thomas Hisch <t.hisch@gmail.com>
  14. # Copyright (c) 2017 Ville Skyttä <ville.skytta@iki.fi>
  15. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  16. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  17. import contextlib
  18. import json
  19. import re
  20. import sys
  21. import os
  22. from os.path import join, dirname, abspath
  23. import tempfile
  24. import textwrap
  25. import six
  26. from pylint.lint import Run
  27. from pylint.reporters import BaseReporter
  28. from pylint.reporters.text import *
  29. from pylint.reporters.json import JSONReporter
  30. import pytest
  31. from pylint import utils
  32. HERE = abspath(dirname(__file__))
  33. @contextlib.contextmanager
  34. def _patch_streams(out):
  35. sys.stderr = sys.stdout = out
  36. try:
  37. yield
  38. finally:
  39. sys.stderr = sys.__stderr__
  40. sys.stdout = sys.__stdout__
  41. @contextlib.contextmanager
  42. def _configure_lc_ctype(lc_ctype):
  43. lc_ctype_env = 'LC_CTYPE'
  44. original_lctype = os.environ.get(lc_ctype_env)
  45. os.environ[lc_ctype_env] = lc_ctype
  46. try:
  47. yield
  48. finally:
  49. os.environ.pop(lc_ctype_env)
  50. if original_lctype:
  51. os.environ[lc_ctype_env] = original_lctype
  52. class MultiReporter(BaseReporter):
  53. def __init__(self, reporters):
  54. self._reporters = reporters
  55. self.path_strip_prefix = os.getcwd() + os.sep
  56. def on_set_current_module(self, *args, **kwargs):
  57. for rep in self._reporters:
  58. rep.on_set_current_module(*args, **kwargs)
  59. def handle_message(self, msg):
  60. for rep in self._reporters:
  61. rep.handle_message(msg)
  62. def display_reports(self, layout):
  63. pass
  64. @property
  65. def out(self):
  66. return self._reporters[0].out
  67. @property
  68. def linter(self):
  69. return self._linter
  70. @linter.setter
  71. def linter(self, value):
  72. self._linter = value
  73. for rep in self._reporters:
  74. rep.linter = value
  75. class TestRunTC(object):
  76. def _runtest(self, args, reporter=None, out=None, code=28):
  77. if out is None:
  78. out = six.StringIO()
  79. pylint_code = self._run_pylint(args, reporter=reporter, out=out)
  80. if reporter:
  81. output = reporter.out.getvalue()
  82. elif hasattr(out, 'getvalue'):
  83. output = out.getvalue()
  84. else:
  85. output = None
  86. msg = 'expected output status %s, got %s' % (code, pylint_code)
  87. if output is not None:
  88. msg = '%s. Below pylint output: \n%s' % (msg, output)
  89. assert pylint_code == code, msg
  90. def _run_pylint(self, args, out, reporter=None):
  91. args = args + ['--persistent=no']
  92. with _patch_streams(out):
  93. with pytest.raises(SystemExit) as cm:
  94. with warnings.catch_warnings():
  95. warnings.simplefilter("ignore")
  96. Run(args, reporter=reporter)
  97. return cm.value.code
  98. def _test_output(self, args, expected_output):
  99. out = six.StringIO()
  100. self._run_pylint(args, out=out)
  101. actual_output = out.getvalue()
  102. assert expected_output.strip() in actual_output.strip()
  103. def test_pkginfo(self):
  104. """Make pylint check itself."""
  105. self._runtest(['pylint.__pkginfo__'], reporter=TextReporter(six.StringIO()),
  106. code=0)
  107. def test_all(self):
  108. """Make pylint check itself."""
  109. reporters = [
  110. TextReporter(six.StringIO()),
  111. ColorizedTextReporter(six.StringIO()),
  112. JSONReporter(six.StringIO())
  113. ]
  114. self._runtest(['pylint/test/functional/arguments.py'],
  115. reporter=MultiReporter(reporters), code=1)
  116. def test_no_ext_file(self):
  117. self._runtest([join(HERE, 'input', 'noext')], code=0)
  118. def test_w0704_ignored(self):
  119. self._runtest([join(HERE, 'input', 'ignore_except_pass_by_default.py')], code=0)
  120. def test_generate_config_option(self):
  121. self._runtest(['--generate-rcfile'], code=0)
  122. def test_generate_config_option_order(self):
  123. out1 = six.StringIO()
  124. out2 = six.StringIO()
  125. self._runtest(['--generate-rcfile'], code=0, out=out1)
  126. self._runtest(['--generate-rcfile'], code=0, out=out2)
  127. output1 = out1.getvalue()
  128. output2 = out2.getvalue()
  129. assert output1 == output2
  130. def test_generate_config_disable_symbolic_names(self):
  131. # Test that --generate-rcfile puts symbolic names in the --disable
  132. # option.
  133. out = six.StringIO()
  134. self._run_pylint(["--generate-rcfile", "--rcfile="], out=out)
  135. output = out.getvalue()
  136. # Get rid of the pesky messages that pylint emits if the
  137. # configuration file is not found.
  138. master = re.search(r"\[MASTER", output)
  139. out = six.StringIO(output[master.start():])
  140. parser = six.moves.configparser.RawConfigParser()
  141. parser.readfp(out)
  142. messages = utils._splitstrip(parser.get('MESSAGES CONTROL', 'disable'))
  143. assert 'suppressed-message' in messages
  144. def test_generate_rcfile_no_obsolete_methods(self):
  145. out = six.StringIO()
  146. self._run_pylint(["--generate-rcfile"], out=out)
  147. output = out.getvalue()
  148. assert "profile" not in output
  149. def test_inexisting_rcfile(self):
  150. out = six.StringIO()
  151. with pytest.raises(IOError) as excinfo:
  152. self._run_pylint(["--rcfile=/tmp/norcfile.txt"], out=out)
  153. assert "The config file /tmp/norcfile.txt doesn't exist!" == str(excinfo.value)
  154. def test_help_message_option(self):
  155. self._runtest(['--help-msg', 'W0101'], code=0)
  156. def test_error_help_message_option(self):
  157. self._runtest(['--help-msg', 'WX101'], code=0)
  158. def test_error_missing_arguments(self):
  159. self._runtest([], code=32)
  160. def test_no_out_encoding(self):
  161. """test redirection of stdout with non ascii caracters
  162. """
  163. #This test reproduces bug #48066 ; it happens when stdout is redirected
  164. # through '>' : the sys.stdout.encoding becomes then None, and if the
  165. # output contains non ascii, pylint will crash
  166. if sys.version_info < (3, 0):
  167. strio = tempfile.TemporaryFile()
  168. else:
  169. strio = six.StringIO()
  170. assert strio.encoding is None
  171. self._runtest([join(HERE, 'regrtest_data/no_stdout_encoding.py'),
  172. '--enable=all'],
  173. out=strio, code=28)
  174. def test_parallel_execution(self):
  175. self._runtest(['-j 2', 'pylint/test/functional/arguments.py',
  176. 'pylint/test/functional/bad_continuation.py'], code=1)
  177. def test_parallel_execution_missing_arguments(self):
  178. self._runtest(['-j 2', 'not_here', 'not_here_too'], code=1)
  179. def test_py3k_option(self):
  180. # Test that --py3k flag works.
  181. rc_code = 2 if six.PY2 else 0
  182. self._runtest([join(HERE, 'functional', 'unpacked_exceptions.py'),
  183. '--py3k'],
  184. code=rc_code)
  185. def test_py3k_jobs_option(self):
  186. rc_code = 2 if six.PY2 else 0
  187. self._runtest([join(HERE, 'functional', 'unpacked_exceptions.py'),
  188. '--py3k', '-j 2'],
  189. code=rc_code)
  190. @pytest.mark.skipif(sys.version_info[0] > 2, reason="Requires the --py3k flag.")
  191. def test_py3k_commutative_with_errors_only(self):
  192. # Test what gets emitted with -E only
  193. module = join(HERE, 'regrtest_data', 'py3k_error_flag.py')
  194. expected = textwrap.dedent("""
  195. ************* Module py3k_error_flag
  196. Explicit return in __init__
  197. """)
  198. self._test_output([module, "-E", "--msg-template='{msg}'"],
  199. expected_output=expected)
  200. # Test what gets emitted with -E --py3k
  201. expected = textwrap.dedent("""
  202. ************* Module py3k_error_flag
  203. Use raise ErrorClass(args) instead of raise ErrorClass, args.
  204. """)
  205. self._test_output([module, "-E", "--py3k", "--msg-template='{msg}'"],
  206. expected_output=expected)
  207. # Test what gets emitted with --py3k -E
  208. self._test_output([module, "--py3k", "-E", "--msg-template='{msg}'"],
  209. expected_output=expected)
  210. @pytest.mark.skipif(sys.version_info[0] > 2, reason="Requires the --py3k flag.")
  211. def test_py3k_commutative_with_config_disable(self):
  212. module = join(HERE, 'regrtest_data', 'py3k_errors_and_warnings.py')
  213. rcfile = join(HERE, 'regrtest_data', 'py3k-disabled.rc')
  214. cmd = [module, "--msg-template='{msg}'", "--reports=n"]
  215. expected = textwrap.dedent("""
  216. ************* Module py3k_errors_and_warnings
  217. import missing `from __future__ import absolute_import`
  218. Use raise ErrorClass(args) instead of raise ErrorClass, args.
  219. Calling a dict.iter*() method
  220. print statement used
  221. """)
  222. self._test_output(cmd + ["--py3k"], expected_output=expected)
  223. expected = textwrap.dedent("""
  224. ************* Module py3k_errors_and_warnings
  225. Use raise ErrorClass(args) instead of raise ErrorClass, args.
  226. Calling a dict.iter*() method
  227. print statement used
  228. """)
  229. self._test_output(cmd + ["--py3k", "--rcfile", rcfile],
  230. expected_output=expected)
  231. expected = textwrap.dedent("""
  232. ************* Module py3k_errors_and_warnings
  233. Use raise ErrorClass(args) instead of raise ErrorClass, args.
  234. print statement used
  235. """)
  236. self._test_output(cmd + ["--py3k", "-E", "--rcfile", rcfile],
  237. expected_output=expected)
  238. self._test_output(cmd + ["-E", "--py3k", "--rcfile", rcfile],
  239. expected_output=expected)
  240. def test_abbreviations_are_not_supported(self):
  241. expected = "no such option: --load-plugin"
  242. self._test_output([".", "--load-plugin"], expected_output=expected)
  243. def test_enable_all_works(self):
  244. module = join(HERE, 'data', 'clientmodule_test.py')
  245. expected = textwrap.dedent("""
  246. ************* Module data.clientmodule_test
  247. W: 10, 8: Unused variable 'local_variable' (unused-variable)
  248. C: 18, 4: Missing method docstring (missing-docstring)
  249. C: 22, 0: Missing class docstring (missing-docstring)
  250. """)
  251. self._test_output([module, "--disable=all", "--enable=all", "-rn"],
  252. expected_output=expected)
  253. def test_wrong_import_position_when_others_disabled(self):
  254. expected_output = textwrap.dedent('''
  255. ************* Module wrong_import_position
  256. C: 11, 0: Import "import os" should be placed at the top of the module (wrong-import-position)
  257. ''')
  258. module1 = join(HERE, 'regrtest_data', 'import_something.py')
  259. module2 = join(HERE, 'regrtest_data', 'wrong_import_position.py')
  260. args = [module2, module1,
  261. "--disable=all", "--enable=wrong-import-position",
  262. "-rn", "-sn"]
  263. out = six.StringIO()
  264. self._run_pylint(args, out=out)
  265. actual_output = out.getvalue().strip()
  266. to_remove = "No config file found, using default configuration"
  267. if to_remove in actual_output:
  268. actual_output = actual_output[len(to_remove):]
  269. if actual_output.startswith("Using config file "):
  270. # If ~/.pylintrc is present remove the
  271. # Using config file... line
  272. actual_output = actual_output[actual_output.find("\n"):]
  273. assert expected_output.strip() == actual_output.strip()
  274. def test_import_itself_not_accounted_for_relative_imports(self):
  275. expected = 'Your code has been rated at 10.00/10'
  276. package = join(HERE, 'regrtest_data', 'dummy')
  277. self._test_output([package, '--disable=locally-disabled', '-rn'],
  278. expected_output=expected)
  279. def test_reject_empty_indent_strings(self):
  280. expected = "indent string can't be empty"
  281. module = join(HERE, 'data', 'clientmodule_test.py')
  282. self._test_output([module, '--indent-string='],
  283. expected_output=expected)
  284. def test_json_report_when_file_has_syntax_error(self):
  285. out = six.StringIO()
  286. module = join(HERE, 'regrtest_data', 'syntax_error.py')
  287. self._runtest([module], code=2, reporter=JSONReporter(out))
  288. output = json.loads(out.getvalue())
  289. assert isinstance(output, list)
  290. assert len(output) == 1
  291. assert isinstance(output[0], dict)
  292. expected = {
  293. "obj": "",
  294. "column": 0,
  295. "line": 1,
  296. "type": "error",
  297. "symbol": "syntax-error",
  298. "module": "syntax_error"
  299. }
  300. message = output[0]
  301. for key, value in expected.items():
  302. assert key in message
  303. assert message[key] == value
  304. assert 'invalid syntax' in message['message'].lower()
  305. def test_json_report_when_file_is_missing(self):
  306. out = six.StringIO()
  307. module = join(HERE, 'regrtest_data', 'totally_missing.py')
  308. self._runtest([module], code=1, reporter=JSONReporter(out))
  309. output = json.loads(out.getvalue())
  310. assert isinstance(output, list)
  311. assert len(output) == 1
  312. assert isinstance(output[0], dict)
  313. expected = {
  314. "obj": "",
  315. "column": 0,
  316. "line": 1,
  317. "type": "fatal",
  318. "symbol": "fatal",
  319. "module": module
  320. }
  321. message = output[0]
  322. for key, value in expected.items():
  323. assert key in message
  324. assert message[key] == value
  325. assert message['message'].startswith("No module named")
  326. def test_information_category_disabled_by_default(self):
  327. expected = 'Your code has been rated at 10.00/10'
  328. path = join(HERE, 'regrtest_data', 'meta.py')
  329. self._test_output([path], expected_output=expected)
  330. def test_error_mode_shows_no_score(self):
  331. expected_output = textwrap.dedent('''
  332. ************* Module application_crash
  333. E: 1, 6: Undefined variable 'something_undefined' (undefined-variable)
  334. ''')
  335. module = join(HERE, 'regrtest_data', 'application_crash.py')
  336. self._test_output([module, "-E"], expected_output=expected_output)
  337. def test_evaluation_score_shown_by_default(self):
  338. expected_output = 'Your code has been rated at '
  339. module = join(HERE, 'regrtest_data', 'application_crash.py')
  340. self._test_output([module], expected_output=expected_output)
  341. def test_confidence_levels(self):
  342. expected = 'Your code has been rated at'
  343. path = join(HERE, 'regrtest_data', 'meta.py')
  344. self._test_output([path, "--confidence=HIGH,INFERENCE"],
  345. expected_output=expected)
  346. def test_bom_marker(self):
  347. path = join(HERE, 'regrtest_data', 'meta.py')
  348. config_path = join(HERE, 'regrtest_data', '.pylintrc')
  349. expected = 'Your code has been rated at 10.00/10'
  350. self._test_output([path, "--rcfile=%s" % config_path, "-rn"],
  351. expected_output=expected)
  352. def test_pylintrc_plugin_duplicate_options(self):
  353. dummy_plugin_path = join(HERE, 'regrtest_data', 'dummy_plugin')
  354. # Enable --load-plugins=dummy_plugin
  355. sys.path.append(dummy_plugin_path)
  356. config_path = join(HERE, 'regrtest_data', 'dummy_plugin.rc')
  357. expected = (
  358. ":dummy-message-01 (I9061): *Dummy short desc 01*\n"
  359. " Dummy long desc This message belongs to the dummy_plugin checker.\n\n"
  360. ":dummy-message-02 (I9060): *Dummy short desc 02*\n"
  361. " Dummy long desc This message belongs to the dummy_plugin checker.")
  362. self._test_output(["--rcfile=%s" % config_path,
  363. "--help-msg=dummy-message-01,dummy-message-02"],
  364. expected_output=expected)
  365. expected = (
  366. "[DUMMY_PLUGIN]\n\n# Dummy option 1\ndummy_option_1=dummy value 1\n\n"
  367. "# Dummy option 2\ndummy_option_2=dummy value 2")
  368. self._test_output(["--rcfile=%s" % config_path, "--generate-rcfile"],
  369. expected_output=expected)
  370. sys.path.remove(dummy_plugin_path)
  371. def test_pylintrc_comments_in_values(self):
  372. path = join(HERE, 'regrtest_data', 'test_pylintrc_comments.py')
  373. config_path = join(HERE, 'regrtest_data', 'comments_pylintrc')
  374. expected = textwrap.dedent('''
  375. ************* Module test_pylintrc_comments
  376. W: 2, 0: Bad indentation. Found 1 spaces, expected 4 (bad-indentation)
  377. C: 1, 0: Missing module docstring (missing-docstring)
  378. C: 1, 0: Missing function docstring (missing-docstring)
  379. ''')
  380. self._test_output([path, "--rcfile=%s" % config_path, "-rn"],
  381. expected_output=expected)
  382. def test_no_crash_with_formatting_regex_defaults(self):
  383. self._runtest(["--ignore-patterns=a"], reporter=TextReporter(six.StringIO()),
  384. code=32)
  385. def test_getdefaultencoding_crashes_with_lc_ctype_utf8(self):
  386. expected_output = textwrap.dedent('''
  387. ************* Module application_crash
  388. E: 1, 6: Undefined variable 'something_undefined' (undefined-variable)
  389. ''')
  390. module = join(HERE, 'regrtest_data', 'application_crash.py')
  391. with _configure_lc_ctype('UTF-8'):
  392. self._test_output([module, '-E'], expected_output=expected_output)