diff --git a/CHANGELOG.md b/CHANGELOG.md index 1041d84d..3baeaf1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The client here will eventually be released as "spython" (and eventually to singularity on pypi), and the versions here will coincide with these releases. ## [master](https://github.com/singularityhub/singularity-cli/tree/master) + - stream command should print to stdout given CalledProcessError (0.0.81) - USER regular expression should check for USER at start of line (0.0.80) - add singularity options parameters to send to singularity (0.0.79) - add support for library:// urls (0.0.78) diff --git a/spython/tests/test_client.py b/spython/tests/test_client.py index 844960f1..61d0ee96 100644 --- a/spython/tests/test_client.py +++ b/spython/tests/test_client.py @@ -7,8 +7,11 @@ # with this file, You can obtain one at http://mozilla.org/MPL/2.0/. from spython.main import Client +from spython.utils import write_file import shutil import os +import pytest +from subprocess import CalledProcessError def test_build_from_docker(tmp_path): @@ -50,6 +53,37 @@ def test_execute_with_return_code(docker_container): assert result["return_code"] == 0 +@pytest.mark.parametrize("return_code", [True, False]) +def test_execute_with_called_process_error( + capsys, docker_container, return_code, tmp_path +): + tmp_file = os.path.join(tmp_path, "CalledProcessError.sh") + # "This is stdout" to stdout, "This is stderr" to stderr + script = f"""#!/bin/bash +echo "This is stdout" +>&2 echo "This is stderr" +{"exit 1" if return_code else ""} +""" + write_file(tmp_file, script) + if return_code: + with pytest.raises(CalledProcessError): + for line in Client.execute( + docker_container[1], f"/bin/sh {tmp_file}", stream=True + ): + print(line, "") + else: + for line in Client.execute( + docker_container[1], f"/bin/sh {tmp_file}", stream=True + ): + print(line, "") + captured = capsys.readouterr() + assert "stdout" in captured.out + if return_code: + assert "stderr" in captured.err + else: + assert "stderr" not in captured.err + + def test_inspect(docker_container): result = Client.inspect(docker_container[1]) assert "attributes" in result or "data" in result diff --git a/spython/utils/terminal.py b/spython/utils/terminal.py index 9331ee87..2856a680 100644 --- a/spython/utils/terminal.py +++ b/spython/utils/terminal.py @@ -134,13 +134,16 @@ def stream_command(cmd, no_newline_regexp="Progess", sudo=False, sudo_options=No """ cmd = _process_sudo_cmd(cmd, sudo, sudo_options) - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) + process = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True + ) for line in iter(process.stdout.readline, ""): if not re.search(no_newline_regexp, line): yield line process.stdout.close() return_code = process.wait() if return_code: + print(process.stderr.read(), file=sys.stderr) raise subprocess.CalledProcessError(return_code, cmd) diff --git a/spython/version.py b/spython/version.py index 49611ad8..d52fddd7 100644 --- a/spython/version.py +++ b/spython/version.py @@ -5,7 +5,7 @@ # with this file, You can obtain one at http://mozilla.org/MPL/2.0/. -__version__ = "0.0.80" +__version__ = "0.0.81" AUTHOR = "Vanessa Sochat" AUTHOR_EMAIL = "vsochat@stanford.edu" NAME = "spython"