Skip to content

Commit

Permalink
Share the stderr with the user, when non-zero exit code (#927)
Browse files Browse the repository at this point in the history
* Share the stderr with the user, when failing a require_command() due to non-zero exit code.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Too long line.

* Just log at the place where the call is made.

* Undo added empty lines.

* Test for log-output in case of non-zero return code from a command.

---------

Co-authored-by: Arvid Müllern-Aspegren <x@rvid.se>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 21, 2023
1 parent 7b668cc commit 65e7c56
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/setuptools_scm/_run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def has_command(
) -> bool:
try:
p = run([name, *args], cwd=".", timeout=5)
if p.returncode != 0:
log.error(f"Command '{name}' returned non-zero. This is stderr:")
log.error(p.stderr)
except OSError as e:
log.warning("command %s missing: %s", name, e)
res = False
Expand Down
14 changes: 14 additions & 0 deletions testing/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ def test_has_command() -> None:
assert not has_command("yadayada_setuptools_aint_ne")


def test_has_command_logs_stderr(caplog: pytest.LogCaptureFixture) -> None:
"""
If the name provided to has_command() exists as a command, but gives a non-zero
return code, there should be a log message generated.
"""
with pytest.warns(RuntimeWarning, match="ls"):
has_command("ls", ["--a-flag-that-doesnt-exist-should-give-output-on-stderr"])
found_it = False
for record in caplog.records:
if "returned non-zero. This is stderr" in record.message:
found_it = True
assert found_it, "Did not find expected log record for "


@pytest.mark.parametrize(
"tag, expected_version",
[
Expand Down

0 comments on commit 65e7c56

Please sign in to comment.