Skip to content

Commit

Permalink
make it possible to skip the warning with stderr output
Browse files Browse the repository at this point in the history
  • Loading branch information
rambo committed Jan 28, 2024
1 parent 20faf25 commit a67c09f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libpvarki/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
LOGGER = logging.getLogger(__name__)


async def call_cmd(cmd: str, timeout: float = 2.5) -> Tuple[int, str, str]:
async def call_cmd(cmd: str, timeout: float = 2.5, *, stderr_warn: bool = True) -> Tuple[int, str, str]:
"""Do the boilerplate for calling cmd and returning the exit code and output as strings"""
LOGGER.debug("Calling create_subprocess_shell(({})".format(cmd))
process = await asyncio.create_subprocess_shell(
Expand All @@ -17,7 +17,7 @@ async def call_cmd(cmd: str, timeout: float = 2.5) -> Tuple[int, str, str]:
out, err = await asyncio.wait_for(process.communicate(), timeout=timeout)
out_str = out.decode("utf-8")
err_str = err.decode("utf-8")
if err:
if err and stderr_warn:
LOGGER.warning("{} stderr: {}".format(cmd, err_str))
LOGGER.info(out_str)
assert isinstance(process.returncode, int) # at this point it is, keep mypy happy
Expand Down
11 changes: 11 additions & 0 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ async def test_stdout() -> None:
async def test_stderr() -> None:
"""Test that echo exists with code 0 and ouputs what we expect to stderr"""
code, stdout, stderr = await call_cmd("echo 'goodbye world' >&2")
# FIXME: Capture log output and check for the warning
assert code == 0
assert stdout == ""
assert stderr == "goodbye world\n"


@pytest.mark.asyncio
async def test_stderr_nowarn() -> None:
"""Test that echo exists with code 0 and ouputs what we expect to stderr"""
code, stdout, stderr = await call_cmd("echo 'goodbye world' >&2", stderr_warn=False)
# FIXME: Capture log output and check that there is no warning
assert code == 0
assert stdout == ""
assert stderr == "goodbye world\n"
Expand Down

0 comments on commit a67c09f

Please sign in to comment.