Thorben Troebst | 3fbe2b8 | 2022-07-11 22:55:49 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
Avi Drissman | 047c7dc | 2022-09-27 23:23:14 | [diff] [blame] | 2 | # Copyright 2017 The Chromium Authors |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | # |
| 6 | # Note: running this test requires installing the package python-mock. |
| 7 | # pylint: disable=C0103 |
| 8 | # pylint: disable=F0401 |
| 9 | import PRESUBMIT |
| 10 | |
| 11 | import os.path |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import unittest |
| 15 | |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 16 | _THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 17 | sys.path.append(os.path.join(_THIS_DIR, '..', '..')) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 18 | |
| 19 | import mock |
| 20 | from PRESUBMIT_test_mocks import MockInputApi |
| 21 | from PRESUBMIT_test_mocks import MockOutputApi |
| 22 | from PRESUBMIT_test_mocks import MockAffectedFile |
| 23 | |
| 24 | |
| 25 | class Capture(object): |
| 26 | """Class to capture a call argument that can be tested later on.""" |
| 27 | |
| 28 | def __init__(self): |
| 29 | self.value = None |
| 30 | |
| 31 | def __eq__(self, other): |
| 32 | self.value = other |
| 33 | return True |
| 34 | |
| 35 | |
| 36 | class PresubmitTest(unittest.TestCase): |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 37 | @mock.patch('subprocess.Popen') |
Jonathan Lee | 4c5f1f5 | 2022-09-26 20:43:04 | [diff] [blame] | 38 | @mock.patch('PRESUBMIT_test_mocks.MockInputApi.RunTests', create=True) |
| 39 | @mock.patch('PRESUBMIT_test_mocks.MockCannedChecks.GetPylint', create=True) |
| 40 | def testCheckChangeOnUploadWithBlinkAndChromiumFiles( |
| 41 | self, _, _run_tests, _get_pylint): |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 42 | """This verifies that CheckChangeOnUpload will only call |
| 43 | check_blink_style.py on non-test files. |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 44 | """ |
| 45 | diff_file_blink_h = ['some diff'] |
| 46 | diff_file_chromium_h = ['another diff'] |
| 47 | diff_file_test_expectations = ['more diff'] |
| 48 | mock_input_api = MockInputApi() |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 49 | B = 'third_party/blink' |
| 50 | mock_python_file = MockAffectedFile(f'{B}/file_blink.py', ['lint me']) |
| 51 | mock_input_api.InitFiles([ |
| 52 | MockAffectedFile(f'{B}/file_blink.h', diff_file_blink_h), |
| 53 | MockAffectedFile(f'{B}/file_chromium.h', diff_file_chromium_h), |
| 54 | MockAffectedFile(f'{B}/web_tests/TestExpectations', |
| 55 | diff_file_test_expectations), |
Jonathan Lee | 4c5f1f5 | 2022-09-26 20:43:04 | [diff] [blame] | 56 | mock_python_file, |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 57 | ]) |
| 58 | # Access to a protected member _CheckStyle |
| 59 | # pylint: disable=W0212 |
| 60 | PRESUBMIT._CheckStyle(mock_input_api, MockOutputApi()) |
| 61 | mock_input_api.canned_checks.GetPylint.assert_called_once_with( |
| 62 | mock.ANY, |
| 63 | mock.ANY, |
| 64 | files_to_check=[r'file_blink\.py'], |
| 65 | pylintrc=mock_input_api.os_path.join('tools', 'blinkpy', |
| 66 | 'pylintrc')) |
Jonathan Lee | 4c5f1f5 | 2022-09-26 20:43:04 | [diff] [blame] | 67 | |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 68 | capture = Capture() |
| 69 | # pylint: disable=E1101 |
| 70 | subprocess.Popen.assert_called_with(capture, stderr=-1) |
Jonathan Lee | 4c5f1f5 | 2022-09-26 20:43:04 | [diff] [blame] | 71 | self.assertEqual(6, len(capture.value)) |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 72 | self.assertEqual(os.path.join(_THIS_DIR, 'file_blink.h'), |
| 73 | capture.value[3]) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 74 | |
| 75 | @mock.patch('subprocess.Popen') |
| 76 | def testCheckChangeOnUploadWithEmptyAffectedFileList(self, _): |
| 77 | """This verifies that CheckChangeOnUpload will skip calling |
Kent Tamura | f76ea8f | 2018-04-17 04:45:07 | [diff] [blame] | 78 | check_blink_style.py if the affected file list is empty. |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 79 | """ |
| 80 | diff_file_chromium1_h = ['some diff'] |
| 81 | diff_file_chromium2_h = ['another diff'] |
| 82 | diff_file_layout_test_html = ['more diff'] |
| 83 | mock_input_api = MockInputApi() |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 84 | mock_input_api.files = [] |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 85 | # Access to a protected member _CheckStyle |
| 86 | # pylint: disable=W0212 |
| 87 | PRESUBMIT._CheckStyle(mock_input_api, MockOutputApi()) |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 88 | self.assertEqual(0, subprocess.Popen.call_count) |
| 89 | |
| 90 | def test_FilterPaths(self): |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 91 | """This verifies that FilterPaths removes expected paths.""" |
Fergal Daly | 4fc8a21 | 2018-10-01 08:43:26 | [diff] [blame] | 92 | diff_file_chromium1_h = ['some diff'] |
| 93 | diff_web_tests_html = ['more diff'] |
| 94 | diff_presubmit = ['morer diff'] |
| 95 | diff_test_expectations = ['morest diff'] |
| 96 | mock_input_api = MockInputApi() |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 97 | B = 'third_party/blink' |
| 98 | mock_input_api.InitFiles([ |
| 99 | MockAffectedFile(f'{B}/file_chromium1.h', diff_file_chromium1_h), |
| 100 | MockAffectedFile(f'{B}/web_tests/some_tests.html', |
| 101 | diff_web_tests_html), |
| 102 | MockAffectedFile(f'{B}/web_tests/TestExpectations', |
| 103 | diff_test_expectations), |
| 104 | MockAffectedFile(f'{B}/blink/PRESUBMIT', diff_presubmit), |
| 105 | ]) |
| 106 | filtered = PRESUBMIT.FilterPaths(mock_input_api) |
| 107 | self.assertEqual([os.path.join(_THIS_DIR, 'file_chromium1.h')], |
| 108 | filtered) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 109 | |
| 110 | def testCheckPublicHeaderWithBlinkMojo(self): |
| 111 | """This verifies that _CheckForWrongMojomIncludes detects -blink mojo |
| 112 | headers in public files. |
| 113 | """ |
| 114 | |
| 115 | mock_input_api = MockInputApi() |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 116 | potentially_bad_content = \ |
| 117 | '#include "public/platform/modules/cache_storage.mojom-blink.h"' |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 118 | mock_input_api.InitFiles([ |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 119 | MockAffectedFile( |
| 120 | mock_input_api.os_path.join('third_party', 'blink', 'public', |
| 121 | 'a_header.h'), |
| 122 | [potentially_bad_content], None) |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 123 | ]) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 124 | # Access to a protected member _CheckForWrongMojomIncludes |
| 125 | # pylint: disable=W0212 |
| 126 | errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api, |
| 127 | MockOutputApi()) |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 128 | self.assertEqual( |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 129 | 'Public blink headers using Blink variant mojoms found. ' + |
Eriko Kurimoto | 61f1281 | 2020-01-31 08:06:50 | [diff] [blame] | 130 | 'You must include .mojom-forward.h or .mojom-shared.h instead:', |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 131 | errors[0].message) |
| 132 | |
| 133 | def testCheckInternalHeaderWithBlinkMojo(self): |
| 134 | """This verifies that _CheckForWrongMojomIncludes accepts -blink mojo |
| 135 | headers in blink internal files. |
| 136 | """ |
| 137 | |
| 138 | mock_input_api = MockInputApi() |
Takuto Ikuta | da932f2 | 2019-02-21 05:39:30 | [diff] [blame] | 139 | potentially_bad_content = """ |
| 140 | #include "public/platform/modules/cache_storage.mojom-blink.h" |
| 141 | #include "public/platform/modules/cache_storage.mojom-blink-forward.h" |
Oksana Zhuravlova | 2b5fe283 | 2019-02-22 05:44:14 | [diff] [blame] | 142 | #include "public/platform/modules/cache_storage.mojom-blink-test-utils.h" |
Takuto Ikuta | da932f2 | 2019-02-21 05:39:30 | [diff] [blame] | 143 | """ |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 144 | mock_input_api.InitFiles([ |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 145 | MockAffectedFile( |
| 146 | mock_input_api.os_path.join('third_party', 'blink', 'renderer', |
| 147 | 'core', 'a_header.h'), |
| 148 | [potentially_bad_content], None) |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 149 | ]) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 150 | # Access to a protected member _CheckForWrongMojomIncludes |
| 151 | # pylint: disable=W0212 |
| 152 | errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api, |
| 153 | MockOutputApi()) |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 154 | self.assertEqual([], errors) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 155 | |
| 156 | |
| 157 | class CxxDependencyTest(unittest.TestCase): |
| 158 | allow_list = [ |
Colin Blundell | 3be06972 | 2021-06-14 13:48:51 | [diff] [blame] | 159 | 'base::OnceCallback<void()>', |
| 160 | 'base::RepeatingCallback<void()>', |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 161 | 'gfx::ColorSpace', |
| 162 | 'gfx::CubicBezier', |
| 163 | 'gfx::ICCProfile', |
danakj | 85c4d030 | 2018-06-05 15:16:00 | [diff] [blame] | 164 | 'gfx::Point', |
| 165 | 'gfx::Rect', |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 166 | 'scoped_refptr<base::SingleThreadTaskRunner>', |
| 167 | ] |
| 168 | disallow_list = [ |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 169 | 'content::RenderFrame', |
danakj | 85c4d030 | 2018-06-05 15:16:00 | [diff] [blame] | 170 | 'gfx::Canvas', |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 171 | 'net::IPEndPoint', |
| 172 | 'ui::Clipboard', |
| 173 | ] |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 174 | disallow_message = [] |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 175 | |
| 176 | def runCheck(self, filename, file_contents): |
| 177 | mock_input_api = MockInputApi() |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 178 | mock_input_api.InitFiles([ |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 179 | MockAffectedFile(filename, file_contents), |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 180 | ]) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 181 | # Access to a protected member |
| 182 | # pylint: disable=W0212 |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 183 | return PRESUBMIT._CheckForForbiddenChromiumCode( |
| 184 | mock_input_api, MockOutputApi()) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 185 | |
| 186 | # References in comments should never be checked. |
| 187 | def testCheckCommentsIgnored(self): |
| 188 | filename = 'third_party/blink/renderer/core/frame/frame.cc' |
| 189 | for item in self.allow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 190 | results = self.runCheck(filename, ['// %s' % item]) |
| 191 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 192 | |
| 193 | for item in self.disallow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 194 | results = self.runCheck(filename, ['// %s' % item]) |
| 195 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 196 | |
| 197 | # References in Test files should never be checked. |
| 198 | def testCheckTestsIgnored(self): |
| 199 | filename = 'third_party/blink/rendere/core/frame/frame_test.cc' |
| 200 | for item in self.allow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 201 | results = self.runCheck(filename, ['// %s' % item]) |
| 202 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 203 | |
| 204 | for item in self.disallow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 205 | results = self.runCheck(filename, ['// %s' % item]) |
| 206 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 207 | |
| 208 | # core, modules, public, et cetera should all have dependency enforcement. |
| 209 | def testCheckCoreEnforcement(self): |
| 210 | filename = 'third_party/blink/renderer/core/frame/frame.cc' |
| 211 | for item in self.allow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 212 | results = self.runCheck(filename, ['%s' % item]) |
| 213 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 214 | |
| 215 | for item in self.disallow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 216 | results = self.runCheck(filename, ['%s' % item]) |
| 217 | self.assertEqual(2, len(results)) |
| 218 | self.assertIn('Non-Blink usage violations detected.', |
| 219 | results[0].message) |
| 220 | self.assertRegex(results[1].message, |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 221 | r'^[^:]+:\d+ uses disallowed identifier .+$') |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 222 | |
| 223 | def testCheckModulesEnforcement(self): |
| 224 | filename = 'third_party/blink/renderer/modules/modules_initializer.cc' |
| 225 | for item in self.allow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 226 | results = self.runCheck(filename, ['%s' % item]) |
| 227 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 228 | |
| 229 | for item in self.disallow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 230 | results = self.runCheck(filename, ['%s' % item]) |
| 231 | self.assertEqual(2, len(results)) |
| 232 | self.assertIn('Non-Blink usage violations detected.', |
| 233 | results[0].message) |
| 234 | self.assertRegex(results[1].message, |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 235 | r'^[^:]+:\d+ uses disallowed identifier .+$') |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 236 | |
| 237 | def testCheckPublicEnforcement(self): |
| 238 | filename = 'third_party/blink/renderer/public/platform/web_thread.h' |
| 239 | for item in self.allow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 240 | results = self.runCheck(filename, ['%s' % item]) |
| 241 | self.assertEqual([], results) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 242 | |
| 243 | for item in self.disallow_list: |
Daniel Cheng | a09856d | 2025-05-29 01:04:45 | [diff] [blame] | 244 | results = self.runCheck(filename, ['%s' % item]) |
| 245 | self.assertEqual(2, len(results)) |
| 246 | self.assertIn('Non-Blink usage violations detected.', |
| 247 | results[0].message) |
| 248 | self.assertRegex(results[1].message, |
Bruce Dawson | 58436b6 | 2022-04-05 00:38:49 | [diff] [blame] | 249 | r'^[^:]+:\d+ uses disallowed identifier .+$') |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 250 | |
| 251 | # platform and controller should be opted out of enforcement, but aren't |
| 252 | # currently checked because the PRESUBMIT test mocks are missing too |
| 253 | # much functionality... |
| 254 | |
| 255 | # External module checks should not affect CSS files. |
| 256 | def testCheckCSSIgnored(self): |
Yang Guo | ab2584f | 2019-10-07 06:49:39 | [diff] [blame] | 257 | filename = 'third_party/blink/renderer/someFile.css' |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 258 | errors = self.runCheck(filename, |
| 259 | ['.toolbar::after { color: pink; }\n']) |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 260 | self.assertEqual([], errors) |
| 261 | |
Kent Tamura | c9bbe0a | 2020-12-22 14:39:56 | [diff] [blame] | 262 | |
Kent Tamura | c3e4431 | 2018-04-07 16:30:24 | [diff] [blame] | 263 | if __name__ == '__main__': |
| 264 | unittest.main() |