blob: c3a195aad44a0938e7cee3058f8a314fb77592d0 [file] [log] [blame]
Sergio Villar Senin225de052019-11-07 10:47:171#!/usr/bin/env python
Avi Drissman4a8573c2022-09-09 19:35:542# Copyright 2019 The Chromium Authors
Sergio Villar Senin225de052019-11-07 10:47:173# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os.path
Sergio Villar Senin225de052019-11-07 10:47:177import sys
8import unittest
9
10import PRESUBMIT
11
12file_dir_path = os.path.dirname(os.path.abspath(__file__))
13sys.path.insert(0, os.path.join(file_dir_path, '..', '..'))
Georg Neis9080e0602024-08-23 01:50:2914from PRESUBMIT_test_mocks import MockAffectedFile
Sergio Villar Senin225de052019-11-07 10:47:1715from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
16
17_VALID_DEP = "+third_party/blink/public/platform/web_something.h,"
18_INVALID_DEP = "+third_party/blink/public/web/web_something.h,"
19_INVALID_DEP2 = "+third_party/blink/public/web/web_nothing.h,"
20
Georg Neis9080e0602024-08-23 01:50:2921
Sergio Villar Senin225de052019-11-07 10:47:1722class BlinkPublicWebUnwantedDependenciesTest(unittest.TestCase):
23 def makeInputApi(self, files):
24 input_api = MockInputApi()
Andrew Grieve713b89b2024-10-15 20:20:0825 input_api.InitFiles(files)
Sergio Villar Senin225de052019-11-07 10:47:1726 return input_api
27
28 INVALID_DEPS_MESSAGE = ('chrome/browser cannot depend on '
29 'blink/public/web interfaces. Use'
30 ' blink/public/common instead.')
31
32 def testAdditionOfUnwantedDependency(self):
33 input_api = self.makeInputApi([
34 MockAffectedFile('DEPS', [_INVALID_DEP], [], action='M')])
35 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
36 self.assertEqual(1, len(warnings))
37 self.assertEqual(self.INVALID_DEPS_MESSAGE, warnings[0].message)
38 self.assertEqual(1, len(warnings[0].items))
39
40 def testAdditionOfUnwantedDependencyInComment(self):
41 input_api = self.makeInputApi([
42 MockAffectedFile('DEPS', ["#" + _INVALID_DEP], [], action='M')])
43 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
44 self.assertEqual([], warnings)
45
46 def testAdditionOfValidDependency(self):
47 input_api = self.makeInputApi([
48 MockAffectedFile('DEPS', [_VALID_DEP], [], action='M')])
49 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
50 self.assertEqual([], warnings)
51
52 def testAdditionOfMultipleUnwantedDependency(self):
53 input_api = self.makeInputApi([
54 MockAffectedFile('DEPS', [_INVALID_DEP, _INVALID_DEP2], action='M')])
55 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
56 self.assertEqual(1, len(warnings))
57 self.assertEqual(self.INVALID_DEPS_MESSAGE, warnings[0].message)
58 self.assertEqual(2, len(warnings[0].items))
59
60 input_api = self.makeInputApi([
61 MockAffectedFile('DEPS', [_INVALID_DEP, _VALID_DEP], [], action='M')])
62 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
63 self.assertEqual(1, len(warnings))
64 self.assertEqual(self.INVALID_DEPS_MESSAGE, warnings[0].message)
65 self.assertEqual(1, len(warnings[0].items))
66
67 def testRemovalOfUnwantedDependency(self):
68 input_api = self.makeInputApi([
69 MockAffectedFile('DEPS', [], [_INVALID_DEP], action='M')])
70 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
71 self.assertEqual([], warnings)
72
73 def testRemovalOfValidDependency(self):
74 input_api = self.makeInputApi([
75 MockAffectedFile('DEPS', [], [_VALID_DEP], action='M')])
76 warnings = PRESUBMIT._CheckUnwantedDependencies(input_api, MockOutputApi())
77 self.assertEqual([], warnings)
78
Mitsuru Oshima69657a62023-05-20 04:39:1779
Georg Neis9080e0602024-08-23 01:50:2980class InteractiveUiTestLibIncludeTest(unittest.TestCase):
Mitsuru Oshima69657a62023-05-20 04:39:1781 def testAdditionOfUnwantedDependency(self):
82 lines = ['#include "ui/base/test/ui_controls.h"',
83 '#include "ui/base/test/foo.h"',
84 '#include "chrome/test/base/interactive_test_utils.h"']
85 mock_input_api = MockInputApi()
86 mock_input_api.files = [
87 MockAffectedFile('foo_interactive_uitest.cc', lines),
88 MockAffectedFile('foo_browsertest.cc', lines),
89 MockAffectedFile('foo_interactive_browsertest.cc', lines),
90 MockAffectedFile('foo_unittest.cc', lines) ]
Georg Neis9080e0602024-08-23 01:50:2991 mock_output_api = MockOutputApi()
Mitsuru Oshima69657a62023-05-20 04:39:1792 errors = PRESUBMIT._CheckNoInteractiveUiTestLibInNonInteractiveUiTest(
93 mock_input_api, mock_output_api)
94 self.assertEqual(1, len(errors))
95 # 2 lines from 2 files.
96 self.assertEqual(4, len(errors[0].items))
97
Georg Neis9080e0602024-08-23 01:50:2998
99class CheckBuildFilesForIndirectAshSourcesTest(unittest.TestCase):
100 MESSAGE = "Indirect sources detected."
101
102 def testScope(self):
103 """We only complain for changes to BUILD.gn under certain directories."""
104
105 new_contents = [
106 'source_set("foo") {',
107 ' sources = [ "a/b.cc" ]',
108 '}',
109 ]
110
111 mock_output_api = MockOutputApi()
112 mock_input_api = MockInputApi()
113 mock_input_api.files = [
114 MockAffectedFile('BUILD.gn', new_contents),
115 MockAffectedFile('chrome/browser/BUILD.gn', new_contents),
116 MockAffectedFile('chrome/browser/ash/build.cc', new_contents),
117 MockAffectedFile('chrome/browser/ash/BUILD.gn', new_contents),
118 MockAffectedFile('chrome/browser/ashley/BUILD.gn', new_contents),
119 MockAffectedFile('chrome/browser/chromeos/a/b/BUILD.gn', new_contents),
120 MockAffectedFile('chrome/browser/resources/ash/BUILD.gn', new_contents),
121 MockAffectedFile('chrome/browser/ui/BUILD.gn', new_contents),
122 MockAffectedFile('chrome/browser/ui/ash/foo/BUILD.gn', new_contents),
123 MockAffectedFile('chrome/browser/ui/chromeos/BUILD.gn', new_contents),
124 MockAffectedFile('chrome/browser/ui/webui/ash/BUILD.gn', new_contents),
125 ]
126
127 results = PRESUBMIT._CheckBuildFilesForIndirectAshSources(mock_input_api,
128 mock_output_api)
129
130 for result in results:
131 self.assertEqual(result.message, self.MESSAGE)
132
133 self.assertCountEqual(
134 [r.items for r in results],
135 [["chrome/browser/ash/BUILD.gn"],
136 ["chrome/browser/chromeos/a/b/BUILD.gn"],
137 ["chrome/browser/ui/ash/foo/BUILD.gn"],
138 ["chrome/browser/ui/chromeos/BUILD.gn"],
139 ["chrome/browser/ui/webui/ash/BUILD.gn"]])
140
141 def testComplexFormatting(self):
142 new_contents = [
143 'source_set("foo") {',
144 ' sources = [ "../0", "a/1",]',
145 '\tsources += ["a/2" ]',
146 'sources += [ # bla',
147 ' "a/3",',
148 ' ]',
149 ' # sources = ["a/b"]',
150 'sources += # bla',
151 ' ["a/4"]#bla',
152 '}',
153 'static_library("bar"){',
154 ' deps = []',
155 ' sources = []',
156 ' if (something) {',
157 ' sources += [',
158 '',
159 ' "a/5", "ab", "a/6","a/7",# "a/b"',
160 ' "a/8"]',
161 ' sources',
162 ' += [ "a/9" ]}',
163 '}',
164 ]
165
166 mock_output_api = MockOutputApi()
167 mock_input_api = MockInputApi()
168 mock_input_api.files = [
169 MockAffectedFile('chrome/browser/ash/BUILD.gn', new_contents),
170 ]
171
172 results = PRESUBMIT._CheckBuildFilesForIndirectAshSources(mock_input_api,
173 mock_output_api)
174
175 self.assertEqual(len(results), 1)
176 self.assertEqual(results[0].message, self.MESSAGE)
177 self.assertEqual(results[0].items, ["chrome/browser/ash/BUILD.gn"])
178 self.assertEqual(
179 [s.lstrip() for s in results[0].long_text.splitlines()[1:]],
180 ['../0', 'a/1', 'a/2', 'a/3', 'a/4', 'a/5', 'a/6', 'a/7', 'a/8', 'a/9'])
181
182 def testModifications(self):
183 old_contents = [
184 'source_set("foo") {',
185 ' sources = ["x/y", "a/b"]',
186 '}',
187 ]
188 new_contents_good = [
189 'source_set("foo") {',
190 ' sources = ["x/y", "ab"]',
191 '}',
192 ]
193 new_contents_bad = [
194 'source_set("foo") {',
195 ' sources = ["x/y", "a/b", "a/c"]',
196 '}',
197 ]
198
199 mock_output_api = MockOutputApi()
200 mock_input_api = MockInputApi()
201 mock_input_api.files = [
202 MockAffectedFile('chrome/browser/ash/BUILD.gn',
203 new_contents_bad, old_contents),
204 MockAffectedFile('chrome/browser/chromeos/BUILD.gn',
205 new_contents_good, old_contents),
206 ]
207
208 results = PRESUBMIT._CheckBuildFilesForIndirectAshSources(mock_input_api,
209 mock_output_api)
210
211 self.assertEqual(len(results), 1)
212 self.assertEqual(results[0].message, self.MESSAGE)
213 self.assertEqual(results[0].items, ["chrome/browser/ash/BUILD.gn"])
214 self.assertEqual(
215 [s.lstrip() for s in results[0].long_text.splitlines()[1:]],
216 ['a/c'])
217
218
Sergio Villar Senin225de052019-11-07 10:47:17219if __name__ == '__main__':
220 unittest.main()