You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
7.2KB

  1. import os
  2. from pathlib import Path
  3. from typing import Dict
  4. from typing import Iterable
  5. from typing import List
  6. from typing import Optional
  7. from typing import Sequence
  8. from typing import Tuple
  9. from typing import TYPE_CHECKING
  10. from typing import Union
  11. import iniconfig
  12. from .exceptions import UsageError
  13. from _pytest.outcomes import fail
  14. from _pytest.pathlib import absolutepath
  15. from _pytest.pathlib import commonpath
  16. if TYPE_CHECKING:
  17. from . import Config
  18. def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
  19. """Parse the given generic '.ini' file using legacy IniConfig parser, returning
  20. the parsed object.
  21. Raise UsageError if the file cannot be parsed.
  22. """
  23. try:
  24. return iniconfig.IniConfig(str(path))
  25. except iniconfig.ParseError as exc:
  26. raise UsageError(str(exc)) from exc
  27. def load_config_dict_from_file(
  28. filepath: Path,
  29. ) -> Optional[Dict[str, Union[str, List[str]]]]:
  30. """Load pytest configuration from the given file path, if supported.
  31. Return None if the file does not contain valid pytest configuration.
  32. """
  33. # Configuration from ini files are obtained from the [pytest] section, if present.
  34. if filepath.suffix == ".ini":
  35. iniconfig = _parse_ini_config(filepath)
  36. if "pytest" in iniconfig:
  37. return dict(iniconfig["pytest"].items())
  38. else:
  39. # "pytest.ini" files are always the source of configuration, even if empty.
  40. if filepath.name == "pytest.ini":
  41. return {}
  42. # '.cfg' files are considered if they contain a "[tool:pytest]" section.
  43. elif filepath.suffix == ".cfg":
  44. iniconfig = _parse_ini_config(filepath)
  45. if "tool:pytest" in iniconfig.sections:
  46. return dict(iniconfig["tool:pytest"].items())
  47. elif "pytest" in iniconfig.sections:
  48. # If a setup.cfg contains a "[pytest]" section, we raise a failure to indicate users that
  49. # plain "[pytest]" sections in setup.cfg files is no longer supported (#3086).
  50. fail(CFG_PYTEST_SECTION.format(filename="setup.cfg"), pytrace=False)
  51. # '.toml' files are considered if they contain a [tool.pytest.ini_options] table.
  52. elif filepath.suffix == ".toml":
  53. import toml
  54. config = toml.load(str(filepath))
  55. result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
  56. if result is not None:
  57. # TOML supports richer data types than ini files (strings, arrays, floats, ints, etc),
  58. # however we need to convert all scalar values to str for compatibility with the rest
  59. # of the configuration system, which expects strings only.
  60. def make_scalar(v: object) -> Union[str, List[str]]:
  61. return v if isinstance(v, list) else str(v)
  62. return {k: make_scalar(v) for k, v in result.items()}
  63. return None
  64. def locate_config(
  65. args: Iterable[Path],
  66. ) -> Tuple[
  67. Optional[Path], Optional[Path], Dict[str, Union[str, List[str]]],
  68. ]:
  69. """Search in the list of arguments for a valid ini-file for pytest,
  70. and return a tuple of (rootdir, inifile, cfg-dict)."""
  71. config_names = [
  72. "pytest.ini",
  73. "pyproject.toml",
  74. "tox.ini",
  75. "setup.cfg",
  76. ]
  77. args = [x for x in args if not str(x).startswith("-")]
  78. if not args:
  79. args = [Path.cwd()]
  80. for arg in args:
  81. argpath = absolutepath(arg)
  82. for base in (argpath, *argpath.parents):
  83. for config_name in config_names:
  84. p = base / config_name
  85. if p.is_file():
  86. ini_config = load_config_dict_from_file(p)
  87. if ini_config is not None:
  88. return base, p, ini_config
  89. return None, None, {}
  90. def get_common_ancestor(paths: Iterable[Path]) -> Path:
  91. common_ancestor: Optional[Path] = None
  92. for path in paths:
  93. if not path.exists():
  94. continue
  95. if common_ancestor is None:
  96. common_ancestor = path
  97. else:
  98. if common_ancestor in path.parents or path == common_ancestor:
  99. continue
  100. elif path in common_ancestor.parents:
  101. common_ancestor = path
  102. else:
  103. shared = commonpath(path, common_ancestor)
  104. if shared is not None:
  105. common_ancestor = shared
  106. if common_ancestor is None:
  107. common_ancestor = Path.cwd()
  108. elif common_ancestor.is_file():
  109. common_ancestor = common_ancestor.parent
  110. return common_ancestor
  111. def get_dirs_from_args(args: Iterable[str]) -> List[Path]:
  112. def is_option(x: str) -> bool:
  113. return x.startswith("-")
  114. def get_file_part_from_node_id(x: str) -> str:
  115. return x.split("::")[0]
  116. def get_dir_from_path(path: Path) -> Path:
  117. if path.is_dir():
  118. return path
  119. return path.parent
  120. def safe_exists(path: Path) -> bool:
  121. # This can throw on paths that contain characters unrepresentable at the OS level,
  122. # or with invalid syntax on Windows (https://bugs.python.org/issue35306)
  123. try:
  124. return path.exists()
  125. except OSError:
  126. return False
  127. # These look like paths but may not exist
  128. possible_paths = (
  129. absolutepath(get_file_part_from_node_id(arg))
  130. for arg in args
  131. if not is_option(arg)
  132. )
  133. return [get_dir_from_path(path) for path in possible_paths if safe_exists(path)]
  134. CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."
  135. def determine_setup(
  136. inifile: Optional[str],
  137. args: Sequence[str],
  138. rootdir_cmd_arg: Optional[str] = None,
  139. config: Optional["Config"] = None,
  140. ) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]:
  141. rootdir = None
  142. dirs = get_dirs_from_args(args)
  143. if inifile:
  144. inipath_ = absolutepath(inifile)
  145. inipath: Optional[Path] = inipath_
  146. inicfg = load_config_dict_from_file(inipath_) or {}
  147. if rootdir_cmd_arg is None:
  148. rootdir = get_common_ancestor(dirs)
  149. else:
  150. ancestor = get_common_ancestor(dirs)
  151. rootdir, inipath, inicfg = locate_config([ancestor])
  152. if rootdir is None and rootdir_cmd_arg is None:
  153. for possible_rootdir in (ancestor, *ancestor.parents):
  154. if (possible_rootdir / "setup.py").is_file():
  155. rootdir = possible_rootdir
  156. break
  157. else:
  158. if dirs != [ancestor]:
  159. rootdir, inipath, inicfg = locate_config(dirs)
  160. if rootdir is None:
  161. if config is not None:
  162. cwd = config.invocation_params.dir
  163. else:
  164. cwd = Path.cwd()
  165. rootdir = get_common_ancestor([cwd, ancestor])
  166. is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
  167. if is_fs_root:
  168. rootdir = ancestor
  169. if rootdir_cmd_arg:
  170. rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
  171. if not rootdir.is_dir():
  172. raise UsageError(
  173. "Directory '{}' not found. Check your '--rootdir' option.".format(
  174. rootdir
  175. )
  176. )
  177. assert rootdir is not None
  178. return rootdir, inipath, inicfg or {}