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_func.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (c) 2006-2010, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2012 FELD Boris <lothiraldan@gmail.com>
  3. # Copyright (c) 2013-2014 Google, Inc.
  4. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  5. # Copyright (c) 2014 Michal Nowikowski <godfryd@gmail.com>
  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) 2017 Michka Popoff <michkapopoff@gmail.com>
  10. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  11. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  12. """functional/non regression tests for pylint"""
  13. import sys
  14. import re
  15. import pytest
  16. from os.path import abspath, dirname, join
  17. from pylint.testutils import _get_tests_info, linter
  18. PY3K = sys.version_info >= (3, 0)
  19. SYS_VERS_STR = '%d%d%d' % sys.version_info[:3]
  20. # Configure paths
  21. INPUT_DIR = join(dirname(abspath(__file__)), 'input')
  22. MSG_DIR = join(dirname(abspath(__file__)), 'messages')
  23. FILTER_RGX = None
  24. UPDATE = False
  25. INFO_TEST_RGX = re.compile(r'^func_i\d\d\d\d$')
  26. # Classes
  27. quote = "'" if sys.version_info >= (3, 3) else ''
  28. def exception_str(self, ex): # pylint: disable=unused-argument
  29. """function used to replace default __str__ method of exception instances"""
  30. return 'in %s\n:: %s' % (ex.file, ', '.join(ex.args))
  31. class LintTestUsingModule(object):
  32. INPUT_DIR = None
  33. DEFAULT_PACKAGE = 'input'
  34. package = DEFAULT_PACKAGE
  35. linter = linter
  36. module = None
  37. depends = None
  38. output = None
  39. _TEST_TYPE = 'module'
  40. # def runTest(self):
  41. # # This is a hack to make ./test/test_func.py work under pytest.
  42. # pass
  43. def _test_functionality(self):
  44. tocheck = [self.package+'.'+self.module]
  45. # pylint: disable=not-an-iterable; can't handle boolean checks for now
  46. if self.depends:
  47. tocheck += [self.package+'.%s' % name.replace('.py', '')
  48. for name, _ in self.depends]
  49. self._test(tocheck)
  50. def _check_result(self, got):
  51. assert self._get_expected().strip()+'\n' == got.strip()+'\n'
  52. def _test(self, tocheck):
  53. if INFO_TEST_RGX.match(self.module):
  54. self.linter.enable('I')
  55. else:
  56. self.linter.disable('I')
  57. try:
  58. self.linter.check(tocheck)
  59. except Exception as ex:
  60. # need finalization to restore a correct state
  61. self.linter.reporter.finalize()
  62. ex.file = tocheck
  63. print(ex)
  64. ex.__str__ = exception_str
  65. raise
  66. self._check_result(self.linter.reporter.finalize())
  67. def _has_output(self):
  68. return not self.module.startswith('func_noerror_')
  69. def _get_expected(self):
  70. if self._has_output() and self.output:
  71. with open(self.output, 'U') as fobj:
  72. return fobj.read().strip() + '\n'
  73. else:
  74. return ''
  75. class LintTestUpdate(LintTestUsingModule):
  76. _TEST_TYPE = 'update'
  77. def _check_result(self, got):
  78. if self._has_output():
  79. try:
  80. expected = self._get_expected()
  81. except IOError:
  82. expected = ''
  83. if got != expected:
  84. with open(self.output, 'w') as fobj:
  85. fobj.write(got)
  86. def gen_tests(filter_rgx):
  87. if filter_rgx:
  88. is_to_run = re.compile(filter_rgx).search
  89. else:
  90. is_to_run = lambda x: 1
  91. tests = []
  92. for module_file, messages_file in (
  93. _get_tests_info(INPUT_DIR, MSG_DIR, 'func_', '')
  94. ):
  95. if not is_to_run(module_file) or module_file.endswith(('.pyc', "$py.class")):
  96. continue
  97. base = module_file.replace('.py', '').split('_')[1]
  98. dependencies = _get_tests_info(INPUT_DIR, MSG_DIR, base, '.py')
  99. tests.append((module_file, messages_file, dependencies))
  100. if UPDATE:
  101. return tests
  102. assert len(tests) < 196, "Please do not add new test cases here."
  103. return tests
  104. @pytest.mark.parametrize("module_file,messages_file,dependencies", gen_tests(FILTER_RGX),
  105. ids=[o[0] for o in gen_tests(FILTER_RGX)])
  106. def test_functionality(module_file, messages_file, dependencies,):
  107. LT = LintTestUpdate() if UPDATE else LintTestUsingModule()
  108. LT.module = module_file.replace('.py', '')
  109. LT.output = messages_file
  110. LT.depends = dependencies or None
  111. LT.INPUT_DIR = INPUT_DIR
  112. LT._test_functionality()
  113. if __name__ == '__main__':
  114. if '-u' in sys.argv:
  115. UPDATE = True
  116. sys.argv.remove('-u')
  117. if len(sys.argv) > 1:
  118. FILTER_RGX = sys.argv[1]
  119. del sys.argv[1]
  120. pytest.main(sys.argv)