diff --git a/changelog.d/+empty-native-section.bugfix.md b/changelog.d/+empty-native-section.bugfix.md new file mode 100644 index 0000000..fd6ddff --- /dev/null +++ b/changelog.d/+empty-native-section.bugfix.md @@ -0,0 +1 @@ +Honor an empty native TOML environment section over INI `env` settings. diff --git a/src/pytest_env/plugin.py b/src/pytest_env/plugin.py index 61f8c81..d32f5e8 100644 --- a/src/pytest_env/plugin.py +++ b/src/pytest_env/plugin.py @@ -232,9 +232,8 @@ def _load_values(early_config: pytest.Config) -> Iterator[Entry]: """Load env entries from config, preferring TOML over INI.""" if toml_config := _find_toml_config(early_config): _, entries, _ = _load_toml_config(toml_config) - if entries: - yield from entries - return + yield from entries + return for line in early_config.getini("env"): # INI lines e.g. D:R:NAME=VAL has two flags (R and D), NAME key, and VAL value diff --git a/tests/test_env.py b/tests/test_env.py index 8d38098..6de592c 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -226,6 +226,33 @@ def test_env_via_pytest( ".pytest.toml", id="hidden pytest toml native over ini", ), + pytest.param( + {}, + "[tool.pytest_env]\n", + "", + "[pytest]\nenv = FROM_INI=unexpected", + {"FROM_INI": None}, + None, + id="empty pyproject native over ini", + ), + pytest.param( + {}, + "", + "[pytest_env]\n", + "[pytest]\nenv = FROM_INI=unexpected", + {"FROM_INI": None}, + "pytest.toml", + id="empty pytest toml native over ini", + ), + pytest.param( + {}, + "", + "[pytest_env]\n", + "[pytest]\nenv = FROM_INI=unexpected", + {"FROM_INI": None}, + ".pytest.toml", + id="empty hidden pytest toml native over ini", + ), pytest.param( {}, '[tool.pytest_env]\nMAGIC = {value = "toml", "transform"= true, "skip_if_set" = true}', @@ -316,9 +343,8 @@ def test_env_via_toml( "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin", } - with mock.patch.dict(os.environ, new_env, clear=True): - result = pytester.runpytest(str(test_dir)) + result = pytester.runpytest(*(("-c", str(pytester.path / "pytest.ini")) if ini else ()), str(test_dir)) result.assert_outcomes(passed=1)