findpaths.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 tomli
  54. toml_text = filepath.read_text(encoding="utf-8")
  55. try:
  56. config = tomli.loads(toml_text)
  57. except tomli.TOMLDecodeError as exc:
  58. raise UsageError(str(exc)) from exc
  59. result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
  60. if result is not None:
  61. # TOML supports richer data types than ini files (strings, arrays, floats, ints, etc),
  62. # however we need to convert all scalar values to str for compatibility with the rest
  63. # of the configuration system, which expects strings only.
  64. def make_scalar(v: object) -> Union[str, List[str]]:
  65. return v if isinstance(v, list) else str(v)
  66. return {k: make_scalar(v) for k, v in result.items()}
  67. return None
  68. def locate_config(
  69. args: Iterable[Path],
  70. ) -> Tuple[Optional[Path], Optional[Path], Dict[str, Union[str, List[str]]]]:
  71. """Search in the list of arguments for a valid ini-file for pytest,
  72. and return a tuple of (rootdir, inifile, cfg-dict)."""
  73. config_names = [
  74. "pytest.ini",
  75. "pyproject.toml",
  76. "tox.ini",
  77. "setup.cfg",
  78. ]
  79. args = [x for x in args if not str(x).startswith("-")]
  80. if not args:
  81. args = [Path.cwd()]
  82. for arg in args:
  83. argpath = absolutepath(arg)
  84. for base in (argpath, *argpath.parents):
  85. for config_name in config_names:
  86. p = base / config_name
  87. if p.is_file():
  88. ini_config = load_config_dict_from_file(p)
  89. if ini_config is not None:
  90. return base, p, ini_config
  91. return None, None, {}
  92. def get_common_ancestor(paths: Iterable[Path]) -> Path:
  93. common_ancestor: Optional[Path] = None
  94. for path in paths:
  95. if not path.exists():
  96. continue
  97. if common_ancestor is None:
  98. common_ancestor = path
  99. else:
  100. if common_ancestor in path.parents or path == common_ancestor:
  101. continue
  102. elif path in common_ancestor.parents:
  103. common_ancestor = path
  104. else:
  105. shared = commonpath(path, common_ancestor)
  106. if shared is not None:
  107. common_ancestor = shared
  108. if common_ancestor is None:
  109. common_ancestor = Path.cwd()
  110. elif common_ancestor.is_file():
  111. common_ancestor = common_ancestor.parent
  112. return common_ancestor
  113. def get_dirs_from_args(args: Iterable[str]) -> List[Path]:
  114. def is_option(x: str) -> bool:
  115. return x.startswith("-")
  116. def get_file_part_from_node_id(x: str) -> str:
  117. return x.split("::")[0]
  118. def get_dir_from_path(path: Path) -> Path:
  119. if path.is_dir():
  120. return path
  121. return path.parent
  122. def safe_exists(path: Path) -> bool:
  123. # This can throw on paths that contain characters unrepresentable at the OS level,
  124. # or with invalid syntax on Windows (https://bugs.python.org/issue35306)
  125. try:
  126. return path.exists()
  127. except OSError:
  128. return False
  129. # These look like paths but may not exist
  130. possible_paths = (
  131. absolutepath(get_file_part_from_node_id(arg))
  132. for arg in args
  133. if not is_option(arg)
  134. )
  135. return [get_dir_from_path(path) for path in possible_paths if safe_exists(path)]
  136. CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."
  137. def determine_setup(
  138. inifile: Optional[str],
  139. args: Sequence[str],
  140. rootdir_cmd_arg: Optional[str] = None,
  141. config: Optional["Config"] = None,
  142. ) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]:
  143. rootdir = None
  144. dirs = get_dirs_from_args(args)
  145. if inifile:
  146. inipath_ = absolutepath(inifile)
  147. inipath: Optional[Path] = inipath_
  148. inicfg = load_config_dict_from_file(inipath_) or {}
  149. if rootdir_cmd_arg is None:
  150. rootdir = inipath_.parent
  151. else:
  152. ancestor = get_common_ancestor(dirs)
  153. rootdir, inipath, inicfg = locate_config([ancestor])
  154. if rootdir is None and rootdir_cmd_arg is None:
  155. for possible_rootdir in (ancestor, *ancestor.parents):
  156. if (possible_rootdir / "setup.py").is_file():
  157. rootdir = possible_rootdir
  158. break
  159. else:
  160. if dirs != [ancestor]:
  161. rootdir, inipath, inicfg = locate_config(dirs)
  162. if rootdir is None:
  163. if config is not None:
  164. cwd = config.invocation_params.dir
  165. else:
  166. cwd = Path.cwd()
  167. rootdir = get_common_ancestor([cwd, ancestor])
  168. is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
  169. if is_fs_root:
  170. rootdir = ancestor
  171. if rootdir_cmd_arg:
  172. rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
  173. if not rootdir.is_dir():
  174. raise UsageError(
  175. "Directory '{}' not found. Check your '--rootdir' option.".format(
  176. rootdir
  177. )
  178. )
  179. assert rootdir is not None
  180. return rootdir, inipath, inicfg or {}