Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Config.inifile is available during pytest_cmdline_main #9400

Merged
merged 2 commits into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/9396.bugfix.rst
@@ -0,0 +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``).
69 changes: 35 additions & 34 deletions src/_pytest/legacypath.py
Expand Up @@ -403,60 +403,61 @@ def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
self.path = Path(value)


@hookimpl
def pytest_configure(config: Config) -> None:
import pytest

mp = pytest.MonkeyPatch()
config.add_cleanup(mp.undo)

if config.pluginmanager.has_plugin("tmpdir"):
# Create TmpdirFactory and attach it to the config object.
#
# This is to comply with existing plugins which expect the handler to be
# available at pytest_configure time, but ideally should be moved entirely
# to the tmpdir_factory session fixture.
try:
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
except AttributeError:
# tmpdir plugin is blocked.
pass
else:
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)

config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")
@hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config: Config) -> None:
"""Monkeypatch legacy path attributes in several classes, as early as possible."""
mp = MonkeyPatch()
early_config.add_cleanup(mp.undo)

# Add Cache.makedir().
mp.setattr(pytest.Cache, "makedir", Cache_makedir, raising=False)
mp.setattr(Cache, "makedir", Cache_makedir, raising=False)

# Add FixtureRequest.fspath property.
mp.setattr(
pytest.FixtureRequest, "fspath", property(FixtureRequest_fspath), raising=False
)
mp.setattr(FixtureRequest, "fspath", property(FixtureRequest_fspath), raising=False)

# Add TerminalReporter.startdir property.
mp.setattr(
TerminalReporter, "startdir", property(TerminalReporter_startdir), raising=False
)

# Add Config.{invocation_dir,rootdir,inifile} properties.
mp.setattr(
pytest.Config, "invocation_dir", property(Config_invocation_dir), raising=False
)
mp.setattr(pytest.Config, "rootdir", property(Config_rootdir), raising=False)
mp.setattr(pytest.Config, "inifile", property(Config_inifile), raising=False)
mp.setattr(Config, "invocation_dir", property(Config_invocation_dir), raising=False)
mp.setattr(Config, "rootdir", property(Config_rootdir), raising=False)
mp.setattr(Config, "inifile", property(Config_inifile), raising=False)

# Add Session.startdir property.
mp.setattr(pytest.Session, "startdir", property(Session_stardir), raising=False)
mp.setattr(Session, "startdir", property(Session_stardir), raising=False)

# Add pathlist configuration type.
mp.setattr(pytest.Config, "_getini_unknown_type", Config__getini_unknown_type)
mp.setattr(Config, "_getini_unknown_type", Config__getini_unknown_type)

# Add Node.fspath property.
mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False)


@hookimpl
def pytest_configure(config: Config) -> None:
"""Installs the LegacyTmpdirPlugin if the ``tmpdir`` plugin is also installed."""
if config.pluginmanager.has_plugin("tmpdir"):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LegacyTmpdirPlugin should be installed during pytest_configure as that's called after tmpdir is installed.

mp = MonkeyPatch()
config.add_cleanup(mp.undo)
# Create TmpdirFactory and attach it to the config object.
#
# This is to comply with existing plugins which expect the handler to be
# available at pytest_configure time, but ideally should be moved entirely
# to the tmpdir_factory session fixture.
try:
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
except AttributeError:
# tmpdir plugin is blocked.
pass
else:
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)

config.pluginmanager.register(LegacyTmpdirPlugin, "legacypath-tmpdir")


@hookimpl
def pytest_plugin_registered(plugin: object, manager: PytestPluginManager) -> None:
# pytester is not loaded by default and is commonly loaded from a conftest,
Expand Down
7 changes: 4 additions & 3 deletions testing/test_config.py
Expand Up @@ -1264,15 +1264,16 @@ def pytest_load_initial_conftests(self):
m = My()
pm.register(m)
hc = pm.hook.pytest_load_initial_conftests
values = hc._nonwrappers + hc._wrappers
expected = [
assert [x.function.__module__ for x in hc._nonwrappers] == [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to also split checking wrappers and non-wrappers, seems better to explicitly test them separately.

"_pytest.config",
m.__module__,
"_pytest.legacypath",
"_pytest.pythonpath",
]
assert [x.function.__module__ for x in hc._wrappers] == [
"_pytest.capture",
"_pytest.warnings",
]
assert [x.function.__module__ for x in values] == expected


def test_get_plugin_specs_as_list() -> None:
Expand Down
17 changes: 17 additions & 0 deletions testing/test_legacypath.py
Expand Up @@ -161,3 +161,20 @@ def test_overriden(pytestconfig):
)
result = pytester.runpytest("--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s")
result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"])


def test_inifile_from_cmdline_main_hook(pytester: pytest.Pytester) -> None:
"""Ensure Config.inifile is available during pytest_cmdline_main (#9396)."""
p = pytester.makeini(
"""
[pytest]
"""
)
pytester.makeconftest(
"""
def pytest_cmdline_main(config):
print("pytest_cmdline_main inifile =", config.inifile)
"""
)
result = pytester.runpytest_subprocess("-s")
result.stdout.fnmatch_lines(f"*pytest_cmdline_main inifile = {p}")