From 13d25f18aa3e90f7e85fd0761048ab9c5f3f73f9 Mon Sep 17 00:00:00 2001 From: Zhuyuyangyy <193758752+Zhuyuyangyy@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:01:20 +0800 Subject: [PATCH 1/2] fix: honor empty native TOML environment sections --- changelog.d/+empty-native-section.bugfix.md | 1 + src/pytest_env/plugin.py | 5 ++-- tests/test_env.py | 30 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 changelog.d/+empty-native-section.bugfix.md 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..79f909e 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,10 @@ def test_env_via_toml( "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin", } + pytest_args = ("-c", str(pytester.path / "pytest.ini")) if ini else () with mock.patch.dict(os.environ, new_env, clear=True): - result = pytester.runpytest(str(test_dir)) + result = pytester.runpytest(*pytest_args, str(test_dir)) result.assert_outcomes(passed=1) From 39d9c139370046d450f8ee0bb5d47c07db20f83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Sat, 31 Oct 2026 07:33:07 -0700 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=92=84=20style(test):=20inline=20one-?= =?UTF-8?q?use=20pytest=20args?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_env.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_env.py b/tests/test_env.py index 79f909e..6de592c 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -343,10 +343,8 @@ def test_env_via_toml( "PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin", } - pytest_args = ("-c", str(pytester.path / "pytest.ini")) if ini else () - with mock.patch.dict(os.environ, new_env, clear=True): - result = pytester.runpytest(*pytest_args, str(test_dir)) + result = pytester.runpytest(*(("-c", str(pytester.path / "pytest.ini")) if ini else ()), str(test_dir)) result.assert_outcomes(passed=1)