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
2 changes: 1 addition & 1 deletion src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def set_as_environment_variables(self) -> bool:
"""
Load the current dotenv as system environment variable.
"""
if not self.dict():
if not any(value is not None for value in self.dict().values()):
return False

for k, v in self.dict().items():
Expand Down
48 changes: 48 additions & 0 deletions tests/test_load_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import io
import os
from unittest import mock

import pytest

from dotenv import dotenv_values, load_dotenv


@pytest.mark.parametrize("override", [False, True])
@pytest.mark.parametrize("source", ["file", "stream"])
@pytest.mark.parametrize("existing", [False, True])
@pytest.mark.parametrize(
"content", ["DOTENV_TEST_KEY", "# comment\nDOTENV_TEST_KEY\nOTHER_KEY\n"]
)
def test_load_dotenv_without_values_returns_false(
tmp_path, override, source, existing, content
):
environ = {"DOTENV_TEST_KEY": "existing"} if existing else {}
with mock.patch.dict(os.environ, environ, clear=True):
if source == "file":
path = tmp_path / ".env"
path.write_text(content)
result = load_dotenv(path, override=override)
else:
result = load_dotenv(stream=io.StringIO(content), override=override)

assert result is False
assert os.environ == environ

assert dotenv_values(stream=io.StringIO(content))["DOTENV_TEST_KEY"] is None


@pytest.mark.parametrize("override", [False, True])
@pytest.mark.parametrize(
"content",
[
"DOTENV_TEST_KEY=",
"BARE_KEY\nDOTENV_TEST_KEY=",
"DOTENV_TEST_KEY=value\nBARE_KEY",
],
)
def test_load_dotenv_with_values_returns_true(override, content):
with mock.patch.dict(os.environ, {}, clear=True):
result = load_dotenv(stream=io.StringIO(content), override=override)

assert result is True
assert os.environ == {"DOTENV_TEST_KEY": "value" if "=value" in content else ""}
Loading