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/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def get_cli_string(
command.append(action)
if key:
command.append(key)
if value:
if " " in value:
if value is not None:
if not value or " " in value:
command.append(f'"{value}"')
else:
command.append(value)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import shlex

from dotenv import dotenv_values
from dotenv import get_cli_string as c
from dotenv.cli import cli as dotenv_cli


def test_to_cli_string():
Expand All @@ -17,3 +21,19 @@ def test_to_cli_string():
c(action="set", key="SECRET", value="a b", quote="always")
== 'dotenv -q always set SECRET "a b"'
)


def test_to_cli_string_empty_value(cli, dotenv_path):
command = c(action="set", key="EMPTY", value="")

assert command == 'dotenv set EMPTY ""'
result = cli.invoke(
dotenv_cli, ["--file", str(dotenv_path), *shlex.split(command)[1:]]
)

assert result.exit_code == 0, result.output
assert dotenv_values(dotenv_path) == {"EMPTY": ""}


def test_to_cli_string_omitted_value():
assert c(action="set", key="EMPTY", value=None) == "dotenv set EMPTY"