Skip to content
Closed
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
11 changes: 6 additions & 5 deletions src/pytest_env/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ def _existing_env_files(early_config: pytest.Config, env_files: list[str]) -> Ge


def _regular_env_file(candidates: tuple[Path, ...]) -> Path | None:
if path := next((path for path in candidates if path.is_file()), None):
return path
if path := next((path for path in candidates if path.exists()), None):
msg = f"Environment file is not a regular file: {path}"
raise pytest.UsageError(msg)
for path in candidates:
if path.is_file():
return path
if path.exists():
msg = f"Environment file is not a regular file: {path}"
raise pytest.UsageError(msg)
return None


Expand Down
18 changes: 18 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,24 @@ def test_envfile_cli_falls_back_to_rootdir(
pytester.runpytest("--envfile=root.env").assert_outcomes(passed=1)


def test_envfile_cli_does_not_fall_back_past_invocation_directory(
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
pytester.makeini("[pytest]\n")
(pytester.path / "data.env").write_text("FROM=root\n", encoding="utf-8")
path: Final[Path] = pytester.mkdir("sub") / "data.env"
path.mkdir()
(path.parent / "test_it.py").write_text("def test_it() -> None:\n pass\n", encoding="utf-8")

monkeypatch.chdir(path.parent)
result = pytester.runpytest("--envfile=data.env")

assert (result.ret, result.errlines) == (
pytest.ExitCode.USAGE_ERROR,
[f"ERROR: Environment file is not a regular file: {path}", ""],
)


@pytest.mark.parametrize(
"config_name",
[
Expand Down