/
test_func.py
91 lines (66 loc) · 2.83 KB
/
test_func.py
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
import os
import sys
import pytest
import pylint
if pylint.__version__ >= '2.4':
# after version 2.4 pylint stopped shipping the test directory
# as part of the package so we check it out locally for testing
sys.path.append(os.getenv('HOME'), 'pylint', 'tests')
else:
# because there's no __init__ file in pylint/test/
sys.path.append(os.path.join(os.path.dirname(pylint.__file__), 'test'))
import test_functional # noqa: E402
# alter sys.path again because the tests now live as a subdirectory
# of pylint_django
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
# so we can find migrations
sys.path.append(os.path.join(os.path.dirname(__file__), 'input'))
class PylintDjangoLintModuleTest(test_functional.LintModuleTest):
"""
Only used so that we can load this plugin into the linter!
"""
def __init__(self, test_file):
super(PylintDjangoLintModuleTest, self).__init__(test_file)
self._linter.load_plugin_modules(['pylint_django'])
class PylintDjangoDbPerformanceTest(PylintDjangoLintModuleTest):
"""
Only used so that we can load
pylint_django.checkers.db_performance into the linter!
"""
def __init__(self, test_file):
super(PylintDjangoDbPerformanceTest, self).__init__(test_file)
self._linter.load_plugin_modules(['pylint_django.checkers.db_performance'])
def get_tests(input_dir='input', sort=False):
def _file_name(test):
return test.base
HERE = os.path.dirname(os.path.abspath(__file__))
input_dir = os.path.join(HERE, input_dir)
suite = []
for fname in os.listdir(input_dir):
if fname != '__init__.py' and fname.endswith('.py'):
suite.append(test_functional.FunctionalTestFile(input_dir, fname))
# when testing the db_performance plugin we need to sort by input file name
# because the plugin reports the errors in close() which appends them to the
# report for the last file in the list
if sort:
suite.sort(key=_file_name)
return suite
TESTS = get_tests()
TESTS.extend(get_tests('input/models'))
TESTS_NAMES = [t.base for t in TESTS]
@pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
def test_everything(test_file):
# copied from pylint.tests.test_functional.test_functional
LintTest = PylintDjangoLintModuleTest(test_file)
LintTest.setUp()
LintTest._runTest()
# NOTE: define tests for the db_performance checker!
DB_PERFORMANCE_TESTS = get_tests('input/migrations', True)
DB_PERFORMANCE_TESTS_NAMES = [t.base for t in DB_PERFORMANCE_TESTS]
@pytest.mark.parametrize("test_file", DB_PERFORMANCE_TESTS, ids=DB_PERFORMANCE_TESTS_NAMES)
def test_db_performance_plugin(test_file):
LintTest = PylintDjangoDbPerformanceTest(test_file)
LintTest.setUp()
LintTest._runTest()
if __name__ == '__main__':
sys.exit(pytest.main(sys.argv))