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 git_cl.py.""" | 6 """Unit tests for git_cl.py.""" |
7 | 7 |
8 import json | 8 import json |
9 import os | 9 import os |
10 import StringIO | 10 import StringIO |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 'approval': True, | 67 'approval': True, |
68 'sender': 'john@chromium.org', | 68 'sender': 'john@chromium.org', |
69 }, | 69 }, |
70 ], | 70 ], |
71 } | 71 } |
72 | 72 |
73 @staticmethod | 73 @staticmethod |
74 def close_issue(_issue): | 74 def close_issue(_issue): |
75 return 'Closed' | 75 return 'Closed' |
76 | 76 |
| 77 @staticmethod |
| 78 def get_patch(issue, patchset): |
| 79 return 'patch set from issue %s patchset %s' % (issue, patchset) |
| 80 |
| 81 |
| 82 class GitCheckoutMock(object): |
| 83 def __init__(self, *args, **kwargs): |
| 84 pass |
| 85 |
| 86 @staticmethod |
| 87 def reset(): |
| 88 GitCheckoutMock.conflict = False |
| 89 |
| 90 def apply_patch(self, p): |
| 91 if GitCheckoutMock.conflict: |
| 92 raise Exception('failed') |
| 93 |
77 | 94 |
78 class WatchlistsMock(object): | 95 class WatchlistsMock(object): |
79 def __init__(self, _): | 96 def __init__(self, _): |
80 pass | 97 pass |
81 @staticmethod | 98 @staticmethod |
82 def GetWatchersForPaths(_): | 99 def GetWatchersForPaths(_): |
83 return ['joe@example.com'] | 100 return ['joe@example.com'] |
84 | 101 |
85 | 102 |
86 class CodereviewSettingsFileMock(object): | 103 class CodereviewSettingsFileMock(object): |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 if fail: | 166 if fail: |
150 self.assertIsNone(result) | 167 self.assertIsNone(result) |
151 return None | 168 return None |
152 self.assertIsNotNone(result) | 169 self.assertIsNotNone(result) |
153 self.assertEqual(result.issue, issue) | 170 self.assertEqual(result.issue, issue) |
154 self.assertEqual(result.patchset, patchset) | 171 self.assertEqual(result.patchset, patchset) |
155 self.assertEqual(result.hostname, hostname) | 172 self.assertEqual(result.hostname, hostname) |
156 return result | 173 return result |
157 | 174 |
158 def test_ParseIssueURL_rietveld(self): | 175 def test_ParseIssueURL_rietveld(self): |
159 def test(url, issue=None, patchset=None, hostname=None, patch_url=None, | 176 def test(url, issue=None, patchset=None, hostname=None, fail=None): |
160 fail=None): | 177 self._test_ParseIssueUrl( |
161 result = self._test_ParseIssueUrl( | |
162 git_cl._RietveldChangelistImpl.ParseIssueURL, | 178 git_cl._RietveldChangelistImpl.ParseIssueURL, |
163 url, issue, patchset, hostname, fail) | 179 url, issue, patchset, hostname, fail) |
164 if not fail: | |
165 self.assertEqual(result.patch_url, patch_url) | |
166 | 180 |
167 test('http://codereview.chromium.org/123', | 181 test('http://codereview.chromium.org/123', |
168 123, None, 'codereview.chromium.org') | 182 123, None, 'codereview.chromium.org') |
169 test('https://codereview.chromium.org/123', | 183 test('https://codereview.chromium.org/123', |
170 123, None, 'codereview.chromium.org') | 184 123, None, 'codereview.chromium.org') |
171 test('https://codereview.chromium.org/123/', | 185 test('https://codereview.chromium.org/123/', |
172 123, None, 'codereview.chromium.org') | 186 123, None, 'codereview.chromium.org') |
173 test('https://codereview.chromium.org/123/whatever', | 187 test('https://codereview.chromium.org/123/whatever', |
174 123, None, 'codereview.chromium.org') | 188 123, None, 'codereview.chromium.org') |
175 test('https://codereview.chromium.org/123/#ps20001', | 189 test('https://codereview.chromium.org/123/#ps20001', |
176 123, 20001, 'codereview.chromium.org') | 190 123, 20001, 'codereview.chromium.org') |
177 test('http://codereview.chromium.org/download/issue123_4.diff', | 191 test('http://codereview.chromium.org/download/issue123_4.diff', |
178 123, 4, 'codereview.chromium.org', | 192 123, 4, 'codereview.chromium.org') |
179 patch_url='https://codereview.chromium.org/download/issue123_4.diff') | |
180 # This looks like bad Gerrit, but is actually valid Rietveld. | 193 # This looks like bad Gerrit, but is actually valid Rietveld. |
181 test('https://chrome-review.source.com/123/4/', | 194 test('https://chrome-review.source.com/123/4/', |
182 123, None, 'chrome-review.source.com') | 195 123, None, 'chrome-review.source.com') |
183 | 196 |
184 test('https://codereview.chromium.org/deadbeaf', fail=True) | 197 test('https://codereview.chromium.org/deadbeaf', fail=True) |
185 test('https://codereview.chromium.org/api/123', fail=True) | 198 test('https://codereview.chromium.org/api/123', fail=True) |
186 test('bad://codereview.chromium.org/123', fail=True) | 199 test('bad://codereview.chromium.org/123', fail=True) |
187 test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True) | 200 test('http://codereview.chromium.org/download/issue123_4.diffff', fail=True) |
188 | 201 |
189 def test_ParseIssueURL_gerrit(self): | 202 def test_ParseIssueURL_gerrit(self): |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 lambda *a: ( | 277 lambda *a: ( |
265 self._mocked_call(['get_or_create_merge_base']+list(a)))) | 278 self._mocked_call(['get_or_create_merge_base']+list(a)))) |
266 self.mock(git_cl, 'BranchExists', lambda _: True) | 279 self.mock(git_cl, 'BranchExists', lambda _: True) |
267 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') | 280 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
268 self.mock(git_cl, 'ask_for_data', self._mocked_call) | 281 self.mock(git_cl, 'ask_for_data', self._mocked_call) |
269 self.mock(git_cl, 'write_json', lambda path, contents: | 282 self.mock(git_cl, 'write_json', lambda path, contents: |
270 self._mocked_call('write_json', path, contents)) | 283 self._mocked_call('write_json', path, contents)) |
271 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) | 284 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
272 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) | 285 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
273 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) | 286 self.mock(git_cl.rietveld, 'CachingRietveld', RietveldMock) |
| 287 self.mock(git_cl.checkout, 'GitCheckout', GitCheckoutMock) |
| 288 GitCheckoutMock.reset() |
274 self.mock(git_cl.upload, 'RealMain', self.fail) | 289 self.mock(git_cl.upload, 'RealMain', self.fail) |
275 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) | 290 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
276 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) | 291 self.mock(git_cl.auth, 'get_authenticator_for_host', AuthenticatorMock) |
277 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', | 292 self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce', |
278 classmethod(lambda _: False)) | 293 classmethod(lambda _: False)) |
279 self.mock(git_cl, 'DieWithError', | 294 self.mock(git_cl, 'DieWithError', |
280 lambda msg: self._mocked_call(['DieWithError', msg])) | 295 lambda msg: self._mocked_call(['DieWithError', msg])) |
281 # It's important to reset settings to not have inter-tests interference. | 296 # It's important to reset settings to not have inter-tests interference. |
282 git_cl.settings = None | 297 git_cl.settings = None |
283 | 298 |
(...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1281 def test_diff_when_dirty(self): | 1296 def test_diff_when_dirty(self): |
1282 # Do 'git cl diff' when local tree is dirty | 1297 # Do 'git cl diff' when local tree is dirty |
1283 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) | 1298 self.mock(git_common, 'is_dirty_git_tree', lambda x: True) |
1284 self.assertNotEqual(git_cl.main(['diff']), 0) | 1299 self.assertNotEqual(git_cl.main(['diff']), 0) |
1285 | 1300 |
1286 def _patch_common(self, is_gerrit=False, force_codereview=False, | 1301 def _patch_common(self, is_gerrit=False, force_codereview=False, |
1287 new_branch=False): | 1302 new_branch=False): |
1288 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) | 1303 self.mock(git_cl.sys, 'stdout', StringIO.StringIO()) |
1289 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', | 1304 self.mock(git_cl._RietveldChangelistImpl, 'GetMostRecentPatchset', |
1290 lambda x: '60001') | 1305 lambda x: '60001') |
1291 self.mock(git_cl._RietveldChangelistImpl, 'GetPatchSetDiff', | |
1292 lambda *args: None) | |
1293 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', | 1306 self.mock(git_cl._GerritChangelistImpl, '_GetChangeDetail', |
1294 lambda *args: { | 1307 lambda *args: { |
1295 'current_revision': '7777777777', | 1308 'current_revision': '7777777777', |
1296 'revisions': { | 1309 'revisions': { |
1297 '1111111111': { | 1310 '1111111111': { |
1298 '_number': 1, | 1311 '_number': 1, |
1299 'fetch': {'http': { | 1312 'fetch': {'http': { |
1300 'url': 'https://chromium.googlesource.com/my/repo', | 1313 'url': 'https://chromium.googlesource.com/my/repo', |
1301 'ref': 'refs/changes/56/123456/1', | 1314 'ref': 'refs/changes/56/123456/1', |
1302 }}, | 1315 }}, |
(...skipping 26 matching lines...) Expand all Loading... |
1329 | 1342 |
1330 if is_gerrit: | 1343 if is_gerrit: |
1331 if not force_codereview: | 1344 if not force_codereview: |
1332 self.calls += [ | 1345 self.calls += [ |
1333 ((['git', 'config', 'gerrit.host'],), 'true'), | 1346 ((['git', 'config', 'gerrit.host'],), 'true'), |
1334 ] | 1347 ] |
1335 else: | 1348 else: |
1336 self.calls += [ | 1349 self.calls += [ |
1337 ((['git', 'config', 'gerrit.host'],), CERR1), | 1350 ((['git', 'config', 'gerrit.host'],), CERR1), |
1338 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), | 1351 ((['git', 'config', 'rietveld.server'],), 'codereview.example.com'), |
| 1352 ((['git', 'config', 'branch.master.rietveldserver',],), CERR1), |
1339 ((['git', 'rev-parse', '--show-cdup'],), ''), | 1353 ((['git', 'rev-parse', '--show-cdup'],), ''), |
1340 ((['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],), ''), | |
1341 ] | 1354 ] |
1342 | 1355 |
1343 def _common_patch_successful(self, new_branch=False): | 1356 def _common_patch_successful(self, new_branch=False): |
1344 self._patch_common(new_branch=new_branch) | 1357 self._patch_common(new_branch=new_branch) |
1345 self.calls += [ | 1358 self.calls += [ |
1346 ((['git', 'apply', '--index', '-p0', '--3way'],), ''), | |
1347 ((['git', 'commit', '-m', | 1359 ((['git', 'commit', '-m', |
1348 'Description\n\n' + | 1360 'Description\n\n' + |
1349 'patch from issue 123456 at patchset 60001 ' + | 1361 'patch from issue 123456 at patchset 60001 ' + |
1350 '(http://crrev.com/123456#ps60001)'],), ''), | 1362 '(http://crrev.com/123456#ps60001)'],), ''), |
1351 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), | 1363 ((['git', 'config', 'branch.master.rietveldissue', '123456'],), |
1352 ''), | 1364 ''), |
1353 ((['git', 'config', 'branch.master.rietveldserver'],), CERR1), | |
1354 ((['git', 'config', 'branch.master.rietveldserver', | 1365 ((['git', 'config', 'branch.master.rietveldserver', |
1355 'https://codereview.example.com'],), ''), | 1366 'https://codereview.example.com'],), ''), |
1356 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), | 1367 ((['git', 'config', 'branch.master.rietveldpatchset', '60001'],), |
1357 ''), | 1368 ''), |
1358 ] | 1369 ] |
1359 | 1370 |
1360 def test_patch_successful(self): | 1371 def test_patch_successful(self): |
1361 self._common_patch_successful() | 1372 self._common_patch_successful() |
1362 self.assertEqual(git_cl.main(['patch', '123456']), 0) | 1373 self.assertEqual(git_cl.main(['patch', '123456']), 0) |
1363 | 1374 |
1364 def test_patch_successful_new_branch(self): | 1375 def test_patch_successful_new_branch(self): |
1365 self._common_patch_successful(new_branch=True) | 1376 self._common_patch_successful(new_branch=True) |
1366 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) | 1377 self.assertEqual(git_cl.main(['patch', '-b', 'master', '123456']), 0) |
1367 | 1378 |
1368 def test_patch_conflict(self): | 1379 def test_patch_conflict(self): |
1369 self._patch_common() | 1380 self._patch_common() |
1370 self.calls += [ | 1381 GitCheckoutMock.conflict = True |
1371 ((['git', 'apply', '--index', '-p0', '--3way'],), CERR1), | |
1372 ] | |
1373 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) | 1382 self.assertNotEqual(git_cl.main(['patch', '123456']), 0) |
1374 | 1383 |
1375 def test_gerrit_patch_successful(self): | 1384 def test_gerrit_patch_successful(self): |
1376 self._patch_common(is_gerrit=True) | 1385 self._patch_common(is_gerrit=True) |
1377 self.calls += [ | 1386 self.calls += [ |
1378 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', | 1387 ((['git', 'fetch', 'https://chromium.googlesource.com/my/repo', |
1379 'refs/changes/56/123456/7'],), ''), | 1388 'refs/changes/56/123456/7'],), ''), |
1380 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), | 1389 ((['git', 'cherry-pick', 'FETCH_HEAD'],), ''), |
1381 ((['git', 'config', 'branch.master.gerritissue', '123456'],), | 1390 ((['git', 'config', 'branch.master.gerritissue', '123456'],), |
1382 ''), | 1391 ''), |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2197 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') | 2206 self.assertNotRegexpMatches(sys.stdout.getvalue(), 'Warning') |
2198 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') | 2207 self.assertRegexpMatches(sys.stdout.getvalue(), '^Failures:') |
2199 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') | 2208 self.assertRegexpMatches(sys.stdout.getvalue(), 'Started:') |
2200 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') | 2209 self.assertRegexpMatches(sys.stdout.getvalue(), '2 try jobs') |
2201 | 2210 |
2202 | 2211 |
2203 if __name__ == '__main__': | 2212 if __name__ == '__main__': |
2204 git_cl.logging.basicConfig( | 2213 git_cl.logging.basicConfig( |
2205 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 2214 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
2206 unittest.main() | 2215 unittest.main() |
OLD | NEW |