diff --git a/src/libpvarki/shell.py b/src/libpvarki/shell.py index cba0af6..66d8dfd 100644 --- a/src/libpvarki/shell.py +++ b/src/libpvarki/shell.py @@ -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( @@ -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 diff --git a/tests/test_shell.py b/tests/test_shell.py index 757ff9b..4e4bf9f 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -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"