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 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 self.url = 'git://foo' | 593 self.url = 'git://foo' |
594 self.root_dir = '/tmp' if sys.platform != 'win32' else 't:\\tmp' | 594 self.root_dir = '/tmp' if sys.platform != 'win32' else 't:\\tmp' |
595 self.relpath = 'fake' | 595 self.relpath = 'fake' |
596 self.base_path = os.path.join(self.root_dir, self.relpath) | 596 self.base_path = os.path.join(self.root_dir, self.relpath) |
597 self.backup_base_path = os.path.join(self.root_dir, | 597 self.backup_base_path = os.path.join(self.root_dir, |
598 'old_%s.git' % self.relpath) | 598 'old_%s.git' % self.relpath) |
599 | 599 |
600 def tearDown(self): | 600 def tearDown(self): |
601 BaseTestCase.tearDown(self) | 601 BaseTestCase.tearDown(self) |
602 | 602 |
603 def testGetUsableRevGit(self): | |
604 # pylint: disable=E1101 | |
605 options = self.Options(verbose=True) | |
606 | |
607 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) | |
608 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 | |
609 ).AndReturn(True) | |
610 | |
611 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True) | |
612 gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes( | |
613 ).AndReturn(False) | |
614 | |
615 gclient_scm.scm.os.path.isdir(self.base_path).AndReturn(True) | |
616 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) | |
617 | |
618 self.mox.ReplayAll() | |
619 | |
620 git_scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | |
621 relpath=self.relpath) | |
622 # A [fake] git sha1 with a git repo should work (this is in the case that | |
623 # the LKGR gets flipped to git sha1's some day). | |
624 self.assertEquals(git_scm.GetUsableRev(self.fake_hash_1, options), | |
625 self.fake_hash_1) | |
626 # An SVN rev with an existing purely git repo should raise an exception. | |
627 self.assertRaises(gclient_scm.gclient_utils.Error, | |
628 git_scm.GetUsableRev, '1', options) | |
629 | |
630 def testGetUsableRevGitSvn(self): | |
631 # pylint: disable=E1101 | |
632 options = self.Options() | |
633 too_big = str(1e7) | |
634 | |
635 # Pretend like the git-svn repo's HEAD is at r2. | |
636 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'GetGitSvnHeadRev', True) | |
637 gclient_scm.scm.GIT.GetGitSvnHeadRev(cwd=self.base_path).MultipleTimes( | |
638 ).AndReturn(2) | |
639 | |
640 self.mox.StubOutWithMock( | |
641 gclient_scm.scm.GIT, 'GetBlessedSha1ForSvnRev', True) | |
642 # r1 -> first fake hash, r3 -> second fake hash. | |
643 gclient_scm.scm.GIT.GetBlessedSha1ForSvnRev(cwd=self.base_path, rev='1' | |
644 ).AndReturn(self.fake_hash_1) | |
645 gclient_scm.scm.GIT.GetBlessedSha1ForSvnRev(cwd=self.base_path, rev='3' | |
646 ).MultipleTimes().AndReturn(self.fake_hash_2) | |
647 | |
648 # Ensure that we call git svn fetch if our LKGR is > the git-svn HEAD rev. | |
649 self.mox.StubOutWithMock(gclient_scm.GitWrapper, '_Fetch', True) | |
650 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'Capture', True) | |
651 gclient_scm.scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'], | |
652 cwd=self.base_path).AndReturn('blah') | |
653 # pylint: disable=E1120 | |
654 gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path) | |
655 error = subprocess2.CalledProcessError(1, 'cmd', '/cwd', 'stdout', 'stderr') | |
656 gclient_scm.scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'], | |
657 cwd=self.base_path).AndRaise(error) | |
658 gclient_scm.GitWrapper._Fetch(options) | |
659 gclient_scm.scm.GIT.Capture(['svn', 'fetch'], cwd=self.base_path) | |
660 gclient_scm.GitWrapper._Fetch(options) | |
661 | |
662 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsGitSvn', True) | |
663 gclient_scm.scm.GIT.IsGitSvn(cwd=self.base_path).MultipleTimes( | |
664 ).AndReturn(True) | |
665 | |
666 self.mox.StubOutWithMock(gclient_scm.scm.GIT, 'IsValidRevision', True) | |
667 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=self.fake_hash_1 | |
668 ).AndReturn(True) | |
669 gclient_scm.scm.GIT.IsValidRevision(cwd=self.base_path, rev=too_big | |
670 ).MultipleTimes(2).AndReturn(False) | |
671 | |
672 gclient_scm.os.path.isdir(self.base_path).AndReturn(False) | |
673 gclient_scm.os.path.isdir(self.base_path).MultipleTimes().AndReturn(True) | |
674 | |
675 self.mox.ReplayAll() | |
676 | |
677 git_svn_scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
678 relpath=self.relpath) | |
679 # Without an existing checkout, this should fail. | |
680 # TODO(dbeam) Fix this. http://crbug.com/109184 | |
681 self.assertRaises(gclient_scm.gclient_utils.Error, | |
682 git_svn_scm.GetUsableRev, '1', options) | |
683 # Given an SVN revision with a git-svn checkout, it should be translated to | |
684 # a git sha1 and be usable. | |
685 self.assertEquals(git_svn_scm.GetUsableRev('1', options), | |
686 self.fake_hash_1) | |
687 # Our fake HEAD rev is r2, so this should call git fetch and git svn fetch | |
688 # to get more revs (pymox will complain if this doesn't happen). We mock an | |
689 # optimized checkout the first time, so this run should call git fetch. | |
690 self.assertEquals(git_svn_scm.GetUsableRev('3', options), | |
691 self.fake_hash_2) | |
692 # The time we pretend we're not optimized, so no git fetch should fire. | |
693 self.assertEquals(git_svn_scm.GetUsableRev('3', options), | |
694 self.fake_hash_2) | |
695 # Given a git sha1 with a git-svn checkout, it should be used as is. | |
696 self.assertEquals(git_svn_scm.GetUsableRev(self.fake_hash_1, options), | |
697 self.fake_hash_1) | |
698 # We currently check for seemingly valid SVN revisions by assuming 6 digit | |
699 # numbers, so assure that numeric revs >= 1000000 don't work. | |
700 self.assertRaises(gclient_scm.gclient_utils.Error, | |
701 git_svn_scm.GetUsableRev, too_big, options) | |
702 | |
703 def testUpdateNoDotGit(self): | 603 def testUpdateNoDotGit(self): |
704 options = self.Options() | 604 options = self.Options() |
705 | 605 |
706 gclient_scm.os.path.isdir( | 606 gclient_scm.os.path.isdir( |
707 os.path.join(self.base_path, '.git', 'hooks')).AndReturn(False) | 607 os.path.join(self.base_path, '.git', 'hooks')).AndReturn(False) |
708 gclient_scm.os.path.exists(self.backup_base_path).AndReturn(False) | 608 gclient_scm.os.path.exists(self.backup_base_path).AndReturn(False) |
709 gclient_scm.os.path.exists(self.base_path).AndReturn(True) | 609 gclient_scm.os.path.exists(self.base_path).AndReturn(True) |
710 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) | 610 gclient_scm.os.path.isdir(self.base_path).AndReturn(True) |
711 gclient_scm.os.path.exists(os.path.join(self.base_path, '.git') | 611 gclient_scm.os.path.exists(os.path.join(self.base_path, '.git') |
712 ).AndReturn(False) | 612 ).AndReturn(False) |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 | 871 |
972 if __name__ == '__main__': | 872 if __name__ == '__main__': |
973 level = logging.DEBUG if '-v' in sys.argv else logging.FATAL | 873 level = logging.DEBUG if '-v' in sys.argv else logging.FATAL |
974 logging.basicConfig( | 874 logging.basicConfig( |
975 level=level, | 875 level=level, |
976 format='%(asctime).19s %(levelname)s %(filename)s:' | 876 format='%(asctime).19s %(levelname)s %(filename)s:' |
977 '%(lineno)s %(message)s') | 877 '%(lineno)s %(message)s') |
978 unittest.main() | 878 unittest.main() |
979 | 879 |
980 # vim: ts=2:sw=2:tw=80:et: | 880 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |