findpaths.py 7.4 KB

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