Skip to content

Commit 889741a

Browse files
committed
Update the way we load tests
this mimics more closely what pylint does for their own tests and doesn't rely on deprecated functionality. Fixes pylint-dev#107 and pylint-dev#97 Obsoletes pylint-dev#98
1 parent 77f8d74 commit 889741a

File tree

1 file changed

+26
-57
lines changed

1 file changed

+26
-57
lines changed

test/test_func.py

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,42 @@
11

22
import os
33
import sys
4-
import unittest
5-
from django.conf import settings
6-
from pylint.testutils import make_tests, LintTestUsingFile, cb_test_gen, linter
4+
import pytest
5+
from pylint.test import test_functional
76
from pylint_django.compat import django_version
87

98

10-
settings.configure()
9+
class PylintDjangoLintModuleTest(test_functional.LintModuleTest):
10+
"""
11+
Only used so that we can load this plugin into the linter!
12+
"""
13+
def __init__(self, test_file):
14+
super().__init__(test_file)
15+
self._linter.load_plugin_modules(['pylint_django'])
1116

1217

13-
HERE = os.path.dirname(os.path.abspath(__file__))
18+
def get_tests():
19+
HERE = os.path.dirname(os.path.abspath(__file__))
20+
input_dir = os.path.join(HERE, 'input')
1421

22+
suite = []
23+
for fname in os.listdir(input_dir):
24+
if fname != '__init__.py' and fname.endswith('.py'):
25+
suite.append(test_functional.FunctionalTestFile(input_dir, fname))
26+
return suite
1527

16-
linter.load_plugin_modules(['pylint_django'])
17-
# Disable some things on Python2.6, since we use a different pylint version here
18-
# (1.3 on Python2.6, 1.4+ on later versions)
19-
if sys.version_info < (2, 7):
20-
linter.global_set_option('required-attributes', ())
21-
linter.global_set_option('disable', ('E0012',))
2228

29+
TESTS = get_tests()
30+
TESTS_NAMES = [t.base for t in TESTS]
2331

24-
SKIP_TESTS_FOR_DJANGO_VERSION = {
25-
# if the second value is False, skip the test, otherwise run it
26-
('func_noerror_protected_meta_access', django_version >= (1, 8)),
27-
}
2832

33+
@pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
34+
def test_everything(test_file):
35+
# copied from pylint.tests.test_functional.test_functional
36+
LintTest = PylintDjangoLintModuleTest(test_file)
37+
LintTest.setUp()
38+
LintTest._runTest()
2939

30-
def module_exists(module_name):
31-
try:
32-
__import__(module_name)
33-
except ImportError:
34-
return False
35-
else:
36-
return True
37-
38-
39-
def tests(input_dir, messages_dir):
40-
callbacks = [cb_test_gen(LintTestUsingFile)]
41-
42-
input_dir = os.path.join(HERE, input_dir)
43-
messages_dir = os.path.join(HERE, messages_dir)
44-
45-
# first tests which pass for all Django versions
46-
tests = make_tests(input_dir, messages_dir, None, callbacks)
47-
48-
# now skip some tests test for specific versions - for example,
49-
# _meta access should not work for django<1.8 but should run and
50-
# pass for django 1.4 - skip the tests which will be checking
51-
# a piece of functionality in pylint-django that should only
52-
# in higher versions.
53-
specific_tests = []
54-
for test_name, version_range in SKIP_TESTS_FOR_DJANGO_VERSION:
55-
if not version_range:
56-
specific_tests.append(test_name)
57-
filter_rgx = '(%s)' % '|'.join(specific_tests)
58-
59-
tests += make_tests(os.path.join(input_dir, 'versions'), messages_dir, filter_rgx, callbacks)
60-
return tests
61-
62-
63-
def suite():
64-
test_list = tests('input', 'messages')
65-
66-
if module_exists('rest_framework'):
67-
test_list += tests('external_drf', '')
68-
69-
return unittest.TestSuite([unittest.makeSuite(test, suiteClass=unittest.TestSuite)
70-
for test in test_list])
7140

7241
if __name__ == '__main__':
73-
unittest.main(defaultTest='suite')
42+
sys.exit(pytest.main(sys.argv))

0 commit comments

Comments
 (0)