Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 1 | #!/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 | |
| 6 | import os |
| 7 | import sys |
| 8 | import unittest |
| 9 | |
| 10 | import PRESUBMIT |
| 11 | |
| 12 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 13 | from PRESUBMIT_test_mocks import MockFile |
| 14 | from 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 Blundell | 915d6bd | 2015-01-28 16:46:53 +0100 | [diff] [blame] | 18 | _SERVICE_BUILD_FILE = 'mojo/services/some_service/public/BUILD.gn' |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 19 | _IRRELEVANT_BUILD_FILE = 'mojo/foo/some/path/BUILD.gn' |
| 20 | |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 21 | _PACKAGE_BUILDFILES = { |
| 22 | 'SDK' : _SDK_BUILD_FILE, |
| 23 | 'EDK' : _EDK_BUILD_FILE, |
Colin Blundell | 915d6bd | 2015-01-28 16:46:53 +0100 | [diff] [blame] | 24 | 'services' : _SERVICE_BUILD_FILE |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 25 | } |
| 26 | |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 27 | class 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 Blundell | 915d6bd | 2015-01-28 16:46:53 +0100 | [diff] [blame] | 35 | self.service_absolute_path = '//mojo/services/some/service' |
| 36 | self.service_relative_path = '../../../some/service' |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 37 | 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 Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 83 | def _testAbsoluteSDKReferenceInPackage(self, package): |
| 84 | """Tests that an absolute SDK path within |package| is flagged.""" |
| 85 | buildfile = _PACKAGE_BUILDFILES[package] |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 86 | mock_input_api = self.inputApiContainingFileWithPaths( |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 87 | buildfile, |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 88 | [ 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 Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 93 | package, |
| 94 | buildfile, |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 95 | 2, |
| 96 | self.sdk_absolute_path) |
| 97 | |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 98 | def _testExternalReferenceInPackage(self, package): |
| 99 | """Tests that an illegal external path within |package| is flagged.""" |
| 100 | buildfile = _PACKAGE_BUILDFILES[package] |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 101 | mock_input_api = self.inputApiContainingFileWithPaths( |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 102 | buildfile, |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 103 | [ self.non_whitelisted_external_path, self.whitelisted_external_path ]) |
| 104 | warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) |
| 105 | |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 106 | if package == 'EDK': |
| 107 | # All external paths are allowed in the EDK. |
| 108 | self.assertEqual(0, len(warnings)) |
| 109 | return |
| 110 | |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 111 | self.assertEqual(1, len(warnings)) |
| 112 | expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE |
| 113 | self.checkWarningWithSingleItem(warnings[0], |
| 114 | expected_message, |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 115 | buildfile, |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 116 | 1, |
| 117 | self.non_whitelisted_external_path) |
| 118 | |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 119 | 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 Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 127 | 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 Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 144 | self._testAbsoluteSDKReferenceInPackage('EDK') |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 145 | |
| 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 Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 163 | self._testExternalReferenceInPackage('EDK') |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 164 | |
Colin Blundell | 915d6bd | 2015-01-28 16:46:53 +0100 | [diff] [blame] | 165 | 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 Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 192 | 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 | |
| 205 | class 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 Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 232 | 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 Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 239 | mock_input_api = self.inputApiContainingFileWithSourceSets( |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 240 | buildfile, |
| 241 | [ correct_source_set, naked_source_set ]) |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 242 | warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) |
| 243 | |
| 244 | self.assertEqual(1, len(warnings)) |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 245 | 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 Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 268 | |
Etienne Membrives | 0e776c2 | 2014-12-17 14:26:23 +0100 | [diff] [blame] | 269 | 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 Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 279 | def testEDKSourceSetInSDKBuildFile(self): |
| 280 | """Tests that a mojo_edk_source_set within an SDK buildfile is flagged.""" |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 281 | self._testWrongTypeOfWrapperSourceSetInPackage('SDK') |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 282 | |
| 283 | def testNakedSourceSetInEDKBuildFile(self): |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 284 | """Tests that a source_set within the EDK is flagged.""" |
| 285 | self._testNakedSourceSetInPackage('EDK') |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 286 | |
| 287 | def testSDKSourceSetInEDKBuildFile(self): |
| 288 | """Tests that a mojo_sdk_source_set within an EDK buildfile is flagged.""" |
Colin Blundell | 4dc8a11 | 2015-01-28 16:39:58 +0100 | [diff] [blame] | 289 | self._testWrongTypeOfWrapperSourceSetInPackage('EDK') |
Benjamin Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 290 | |
Colin Blundell | 915d6bd | 2015-01-28 16:46:53 +0100 | [diff] [blame] | 291 | 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 Lerman | 3723fae | 2014-12-16 10:27:46 +0100 | [diff] [blame] | 301 | 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 | |
| 309 | if __name__ == '__main__': |
| 310 | unittest.main() |