diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_repo_logging.py | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/tests/test_repo_logging.py b/tests/test_repo_logging.py index 52f251a7..0f6a3355 100644 --- a/tests/test_repo_logging.py +++ b/tests/test_repo_logging.py | |||
| @@ -16,47 +16,49 @@ | |||
| 16 | import unittest | 16 | import unittest |
| 17 | from unittest import mock | 17 | from unittest import mock |
| 18 | 18 | ||
| 19 | from error import RepoExitError | ||
| 19 | from repo_logging import RepoLogger | 20 | from repo_logging import RepoLogger |
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | class TestRepoLogger(unittest.TestCase): | 23 | class TestRepoLogger(unittest.TestCase): |
| 23 | def test_log_aggregated_errors_logs_aggregated_errors(self): | 24 | @mock.patch.object(RepoLogger, "error") |
| 24 | """Test if log_aggregated_errors outputs aggregated errors.""" | 25 | def test_log_aggregated_errors_logs_aggregated_errors(self, mock_error): |
| 26 | """Test if log_aggregated_errors logs a list of aggregated errors.""" | ||
| 25 | logger = RepoLogger(__name__) | 27 | logger = RepoLogger(__name__) |
| 26 | result = [] | ||
| 27 | |||
| 28 | def mock_handler(log): | ||
| 29 | nonlocal result | ||
| 30 | result.append(log.getMessage()) | ||
| 31 | |||
| 32 | mock_out = mock.MagicMock() | ||
| 33 | mock_out.level = 0 | ||
| 34 | mock_out.handle = mock_handler | ||
| 35 | logger.addHandler(mock_out) | ||
| 36 | |||
| 37 | logger.error("Never gonna give you up") | ||
| 38 | logger.error("Never gonna let you down") | ||
| 39 | logger.error("Never gonna run around and desert you") | ||
| 40 | logger.log_aggregated_errors( | 28 | logger.log_aggregated_errors( |
| 29 | RepoExitError( | ||
| 30 | aggregate_errors=[ | ||
| 31 | Exception("foo"), | ||
| 32 | Exception("bar"), | ||
| 33 | Exception("baz"), | ||
| 34 | Exception("hello"), | ||
| 35 | Exception("world"), | ||
| 36 | Exception("test"), | ||
| 37 | ] | ||
| 38 | ) | ||
| 39 | ) | ||
| 40 | |||
| 41 | mock_error.assert_has_calls( | ||
| 41 | [ | 42 | [ |
| 42 | "Never gonna give you up", | 43 | mock.call("=" * 80), |
| 43 | "Never gonna let you down", | 44 | mock.call( |
| 44 | "Never gonna run around and desert you", | 45 | "Repo command failed due to the following `%s` errors:", |
| 46 | "RepoExitError", | ||
| 47 | ), | ||
| 48 | mock.call("foo\nbar\nbaz\nhello\nworld"), | ||
| 49 | mock.call("+%d additional errors...", 1), | ||
| 45 | ] | 50 | ] |
| 46 | ) | 51 | ) |
| 47 | 52 | ||
| 48 | self.assertEqual( | 53 | @mock.patch.object(RepoLogger, "error") |
| 49 | result, | 54 | def test_log_aggregated_errors_logs_single_error(self, mock_error): |
| 55 | """Test if log_aggregated_errors logs empty aggregated_errors.""" | ||
| 56 | logger = RepoLogger(__name__) | ||
| 57 | logger.log_aggregated_errors(RepoExitError()) | ||
| 58 | |||
| 59 | mock_error.assert_has_calls( | ||
| 50 | [ | 60 | [ |
| 51 | "Never gonna give you up", | 61 | mock.call("=" * 80), |
| 52 | "Never gonna let you down", | 62 | mock.call("Repo command failed: %s", "RepoExitError"), |
| 53 | "Never gonna run around and desert you", | 63 | ] |
| 54 | "=" * 80, | ||
| 55 | "Repo command failed due to following errors:", | ||
| 56 | ( | ||
| 57 | "Never gonna give you up\n" | ||
| 58 | "Never gonna let you down\n" | ||
| 59 | "Never gonna run around and desert you" | ||
| 60 | ), | ||
| 61 | ], | ||
| 62 | ) | 64 | ) |
