Skip to content

Commit

Permalink
Update the way we load tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
atodorov committed Jan 18, 2018
1 parent fa083d0 commit 533b66c
Showing 1 changed file with 30 additions and 56 deletions.
86 changes: 30 additions & 56 deletions test/test_func.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,47 @@

import os
import sys
import unittest
from django.conf import settings
from pylint.testutils import make_tests, LintTestUsingFile, cb_test_gen, linter
from pylint_django.compat import django_version


settings.configure()


HERE = os.path.dirname(os.path.abspath(__file__))
import pytest

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

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


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

from pylint_django.compat import django_version

def module_exists(module_name):
try:
__import__(module_name)
except ImportError:
return False
else:
return True

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'])

def tests(input_dir, messages_dir):
callbacks = [cb_test_gen(LintTestUsingFile)]

input_dir = os.path.join(HERE, input_dir)
messages_dir = os.path.join(HERE, messages_dir)
def get_tests():
HERE = os.path.dirname(os.path.abspath(__file__))
input_dir = os.path.join(HERE, 'input')

# first tests which pass for all Django versions
tests = make_tests(input_dir, messages_dir, None, callbacks)
suite = []
for fname in os.listdir(input_dir):
if fname != '__init__.py' and fname.endswith('.py'):
suite.append(test_functional.FunctionalTestFile(input_dir, fname))
return suite

# now skip some tests test for specific versions - for example,
# _meta access should not work for django<1.8 but should run and
# pass for django 1.4 - skip the tests which will be checking
# a piece of functionality in pylint-django that should only
# in higher versions.
specific_tests = []
for test_name, version_range in SKIP_TESTS_FOR_DJANGO_VERSION:
if not version_range:
specific_tests.append(test_name)
filter_rgx = '(%s)' % '|'.join(specific_tests)

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


def suite():
test_list = tests('input', 'messages')

if module_exists('rest_framework'):
test_list += tests('external_drf', '')
@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()

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

if __name__ == '__main__':
unittest.main(defaultTest='suite')
sys.exit(pytest.main(sys.argv))

0 comments on commit 533b66c

Please sign in to comment.