From 30e69bccee56b3e7a106df91b28b050884b3ad3c Mon Sep 17 00:00:00 2001 From: yuvipanda Date: Mon, 10 Dec 2018 23:06:07 -0800 Subject: [PATCH] Add unit test for execute_cmd in repo2docker.utils --- repo2docker/utils.py | 4 +++- tests/test_utils.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/test_utils.py diff --git a/repo2docker/utils.py b/repo2docker/utils.py index d6854c8f..ce0f470b 100644 --- a/repo2docker/utils.py +++ b/repo2docker/utils.py @@ -10,7 +10,9 @@ from traitlets import Integer, TraitError def execute_cmd(cmd, capture=False, **kwargs): """ - Call given command, yielding output line by line if capture=True + Call given command, yielding output line by line if capture=True. + + Must be yielded from. """ if capture: kwargs['stdout'] = subprocess.PIPE diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..17c78aa0 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,37 @@ +""" +Tests for repo2docker/utils.py +""" +from repo2docker import utils +import pytest +import subprocess + + +def test_capture_cmd_no_capture_success(): + # This should succeed + for line in utils.execute_cmd([ + '/bin/bash', '-c', 'echo test' + ]): + pass + +def test_capture_cmd_no_capture_fail(): + with pytest.raises(subprocess.CalledProcessError): + for line in utils.execute_cmd([ + '/bin/bash', '-c', 'e ' + ]): + pass + + +def test_capture_cmd_capture_success(): + # This should succeed + for line in utils.execute_cmd([ + '/bin/bash', '-c', 'echo test' + ], capture=True): + assert line == 'test\n' + + +def test_capture_cmd_capture_fail(): + with pytest.raises(subprocess.CalledProcessError): + for line in utils.execute_cmd([ + '/bin/bash', '-c', 'echo test; exit 1 ' + ], capture=True): + assert line == 'test\n' \ No newline at end of file