Skip to content

Commit

Permalink
Assure returned stderr matches one from subprocess.run (#35)
Browse files Browse the repository at this point in the history
Apparently python subprocess run return different values for stderr
when it does raise instead of when it returns CompletedProcess.

This assures our method matches Python behavior, so users can easily
use it as a drop-in alternative.
  • Loading branch information
ssbarnea authored Apr 12, 2021
1 parent 3ac9bbb commit 8b4522d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/subprocess_tee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,19 @@ def tee(line: bytes, sink: List[str], pipe: Optional[Any]) -> None:

# We need to be sure we keep the stdout/stderr output identical with
# the ones procued by subprocess.run(), at least when in text mode.
check = kwargs.get("check", False)
stdout = None if check else ""
stderr = None if check else ""
if out:
stdout = os.linesep.join(out) + os.linesep
if err:
stderr = os.linesep.join(err) + os.linesep

return CompletedProcess(
args=args,
returncode=await process.wait(),
stdout=(os.linesep.join(out) + os.linesep) if out else None,
stderr=(os.linesep.join(err) + os.linesep) if err else "",
stdout=stdout,
stderr=stderr,
)


Expand Down
2 changes: 2 additions & 0 deletions src/subprocess_tee/test/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def test_run_with_check_raise() -> None:
assert ours.value.cmd == original.value.cmd
assert ours.value.output == original.value.output
assert ours.value.stdout == original.value.stdout
assert ours.value.stderr == original.value.stderr


def test_run_with_check_pass() -> None:
Expand All @@ -122,3 +123,4 @@ def test_run_with_check_pass() -> None:
assert ours.returncode == original.returncode
assert ours.args == original.args
assert ours.stdout == original.stdout
assert ours.stderr == original.stderr

0 comments on commit 8b4522d

Please sign in to comment.