|
| 1 | +# |
| 2 | +# Copyright 2024 ABSA Group Limited |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | + |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import sys |
| 20 | + |
| 21 | +from logging import StreamHandler |
| 22 | + |
| 23 | +from release_notes_presence_check.utils.logging_config import setup_logging |
| 24 | + |
| 25 | + |
| 26 | +def validate_logging_config(mock_logging_setup, caplog, expected_level, expected_message): |
| 27 | + mock_logging_setup.assert_called_once() |
| 28 | + |
| 29 | + # Get the actual call arguments from the mock |
| 30 | + call_args = mock_logging_setup.call_args[1] # Extract the kwargs from the call |
| 31 | + |
| 32 | + # Validate the logging level and format |
| 33 | + assert call_args["level"] == expected_level |
| 34 | + assert call_args["format"] == "%(asctime)s - %(levelname)s - %(message)s" |
| 35 | + assert call_args["datefmt"] == "%Y-%m-%d %H:%M:%S" |
| 36 | + |
| 37 | + # Check that the handler is a StreamHandler and outputs to sys.stdout |
| 38 | + handlers = call_args["handlers"] |
| 39 | + assert 1 == len(handlers) # Only one handler is expected |
| 40 | + assert isinstance(handlers[0], StreamHandler) # Handler should be StreamHandler |
| 41 | + assert handlers[0].stream is sys.stdout # Stream should be sys.stdout |
| 42 | + |
| 43 | + # Check that the log message is present |
| 44 | + assert expected_message in caplog.text |
| 45 | + |
| 46 | + |
| 47 | +# setup_logging |
| 48 | + |
| 49 | +def test_setup_logging_default_logging_level(mock_logging_setup, caplog): |
| 50 | + with caplog.at_level(logging.INFO): |
| 51 | + setup_logging() |
| 52 | + |
| 53 | + validate_logging_config(mock_logging_setup, caplog, logging.INFO, "Logging configuration set up.") |
| 54 | + |
| 55 | + |
| 56 | +def test_setup_logging_debug_mode_enabled_by_ci(mock_logging_setup, caplog): |
| 57 | + os.environ["RUNNER_DEBUG"] = "1" |
| 58 | + |
| 59 | + with caplog.at_level(logging.DEBUG): |
| 60 | + setup_logging() |
| 61 | + |
| 62 | + validate_logging_config(mock_logging_setup, caplog, logging.DEBUG, "Debug mode enabled by CI runner.") |
0 commit comments