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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ env = [
```

In `pytest.toml` / `.pytest.toml`, this pytest-native `[pytest]` table is an alternative to the plugin-specific
`[pytest_env]` TOML table described above. Both forms work there, so `pytest.toml` does not have to use
`[pytest_env]`.
`[pytest_env]` TOML table described above. Both forms work there, so `pytest.toml` does not have to use `[pytest_env]`.

Prefix flags modify behavior. Flags are case-insensitive and can be combined in any order (e.g., `R:D:KEY=VALUE`):

Expand Down Expand Up @@ -243,8 +242,8 @@ MESSAGE="hello\nworld"
API_KEY=${FALLBACK_KEY:-default_key}
```

Missing `.env` files from configuration are silently skipped. Paths are resolved relative to the project root.
An explicit `env_files = []` in native TOML configuration disables INI-style `env_files` fallback; omitting the key
Missing `.env` files from configuration are silently skipped. Paths are resolved relative to the project root. An
explicit `env_files = []` in native TOML configuration disables INI-style `env_files` fallback; omitting the key
preserves that fallback. CLI `--envfile` paths still load in either case.

### CLI options
Expand Down
8 changes: 5 additions & 3 deletions src/pytest_env/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import tomllib
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Final

import pytest
from dotenv import dotenv_values
Expand Down Expand Up @@ -194,7 +194,7 @@ def _load_toml_config(config_path: Path) -> tuple[list[str] | None, list[Entry],
return None, [], None

raw_env_files = pytest_env_config.get("env_files")
env_files = [str(f) for f in raw_env_files] if isinstance(raw_env_files, list) else None
env_files: Final[list[str] | None] = [str(f) for f in raw_env_files] if isinstance(raw_env_files, list) else None
raw_skip = pytest_env_config.get("env_files_skip_if_set")
env_files_skip_if_set = raw_skip if isinstance(raw_skip, bool) else None

Expand All @@ -204,7 +204,9 @@ def _load_toml_config(config_path: Path) -> tuple[list[str] | None, list[Entry],

def _load_env_files(early_config: pytest.Config, env_files: list[str] | None) -> Generator[Path, None, None]:
"""Resolve and yield existing env files, with CLI option taking precedence."""
configured_env_files = env_files if env_files is not None else list(early_config.getini("env_files"))
configured_env_files: Final[list[str]] = (
env_files if env_files is not None else list(early_config.getini("env_files"))
)
if cli_envfile := getattr(early_config.known_args_namespace, "envfile", None):
if cli_envfile.startswith("+"):
if not (resolved := early_config.rootpath / cli_envfile[1:]).is_file():
Expand Down
22 changes: 10 additions & 12 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,28 +787,26 @@ def test_native_env_files_override_ini(
) -> None:
(pytester.path / "fallback.env").write_text("INHERITED=from_ini\n", encoding="utf-8")
(pytester.path / "cli.env").write_text("FROM_CLI=from_cli\n", encoding="utf-8")
section = "tool.pytest_env" if config_name == "pyproject.toml" else "pytest_env"
native = f"[{section}]\n" + ("env_files = []\n" if empty_env_files else "")
native = f"[{'tool.pytest_env' if config_name == 'pyproject.toml' else 'pytest_env'}]\n"
if empty_env_files:
native += "env_files = []\n"
if config_name == "pyproject.toml":
pytester.makeini("[pytest]\nenv_files = fallback.env\n")
else:
native += '\n[pytest]\nenv_files = ["fallback.env"]\n'
(pytester.path / config_name).write_text(native, encoding="utf-8")
expected_inherited = None if empty_env_files else "from_ini"
expected_cli = "from_cli" if extend else None
expected_env: Final[tuple[str | None, str | None]] = (
None if empty_env_files else "from_ini",
"from_cli" if extend else None,
)
pytester.makepyfile(
test_env=dedent(f"""\
import os

def test_env() -> None:
assert (os.environ.get("INHERITED"), os.environ.get("FROM_CLI")) == (
{expected_inherited!r}, {expected_cli!r}
)
assert (os.environ.get("INHERITED"), os.environ.get("FROM_CLI")) == {expected_env!r}
"""),
)
new_env = {"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1", "PYTEST_PLUGINS": "pytest_env.plugin"}
args = ["--envfile", "+cli.env"] if extend else []
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):
result = pytester.runpytest_subprocess(*args)
result.assert_outcomes(passed=1)
assert result.ret == pytest.ExitCode.OK
pytester.runpytest_subprocess(*(["--envfile", "+cli.env"] if extend else [])).assert_outcomes(passed=1)