cacheprovider.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. # -*- coding: utf-8 -*-
  2. """
  3. merged implementation of the cache provider
  4. the name cache was not chosen to ensure pluggy automatically
  5. ignores the external pytest-cache
  6. """
  7. from __future__ import absolute_import
  8. from __future__ import division
  9. from __future__ import print_function
  10. import json
  11. import os
  12. from collections import OrderedDict
  13. import attr
  14. import py
  15. import six
  16. import pytest
  17. from .compat import _PY2 as PY2
  18. from .pathlib import Path
  19. from .pathlib import resolve_from_str
  20. from .pathlib import rm_rf
  21. README_CONTENT = u"""\
  22. # pytest cache directory #
  23. This directory contains data from the pytest's cache plugin,
  24. which provides the `--lf` and `--ff` options, as well as the `cache` fixture.
  25. **Do not** commit this to version control.
  26. See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information.
  27. """
  28. CACHEDIR_TAG_CONTENT = b"""\
  29. Signature: 8a477f597d28d172789f06886806bc55
  30. # This file is a cache directory tag created by pytest.
  31. # For information about cache directory tags, see:
  32. # http://www.bford.info/cachedir/spec.html
  33. """
  34. @attr.s
  35. class Cache(object):
  36. _cachedir = attr.ib(repr=False)
  37. _config = attr.ib(repr=False)
  38. @classmethod
  39. def for_config(cls, config):
  40. cachedir = cls.cache_dir_from_config(config)
  41. if config.getoption("cacheclear") and cachedir.exists():
  42. rm_rf(cachedir)
  43. cachedir.mkdir()
  44. return cls(cachedir, config)
  45. @staticmethod
  46. def cache_dir_from_config(config):
  47. return resolve_from_str(config.getini("cache_dir"), config.rootdir)
  48. def warn(self, fmt, **args):
  49. from _pytest.warnings import _issue_warning_captured
  50. from _pytest.warning_types import PytestCacheWarning
  51. _issue_warning_captured(
  52. PytestCacheWarning(fmt.format(**args) if args else fmt),
  53. self._config.hook,
  54. stacklevel=3,
  55. )
  56. def makedir(self, name):
  57. """ return a directory path object with the given name. If the
  58. directory does not yet exist, it will be created. You can use it
  59. to manage files likes e. g. store/retrieve database
  60. dumps across test sessions.
  61. :param name: must be a string not containing a ``/`` separator.
  62. Make sure the name contains your plugin or application
  63. identifiers to prevent clashes with other cache users.
  64. """
  65. name = Path(name)
  66. if len(name.parts) > 1:
  67. raise ValueError("name is not allowed to contain path separators")
  68. res = self._cachedir.joinpath("d", name)
  69. res.mkdir(exist_ok=True, parents=True)
  70. return py.path.local(res)
  71. def _getvaluepath(self, key):
  72. return self._cachedir.joinpath("v", Path(key))
  73. def get(self, key, default):
  74. """ return cached value for the given key. If no value
  75. was yet cached or the value cannot be read, the specified
  76. default is returned.
  77. :param key: must be a ``/`` separated value. Usually the first
  78. name is the name of your plugin or your application.
  79. :param default: must be provided in case of a cache-miss or
  80. invalid cache values.
  81. """
  82. path = self._getvaluepath(key)
  83. try:
  84. with path.open("r") as f:
  85. return json.load(f)
  86. except (ValueError, IOError, OSError):
  87. return default
  88. def set(self, key, value):
  89. """ save value for the given key.
  90. :param key: must be a ``/`` separated value. Usually the first
  91. name is the name of your plugin or your application.
  92. :param value: must be of any combination of basic
  93. python types, including nested types
  94. like e. g. lists of dictionaries.
  95. """
  96. path = self._getvaluepath(key)
  97. try:
  98. if path.parent.is_dir():
  99. cache_dir_exists_already = True
  100. else:
  101. cache_dir_exists_already = self._cachedir.exists()
  102. path.parent.mkdir(exist_ok=True, parents=True)
  103. except (IOError, OSError):
  104. self.warn("could not create cache path {path}", path=path)
  105. return
  106. if not cache_dir_exists_already:
  107. self._ensure_supporting_files()
  108. try:
  109. f = path.open("wb" if PY2 else "w")
  110. except (IOError, OSError):
  111. self.warn("cache could not write path {path}", path=path)
  112. else:
  113. with f:
  114. json.dump(value, f, indent=2, sort_keys=True)
  115. def _ensure_supporting_files(self):
  116. """Create supporting files in the cache dir that are not really part of the cache."""
  117. readme_path = self._cachedir / "README.md"
  118. readme_path.write_text(README_CONTENT)
  119. gitignore_path = self._cachedir.joinpath(".gitignore")
  120. msg = u"# Created by pytest automatically.\n*"
  121. gitignore_path.write_text(msg, encoding="UTF-8")
  122. cachedir_tag_path = self._cachedir.joinpath("CACHEDIR.TAG")
  123. cachedir_tag_path.write_bytes(CACHEDIR_TAG_CONTENT)
  124. class LFPlugin(object):
  125. """ Plugin which implements the --lf (run last-failing) option """
  126. def __init__(self, config):
  127. self.config = config
  128. active_keys = "lf", "failedfirst"
  129. self.active = any(config.getoption(key) for key in active_keys)
  130. self.lastfailed = config.cache.get("cache/lastfailed", {})
  131. self._previously_failed_count = None
  132. self._report_status = None
  133. self._skipped_files = 0 # count skipped files during collection due to --lf
  134. def last_failed_paths(self):
  135. """Returns a set with all Paths()s of the previously failed nodeids (cached).
  136. """
  137. try:
  138. return self._last_failed_paths
  139. except AttributeError:
  140. rootpath = Path(self.config.rootdir)
  141. result = {rootpath / nodeid.split("::")[0] for nodeid in self.lastfailed}
  142. result = {x for x in result if x.exists()}
  143. self._last_failed_paths = result
  144. return result
  145. def pytest_ignore_collect(self, path):
  146. """
  147. Ignore this file path if we are in --lf mode and it is not in the list of
  148. previously failed files.
  149. """
  150. if self.active and self.config.getoption("lf") and path.isfile():
  151. last_failed_paths = self.last_failed_paths()
  152. if last_failed_paths:
  153. skip_it = Path(path) not in self.last_failed_paths()
  154. if skip_it:
  155. self._skipped_files += 1
  156. return skip_it
  157. def pytest_report_collectionfinish(self):
  158. if self.active and self.config.getoption("verbose") >= 0:
  159. return "run-last-failure: %s" % self._report_status
  160. def pytest_runtest_logreport(self, report):
  161. if (report.when == "call" and report.passed) or report.skipped:
  162. self.lastfailed.pop(report.nodeid, None)
  163. elif report.failed:
  164. self.lastfailed[report.nodeid] = True
  165. def pytest_collectreport(self, report):
  166. passed = report.outcome in ("passed", "skipped")
  167. if passed:
  168. if report.nodeid in self.lastfailed:
  169. self.lastfailed.pop(report.nodeid)
  170. self.lastfailed.update((item.nodeid, True) for item in report.result)
  171. else:
  172. self.lastfailed[report.nodeid] = True
  173. def pytest_collection_modifyitems(self, session, config, items):
  174. if not self.active:
  175. return
  176. if self.lastfailed:
  177. previously_failed = []
  178. previously_passed = []
  179. for item in items:
  180. if item.nodeid in self.lastfailed:
  181. previously_failed.append(item)
  182. else:
  183. previously_passed.append(item)
  184. self._previously_failed_count = len(previously_failed)
  185. if not previously_failed:
  186. # Running a subset of all tests with recorded failures
  187. # only outside of it.
  188. self._report_status = "%d known failures not in selected tests" % (
  189. len(self.lastfailed),
  190. )
  191. else:
  192. if self.config.getoption("lf"):
  193. items[:] = previously_failed
  194. config.hook.pytest_deselected(items=previously_passed)
  195. else: # --failedfirst
  196. items[:] = previously_failed + previously_passed
  197. noun = "failure" if self._previously_failed_count == 1 else "failures"
  198. suffix = " first" if self.config.getoption("failedfirst") else ""
  199. self._report_status = "rerun previous {count} {noun}{suffix}".format(
  200. count=self._previously_failed_count, suffix=suffix, noun=noun
  201. )
  202. if self._skipped_files > 0:
  203. files_noun = "file" if self._skipped_files == 1 else "files"
  204. self._report_status += " (skipped {files} {files_noun})".format(
  205. files=self._skipped_files, files_noun=files_noun
  206. )
  207. else:
  208. self._report_status = "no previously failed tests, "
  209. if self.config.getoption("last_failed_no_failures") == "none":
  210. self._report_status += "deselecting all items."
  211. config.hook.pytest_deselected(items=items)
  212. items[:] = []
  213. else:
  214. self._report_status += "not deselecting items."
  215. def pytest_sessionfinish(self, session):
  216. config = self.config
  217. if config.getoption("cacheshow") or hasattr(config, "slaveinput"):
  218. return
  219. saved_lastfailed = config.cache.get("cache/lastfailed", {})
  220. if saved_lastfailed != self.lastfailed:
  221. config.cache.set("cache/lastfailed", self.lastfailed)
  222. class NFPlugin(object):
  223. """ Plugin which implements the --nf (run new-first) option """
  224. def __init__(self, config):
  225. self.config = config
  226. self.active = config.option.newfirst
  227. self.cached_nodeids = config.cache.get("cache/nodeids", [])
  228. def pytest_collection_modifyitems(self, session, config, items):
  229. if self.active:
  230. new_items = OrderedDict()
  231. other_items = OrderedDict()
  232. for item in items:
  233. if item.nodeid not in self.cached_nodeids:
  234. new_items[item.nodeid] = item
  235. else:
  236. other_items[item.nodeid] = item
  237. items[:] = self._get_increasing_order(
  238. six.itervalues(new_items)
  239. ) + self._get_increasing_order(six.itervalues(other_items))
  240. self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)]
  241. def _get_increasing_order(self, items):
  242. return sorted(items, key=lambda item: item.fspath.mtime(), reverse=True)
  243. def pytest_sessionfinish(self, session):
  244. config = self.config
  245. if config.getoption("cacheshow") or hasattr(config, "slaveinput"):
  246. return
  247. config.cache.set("cache/nodeids", self.cached_nodeids)
  248. def pytest_addoption(parser):
  249. group = parser.getgroup("general")
  250. group.addoption(
  251. "--lf",
  252. "--last-failed",
  253. action="store_true",
  254. dest="lf",
  255. help="rerun only the tests that failed "
  256. "at the last run (or all if none failed)",
  257. )
  258. group.addoption(
  259. "--ff",
  260. "--failed-first",
  261. action="store_true",
  262. dest="failedfirst",
  263. help="run all tests but run the last failures first. "
  264. "This may re-order tests and thus lead to "
  265. "repeated fixture setup/teardown",
  266. )
  267. group.addoption(
  268. "--nf",
  269. "--new-first",
  270. action="store_true",
  271. dest="newfirst",
  272. help="run tests from new files first, then the rest of the tests "
  273. "sorted by file mtime",
  274. )
  275. group.addoption(
  276. "--cache-show",
  277. action="append",
  278. nargs="?",
  279. dest="cacheshow",
  280. help=(
  281. "show cache contents, don't perform collection or tests. "
  282. "Optional argument: glob (default: '*')."
  283. ),
  284. )
  285. group.addoption(
  286. "--cache-clear",
  287. action="store_true",
  288. dest="cacheclear",
  289. help="remove all cache contents at start of test run.",
  290. )
  291. cache_dir_default = ".pytest_cache"
  292. if "TOX_ENV_DIR" in os.environ:
  293. cache_dir_default = os.path.join(os.environ["TOX_ENV_DIR"], cache_dir_default)
  294. parser.addini("cache_dir", default=cache_dir_default, help="cache directory path.")
  295. group.addoption(
  296. "--lfnf",
  297. "--last-failed-no-failures",
  298. action="store",
  299. dest="last_failed_no_failures",
  300. choices=("all", "none"),
  301. default="all",
  302. help="which tests to run with no previously (known) failures.",
  303. )
  304. def pytest_cmdline_main(config):
  305. if config.option.cacheshow:
  306. from _pytest.main import wrap_session
  307. return wrap_session(config, cacheshow)
  308. @pytest.hookimpl(tryfirst=True)
  309. def pytest_configure(config):
  310. config.cache = Cache.for_config(config)
  311. config.pluginmanager.register(LFPlugin(config), "lfplugin")
  312. config.pluginmanager.register(NFPlugin(config), "nfplugin")
  313. @pytest.fixture
  314. def cache(request):
  315. """
  316. Return a cache object that can persist state between testing sessions.
  317. cache.get(key, default)
  318. cache.set(key, value)
  319. Keys must be a ``/`` separated value, where the first part is usually the
  320. name of your plugin or application to avoid clashes with other cache users.
  321. Values can be any object handled by the json stdlib module.
  322. """
  323. return request.config.cache
  324. def pytest_report_header(config):
  325. """Display cachedir with --cache-show and if non-default."""
  326. if config.option.verbose > 0 or config.getini("cache_dir") != ".pytest_cache":
  327. cachedir = config.cache._cachedir
  328. # TODO: evaluate generating upward relative paths
  329. # starting with .., ../.. if sensible
  330. try:
  331. displaypath = cachedir.relative_to(config.rootdir)
  332. except ValueError:
  333. displaypath = cachedir
  334. return "cachedir: {}".format(displaypath)
  335. def cacheshow(config, session):
  336. from pprint import pformat
  337. tw = py.io.TerminalWriter()
  338. tw.line("cachedir: " + str(config.cache._cachedir))
  339. if not config.cache._cachedir.is_dir():
  340. tw.line("cache is empty")
  341. return 0
  342. glob = config.option.cacheshow[0]
  343. if glob is None:
  344. glob = "*"
  345. dummy = object()
  346. basedir = config.cache._cachedir
  347. vdir = basedir / "v"
  348. tw.sep("-", "cache values for %r" % glob)
  349. for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
  350. key = valpath.relative_to(vdir)
  351. val = config.cache.get(key, dummy)
  352. if val is dummy:
  353. tw.line("%s contains unreadable content, will be ignored" % key)
  354. else:
  355. tw.line("%s contains:" % key)
  356. for line in pformat(val).splitlines():
  357. tw.line(" " + line)
  358. ddir = basedir / "d"
  359. if ddir.is_dir():
  360. contents = sorted(ddir.rglob(glob))
  361. tw.sep("-", "cache directories for %r" % glob)
  362. for p in contents:
  363. # if p.check(dir=1):
  364. # print("%s/" % p.relto(basedir))
  365. if p.is_file():
  366. key = p.relative_to(basedir)
  367. tw.line("{} is a file of length {:d}".format(key, p.stat().st_size))
  368. return 0