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 gclient_scm.py.""" | 6 """Unit tests for gclient_scm.py.""" |
7 | 7 |
8 # pylint: disable=E1103 | 8 # pylint: disable=E1103 |
9 | 9 |
10 # Import before super_mox to keep valid references. | 10 # Import before super_mox to keep valid references. |
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 def _GetAskForDataCallback(self, expected_prompt, return_value): | 884 def _GetAskForDataCallback(self, expected_prompt, return_value): |
885 def AskForData(prompt, options): | 885 def AskForData(prompt, options): |
886 self.assertEquals(prompt, expected_prompt) | 886 self.assertEquals(prompt, expected_prompt) |
887 return return_value | 887 return return_value |
888 return AskForData | 888 return AskForData |
889 | 889 |
890 def setUp(self): | 890 def setUp(self): |
891 TestCaseUtils.setUp(self) | 891 TestCaseUtils.setUp(self) |
892 unittest.TestCase.setUp(self) | 892 unittest.TestCase.setUp(self) |
893 self.url = 'git://foo' | 893 self.url = 'git://foo' |
894 self.root_dir = tempfile.mkdtemp() | 894 # The .git suffix allows gclient_scm to recognize the dir as a git repo |
| 895 # when cloning it locally |
| 896 self.root_dir = tempfile.mkdtemp('.git') |
895 self.relpath = '.' | 897 self.relpath = '.' |
896 self.base_path = join(self.root_dir, self.relpath) | 898 self.base_path = join(self.root_dir, self.relpath) |
897 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) | 899 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
898 StdoutCheck.setUp(self) | 900 StdoutCheck.setUp(self) |
899 self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists | 901 self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists |
900 self._original_SVNBinaryExists = gclient_scm.SVNWrapper.BinaryExists | 902 self._original_SVNBinaryExists = gclient_scm.SVNWrapper.BinaryExists |
901 gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) | 903 gclient_scm.GitWrapper.BinaryExists = staticmethod(lambda : True) |
902 gclient_scm.SVNWrapper.BinaryExists = staticmethod(lambda : True) | 904 gclient_scm.SVNWrapper.BinaryExists = staticmethod(lambda : True) |
903 | 905 |
904 def tearDown(self): | 906 def tearDown(self): |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1370 ).AndReturn('') | 1372 ).AndReturn('') |
1371 | 1373 |
1372 self.mox.ReplayAll() | 1374 self.mox.ReplayAll() |
1373 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | 1375 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
1374 relpath=self.relpath) | 1376 relpath=self.relpath) |
1375 scm.update(options, None, []) | 1377 scm.update(options, None, []) |
1376 self.checkstdout('\n') | 1378 self.checkstdout('\n') |
1377 | 1379 |
1378 | 1380 |
1379 class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): | 1381 class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase): |
| 1382 def checkInStdout(self, expected): |
| 1383 value = sys.stdout.getvalue() |
| 1384 sys.stdout.close() |
| 1385 # pylint: disable=E1101 |
| 1386 self.assertIn(expected, value) |
| 1387 |
| 1388 def checkNotInStdout(self, expected): |
| 1389 value = sys.stdout.getvalue() |
| 1390 sys.stdout.close() |
| 1391 # pylint: disable=E1101 |
| 1392 self.assertNotIn(expected, value) |
| 1393 |
| 1394 def getCurrentBranch(self): |
| 1395 # Returns name of current branch or HEAD for detached HEAD |
| 1396 branch = gclient_scm.scm.GIT.Capture(['rev-parse', '--abbrev-ref', 'HEAD'], |
| 1397 cwd=self.base_path) |
| 1398 if branch == 'HEAD': |
| 1399 return None |
| 1400 return branch |
| 1401 |
| 1402 def testUpdateClone(self): |
| 1403 if not self.enabled: |
| 1404 return |
| 1405 options = self.Options() |
| 1406 |
| 1407 origin_root_dir = self.root_dir |
| 1408 self.root_dir = tempfile.mkdtemp() |
| 1409 self.relpath = '.' |
| 1410 self.base_path = join(self.root_dir, self.relpath) |
| 1411 |
| 1412 scm = gclient_scm.CreateSCM(url=origin_root_dir, |
| 1413 root_dir=self.root_dir, |
| 1414 relpath=self.relpath) |
| 1415 |
| 1416 expected_file_list = [join(self.base_path, "a"), |
| 1417 join(self.base_path, "b")] |
| 1418 file_list = [] |
| 1419 options.revision = 'unmanaged' |
| 1420 scm.update(options, (), file_list) |
| 1421 |
| 1422 self.assertEquals(file_list, expected_file_list) |
| 1423 self.assertEquals(scm.revinfo(options, (), None), |
| 1424 '069c602044c5388d2d15c3f875b057c852003458') |
| 1425 # indicates detached HEAD |
| 1426 self.assertEquals(self.getCurrentBranch(), None) |
| 1427 self.checkInStdout( |
| 1428 'Checked out refs/remotes/origin/master to a detached HEAD') |
| 1429 |
| 1430 rmtree(origin_root_dir) |
| 1431 |
| 1432 def testUpdateCloneOnCommit(self): |
| 1433 if not self.enabled: |
| 1434 return |
| 1435 options = self.Options() |
| 1436 |
| 1437 origin_root_dir = self.root_dir |
| 1438 self.root_dir = tempfile.mkdtemp() |
| 1439 self.relpath = '.' |
| 1440 self.base_path = join(self.root_dir, self.relpath) |
| 1441 url_with_commit_ref = origin_root_dir +\ |
| 1442 '@a7142dc9f0009350b96a11f372b6ea658592aa95' |
| 1443 |
| 1444 scm = gclient_scm.CreateSCM(url=url_with_commit_ref, |
| 1445 root_dir=self.root_dir, |
| 1446 relpath=self.relpath) |
| 1447 |
| 1448 expected_file_list = [join(self.base_path, "a"), |
| 1449 join(self.base_path, "b")] |
| 1450 file_list = [] |
| 1451 options.revision = 'unmanaged' |
| 1452 scm.update(options, (), file_list) |
| 1453 |
| 1454 self.assertEquals(file_list, expected_file_list) |
| 1455 self.assertEquals(scm.revinfo(options, (), None), |
| 1456 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 1457 # indicates detached HEAD |
| 1458 self.assertEquals(self.getCurrentBranch(), None) |
| 1459 self.checkInStdout( |
| 1460 'Checked out a7142dc9f0009350b96a11f372b6ea658592aa95 to a detached HEAD') |
| 1461 |
| 1462 rmtree(origin_root_dir) |
| 1463 |
| 1464 def testUpdateCloneOnBranch(self): |
| 1465 if not self.enabled: |
| 1466 return |
| 1467 options = self.Options() |
| 1468 |
| 1469 origin_root_dir = self.root_dir |
| 1470 self.root_dir = tempfile.mkdtemp() |
| 1471 self.relpath = '.' |
| 1472 self.base_path = join(self.root_dir, self.relpath) |
| 1473 url_with_branch_ref = origin_root_dir + '@feature' |
| 1474 |
| 1475 scm = gclient_scm.CreateSCM(url=url_with_branch_ref, |
| 1476 root_dir=self.root_dir, |
| 1477 relpath=self.relpath) |
| 1478 |
| 1479 expected_file_list = [join(self.base_path, "a"), |
| 1480 join(self.base_path, "b"), |
| 1481 join(self.base_path, "c")] |
| 1482 file_list = [] |
| 1483 options.revision = 'unmanaged' |
| 1484 scm.update(options, (), file_list) |
| 1485 |
| 1486 self.assertEquals(file_list, expected_file_list) |
| 1487 self.assertEquals(scm.revinfo(options, (), None), |
| 1488 '9a51244740b25fa2ded5252ca00a3178d3f665a9') |
| 1489 self.assertEquals(self.getCurrentBranch(), 'feature') |
| 1490 self.checkNotInStdout('Checked out feature to a detached HEAD') |
| 1491 |
| 1492 rmtree(origin_root_dir) |
| 1493 |
| 1494 def testUpdateCloneOnDetachedBranch(self): |
| 1495 if not self.enabled: |
| 1496 return |
| 1497 options = self.Options() |
| 1498 |
| 1499 origin_root_dir = self.root_dir |
| 1500 self.root_dir = tempfile.mkdtemp() |
| 1501 self.relpath = '.' |
| 1502 self.base_path = join(self.root_dir, self.relpath) |
| 1503 url_with_branch_ref = origin_root_dir + '@refs/remotes/origin/feature' |
| 1504 |
| 1505 scm = gclient_scm.CreateSCM(url=url_with_branch_ref, |
| 1506 root_dir=self.root_dir, |
| 1507 relpath=self.relpath) |
| 1508 |
| 1509 expected_file_list = [join(self.base_path, "a"), |
| 1510 join(self.base_path, "b"), |
| 1511 join(self.base_path, "c")] |
| 1512 file_list = [] |
| 1513 options.revision = 'unmanaged' |
| 1514 scm.update(options, (), file_list) |
| 1515 |
| 1516 self.assertEquals(file_list, expected_file_list) |
| 1517 self.assertEquals(scm.revinfo(options, (), None), |
| 1518 '9a51244740b25fa2ded5252ca00a3178d3f665a9') |
| 1519 # indicates detached HEAD |
| 1520 self.assertEquals(self.getCurrentBranch(), None) |
| 1521 self.checkInStdout( |
| 1522 'Checked out refs/remotes/origin/feature to a detached HEAD') |
| 1523 |
| 1524 rmtree(origin_root_dir) |
| 1525 |
| 1526 def testUpdateCloneOnBranchHead(self): |
| 1527 if not self.enabled: |
| 1528 return |
| 1529 options = self.Options() |
| 1530 |
| 1531 origin_root_dir = self.root_dir |
| 1532 self.root_dir = tempfile.mkdtemp() |
| 1533 self.relpath = '.' |
| 1534 self.base_path = join(self.root_dir, self.relpath) |
| 1535 url_with_branch_ref = origin_root_dir + '@refs/heads/feature' |
| 1536 |
| 1537 scm = gclient_scm.CreateSCM(url=url_with_branch_ref, |
| 1538 root_dir=self.root_dir, |
| 1539 relpath=self.relpath) |
| 1540 |
| 1541 expected_file_list = [join(self.base_path, "a"), |
| 1542 join(self.base_path, "b"), |
| 1543 join(self.base_path, "c")] |
| 1544 file_list = [] |
| 1545 options.revision = 'unmanaged' |
| 1546 scm.update(options, (), file_list) |
| 1547 |
| 1548 self.assertEquals(file_list, expected_file_list) |
| 1549 self.assertEquals(scm.revinfo(options, (), None), |
| 1550 '9a51244740b25fa2ded5252ca00a3178d3f665a9') |
| 1551 self.assertEquals(self.getCurrentBranch(), 'feature') |
| 1552 self.checkNotInStdout( |
| 1553 'Checked out refs/heads/feature to a detached HEAD') |
| 1554 |
| 1555 rmtree(origin_root_dir) |
| 1556 |
1380 def testUpdateUpdate(self): | 1557 def testUpdateUpdate(self): |
1381 if not self.enabled: | 1558 if not self.enabled: |
1382 return | 1559 return |
1383 options = self.Options() | 1560 options = self.Options() |
1384 expected_file_list = [] | 1561 expected_file_list = [] |
1385 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 1562 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
1386 relpath=self.relpath) | 1563 relpath=self.relpath) |
1387 file_list = [] | 1564 file_list = [] |
1388 options.revision = 'unmanaged' | 1565 options.revision = 'unmanaged' |
1389 scm.update(options, (), file_list) | 1566 scm.update(options, (), file_list) |
1390 self.assertEquals(file_list, expected_file_list) | 1567 self.assertEquals(file_list, expected_file_list) |
1391 self.assertEquals(scm.revinfo(options, (), None), | 1568 self.assertEquals(scm.revinfo(options, (), None), |
1392 '069c602044c5388d2d15c3f875b057c852003458') | 1569 '069c602044c5388d2d15c3f875b057c852003458') |
1393 self.checkstdout('________ unmanaged solution; skipping .\n') | 1570 self.checkstdout('________ unmanaged solution; skipping .\n') |
1394 | 1571 |
1395 | 1572 |
1396 if __name__ == '__main__': | 1573 if __name__ == '__main__': |
1397 if '-v' in sys.argv: | 1574 if '-v' in sys.argv: |
1398 logging.basicConfig( | 1575 logging.basicConfig( |
1399 level=logging.DEBUG, | 1576 level=logging.DEBUG, |
1400 format='%(asctime).19s %(levelname)s %(filename)s:' | 1577 format='%(asctime).19s %(levelname)s %(filename)s:' |
1401 '%(lineno)s %(message)s') | 1578 '%(lineno)s %(message)s') |
1402 unittest.main() | 1579 unittest.main() |
1403 | 1580 |
1404 # vim: ts=2:sw=2:tw=80:et: | 1581 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |