Skip to content

Commit 9510bd7

Browse files
committed
Ensure Config.inifile is available during pytest_cmdline_main
Fix #9396
1 parent 7ae23ff commit 9510bd7

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

changelog/9396.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure :attr:`pytest.Config.inifile` is available during the :func:`pytest_cmdline_main <_pytest.hookspec.pytest_cmdline_main>` hook (regression during ``7.0.0rc1``).

src/_pytest/legacypath.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,27 +400,11 @@ def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
400400
self.path = Path(value)
401401

402402

403-
@pytest.hookimpl
404-
def pytest_configure(config: pytest.Config) -> None:
403+
@pytest.hookimpl(tryfirst=True)
404+
def pytest_load_initial_conftests(early_config: pytest.Config) -> None:
405+
"""Monkeypatch legacy path attributes in several classes, as early as possible."""
405406
mp = pytest.MonkeyPatch()
406-
config.add_cleanup(mp.undo)
407-
408-
if config.pluginmanager.has_plugin("tmpdir"):
409-
# Create TmpdirFactory and attach it to the config object.
410-
#
411-
# This is to comply with existing plugins which expect the handler to be
412-
# available at pytest_configure time, but ideally should be moved entirely
413-
# to the tmpdir_factory session fixture.
414-
try:
415-
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
416-
except AttributeError:
417-
# tmpdir plugin is blocked.
418-
pass
419-
else:
420-
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
421-
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
422-
423-
config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")
407+
early_config.add_cleanup(mp.undo)
424408

425409
# Add Cache.makedir().
426410
mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)
@@ -452,6 +436,29 @@ def pytest_configure(config: pytest.Config) -> None:
452436
mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False)
453437

454438

439+
@pytest.hookimpl
440+
def pytest_configure(config: pytest.Config) -> None:
441+
"""Installs the LegacyTmpdirPlugin if the ``tmpdir`` plugin is also installed."""
442+
if config.pluginmanager.has_plugin("tmpdir"):
443+
mp = pytest.MonkeyPatch()
444+
config.add_cleanup(mp.undo)
445+
# Create TmpdirFactory and attach it to the config object.
446+
#
447+
# This is to comply with existing plugins which expect the handler to be
448+
# available at pytest_configure time, but ideally should be moved entirely
449+
# to the tmpdir_factory session fixture.
450+
try:
451+
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
452+
except AttributeError:
453+
# tmpdir plugin is blocked.
454+
pass
455+
else:
456+
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
457+
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
458+
459+
config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")
460+
461+
455462
@pytest.hookimpl
456463
def pytest_plugin_registered(
457464
plugin: object, manager: pytest.PytestPluginManager

testing/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ def pytest_load_initial_conftests(self):
12681268
expected = [
12691269
"_pytest.config",
12701270
m.__module__,
1271+
"_pytest.legacypath",
12711272
"_pytest.pythonpath",
12721273
"_pytest.capture",
12731274
"_pytest.warnings",

testing/test_legacypath.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,20 @@ def test_overriden(pytestconfig):
161161
)
162162
result = pytester.runpytest("--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s")
163163
result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"])
164+
165+
166+
def test_inifile_from_cmdline_main_hook(pytester: pytest.Pytester) -> None:
167+
"""Ensure Config.inifile is available during pytest_cmdline_main (#9396)."""
168+
p = pytester.makeini(
169+
"""
170+
[pytest]
171+
"""
172+
)
173+
pytester.makeconftest(
174+
"""
175+
def pytest_cmdline_main(config):
176+
print("pytest_cmdline_main inifile =", config.inifile)
177+
"""
178+
)
179+
result = pytester.runpytest_subprocess("-s")
180+
result.stdout.fnmatch_lines(f"*pytest_cmdline_main inifile = {p}")

0 commit comments

Comments
 (0)