Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 1 | # Copyright (C) 2023 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Unit test for repo_logging module.""" |
| 16 | import unittest |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 17 | from unittest import mock |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 18 | |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 19 | from error import RepoExitError |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 20 | from repo_logging import RepoLogger |
| 21 | |
| 22 | |
| 23 | class TestRepoLogger(unittest.TestCase): |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 24 | @mock.patch.object(RepoLogger, "error") |
| 25 | def test_log_aggregated_errors_logs_aggregated_errors(self, mock_error): |
| 26 | """Test if log_aggregated_errors logs a list of aggregated errors.""" |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 27 | logger = RepoLogger(__name__) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 28 | logger.log_aggregated_errors( |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 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( |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 42 | [ |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 43 | mock.call("=" * 80), |
| 44 | mock.call( |
| 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), |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 50 | ] |
| 51 | ) |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 52 | |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 53 | @mock.patch.object(RepoLogger, "error") |
| 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( |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 60 | [ |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 61 | mock.call("=" * 80), |
| 62 | mock.call("Repo command failed: %s", "RepoExitError"), |
| 63 | ] |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 64 | ) |