| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Unit tests for presubmit_support.py and presubmit_canned_checks.py.""" | 6 """Unit tests for presubmit_support.py and presubmit_canned_checks.py.""" |
| 7 | 7 |
| 8 # pylint: disable=E1101,E1103 | 8 # pylint: disable=E1101,E1103 |
| 9 | 9 |
| 10 import logging | 10 import logging |
| (...skipping 2091 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2102 connection.close() | 2102 connection.close() |
| 2103 self.mox.ReplayAll() | 2103 self.mox.ReplayAll() |
| 2104 | 2104 |
| 2105 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 2105 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
| 2106 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 2106 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
| 2107 self.assertEquals(len(results), 1) | 2107 self.assertEquals(len(results), 1) |
| 2108 self.assertEquals(results[0].__class__, | 2108 self.assertEquals(results[0].__class__, |
| 2109 presubmit.OutputApi.PresubmitNotifyResult) | 2109 presubmit.OutputApi.PresubmitNotifyResult) |
| 2110 | 2110 |
| 2111 def AssertOwnersWorks(self, tbr=False, issue='1', approvers=None, | 2111 def AssertOwnersWorks(self, tbr=False, issue='1', approvers=None, |
| 2112 rietveld_response=None, uncovered_files=None, expected_output=''): | 2112 reviewers=None, is_committing=True, rietveld_response=None, |
| 2113 uncovered_dirs=None, expected_output=''): |
| 2113 if approvers is None: | 2114 if approvers is None: |
| 2114 approvers = set() | 2115 approvers = set() |
| 2115 if uncovered_files is None: | 2116 if reviewers is None: |
| 2116 uncovered_files = set() | 2117 reviewers = set() |
| 2118 reviewers = reviewers.union(approvers) |
| 2119 if uncovered_dirs is None: |
| 2120 uncovered_dirs = set() |
| 2117 | 2121 |
| 2118 change = self.mox.CreateMock(presubmit.Change) | 2122 change = self.mox.CreateMock(presubmit.Change) |
| 2119 change.issue = issue | 2123 change.issue = issue |
| 2120 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 2124 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 2121 input_api = self.MockInputApi(change, False) | 2125 input_api = self.MockInputApi(change, False) |
| 2122 fake_db = self.mox.CreateMock(owners.Database) | 2126 fake_db = self.mox.CreateMock(owners.Database) |
| 2123 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) | 2127 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) |
| 2124 input_api.owners_db = fake_db | 2128 input_api.owners_db = fake_db |
| 2125 input_api.is_committing = True | 2129 input_api.is_committing = is_committing |
| 2126 input_api.tbr = tbr | 2130 input_api.tbr = tbr |
| 2127 | 2131 |
| 2128 if not tbr and issue: | 2132 if not is_committing or (not tbr and issue): |
| 2129 affected_file.LocalPath().AndReturn('foo.cc') | 2133 affected_file.LocalPath().AndReturn('foo/xyz.cc') |
| 2130 change.AffectedFiles(file_filter=None).AndReturn([affected_file]) | 2134 change.AffectedFiles(file_filter=None).AndReturn([affected_file]) |
| 2131 owner_email = 'john@example.com' | 2135 owner_email = 'john@example.com' |
| 2132 if not rietveld_response: | 2136 if not rietveld_response: |
| 2133 rietveld_response = { | 2137 rietveld_response = { |
| 2134 "owner_email": owner_email, | 2138 "owner_email": owner_email, |
| 2135 "messages": [ | 2139 "messages": [ |
| 2136 {"sender": a, "text": "I approve", "approval": True} | 2140 {"sender": a, "text": "I approve", "approval": True} |
| 2137 for a in approvers | 2141 for a in approvers |
| 2138 ], | 2142 ], |
| 2143 "reviewers": reviewers |
| 2139 } | 2144 } |
| 2140 input_api.rietveld.get_issue_properties( | 2145 |
| 2141 int(input_api.change.issue), True).AndReturn(rietveld_response) | 2146 if is_committing: |
| 2142 fake_db.files_not_covered_by(set(['foo.cc']), | 2147 people = approvers |
| 2143 approvers.union(set([owner_email]))).AndReturn(uncovered_files) | 2148 else: |
| 2149 people = reviewers |
| 2150 |
| 2151 if issue: |
| 2152 input_api.rietveld.get_issue_properties( |
| 2153 int(input_api.change.issue), True).AndReturn(rietveld_response) |
| 2154 people.add(owner_email) |
| 2155 |
| 2156 fake_db.directories_not_covered_by(set(['foo/xyz.cc']), |
| 2157 people).AndReturn(uncovered_dirs) |
| 2144 | 2158 |
| 2145 self.mox.ReplayAll() | 2159 self.mox.ReplayAll() |
| 2146 output = presubmit.PresubmitOutput() | 2160 output = presubmit.PresubmitOutput() |
| 2147 results = presubmit_canned_checks.CheckOwners(input_api, | 2161 results = presubmit_canned_checks.CheckOwners(input_api, |
| 2148 presubmit.OutputApi) | 2162 presubmit.OutputApi) |
| 2149 if results: | 2163 if results: |
| 2150 results[0].handle(output) | 2164 results[0].handle(output) |
| 2151 self.assertEquals(output.getvalue(), expected_output) | 2165 self.assertEquals(output.getvalue(), expected_output) |
| 2152 | 2166 |
| 2153 def testCannedCheckOwners_Approved(self): | 2167 def testCannedCheckOwners_Approved(self): |
| 2154 response = { | 2168 response = { |
| 2155 "owner_email": "john@example.com", | 2169 "owner_email": "john@example.com", |
| 2156 "messages": [ | 2170 "messages": [ |
| 2157 { | 2171 { |
| 2158 "sender": "ben@example.com", "text": "foo", "approval": True, | 2172 "sender": "ben@example.com", "text": "foo", "approval": True, |
| 2159 }, | 2173 }, |
| 2160 ], | 2174 ], |
| 2175 "reviewers": ["ben@example.com"], |
| 2161 } | 2176 } |
| 2162 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2177 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2163 rietveld_response=response, | 2178 rietveld_response=response, |
| 2164 expected_output='') | 2179 expected_output='') |
| 2165 | 2180 |
| 2181 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2182 is_committing=False, |
| 2183 rietveld_response=response, |
| 2184 expected_output='') |
| 2185 |
| 2166 def testCannedCheckOwners_NotApproved(self): | 2186 def testCannedCheckOwners_NotApproved(self): |
| 2167 response = { | 2187 response = { |
| 2168 "owner_email": "john@example.com", | 2188 "owner_email": "john@example.com", |
| 2169 "messages": [ | 2189 "messages": [ |
| 2170 { | 2190 { |
| 2171 "sender": "ben@example.com", "text": "foo", "approval": False, | 2191 "sender": "ben@example.com", "text": "foo", "approval": False, |
| 2172 }, | 2192 }, |
| 2173 ], | 2193 ], |
| 2194 "reviewers": ["ben@example.com"], |
| 2174 } | 2195 } |
| 2175 self.AssertOwnersWorks( | 2196 self.AssertOwnersWorks( |
| 2176 approvers=set(), | 2197 approvers=set(), |
| 2198 reviewers=set(["ben@example.com"]), |
| 2177 rietveld_response=response, | 2199 rietveld_response=response, |
| 2178 expected_output= | 2200 expected_output= |
| 2179 'Missing LGTM from someone other than john@example.com\n') | 2201 'Missing LGTM from someone other than john@example.com\n') |
| 2180 | 2202 |
| 2203 self.AssertOwnersWorks( |
| 2204 approvers=set(), |
| 2205 reviewers=set(["ben@example.com"]), |
| 2206 is_committing=False, |
| 2207 rietveld_response=response, |
| 2208 expected_output='') |
| 2209 |
| 2210 def testCannedCheckOwners_NoReviewers(self): |
| 2211 response = { |
| 2212 "owner_email": "john@example.com", |
| 2213 "messages": [ |
| 2214 { |
| 2215 "sender": "ben@example.com", "text": "foo", "approval": False, |
| 2216 }, |
| 2217 ], |
| 2218 "reviewers":[], |
| 2219 } |
| 2220 self.AssertOwnersWorks( |
| 2221 approvers=set(), |
| 2222 reviewers=set(), |
| 2223 rietveld_response=response, |
| 2224 expected_output= |
| 2225 'Missing LGTM from someone other than john@example.com\n') |
| 2226 |
| 2227 self.AssertOwnersWorks( |
| 2228 approvers=set(), |
| 2229 reviewers=set(), |
| 2230 is_committing=False, |
| 2231 rietveld_response=response, |
| 2232 expected_output='') |
| 2233 |
| 2181 def testCannedCheckOwners_NoIssue(self): | 2234 def testCannedCheckOwners_NoIssue(self): |
| 2182 self.AssertOwnersWorks(issue=None, | 2235 self.AssertOwnersWorks(issue=None, |
| 2183 expected_output="OWNERS check failed: this change has no Rietveld " | 2236 expected_output="OWNERS check failed: this change has no Rietveld " |
| 2184 "issue number, so we can't check it for approvals.\n") | 2237 "issue number, so we can't check it for approvals.\n") |
| 2185 | 2238 |
| 2239 self.AssertOwnersWorks(issue=None, is_committing=False, |
| 2240 expected_output="") |
| 2241 |
| 2186 def testCannedCheckOwners_NoLGTM(self): | 2242 def testCannedCheckOwners_NoLGTM(self): |
| 2187 self.AssertOwnersWorks(expected_output='Missing LGTM from someone ' | 2243 self.AssertOwnersWorks(expected_output='Missing LGTM from someone ' |
| 2188 'other than john@example.com\n') | 2244 'other than john@example.com\n') |
| 2245 self.AssertOwnersWorks(is_committing=False, expected_output='') |
| 2189 | 2246 |
| 2190 def testCannedCheckOwners_OnlyOwnerLGTM(self): | 2247 def testCannedCheckOwners_OnlyOwnerLGTM(self): |
| 2191 self.AssertOwnersWorks(approvers=set(['john@example.com']), | 2248 self.AssertOwnersWorks(approvers=set(['john@example.com']), |
| 2192 expected_output='Missing LGTM from someone ' | 2249 expected_output='Missing LGTM from someone ' |
| 2193 'other than john@example.com\n') | 2250 'other than john@example.com\n') |
| 2251 self.AssertOwnersWorks(approvers=set(['john@example.com']), |
| 2252 is_committing=False, |
| 2253 expected_output='') |
| 2194 | 2254 |
| 2195 def testCannedCheckOwners_TBR(self): | 2255 def testCannedCheckOwners_TBR(self): |
| 2196 self.AssertOwnersWorks(tbr=True, | 2256 self.AssertOwnersWorks(tbr=True, |
| 2197 expected_output='--tbr was specified, skipping OWNERS check\n') | 2257 expected_output='--tbr was specified, skipping OWNERS check\n') |
| 2198 | 2258 self.AssertOwnersWorks(tbr=True, is_committing=False, expected_output='') |
| 2199 def testCannedCheckOwners_Upload(self): | |
| 2200 class FakeInputAPI(object): | |
| 2201 is_committing = False | |
| 2202 | |
| 2203 results = presubmit_canned_checks.CheckOwners(FakeInputAPI(), | |
| 2204 presubmit.OutputApi) | |
| 2205 self.assertEqual(results, []) | |
| 2206 | 2259 |
| 2207 def testCannedCheckOwners_WithoutOwnerLGTM(self): | 2260 def testCannedCheckOwners_WithoutOwnerLGTM(self): |
| 2208 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']), | 2261 self.AssertOwnersWorks(uncovered_dirs=set(['foo']), |
| 2209 expected_output='Missing LGTM from an OWNER for: foo.cc\n') | 2262 expected_output='Missing LGTM from an OWNER for files in these ' |
| 2263 'directories:\n' |
| 2264 ' foo\n') |
| 2265 self.AssertOwnersWorks(uncovered_dirs=set(['foo']), |
| 2266 is_committing=False, |
| 2267 expected_output='Missing OWNER reviewers for files in these ' |
| 2268 'directories:\n' |
| 2269 ' foo\n') |
| 2210 | 2270 |
| 2211 def testCannedCheckOwners_WithLGTMs(self): | 2271 def testCannedCheckOwners_WithLGTMs(self): |
| 2212 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2272 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2213 uncovered_files=set()) | 2273 uncovered_dirs=set()) |
| 2274 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2275 is_committing=False, |
| 2276 uncovered_dirs=set()) |
| 2214 | 2277 |
| 2215 def testCannedRunUnitTests(self): | 2278 def testCannedRunUnitTests(self): |
| 2216 change = presubmit.Change( | 2279 change = presubmit.Change( |
| 2217 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) | 2280 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
| 2218 input_api = self.MockInputApi(change, False) | 2281 input_api = self.MockInputApi(change, False) |
| 2219 input_api.verbose = True | 2282 input_api.verbose = True |
| 2220 unit_tests = ['allo', 'bar.py'] | 2283 unit_tests = ['allo', 'bar.py'] |
| 2221 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2284 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2222 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2285 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2223 input_api.subprocess.check_call( | 2286 input_api.subprocess.check_call( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2292 affected_file.LocalPath().AndReturn('hello.py') | 2355 affected_file.LocalPath().AndReturn('hello.py') |
| 2293 | 2356 |
| 2294 self.mox.ReplayAll() | 2357 self.mox.ReplayAll() |
| 2295 results = presubmit_canned_checks.PanProjectChecks( | 2358 results = presubmit_canned_checks.PanProjectChecks( |
| 2296 input_api, | 2359 input_api, |
| 2297 presubmit.OutputApi, | 2360 presubmit.OutputApi, |
| 2298 excluded_paths=None, | 2361 excluded_paths=None, |
| 2299 text_files=None, | 2362 text_files=None, |
| 2300 license_header=None, | 2363 license_header=None, |
| 2301 project_name=None, | 2364 project_name=None, |
| 2302 owners_check=True) | 2365 owners_check=False) |
| 2303 self.assertEqual(1, len(results)) | 2366 self.assertEqual(1, len(results)) |
| 2304 self.assertEqual( | 2367 self.assertEqual( |
| 2305 'Found line ending with white spaces in:', results[0]._message) | 2368 'Found line ending with white spaces in:', results[0]._message) |
| 2306 self.checkstdout('') | 2369 self.checkstdout('') |
| 2307 | 2370 |
| 2308 | 2371 |
| 2309 if __name__ == '__main__': | 2372 if __name__ == '__main__': |
| 2310 import unittest | 2373 import unittest |
| 2311 unittest.main() | 2374 unittest.main() |
| OLD | NEW |