test_logger.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic import logger as module
  4. @pytest.mark.parametrize('bool_val', (True, 'yes', 'on', '1', 'true', 'True', 1))
  5. def test_to_bool_parses_true_values(bool_val):
  6. assert module.to_bool(bool_val)
  7. @pytest.mark.parametrize('bool_val', (False, 'no', 'off', '0', 'false', 'False', 0))
  8. def test_to_bool_parses_false_values(bool_val):
  9. assert not module.to_bool(bool_val)
  10. def test_to_bool_passes_none_through():
  11. assert module.to_bool(None) is None
  12. def test_should_do_markup_respects_no_color_value():
  13. assert module.should_do_markup(no_color=True) is False
  14. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  15. flexmock(module.os.environ).should_receive('get').and_return('True')
  16. flexmock(module).should_receive('to_bool').and_return(True)
  17. assert module.should_do_markup(no_color=False) is True
  18. def test_should_do_markup_prefers_no_color_value_to_PY_COLORS():
  19. flexmock(module.os.environ).should_receive('get').and_return('True')
  20. flexmock(module).should_receive('to_bool').and_return(True)
  21. assert module.should_do_markup(no_color=True) is False
  22. def test_should_do_markup_respects_stdout_tty_value():
  23. flexmock(module.os.environ).should_receive('get').and_return(None)
  24. assert module.should_do_markup(no_color=False) is False
  25. def test_should_do_markup_prefers_PY_COLORS_to_stdout_tty_value():
  26. flexmock(module.os.environ).should_receive('get').and_return('True')
  27. flexmock(module).should_receive('to_bool').and_return(True)
  28. assert module.should_do_markup(no_color=False) is True
  29. @pytest.mark.parametrize('method_name', ('critical', 'error', 'warn', 'info', 'debug'))
  30. def test_borgmatic_logger_log_method_does_not_raise(method_name):
  31. flexmock(module).should_receive('color_text')
  32. flexmock(module.logging.Logger).should_receive(method_name)
  33. getattr(module.Borgmatic_logger('test'), method_name)(msg='hi')
  34. def test_borgmatic_logger_handle_does_not_raise():
  35. flexmock(module).should_receive('color_text')
  36. flexmock(module.logging.Logger).should_receive('handle')
  37. module.Borgmatic_logger('test').handle(
  38. module.logging.makeLogRecord(dict(levelno=module.logging.CRITICAL, msg='hi'))
  39. )
  40. def test_color_text_does_not_raise():
  41. module.color_text(module.colorama.Fore.RED, 'hi')
  42. def test_color_text_without_color_does_not_raise():
  43. module.color_text(None, 'hi')