functional_test_file.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. import configparser
  4. from os.path import basename, exists, join
  5. def parse_python_version(ver_str):
  6. return tuple(int(digit) for digit in ver_str.split("."))
  7. class NoFileError(Exception):
  8. pass
  9. class FunctionalTestFile:
  10. """A single functional test case file with options."""
  11. _CONVERTERS = {
  12. "min_pyver": parse_python_version,
  13. "max_pyver": parse_python_version,
  14. "min_pyver_end_position": parse_python_version,
  15. "requires": lambda s: s.split(","),
  16. }
  17. def __init__(self, directory, filename):
  18. self._directory = directory
  19. self.base = filename.replace(".py", "")
  20. self.options = {
  21. "min_pyver": (2, 5),
  22. "max_pyver": (4, 0),
  23. "min_pyver_end_position": (3, 8),
  24. "requires": [],
  25. "except_implementations": [],
  26. "exclude_platforms": [],
  27. }
  28. self._parse_options()
  29. def __repr__(self):
  30. return f"FunctionalTest:{self.base}"
  31. def _parse_options(self):
  32. cp = configparser.ConfigParser()
  33. cp.add_section("testoptions")
  34. try:
  35. cp.read(self.option_file)
  36. except NoFileError:
  37. pass
  38. for name, value in cp.items("testoptions"):
  39. conv = self._CONVERTERS.get(name, lambda v: v)
  40. self.options[name] = conv(value)
  41. @property
  42. def option_file(self):
  43. return self._file_type(".rc")
  44. @property
  45. def module(self):
  46. package = basename(self._directory)
  47. return ".".join([package, self.base])
  48. @property
  49. def expected_output(self):
  50. return self._file_type(".txt", check_exists=False)
  51. @property
  52. def source(self):
  53. return self._file_type(".py")
  54. def _file_type(self, ext, check_exists=True):
  55. name = join(self._directory, self.base + ext)
  56. if not check_exists or exists(name):
  57. return name
  58. raise NoFileError(f"Cannot find '{name}'.")