Skip to content

Commit 65e7c56

Browse files
arvidmaArvid Müllern-Aspegrenpre-commit-ci[bot]
authored
Share the stderr with the user, when non-zero exit code (#927)
* 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>
1 parent 7b668cc commit 65e7c56

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/setuptools_scm/_run_cmd.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def has_command(
175175
) -> bool:
176176
try:
177177
p = run([name, *args], cwd=".", timeout=5)
178+
if p.returncode != 0:
179+
log.error(f"Command '{name}' returned non-zero. This is stderr:")
180+
log.error(p.stderr)
178181
except OSError as e:
179182
log.warning("command %s missing: %s", name, e)
180183
res = False

testing/test_functions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ def test_has_command() -> None:
153153
assert not has_command("yadayada_setuptools_aint_ne")
154154

155155

156+
def test_has_command_logs_stderr(caplog: pytest.LogCaptureFixture) -> None:
157+
"""
158+
If the name provided to has_command() exists as a command, but gives a non-zero
159+
return code, there should be a log message generated.
160+
"""
161+
with pytest.warns(RuntimeWarning, match="ls"):
162+
has_command("ls", ["--a-flag-that-doesnt-exist-should-give-output-on-stderr"])
163+
found_it = False
164+
for record in caplog.records:
165+
if "returned non-zero. This is stderr" in record.message:
166+
found_it = True
167+
assert found_it, "Did not find expected log record for "
168+
169+
156170
@pytest.mark.parametrize(
157171
"tag, expected_version",
158172
[

0 commit comments

Comments
 (0)