test_func.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import csv
  2. import os
  3. import pickle
  4. import sys
  5. from pathlib import Path
  6. import pylint
  7. import pytest
  8. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pylint_django.tests.settings")
  9. HERE = Path(__file__).parent
  10. try:
  11. # pylint 2.5: test_functional has been moved to pylint.testutils
  12. from pylint.testutils import FunctionalTestFile, LintModuleTest, lint_module_test
  13. if "test" not in csv.list_dialects():
  14. class test_dialect(csv.excel):
  15. delimiter = ":"
  16. lineterminator = "\n"
  17. csv.register_dialect("test", test_dialect)
  18. lint_module_test.PYLINTRC = HERE / "testing_pylint.rc"
  19. except (ImportError, AttributeError):
  20. # specify directly the directory containing test_functional.py
  21. test_functional_dir = os.getenv("PYLINT_TEST_FUNCTIONAL_DIR", "")
  22. # otherwise look for in in ~/pylint/tests - pylint 2.4
  23. # this is the pylint git checkout dir, not the pylint module dir
  24. if not os.path.isdir(test_functional_dir):
  25. test_functional_dir = os.path.join(os.getenv("HOME", "/home/travis"), "pylint", "tests")
  26. # or site-packages/pylint/test/ - pylint before 2.4
  27. if not os.path.isdir(test_functional_dir):
  28. test_functional_dir = os.path.join(os.path.dirname(pylint.__file__), "test")
  29. sys.path.append(test_functional_dir)
  30. from test_functional import FunctionalTestFile, LintModuleTest
  31. sys.path += [str(p.absolute()) for p in (HERE, HERE / "../../", HERE / "input")]
  32. class PylintDjangoLintModuleTest(LintModuleTest):
  33. """
  34. Only used so that we can load this plugin into the linter!
  35. """
  36. def __init__(self, test_file):
  37. # if hasattr(test_file, 'option_file') and test_file.option_file is None:
  38. super(PylintDjangoLintModuleTest, self).__init__(test_file)
  39. self._linter.load_plugin_modules(["pylint_django"])
  40. self._linter.load_plugin_configuration()
  41. class PylintDjangoMigrationsTest(PylintDjangoLintModuleTest):
  42. """
  43. Only used so that we can load
  44. pylint_django.checkers.migrations into the linter!
  45. """
  46. def __init__(self, test_file):
  47. super().__init__(test_file)
  48. self._linter.load_plugin_modules(["pylint_django.checkers.migrations"])
  49. self._linter.load_plugin_configuration()
  50. def get_tests(input_dir="input", sort=False):
  51. def _file_name(test):
  52. return test.base
  53. input_dir = HERE / input_dir
  54. suite = []
  55. for fname in input_dir.iterdir():
  56. if fname.name != "__init__.py" and fname.name.endswith(".py"):
  57. suite.append(FunctionalTestFile(str(input_dir.absolute()), str(fname.absolute())))
  58. # when testing the migrations plugin we need to sort by input file name
  59. # because the plugin reports the errors in close() which appends them to the
  60. # report for the last file in the list
  61. if sort:
  62. suite.sort(key=_file_name)
  63. return suite
  64. TESTS = get_tests()
  65. TESTS.extend(get_tests("input/models"))
  66. TESTS_NAMES = [t.base for t in TESTS]
  67. @pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
  68. def test_everything(test_file):
  69. # copied from pylint.tests.test_functional.test_functional
  70. LintTest = PylintDjangoLintModuleTest(test_file)
  71. LintTest.setUp()
  72. LintTest._runTest()
  73. # NOTE: define tests for the migrations checker!
  74. MIGRATIONS_TESTS = get_tests("input/migrations", True)
  75. MIGRATIONS_TESTS_NAMES = [t.base for t in MIGRATIONS_TESTS]
  76. @pytest.mark.parametrize("test_file", MIGRATIONS_TESTS, ids=MIGRATIONS_TESTS_NAMES)
  77. def test_migrations_plugin(test_file):
  78. LintTest = PylintDjangoMigrationsTest(test_file)
  79. LintTest.setUp()
  80. LintTest._runTest()
  81. @pytest.mark.parametrize("test_file", MIGRATIONS_TESTS[:1], ids=MIGRATIONS_TESTS_NAMES[:1])
  82. def test_linter_should_be_pickleable_with_pylint_django_plugin_installed(test_file):
  83. LintTest = PylintDjangoMigrationsTest(test_file)
  84. LintTest.setUp()
  85. # LintModuleTest sets reporter to instance of FunctionalTestReporter that is not picklable
  86. LintTest._linter.reporter = None
  87. pickle.dumps(LintTest._linter)
  88. if __name__ == "__main__":
  89. sys.exit(pytest.main(sys.argv))