test_logger.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import logger as module
  5. @pytest.mark.parametrize('bool_val', (True, 'yes', 'on', '1', 'true', 'True', 1))
  6. def test_to_bool_parses_true_values(bool_val):
  7. assert module.to_bool(bool_val)
  8. @pytest.mark.parametrize('bool_val', (False, 'no', 'off', '0', 'false', 'False', 0))
  9. def test_to_bool_parses_false_values(bool_val):
  10. assert not module.to_bool(bool_val)
  11. def test_to_bool_passes_none_through():
  12. assert module.to_bool(None) is None
  13. def test_should_do_markup_respects_no_color_value():
  14. assert module.should_do_markup(no_color=True) is False
  15. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  16. flexmock(module.os.environ).should_receive('get').and_return('True')
  17. flexmock(module).should_receive('to_bool').and_return(True)
  18. assert module.should_do_markup(no_color=False) is True
  19. def test_should_do_markup_prefers_no_color_value_to_PY_COLORS():
  20. flexmock(module.os.environ).should_receive('get').and_return('True')
  21. flexmock(module).should_receive('to_bool').and_return(True)
  22. assert module.should_do_markup(no_color=True) is False
  23. def test_should_do_markup_respects_stdout_tty_value():
  24. flexmock(module.os.environ).should_receive('get').and_return(None)
  25. assert module.should_do_markup(no_color=False) is False
  26. def test_should_do_markup_prefers_PY_COLORS_to_stdout_tty_value():
  27. flexmock(module.os.environ).should_receive('get').and_return('True')
  28. flexmock(module).should_receive('to_bool').and_return(True)
  29. assert module.should_do_markup(no_color=False) is True
  30. def test_console_color_formatter_format_includes_log_message():
  31. plain_message = 'uh oh'
  32. record = flexmock(levelno=logging.CRITICAL, msg=plain_message)
  33. colored_message = module.Console_color_formatter().format(record)
  34. assert colored_message != plain_message
  35. assert plain_message in colored_message
  36. def test_color_text_does_not_raise():
  37. module.color_text(module.colorama.Fore.RED, 'hi')
  38. def test_color_text_without_color_does_not_raise():
  39. module.color_text(None, 'hi')
  40. def test_configure_logging_probes_for_log_socket_on_linux():
  41. flexmock(module).should_receive('Console_color_formatter')
  42. flexmock(module.logging).should_receive('basicConfig').with_args(
  43. level=logging.INFO, handlers=tuple
  44. )
  45. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  46. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  47. syslog_handler = logging.handlers.SysLogHandler()
  48. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  49. address='/dev/log'
  50. ).and_return(syslog_handler).once()
  51. module.configure_logging(logging.INFO)
  52. def test_configure_logging_probes_for_log_socket_on_macos():
  53. flexmock(module).should_receive('Console_color_formatter')
  54. flexmock(module.logging).should_receive('basicConfig').with_args(
  55. level=logging.INFO, handlers=tuple
  56. )
  57. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  58. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  59. syslog_handler = logging.handlers.SysLogHandler()
  60. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  61. address='/var/run/syslog'
  62. ).and_return(syslog_handler).once()
  63. module.configure_logging(logging.INFO)
  64. def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
  65. flexmock(module).should_receive('Console_color_formatter')
  66. flexmock(module.logging).should_receive('basicConfig').with_args(
  67. level=logging.DEBUG, handlers=tuple
  68. ).once()
  69. flexmock(module.os.path).should_receive('exists').and_return(False)
  70. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  71. def test_configure_logging_skips_syslog_if_not_found():
  72. flexmock(module).should_receive('Console_color_formatter')
  73. flexmock(module.logging).should_receive('basicConfig').with_args(
  74. level=logging.INFO, handlers=tuple
  75. )
  76. flexmock(module.os.path).should_receive('exists').and_return(False)
  77. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  78. module.configure_logging(console_log_level=logging.INFO)