blob: 3e2443c7e77c6355306069bcd21da7c5765a131c [file] [log] [blame]
Benjamin Lerman3723fae2014-12-16 10:27:46 +01001#!/usr/bin/env python
2# Copyright 2014 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
6import os
7import sys
8import unittest
9
10import PRESUBMIT
11
12sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13from PRESUBMIT_test_mocks import MockFile
14from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
15
16_SDK_BUILD_FILE = 'mojo/public/some/path/BUILD.gn'
17_EDK_BUILD_FILE = 'mojo/edk/some/path/BUILD.gn'
Colin Blundell915d6bd2015-01-28 16:46:53 +010018_SERVICE_BUILD_FILE = 'mojo/services/some_service/public/BUILD.gn'
Benjamin Lerman3723fae2014-12-16 10:27:46 +010019_IRRELEVANT_BUILD_FILE = 'mojo/foo/some/path/BUILD.gn'
20
Colin Blundell4dc8a112015-01-28 16:39:58 +010021_PACKAGE_BUILDFILES = {
22 'SDK' : _SDK_BUILD_FILE,
23 'EDK' : _EDK_BUILD_FILE,
Colin Blundell915d6bd2015-01-28 16:46:53 +010024 'services' : _SERVICE_BUILD_FILE
Colin Blundell4dc8a112015-01-28 16:39:58 +010025}
26
Benjamin Lerman3723fae2014-12-16 10:27:46 +010027class AbsoluteReferencesInBuildFilesTest(unittest.TestCase):
28 """Tests the checking for illegal absolute paths within SDK/EDK buildfiles.
29 """
30 def setUp(self):
31 self.sdk_absolute_path = '//mojo/public/some/absolute/path'
32 self.sdk_relative_path = 'mojo/public/some/relative/path'
33 self.edk_absolute_path = '//mojo/edk/some/absolute/path'
34 self.edk_relative_path = 'mojo/edk/some/relative/path'
Colin Blundell915d6bd2015-01-28 16:46:53 +010035 self.service_absolute_path = '//mojo/services/some/service'
36 self.service_relative_path = '../../../some/service'
Benjamin Lerman3723fae2014-12-16 10:27:46 +010037 self.whitelisted_external_path = '//testing/gtest'
38 self.non_whitelisted_external_path = '//base'
39
40 def inputApiContainingFileWithPaths(self, filename, paths):
41 """Returns a MockInputApi object with a single file having |filename| as
42 its name and |paths| as its contents, with each path being wrapped in a
43 pair of double-quotes to match the syntax for strings within BUILD.gn
44 files."""
45 contents = [ '"%s"' % path for path in paths ]
46 mock_file = MockFile(filename, contents)
47 mock_input_api = MockInputApi()
48 mock_input_api.files.append(mock_file)
49 return mock_input_api
50
51 def checkWarningWithSingleItem(self,
52 warning,
53 expected_message,
54 build_file,
55 line_num,
56 referenced_path):
57 """Checks that |warning| has a message of |expected_message| and a single
58 item whose contents are the absolute path warning item for
59 (build_file, line_num, referenced_path)."""
60 self.assertEqual(expected_message, warning.message)
61 self.assertEqual(1, len(warning.items))
62 expected_item = PRESUBMIT._PathReferenceInBuildFileWarningItem(
63 build_file, line_num, referenced_path)
64 self.assertEqual(expected_item, warning.items[0])
65
66 def checkSDKAbsolutePathWarningWithSingleItem(self,
67 warning,
68 package,
69 build_file,
70 line_num,
71 referenced_path):
72 """Checks that |warning| has the message for an absolute SDK path within
73 |package| and a single item whose contents are the absolute path warning
74 item for (build_file, line_num, referenced_path)."""
75 expected_message = \
76 PRESUBMIT._ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package]
77 self.checkWarningWithSingleItem(warning,
78 expected_message,
79 build_file,
80 line_num,
81 referenced_path)
82
Colin Blundell4dc8a112015-01-28 16:39:58 +010083 def _testAbsoluteSDKReferenceInPackage(self, package):
84 """Tests that an absolute SDK path within |package| is flagged."""
85 buildfile = _PACKAGE_BUILDFILES[package]
Benjamin Lerman3723fae2014-12-16 10:27:46 +010086 mock_input_api = self.inputApiContainingFileWithPaths(
Colin Blundell4dc8a112015-01-28 16:39:58 +010087 buildfile,
Benjamin Lerman3723fae2014-12-16 10:27:46 +010088 [ self.sdk_relative_path, self.sdk_absolute_path ])
89 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
90
91 self.assertEqual(1, len(warnings))
92 self.checkSDKAbsolutePathWarningWithSingleItem(warnings[0],
Colin Blundell4dc8a112015-01-28 16:39:58 +010093 package,
94 buildfile,
Benjamin Lerman3723fae2014-12-16 10:27:46 +010095 2,
96 self.sdk_absolute_path)
97
Colin Blundell4dc8a112015-01-28 16:39:58 +010098 def _testExternalReferenceInPackage(self, package):
99 """Tests that an illegal external path within |package| is flagged."""
100 buildfile = _PACKAGE_BUILDFILES[package]
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100101 mock_input_api = self.inputApiContainingFileWithPaths(
Colin Blundell4dc8a112015-01-28 16:39:58 +0100102 buildfile,
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100103 [ self.non_whitelisted_external_path, self.whitelisted_external_path ])
104 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
105
Colin Blundell4dc8a112015-01-28 16:39:58 +0100106 if package == 'EDK':
107 # All external paths are allowed in the EDK.
108 self.assertEqual(0, len(warnings))
109 return
110
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100111 self.assertEqual(1, len(warnings))
112 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE
113 self.checkWarningWithSingleItem(warnings[0],
114 expected_message,
Colin Blundell4dc8a112015-01-28 16:39:58 +0100115 buildfile,
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100116 1,
117 self.non_whitelisted_external_path)
118
Colin Blundell4dc8a112015-01-28 16:39:58 +0100119 def testAbsoluteSDKReferenceInSDKBuildFile(self):
120 """Tests that an absolute SDK path within an SDK buildfile is flagged."""
121 self._testAbsoluteSDKReferenceInPackage('SDK')
122
123 def testExternalReferenceInSDKBuildFile(self):
124 """Tests that an illegal external path in an SDK buildfile is flagged."""
125 self._testExternalReferenceInPackage('SDK')
126
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100127 def testAbsoluteEDKReferenceInSDKBuildFile(self):
128 """Tests that an absolute EDK path in an SDK buildfile is flagged."""
129 mock_input_api = self.inputApiContainingFileWithPaths(
130 _SDK_BUILD_FILE,
131 [ self.edk_absolute_path ])
132 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
133
134 self.assertEqual(1, len(warnings))
135 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE
136 self.checkWarningWithSingleItem(warnings[0],
137 expected_message,
138 _SDK_BUILD_FILE,
139 1,
140 self.edk_absolute_path)
141
142 def testAbsoluteSDKReferenceInEDKBuildFile(self):
143 """Tests that an absolute SDK path within an EDK buildfile is flagged."""
Colin Blundell4dc8a112015-01-28 16:39:58 +0100144 self._testAbsoluteSDKReferenceInPackage('EDK')
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100145
146 def testAbsoluteEDKReferenceInEDKBuildFile(self):
147 """Tests that an absolute EDK path in an EDK buildfile is flagged."""
148 mock_input_api = self.inputApiContainingFileWithPaths(
149 _EDK_BUILD_FILE,
150 [ self.edk_absolute_path, self.edk_relative_path ])
151 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
152
153 self.assertEqual(1, len(warnings))
154 expected_message = PRESUBMIT._ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE
155 self.checkWarningWithSingleItem(warnings[0],
156 expected_message,
157 _EDK_BUILD_FILE,
158 1,
159 self.edk_absolute_path)
160
161 def testExternalReferenceInEDKBuildFile(self):
162 """Tests that an external path in an EDK buildfile is not flagged."""
Colin Blundell4dc8a112015-01-28 16:39:58 +0100163 self._testExternalReferenceInPackage('EDK')
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100164
Colin Blundell915d6bd2015-01-28 16:46:53 +0100165 def testAbsoluteSDKReferenceInServiceBuildFile(self):
166 """Tests that an absolute SDK path within a service's public buildfile is
167 flagged."""
168 self._testAbsoluteSDKReferenceInPackage("services")
169
170 def testAbsoluteServiceReferenceInServiceBuildFile(self):
171 """Tests that an absolute path to a service within a service's public
172 buildfile is flagged."""
173 mock_input_api = self.inputApiContainingFileWithPaths(
174 _SERVICE_BUILD_FILE,
175 [ self.service_relative_path, self.service_absolute_path ])
176 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
177
178 self.assertEqual(1, len(warnings))
179 expected_message = \
180 PRESUBMIT._ILLEGAL_SERVICES_ABSOLUTE_PATH_WARNING_MESSAGE
181 self.checkWarningWithSingleItem(warnings[0],
182 expected_message,
183 _SERVICE_BUILD_FILE,
184 2,
185 self.service_absolute_path)
186
187 def testExternalReferenceInServiceBuildFile(self):
188 """Tests that an illegal external path in a service's buildfile is flagged
189 ."""
190 self._testExternalReferenceInPackage("services")
191
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100192 def testIrrelevantBuildFile(self):
193 """Tests that nothing is flagged in a non SDK/EDK buildfile."""
194 mock_input_api = self.inputApiContainingFileWithPaths(
195 _IRRELEVANT_BUILD_FILE,
196 [ self.sdk_absolute_path,
197 self.sdk_relative_path,
198 self.edk_absolute_path,
199 self.edk_relative_path,
200 self.non_whitelisted_external_path,
201 self.whitelisted_external_path ])
202 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
203 self.assertEqual(0, len(warnings))
204
205class SourceSetTypesInBuildFilesTest(unittest.TestCase):
206 """Tests checking of correct source set types within SDK/EDK buildfiles."""
207
208 def inputApiContainingFileWithSourceSets(self, filename, source_sets):
209 """Returns a MockInputApi object containing a single file having |filename|
210 as its name and |source_sets| as its contents."""
211 mock_file = MockFile(filename, source_sets)
212 mock_input_api = MockInputApi()
213 mock_input_api.files.append(mock_file)
214 return mock_input_api
215
216 def checkWarningWithSingleItem(self,
217 warning,
218 package,
219 build_file,
220 line_num):
221 """Checks that warning has the expected incorrect source set type message
222 for |package| and a single item whose contents are the incorrect source
223 set type item for (build_file, line_num)."""
224 expected_message = \
225 PRESUBMIT._INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES[package]
226 self.assertEqual(expected_message, warning.message)
227 self.assertEqual(1, len(warning.items))
228 expected_item = PRESUBMIT._IncorrectSourceSetTypeWarningItem(
229 build_file, line_num)
230 self.assertEqual(expected_item, warning.items[0])
231
Colin Blundell4dc8a112015-01-28 16:39:58 +0100232 def _testNakedSourceSetInPackage(self, package):
233 """Tests that a source_set within |package| is flagged."""
234 buildfile = _PACKAGE_BUILDFILES[package]
235 naked_source_set = 'source_set('
236 correct_source_set = 'mojo_sdk_source_set('
237 if package == 'EDK':
238 correct_source_set = 'mojo_edk_source_set('
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100239 mock_input_api = self.inputApiContainingFileWithSourceSets(
Colin Blundell4dc8a112015-01-28 16:39:58 +0100240 buildfile,
241 [ correct_source_set, naked_source_set ])
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100242 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
243
244 self.assertEqual(1, len(warnings))
Colin Blundell4dc8a112015-01-28 16:39:58 +0100245 self.checkWarningWithSingleItem(warnings[0], package, buildfile, 2)
246
247 def _testWrongTypeOfWrapperSourceSetInPackage(self, package):
248 """Tests that the wrong type of wrapper source_set in |package| is flagged
249 (e.g., mojo_edk_source_set within the SDK)."""
250 buildfile = _PACKAGE_BUILDFILES[package]
251 mock_input_api = self.inputApiContainingFileWithSourceSets(
252 buildfile,
253 [ 'mojo_sdk_source_set(', 'mojo_edk_source_set(' ])
254 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
255
256 self.assertEqual(1, len(warnings))
257 warning_line = 2
258 if package == 'EDK':
259 warning_line = 1
260 self.checkWarningWithSingleItem(warnings[0],
261 package,
262 buildfile,
263 warning_line)
264
265 def testNakedSourceSetInSDKBuildFile(self):
266 """Tests that a source_set within the SDK is flagged."""
267 self._testNakedSourceSetInPackage('SDK')
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100268
Etienne Membrives0e776c22014-12-17 14:26:23 +0100269 def testPythonSourceSetInSDKBuildFile(self):
270 """Tests that a python_binary_source_set within an SDK buildfile is
271 accepted."""
272 mock_input_api = self.inputApiContainingFileWithSourceSets(
273 _SDK_BUILD_FILE,
274 [ 'mojo_sdk_source_set(', 'python_binary_source_set(' ])
275 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
276
277 self.assertEqual(0, len(warnings))
278
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100279 def testEDKSourceSetInSDKBuildFile(self):
280 """Tests that a mojo_edk_source_set within an SDK buildfile is flagged."""
Colin Blundell4dc8a112015-01-28 16:39:58 +0100281 self._testWrongTypeOfWrapperSourceSetInPackage('SDK')
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100282
283 def testNakedSourceSetInEDKBuildFile(self):
Colin Blundell4dc8a112015-01-28 16:39:58 +0100284 """Tests that a source_set within the EDK is flagged."""
285 self._testNakedSourceSetInPackage('EDK')
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100286
287 def testSDKSourceSetInEDKBuildFile(self):
288 """Tests that a mojo_sdk_source_set within an EDK buildfile is flagged."""
Colin Blundell4dc8a112015-01-28 16:39:58 +0100289 self._testWrongTypeOfWrapperSourceSetInPackage('EDK')
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100290
Colin Blundell915d6bd2015-01-28 16:46:53 +0100291 def testNakedSourceSetInServiceBuildFile(self):
292 """Tests that a source_set within a service's public buildfile is flagged.
293 """
294 self._testNakedSourceSetInPackage("services")
295
296 def testEDKSourceSetInServiceBuildFile(self):
297 """Tests that a mojo_edk_source_set within a service's public buildfile is
298 flagged."""
299 self._testWrongTypeOfWrapperSourceSetInPackage("services")
300
Benjamin Lerman3723fae2014-12-16 10:27:46 +0100301 def testIrrelevantBuildFile(self):
302 """Tests that a source_set in a non-SDK/EDK buildfile isn't flagged."""
303 mock_input_api = self.inputApiContainingFileWithSourceSets(
304 _IRRELEVANT_BUILD_FILE,
305 [ 'source_set(' ])
306 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
307 self.assertEqual(0, len(warnings))
308
309if __name__ == '__main__':
310 unittest.main()