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