Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(465)

Side by Side Diff: mojo/PRESUBMIT_test.py

Issue 869873005: Refactor //mojo/PRESUBMIT_tests.py (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Response to review Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import os 6 import os
7 import sys 7 import sys
8 import unittest 8 import unittest
9 9
10 import PRESUBMIT 10 import PRESUBMIT
11 11
12 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 12 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13 from PRESUBMIT_test_mocks import MockFile 13 from PRESUBMIT_test_mocks import MockFile
14 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi 14 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
15 15
16 _SDK_BUILD_FILE = 'mojo/public/some/path/BUILD.gn' 16 _SDK_BUILD_FILE = 'mojo/public/some/path/BUILD.gn'
17 _EDK_BUILD_FILE = 'mojo/edk/some/path/BUILD.gn' 17 _EDK_BUILD_FILE = 'mojo/edk/some/path/BUILD.gn'
18 _IRRELEVANT_BUILD_FILE = 'mojo/foo/some/path/BUILD.gn' 18 _IRRELEVANT_BUILD_FILE = 'mojo/foo/some/path/BUILD.gn'
19 19
20 _PACKAGE_BUILDFILES = {
21 "SDK" : _SDK_BUILD_FILE,
qsr 2015/01/28 15:26:01 This file seems to be using ' for strings. Here an
blundell 2015/01/28 15:39:47 Done.
22 "EDK" : _EDK_BUILD_FILE,
23 }
24
20 class AbsoluteReferencesInBuildFilesTest(unittest.TestCase): 25 class AbsoluteReferencesInBuildFilesTest(unittest.TestCase):
21 """Tests the checking for illegal absolute paths within SDK/EDK buildfiles. 26 """Tests the checking for illegal absolute paths within SDK/EDK buildfiles.
22 """ 27 """
23 def setUp(self): 28 def setUp(self):
24 self.sdk_absolute_path = '//mojo/public/some/absolute/path' 29 self.sdk_absolute_path = '//mojo/public/some/absolute/path'
25 self.sdk_relative_path = 'mojo/public/some/relative/path' 30 self.sdk_relative_path = 'mojo/public/some/relative/path'
26 self.edk_absolute_path = '//mojo/edk/some/absolute/path' 31 self.edk_absolute_path = '//mojo/edk/some/absolute/path'
27 self.edk_relative_path = 'mojo/edk/some/relative/path' 32 self.edk_relative_path = 'mojo/edk/some/relative/path'
28 self.whitelisted_external_path = '//testing/gtest' 33 self.whitelisted_external_path = '//testing/gtest'
29 self.non_whitelisted_external_path = '//base' 34 self.non_whitelisted_external_path = '//base'
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 |package| and a single item whose contents are the absolute path warning 69 |package| and a single item whose contents are the absolute path warning
65 item for (build_file, line_num, referenced_path).""" 70 item for (build_file, line_num, referenced_path)."""
66 expected_message = \ 71 expected_message = \
67 PRESUBMIT._ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package] 72 PRESUBMIT._ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package]
68 self.checkWarningWithSingleItem(warning, 73 self.checkWarningWithSingleItem(warning,
69 expected_message, 74 expected_message,
70 build_file, 75 build_file,
71 line_num, 76 line_num,
72 referenced_path) 77 referenced_path)
73 78
74 def testAbsoluteSDKReferenceInSDKBuildFile(self): 79 def _testAbsoluteSDKReferenceInPackage(self, package):
75 """Tests that an absolute SDK path within an SDK buildfile is flagged.""" 80 """Tests that an absolute SDK path within |package| is flagged."""
81 buildfile = _PACKAGE_BUILDFILES[package]
76 mock_input_api = self.inputApiContainingFileWithPaths( 82 mock_input_api = self.inputApiContainingFileWithPaths(
77 _SDK_BUILD_FILE, 83 buildfile,
78 [ self.sdk_relative_path, self.sdk_absolute_path ]) 84 [ self.sdk_relative_path, self.sdk_absolute_path ])
79 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 85 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
80 86
81 self.assertEqual(1, len(warnings)) 87 self.assertEqual(1, len(warnings))
82 self.checkSDKAbsolutePathWarningWithSingleItem(warnings[0], 88 self.checkSDKAbsolutePathWarningWithSingleItem(warnings[0],
83 'SDK', 89 package,
84 _SDK_BUILD_FILE, 90 buildfile,
85 2, 91 2,
86 self.sdk_absolute_path) 92 self.sdk_absolute_path)
87 93
88 def testExternalReferenceInSDKBuildFile(self): 94 def _testExternalReferenceInPackage(self, package):
89 """Tests that an illegal external path in an SDK buildfile is flagged.""" 95 """Tests that an illegal external path within |package| is flagged."""
96 buildfile = _PACKAGE_BUILDFILES[package]
90 mock_input_api = self.inputApiContainingFileWithPaths( 97 mock_input_api = self.inputApiContainingFileWithPaths(
91 _SDK_BUILD_FILE, 98 buildfile,
92 [ self.non_whitelisted_external_path, self.whitelisted_external_path ]) 99 [ self.non_whitelisted_external_path, self.whitelisted_external_path ])
93 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 100 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
94 101
102 if package == "EDK":
103 # All external paths are allowed in the EDK.
104 self.assertEqual(0, len(warnings))
105 return
106
95 self.assertEqual(1, len(warnings)) 107 self.assertEqual(1, len(warnings))
96 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE 108 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE
97 self.checkWarningWithSingleItem(warnings[0], 109 self.checkWarningWithSingleItem(warnings[0],
98 expected_message, 110 expected_message,
99 _SDK_BUILD_FILE, 111 buildfile,
100 1, 112 1,
101 self.non_whitelisted_external_path) 113 self.non_whitelisted_external_path)
102 114
115 def testAbsoluteSDKReferenceInSDKBuildFile(self):
116 """Tests that an absolute SDK path within an SDK buildfile is flagged."""
117 self._testAbsoluteSDKReferenceInPackage("SDK")
118
119 def testExternalReferenceInSDKBuildFile(self):
120 """Tests that an illegal external path in an SDK buildfile is flagged."""
121 self._testExternalReferenceInPackage("SDK")
122
103 def testAbsoluteEDKReferenceInSDKBuildFile(self): 123 def testAbsoluteEDKReferenceInSDKBuildFile(self):
104 """Tests that an absolute EDK path in an SDK buildfile is flagged.""" 124 """Tests that an absolute EDK path in an SDK buildfile is flagged."""
105 mock_input_api = self.inputApiContainingFileWithPaths( 125 mock_input_api = self.inputApiContainingFileWithPaths(
106 _SDK_BUILD_FILE, 126 _SDK_BUILD_FILE,
107 [ self.edk_absolute_path ]) 127 [ self.edk_absolute_path ])
108 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 128 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
109 129
110 self.assertEqual(1, len(warnings)) 130 self.assertEqual(1, len(warnings))
111 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE 131 expected_message = PRESUBMIT._ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE
112 self.checkWarningWithSingleItem(warnings[0], 132 self.checkWarningWithSingleItem(warnings[0],
113 expected_message, 133 expected_message,
114 _SDK_BUILD_FILE, 134 _SDK_BUILD_FILE,
115 1, 135 1,
116 self.edk_absolute_path) 136 self.edk_absolute_path)
117 137
118 def testAbsoluteSDKReferenceInEDKBuildFile(self): 138 def testAbsoluteSDKReferenceInEDKBuildFile(self):
119 """Tests that an absolute SDK path within an EDK buildfile is flagged.""" 139 """Tests that an absolute SDK path within an EDK buildfile is flagged."""
120 mock_input_api = self.inputApiContainingFileWithPaths( 140 self._testAbsoluteSDKReferenceInPackage("EDK")
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 141
131 def testAbsoluteEDKReferenceInEDKBuildFile(self): 142 def testAbsoluteEDKReferenceInEDKBuildFile(self):
132 """Tests that an absolute EDK path in an EDK buildfile is flagged.""" 143 """Tests that an absolute EDK path in an EDK buildfile is flagged."""
133 mock_input_api = self.inputApiContainingFileWithPaths( 144 mock_input_api = self.inputApiContainingFileWithPaths(
134 _EDK_BUILD_FILE, 145 _EDK_BUILD_FILE,
135 [ self.edk_absolute_path, self.edk_relative_path ]) 146 [ self.edk_absolute_path, self.edk_relative_path ])
136 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 147 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
137 148
138 self.assertEqual(1, len(warnings)) 149 self.assertEqual(1, len(warnings))
139 expected_message = PRESUBMIT._ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE 150 expected_message = PRESUBMIT._ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE
140 self.checkWarningWithSingleItem(warnings[0], 151 self.checkWarningWithSingleItem(warnings[0],
141 expected_message, 152 expected_message,
142 _EDK_BUILD_FILE, 153 _EDK_BUILD_FILE,
143 1, 154 1,
144 self.edk_absolute_path) 155 self.edk_absolute_path)
145 156
146 def testExternalReferenceInEDKBuildFile(self): 157 def testExternalReferenceInEDKBuildFile(self):
147 """Tests that an external path in an EDK buildfile is not flagged.""" 158 """Tests that an external path in an EDK buildfile is not flagged."""
148 mock_input_api = self.inputApiContainingFileWithPaths( 159 self._testExternalReferenceInPackage("EDK")
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 160
154 def testIrrelevantBuildFile(self): 161 def testIrrelevantBuildFile(self):
155 """Tests that nothing is flagged in a non SDK/EDK buildfile.""" 162 """Tests that nothing is flagged in a non SDK/EDK buildfile."""
156 mock_input_api = self.inputApiContainingFileWithPaths( 163 mock_input_api = self.inputApiContainingFileWithPaths(
157 _IRRELEVANT_BUILD_FILE, 164 _IRRELEVANT_BUILD_FILE,
158 [ self.sdk_absolute_path, 165 [ self.sdk_absolute_path,
159 self.sdk_relative_path, 166 self.sdk_relative_path,
160 self.edk_absolute_path, 167 self.edk_absolute_path,
161 self.edk_relative_path, 168 self.edk_relative_path,
162 self.non_whitelisted_external_path, 169 self.non_whitelisted_external_path,
(...skipping 21 matching lines...) Expand all
184 for |package| and a single item whose contents are the incorrect source 191 for |package| and a single item whose contents are the incorrect source
185 set type item for (build_file, line_num).""" 192 set type item for (build_file, line_num)."""
186 expected_message = \ 193 expected_message = \
187 PRESUBMIT._INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES[package] 194 PRESUBMIT._INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES[package]
188 self.assertEqual(expected_message, warning.message) 195 self.assertEqual(expected_message, warning.message)
189 self.assertEqual(1, len(warning.items)) 196 self.assertEqual(1, len(warning.items))
190 expected_item = PRESUBMIT._IncorrectSourceSetTypeWarningItem( 197 expected_item = PRESUBMIT._IncorrectSourceSetTypeWarningItem(
191 build_file, line_num) 198 build_file, line_num)
192 self.assertEqual(expected_item, warning.items[0]) 199 self.assertEqual(expected_item, warning.items[0])
193 200
194 def testNakedSourceSetInSDKBuildFile(self): 201 def _testNakedSourceSetInPackage(self, package):
195 """Tests that a source_set within an SDK buildfile is flagged.""" 202 """Tests that a source_set within |package| is flagged."""
203 buildfile = _PACKAGE_BUILDFILES[package]
204 naked_source_set = 'source_set('
205 correct_source_set = 'mojo_sdk_source_set('
206 if package == "EDK":
207 correct_source_set = 'mojo_edk_source_set('
196 mock_input_api = self.inputApiContainingFileWithSourceSets( 208 mock_input_api = self.inputApiContainingFileWithSourceSets(
197 _SDK_BUILD_FILE, 209 buildfile,
198 [ 'mojo_sdk_source_set(', 'source_set(' ]) 210 [ correct_source_set, naked_source_set ])
199 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 211 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
200 212
201 self.assertEqual(1, len(warnings)) 213 self.assertEqual(1, len(warnings))
202 self.checkWarningWithSingleItem(warnings[0], 'SDK', _SDK_BUILD_FILE, 2) 214 self.checkWarningWithSingleItem(warnings[0], package, buildfile, 2)
215
216 def _testWrongTypeOfWrapperSourceSetInPackage(self, package):
217 """Tests that the wrong type of wrapper source_set in |package| is flagged
218 (e.g., mojo_edk_source_set within the SDK)."""
219 buildfile = _PACKAGE_BUILDFILES[package]
220 mock_input_api = self.inputApiContainingFileWithSourceSets(
221 buildfile,
222 [ 'mojo_sdk_source_set(', 'mojo_edk_source_set(' ])
223 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
224
225 self.assertEqual(1, len(warnings))
226 warning_line = 2
227 if package == "EDK":
228 warning_line = 1
229 self.checkWarningWithSingleItem(warnings[0],
230 package,
231 buildfile,
232 warning_line)
233
234 def testNakedSourceSetInSDKBuildFile(self):
235 """Tests that a source_set within the SDK is flagged."""
236 self._testNakedSourceSetInPackage("SDK")
203 237
204 def testPythonSourceSetInSDKBuildFile(self): 238 def testPythonSourceSetInSDKBuildFile(self):
205 """Tests that a python_binary_source_set within an SDK buildfile is 239 """Tests that a python_binary_source_set within an SDK buildfile is
206 accepted.""" 240 accepted."""
207 mock_input_api = self.inputApiContainingFileWithSourceSets( 241 mock_input_api = self.inputApiContainingFileWithSourceSets(
208 _SDK_BUILD_FILE, 242 _SDK_BUILD_FILE,
209 [ 'mojo_sdk_source_set(', 'python_binary_source_set(' ]) 243 [ 'mojo_sdk_source_set(', 'python_binary_source_set(' ])
210 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 244 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
211 245
212 self.assertEqual(0, len(warnings)) 246 self.assertEqual(0, len(warnings))
213 247
214 def testEDKSourceSetInSDKBuildFile(self): 248 def testEDKSourceSetInSDKBuildFile(self):
215 """Tests that a mojo_edk_source_set within an SDK buildfile is flagged.""" 249 """Tests that a mojo_edk_source_set within an SDK buildfile is flagged."""
216 mock_input_api = self.inputApiContainingFileWithSourceSets( 250 self._testWrongTypeOfWrapperSourceSetInPackage("SDK")
217 _SDK_BUILD_FILE,
218 [ 'mojo_sdk_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], 'SDK', _SDK_BUILD_FILE, 2)
223 251
224 def testNakedSourceSetInEDKBuildFile(self): 252 def testNakedSourceSetInEDKBuildFile(self):
225 """Tests that a source_set within an EDK buildfile is flagged.""" 253 """Tests that a source_set within the EDK is flagged."""
226 mock_input_api = self.inputApiContainingFileWithSourceSets( 254 self._testNakedSourceSetInPackage("EDK")
227 _EDK_BUILD_FILE,
228 [ '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 255
234 def testSDKSourceSetInEDKBuildFile(self): 256 def testSDKSourceSetInEDKBuildFile(self):
235 """Tests that a mojo_sdk_source_set within an EDK buildfile is flagged.""" 257 """Tests that a mojo_sdk_source_set within an EDK buildfile is flagged."""
236 mock_input_api = self.inputApiContainingFileWithSourceSets( 258 self._testWrongTypeOfWrapperSourceSetInPackage("EDK")
237 _EDK_BUILD_FILE,
238 [ 'mojo_sdk_source_set(', 'mojo_edk_source_set(' ])
239 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
240
241 self.assertEqual(1, len(warnings))
242 self.checkWarningWithSingleItem(warnings[0], 'EDK', _EDK_BUILD_FILE, 1)
243 259
244 def testIrrelevantBuildFile(self): 260 def testIrrelevantBuildFile(self):
245 """Tests that a source_set in a non-SDK/EDK buildfile isn't flagged.""" 261 """Tests that a source_set in a non-SDK/EDK buildfile isn't flagged."""
246 mock_input_api = self.inputApiContainingFileWithSourceSets( 262 mock_input_api = self.inputApiContainingFileWithSourceSets(
247 _IRRELEVANT_BUILD_FILE, 263 _IRRELEVANT_BUILD_FILE,
248 [ 'source_set(' ]) 264 [ 'source_set(' ])
249 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi()) 265 warnings = PRESUBMIT._BuildFileChecks(mock_input_api, MockOutputApi())
250 self.assertEqual(0, len(warnings)) 266 self.assertEqual(0, len(warnings))
251 267
252 if __name__ == '__main__': 268 if __name__ == '__main__':
253 unittest.main() 269 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698