findpaths.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. from __future__ import annotations
  2. from collections.abc import Iterable
  3. from collections.abc import Sequence
  4. from dataclasses import dataclass
  5. from dataclasses import KW_ONLY
  6. import os
  7. from pathlib import Path
  8. import sys
  9. from typing import Literal
  10. from typing import TypeAlias
  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. from _pytest.pathlib import safe_exists
  17. @dataclass(frozen=True)
  18. class ConfigValue:
  19. """Represents a configuration value with its origin and parsing mode.
  20. This allows tracking whether a value came from a configuration file
  21. or from a CLI override (--override-ini), which is important for
  22. determining precedence when dealing with ini option aliases.
  23. The mode tracks the parsing mode/data model used for the value:
  24. - "ini": from INI files or [tool.pytest.ini_options], where the only
  25. supported value types are `str` or `list[str]`.
  26. - "toml": from TOML files (not in INI mode), where native TOML types
  27. are preserved.
  28. """
  29. value: object
  30. _: KW_ONLY
  31. origin: Literal["file", "override"]
  32. mode: Literal["ini", "toml"]
  33. ConfigDict: TypeAlias = dict[str, ConfigValue]
  34. def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
  35. """Parse the given generic '.ini' file using legacy IniConfig parser, returning
  36. the parsed object.
  37. Raise UsageError if the file cannot be parsed.
  38. """
  39. try:
  40. return iniconfig.IniConfig(str(path))
  41. except iniconfig.ParseError as exc:
  42. raise UsageError(str(exc)) from exc
  43. def load_config_dict_from_file(
  44. filepath: Path,
  45. ) -> ConfigDict | None:
  46. """Load pytest configuration from the given file path, if supported.
  47. Return None if the file does not contain valid pytest configuration.
  48. """
  49. # Configuration from ini files are obtained from the [pytest] section, if present.
  50. if filepath.suffix == ".ini":
  51. iniconfig = _parse_ini_config(filepath)
  52. if "pytest" in iniconfig:
  53. return {
  54. k: ConfigValue(v, origin="file", mode="ini")
  55. for k, v in iniconfig["pytest"].items()
  56. }
  57. else:
  58. # "pytest.ini" files are always the source of configuration, even if empty.
  59. if filepath.name in {"pytest.ini", ".pytest.ini"}:
  60. return {}
  61. # '.cfg' files are considered if they contain a "[tool:pytest]" section.
  62. elif filepath.suffix == ".cfg":
  63. iniconfig = _parse_ini_config(filepath)
  64. if "tool:pytest" in iniconfig.sections:
  65. return {
  66. k: ConfigValue(v, origin="file", mode="ini")
  67. for k, v in iniconfig["tool:pytest"].items()
  68. }
  69. elif "pytest" in iniconfig.sections:
  70. # If a setup.cfg contains a "[pytest]" section, we raise a failure to indicate users that
  71. # plain "[pytest]" sections in setup.cfg files is no longer supported (#3086).
  72. fail(CFG_PYTEST_SECTION.format(filename="setup.cfg"), pytrace=False)
  73. # '.toml' files are considered if they contain a [tool.pytest] table (toml mode)
  74. # or [tool.pytest.ini_options] table (ini mode) for pyproject.toml,
  75. # or [pytest] table (toml mode) for pytest.toml/.pytest.toml.
  76. elif filepath.suffix == ".toml":
  77. if sys.version_info >= (3, 11):
  78. import tomllib
  79. else:
  80. import tomli as tomllib
  81. toml_text = filepath.read_text(encoding="utf-8")
  82. try:
  83. config = tomllib.loads(toml_text)
  84. except tomllib.TOMLDecodeError as exc:
  85. raise UsageError(f"{filepath}: {exc}") from exc
  86. # pytest.toml and .pytest.toml use [pytest] table directly.
  87. if filepath.name in ("pytest.toml", ".pytest.toml"):
  88. pytest_config = config.get("pytest", {})
  89. if pytest_config:
  90. # TOML mode - preserve native TOML types.
  91. return {
  92. k: ConfigValue(v, origin="file", mode="toml")
  93. for k, v in pytest_config.items()
  94. }
  95. # "pytest.toml" files are always the source of configuration, even if empty.
  96. return {}
  97. # pyproject.toml uses [tool.pytest] or [tool.pytest.ini_options].
  98. else:
  99. tool_pytest = config.get("tool", {}).get("pytest", {})
  100. # Check for toml mode config: [tool.pytest] with content outside of ini_options.
  101. toml_config = {k: v for k, v in tool_pytest.items() if k != "ini_options"}
  102. # Check for ini mode config: [tool.pytest.ini_options].
  103. ini_config = tool_pytest.get("ini_options", None)
  104. if toml_config and ini_config:
  105. raise UsageError(
  106. f"{filepath}: Cannot use both [tool.pytest] (native TOML types) and "
  107. "[tool.pytest.ini_options] (string-based INI format) simultaneously. "
  108. "Please use [tool.pytest] with native TOML types (recommended) "
  109. "or [tool.pytest.ini_options] for backwards compatibility."
  110. )
  111. if toml_config:
  112. # TOML mode - preserve native TOML types.
  113. return {
  114. k: ConfigValue(v, origin="file", mode="toml")
  115. for k, v in toml_config.items()
  116. }
  117. elif ini_config is not None:
  118. # INI mode - TOML supports richer data types than INI files, but we need to
  119. # convert all scalar values to str for compatibility with the INI system.
  120. def make_scalar(v: object) -> str | list[str]:
  121. return v if isinstance(v, list) else str(v)
  122. return {
  123. k: ConfigValue(make_scalar(v), origin="file", mode="ini")
  124. for k, v in ini_config.items()
  125. }
  126. return None
  127. def locate_config(
  128. invocation_dir: Path,
  129. args: Iterable[Path],
  130. ) -> tuple[Path | None, Path | None, ConfigDict, Sequence[str]]:
  131. """Search in the list of arguments for a valid ini-file for pytest,
  132. and return a tuple of (rootdir, inifile, cfg-dict, ignored-config-files), where
  133. ignored-config-files is a list of config basenames found that contain
  134. pytest configuration but were ignored."""
  135. config_names = [
  136. "pytest.toml",
  137. ".pytest.toml",
  138. "pytest.ini",
  139. ".pytest.ini",
  140. "pyproject.toml",
  141. "tox.ini",
  142. "setup.cfg",
  143. ]
  144. args = [x for x in args if not str(x).startswith("-")]
  145. if not args:
  146. args = [invocation_dir]
  147. found_pyproject_toml: Path | None = None
  148. ignored_config_files: list[str] = []
  149. for arg in args:
  150. argpath = absolutepath(arg)
  151. for base in (argpath, *argpath.parents):
  152. for config_name in config_names:
  153. p = base / config_name
  154. if p.is_file():
  155. if p.name == "pyproject.toml" and found_pyproject_toml is None:
  156. found_pyproject_toml = p
  157. ini_config = load_config_dict_from_file(p)
  158. if ini_config is not None:
  159. index = config_names.index(config_name)
  160. for remainder in config_names[index + 1 :]:
  161. p2 = base / remainder
  162. if (
  163. p2.is_file()
  164. and load_config_dict_from_file(p2) is not None
  165. ):
  166. ignored_config_files.append(remainder)
  167. return base, p, ini_config, ignored_config_files
  168. if found_pyproject_toml is not None:
  169. return found_pyproject_toml.parent, found_pyproject_toml, {}, []
  170. return None, None, {}, []
  171. def get_common_ancestor(
  172. invocation_dir: Path,
  173. paths: Iterable[Path],
  174. ) -> Path:
  175. common_ancestor: Path | None = None
  176. for path in paths:
  177. if not path.exists():
  178. continue
  179. if common_ancestor is None:
  180. common_ancestor = path
  181. else:
  182. if common_ancestor in path.parents or path == common_ancestor:
  183. continue
  184. elif path in common_ancestor.parents:
  185. common_ancestor = path
  186. else:
  187. shared = commonpath(path, common_ancestor)
  188. if shared is not None:
  189. common_ancestor = shared
  190. if common_ancestor is None:
  191. common_ancestor = invocation_dir
  192. elif common_ancestor.is_file():
  193. common_ancestor = common_ancestor.parent
  194. return common_ancestor
  195. def get_dirs_from_args(args: Iterable[str]) -> list[Path]:
  196. def is_option(x: str) -> bool:
  197. return x.startswith("-")
  198. def get_file_part_from_node_id(x: str) -> str:
  199. return x.split("::")[0]
  200. def get_dir_from_path(path: Path) -> Path:
  201. if path.is_dir():
  202. return path
  203. return path.parent
  204. # These look like paths but may not exist
  205. possible_paths = (
  206. absolutepath(get_file_part_from_node_id(arg))
  207. for arg in args
  208. if not is_option(arg)
  209. )
  210. return [get_dir_from_path(path) for path in possible_paths if safe_exists(path)]
  211. def parse_override_ini(override_ini: Sequence[str] | None) -> ConfigDict:
  212. """Parse the -o/--override-ini command line arguments and return the overrides.
  213. :raises UsageError:
  214. If one of the values is malformed.
  215. """
  216. overrides = {}
  217. # override_ini is a list of "ini=value" options.
  218. # Always use the last item if multiple values are set for same ini-name,
  219. # e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2.
  220. for ini_config in override_ini or ():
  221. try:
  222. key, user_ini_value = ini_config.split("=", 1)
  223. except ValueError as e:
  224. raise UsageError(
  225. f"-o/--override-ini expects option=value style (got: {ini_config!r})."
  226. ) from e
  227. else:
  228. overrides[key] = ConfigValue(user_ini_value, origin="override", mode="ini")
  229. return overrides
  230. CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."
  231. def determine_setup(
  232. *,
  233. inifile: str | None,
  234. override_ini: Sequence[str] | None,
  235. args: Sequence[str],
  236. rootdir_cmd_arg: str | None,
  237. invocation_dir: Path,
  238. ) -> tuple[Path, Path | None, ConfigDict, Sequence[str]]:
  239. """Determine the rootdir, inifile and ini configuration values from the
  240. command line arguments.
  241. :param inifile:
  242. The `--inifile` command line argument, if given.
  243. :param override_ini:
  244. The -o/--override-ini command line arguments, if given.
  245. :param args:
  246. The free command line arguments.
  247. :param rootdir_cmd_arg:
  248. The `--rootdir` command line argument, if given.
  249. :param invocation_dir:
  250. The working directory when pytest was invoked.
  251. :raises UsageError:
  252. """
  253. rootdir = None
  254. dirs = get_dirs_from_args(args)
  255. ignored_config_files: Sequence[str] = []
  256. if inifile:
  257. inipath_ = absolutepath(inifile)
  258. inipath: Path | None = inipath_
  259. inicfg = load_config_dict_from_file(inipath_) or {}
  260. if rootdir_cmd_arg is None:
  261. rootdir = inipath_.parent
  262. else:
  263. ancestor = get_common_ancestor(invocation_dir, dirs)
  264. rootdir, inipath, inicfg, ignored_config_files = locate_config(
  265. invocation_dir, [ancestor]
  266. )
  267. if rootdir is None and rootdir_cmd_arg is None:
  268. for possible_rootdir in (ancestor, *ancestor.parents):
  269. if (possible_rootdir / "setup.py").is_file():
  270. rootdir = possible_rootdir
  271. break
  272. else:
  273. if dirs != [ancestor]:
  274. rootdir, inipath, inicfg, _ = locate_config(invocation_dir, dirs)
  275. if rootdir is None:
  276. rootdir = get_common_ancestor(
  277. invocation_dir, [invocation_dir, ancestor]
  278. )
  279. if is_fs_root(rootdir):
  280. rootdir = ancestor
  281. if rootdir_cmd_arg:
  282. rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
  283. if not rootdir.is_dir():
  284. raise UsageError(
  285. f"Directory '{rootdir}' not found. Check your '--rootdir' option."
  286. )
  287. ini_overrides = parse_override_ini(override_ini)
  288. inicfg.update(ini_overrides)
  289. assert rootdir is not None
  290. return rootdir, inipath, inicfg, ignored_config_files
  291. def is_fs_root(p: Path) -> bool:
  292. r"""
  293. Return True if the given path is pointing to the root of the
  294. file system ("/" on Unix and "C:\\" on Windows for example).
  295. """
  296. return os.path.splitdrive(str(p))[1] == os.sep