Integration tests for execute.py.

This commit is contained in:
Dan Helfman 2019-06-13 10:48:21 -07:00
parent b43ef9d76d
commit 7d7308a80d
2 changed files with 37 additions and 1 deletions

View File

@ -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:

View File

@ -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)