From f5ecce77a1adb0b554005dce825e99a430012049 Mon Sep 17 00:00:00 2001 From: yuvipanda Date: Sun, 19 May 2019 14:24:57 -0700 Subject: [PATCH] Add unit tests for run_subprocess --- bootstrap/bootstrap.py | 10 +++++----- dev-requirements.txt | 1 + tests/test_utils.py | 21 +++++++++++++++++++++ tljh/utils.py | 9 ++++----- 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 tests/test_utils.py diff --git a/bootstrap/bootstrap.py b/bootstrap/bootstrap.py index 3370d70..ad013ca 100644 --- a/bootstrap/bootstrap.py +++ b/bootstrap/bootstrap.py @@ -44,16 +44,16 @@ def run_subprocess(cmd, *args, **kwargs): In TLJH, this sends successful output to the installer log, and failed output directly to the user's screen """ + logger = logging.getLogger('tljh') proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, *args, **kwargs) printable_command = ' '.join(cmd) if proc.returncode != 0: # Our process failed! Show output to the user - logger.error(proc.stdout.decode()) - e = Exception( 'command {command} failed with return code {code}'.format( - printable_command, proc.returncode + logger.error('Ran {command} with exit code {code}'.format( + command=printable_command, code=proc.returncode )) - logging.exception(e) - raise e + logger.error(proc.stdout.decode()) + raise subprocess.CalledProcessError(cmd=cmd, returncode=proc.returncode) else: # This goes into installer.log logger.debug('Ran {command} with exit code {code}'.format( diff --git a/dev-requirements.txt b/dev-requirements.txt index 166dd49..6296fdd 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,4 +1,5 @@ pytest pytest-cov +pytest-mock codecov pytoml \ No newline at end of file diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..b123c95 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,21 @@ +import pytest +from tljh import utils +import subprocess +import logging + + +def test_run_subprocess_exception(mocker): + logger = logging.getLogger('tljh') + mocker.patch.object(logger, 'error') + with pytest.raises(subprocess.CalledProcessError): + utils.run_subprocess( + ['/bin/bash', '-c', 'echo error; exit 1'] + ) + logger.error.assert_called_with('error\n') + + +def test_run_subprocess(mocker): + logger = logging.getLogger('tljh') + mocker.patch.object(logger, 'debug') + utils.run_subprocess(['/bin/bash', '-c', 'echo success']) + logger.debug.assert_called_with('success\n') \ No newline at end of file diff --git a/tljh/utils.py b/tljh/utils.py index 03e09ec..395d395 100644 --- a/tljh/utils.py +++ b/tljh/utils.py @@ -21,12 +21,11 @@ def run_subprocess(cmd, *args, **kwargs): printable_command = ' '.join(cmd) if proc.returncode != 0: # Our process failed! Show output to the user - logger.error(proc.stdout.decode()) - e = Exception( 'command {command} failed with return code {code}'.format( - printable_command, proc.returncode + logger.error('Ran {command} with exit code {code}'.format( + command=printable_command, code=proc.returncode )) - logger.exception(e) - raise e + logger.error(proc.stdout.decode()) + raise subprocess.CalledProcessError(cmd=cmd, returncode=proc.returncode) else: # This goes into installer.log logger.debug('Ran {command} with exit code {code}'.format(