Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pytest_env/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ def _apply_env_files(
for env_file in _load_env_files(early_config, env_files_list):
for key, value in dotenv_values(env_file).items():
if value is not None:
if skip_if_set and key in preexisting:
if skip_if_set and (env_key := key.upper() if os.name == "nt" else key) in preexisting:
if actions is not None:
actions.append(("SKIP", key, preexisting[key], str(env_file)))
actions.append(("SKIP", key, preexisting[env_key], str(env_file)))
else:
os.environ[key] = value
if actions is not None:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,38 @@ def test_env() -> None:
new_env: Final[dict[str, str]] = {"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin"}
with mock.patch.dict(os.environ, new_env, clear=True):
pytester.runpytest_subprocess(*(["--envfile", "+cli.env"] if extend else [])).assert_outcomes(passed=1)


@pytest.mark.parametrize(
"file_key",
[
pytest.param("PYTEST_ENV_CASE", id="same-case"),
pytest.param("pytest_env_case", id="different-case"),
],
)
def test_env_files_skip_if_set_respects_platform_case(pytester: pytest.Pytester, file_key: str) -> None:
expected: Final[str] = "from_file" if os.name != "nt" and file_key == "pytest_env_case" else "original"
(pytester.path / ".env").write_text(f"{file_key}=from_file\n", encoding="utf-8")
(pytester.path / "pytest.ini").write_text(
"[pytest]\nenv_files = .env\nenv_files_skip_if_set = true\n", encoding="utf-8"
)
pytester.makepyfile(
test_case=(
f'import os\ndef test_case() -> None:\n assert os.environ["{file_key}"] == {expected!r}\n'
' assert os.environ["PYTEST_ENV_CASE"] == "original"\n'
)
)
with mock.patch.dict(
os.environ,
{
"PYTEST_ENV_CASE": "original",
"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
"PYTEST_PLUGINS": "pytest_env.plugin",
},
clear=True,
):
result: Final[pytest.RunResult] = pytester.runpytest("--pytest-env-verbose")

result.assert_outcomes(passed=1)
action: Final[str] = "SET" if expected == "from_file" else "SKIP"
result.stdout.fnmatch_lines([f"*{action}*{file_key}={expected}*(from*.env*"])