test_logger.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, configs={}) is False
  15. def test_should_do_markup_respects_config_value():
  16. assert (
  17. module.should_do_markup(no_color=False, configs={'foo.yaml': {'output': {'color': False}}})
  18. is False
  19. )
  20. def test_should_do_markup_prefers_any_false_config_value():
  21. assert (
  22. module.should_do_markup(
  23. no_color=False,
  24. configs={
  25. 'foo.yaml': {'output': {'color': True}},
  26. 'bar.yaml': {'output': {'color': False}},
  27. },
  28. )
  29. is False
  30. )
  31. def test_should_do_markup_respects_PY_COLORS_environment_variable():
  32. flexmock(module.os.environ).should_receive('get').and_return('True')
  33. flexmock(module).should_receive('to_bool').and_return(True)
  34. assert module.should_do_markup(no_color=False, configs={}) is True
  35. def test_should_do_markup_prefers_no_color_value_to_config_value():
  36. assert (
  37. module.should_do_markup(no_color=True, configs={'foo.yaml': {'output': {'color': True}}})
  38. is False
  39. )
  40. def test_should_do_markup_prefers_config_value_to_PY_COLORS():
  41. flexmock(module.os.environ).should_receive('get').and_return('True')
  42. flexmock(module).should_receive('to_bool').and_return(True)
  43. assert (
  44. module.should_do_markup(no_color=False, configs={'foo.yaml': {'output': {'color': False}}})
  45. is False
  46. )
  47. def test_should_do_markup_prefers_no_color_value_to_PY_COLORS():
  48. flexmock(module.os.environ).should_receive('get').and_return('True')
  49. flexmock(module).should_receive('to_bool').and_return(True)
  50. assert module.should_do_markup(no_color=True, configs={}) is False
  51. def test_should_do_markup_respects_stdout_tty_value():
  52. flexmock(module.os.environ).should_receive('get').and_return(None)
  53. assert module.should_do_markup(no_color=False, configs={}) is False
  54. def test_should_do_markup_prefers_PY_COLORS_to_stdout_tty_value():
  55. flexmock(module.os.environ).should_receive('get').and_return('True')
  56. flexmock(module).should_receive('to_bool').and_return(True)
  57. assert module.should_do_markup(no_color=False, configs={}) is True
  58. def test_console_color_formatter_format_includes_log_message():
  59. plain_message = 'uh oh'
  60. record = flexmock(levelno=logging.CRITICAL, msg=plain_message)
  61. colored_message = module.Console_color_formatter().format(record)
  62. assert colored_message != plain_message
  63. assert plain_message in colored_message
  64. def test_color_text_does_not_raise():
  65. module.color_text(module.colorama.Fore.RED, 'hi')
  66. def test_color_text_without_color_does_not_raise():
  67. module.color_text(None, 'hi')
  68. def test_configure_logging_probes_for_log_socket_on_linux():
  69. flexmock(module).should_receive('Console_color_formatter')
  70. flexmock(module.logging).should_receive('basicConfig').with_args(
  71. level=logging.INFO, handlers=tuple
  72. )
  73. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
  74. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
  75. syslog_handler = logging.handlers.SysLogHandler()
  76. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  77. address='/dev/log'
  78. ).and_return(syslog_handler).once()
  79. module.configure_logging(logging.INFO)
  80. def test_configure_logging_probes_for_log_socket_on_macos():
  81. flexmock(module).should_receive('Console_color_formatter')
  82. flexmock(module.logging).should_receive('basicConfig').with_args(
  83. level=logging.INFO, handlers=tuple
  84. )
  85. flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
  86. flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
  87. syslog_handler = logging.handlers.SysLogHandler()
  88. flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
  89. address='/var/run/syslog'
  90. ).and_return(syslog_handler).once()
  91. module.configure_logging(logging.INFO)
  92. def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
  93. flexmock(module).should_receive('Console_color_formatter')
  94. flexmock(module.logging).should_receive('basicConfig').with_args(
  95. level=logging.DEBUG, handlers=tuple
  96. ).once()
  97. flexmock(module.os.path).should_receive('exists').and_return(False)
  98. module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
  99. def test_configure_logging_skips_syslog_if_not_found():
  100. flexmock(module).should_receive('Console_color_formatter')
  101. flexmock(module.logging).should_receive('basicConfig').with_args(
  102. level=logging.INFO, handlers=tuple
  103. )
  104. flexmock(module.os.path).should_receive('exists').and_return(False)
  105. flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
  106. module.configure_logging(console_log_level=logging.INFO)