Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions spython/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion spython/utils/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion spython/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down