blob: 0a830ae0bcb46f4b5566298ac86db4d9fa6a9876 [file] [log] [blame]
Thorben Troebst3fbe2b82022-07-11 22:55:491#!/usr/bin/env vpython3
Avi Drissman047c7dc2022-09-27 23:23:142# Copyright 2017 The Chromium Authors
Kent Tamurac3e44312018-04-07 16:30:243# 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
9import PRESUBMIT
10
11import os.path
12import subprocess
13import sys
14import unittest
15
Andrew Grieve713b89b2024-10-15 20:20:0816_THIS_DIR = os.path.dirname(os.path.abspath(__file__))
17sys.path.append(os.path.join(_THIS_DIR, '..', '..'))
Kent Tamurac3e44312018-04-07 16:30:2418
19import mock
20from PRESUBMIT_test_mocks import MockInputApi
21from PRESUBMIT_test_mocks import MockOutputApi
22from PRESUBMIT_test_mocks import MockAffectedFile
23
24
25class 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
36class PresubmitTest(unittest.TestCase):
Kent Tamurac3e44312018-04-07 16:30:2437 @mock.patch('subprocess.Popen')
Jonathan Lee4c5f1f52022-09-26 20:43:0438 @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 Tamurac9bbe0a2020-12-22 14:39:5642 """This verifies that CheckChangeOnUpload will only call
43 check_blink_style.py on non-test files.
Kent Tamurac3e44312018-04-07 16:30:2444 """
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 Grieve713b89b2024-10-15 20:20:0849 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 Lee4c5f1f52022-09-26 20:43:0456 mock_python_file,
Andrew Grieve713b89b2024-10-15 20:20:0857 ])
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 Lee4c5f1f52022-09-26 20:43:0467
Kent Tamurac3e44312018-04-07 16:30:2468 capture = Capture()
69 # pylint: disable=E1101
70 subprocess.Popen.assert_called_with(capture, stderr=-1)
Jonathan Lee4c5f1f52022-09-26 20:43:0471 self.assertEqual(6, len(capture.value))
Andrew Grieve713b89b2024-10-15 20:20:0872 self.assertEqual(os.path.join(_THIS_DIR, 'file_blink.h'),
73 capture.value[3])
Kent Tamurac3e44312018-04-07 16:30:2474
75 @mock.patch('subprocess.Popen')
76 def testCheckChangeOnUploadWithEmptyAffectedFileList(self, _):
77 """This verifies that CheckChangeOnUpload will skip calling
Kent Tamuraf76ea8f2018-04-17 04:45:0778 check_blink_style.py if the affected file list is empty.
Kent Tamurac3e44312018-04-07 16:30:2479 """
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 Daly4fc8a212018-10-01 08:43:2684 mock_input_api.files = []
Kent Tamurac3e44312018-04-07 16:30:2485 # Access to a protected member _CheckStyle
86 # pylint: disable=W0212
87 PRESUBMIT._CheckStyle(mock_input_api, MockOutputApi())
Fergal Daly4fc8a212018-10-01 08:43:2688 self.assertEqual(0, subprocess.Popen.call_count)
89
90 def test_FilterPaths(self):
Andrew Grieve713b89b2024-10-15 20:20:0891 """This verifies that FilterPaths removes expected paths."""
Fergal Daly4fc8a212018-10-01 08:43:2692 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 Grieve713b89b2024-10-15 20:20:0897 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 Tamurac3e44312018-04-07 16:30:24109
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 Tamurac9bbe0a2020-12-22 14:39:56116 potentially_bad_content = \
117 '#include "public/platform/modules/cache_storage.mojom-blink.h"'
Andrew Grieve713b89b2024-10-15 20:20:08118 mock_input_api.InitFiles([
Bruce Dawson58436b62022-04-05 00:38:49119 MockAffectedFile(
120 mock_input_api.os_path.join('third_party', 'blink', 'public',
121 'a_header.h'),
122 [potentially_bad_content], None)
Andrew Grieve713b89b2024-10-15 20:20:08123 ])
Kent Tamurac3e44312018-04-07 16:30:24124 # Access to a protected member _CheckForWrongMojomIncludes
125 # pylint: disable=W0212
126 errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api,
127 MockOutputApi())
Bruce Dawson58436b62022-04-05 00:38:49128 self.assertEqual(
Kent Tamurac3e44312018-04-07 16:30:24129 'Public blink headers using Blink variant mojoms found. ' +
Eriko Kurimoto61f12812020-01-31 08:06:50130 'You must include .mojom-forward.h or .mojom-shared.h instead:',
Kent Tamurac3e44312018-04-07 16:30:24131 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 Ikutada932f22019-02-21 05:39:30139 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 Zhuravlova2b5fe2832019-02-22 05:44:14142 #include "public/platform/modules/cache_storage.mojom-blink-test-utils.h"
Takuto Ikutada932f22019-02-21 05:39:30143 """
Andrew Grieve713b89b2024-10-15 20:20:08144 mock_input_api.InitFiles([
Bruce Dawson58436b62022-04-05 00:38:49145 MockAffectedFile(
146 mock_input_api.os_path.join('third_party', 'blink', 'renderer',
147 'core', 'a_header.h'),
148 [potentially_bad_content], None)
Andrew Grieve713b89b2024-10-15 20:20:08149 ])
Kent Tamurac3e44312018-04-07 16:30:24150 # Access to a protected member _CheckForWrongMojomIncludes
151 # pylint: disable=W0212
152 errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api,
153 MockOutputApi())
Bruce Dawson58436b62022-04-05 00:38:49154 self.assertEqual([], errors)
Kent Tamurac3e44312018-04-07 16:30:24155
156
157class CxxDependencyTest(unittest.TestCase):
158 allow_list = [
Colin Blundell3be069722021-06-14 13:48:51159 'base::OnceCallback<void()>',
160 'base::RepeatingCallback<void()>',
Kent Tamurac3e44312018-04-07 16:30:24161 'gfx::ColorSpace',
162 'gfx::CubicBezier',
163 'gfx::ICCProfile',
danakj85c4d0302018-06-05 15:16:00164 'gfx::Point',
165 'gfx::Rect',
Kent Tamurac3e44312018-04-07 16:30:24166 'scoped_refptr<base::SingleThreadTaskRunner>',
167 ]
168 disallow_list = [
Kent Tamurac3e44312018-04-07 16:30:24169 'content::RenderFrame',
danakj85c4d0302018-06-05 15:16:00170 'gfx::Canvas',
Kent Tamurac3e44312018-04-07 16:30:24171 'net::IPEndPoint',
172 'ui::Clipboard',
173 ]
Kent Tamurac9bbe0a2020-12-22 14:39:56174 disallow_message = []
Kent Tamurac3e44312018-04-07 16:30:24175
176 def runCheck(self, filename, file_contents):
177 mock_input_api = MockInputApi()
Andrew Grieve713b89b2024-10-15 20:20:08178 mock_input_api.InitFiles([
Kent Tamurac3e44312018-04-07 16:30:24179 MockAffectedFile(filename, file_contents),
Andrew Grieve713b89b2024-10-15 20:20:08180 ])
Kent Tamurac3e44312018-04-07 16:30:24181 # Access to a protected member
182 # pylint: disable=W0212
Kent Tamurac9bbe0a2020-12-22 14:39:56183 return PRESUBMIT._CheckForForbiddenChromiumCode(
184 mock_input_api, MockOutputApi())
Kent Tamurac3e44312018-04-07 16:30:24185
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 Chenga09856d2025-05-29 01:04:45190 results = self.runCheck(filename, ['// %s' % item])
191 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24192
193 for item in self.disallow_list:
Daniel Chenga09856d2025-05-29 01:04:45194 results = self.runCheck(filename, ['// %s' % item])
195 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24196
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 Chenga09856d2025-05-29 01:04:45201 results = self.runCheck(filename, ['// %s' % item])
202 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24203
204 for item in self.disallow_list:
Daniel Chenga09856d2025-05-29 01:04:45205 results = self.runCheck(filename, ['// %s' % item])
206 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24207
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 Chenga09856d2025-05-29 01:04:45212 results = self.runCheck(filename, ['%s' % item])
213 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24214
215 for item in self.disallow_list:
Daniel Chenga09856d2025-05-29 01:04:45216 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 Dawson58436b62022-04-05 00:38:49221 r'^[^:]+:\d+ uses disallowed identifier .+$')
Kent Tamurac3e44312018-04-07 16:30:24222
223 def testCheckModulesEnforcement(self):
224 filename = 'third_party/blink/renderer/modules/modules_initializer.cc'
225 for item in self.allow_list:
Daniel Chenga09856d2025-05-29 01:04:45226 results = self.runCheck(filename, ['%s' % item])
227 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24228
229 for item in self.disallow_list:
Daniel Chenga09856d2025-05-29 01:04:45230 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 Dawson58436b62022-04-05 00:38:49235 r'^[^:]+:\d+ uses disallowed identifier .+$')
Kent Tamurac3e44312018-04-07 16:30:24236
237 def testCheckPublicEnforcement(self):
238 filename = 'third_party/blink/renderer/public/platform/web_thread.h'
239 for item in self.allow_list:
Daniel Chenga09856d2025-05-29 01:04:45240 results = self.runCheck(filename, ['%s' % item])
241 self.assertEqual([], results)
Kent Tamurac3e44312018-04-07 16:30:24242
243 for item in self.disallow_list:
Daniel Chenga09856d2025-05-29 01:04:45244 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 Dawson58436b62022-04-05 00:38:49249 r'^[^:]+:\d+ uses disallowed identifier .+$')
Kent Tamurac3e44312018-04-07 16:30:24250
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 Guoab2584f2019-10-07 06:49:39257 filename = 'third_party/blink/renderer/someFile.css'
Kent Tamurac9bbe0a2020-12-22 14:39:56258 errors = self.runCheck(filename,
259 ['.toolbar::after { color: pink; }\n'])
Kent Tamurac3e44312018-04-07 16:30:24260 self.assertEqual([], errors)
261
Kent Tamurac9bbe0a2020-12-22 14:39:56262
Kent Tamurac3e44312018-04-07 16:30:24263if __name__ == '__main__':
264 unittest.main()