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 |
| 17 | from unittest.mock import MagicMock |
| 18 | |
| 19 | from repo_logging import RepoLogger |
| 20 | |
| 21 | |
| 22 | class TestRepoLogger(unittest.TestCase): |
| 23 | def test_error_logs_error(self): |
| 24 | """Test if error fn outputs logs.""" |
| 25 | logger = RepoLogger(__name__) |
| 26 | RepoLogger.errors[:] = [] |
| 27 | result = None |
| 28 | |
| 29 | def mock_handler(log): |
| 30 | nonlocal result |
| 31 | result = log.getMessage() |
| 32 | |
| 33 | mock_out = MagicMock() |
| 34 | mock_out.level = 0 |
| 35 | mock_out.handle = mock_handler |
| 36 | logger.addHandler(mock_out) |
| 37 | |
| 38 | logger.error("We're no strangers to love") |
| 39 | |
| 40 | self.assertEqual(result, "We're no strangers to love") |
| 41 | |
| 42 | def test_warning_logs_error(self): |
| 43 | """Test if warning fn outputs logs.""" |
| 44 | logger = RepoLogger(__name__) |
| 45 | RepoLogger.errors[:] = [] |
| 46 | result = None |
| 47 | |
| 48 | def mock_handler(log): |
| 49 | nonlocal result |
| 50 | result = log.getMessage() |
| 51 | |
| 52 | mock_out = MagicMock() |
| 53 | mock_out.level = 0 |
| 54 | mock_out.handle = mock_handler |
| 55 | logger.addHandler(mock_out) |
| 56 | |
| 57 | logger.warning("You know the rules and so do I (do I)") |
| 58 | |
| 59 | self.assertEqual(result, "You know the rules and so do I (do I)") |
| 60 | |
| 61 | def test_error_aggregates_error_msg(self): |
| 62 | """Test if error fn aggregates error logs.""" |
| 63 | logger = RepoLogger(__name__) |
| 64 | RepoLogger.errors[:] = [] |
| 65 | |
| 66 | logger.error("A full commitment's what I'm thinking of") |
| 67 | logger.error("You wouldn't get this from any other guy") |
| 68 | logger.error("I just wanna tell you how I'm feeling") |
| 69 | logger.error("Gotta make you understand") |
| 70 | |
| 71 | self.assertEqual( |
| 72 | RepoLogger.errors[:], |
| 73 | [ |
| 74 | "A full commitment's what I'm thinking of", |
| 75 | "You wouldn't get this from any other guy", |
| 76 | "I just wanna tell you how I'm feeling", |
| 77 | "Gotta make you understand", |
| 78 | ], |
| 79 | ) |
| 80 | |
| 81 | def test_log_aggregated_errors_logs_aggregated_errors(self): |
| 82 | """Test if log_aggregated_errors outputs aggregated errors.""" |
| 83 | logger = RepoLogger(__name__) |
| 84 | RepoLogger.errors[:] = [] |
| 85 | result = [] |
| 86 | |
| 87 | def mock_handler(log): |
| 88 | nonlocal result |
| 89 | result.append(log.getMessage()) |
| 90 | |
| 91 | mock_out = MagicMock() |
| 92 | mock_out.level = 0 |
| 93 | mock_out.handle = mock_handler |
| 94 | logger.addHandler(mock_out) |
| 95 | |
| 96 | logger.error("Never gonna give you up") |
| 97 | logger.error("Never gonna let you down") |
| 98 | logger.error("Never gonna run around and desert you") |
| 99 | logger.log_aggregated_errors() |
| 100 | |
| 101 | self.assertEqual( |
| 102 | result, |
| 103 | [ |
| 104 | "Never gonna give you up", |
| 105 | "Never gonna let you down", |
| 106 | "Never gonna run around and desert you", |
| 107 | "=" * 80, |
| 108 | "Repo command failed due to following errors:", |
| 109 | ( |
| 110 | "Never gonna give you up\n" |
| 111 | "Never gonna let you down\n" |
| 112 | "Never gonna run around and desert you" |
| 113 | ), |
| 114 | ], |
| 115 | ) |