__init__.py 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. """Command line options, ini-file and conftest.py processing."""
  2. import argparse
  3. import collections.abc
  4. import contextlib
  5. import copy
  6. import enum
  7. import inspect
  8. import os
  9. import re
  10. import shlex
  11. import sys
  12. import types
  13. import warnings
  14. from functools import lru_cache
  15. from pathlib import Path
  16. from textwrap import dedent
  17. from types import TracebackType
  18. from typing import Any
  19. from typing import Callable
  20. from typing import cast
  21. from typing import Dict
  22. from typing import Generator
  23. from typing import IO
  24. from typing import Iterable
  25. from typing import Iterator
  26. from typing import List
  27. from typing import Optional
  28. from typing import Sequence
  29. from typing import Set
  30. from typing import TextIO
  31. from typing import Tuple
  32. from typing import Type
  33. from typing import TYPE_CHECKING
  34. from typing import Union
  35. import attr
  36. from pluggy import HookimplMarker
  37. from pluggy import HookspecMarker
  38. from pluggy import PluginManager
  39. import _pytest._code
  40. import _pytest.deprecated
  41. import _pytest.hookspec
  42. from .exceptions import PrintHelp as PrintHelp
  43. from .exceptions import UsageError as UsageError
  44. from .findpaths import determine_setup
  45. from _pytest._code import ExceptionInfo
  46. from _pytest._code import filter_traceback
  47. from _pytest._io import TerminalWriter
  48. from _pytest.compat import final
  49. from _pytest.compat import importlib_metadata
  50. from _pytest.outcomes import fail
  51. from _pytest.outcomes import Skipped
  52. from _pytest.pathlib import absolutepath
  53. from _pytest.pathlib import bestrelpath
  54. from _pytest.pathlib import import_path
  55. from _pytest.pathlib import ImportMode
  56. from _pytest.pathlib import resolve_package_path
  57. from _pytest.stash import Stash
  58. from _pytest.warning_types import PytestConfigWarning
  59. if TYPE_CHECKING:
  60. from _pytest._code.code import _TracebackStyle
  61. from _pytest.terminal import TerminalReporter
  62. from .argparsing import Argument
  63. _PluggyPlugin = object
  64. """A type to represent plugin objects.
  65. Plugins can be any namespace, so we can't narrow it down much, but we use an
  66. alias to make the intent clear.
  67. Ideally this type would be provided by pluggy itself.
  68. """
  69. hookimpl = HookimplMarker("pytest")
  70. hookspec = HookspecMarker("pytest")
  71. @final
  72. class ExitCode(enum.IntEnum):
  73. """Encodes the valid exit codes by pytest.
  74. Currently users and plugins may supply other exit codes as well.
  75. .. versionadded:: 5.0
  76. """
  77. #: Tests passed.
  78. OK = 0
  79. #: Tests failed.
  80. TESTS_FAILED = 1
  81. #: pytest was interrupted.
  82. INTERRUPTED = 2
  83. #: An internal error got in the way.
  84. INTERNAL_ERROR = 3
  85. #: pytest was misused.
  86. USAGE_ERROR = 4
  87. #: pytest couldn't find tests.
  88. NO_TESTS_COLLECTED = 5
  89. class ConftestImportFailure(Exception):
  90. def __init__(
  91. self,
  92. path: Path,
  93. excinfo: Tuple[Type[Exception], Exception, TracebackType],
  94. ) -> None:
  95. super().__init__(path, excinfo)
  96. self.path = path
  97. self.excinfo = excinfo
  98. def __str__(self) -> str:
  99. return "{}: {} (from {})".format(
  100. self.excinfo[0].__name__, self.excinfo[1], self.path
  101. )
  102. def filter_traceback_for_conftest_import_failure(
  103. entry: _pytest._code.TracebackEntry,
  104. ) -> bool:
  105. """Filter tracebacks entries which point to pytest internals or importlib.
  106. Make a special case for importlib because we use it to import test modules and conftest files
  107. in _pytest.pathlib.import_path.
  108. """
  109. return filter_traceback(entry) and "importlib" not in str(entry.path).split(os.sep)
  110. def main(
  111. args: Optional[Union[List[str], "os.PathLike[str]"]] = None,
  112. plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
  113. ) -> Union[int, ExitCode]:
  114. """Perform an in-process test run.
  115. :param args: List of command line arguments.
  116. :param plugins: List of plugin objects to be auto-registered during initialization.
  117. :returns: An exit code.
  118. """
  119. try:
  120. try:
  121. config = _prepareconfig(args, plugins)
  122. except ConftestImportFailure as e:
  123. exc_info = ExceptionInfo.from_exc_info(e.excinfo)
  124. tw = TerminalWriter(sys.stderr)
  125. tw.line(f"ImportError while loading conftest '{e.path}'.", red=True)
  126. exc_info.traceback = exc_info.traceback.filter(
  127. filter_traceback_for_conftest_import_failure
  128. )
  129. exc_repr = (
  130. exc_info.getrepr(style="short", chain=False)
  131. if exc_info.traceback
  132. else exc_info.exconly()
  133. )
  134. formatted_tb = str(exc_repr)
  135. for line in formatted_tb.splitlines():
  136. tw.line(line.rstrip(), red=True)
  137. return ExitCode.USAGE_ERROR
  138. else:
  139. try:
  140. ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  141. config=config
  142. )
  143. try:
  144. return ExitCode(ret)
  145. except ValueError:
  146. return ret
  147. finally:
  148. config._ensure_unconfigure()
  149. except UsageError as e:
  150. tw = TerminalWriter(sys.stderr)
  151. for msg in e.args:
  152. tw.line(f"ERROR: {msg}\n", red=True)
  153. return ExitCode.USAGE_ERROR
  154. def console_main() -> int:
  155. """The CLI entry point of pytest.
  156. This function is not meant for programmable use; use `main()` instead.
  157. """
  158. # https://docs.python.org/3/library/signal.html#note-on-sigpipe
  159. try:
  160. code = main()
  161. sys.stdout.flush()
  162. return code
  163. except BrokenPipeError:
  164. # Python flushes standard streams on exit; redirect remaining output
  165. # to devnull to avoid another BrokenPipeError at shutdown
  166. devnull = os.open(os.devnull, os.O_WRONLY)
  167. os.dup2(devnull, sys.stdout.fileno())
  168. return 1 # Python exits with error code 1 on EPIPE
  169. class cmdline: # compatibility namespace
  170. main = staticmethod(main)
  171. def filename_arg(path: str, optname: str) -> str:
  172. """Argparse type validator for filename arguments.
  173. :path: Path of filename.
  174. :optname: Name of the option.
  175. """
  176. if os.path.isdir(path):
  177. raise UsageError(f"{optname} must be a filename, given: {path}")
  178. return path
  179. def directory_arg(path: str, optname: str) -> str:
  180. """Argparse type validator for directory arguments.
  181. :path: Path of directory.
  182. :optname: Name of the option.
  183. """
  184. if not os.path.isdir(path):
  185. raise UsageError(f"{optname} must be a directory, given: {path}")
  186. return path
  187. # Plugins that cannot be disabled via "-p no:X" currently.
  188. essential_plugins = (
  189. "mark",
  190. "main",
  191. "runner",
  192. "fixtures",
  193. "helpconfig", # Provides -p.
  194. )
  195. default_plugins = essential_plugins + (
  196. "python",
  197. "terminal",
  198. "debugging",
  199. "unittest",
  200. "capture",
  201. "skipping",
  202. "legacypath",
  203. "tmpdir",
  204. "monkeypatch",
  205. "recwarn",
  206. "pastebin",
  207. "nose",
  208. "assertion",
  209. "junitxml",
  210. "doctest",
  211. "cacheprovider",
  212. "freeze_support",
  213. "setuponly",
  214. "setupplan",
  215. "stepwise",
  216. "warnings",
  217. "logging",
  218. "reports",
  219. "pythonpath",
  220. *(["unraisableexception", "threadexception"] if sys.version_info >= (3, 8) else []),
  221. "faulthandler",
  222. )
  223. builtin_plugins = set(default_plugins)
  224. builtin_plugins.add("pytester")
  225. builtin_plugins.add("pytester_assertions")
  226. def get_config(
  227. args: Optional[List[str]] = None,
  228. plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
  229. ) -> "Config":
  230. # subsequent calls to main will create a fresh instance
  231. pluginmanager = PytestPluginManager()
  232. config = Config(
  233. pluginmanager,
  234. invocation_params=Config.InvocationParams(
  235. args=args or (),
  236. plugins=plugins,
  237. dir=Path.cwd(),
  238. ),
  239. )
  240. if args is not None:
  241. # Handle any "-p no:plugin" args.
  242. pluginmanager.consider_preparse(args, exclude_only=True)
  243. for spec in default_plugins:
  244. pluginmanager.import_plugin(spec)
  245. return config
  246. def get_plugin_manager() -> "PytestPluginManager":
  247. """Obtain a new instance of the
  248. :py:class:`pytest.PytestPluginManager`, with default plugins
  249. already loaded.
  250. This function can be used by integration with other tools, like hooking
  251. into pytest to run tests into an IDE.
  252. """
  253. return get_config().pluginmanager
  254. def _prepareconfig(
  255. args: Optional[Union[List[str], "os.PathLike[str]"]] = None,
  256. plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
  257. ) -> "Config":
  258. if args is None:
  259. args = sys.argv[1:]
  260. elif isinstance(args, os.PathLike):
  261. args = [os.fspath(args)]
  262. elif not isinstance(args, list):
  263. msg = "`args` parameter expected to be a list of strings, got: {!r} (type: {})"
  264. raise TypeError(msg.format(args, type(args)))
  265. config = get_config(args, plugins)
  266. pluginmanager = config.pluginmanager
  267. try:
  268. if plugins:
  269. for plugin in plugins:
  270. if isinstance(plugin, str):
  271. pluginmanager.consider_pluginarg(plugin)
  272. else:
  273. pluginmanager.register(plugin)
  274. config = pluginmanager.hook.pytest_cmdline_parse(
  275. pluginmanager=pluginmanager, args=args
  276. )
  277. return config
  278. except BaseException:
  279. config._ensure_unconfigure()
  280. raise
  281. def _get_directory(path: Path) -> Path:
  282. """Get the directory of a path - itself if already a directory."""
  283. if path.is_file():
  284. return path.parent
  285. else:
  286. return path
  287. @final
  288. class PytestPluginManager(PluginManager):
  289. """A :py:class:`pluggy.PluginManager <pluggy.PluginManager>` with
  290. additional pytest-specific functionality:
  291. * Loading plugins from the command line, ``PYTEST_PLUGINS`` env variable and
  292. ``pytest_plugins`` global variables found in plugins being loaded.
  293. * ``conftest.py`` loading during start-up.
  294. """
  295. def __init__(self) -> None:
  296. import _pytest.assertion
  297. super().__init__("pytest")
  298. # The objects are module objects, only used generically.
  299. self._conftest_plugins: Set[types.ModuleType] = set()
  300. # State related to local conftest plugins.
  301. self._dirpath2confmods: Dict[Path, List[types.ModuleType]] = {}
  302. self._conftestpath2mod: Dict[Path, types.ModuleType] = {}
  303. self._confcutdir: Optional[Path] = None
  304. self._noconftest = False
  305. # _getconftestmodules()'s call to _get_directory() causes a stat
  306. # storm when it's called potentially thousands of times in a test
  307. # session (#9478), often with the same path, so cache it.
  308. self._get_directory = lru_cache(256)(_get_directory)
  309. self._duplicatepaths: Set[Path] = set()
  310. # plugins that were explicitly skipped with pytest.skip
  311. # list of (module name, skip reason)
  312. # previously we would issue a warning when a plugin was skipped, but
  313. # since we refactored warnings as first citizens of Config, they are
  314. # just stored here to be used later.
  315. self.skipped_plugins: List[Tuple[str, str]] = []
  316. self.add_hookspecs(_pytest.hookspec)
  317. self.register(self)
  318. if os.environ.get("PYTEST_DEBUG"):
  319. err: IO[str] = sys.stderr
  320. encoding: str = getattr(err, "encoding", "utf8")
  321. try:
  322. err = open(
  323. os.dup(err.fileno()),
  324. mode=err.mode,
  325. buffering=1,
  326. encoding=encoding,
  327. )
  328. except Exception:
  329. pass
  330. self.trace.root.setwriter(err.write)
  331. self.enable_tracing()
  332. # Config._consider_importhook will set a real object if required.
  333. self.rewrite_hook = _pytest.assertion.DummyRewriteHook()
  334. # Used to know when we are importing conftests after the pytest_configure stage.
  335. self._configured = False
  336. def parse_hookimpl_opts(self, plugin: _PluggyPlugin, name: str):
  337. # pytest hooks are always prefixed with "pytest_",
  338. # so we avoid accessing possibly non-readable attributes
  339. # (see issue #1073).
  340. if not name.startswith("pytest_"):
  341. return
  342. # Ignore names which can not be hooks.
  343. if name == "pytest_plugins":
  344. return
  345. method = getattr(plugin, name)
  346. opts = super().parse_hookimpl_opts(plugin, name)
  347. # Consider only actual functions for hooks (#3775).
  348. if not inspect.isroutine(method):
  349. return
  350. # Collect unmarked hooks as long as they have the `pytest_' prefix.
  351. if opts is None and name.startswith("pytest_"):
  352. opts = {}
  353. if opts is not None:
  354. # TODO: DeprecationWarning, people should use hookimpl
  355. # https://github.com/pytest-dev/pytest/issues/4562
  356. known_marks = {m.name for m in getattr(method, "pytestmark", [])}
  357. for name in ("tryfirst", "trylast", "optionalhook", "hookwrapper"):
  358. opts.setdefault(name, hasattr(method, name) or name in known_marks)
  359. return opts
  360. def parse_hookspec_opts(self, module_or_class, name: str):
  361. opts = super().parse_hookspec_opts(module_or_class, name)
  362. if opts is None:
  363. method = getattr(module_or_class, name)
  364. if name.startswith("pytest_"):
  365. # todo: deprecate hookspec hacks
  366. # https://github.com/pytest-dev/pytest/issues/4562
  367. known_marks = {m.name for m in getattr(method, "pytestmark", [])}
  368. opts = {
  369. "firstresult": hasattr(method, "firstresult")
  370. or "firstresult" in known_marks,
  371. "historic": hasattr(method, "historic")
  372. or "historic" in known_marks,
  373. }
  374. return opts
  375. def register(
  376. self, plugin: _PluggyPlugin, name: Optional[str] = None
  377. ) -> Optional[str]:
  378. if name in _pytest.deprecated.DEPRECATED_EXTERNAL_PLUGINS:
  379. warnings.warn(
  380. PytestConfigWarning(
  381. "{} plugin has been merged into the core, "
  382. "please remove it from your requirements.".format(
  383. name.replace("_", "-")
  384. )
  385. )
  386. )
  387. return None
  388. ret: Optional[str] = super().register(plugin, name)
  389. if ret:
  390. self.hook.pytest_plugin_registered.call_historic(
  391. kwargs=dict(plugin=plugin, manager=self)
  392. )
  393. if isinstance(plugin, types.ModuleType):
  394. self.consider_module(plugin)
  395. return ret
  396. def getplugin(self, name: str):
  397. # Support deprecated naming because plugins (xdist e.g.) use it.
  398. plugin: Optional[_PluggyPlugin] = self.get_plugin(name)
  399. return plugin
  400. def hasplugin(self, name: str) -> bool:
  401. """Return whether a plugin with the given name is registered."""
  402. return bool(self.get_plugin(name))
  403. def pytest_configure(self, config: "Config") -> None:
  404. """:meta private:"""
  405. # XXX now that the pluginmanager exposes hookimpl(tryfirst...)
  406. # we should remove tryfirst/trylast as markers.
  407. config.addinivalue_line(
  408. "markers",
  409. "tryfirst: mark a hook implementation function such that the "
  410. "plugin machinery will try to call it first/as early as possible.",
  411. )
  412. config.addinivalue_line(
  413. "markers",
  414. "trylast: mark a hook implementation function such that the "
  415. "plugin machinery will try to call it last/as late as possible.",
  416. )
  417. self._configured = True
  418. #
  419. # Internal API for local conftest plugin handling.
  420. #
  421. def _set_initial_conftests(
  422. self, namespace: argparse.Namespace, rootpath: Path
  423. ) -> None:
  424. """Load initial conftest files given a preparsed "namespace".
  425. As conftest files may add their own command line options which have
  426. arguments ('--my-opt somepath') we might get some false positives.
  427. All builtin and 3rd party plugins will have been loaded, however, so
  428. common options will not confuse our logic here.
  429. """
  430. current = Path.cwd()
  431. self._confcutdir = (
  432. absolutepath(current / namespace.confcutdir)
  433. if namespace.confcutdir
  434. else None
  435. )
  436. self._noconftest = namespace.noconftest
  437. self._using_pyargs = namespace.pyargs
  438. testpaths = namespace.file_or_dir
  439. foundanchor = False
  440. for testpath in testpaths:
  441. path = str(testpath)
  442. # remove node-id syntax
  443. i = path.find("::")
  444. if i != -1:
  445. path = path[:i]
  446. anchor = absolutepath(current / path)
  447. if anchor.exists(): # we found some file object
  448. self._try_load_conftest(anchor, namespace.importmode, rootpath)
  449. foundanchor = True
  450. if not foundanchor:
  451. self._try_load_conftest(current, namespace.importmode, rootpath)
  452. def _try_load_conftest(
  453. self, anchor: Path, importmode: Union[str, ImportMode], rootpath: Path
  454. ) -> None:
  455. self._getconftestmodules(anchor, importmode, rootpath)
  456. # let's also consider test* subdirs
  457. if anchor.is_dir():
  458. for x in anchor.glob("test*"):
  459. if x.is_dir():
  460. self._getconftestmodules(x, importmode, rootpath)
  461. def _getconftestmodules(
  462. self, path: Path, importmode: Union[str, ImportMode], rootpath: Path
  463. ) -> List[types.ModuleType]:
  464. if self._noconftest:
  465. return []
  466. directory = self._get_directory(path)
  467. # Optimization: avoid repeated searches in the same directory.
  468. # Assumes always called with same importmode and rootpath.
  469. existing_clist = self._dirpath2confmods.get(directory)
  470. if existing_clist is not None:
  471. return existing_clist
  472. # XXX these days we may rather want to use config.rootpath
  473. # and allow users to opt into looking into the rootdir parent
  474. # directories instead of requiring to specify confcutdir.
  475. clist = []
  476. confcutdir_parents = self._confcutdir.parents if self._confcutdir else []
  477. for parent in reversed((directory, *directory.parents)):
  478. if parent in confcutdir_parents:
  479. continue
  480. conftestpath = parent / "conftest.py"
  481. if conftestpath.is_file():
  482. mod = self._importconftest(conftestpath, importmode, rootpath)
  483. clist.append(mod)
  484. self._dirpath2confmods[directory] = clist
  485. return clist
  486. def _rget_with_confmod(
  487. self,
  488. name: str,
  489. path: Path,
  490. importmode: Union[str, ImportMode],
  491. rootpath: Path,
  492. ) -> Tuple[types.ModuleType, Any]:
  493. modules = self._getconftestmodules(path, importmode, rootpath=rootpath)
  494. for mod in reversed(modules):
  495. try:
  496. return mod, getattr(mod, name)
  497. except AttributeError:
  498. continue
  499. raise KeyError(name)
  500. def _importconftest(
  501. self, conftestpath: Path, importmode: Union[str, ImportMode], rootpath: Path
  502. ) -> types.ModuleType:
  503. # Use a resolved Path object as key to avoid loading the same conftest
  504. # twice with build systems that create build directories containing
  505. # symlinks to actual files.
  506. # Using Path().resolve() is better than py.path.realpath because
  507. # it resolves to the correct path/drive in case-insensitive file systems (#5792)
  508. key = conftestpath.resolve()
  509. with contextlib.suppress(KeyError):
  510. return self._conftestpath2mod[key]
  511. pkgpath = resolve_package_path(conftestpath)
  512. if pkgpath is None:
  513. _ensure_removed_sysmodule(conftestpath.stem)
  514. try:
  515. mod = import_path(conftestpath, mode=importmode, root=rootpath)
  516. except Exception as e:
  517. assert e.__traceback__ is not None
  518. exc_info = (type(e), e, e.__traceback__)
  519. raise ConftestImportFailure(conftestpath, exc_info) from e
  520. self._check_non_top_pytest_plugins(mod, conftestpath)
  521. self._conftest_plugins.add(mod)
  522. self._conftestpath2mod[key] = mod
  523. dirpath = conftestpath.parent
  524. if dirpath in self._dirpath2confmods:
  525. for path, mods in self._dirpath2confmods.items():
  526. if path and dirpath in path.parents or path == dirpath:
  527. assert mod not in mods
  528. mods.append(mod)
  529. self.trace(f"loading conftestmodule {mod!r}")
  530. self.consider_conftest(mod)
  531. return mod
  532. def _check_non_top_pytest_plugins(
  533. self,
  534. mod: types.ModuleType,
  535. conftestpath: Path,
  536. ) -> None:
  537. if (
  538. hasattr(mod, "pytest_plugins")
  539. and self._configured
  540. and not self._using_pyargs
  541. ):
  542. msg = (
  543. "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:\n"
  544. "It affects the entire test suite instead of just below the conftest as expected.\n"
  545. " {}\n"
  546. "Please move it to a top level conftest file at the rootdir:\n"
  547. " {}\n"
  548. "For more information, visit:\n"
  549. " https://docs.pytest.org/en/stable/deprecations.html#pytest-plugins-in-non-top-level-conftest-files"
  550. )
  551. fail(msg.format(conftestpath, self._confcutdir), pytrace=False)
  552. #
  553. # API for bootstrapping plugin loading
  554. #
  555. #
  556. def consider_preparse(
  557. self, args: Sequence[str], *, exclude_only: bool = False
  558. ) -> None:
  559. """:meta private:"""
  560. i = 0
  561. n = len(args)
  562. while i < n:
  563. opt = args[i]
  564. i += 1
  565. if isinstance(opt, str):
  566. if opt == "-p":
  567. try:
  568. parg = args[i]
  569. except IndexError:
  570. return
  571. i += 1
  572. elif opt.startswith("-p"):
  573. parg = opt[2:]
  574. else:
  575. continue
  576. if exclude_only and not parg.startswith("no:"):
  577. continue
  578. self.consider_pluginarg(parg)
  579. def consider_pluginarg(self, arg: str) -> None:
  580. """:meta private:"""
  581. if arg.startswith("no:"):
  582. name = arg[3:]
  583. if name in essential_plugins:
  584. raise UsageError("plugin %s cannot be disabled" % name)
  585. # PR #4304: remove stepwise if cacheprovider is blocked.
  586. if name == "cacheprovider":
  587. self.set_blocked("stepwise")
  588. self.set_blocked("pytest_stepwise")
  589. self.set_blocked(name)
  590. if not name.startswith("pytest_"):
  591. self.set_blocked("pytest_" + name)
  592. else:
  593. name = arg
  594. # Unblock the plugin. None indicates that it has been blocked.
  595. # There is no interface with pluggy for this.
  596. if self._name2plugin.get(name, -1) is None:
  597. del self._name2plugin[name]
  598. if not name.startswith("pytest_"):
  599. if self._name2plugin.get("pytest_" + name, -1) is None:
  600. del self._name2plugin["pytest_" + name]
  601. self.import_plugin(arg, consider_entry_points=True)
  602. def consider_conftest(self, conftestmodule: types.ModuleType) -> None:
  603. """:meta private:"""
  604. self.register(conftestmodule, name=conftestmodule.__file__)
  605. def consider_env(self) -> None:
  606. """:meta private:"""
  607. self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS"))
  608. def consider_module(self, mod: types.ModuleType) -> None:
  609. """:meta private:"""
  610. self._import_plugin_specs(getattr(mod, "pytest_plugins", []))
  611. def _import_plugin_specs(
  612. self, spec: Union[None, types.ModuleType, str, Sequence[str]]
  613. ) -> None:
  614. plugins = _get_plugin_specs_as_list(spec)
  615. for import_spec in plugins:
  616. self.import_plugin(import_spec)
  617. def import_plugin(self, modname: str, consider_entry_points: bool = False) -> None:
  618. """Import a plugin with ``modname``.
  619. If ``consider_entry_points`` is True, entry point names are also
  620. considered to find a plugin.
  621. """
  622. # Most often modname refers to builtin modules, e.g. "pytester",
  623. # "terminal" or "capture". Those plugins are registered under their
  624. # basename for historic purposes but must be imported with the
  625. # _pytest prefix.
  626. assert isinstance(modname, str), (
  627. "module name as text required, got %r" % modname
  628. )
  629. if self.is_blocked(modname) or self.get_plugin(modname) is not None:
  630. return
  631. importspec = "_pytest." + modname if modname in builtin_plugins else modname
  632. self.rewrite_hook.mark_rewrite(importspec)
  633. if consider_entry_points:
  634. loaded = self.load_setuptools_entrypoints("pytest11", name=modname)
  635. if loaded:
  636. return
  637. try:
  638. __import__(importspec)
  639. except ImportError as e:
  640. raise ImportError(
  641. f'Error importing plugin "{modname}": {e.args[0]}'
  642. ).with_traceback(e.__traceback__) from e
  643. except Skipped as e:
  644. self.skipped_plugins.append((modname, e.msg or ""))
  645. else:
  646. mod = sys.modules[importspec]
  647. self.register(mod, modname)
  648. def _get_plugin_specs_as_list(
  649. specs: Union[None, types.ModuleType, str, Sequence[str]]
  650. ) -> List[str]:
  651. """Parse a plugins specification into a list of plugin names."""
  652. # None means empty.
  653. if specs is None:
  654. return []
  655. # Workaround for #3899 - a submodule which happens to be called "pytest_plugins".
  656. if isinstance(specs, types.ModuleType):
  657. return []
  658. # Comma-separated list.
  659. if isinstance(specs, str):
  660. return specs.split(",") if specs else []
  661. # Direct specification.
  662. if isinstance(specs, collections.abc.Sequence):
  663. return list(specs)
  664. raise UsageError(
  665. "Plugins may be specified as a sequence or a ','-separated string of plugin names. Got: %r"
  666. % specs
  667. )
  668. def _ensure_removed_sysmodule(modname: str) -> None:
  669. try:
  670. del sys.modules[modname]
  671. except KeyError:
  672. pass
  673. class Notset:
  674. def __repr__(self):
  675. return "<NOTSET>"
  676. notset = Notset()
  677. def _iter_rewritable_modules(package_files: Iterable[str]) -> Iterator[str]:
  678. """Given an iterable of file names in a source distribution, return the "names" that should
  679. be marked for assertion rewrite.
  680. For example the package "pytest_mock/__init__.py" should be added as "pytest_mock" in
  681. the assertion rewrite mechanism.
  682. This function has to deal with dist-info based distributions and egg based distributions
  683. (which are still very much in use for "editable" installs).
  684. Here are the file names as seen in a dist-info based distribution:
  685. pytest_mock/__init__.py
  686. pytest_mock/_version.py
  687. pytest_mock/plugin.py
  688. pytest_mock.egg-info/PKG-INFO
  689. Here are the file names as seen in an egg based distribution:
  690. src/pytest_mock/__init__.py
  691. src/pytest_mock/_version.py
  692. src/pytest_mock/plugin.py
  693. src/pytest_mock.egg-info/PKG-INFO
  694. LICENSE
  695. setup.py
  696. We have to take in account those two distribution flavors in order to determine which
  697. names should be considered for assertion rewriting.
  698. More information:
  699. https://github.com/pytest-dev/pytest-mock/issues/167
  700. """
  701. package_files = list(package_files)
  702. seen_some = False
  703. for fn in package_files:
  704. is_simple_module = "/" not in fn and fn.endswith(".py")
  705. is_package = fn.count("/") == 1 and fn.endswith("__init__.py")
  706. if is_simple_module:
  707. module_name, _ = os.path.splitext(fn)
  708. # we ignore "setup.py" at the root of the distribution
  709. if module_name != "setup":
  710. seen_some = True
  711. yield module_name
  712. elif is_package:
  713. package_name = os.path.dirname(fn)
  714. seen_some = True
  715. yield package_name
  716. if not seen_some:
  717. # At this point we did not find any packages or modules suitable for assertion
  718. # rewriting, so we try again by stripping the first path component (to account for
  719. # "src" based source trees for example).
  720. # This approach lets us have the common case continue to be fast, as egg-distributions
  721. # are rarer.
  722. new_package_files = []
  723. for fn in package_files:
  724. parts = fn.split("/")
  725. new_fn = "/".join(parts[1:])
  726. if new_fn:
  727. new_package_files.append(new_fn)
  728. if new_package_files:
  729. yield from _iter_rewritable_modules(new_package_files)
  730. def _args_converter(args: Iterable[str]) -> Tuple[str, ...]:
  731. return tuple(args)
  732. @final
  733. class Config:
  734. """Access to configuration values, pluginmanager and plugin hooks.
  735. :param PytestPluginManager pluginmanager:
  736. A pytest PluginManager.
  737. :param InvocationParams invocation_params:
  738. Object containing parameters regarding the :func:`pytest.main`
  739. invocation.
  740. """
  741. @final
  742. @attr.s(frozen=True, auto_attribs=True)
  743. class InvocationParams:
  744. """Holds parameters passed during :func:`pytest.main`.
  745. The object attributes are read-only.
  746. .. versionadded:: 5.1
  747. .. note::
  748. Note that the environment variable ``PYTEST_ADDOPTS`` and the ``addopts``
  749. ini option are handled by pytest, not being included in the ``args`` attribute.
  750. Plugins accessing ``InvocationParams`` must be aware of that.
  751. """
  752. args: Tuple[str, ...] = attr.ib(converter=_args_converter)
  753. """The command-line arguments as passed to :func:`pytest.main`."""
  754. plugins: Optional[Sequence[Union[str, _PluggyPlugin]]]
  755. """Extra plugins, might be `None`."""
  756. dir: Path
  757. """The directory from which :func:`pytest.main` was invoked."""
  758. def __init__(
  759. self,
  760. pluginmanager: PytestPluginManager,
  761. *,
  762. invocation_params: Optional[InvocationParams] = None,
  763. ) -> None:
  764. from .argparsing import Parser, FILE_OR_DIR
  765. if invocation_params is None:
  766. invocation_params = self.InvocationParams(
  767. args=(), plugins=None, dir=Path.cwd()
  768. )
  769. self.option = argparse.Namespace()
  770. """Access to command line option as attributes.
  771. :type: argparse.Namespace
  772. """
  773. self.invocation_params = invocation_params
  774. """The parameters with which pytest was invoked.
  775. :type: InvocationParams
  776. """
  777. _a = FILE_OR_DIR
  778. self._parser = Parser(
  779. usage=f"%(prog)s [options] [{_a}] [{_a}] [...]",
  780. processopt=self._processopt,
  781. _ispytest=True,
  782. )
  783. self.pluginmanager = pluginmanager
  784. """The plugin manager handles plugin registration and hook invocation.
  785. :type: PytestPluginManager
  786. """
  787. self.stash = Stash()
  788. """A place where plugins can store information on the config for their
  789. own use.
  790. :type: Stash
  791. """
  792. # Deprecated alias. Was never public. Can be removed in a few releases.
  793. self._store = self.stash
  794. from .compat import PathAwareHookProxy
  795. self.trace = self.pluginmanager.trace.root.get("config")
  796. self.hook = PathAwareHookProxy(self.pluginmanager.hook)
  797. self._inicache: Dict[str, Any] = {}
  798. self._override_ini: Sequence[str] = ()
  799. self._opt2dest: Dict[str, str] = {}
  800. self._cleanup: List[Callable[[], None]] = []
  801. self.pluginmanager.register(self, "pytestconfig")
  802. self._configured = False
  803. self.hook.pytest_addoption.call_historic(
  804. kwargs=dict(parser=self._parser, pluginmanager=self.pluginmanager)
  805. )
  806. if TYPE_CHECKING:
  807. from _pytest.cacheprovider import Cache
  808. self.cache: Optional[Cache] = None
  809. @property
  810. def rootpath(self) -> Path:
  811. """The path to the :ref:`rootdir <rootdir>`.
  812. :type: pathlib.Path
  813. .. versionadded:: 6.1
  814. """
  815. return self._rootpath
  816. @property
  817. def inipath(self) -> Optional[Path]:
  818. """The path to the :ref:`configfile <configfiles>`.
  819. :type: Optional[pathlib.Path]
  820. .. versionadded:: 6.1
  821. """
  822. return self._inipath
  823. def add_cleanup(self, func: Callable[[], None]) -> None:
  824. """Add a function to be called when the config object gets out of
  825. use (usually coinciding with pytest_unconfigure)."""
  826. self._cleanup.append(func)
  827. def _do_configure(self) -> None:
  828. assert not self._configured
  829. self._configured = True
  830. with warnings.catch_warnings():
  831. warnings.simplefilter("default")
  832. self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
  833. def _ensure_unconfigure(self) -> None:
  834. if self._configured:
  835. self._configured = False
  836. self.hook.pytest_unconfigure(config=self)
  837. self.hook.pytest_configure._call_history = []
  838. while self._cleanup:
  839. fin = self._cleanup.pop()
  840. fin()
  841. def get_terminal_writer(self) -> TerminalWriter:
  842. terminalreporter: TerminalReporter = self.pluginmanager.get_plugin(
  843. "terminalreporter"
  844. )
  845. return terminalreporter._tw
  846. def pytest_cmdline_parse(
  847. self, pluginmanager: PytestPluginManager, args: List[str]
  848. ) -> "Config":
  849. try:
  850. self.parse(args)
  851. except UsageError:
  852. # Handle --version and --help here in a minimal fashion.
  853. # This gets done via helpconfig normally, but its
  854. # pytest_cmdline_main is not called in case of errors.
  855. if getattr(self.option, "version", False) or "--version" in args:
  856. from _pytest.helpconfig import showversion
  857. showversion(self)
  858. elif (
  859. getattr(self.option, "help", False) or "--help" in args or "-h" in args
  860. ):
  861. self._parser._getparser().print_help()
  862. sys.stdout.write(
  863. "\nNOTE: displaying only minimal help due to UsageError.\n\n"
  864. )
  865. raise
  866. return self
  867. def notify_exception(
  868. self,
  869. excinfo: ExceptionInfo[BaseException],
  870. option: Optional[argparse.Namespace] = None,
  871. ) -> None:
  872. if option and getattr(option, "fulltrace", False):
  873. style: _TracebackStyle = "long"
  874. else:
  875. style = "native"
  876. excrepr = excinfo.getrepr(
  877. funcargs=True, showlocals=getattr(option, "showlocals", False), style=style
  878. )
  879. res = self.hook.pytest_internalerror(excrepr=excrepr, excinfo=excinfo)
  880. if not any(res):
  881. for line in str(excrepr).split("\n"):
  882. sys.stderr.write("INTERNALERROR> %s\n" % line)
  883. sys.stderr.flush()
  884. def cwd_relative_nodeid(self, nodeid: str) -> str:
  885. # nodeid's are relative to the rootpath, compute relative to cwd.
  886. if self.invocation_params.dir != self.rootpath:
  887. fullpath = self.rootpath / nodeid
  888. nodeid = bestrelpath(self.invocation_params.dir, fullpath)
  889. return nodeid
  890. @classmethod
  891. def fromdictargs(cls, option_dict, args) -> "Config":
  892. """Constructor usable for subprocesses."""
  893. config = get_config(args)
  894. config.option.__dict__.update(option_dict)
  895. config.parse(args, addopts=False)
  896. for x in config.option.plugins:
  897. config.pluginmanager.consider_pluginarg(x)
  898. return config
  899. def _processopt(self, opt: "Argument") -> None:
  900. for name in opt._short_opts + opt._long_opts:
  901. self._opt2dest[name] = opt.dest
  902. if hasattr(opt, "default"):
  903. if not hasattr(self.option, opt.dest):
  904. setattr(self.option, opt.dest, opt.default)
  905. @hookimpl(trylast=True)
  906. def pytest_load_initial_conftests(self, early_config: "Config") -> None:
  907. self.pluginmanager._set_initial_conftests(
  908. early_config.known_args_namespace, rootpath=early_config.rootpath
  909. )
  910. def _initini(self, args: Sequence[str]) -> None:
  911. ns, unknown_args = self._parser.parse_known_and_unknown_args(
  912. args, namespace=copy.copy(self.option)
  913. )
  914. rootpath, inipath, inicfg = determine_setup(
  915. ns.inifilename,
  916. ns.file_or_dir + unknown_args,
  917. rootdir_cmd_arg=ns.rootdir or None,
  918. config=self,
  919. )
  920. self._rootpath = rootpath
  921. self._inipath = inipath
  922. self.inicfg = inicfg
  923. self._parser.extra_info["rootdir"] = str(self.rootpath)
  924. self._parser.extra_info["inifile"] = str(self.inipath)
  925. self._parser.addini("addopts", "extra command line options", "args")
  926. self._parser.addini("minversion", "minimally required pytest version")
  927. self._parser.addini(
  928. "required_plugins",
  929. "plugins that must be present for pytest to run",
  930. type="args",
  931. default=[],
  932. )
  933. self._override_ini = ns.override_ini or ()
  934. def _consider_importhook(self, args: Sequence[str]) -> None:
  935. """Install the PEP 302 import hook if using assertion rewriting.
  936. Needs to parse the --assert=<mode> option from the commandline
  937. and find all the installed plugins to mark them for rewriting
  938. by the importhook.
  939. """
  940. ns, unknown_args = self._parser.parse_known_and_unknown_args(args)
  941. mode = getattr(ns, "assertmode", "plain")
  942. if mode == "rewrite":
  943. import _pytest.assertion
  944. try:
  945. hook = _pytest.assertion.install_importhook(self)
  946. except SystemError:
  947. mode = "plain"
  948. else:
  949. self._mark_plugins_for_rewrite(hook)
  950. self._warn_about_missing_assertion(mode)
  951. def _mark_plugins_for_rewrite(self, hook) -> None:
  952. """Given an importhook, mark for rewrite any top-level
  953. modules or packages in the distribution package for
  954. all pytest plugins."""
  955. self.pluginmanager.rewrite_hook = hook
  956. if os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
  957. # We don't autoload from setuptools entry points, no need to continue.
  958. return
  959. package_files = (
  960. str(file)
  961. for dist in importlib_metadata.distributions()
  962. if any(ep.group == "pytest11" for ep in dist.entry_points)
  963. for file in dist.files or []
  964. )
  965. for name in _iter_rewritable_modules(package_files):
  966. hook.mark_rewrite(name)
  967. def _validate_args(self, args: List[str], via: str) -> List[str]:
  968. """Validate known args."""
  969. self._parser._config_source_hint = via # type: ignore
  970. try:
  971. self._parser.parse_known_and_unknown_args(
  972. args, namespace=copy.copy(self.option)
  973. )
  974. finally:
  975. del self._parser._config_source_hint # type: ignore
  976. return args
  977. def _preparse(self, args: List[str], addopts: bool = True) -> None:
  978. if addopts:
  979. env_addopts = os.environ.get("PYTEST_ADDOPTS", "")
  980. if len(env_addopts):
  981. args[:] = (
  982. self._validate_args(shlex.split(env_addopts), "via PYTEST_ADDOPTS")
  983. + args
  984. )
  985. self._initini(args)
  986. if addopts:
  987. args[:] = (
  988. self._validate_args(self.getini("addopts"), "via addopts config") + args
  989. )
  990. self.known_args_namespace = self._parser.parse_known_args(
  991. args, namespace=copy.copy(self.option)
  992. )
  993. self._checkversion()
  994. self._consider_importhook(args)
  995. self.pluginmanager.consider_preparse(args, exclude_only=False)
  996. if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
  997. # Don't autoload from setuptools entry point. Only explicitly specified
  998. # plugins are going to be loaded.
  999. self.pluginmanager.load_setuptools_entrypoints("pytest11")
  1000. self.pluginmanager.consider_env()
  1001. self.known_args_namespace = self._parser.parse_known_args(
  1002. args, namespace=copy.copy(self.known_args_namespace)
  1003. )
  1004. self._validate_plugins()
  1005. self._warn_about_skipped_plugins()
  1006. if self.known_args_namespace.strict:
  1007. self.issue_config_time_warning(
  1008. _pytest.deprecated.STRICT_OPTION, stacklevel=2
  1009. )
  1010. if self.known_args_namespace.confcutdir is None and self.inipath is not None:
  1011. confcutdir = str(self.inipath.parent)
  1012. self.known_args_namespace.confcutdir = confcutdir
  1013. try:
  1014. self.hook.pytest_load_initial_conftests(
  1015. early_config=self, args=args, parser=self._parser
  1016. )
  1017. except ConftestImportFailure as e:
  1018. if self.known_args_namespace.help or self.known_args_namespace.version:
  1019. # we don't want to prevent --help/--version to work
  1020. # so just let is pass and print a warning at the end
  1021. self.issue_config_time_warning(
  1022. PytestConfigWarning(f"could not load initial conftests: {e.path}"),
  1023. stacklevel=2,
  1024. )
  1025. else:
  1026. raise
  1027. @hookimpl(hookwrapper=True)
  1028. def pytest_collection(self) -> Generator[None, None, None]:
  1029. # Validate invalid ini keys after collection is done so we take in account
  1030. # options added by late-loading conftest files.
  1031. yield
  1032. self._validate_config_options()
  1033. def _checkversion(self) -> None:
  1034. import pytest
  1035. minver = self.inicfg.get("minversion", None)
  1036. if minver:
  1037. # Imported lazily to improve start-up time.
  1038. from packaging.version import Version
  1039. if not isinstance(minver, str):
  1040. raise pytest.UsageError(
  1041. "%s: 'minversion' must be a single value" % self.inipath
  1042. )
  1043. if Version(minver) > Version(pytest.__version__):
  1044. raise pytest.UsageError(
  1045. "%s: 'minversion' requires pytest-%s, actual pytest-%s'"
  1046. % (
  1047. self.inipath,
  1048. minver,
  1049. pytest.__version__,
  1050. )
  1051. )
  1052. def _validate_config_options(self) -> None:
  1053. for key in sorted(self._get_unknown_ini_keys()):
  1054. self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")
  1055. def _validate_plugins(self) -> None:
  1056. required_plugins = sorted(self.getini("required_plugins"))
  1057. if not required_plugins:
  1058. return
  1059. # Imported lazily to improve start-up time.
  1060. from packaging.version import Version
  1061. from packaging.requirements import InvalidRequirement, Requirement
  1062. plugin_info = self.pluginmanager.list_plugin_distinfo()
  1063. plugin_dist_info = {dist.project_name: dist.version for _, dist in plugin_info}
  1064. missing_plugins = []
  1065. for required_plugin in required_plugins:
  1066. try:
  1067. req = Requirement(required_plugin)
  1068. except InvalidRequirement:
  1069. missing_plugins.append(required_plugin)
  1070. continue
  1071. if req.name not in plugin_dist_info:
  1072. missing_plugins.append(required_plugin)
  1073. elif not req.specifier.contains(
  1074. Version(plugin_dist_info[req.name]), prereleases=True
  1075. ):
  1076. missing_plugins.append(required_plugin)
  1077. if missing_plugins:
  1078. raise UsageError(
  1079. "Missing required plugins: {}".format(", ".join(missing_plugins)),
  1080. )
  1081. def _warn_or_fail_if_strict(self, message: str) -> None:
  1082. if self.known_args_namespace.strict_config:
  1083. raise UsageError(message)
  1084. self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3)
  1085. def _get_unknown_ini_keys(self) -> List[str]:
  1086. parser_inicfg = self._parser._inidict
  1087. return [name for name in self.inicfg if name not in parser_inicfg]
  1088. def parse(self, args: List[str], addopts: bool = True) -> None:
  1089. # Parse given cmdline arguments into this config object.
  1090. assert not hasattr(
  1091. self, "args"
  1092. ), "can only parse cmdline args at most once per Config object"
  1093. self.hook.pytest_addhooks.call_historic(
  1094. kwargs=dict(pluginmanager=self.pluginmanager)
  1095. )
  1096. self._preparse(args, addopts=addopts)
  1097. # XXX deprecated hook:
  1098. self.hook.pytest_cmdline_preparse(config=self, args=args)
  1099. self._parser.after_preparse = True # type: ignore
  1100. try:
  1101. args = self._parser.parse_setoption(
  1102. args, self.option, namespace=self.option
  1103. )
  1104. if not args:
  1105. if self.invocation_params.dir == self.rootpath:
  1106. args = self.getini("testpaths")
  1107. if not args:
  1108. args = [str(self.invocation_params.dir)]
  1109. self.args = args
  1110. except PrintHelp:
  1111. pass
  1112. def issue_config_time_warning(self, warning: Warning, stacklevel: int) -> None:
  1113. """Issue and handle a warning during the "configure" stage.
  1114. During ``pytest_configure`` we can't capture warnings using the ``catch_warnings_for_item``
  1115. function because it is not possible to have hookwrappers around ``pytest_configure``.
  1116. This function is mainly intended for plugins that need to issue warnings during
  1117. ``pytest_configure`` (or similar stages).
  1118. :param warning: The warning instance.
  1119. :param stacklevel: stacklevel forwarded to warnings.warn.
  1120. """
  1121. if self.pluginmanager.is_blocked("warnings"):
  1122. return
  1123. cmdline_filters = self.known_args_namespace.pythonwarnings or []
  1124. config_filters = self.getini("filterwarnings")
  1125. with warnings.catch_warnings(record=True) as records:
  1126. warnings.simplefilter("always", type(warning))
  1127. apply_warning_filters(config_filters, cmdline_filters)
  1128. warnings.warn(warning, stacklevel=stacklevel)
  1129. if records:
  1130. frame = sys._getframe(stacklevel - 1)
  1131. location = frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name
  1132. self.hook.pytest_warning_captured.call_historic(
  1133. kwargs=dict(
  1134. warning_message=records[0],
  1135. when="config",
  1136. item=None,
  1137. location=location,
  1138. )
  1139. )
  1140. self.hook.pytest_warning_recorded.call_historic(
  1141. kwargs=dict(
  1142. warning_message=records[0],
  1143. when="config",
  1144. nodeid="",
  1145. location=location,
  1146. )
  1147. )
  1148. def addinivalue_line(self, name: str, line: str) -> None:
  1149. """Add a line to an ini-file option. The option must have been
  1150. declared but might not yet be set in which case the line becomes
  1151. the first line in its value."""
  1152. x = self.getini(name)
  1153. assert isinstance(x, list)
  1154. x.append(line) # modifies the cached list inline
  1155. def getini(self, name: str):
  1156. """Return configuration value from an :ref:`ini file <configfiles>`.
  1157. If the specified name hasn't been registered through a prior
  1158. :func:`parser.addini <pytest.Parser.addini>` call (usually from a
  1159. plugin), a ValueError is raised.
  1160. """
  1161. try:
  1162. return self._inicache[name]
  1163. except KeyError:
  1164. self._inicache[name] = val = self._getini(name)
  1165. return val
  1166. # Meant for easy monkeypatching by legacypath plugin.
  1167. # Can be inlined back (with no cover removed) once legacypath is gone.
  1168. def _getini_unknown_type(self, name: str, type: str, value: Union[str, List[str]]):
  1169. msg = f"unknown configuration type: {type}"
  1170. raise ValueError(msg, value) # pragma: no cover
  1171. def _getini(self, name: str):
  1172. try:
  1173. description, type, default = self._parser._inidict[name]
  1174. except KeyError as e:
  1175. raise ValueError(f"unknown configuration value: {name!r}") from e
  1176. override_value = self._get_override_ini_value(name)
  1177. if override_value is None:
  1178. try:
  1179. value = self.inicfg[name]
  1180. except KeyError:
  1181. if default is not None:
  1182. return default
  1183. if type is None:
  1184. return ""
  1185. return []
  1186. else:
  1187. value = override_value
  1188. # Coerce the values based on types.
  1189. #
  1190. # Note: some coercions are only required if we are reading from .ini files, because
  1191. # the file format doesn't contain type information, but when reading from toml we will
  1192. # get either str or list of str values (see _parse_ini_config_from_pyproject_toml).
  1193. # For example:
  1194. #
  1195. # ini:
  1196. # a_line_list = "tests acceptance"
  1197. # in this case, we need to split the string to obtain a list of strings.
  1198. #
  1199. # toml:
  1200. # a_line_list = ["tests", "acceptance"]
  1201. # in this case, we already have a list ready to use.
  1202. #
  1203. if type == "paths":
  1204. # TODO: This assert is probably not valid in all cases.
  1205. assert self.inipath is not None
  1206. dp = self.inipath.parent
  1207. input_values = shlex.split(value) if isinstance(value, str) else value
  1208. return [dp / x for x in input_values]
  1209. elif type == "args":
  1210. return shlex.split(value) if isinstance(value, str) else value
  1211. elif type == "linelist":
  1212. if isinstance(value, str):
  1213. return [t for t in map(lambda x: x.strip(), value.split("\n")) if t]
  1214. else:
  1215. return value
  1216. elif type == "bool":
  1217. return _strtobool(str(value).strip())
  1218. elif type == "string":
  1219. return value
  1220. elif type is None:
  1221. return value
  1222. else:
  1223. return self._getini_unknown_type(name, type, value)
  1224. def _getconftest_pathlist(
  1225. self, name: str, path: Path, rootpath: Path
  1226. ) -> Optional[List[Path]]:
  1227. try:
  1228. mod, relroots = self.pluginmanager._rget_with_confmod(
  1229. name, path, self.getoption("importmode"), rootpath
  1230. )
  1231. except KeyError:
  1232. return None
  1233. modpath = Path(mod.__file__).parent
  1234. values: List[Path] = []
  1235. for relroot in relroots:
  1236. if isinstance(relroot, os.PathLike):
  1237. relroot = Path(relroot)
  1238. else:
  1239. relroot = relroot.replace("/", os.sep)
  1240. relroot = absolutepath(modpath / relroot)
  1241. values.append(relroot)
  1242. return values
  1243. def _get_override_ini_value(self, name: str) -> Optional[str]:
  1244. value = None
  1245. # override_ini is a list of "ini=value" options.
  1246. # Always use the last item if multiple values are set for same ini-name,
  1247. # e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2.
  1248. for ini_config in self._override_ini:
  1249. try:
  1250. key, user_ini_value = ini_config.split("=", 1)
  1251. except ValueError as e:
  1252. raise UsageError(
  1253. "-o/--override-ini expects option=value style (got: {!r}).".format(
  1254. ini_config
  1255. )
  1256. ) from e
  1257. else:
  1258. if key == name:
  1259. value = user_ini_value
  1260. return value
  1261. def getoption(self, name: str, default=notset, skip: bool = False):
  1262. """Return command line option value.
  1263. :param name: Name of the option. You may also specify
  1264. the literal ``--OPT`` option instead of the "dest" option name.
  1265. :param default: Default value if no option of that name exists.
  1266. :param skip: If True, raise pytest.skip if option does not exists
  1267. or has a None value.
  1268. """
  1269. name = self._opt2dest.get(name, name)
  1270. try:
  1271. val = getattr(self.option, name)
  1272. if val is None and skip:
  1273. raise AttributeError(name)
  1274. return val
  1275. except AttributeError as e:
  1276. if default is not notset:
  1277. return default
  1278. if skip:
  1279. import pytest
  1280. pytest.skip(f"no {name!r} option found")
  1281. raise ValueError(f"no option named {name!r}") from e
  1282. def getvalue(self, name: str, path=None):
  1283. """Deprecated, use getoption() instead."""
  1284. return self.getoption(name)
  1285. def getvalueorskip(self, name: str, path=None):
  1286. """Deprecated, use getoption(skip=True) instead."""
  1287. return self.getoption(name, skip=True)
  1288. def _warn_about_missing_assertion(self, mode: str) -> None:
  1289. if not _assertion_supported():
  1290. if mode == "plain":
  1291. warning_text = (
  1292. "ASSERTIONS ARE NOT EXECUTED"
  1293. " and FAILING TESTS WILL PASS. Are you"
  1294. " using python -O?"
  1295. )
  1296. else:
  1297. warning_text = (
  1298. "assertions not in test modules or"
  1299. " plugins will be ignored"
  1300. " because assert statements are not executed "
  1301. "by the underlying Python interpreter "
  1302. "(are you using python -O?)\n"
  1303. )
  1304. self.issue_config_time_warning(
  1305. PytestConfigWarning(warning_text),
  1306. stacklevel=3,
  1307. )
  1308. def _warn_about_skipped_plugins(self) -> None:
  1309. for module_name, msg in self.pluginmanager.skipped_plugins:
  1310. self.issue_config_time_warning(
  1311. PytestConfigWarning(f"skipped plugin {module_name!r}: {msg}"),
  1312. stacklevel=2,
  1313. )
  1314. def _assertion_supported() -> bool:
  1315. try:
  1316. assert False
  1317. except AssertionError:
  1318. return True
  1319. else:
  1320. return False # type: ignore[unreachable]
  1321. def create_terminal_writer(
  1322. config: Config, file: Optional[TextIO] = None
  1323. ) -> TerminalWriter:
  1324. """Create a TerminalWriter instance configured according to the options
  1325. in the config object.
  1326. Every code which requires a TerminalWriter object and has access to a
  1327. config object should use this function.
  1328. """
  1329. tw = TerminalWriter(file=file)
  1330. if config.option.color == "yes":
  1331. tw.hasmarkup = True
  1332. elif config.option.color == "no":
  1333. tw.hasmarkup = False
  1334. if config.option.code_highlight == "yes":
  1335. tw.code_highlight = True
  1336. elif config.option.code_highlight == "no":
  1337. tw.code_highlight = False
  1338. return tw
  1339. def _strtobool(val: str) -> bool:
  1340. """Convert a string representation of truth to True or False.
  1341. True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
  1342. are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
  1343. 'val' is anything else.
  1344. .. note:: Copied from distutils.util.
  1345. """
  1346. val = val.lower()
  1347. if val in ("y", "yes", "t", "true", "on", "1"):
  1348. return True
  1349. elif val in ("n", "no", "f", "false", "off", "0"):
  1350. return False
  1351. else:
  1352. raise ValueError(f"invalid truth value {val!r}")
  1353. @lru_cache(maxsize=50)
  1354. def parse_warning_filter(
  1355. arg: str, *, escape: bool
  1356. ) -> Tuple[str, str, Type[Warning], str, int]:
  1357. """Parse a warnings filter string.
  1358. This is copied from warnings._setoption with the following changes:
  1359. * Does not apply the filter.
  1360. * Escaping is optional.
  1361. * Raises UsageError so we get nice error messages on failure.
  1362. """
  1363. __tracebackhide__ = True
  1364. error_template = dedent(
  1365. f"""\
  1366. while parsing the following warning configuration:
  1367. {arg}
  1368. This error occurred:
  1369. {{error}}
  1370. """
  1371. )
  1372. parts = arg.split(":")
  1373. if len(parts) > 5:
  1374. doc_url = (
  1375. "https://docs.python.org/3/library/warnings.html#describing-warning-filters"
  1376. )
  1377. error = dedent(
  1378. f"""\
  1379. Too many fields ({len(parts)}), expected at most 5 separated by colons:
  1380. action:message:category:module:line
  1381. For more information please consult: {doc_url}
  1382. """
  1383. )
  1384. raise UsageError(error_template.format(error=error))
  1385. while len(parts) < 5:
  1386. parts.append("")
  1387. action_, message, category_, module, lineno_ = (s.strip() for s in parts)
  1388. try:
  1389. action: str = warnings._getaction(action_) # type: ignore[attr-defined]
  1390. except warnings._OptionError as e:
  1391. raise UsageError(error_template.format(error=str(e)))
  1392. try:
  1393. category: Type[Warning] = _resolve_warning_category(category_)
  1394. except Exception:
  1395. exc_info = ExceptionInfo.from_current()
  1396. exception_text = exc_info.getrepr(style="native")
  1397. raise UsageError(error_template.format(error=exception_text))
  1398. if message and escape:
  1399. message = re.escape(message)
  1400. if module and escape:
  1401. module = re.escape(module) + r"\Z"
  1402. if lineno_:
  1403. try:
  1404. lineno = int(lineno_)
  1405. if lineno < 0:
  1406. raise ValueError("number is negative")
  1407. except ValueError as e:
  1408. raise UsageError(
  1409. error_template.format(error=f"invalid lineno {lineno_!r}: {e}")
  1410. )
  1411. else:
  1412. lineno = 0
  1413. return action, message, category, module, lineno
  1414. def _resolve_warning_category(category: str) -> Type[Warning]:
  1415. """
  1416. Copied from warnings._getcategory, but changed so it lets exceptions (specially ImportErrors)
  1417. propagate so we can get access to their tracebacks (#9218).
  1418. """
  1419. __tracebackhide__ = True
  1420. if not category:
  1421. return Warning
  1422. if "." not in category:
  1423. import builtins as m
  1424. klass = category
  1425. else:
  1426. module, _, klass = category.rpartition(".")
  1427. m = __import__(module, None, None, [klass])
  1428. cat = getattr(m, klass)
  1429. if not issubclass(cat, Warning):
  1430. raise UsageError(f"{cat} is not a Warning subclass")
  1431. return cast(Type[Warning], cat)
  1432. def apply_warning_filters(
  1433. config_filters: Iterable[str], cmdline_filters: Iterable[str]
  1434. ) -> None:
  1435. """Applies pytest-configured filters to the warnings module"""
  1436. # Filters should have this precedence: cmdline options, config.
  1437. # Filters should be applied in the inverse order of precedence.
  1438. for arg in config_filters:
  1439. warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
  1440. for arg in cmdline_filters:
  1441. warnings.filterwarnings(*parse_warning_filter(arg, escape=True))