• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2022 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Tests for check_clang_diags."""
7
8import unittest
9from unittest import mock
10
11import check_clang_diags
12from cros_utils import bugs
13
14
15# pylint: disable=protected-access
16
17
18class Test(unittest.TestCase):
19    """Test class."""
20
21    def test_process_new_diagnostics_ignores_new_tools(self):
22        new_state, new_diags = check_clang_diags._process_new_diagnostics(
23            old={},
24            new={"clang": ["-Wone", "-Wtwo"]},
25        )
26        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
27        self.assertEqual(new_diags, {})
28
29    def test_process_new_diagnostics_is_a_nop_when_no_changes(self):
30        new_state, new_diags = check_clang_diags._process_new_diagnostics(
31            old={"clang": ["-Wone", "-Wtwo"]},
32            new={"clang": ["-Wone", "-Wtwo"]},
33        )
34        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
35        self.assertEqual(new_diags, {})
36
37    def test_process_new_diagnostics_ignores_removals_and_readds(self):
38        new_state, new_diags = check_clang_diags._process_new_diagnostics(
39            old={"clang": ["-Wone", "-Wtwo"]},
40            new={"clang": ["-Wone"]},
41        )
42        self.assertEqual(new_diags, {})
43        new_state, new_diags = check_clang_diags._process_new_diagnostics(
44            old=new_state,
45            new={"clang": ["-Wone", "-Wtwo"]},
46        )
47        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
48        self.assertEqual(new_diags, {})
49
50    def test_process_new_diagnostics_complains_when_warnings_are_added(self):
51        new_state, new_diags = check_clang_diags._process_new_diagnostics(
52            old={"clang": ["-Wone"]},
53            new={"clang": ["-Wone", "-Wtwo"]},
54        )
55        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
56        self.assertEqual(new_diags, {"clang": ["-Wtwo"]})
57
58    @mock.patch.object(bugs, "CreateNewBug")
59    def test_bugs_are_created_as_expected(self, create_new_bug_mock):
60        check_clang_diags._file_bugs_for_new_diags(
61            {
62                "clang": ["-Wone"],
63                "clang-tidy": ["bugprone-foo"],
64            }
65        )
66
67        expected_calls = [
68            mock.call(
69                component_id=bugs.WellKnownComponents.CrOSToolchainPublic,
70                title="Investigate clang check `-Wone`",
71                body="\n".join(
72                    (
73                        "It seems that the `-Wone` check was recently added to clang.",
74                        "It's probably good to TAL at whether this check would be good",
75                        "for us to enable in e.g., platform2, or across ChromeOS.",
76                    )
77                ),
78                assignee=check_clang_diags._DEFAULT_ASSIGNEE,
79                cc=check_clang_diags._DEFAULT_CCS,
80            ),
81            mock.call(
82                component_id=bugs.WellKnownComponents.CrOSToolchainPublic,
83                title="Investigate clang-tidy check `bugprone-foo`",
84                body="\n".join(
85                    (
86                        "It seems that the `bugprone-foo` check was recently added to "
87                        "clang-tidy.",
88                        "It's probably good to TAL at whether this check would be good",
89                        "for us to enable in e.g., platform2, or across ChromeOS.",
90                    )
91                ),
92                assignee=check_clang_diags._DEFAULT_ASSIGNEE,
93                cc=check_clang_diags._DEFAULT_CCS,
94            ),
95        ]
96
97        # Don't assertEqual the lists, since the diff is really hard to read for
98        # that.
99        for actual, expected in zip(
100            create_new_bug_mock.call_args_list, expected_calls
101        ):
102            self.assertEqual(actual, expected)
103
104        self.assertEqual(
105            len(create_new_bug_mock.call_args_list), len(expected_calls)
106        )
107
108
109if __name__ == "__main__":
110    unittest.main()
111