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: 3 additions & 1 deletion src/dotenv/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def __hash__(self) -> int:
def resolve(self, env: Mapping[str, Optional[str]]) -> str:
default = self.default if self.default is not None else ""
result = env.get(self.name, default)
return result if result is not None else ""
# A variable declared without a value (`FOO`) is unset, not empty, so
# a provided default applies to it just as it does to a missing name.
return result if result is not None else default


def parse_variables(value: str) -> Iterator[Atom]:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ def test_dotenv_values_file(dotenv_path):
# Undefined
({}, "a=${b}", True, {"a": ""}),
({}, "a=${b:-d}", True, {"a": "d"}),
# Declared without a value, but not empty
({}, "b\na=${b:-d}", True, {"a": "d", "b": None}),
({}, "b\na=${b}", True, {"a": "", "b": None}),
# With quotes
({"b": "c"}, 'a="${b}"', True, {"a": "c"}),
({"b": "c"}, "a='${b}'", True, {"a": "c"}),
Expand Down
13 changes: 13 additions & 0 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ def test_parse_variables(value, expected):
result = parse_variables(value)

assert list(result) == expected


@pytest.mark.parametrize(
"env,default,expected",
[
({"a": "b"}, "c", "b"),
({}, "c", "c"),
({"a": ""}, "c", ""),
({"a": None}, "c", "c"),
],
)
def test_variable_resolve(env, default, expected):
assert Variable(name="a", default=default).resolve(env) == expected
Loading