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 os | 8 import os |
9 import StringIO | 9 import StringIO |
10 import stat | 10 import stat |
11 import sys | 11 import sys |
12 import unittest | 12 import unittest |
13 import urlparse | 13 import urlparse |
14 | 14 |
15 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 15 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
16 | 16 |
17 from testing_support.auto_stub import TestCase | 17 from testing_support.auto_stub import TestCase |
18 | 18 |
19 import git_cl | 19 import git_cl |
20 import git_common | 20 import git_common |
21 import git_footers | 21 import git_footers |
22 import subprocess2 | 22 import subprocess2 |
23 | 23 |
24 class ChangelistMock(): | |
25 # A class variable so we can access it when we don't have access to the | |
26 # instance that's being set. | |
27 desc = "" | |
28 def __init__(self, **kwargs): | |
29 pass | |
30 def GetIssue(self): | |
31 return 1 | |
32 def GetDescription(self): | |
33 return ChangelistMock.desc | |
34 def UpdateDescription(self, desc): | |
35 ChangelistMock.desc = desc | |
36 | |
37 class PresubmitMock(object): | 24 class PresubmitMock(object): |
38 def __init__(self, *args, **kwargs): | 25 def __init__(self, *args, **kwargs): |
39 self.reviewers = [] | 26 self.reviewers = [] |
40 @staticmethod | 27 @staticmethod |
41 def should_continue(): | 28 def should_continue(): |
42 return True | 29 return True |
43 | 30 |
44 | 31 |
45 class RietveldMock(object): | 32 class RietveldMock(object): |
46 def __init__(self, *args, **kwargs): | 33 def __init__(self, *args, **kwargs): |
(...skipping 1344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1391 ] | 1378 ] |
1392 # TODO(tandrii): consider testing just set-commit and set-commit --clear, | 1379 # TODO(tandrii): consider testing just set-commit and set-commit --clear, |
1393 # but without copy-pasting tons of expectations, as modifying them later is | 1380 # but without copy-pasting tons of expectations, as modifying them later is |
1394 # super tedious. | 1381 # super tedious. |
1395 self.assertEqual(0, git_cl.main(['set-commit', '-d'])) | 1382 self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
1396 | 1383 |
1397 def test_description_display(self): | 1384 def test_description_display(self): |
1398 out = StringIO.StringIO() | 1385 out = StringIO.StringIO() |
1399 self.mock(git_cl.sys, 'stdout', out) | 1386 self.mock(git_cl.sys, 'stdout', out) |
1400 | 1387 |
1401 self.mock(git_cl, 'Changelist', ChangelistMock) | 1388 class MockChangelist(): |
1402 ChangelistMock.desc = 'foo\n' | 1389 def __init__(self, **kwargs): |
| 1390 pass |
| 1391 def GetIssue(self): |
| 1392 return 1 |
| 1393 def GetDescription(self): |
| 1394 return 'foo' |
| 1395 |
| 1396 self.mock(git_cl, 'Changelist', MockChangelist) |
1403 | 1397 |
1404 self.assertEqual(0, git_cl.main(['description', '-d'])) | 1398 self.assertEqual(0, git_cl.main(['description', '-d'])) |
1405 self.assertEqual('foo\n', out.getvalue()) | 1399 self.assertEqual('foo\n', out.getvalue()) |
1406 | 1400 |
1407 def test_description_rietveld(self): | 1401 def test_description_rietveld(self): |
1408 out = StringIO.StringIO() | 1402 out = StringIO.StringIO() |
1409 self.mock(git_cl.sys, 'stdout', out) | 1403 self.mock(git_cl.sys, 'stdout', out) |
1410 self.mock(git_cl.Changelist, 'GetDescription', | 1404 self.mock(git_cl.Changelist, 'GetDescription', |
1411 lambda *args: 'foobar') | 1405 lambda *args: 'foobar') |
1412 | 1406 |
1413 self.calls = [ | 1407 self.calls = [ |
1414 ((['git', 'config', 'rietveld.autoupdate'],), ''), | 1408 ((['git', 'config', 'rietveld.autoupdate'],), ''), |
1415 ((['git', 'config', 'rietveld.server'],), ''), | 1409 ((['git', 'config', 'rietveld.server'],), ''), |
1416 ((['git', 'config', 'rietveld.server'],), ''), | 1410 ((['git', 'config', 'rietveld.server'],), ''), |
1417 ] | 1411 ] |
1418 self.assertEqual(0, git_cl.main([ | 1412 self.assertEqual(0, git_cl.main([ |
1419 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) | 1413 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) |
1420 self.assertEqual('foobar\n', out.getvalue()) | 1414 self.assertEqual('foobar\n', out.getvalue()) |
1421 | 1415 |
1422 def test_description_gerrit(self): | 1416 def test_description_gerrit(self): |
1423 out = StringIO.StringIO() | 1417 out = StringIO.StringIO() |
1424 self.mock(git_cl.sys, 'stdout', out) | 1418 self.mock(git_cl.sys, 'stdout', out) |
1425 self.mock(git_cl.Changelist, 'GetDescription', | 1419 self.mock(git_cl.Changelist, 'GetDescription', |
1426 lambda *args: 'foobar') | 1420 lambda *args: 'foobar') |
1427 | 1421 |
1428 self.assertEqual(0, git_cl.main([ | 1422 self.assertEqual(0, git_cl.main([ |
1429 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) | 1423 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) |
1430 self.assertEqual('foobar\n', out.getvalue()) | 1424 self.assertEqual('foobar\n', out.getvalue()) |
1431 | 1425 |
1432 def test_description_set_raw(self): | |
1433 out = StringIO.StringIO() | |
1434 self.mock(git_cl.sys, 'stdout', out) | |
1435 | |
1436 self.mock(git_cl, 'Changelist', ChangelistMock) | |
1437 class TMP(): | |
1438 def splitlines(self): | |
1439 return ['hihi'] | |
1440 | |
1441 self.mock(git_cl.sys, 'stdin', TMP()) | |
1442 | |
1443 self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) | |
1444 self.assertEqual('hihi', ChangelistMock.desc) | |
1445 | |
1446 def test_description_set_stdin(self): | |
1447 out = StringIO.StringIO() | |
1448 self.mock(git_cl.sys, 'stdout', out) | |
1449 | |
1450 self.mock(git_cl, 'Changelist', ChangelistMock) | |
1451 class TMP(): | |
1452 def splitlines(self): | |
1453 return ['hi', 'there'] | |
1454 | |
1455 self.mock(git_cl.sys, 'stdin', TMP()) | |
1456 | |
1457 self.assertEqual(0, git_cl.main(['description', '-n', '-'])) | |
1458 self.assertEqual('hi\nthere', ChangelistMock.desc) | |
1459 | |
1460 | 1426 |
1461 if __name__ == '__main__': | 1427 if __name__ == '__main__': |
1462 git_cl.logging.basicConfig( | 1428 git_cl.logging.basicConfig( |
1463 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1429 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
1464 unittest.main() | 1430 unittest.main() |
OLD | NEW |