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(object): |
| 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 |
24 class PresubmitMock(object): | 37 class PresubmitMock(object): |
25 def __init__(self, *args, **kwargs): | 38 def __init__(self, *args, **kwargs): |
26 self.reviewers = [] | 39 self.reviewers = [] |
27 @staticmethod | 40 @staticmethod |
28 def should_continue(): | 41 def should_continue(): |
29 return True | 42 return True |
30 | 43 |
31 | 44 |
32 class RietveldMock(object): | 45 class RietveldMock(object): |
33 def __init__(self, *args, **kwargs): | 46 def __init__(self, *args, **kwargs): |
(...skipping 1344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1378 ] | 1391 ] |
1379 # TODO(tandrii): consider testing just set-commit and set-commit --clear, | 1392 # TODO(tandrii): consider testing just set-commit and set-commit --clear, |
1380 # but without copy-pasting tons of expectations, as modifying them later is | 1393 # but without copy-pasting tons of expectations, as modifying them later is |
1381 # super tedious. | 1394 # super tedious. |
1382 self.assertEqual(0, git_cl.main(['set-commit', '-d'])) | 1395 self.assertEqual(0, git_cl.main(['set-commit', '-d'])) |
1383 | 1396 |
1384 def test_description_display(self): | 1397 def test_description_display(self): |
1385 out = StringIO.StringIO() | 1398 out = StringIO.StringIO() |
1386 self.mock(git_cl.sys, 'stdout', out) | 1399 self.mock(git_cl.sys, 'stdout', out) |
1387 | 1400 |
1388 class MockChangelist(): | 1401 self.mock(git_cl, 'Changelist', ChangelistMock) |
1389 def __init__(self, **kwargs): | 1402 ChangelistMock.desc = 'foo\n' |
1390 pass | |
1391 def GetIssue(self): | |
1392 return 1 | |
1393 def GetDescription(self): | |
1394 return 'foo' | |
1395 | |
1396 self.mock(git_cl, 'Changelist', MockChangelist) | |
1397 | 1403 |
1398 self.assertEqual(0, git_cl.main(['description', '-d'])) | 1404 self.assertEqual(0, git_cl.main(['description', '-d'])) |
1399 self.assertEqual('foo\n', out.getvalue()) | 1405 self.assertEqual('foo\n', out.getvalue()) |
1400 | 1406 |
1401 def test_description_rietveld(self): | 1407 def test_description_rietveld(self): |
1402 out = StringIO.StringIO() | 1408 out = StringIO.StringIO() |
1403 self.mock(git_cl.sys, 'stdout', out) | 1409 self.mock(git_cl.sys, 'stdout', out) |
1404 self.mock(git_cl.Changelist, 'GetDescription', | 1410 self.mock(git_cl.Changelist, 'GetDescription', |
1405 lambda *args: 'foobar') | 1411 lambda *args: 'foobar') |
1406 | 1412 |
1407 self.calls = [ | 1413 self.calls = [ |
1408 ((['git', 'config', 'rietveld.autoupdate'],), ''), | 1414 ((['git', 'config', 'rietveld.autoupdate'],), ''), |
1409 ((['git', 'config', 'rietveld.server'],), ''), | 1415 ((['git', 'config', 'rietveld.server'],), ''), |
1410 ((['git', 'config', 'rietveld.server'],), ''), | 1416 ((['git', 'config', 'rietveld.server'],), ''), |
1411 ] | 1417 ] |
1412 self.assertEqual(0, git_cl.main([ | 1418 self.assertEqual(0, git_cl.main([ |
1413 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) | 1419 'description', 'https://code.review.org/123123', '-d', '--rietveld'])) |
1414 self.assertEqual('foobar\n', out.getvalue()) | 1420 self.assertEqual('foobar\n', out.getvalue()) |
1415 | 1421 |
1416 def test_description_gerrit(self): | 1422 def test_description_gerrit(self): |
1417 out = StringIO.StringIO() | 1423 out = StringIO.StringIO() |
1418 self.mock(git_cl.sys, 'stdout', out) | 1424 self.mock(git_cl.sys, 'stdout', out) |
1419 self.mock(git_cl.Changelist, 'GetDescription', | 1425 self.mock(git_cl.Changelist, 'GetDescription', |
1420 lambda *args: 'foobar') | 1426 lambda *args: 'foobar') |
1421 | 1427 |
1422 self.assertEqual(0, git_cl.main([ | 1428 self.assertEqual(0, git_cl.main([ |
1423 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) | 1429 'description', 'https://code.review.org/123123', '-d', '--gerrit'])) |
1424 self.assertEqual('foobar\n', out.getvalue()) | 1430 self.assertEqual('foobar\n', out.getvalue()) |
1425 | 1431 |
| 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 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hihi')) |
| 1438 |
| 1439 self.assertEqual(0, git_cl.main(['description', '-n', 'hihi'])) |
| 1440 self.assertEqual('hihi', ChangelistMock.desc) |
| 1441 |
| 1442 def test_description_set_stdin(self): |
| 1443 out = StringIO.StringIO() |
| 1444 self.mock(git_cl.sys, 'stdout', out) |
| 1445 |
| 1446 self.mock(git_cl, 'Changelist', ChangelistMock) |
| 1447 self.mock(git_cl.sys, 'stdin', StringIO.StringIO('hi \r\n\t there\n\nman')) |
| 1448 |
| 1449 self.assertEqual(0, git_cl.main(['description', '-n', '-'])) |
| 1450 self.assertEqual('hi\n\t there\n\nman', ChangelistMock.desc) |
| 1451 |
1426 | 1452 |
1427 if __name__ == '__main__': | 1453 if __name__ == '__main__': |
1428 git_cl.logging.basicConfig( | 1454 git_cl.logging.basicConfig( |
1429 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) | 1455 level=git_cl.logging.DEBUG if '-v' in sys.argv else git_cl.logging.ERROR) |
1430 unittest.main() | 1456 unittest.main() |
OLD | NEW |