aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/check_clang_diags_test.py
blob: a7889038f9a955efffe7c2d36c66fc83a674eaf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for check_clang_diags."""

import unittest
from unittest import mock

import check_clang_diags
from cros_utils import bugs


# pylint: disable=protected-access


class Test(unittest.TestCase):
    """Test class."""

    def test_process_new_diagnostics_ignores_new_tools(self):
        new_state, new_diags = check_clang_diags._process_new_diagnostics(
            old={},
            new={"clang": ["-Wone", "-Wtwo"]},
        )
        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
        self.assertEqual(new_diags, {})

    def test_process_new_diagnostics_is_a_nop_when_no_changes(self):
        new_state, new_diags = check_clang_diags._process_new_diagnostics(
            old={"clang": ["-Wone", "-Wtwo"]},
            new={"clang": ["-Wone", "-Wtwo"]},
        )
        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
        self.assertEqual(new_diags, {})

    def test_process_new_diagnostics_ignores_removals_and_readds(self):
        new_state, new_diags = check_clang_diags._process_new_diagnostics(
            old={"clang": ["-Wone", "-Wtwo"]},
            new={"clang": ["-Wone"]},
        )
        self.assertEqual(new_diags, {})
        new_state, new_diags = check_clang_diags._process_new_diagnostics(
            old=new_state,
            new={"clang": ["-Wone", "-Wtwo"]},
        )
        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
        self.assertEqual(new_diags, {})

    def test_process_new_diagnostics_complains_when_warnings_are_added(self):
        new_state, new_diags = check_clang_diags._process_new_diagnostics(
            old={"clang": ["-Wone"]},
            new={"clang": ["-Wone", "-Wtwo"]},
        )
        self.assertEqual(new_state, {"clang": ["-Wone", "-Wtwo"]})
        self.assertEqual(new_diags, {"clang": ["-Wtwo"]})

    @mock.patch.object(bugs, "CreateNewBug")
    def test_bugs_are_created_as_expected(self, create_new_bug_mock):
        check_clang_diags._file_bugs_for_new_diags(
            {
                "clang": ["-Wone"],
                "clang-tidy": ["bugprone-foo"],
            }
        )

        expected_calls = [
            mock.call(
                component_id=bugs.WellKnownComponents.CrOSToolchainPublic,
                title="Investigate clang check `-Wone`",
                body="\n".join(
                    (
                        "It seems that the `-Wone` check was recently added to clang.",
                        "It's probably good to TAL at whether this check would be good",
                        "for us to enable in e.g., platform2, or across ChromeOS.",
                    )
                ),
                assignee=check_clang_diags._DEFAULT_ASSIGNEE,
                cc=check_clang_diags._DEFAULT_CCS,
            ),
            mock.call(
                component_id=bugs.WellKnownComponents.CrOSToolchainPublic,
                title="Investigate clang-tidy check `bugprone-foo`",
                body="\n".join(
                    (
                        "It seems that the `bugprone-foo` check was recently added to "
                        "clang-tidy.",
                        "It's probably good to TAL at whether this check would be good",
                        "for us to enable in e.g., platform2, or across ChromeOS.",
                    )
                ),
                assignee=check_clang_diags._DEFAULT_ASSIGNEE,
                cc=check_clang_diags._DEFAULT_CCS,
            ),
        ]

        # Don't assertEqual the lists, since the diff is really hard to read for
        # that.
        for actual, expected in zip(
            create_new_bug_mock.call_args_list, expected_calls
        ):
            self.assertEqual(actual, expected)

        self.assertEqual(
            len(create_new_bug_mock.call_args_list), len(expected_calls)
        )


if __name__ == "__main__":
    unittest.main()