diff --git a/src/subprocess_tee/__init__.py b/src/subprocess_tee/__init__.py index 719b104..72cc856 100644 --- a/src/subprocess_tee/__init__.py +++ b/src/subprocess_tee/__init__.py @@ -99,7 +99,7 @@ async def _stream_subprocess( # noqa: C901 err: list[str] = [] def tee_func(line: bytes, sink: list[str], pipe: Any | None) -> None: - line_str = line.decode("utf-8").rstrip() + line_str = line.decode("utf-8", errors="replace").rstrip() sink.append(line_str) if not kwargs.get("quiet"): if pipe and hasattr(pipe, "write"): diff --git a/test/test_unit.py b/test/test_unit.py index 6e71597..bb8a0f2 100644 --- a/test/test_unit.py +++ b/test/test_unit.py @@ -174,3 +174,46 @@ def test_run_exc_no_args() -> None: subprocess.run(check=False) # type: ignore[call-overload] with pytest.raises(TypeError, match=expected): subprocess_tee.run() + + +@pytest.mark.parametrize( + ("quiet", "tee"), [(False, True), (True, True), (False, False)] +) +def test_run_invalid_utf8( + capsys: pytest.CaptureFixture[str], *, quiet: bool, tee: bool +) -> None: + """Keep reading both streams after replacing undecodable output bytes.""" + cmd = [ + sys.executable, + "-c", + "import os; " + "os.write(1, b'out\\xff' b'put\\xe2\\x98\\x83\\nnext out\\n'); " + "os.write(2, b'err\\xc0\\x80or\\nnext err\\n')", + ] + result = run(cmd, quiet=quiet, tee=tee) + assert result.args == cmd + assert result.returncode == 0 + assert result.stdout == "out�put☃\nnext out\n" + assert result.stderr == "err��or\nnext err\n" + captured = capsys.readouterr() + if quiet or not tee: + assert not captured.out + assert not captured.err + else: + assert captured.out == result.stdout + assert captured.err == result.stderr + + +def test_run_invalid_utf8_with_check() -> None: + """Preserve captured output on a failed child when check=True.""" + cmd = [ + sys.executable, + "-c", + "import os; os.write(1, b'out\\xff\\n'); " + "os.write(2, b'err\\xff\\n'); raise SystemExit(7)", + ] + with pytest.raises(subprocess.CalledProcessError) as error: + run(cmd, check=True, quiet=True) + assert error.value.returncode == 7 + assert error.value.stdout == "out�\n" + assert error.value.stderr == "err�\n" diff --git a/tox.ini b/tox.ini index 22dd7a5..163cbfc 100644 --- a/tox.ini +++ b/tox.ini @@ -52,7 +52,7 @@ set_env = PIP_CONSTRAINT = {tox_root}/.config/constraints.txt PIP_DISABLE_PIP_VERSION_CHECK = 1 PRE_COMMIT_COLOR = always - PYTEST_REQPASS = 18 + PYTEST_REQPASS = 22 PYTHONDONTWRITEBYTECODE = 1 PYTHONUNBUFFERED = 1 UV_CONSTRAINT = {tox_root}/.config/constraints.txt