Skip to content
Open
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/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
sys.exit(1)

from .main import dotenv_values, set_key, unset_key
from .main import DotEnv, dotenv_values, set_key, unset_key
from .version import __version__


Expand Down Expand Up @@ -190,7 +190,7 @@ def run(ctx: click.Context, override: bool, commandline: tuple[str, ...]) -> Non
)
dotenv_as_dict = {
k: v
for (k, v) in dotenv_values(file).items()
for (k, v) in DotEnv(file, override=override, encoding="utf-8").dict().items()
if v is not None and (override or k not in os.environ)
}

Expand Down
34 changes: 34 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -211,6 +212,39 @@ def test_run_with_existing_variable_not_overridden(tmp_path):
check_process(result, exit_code=0, stdout="C\n")


@pytest.mark.parametrize("options", [[], ["--override"], ["--no-override"]])
@pytest.mark.parametrize("existing_value", [None, "environment", ""])
def test_run_interpolation_respects_override(tmp_path, options, existing_value):
(tmp_path / ".env").write_text(
"DOTENV_TEST_BASE=file\nDOTENV_TEST_DERIVED=${DOTENV_TEST_BASE}/suffix\n"
)
env = dict(os.environ)
env.pop("DOTENV_TEST_BASE", None)
env.pop("DOTENV_TEST_DERIVED", None)
if existing_value is not None:
env["DOTENV_TEST_BASE"] = existing_value

result = run_dotenv(
[
"run",
*options,
sys.executable,
"-c",
"import os; print(os.environ['DOTENV_TEST_BASE']); "
"print(os.environ['DOTENV_TEST_DERIVED'])",
],
cwd=tmp_path,
env=env,
)

expected = (
existing_value
if options == ["--no-override"] and existing_value is not None
else "file"
)
check_process(result, exit_code=0, stdout=f"{expected}\n{expected}/suffix\n")


def test_run_with_none_value(tmp_path):
(tmp_path / ".env").write_text("A=x\nc")

Expand Down