Skip to content

Commit 4c88c6a

Browse files
authored
Merge pull request #268 from davidhewitt/more-quiet
quiet: clarify implementation
2 parents 9c67fa7 + 6eb0ab8 commit 4c88c6a

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ jobs:
4848
with:
4949
python-version: "3.x"
5050

51+
- name: Install Rust toolchain
52+
uses: actions-rs/toolchain@v1
53+
with:
54+
toolchain: stable
55+
profile: minimal
56+
default: true
57+
5158
- run: pip install nox
5259

5360
- run: nox -s test

setuptools_rust/_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
def format_called_process_error(e: subprocess.CalledProcessError) -> str:
55
"""Helper to convert a CalledProcessError to an error message.
66
7+
8+
>>> format_called_process_error(subprocess.CalledProcessError(
9+
... 777, ['ls', '-la'], None, None
10+
... ))
11+
'`ls -la` failed with code 777'
712
>>> format_called_process_error(subprocess.CalledProcessError(
813
... 1, ['cargo', 'foo bar'], 'message', None
914
... ))
@@ -14,7 +19,9 @@ def format_called_process_error(e: subprocess.CalledProcessError) -> str:
1419
'`cargo` failed with code -1\\n-- Output captured from stdout:\\nstdout\\n-- Output captured from stderr:\\nstderr'
1520
"""
1621
command = " ".join(_quote_whitespace(arg) for arg in e.cmd)
17-
message = f"""`{command}` failed with code {e.returncode}
22+
message = f"`{command}` failed with code {e.returncode}"
23+
if e.stdout is not None:
24+
message += f"""
1825
-- Output captured from stdout:
1926
{e.stdout}"""
2027

setuptools_rust/build.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,13 @@ def build_extension(
207207

208208
# Execute cargo
209209
try:
210+
# If quiet, capture all output and only show it in the exception
211+
# If not quiet, forward all cargo output to stderr
212+
stdout = subprocess.PIPE if quiet else sys.stderr.fileno()
210213
stderr = subprocess.PIPE if quiet else None
211-
output = subprocess.check_output(command, env=env, stderr=stderr, text=True)
214+
subprocess.run(
215+
command, env=env, stdout=stdout, stderr=stderr, text=True, check=True
216+
)
212217
except subprocess.CalledProcessError as e:
213218
raise CompileError(format_called_process_error(e))
214219

@@ -218,10 +223,6 @@ def build_extension(
218223
"requires Rust to be installed and cargo to be on the PATH"
219224
)
220225

221-
if not quiet:
222-
if output:
223-
print(output, file=sys.stderr)
224-
225226
# Find the shared library that cargo hopefully produced and copy
226227
# it into the build directory as if it were produced by build_ext.
227228

setuptools_rust/extension.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def install_script(self, module_name: str, exe_path: str) -> None:
232232
f.write(_SCRIPT_TEMPLATE.format(executable=repr(executable)))
233233

234234
def _metadata(self, *, quiet: bool) -> "_CargoMetadata":
235-
"""Returns cargo metedata for this extension package.
235+
"""Returns cargo metadata for this extension package.
236236
237237
Cached - will only execute cargo on first invocation.
238238
"""
@@ -249,6 +249,8 @@ def _metadata(self, *, quiet: bool) -> "_CargoMetadata":
249249
metadata_command.extend(self.cargo_manifest_args)
250250

251251
try:
252+
# If quiet, capture stderr and only show it on exceptions
253+
# If not quiet, let stderr be inherited
252254
stderr = subprocess.PIPE if quiet else None
253255
payload = subprocess.check_output(
254256
metadata_command, stderr=stderr, encoding="latin-1"

tests/test_extension.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from pytest import CaptureFixture, MonkeyPatch
5+
6+
from setuptools_rust.extension import RustBin
7+
8+
9+
@pytest.fixture()
10+
def hello_world_bin() -> RustBin:
11+
setuptools_rust_dir = Path(__file__).parent.parent
12+
return RustBin(
13+
"hello-world",
14+
path=(
15+
setuptools_rust_dir / "examples" / "hello-world" / "Cargo.toml"
16+
).as_posix(),
17+
)
18+
19+
20+
def test_metadata_contents(hello_world_bin: RustBin) -> None:
21+
metadata = hello_world_bin._metadata(quiet=False)
22+
assert "target_directory" in metadata
23+
24+
25+
def test_metadata_cargo_log(
26+
capfd: CaptureFixture, monkeypatch: MonkeyPatch, hello_world_bin: RustBin
27+
) -> None:
28+
monkeypatch.setenv("CARGO_LOG", "trace")
29+
30+
# With quiet unset, no stdout, plenty of logging stderr
31+
hello_world_bin._metadata(quiet=False)
32+
captured = capfd.readouterr()
33+
assert captured.out == ""
34+
assert "TRACE cargo::util::config" in captured.err
35+
36+
# With quiet set, nothing will be printed
37+
hello_world_bin._metadata(quiet=True)
38+
captured = capfd.readouterr()
39+
assert captured.out == ""
40+
assert captured.err == ""

0 commit comments

Comments
 (0)