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