test_log_utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # test_log_utils.py -- Tests for log_utils.py
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for dulwich.log_utils."""
  22. import logging
  23. from dulwich.log_utils import (
  24. _DULWICH_LOGGER,
  25. _NULL_HANDLER,
  26. _NullHandler,
  27. default_logging_config,
  28. getLogger,
  29. remove_null_handler,
  30. )
  31. from . import TestCase
  32. class LogUtilsTests(TestCase):
  33. """Tests for log_utils."""
  34. def setUp(self):
  35. super().setUp()
  36. # Save original handler configuration
  37. self.original_handlers = list(_DULWICH_LOGGER.handlers)
  38. def tearDown(self):
  39. # Restore original handler configuration
  40. _DULWICH_LOGGER.handlers = self.original_handlers
  41. super().tearDown()
  42. def test_null_handler(self):
  43. """Test the _NullHandler class."""
  44. handler = _NullHandler()
  45. # Create a test record
  46. record = logging.LogRecord(
  47. name="test",
  48. level=logging.INFO,
  49. pathname="test_log_utils.py",
  50. lineno=1,
  51. msg="Test message",
  52. args=(),
  53. exc_info=None,
  54. )
  55. # Should not raise any exceptions
  56. handler.emit(record)
  57. def test_get_logger(self):
  58. """Test the getLogger function."""
  59. # Should return a logger instance
  60. logger = getLogger("dulwich.test")
  61. self.assertIsInstance(logger, logging.Logger)
  62. self.assertEqual(logger.name, "dulwich.test")
  63. def test_remove_null_handler(self):
  64. """Test removing the null handler."""
  65. # Make sure _NULL_HANDLER is in the handlers
  66. if _NULL_HANDLER not in _DULWICH_LOGGER.handlers:
  67. _DULWICH_LOGGER.addHandler(_NULL_HANDLER)
  68. # Remove the null handler
  69. remove_null_handler()
  70. # Check that it was removed
  71. self.assertNotIn(_NULL_HANDLER, _DULWICH_LOGGER.handlers)
  72. def test_default_logging_config(self):
  73. """Test the default logging configuration."""
  74. # Apply default config
  75. default_logging_config()
  76. # Check that the null handler was removed
  77. self.assertNotIn(_NULL_HANDLER, _DULWICH_LOGGER.handlers)
  78. # Check that the root logger has a handler
  79. root_logger = logging.getLogger()
  80. self.assertTrue(root_logger.handlers)
  81. # Reset the root logger to not affect other tests
  82. root_logger.handlers = []