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
15 changes: 13 additions & 2 deletions src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,20 @@ def run_command(command: List[str], env: Dict[str, str]) -> None:
if sys.platform == "win32":
# execvpe on Windows returns control immediately
# rather than once the command has finished.
p = Popen(command, universal_newlines=True, bufsize=0, shell=False, env=cmd_env)
try:
p = Popen(
command, universal_newlines=True, bufsize=0, shell=False, env=cmd_env
)
except FileNotFoundError:
print(f"Command not found: {command[0]}", file=sys.stderr)
sys.exit(1)

_, _ = p.communicate()

sys.exit(p.returncode)
else:
os.execvpe(command[0], args=command, env=cmd_env)
try:
os.execvpe(command[0], args=command, env=cmd_env)
except FileNotFoundError:
print(f"Command not found: {command[0]}", file=sys.stderr)
sys.exit(1)
13 changes: 12 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,18 @@ def test_run_without_cmd(cli):
assert "Invalid value for '-f'" in result.output


def test_run_with_invalid_cmd(cli):
def test_run_with_invalid_cmd(cli, dotenv_path):
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "run", "i_do_not_exist"])

assert result.exit_code == 1
assert "Command not found: i_do_not_exist" in result.output


def test_run_with_env_missing_and_invalid_cmd(cli):
"""
Check that an .env file missing takes precedence over a command not found error.
"""

result = cli.invoke(dotenv_cli, ["run", "i_do_not_exist"])

assert result.exit_code == 2
Expand Down