Plugin which implements the --lf (run last-failing) option.
| 311 | |
| 312 | |
| 313 | class LFPlugin: |
| 314 | """Plugin which implements the --lf (run last-failing) option.""" |
| 315 | |
| 316 | def __init__(self, config: Config) -> None: |
| 317 | self.config = config |
| 318 | active_keys = "lf", "failedfirst" |
| 319 | self.active = any(config.getoption(key) for key in active_keys) |
| 320 | assert config.cache |
| 321 | self.lastfailed: dict[str, bool] = config.cache.get("cache/lastfailed", {}) |
| 322 | self._previously_failed_count: int | None = None |
| 323 | self._report_status: str | None = None |
| 324 | self._skipped_files = 0 # count skipped files during collection due to --lf |
| 325 | |
| 326 | if config.getoption("lf"): |
| 327 | self._last_failed_paths = self.get_last_failed_paths() |
| 328 | config.pluginmanager.register( |
| 329 | LFPluginCollWrapper(self), "lfplugin-collwrapper" |
| 330 | ) |
| 331 | |
| 332 | def get_last_failed_paths(self) -> set[Path]: |
| 333 | """Return a set with all Paths of the previously failed nodeids and |
| 334 | their parents.""" |
| 335 | rootpath = self.config.rootpath |
| 336 | result = set() |
| 337 | for nodeid in self.lastfailed: |
| 338 | path = rootpath / nodeid.split("::")[0] |
| 339 | result.add(path) |
| 340 | result.update(path.parents) |
| 341 | return {x for x in result if x.exists()} |
| 342 | |
| 343 | def pytest_report_collectionfinish(self) -> str | None: |
| 344 | if self.active and self.config.get_verbosity() >= 0: |
| 345 | return f"run-last-failure: {self._report_status}" |
| 346 | return None |
| 347 | |
| 348 | def pytest_runtest_logreport(self, report: TestReport) -> None: |
| 349 | if (report.when == "call" and report.passed) or report.skipped: |
| 350 | self.lastfailed.pop(report.nodeid, None) |
| 351 | elif report.failed: |
| 352 | self.lastfailed[report.nodeid] = True |
| 353 | |
| 354 | def pytest_collectreport(self, report: CollectReport) -> None: |
| 355 | passed = report.outcome in ("passed", "skipped") |
| 356 | if passed: |
| 357 | if report.nodeid in self.lastfailed: |
| 358 | self.lastfailed.pop(report.nodeid) |
| 359 | self.lastfailed.update((item.nodeid, True) for item in report.result) |
| 360 | else: |
| 361 | self.lastfailed[report.nodeid] = True |
| 362 | |
| 363 | @hookimpl(wrapper=True, tryfirst=True) |
| 364 | def pytest_collection_modifyitems( |
| 365 | self, config: Config, items: list[nodes.Item] |
| 366 | ) -> Generator[None]: |
| 367 | res = yield |
| 368 | |
| 369 | if not self.active: |
| 370 | return res |