Skip to content

Commit ad11919

Browse files
committed
Lock pytest 9.0.3 to keep conftest fixtures
pytest 9.1.0 and 9.1.1 bind a conftest's fixtures to the first Package node collected for its directory. A later file argument sitting directly in the parent directory re-collects that parent and creates a second Package node for the same path, so tests collected through it silently lose the conftest's autouse fixtures (pytest-dev/pytest#14635). Here that dropped tests/unit/reconciler/conftest.py's JIRA_PROJECT=DIG pin for tests/unit/reconciler/orchestrate whenever a run listed a tests/unit/reconciler path, then a tests/unit-level file such as test_raw_git_write_lint.py, then the orchestrate directory. 22 reconcile tests then failed on an empty Jira project. The failure looked concurrency-dependent only because the serial and paired re-runs never used that argument order. Exclude the affected releases from the dev extra and lock pytest 9.0.3, the newest release without the defect. 9.2.0 fixes #14635 but is not usable yet: its monkeypatch undo deletes starlette State attributes (pytest-dev/pytest#15099, still open), failing 6 review-bot tests, and it crashes mutmut 3.7.0's stats run (boxed/mutmut#615). Add a regression test that runs the installed pytest on a throwaway project with the same argument shape. rebar-ticket: 81da-a1b4-bf29-4ad2 Signed-off-by: Rebar Bot Nava <[email protected]> Change-Id: Id46287a91216a9898d5300014cc52fb3dd89f16b
1 parent 374cc2f commit ad11919

3 files changed

Lines changed: 94 additions & 6 deletions

File tree

‎pyproject.toml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ grounding-terraform = [
237237
# multiple-bundles corruption.
238238
s3 = ["git-remote-s3>=0.3.2"]
239239
dev = [
240-
"pytest>=8",
240+
# pytest 9.1.0/9.1.1 drop a package conftest's fixtures when a parent-directory file
241+
# argument re-collects the package (pytest-dev/pytest#14635); bug 81da-a1b4-bf29-4ad2.
242+
# tests/unit/test_pytest_interleaved_conftest_args.py guards it. uv.lock holds 9.0.3:
243+
# 9.2.0 fixes #14635 but its monkeypatch undo deletes starlette State attributes
244+
# (pytest-dev/pytest#15099, open) and it breaks mutmut 3.7.0's stats run.
245+
"pytest>=8,!=9.1.0,!=9.1.1",
241246
"mcp>=1.28.1,<2",
242247
# jsonschema/referencing moved to core [project.dependencies] — the schema
243248
# registry + workflow contract validation are lean-runtime paths, not dev-only.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""The pinned pytest keeps a package's conftest fixtures across interleaved path args.
2+
3+
Bug 81da-a1b4-bf29-4ad2 (releasable-cankerous-hammerkop): pytest 9.1.0 and 9.1.1 bind a
4+
conftest's fixtures to the first ``Package`` node collected for its directory. A later
5+
file argument that sits directly in the parent directory re-collects that parent and
6+
creates a second ``Package`` node for the same path, so tests collected through it lose
7+
the conftest's autouse fixtures without any error (pytest-dev/pytest#14635, fixed in
8+
9.2.0). In this repository that dropped ``tests/unit/reconciler/conftest.py``'s
9+
``JIRA_PROJECT=DIG`` pin for ``tests/unit/reconciler/orchestrate`` whenever a run listed a
10+
``tests/unit/reconciler`` path, then ``tests/unit/test_raw_git_write_lint.py``, then the
11+
orchestrate directory, and 22 reconcile tests failed on an empty Jira project.
12+
13+
This test rebuilds that argument shape in a throwaway project and runs the installed
14+
pytest on it, so a lock that resolves to an affected pytest fails here.
15+
"""
16+
17+
from __future__ import annotations
18+
19+
from pathlib import Path
20+
21+
from _nested_pytest import run_nested_pytest
22+
from _subprocess_env import subprocess_env
23+
24+
_CONFTEST = """\
25+
import pytest
26+
27+
28+
@pytest.fixture(autouse=True)
29+
def _package_pin():
30+
return "pinned"
31+
"""
32+
33+
_NEEDS_PIN = """\
34+
def test_package_autouse_fixture_is_active(request):
35+
assert "_package_pin" in request.fixturenames
36+
"""
37+
38+
39+
def _write(path: Path, text: str) -> None:
40+
path.parent.mkdir(parents=True, exist_ok=True)
41+
path.write_text(text, encoding="utf-8")
42+
43+
44+
def _build_project(root: Path) -> None:
45+
"""Mirror ``tests/unit`` (no ``__init__``): a package with a conftest and a subpackage."""
46+
_write(root / "pytest.ini", "[pytest]\n")
47+
_write(root / "tests" / "unit" / "test_top.py", "def test_top():\n pass\n")
48+
pkg = root / "tests" / "unit" / "pkg"
49+
_write(pkg / "__init__.py", "")
50+
_write(pkg / "conftest.py", _CONFTEST)
51+
_write(pkg / "test_first.py", _NEEDS_PIN)
52+
_write(pkg / "sub" / "__init__.py", "")
53+
_write(pkg / "sub" / "test_second.py", _NEEDS_PIN)
54+
55+
56+
def test_conftest_fixtures_survive_a_parent_dir_file_arg_between_package_args(
57+
tmp_path: Path,
58+
) -> None:
59+
"""Leaving the package for a parent-level file and re-entering must keep its fixtures."""
60+
project = tmp_path / "project"
61+
_build_project(project)
62+
env = subprocess_env()
63+
env.pop("PYTEST_ADDOPTS", None)
64+
# Only pytest's own collection is under test; third-party plugins stay out.
65+
env["PYTEST_DISABLE_PLUGIN_AUTOLOAD"] = "1"
66+
67+
result = run_nested_pytest(
68+
tmp_path / "run",
69+
"-q",
70+
"tests/unit/pkg/test_first.py",
71+
"tests/unit/test_top.py",
72+
"tests/unit/pkg/sub",
73+
env=env,
74+
timeout=120,
75+
cwd=project,
76+
)
77+
78+
assert result.returncode == 0, (
79+
"The installed pytest dropped tests/unit/pkg/conftest.py's autouse fixture for "
80+
"tests collected after a parent-directory file argument (pytest-dev/pytest#14635; "
81+
f"affects 9.1.0 and 9.1.1).\n{result.stdout}\n{result.stderr}"
82+
)
83+
assert "3 passed" in result.stdout, result.stdout

‎uv.lock‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)