def test_log_verbose_error_logs_expected_text_at_expected_level( self, mocker, caplog): with caplog.at_level(logging.ERROR): request = mocker.MagicMock(sepc=Request) request.body = {"foo": "bar"} CliLogger().log_verbose_error("code42 dothing --flag YES", request) assert "'code42 dothing --flag YES'" in caplog.text assert "Request parameters: {'foo': 'bar'}" in caplog.text
def convert(self, value, param, ctx): try: with open(value, "rb") as file: self.encoding = chardet.detect(file.read())["encoding"] if self.encoding is None: CliLogger().log_error(f"Failed to detect encoding of file: {value}") except Exception: pass # we'll let click.File do it's own exception handling for the filepath return super().convert(value, param, ctx)
class TestCliLogger: _logger = CliLogger() def test_init_creates_user_error_logger_with_expected_handlers(self): logger = CliLogger() handler_types = [type(h) for h in logger._logger.handlers] assert RotatingFileHandler in handler_types def test_log_error_logs_expected_text_at_expected_level(self, caplog): with caplog.at_level(logging.ERROR): ex = Exception("TEST") self._logger.log_error(ex) assert str(ex) in caplog.text def test_log_verbose_error_logs_expected_text_at_expected_level( self, mocker, caplog): with caplog.at_level(logging.ERROR): request = mocker.MagicMock(sepc=Request) request.body = {"foo": "bar"} self._logger.log_verbose_error("code42 dothing --flag YES", request) assert "'code42 dothing --flag YES'" in caplog.text assert "Request parameters: {'foo': 'bar'}" in caplog.text
def test_log_error_logs_expected_text_at_expected_level(self, caplog): with caplog.at_level(logging.ERROR): ex = Exception("TEST") CliLogger().log_error(ex) assert str(ex) in caplog.text
def test_init_creates_user_error_logger_with_expected_handlers(self): logger = CliLogger() handler_types = [type(h) for h in logger._logger.handlers] assert RotatingFileHandler in handler_types