Skip to content

Commit 81f9e0e

Browse files
Fix verbose source attribution when falling back to INI env (#211)
1 parent 81dc95d commit 81f9e0e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/pytest_env/plugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def _find_toml_config(early_config: pytest.Config) -> Path | None:
146146
def _config_source(early_config: pytest.Config) -> str:
147147
"""Describe the configuration source for verbose output."""
148148
if toml_path := _find_toml_config(early_config):
149-
return str(toml_path)
149+
_, entries = _load_toml_config(toml_path)
150+
if entries:
151+
return str(toml_path)
150152
if early_config.inipath:
151153
return str(early_config.inipath)
152154
return "config" # pragma: no cover

tests/test_verbose.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,31 @@ def test_verbose_shows_set_from_ini(pytester: pytest.Pytester) -> None:
2323
result = pytester.runpytest("--pytest-env-verbose")
2424

2525
result.assert_outcomes(passed=1)
26-
result.stdout.fnmatch_lines(["*pytest-env:*", "*SET*MAGIC=alpha*"])
26+
result.stdout.fnmatch_lines(["*pytest-env:*", "*SET*MAGIC=alpha*(from*pytest.ini*"])
27+
28+
29+
def test_verbose_shows_ini_source_when_pyproject_lacks_pytest_env(pytester: pytest.Pytester) -> None:
30+
(pytester.path / "test_it.py").symlink_to(Path(__file__).parent / "template.py")
31+
(pytester.path / "pyproject.toml").write_text(
32+
dedent("""\
33+
[build-system]
34+
requires = ["setuptools>=61"]
35+
build-backend = "setuptools.build_meta"
36+
"""),
37+
encoding="utf-8",
38+
)
39+
(pytester.path / "pytest.ini").write_text("[pytest]\nenv = MAGIC=alpha", encoding="utf-8")
40+
41+
new_env = {
42+
"_TEST_ENV": repr({"MAGIC": "alpha"}),
43+
"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
44+
"PYTEST_PLUGINS": "pytest_env.plugin",
45+
}
46+
with mock.patch.dict(os.environ, new_env, clear=True):
47+
result = pytester.runpytest("--pytest-env-verbose")
48+
49+
result.assert_outcomes(passed=1)
50+
result.stdout.fnmatch_lines(["*SET*MAGIC=alpha*(from*pytest.ini*"])
2751

2852

2953
def test_verbose_shows_set_from_toml(pytester: pytest.Pytester) -> None:

0 commit comments

Comments
 (0)