blob: 0f6a3355a5fe97483425fadbbed73f29258447ff [file] [log] [blame]
Aravind Vasudevan8c35d942023-07-26 19:16:59 +00001# 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."""
16import unittest
Mike Frysinger06ddc8c2023-08-21 21:26:51 -040017from unittest import mock
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000018
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000019from error import RepoExitError
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000020from repo_logging import RepoLogger
21
22
23class TestRepoLogger(unittest.TestCase):
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000024 @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 Vasudevan8c35d942023-07-26 19:16:59 +000027 logger = RepoLogger(__name__)
Aravind Vasudevane914ec22023-08-31 20:57:31 +000028 logger.log_aggregated_errors(
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000029 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 Vasudevane914ec22023-08-31 20:57:31 +000042 [
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000043 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 Vasudevane914ec22023-08-31 20:57:31 +000050 ]
51 )
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000052
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000053 @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 Vasudevan8c35d942023-07-26 19:16:59 +000060 [
Aravind Vasudevanb8fd1922023-09-14 22:54:04 +000061 mock.call("=" * 80),
62 mock.call("Repo command failed: %s", "RepoExitError"),
63 ]
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000064 )