|
| 1 | +import sys |
1 | 2 | from unittest.mock import patch |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
5 | | -from iwa.core.utils import get_safe_master_copy_address, singleton |
| 6 | +from iwa.core.utils import configure_logger, get_safe_master_copy_address, singleton |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def reset_configure_logger(): |
| 11 | + """Remove the configured flag so configure_logger can be called fresh in each test.""" |
| 12 | + had_flag = hasattr(configure_logger, "configured") |
| 13 | + if had_flag: |
| 14 | + delattr(configure_logger, "configured") |
| 15 | + yield |
| 16 | + # Restore flag state so other tests aren't affected |
| 17 | + if hasattr(configure_logger, "configured"): |
| 18 | + delattr(configure_logger, "configured") |
| 19 | + if had_flag: |
| 20 | + configure_logger.configured = True |
| 21 | + |
| 22 | + |
| 23 | +class TestConfigureLogger: |
| 24 | + def test_happy_path(self, reset_configure_logger, tmp_path): |
| 25 | + """File logging and stderr both succeed — stderr added first, then file.""" |
| 26 | + with ( |
| 27 | + patch("iwa.core.utils.logger") as mock_logger, |
| 28 | + patch("iwa.core.constants.DATA_DIR", tmp_path), |
| 29 | + ): |
| 30 | + result = configure_logger() |
| 31 | + |
| 32 | + mock_logger.remove.assert_called_once() |
| 33 | + assert mock_logger.add.call_count == 2 # stderr + file |
| 34 | + # Verify stderr is the first sink (order matters for fallback logic) |
| 35 | + first_call_args = mock_logger.add.call_args_list[0] |
| 36 | + assert first_call_args == ((sys.stderr,), {"level": "INFO"}) |
| 37 | + assert configure_logger.configured is True |
| 38 | + assert result is mock_logger |
| 39 | + |
| 40 | + def test_permission_error_on_mkdir(self, reset_configure_logger): |
| 41 | + """PermissionError from mkdir → warning with type name, only stderr sink, no crash.""" |
| 42 | + with ( |
| 43 | + patch("iwa.core.utils.logger") as mock_logger, |
| 44 | + patch("iwa.core.constants.DATA_DIR") as mock_data_dir, |
| 45 | + ): |
| 46 | + mock_data_dir.mkdir.side_effect = PermissionError("read-only file system") |
| 47 | + |
| 48 | + configure_logger() |
| 49 | + |
| 50 | + # Only stderr was added (mkdir failed before logger.add for file) |
| 51 | + mock_logger.add.assert_called_once_with(sys.stderr, level="INFO") |
| 52 | + mock_logger.warning.assert_called_once() |
| 53 | + warning_msg = mock_logger.warning.call_args[0][0] |
| 54 | + assert "unavailable" in warning_msg |
| 55 | + assert configure_logger.configured is True |
| 56 | + |
| 57 | + def test_oserror_on_mkdir(self, reset_configure_logger): |
| 58 | + """OSError (e.g. disk full) from mkdir → same fallback as PermissionError.""" |
| 59 | + with ( |
| 60 | + patch("iwa.core.utils.logger") as mock_logger, |
| 61 | + patch("iwa.core.constants.DATA_DIR") as mock_data_dir, |
| 62 | + ): |
| 63 | + mock_data_dir.mkdir.side_effect = OSError("no space left on device") |
| 64 | + |
| 65 | + configure_logger() |
| 66 | + |
| 67 | + mock_logger.add.assert_called_once_with(sys.stderr, level="INFO") |
| 68 | + mock_logger.warning.assert_called_once() |
| 69 | + assert "unavailable" in mock_logger.warning.call_args[0][0] |
| 70 | + assert configure_logger.configured is True |
| 71 | + |
| 72 | + def test_permission_error_on_file_add(self, reset_configure_logger): |
| 73 | + """PermissionError from logger.add(file) → warning with type name, fallback to stderr.""" |
| 74 | + with ( |
| 75 | + patch("iwa.core.utils.logger") as mock_logger, |
| 76 | + patch("iwa.core.constants.DATA_DIR") as mock_data_dir, |
| 77 | + ): |
| 78 | + mock_data_dir.mkdir.return_value = None |
| 79 | + # First add (stderr) succeeds; second add (file) raises PermissionError |
| 80 | + mock_logger.add.side_effect = [None, PermissionError("permission denied")] |
| 81 | + |
| 82 | + configure_logger() |
| 83 | + |
| 84 | + assert mock_logger.add.call_count == 2 |
| 85 | + mock_logger.warning.assert_called_once() |
| 86 | + assert "unavailable" in mock_logger.warning.call_args[0][0] |
| 87 | + assert configure_logger.configured is True |
| 88 | + |
| 89 | + def test_configured_flag_prevents_reinit(self, reset_configure_logger): |
| 90 | + """Second call to configure_logger() returns immediately without re-initializing.""" |
| 91 | + with ( |
| 92 | + patch("iwa.core.utils.logger") as mock_logger, |
| 93 | + patch("iwa.core.constants.DATA_DIR") as mock_data_dir, |
| 94 | + ): |
| 95 | + mock_data_dir.mkdir.return_value = None |
| 96 | + configure_logger() |
| 97 | + # Verify first call did initialize |
| 98 | + assert mock_logger.add.call_count >= 1 |
| 99 | + mock_logger.reset_mock() |
| 100 | + |
| 101 | + configure_logger() |
| 102 | + |
| 103 | + mock_logger.remove.assert_not_called() |
| 104 | + mock_logger.add.assert_not_called() |
6 | 105 |
|
7 | 106 |
|
8 | 107 | def test_get_safe_master_copy_address_found(): |
|
0 commit comments