Skip to content

Commit bc43967

Browse files
yannhambbc2
authored andcommitted
Fix dotenv run crashes on variable without values
1 parent b5b8ae2 commit bc43967

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1515
### Fixed
1616

1717
- Fix escaping of quoted values written by `set_key` (#236 by [@bbc2]).
18+
- Fix dotenv run crashing on environment variables without values
1819

1920
## [0.11.0] - 2020-02-07
2021

src/dotenv/cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'Run pip install "python-dotenv[cli]" to fix this.')
1010
sys.exit(1)
1111

12-
from .compat import IS_TYPE_CHECKING
12+
from .compat import IS_TYPE_CHECKING, to_env
1313
from .main import dotenv_values, get_key, set_key, unset_key
1414
from .version import __version__
1515

@@ -97,11 +97,12 @@ def run(ctx, commandline):
9797
# type: (click.Context, List[str]) -> None
9898
"""Run command with environment variables present."""
9999
file = ctx.obj['FILE']
100-
dotenv_as_dict = dotenv_values(file)
100+
dotenv_as_dict = {to_env(k): to_env(v) for (k, v) in dotenv_values(file).items() if v is not None}
101+
101102
if not commandline:
102103
click.echo('No command given.')
103104
exit(1)
104-
ret = run_command(commandline, dotenv_as_dict) # type: ignore
105+
ret = run_command(commandline, dotenv_as_dict)
105106
exit(ret)
106107

107108

tests/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ def test_run(tmp_path):
121121
assert result == "b\n"
122122

123123

124+
def test_run_with_none_value(tmp_path):
125+
sh.cd(str(tmp_path))
126+
dotenv_file = str(tmp_path / ".env")
127+
with open(dotenv_file, "w") as f:
128+
f.write("a=b\nc")
129+
130+
result = sh.dotenv("run", "printenv", "a")
131+
132+
assert result == "b\n"
133+
134+
124135
def test_run_with_other_env(dotenv_file):
125136
with open(dotenv_file, "w") as f:
126137
f.write("a=b")

0 commit comments

Comments
 (0)