Skip to content

Commit b97f793

Browse files
authored
Merge branch 'main' into refactor/docs
2 parents 9c84698 + fa4e6a9 commit b97f793

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/dotenv/cli.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,20 @@ def run_command(command: List[str], env: Dict[str, str]) -> None:
214214
if sys.platform == "win32":
215215
# execvpe on Windows returns control immediately
216216
# rather than once the command has finished.
217-
p = Popen(command, universal_newlines=True, bufsize=0, shell=False, env=cmd_env)
217+
try:
218+
p = Popen(
219+
command, universal_newlines=True, bufsize=0, shell=False, env=cmd_env
220+
)
221+
except FileNotFoundError:
222+
print(f"Command not found: {command[0]}", file=sys.stderr)
223+
sys.exit(1)
224+
218225
_, _ = p.communicate()
219226

220227
sys.exit(p.returncode)
221228
else:
222-
os.execvpe(command[0], args=command, env=cmd_env)
229+
try:
230+
os.execvpe(command[0], args=command, env=cmd_env)
231+
except FileNotFoundError:
232+
print(f"Command not found: {command[0]}", file=sys.stderr)
233+
sys.exit(1)

src/dotenv/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ def load_dotenv(
461461
verbose: Whether to output a warning the .env file is missing.
462462
override: Whether to override the system environment variables with the variables
463463
from the `.env` file.
464+
interpolate: Whether to interpolate variables using POSIX variable expansion.
464465
encoding: Encoding to be used to read the file.
465466
466467
Returns:
@@ -506,13 +507,12 @@ def dotenv_values(
506507
Keys declared without a value (e.g. bare ``FOO``) will have ``None`` as
507508
their dict value.
508509
509-
Args:
510-
dotenv_path: Absolute or relative path to the ``.env`` file.
511-
stream: Text stream with ``.env`` content, used if *dotenv_path* is
512-
``None``.
513-
verbose: Whether to log a warning when the ``.env`` file is missing.
514-
interpolate: Whether to resolve ``${VAR}`` references in values.
515-
encoding: Encoding used to read the file.
510+
Parameters:
511+
dotenv_path: Absolute or relative path to the .env file.
512+
stream: `StringIO` object with .env content, used if `dotenv_path` is `None`.
513+
verbose: Whether to output a warning if the .env file is missing.
514+
interpolate: Whether to interpolate variables using POSIX variable expansion.
515+
encoding: Encoding to be used to read the file.
516516
517517
If both *dotenv_path* and *stream* are ``None``, :func:`find_dotenv` is
518518
used to locate the ``.env`` file.

tests/test_cli.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,21 @@ def test_run_without_cmd(tmp_path):
237237
assert "Invalid value for '-f'" in result.stderr
238238

239239

240-
def test_run_with_invalid_cmd(tmp_path):
240+
def test_run_with_invalid_cmd(dotenv_path, tmp_path):
241+
result = run_dotenv(
242+
["--file", str(dotenv_path), "run", "i_do_not_exist"],
243+
cwd=tmp_path,
244+
)
245+
246+
check_process(result, exit_code=1)
247+
assert "Command not found: i_do_not_exist" in result.stderr
248+
249+
250+
def test_run_with_env_missing_and_invalid_cmd(tmp_path):
251+
"""
252+
Check that an .env file missing takes precedence over a command not found error.
253+
"""
254+
241255
result = run_dotenv(["run", "i_do_not_exist"], cwd=tmp_path)
242256

243257
check_process(result, exit_code=2)

0 commit comments

Comments
 (0)