blob: e7cf59ca3eb23e45f926e620de4ee96235a84a05 [file] [log] [blame]
Kent Tamurac3e44312018-04-07 16:30:241#!/usr/bin/env python
2# Copyright 2017 The Chromium Authors. All rights reserved.
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
9import PRESUBMIT
10
11import os.path
12import subprocess
13import sys
14import unittest
15
16sys.path.append(
17 os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'pymock'))
18sys.path.append(
19 os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
20
21import mock
22from PRESUBMIT_test_mocks import MockInputApi
23from PRESUBMIT_test_mocks import MockOutputApi
24from PRESUBMIT_test_mocks import MockAffectedFile
25
26
27class Capture(object):
28 """Class to capture a call argument that can be tested later on."""
29
30 def __init__(self):
31 self.value = None
32
33 def __eq__(self, other):
34 self.value = other
35 return True
36
37
38class PresubmitTest(unittest.TestCase):
39
40 @mock.patch('subprocess.Popen')
41 def testCheckChangeOnUploadWithBlinkAndChromiumFiles(self, _):
Kent Tamuraf76ea8f2018-04-17 04:45:0742 """This verifies that CheckChangeOnUpload will only call check_blink_style.py
Kent Tamurac3e44312018-04-07 16:30:2443 on non-test files.
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()
49 mock_input_api.files = [
50 MockAffectedFile('file_blink.h', diff_file_blink_h),
51 MockAffectedFile('file_chromium.h', diff_file_chromium_h),
52 MockAffectedFile('web_tests/TestExpectations', diff_file_test_expectations)
53 ]
54 # Access to a protected member _CheckStyle
55 # pylint: disable=W0212
56 PRESUBMIT._CheckStyle(mock_input_api, MockOutputApi())
57 capture = Capture()
58 # pylint: disable=E1101
59 subprocess.Popen.assert_called_with(capture, stderr=-1)
60 self.assertEqual(6, len(capture.value))
61 self.assertEqual('../../file_blink.h', capture.value[3])
62 self.assertEqual('../../web_tests/TestExpectations', capture.value[5])
63
64 @mock.patch('subprocess.Popen')
65 def testCheckChangeOnUploadWithEmptyAffectedFileList(self, _):
66 """This verifies that CheckChangeOnUpload will skip calling
Kent Tamuraf76ea8f2018-04-17 04:45:0767 check_blink_style.py if the affected file list is empty.
Kent Tamurac3e44312018-04-07 16:30:2468 """
69 diff_file_chromium1_h = ['some diff']
70 diff_file_chromium2_h = ['another diff']
71 diff_file_layout_test_html = ['more diff']
72 mock_input_api = MockInputApi()
Fergal Daly4fc8a212018-10-01 08:43:2673 mock_input_api.files = []
Kent Tamurac3e44312018-04-07 16:30:2474 # Access to a protected member _CheckStyle
75 # pylint: disable=W0212
76 PRESUBMIT._CheckStyle(mock_input_api, MockOutputApi())
Fergal Daly4fc8a212018-10-01 08:43:2677 self.assertEqual(0, subprocess.Popen.call_count)
78
79 def test_FilterPaths(self):
80 """This verifies that _FilterPaths removes expected paths."""
81 diff_file_chromium1_h = ['some diff']
82 diff_web_tests_html = ['more diff']
83 diff_presubmit = ['morer diff']
84 diff_test_expectations = ['morest diff']
85 mock_input_api = MockInputApi()
86 mock_input_api.files = [
87 MockAffectedFile('file_chromium1.h', diff_file_chromium1_h),
88 MockAffectedFile('web_tests/some_tests.html', diff_web_tests_html),
89 MockAffectedFile('web_tests/TestExpectations', diff_test_expectations),
90 MockAffectedFile('blink/PRESUBMIT', diff_presubmit),
91 ]
92 # Access to a protected member _FilterPaths
93 # pylint: disable=W0212
94 filtered = PRESUBMIT._FilterPaths(mock_input_api)
95 self.assertEqual(2, len(filtered))
96 self.assertEqual(
97 mock_input_api.os_path.join('..', '..', 'file_chromium1.h'),
98 filtered[0])
99 self.assertEqual(
100 mock_input_api.os_path.join('..', '..', 'web_tests/TestExpectations'),
101 filtered[1])
Kent Tamurac3e44312018-04-07 16:30:24102
103 def testCheckPublicHeaderWithBlinkMojo(self):
104 """This verifies that _CheckForWrongMojomIncludes detects -blink mojo
105 headers in public files.
106 """
107
108 mock_input_api = MockInputApi()
109 potentially_bad_content = '#include "public/platform/modules/cache_storage.mojom-blink.h"'
110 mock_input_api.files = [
111 MockAffectedFile('third_party/blink/public/a_header.h',
112 [potentially_bad_content], None)
113 ]
114 # Access to a protected member _CheckForWrongMojomIncludes
115 # pylint: disable=W0212
116 errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api,
117 MockOutputApi())
118 self.assertEquals(
119 'Public blink headers using Blink variant mojoms found. ' +
120 'You must include .mojom-shared.h instead:',
121 errors[0].message)
122
123 def testCheckInternalHeaderWithBlinkMojo(self):
124 """This verifies that _CheckForWrongMojomIncludes accepts -blink mojo
125 headers in blink internal files.
126 """
127
128 mock_input_api = MockInputApi()
Takuto Ikutada932f22019-02-21 05:39:30129 potentially_bad_content = """
130 #include "public/platform/modules/cache_storage.mojom-blink.h"
131 #include "public/platform/modules/cache_storage.mojom-blink-forward.h"
Oksana Zhuravlova2b5fe2832019-02-22 05:44:14132 #include "public/platform/modules/cache_storage.mojom-blink-test-utils.h"
Takuto Ikutada932f22019-02-21 05:39:30133 """
Kent Tamurac3e44312018-04-07 16:30:24134 mock_input_api.files = [
135 MockAffectedFile('third_party/blink/renderer/core/a_header.h',
136 [potentially_bad_content], None)
137 ]
138 # Access to a protected member _CheckForWrongMojomIncludes
139 # pylint: disable=W0212
140 errors = PRESUBMIT._CheckForWrongMojomIncludes(mock_input_api,
141 MockOutputApi())
142 self.assertEquals([], errors)
143
144
145class CxxDependencyTest(unittest.TestCase):
146 allow_list = [
147 'gfx::ColorSpace',
148 'gfx::CubicBezier',
149 'gfx::ICCProfile',
danakj85c4d0302018-06-05 15:16:00150 'gfx::Point',
151 'gfx::Rect',
Kent Tamurac3e44312018-04-07 16:30:24152 'gfx::ScrollOffset',
153 'scoped_refptr<base::SingleThreadTaskRunner>',
154 ]
155 disallow_list = [
156 'GURL',
157 'base::Callback<void()>',
158 'content::RenderFrame',
danakj85c4d0302018-06-05 15:16:00159 'gfx::Canvas',
Kent Tamurac3e44312018-04-07 16:30:24160 'net::IPEndPoint',
161 'ui::Clipboard',
162 ]
163 disallow_message = [
164 ]
165
166 def runCheck(self, filename, file_contents):
167 mock_input_api = MockInputApi()
168 mock_input_api.files = [
169 MockAffectedFile(filename, file_contents),
170 ]
171 # Access to a protected member
172 # pylint: disable=W0212
173 return PRESUBMIT._CheckForForbiddenChromiumCode(mock_input_api, MockOutputApi())
174
175 # References in comments should never be checked.
176 def testCheckCommentsIgnored(self):
177 filename = 'third_party/blink/renderer/core/frame/frame.cc'
178 for item in self.allow_list:
179 errors = self.runCheck(filename, ['// %s' % item])
180 self.assertEqual([], errors)
181
182 for item in self.disallow_list:
183 errors = self.runCheck(filename, ['// %s' % item])
184 self.assertEqual([], errors)
185
186 # References in Test files should never be checked.
187 def testCheckTestsIgnored(self):
188 filename = 'third_party/blink/rendere/core/frame/frame_test.cc'
189 for item in self.allow_list:
190 errors = self.runCheck(filename, ['// %s' % item])
191 self.assertEqual([], errors)
192
193 for item in self.disallow_list:
194 errors = self.runCheck(filename, ['// %s' % item])
195 self.assertEqual([], errors)
196
197 # core, modules, public, et cetera should all have dependency enforcement.
198 def testCheckCoreEnforcement(self):
199 filename = 'third_party/blink/renderer/core/frame/frame.cc'
200 for item in self.allow_list:
201 errors = self.runCheck(filename, ['%s' % item])
202 self.assertEqual([], errors)
203
204 for item in self.disallow_list:
205 errors = self.runCheck(filename, ['%s' % item])
206 self.assertEquals(1, len(errors))
207 self.assertRegexpMatches(
208 errors[0].message,
209 r'^[^:]+:\d+ uses disallowed identifier .+$')
210
211 def testCheckModulesEnforcement(self):
212 filename = 'third_party/blink/renderer/modules/modules_initializer.cc'
213 for item in self.allow_list:
214 errors = self.runCheck(filename, ['%s' % item])
215 self.assertEqual([], errors)
216
217 for item in self.disallow_list:
218 errors = self.runCheck(filename, ['%s' % item])
219 self.assertEquals(1, len(errors))
220 self.assertRegexpMatches(
221 errors[0].message,
222 r'^[^:]+:\d+ uses disallowed identifier .+$')
223
224 def testCheckPublicEnforcement(self):
225 filename = 'third_party/blink/renderer/public/platform/web_thread.h'
226 for item in self.allow_list:
227 errors = self.runCheck(filename, ['%s' % item])
228 self.assertEqual([], errors)
229
230 for item in self.disallow_list:
231 errors = self.runCheck(filename, ['%s' % item])
232 self.assertEquals(1, len(errors))
233 self.assertRegexpMatches(
234 errors[0].message,
235 r'^[^:]+:\d+ uses disallowed identifier .+$')
236
237 # platform and controller should be opted out of enforcement, but aren't
238 # currently checked because the PRESUBMIT test mocks are missing too
239 # much functionality...
240
241 # External module checks should not affect CSS files.
242 def testCheckCSSIgnored(self):
243 filename = 'third_party/blink/renderer/devtools/front_end/timeline/someFile.css'
244 errors = self.runCheck(filename, ['.toolbar::after { color: pink; }\n'])
245 self.assertEqual([], errors)
246
247if __name__ == '__main__':
248 unittest.main()