|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import io |
3 | 4 | import logging |
4 | 5 | import unittest |
5 | 6 | from pathlib import Path |
6 | 7 | import sys |
| 8 | +from unittest.mock import patch |
7 | 9 |
|
8 | 10 |
|
9 | 11 | ROOT = Path(__file__).resolve().parents[1] |
|
12 | 14 | if str(SRC) not in sys.path: |
13 | 15 | sys.path.insert(0, str(SRC)) |
14 | 16 |
|
15 | | -from stopliga.logging_utils import KeyValueFormatter # noqa: E402 |
| 17 | +from stopliga.logging_utils import KeyValueFormatter, configure_logging # noqa: E402 |
16 | 18 |
|
17 | 19 |
|
18 | 20 | class LoggingFormatterTests(unittest.TestCase): |
@@ -91,3 +93,38 @@ def test_missing_vpn_client_network_log_includes_docs_url(self) -> None: |
91 | 93 | 'docs_url="https://github.com/jcastro/stopliga/blob/main/README.md#vpn-client-network-required"', |
92 | 94 | output, |
93 | 95 | ) |
| 96 | + |
| 97 | + |
| 98 | +class LoggingConfigurationTests(unittest.TestCase): |
| 99 | + def setUp(self) -> None: |
| 100 | + root = logging.getLogger() |
| 101 | + self._original_handlers = list(root.handlers) |
| 102 | + self._original_level = root.level |
| 103 | + |
| 104 | + def tearDown(self) -> None: |
| 105 | + root = logging.getLogger() |
| 106 | + root.handlers.clear() |
| 107 | + root.handlers.extend(self._original_handlers) |
| 108 | + root.setLevel(self._original_level) |
| 109 | + |
| 110 | + def test_logs_are_split_between_stdout_and_stderr_by_severity(self) -> None: |
| 111 | + stdout = io.StringIO() |
| 112 | + stderr = io.StringIO() |
| 113 | + |
| 114 | + with patch("sys.stdout", stdout), patch("sys.stderr", stderr): |
| 115 | + configure_logging("DEBUG") |
| 116 | + logger = logging.getLogger("stopliga.test") |
| 117 | + logger.info("info_message") |
| 118 | + logger.warning("warning_message") |
| 119 | + logger.error("error_message") |
| 120 | + |
| 121 | + stdout_output = stdout.getvalue() |
| 122 | + stderr_output = stderr.getvalue() |
| 123 | + |
| 124 | + self.assertIn("INFO info_message", stdout_output) |
| 125 | + self.assertIn("WARNING warning_message", stdout_output) |
| 126 | + self.assertNotIn("ERROR error_message", stdout_output) |
| 127 | + |
| 128 | + self.assertIn("ERROR error_message", stderr_output) |
| 129 | + self.assertNotIn("INFO info_message", stderr_output) |
| 130 | + self.assertNotIn("WARNING warning_message", stderr_output) |
0 commit comments