From c353b03529efe45eaaea88847a154fbff775e935 Mon Sep 17 00:00:00 2001 From: Zhang Shuran <150400327+Shuran-z@users.noreply.github.com> Date: Sun, 13 Sep 2026 06:43:53 -0300 Subject: [PATCH] Fix empty values in get_cli_string --- src/dotenv/__init__.py | 4 ++-- tests/test_utils.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/dotenv/__init__.py b/src/dotenv/__init__.py index dde24a01..ccd77119 100644 --- a/src/dotenv/__init__.py +++ b/src/dotenv/__init__.py @@ -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) diff --git a/tests/test_utils.py b/tests/test_utils.py index 93b8bae2..74047ada 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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(): @@ -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"