Skip to content

Commit ba40117

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 7860780 commit ba40117

File tree

1 file changed

+30
-56
lines changed

1 file changed

+30
-56
lines changed

test/test_func.py

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,47 @@
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
7-
from pylint_django.compat import django_version
8-
9-
10-
settings.configure()
11-
12-
13-
HERE = os.path.dirname(os.path.abspath(__file__))
4+
import pytest
145

6+
import pylint
7+
# because there's no __init__ file in pylint/test/
8+
sys.path.append(os.path.join(os.path.dirname(pylint.__file__), 'test'))
9+
import test_functional
1510

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',))
22-
23-
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-
}
28-
11+
from pylint_django.compat import django_version
2912

30-
def module_exists(module_name):
31-
try:
32-
__import__(module_name)
33-
except ImportError:
34-
return False
35-
else:
36-
return True
3713

14+
class PylintDjangoLintModuleTest(test_functional.LintModuleTest):
15+
"""
16+
Only used so that we can load this plugin into the linter!
17+
"""
18+
def __init__(self, test_file):
19+
super(PylintDjangoLintModuleTest, self).__init__(test_file)
20+
self._linter.load_plugin_modules(['pylint_django'])
3821

39-
def tests(input_dir, messages_dir):
40-
callbacks = [cb_test_gen(LintTestUsingFile)]
4122

42-
input_dir = os.path.join(HERE, input_dir)
43-
messages_dir = os.path.join(HERE, messages_dir)
23+
def get_tests():
24+
HERE = os.path.dirname(os.path.abspath(__file__))
25+
input_dir = os.path.join(HERE, 'input')
4426

45-
# first tests which pass for all Django versions
46-
tests = make_tests(input_dir, messages_dir, None, callbacks)
27+
suite = []
28+
for fname in os.listdir(input_dir):
29+
if fname != '__init__.py' and fname.endswith('.py'):
30+
suite.append(test_functional.FunctionalTestFile(input_dir, fname))
31+
return suite
4732

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)
5833

59-
tests += make_tests(os.path.join(input_dir, 'versions'), messages_dir, filter_rgx, callbacks)
60-
return tests
34+
TESTS = get_tests()
35+
TESTS_NAMES = [t.base for t in TESTS]
6136

6237

63-
def suite():
64-
test_list = tests('input', 'messages')
65-
66-
if module_exists('rest_framework'):
67-
test_list += tests('external_drf', '')
38+
@pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
39+
def test_everything(test_file):
40+
# copied from pylint.tests.test_functional.test_functional
41+
LintTest = PylintDjangoLintModuleTest(test_file)
42+
LintTest.setUp()
43+
LintTest._runTest()
6844

69-
return unittest.TestSuite([unittest.makeSuite(test, suiteClass=unittest.TestSuite)
70-
for test in test_list])
7145

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

0 commit comments

Comments
 (0)