Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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__)))) | |
|
jamesr
2014/12/13 01:48:06
this is still modifying sys.path, which i don't th
qsr
2014/12/15 09:02:28
That's the way it is done in the chromium repo:
c
| |
| 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' | |
| 18 _IRRELEVANT_BUILD_FILE = 'mojo/foo/some/path/BUILD.gn' | |
| 19 | |
| 20 class AbsoluteReferencesInBuildFilesTest(unittest.TestCase): | |
| 21 """Tests the checking for illegal absolute paths within SDK/EDK buildfiles. | |
| 22 """ | |
| 23 def setUp(self): | |
| 24 self.sdk_absolute_path = '//mojo/public/some/absolute/path' | |
| 25 self.sdk_relative_path = 'mojo/public/some/relative/path' | |
| 26 self.edk_absolute_path = '//mojo/edk/some/absolute/path' | |
| 27 self.edk_relative_path = 'mojo/edk/some/relative/path' | |
| 28 self.whitelisted_external_path = '//testing/gtest' | |
| 29 self.non_whitelisted_external_path = '//base' | |
| 30 | |
| 31 def inputApiContainingFileWithPaths(self, filename, paths): | |
| 32 """Returns a MockInputApi object with a single file having |filename| as | |
| 33 its name and |paths| as its contents, with each path being wrapped in a | |
| 34 pair of double-quotes to match the syntax for strings within BUILD.gn | |
| 35 files.""" | |
| 36 contents = [ '"%s"' % path for path in paths ] | |
| 37 mock_file = MockFile(filename, contents) | |
| 38 mock_input_api = MockInputApi() | |
| 39 mock_input_api.files.append(mock_file) | |
| 40 return mock_input_api | |
| 41 | |
| 42 def checkWarningWithSingleItem(self, | |
| 43 warning, | |
| 44 expected_message, | |
| 45 build_file, | |
| 46 line_num, | |
| 47 referenced_path): | |
| 48 """Checks that |warning| has a message of |expected_message| and a single | |
| 49 item whose contents are the absolute path warning item for | |
| 50 (build_file, line_num, referenced_path).""" | |
| 51 self.assertEqual(expected_message, warning.message) | |
| 52 self.assertEqual(1, len(warning.items)) | |
| 53 expected_item = PRESUBMIT._PathReferenceInBuildFileWarningItem( | |
| 54 build_file, line_num, referenced_path) | |
| 55 self.assertEqual(expected_item, warning.items[0]) | |
| 56 | |
| 57 def checkSDKAbsolutePathWarningWithSingleItem(self, | |
| 58 warning, | |
| 59 package, | |
| 60 build_file, | |
| 61 line_num, | |
| 62 referenced_path): | |
| 63 """Checks that |warning| has the message for an absolute SDK path within | |
| 64 |package| and a single item whose contents are the absolute path warning | |
| 65 item for (build_file, line_num, referenced_path).""" | |
| 66 expected_message = \ | |
| 67 PRESUBMIT._ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package] | |
| 68 self.checkWarningWithSingleItem(warning, | |
| 69 expected_message, | |
| 70 build_file, | |
| 71 line_num, | |
| 72 referenced_path) | |
| 73 | |
| 74 def testAbsoluteSDKReferenceInSDKBuildFile(self): | |
| 75 """Tests that an absolute SDK path within an SDK buildfile is flagged.""" | |
| 76 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 77 _SDK_BUILD_FILE, | |
| 78 [ self.sdk_relative_path, self.sdk_absolute_path ]) | |
| 79 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 80 | |
| 81 self.assertEqual(1, len(warnings)) | |
| 82 self.checkSDKAbsolutePathWarningWithSingleItem(warnings[0], | |
| 83 'SDK', | |
| 84 _SDK_BUILD_FILE, | |
| 85 2, | |
| 86 self.sdk_absolute_path) | |
| 87 | |
| 88 def testExternalReferenceInSDKBuildFile(self): | |
| 89 """Tests that an illegal external path in an SDK buildfile is flagged.""" | |
| 90 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 91 _SDK_BUILD_FILE, | |
| 92 [ self.non_whitelisted_external_path, self.whitelisted_external_path ]) | |
| 93 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 94 | |
| 95 self.assertEqual(1, len(warnings)) | |
| 96 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE | |
| 97 self.checkWarningWithSingleItem(warnings[0], | |
| 98 expected_message, | |
| 99 _SDK_BUILD_FILE, | |
| 100 1, | |
| 101 self.non_whitelisted_external_path) | |
| 102 | |
| 103 def testAbsoluteEDKReferenceInSDKBuildFile(self): | |
| 104 """Tests that an absolute EDK path in an SDK buildfile is flagged.""" | |
| 105 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 106 _SDK_BUILD_FILE, | |
| 107 [ self.edk_absolute_path ]) | |
| 108 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 109 | |
| 110 self.assertEqual(1, len(warnings)) | |
| 111 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE | |
| 112 self.checkWarningWithSingleItem(warnings[0], | |
| 113 expected_message, | |
| 114 _SDK_BUILD_FILE, | |
| 115 1, | |
| 116 self.edk_absolute_path) | |
| 117 | |
| 118 def testAbsoluteSDKReferenceInEDKBuildFile(self): | |
| 119 """Tests that an absolute SDK path within an EDK buildfile is flagged.""" | |
| 120 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 121 _EDK_BUILD_FILE, | |
| 122 [ self.sdk_relative_path, self.sdk_absolute_path ]) | |
| 123 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 124 self.assertEqual(1, len(warnings)) | |
| 125 self.checkSDKAbsolutePathWarningWithSingleItem(warnings[0], | |
| 126 'EDK', | |
| 127 _EDK_BUILD_FILE, | |
| 128 2, | |
| 129 self.sdk_absolute_path) | |
| 130 | |
| 131 def testAbsoluteEDKReferenceInEDKBuildFile(self): | |
| 132 """Tests that an absolute EDK path in an EDK buildfile is flagged.""" | |
| 133 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 134 _EDK_BUILD_FILE, | |
| 135 [ self.edk_absolute_path, self.edk_relative_path ]) | |
| 136 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 137 | |
| 138 self.assertEqual(1, len(warnings)) | |
| 139 expected_message = PRESUBMIT._ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE | |
| 140 self.checkWarningWithSingleItem(warnings[0], | |
| 141 expected_message, | |
| 142 _EDK_BUILD_FILE, | |
| 143 1, | |
| 144 self.edk_absolute_path) | |
| 145 | |
| 146 def testExternalReferenceInEDKBuildFile(self): | |
| 147 """Tests that an external path in an EDK buildfile is not flagged.""" | |
| 148 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 149 _EDK_BUILD_FILE, | |
| 150 [ self.non_whitelisted_external_path, self.whitelisted_external_path ]) | |
| 151 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 152 self.assertEqual(0, len(warnings)) | |
| 153 | |
| 154 def testIrrelevantBuildFile(self): | |
| 155 """Tests that nothing is flagged in a non SDK/EDK buildfile.""" | |
| 156 mock_input_api = self.inputApiContainingFileWithPaths( | |
| 157 _IRRELEVANT_BUILD_FILE, | |
| 158 [ self.sdk_absolute_path, | |
| 159 self.sdk_relative_path, | |
| 160 self.edk_absolute_path, | |
| 161 self.edk_relative_path, | |
| 162 self.non_whitelisted_external_path, | |
| 163 self.whitelisted_external_path ]) | |
| 164 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 165 self.assertEqual(0, len(warnings)) | |
| 166 | |
| 167 class SourceSetTypesInBuildFilesTest(unittest.TestCase): | |
| 168 """Tests checking of correct source set types within SDK/EDK buildfiles.""" | |
| 169 | |
| 170 def inputApiContainingFileWithSourceSets(self, filename, source_sets): | |
| 171 """Returns a MockInputApi object containing a single file having |filename| | |
| 172 as its name and |source_sets| as its contents.""" | |
| 173 mock_file = MockFile(filename, source_sets) | |
| 174 mock_input_api = MockInputApi() | |
| 175 mock_input_api.files.append(mock_file) | |
| 176 return mock_input_api | |
| 177 | |
| 178 def checkWarningWithSingleItem(self, | |
| 179 warning, | |
| 180 package, | |
| 181 build_file, | |
| 182 line_num): | |
| 183 """Checks that warning has the expected incorrect source set type message | |
| 184 for |package| and a single item whose contents are the incorrect source | |
| 185 set type item for (build_file, line_num).""" | |
| 186 expected_message = \ | |
| 187 PRESUBMIT._INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES[package] | |
| 188 self.assertEqual(expected_message, warning.message) | |
| 189 self.assertEqual(1, len(warning.items)) | |
| 190 expected_item = PRESUBMIT._IncorrectSourceSetTypeWarningItem( | |
| 191 build_file, line_num) | |
| 192 self.assertEqual(expected_item, warning.items[0]) | |
| 193 | |
| 194 def testNakedSourceSetInSDKBuildFile(self): | |
| 195 """Tests that a source_set within an SDK buildfile is flagged.""" | |
| 196 mock_input_api = self.inputApiContainingFileWithSourceSets( | |
| 197 _SDK_BUILD_FILE, | |
| 198 [ 'mojo_sdk_source_set(', 'source_set(' ]) | |
| 199 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 200 | |
| 201 self.assertEqual(1, len(warnings)) | |
| 202 self.checkWarningWithSingleItem(warnings[0], 'SDK', _SDK_BUILD_FILE, 2) | |
| 203 | |
| 204 def testEDKSourceSetInSDKBuildFile(self): | |
| 205 """Tests that a mojo_edk_source_set within an SDK buildfile is flagged.""" | |
| 206 mock_input_api = self.inputApiContainingFileWithSourceSets( | |
| 207 _SDK_BUILD_FILE, | |
| 208 [ 'mojo_sdk_source_set(', 'mojo_edk_source_set(' ]) | |
| 209 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 210 | |
| 211 self.assertEqual(1, len(warnings)) | |
| 212 self.checkWarningWithSingleItem(warnings[0], 'SDK', _SDK_BUILD_FILE, 2) | |
| 213 | |
| 214 def testNakedSourceSetInEDKBuildFile(self): | |
| 215 """Tests that a source_set within an EDK buildfile is flagged.""" | |
| 216 mock_input_api = self.inputApiContainingFileWithSourceSets( | |
| 217 _EDK_BUILD_FILE, | |
| 218 [ 'source_set(', 'mojo_edk_source_set(' ]) | |
| 219 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 220 | |
| 221 self.assertEqual(1, len(warnings)) | |
| 222 self.checkWarningWithSingleItem(warnings[0], 'EDK', _EDK_BUILD_FILE, 1) | |
| 223 | |
| 224 def testSDKSourceSetInEDKBuildFile(self): | |
| 225 """Tests that a mojo_sdk_source_set within an EDK buildfile is flagged.""" | |
| 226 mock_input_api = self.inputApiContainingFileWithSourceSets( | |
| 227 _EDK_BUILD_FILE, | |
| 228 [ 'mojo_sdk_source_set(', 'mojo_edk_source_set(' ]) | |
| 229 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 230 | |
| 231 self.assertEqual(1, len(warnings)) | |
| 232 self.checkWarningWithSingleItem(warnings[0], 'EDK', _EDK_BUILD_FILE, 1) | |
| 233 | |
| 234 def testIrrelevantBuildFile(self): | |
| 235 """Tests that a source_set in a non-SDK/EDK buildfile isn't flagged.""" | |
| 236 mock_input_api = self.inputApiContainingFileWithSourceSets( | |
| 237 _IRRELEVANT_BUILD_FILE, | |
| 238 [ 'source_set(' ]) | |
| 239 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) | |
| 240 self.assertEqual(0, len(warnings)) | |
| 241 | |
| 242 if __name__ == '__main__': | |
| 243 unittest.main() | |
| OLD | NEW |