From 7d7308a80ddaac5399aef335e4bc09d8bc960e9e Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Thu, 13 Jun 2019 10:48:21 -0700 Subject: [PATCH] Integration tests for execute.py. --- borgmatic/execute.py | 2 +- tests/integration/test_execute.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/integration/test_execute.py diff --git a/borgmatic/execute.py b/borgmatic/execute.py index b6074c9..149ebf9 100644 --- a/borgmatic/execute.py +++ b/borgmatic/execute.py @@ -23,7 +23,7 @@ def execute_and_log_output(full_command, output_log_level, shell): remaining_output = process.stdout.read().rstrip().decode() if remaining_output: - logger.info(remaining_output) + logger.log(output_log_level, remaining_output) exit_code = process.poll() if exit_code != 0: diff --git a/tests/integration/test_execute.py b/tests/integration/test_execute.py new file mode 100644 index 0000000..bf580d9 --- /dev/null +++ b/tests/integration/test_execute.py @@ -0,0 +1,36 @@ +import logging +import subprocess + +import pytest +from flexmock import flexmock + +from borgmatic import execute as module + + +def test_execute_and_log_output_logs_each_line_separately(): + flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'hi').once() + flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'there').once() + + module.execute_and_log_output(['echo', 'hi\nthere'], output_log_level=logging.INFO, shell=False) + + +def test_execute_and_log_output_logs_borg_error_as_error(): + flexmock(module.logger).should_receive('error').with_args('borg: error: oopsie').once() + flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'daisie').once() + + module.execute_and_log_output( + ['echo', 'borg: error: oopsie\ndaisie\n'], output_log_level=logging.INFO, shell=False + ) + + +def test_execute_and_log_output_with_no_output_logs_nothing(): + flexmock(module.logger).should_receive('log').never() + + module.execute_and_log_output(['true'], output_log_level=logging.INFO, shell=False) + + +def test_execute_and_log_output_with_error_exit_status_raises(): + flexmock(module.logger).should_receive('log').never() + + with pytest.raises(subprocess.CalledProcessError): + module.execute_and_log_output(['false'], output_log_level=logging.INFO, shell=False)