Skip to content

Commit

Permalink
Display exception name when subprocesses raise them
Browse files Browse the repository at this point in the history
This change provides more context information for cases like when
a command is not found as previous behavior only returned the exist
code of the exception, without any other information. Same exit codes
could have being produced by commands that did run.
  • Loading branch information
ssbarnea committed Nov 19, 2024
1 parent 343fe92 commit 7139187
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/changelog/3450.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Log exception name when subprocess execution produces one.

- by :user:`ssbarnea`
3 changes: 3 additions & 0 deletions src/tox/execute/local_sub_process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ def __enter__(self) -> ExecuteStatus:
env=self.request.env,
)
except OSError as exception:
# We log a nice error message to avout returning opaque error codes,
# like exit code 2 (filenotfound).
logging.error("Exception running subprocess %s", str(exception)) # noqa: TRY400
return LocalSubprocessExecuteFailedStatus(self.options, self._out, self._err, exception.errno)

status = LocalSubprocessExecuteStatus(self.options, self._out, self._err, process)
Expand Down
7 changes: 6 additions & 1 deletion tests/execute/local_subprocess/test_local_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import locale
import logging
import os
import re
import shutil
import stat
import subprocess
Expand Down Expand Up @@ -264,7 +265,11 @@ def test_command_does_not_exist(caplog: LogCaptureFixture, os_env: dict[str, str
assert outcome.exit_code != Outcome.OK
assert not outcome.out
assert not outcome.err
assert not caplog.records
assert len(caplog.records) == 1
assert caplog.records[0].levelname == "ERROR"
assert re.match(
r".*(No such file or directory|The system cannot find the file specified).*", caplog.records[0].message
)


@pytest.mark.skipif(sys.platform == "win32", reason="You need a conhost shell for keyboard interrupt")
Expand Down

0 comments on commit 7139187

Please sign in to comment.