Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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=E1101,E1103,W0403 | 8 # pylint: disable=E1101,E1103,W0403 |
| 9 | 9 |
| 10 # Import before super_mox to keep valid references. | 10 # Import before super_mox to keep valid references. |
| 11 from os import rename | 11 from os import rename |
| 12 from shutil import rmtree | 12 from shutil import rmtree |
| 13 from subprocess import Popen, PIPE, STDOUT | 13 from subprocess import Popen, PIPE, STDOUT |
| 14 import tempfile | 14 import tempfile |
| 15 import unittest | 15 import unittest |
| 16 import __builtin__ | 16 import __builtin__ |
| 17 | 17 |
| 18 # Fixes include path. | 18 # Fixes include path. |
| 19 from super_mox import mox, StdoutCheck, TestCaseUtils, SuperMoxTestBase | 19 from super_mox import mox, StdoutCheck, TestCaseUtils, SuperMoxTestBase |
| 20 | 20 |
| 21 import logging | |
| 21 import sys | 22 import sys |
| 22 import gclient_scm | 23 import gclient_scm |
| 23 | 24 |
| 24 # Shortcut since this function is used often | 25 # Shortcut since this function is used often |
| 25 join = gclient_scm.os.path.join | 26 join = gclient_scm.os.path.join |
| 26 | 27 |
| 27 | 28 |
| 28 class GCBaseTestCase(object): | 29 class GCBaseTestCase(object): |
| 29 def assertRaisesError(self, msg, fn, *args, **kwargs): | 30 def assertRaisesError(self, msg, fn, *args, **kwargs): |
| 30 """Like unittest's assertRaises() but checks for Gclient.Error.""" | 31 """Like unittest's assertRaises() but checks for Gclient.Error.""" |
| (...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 730 scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, | 731 scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, |
| 731 relpath=relpath) | 732 relpath=relpath) |
| 732 file_list = [] | 733 file_list = [] |
| 733 scm.update(options, (), file_list) | 734 scm.update(options, (), file_list) |
| 734 self.assertEquals(len(file_list), 2) | 735 self.assertEquals(len(file_list), 2) |
| 735 self.assert_(gclient_scm.os.path.isfile(join(base_path, 'a'))) | 736 self.assert_(gclient_scm.os.path.isfile(join(base_path, 'a'))) |
| 736 self.assertEquals(scm.revinfo(options, (), None), | 737 self.assertEquals(scm.revinfo(options, (), None), |
| 737 '069c602044c5388d2d15c3f875b057c852003458') | 738 '069c602044c5388d2d15c3f875b057c852003458') |
| 738 finally: | 739 finally: |
| 739 rmtree(root_dir) | 740 rmtree(root_dir) |
| 740 self.checkstdout( | 741 msg1 = ( |
| 741 ('\n_____ foo at refs/heads/master\n\n' | 742 "\n_____ foo at refs/heads/master\n\n" |
| 742 '________ running \'git clone -b master --verbose %s %s\' in \'%s\'\n' | 743 "________ running 'git clone -b master --verbose %s %s' in '%s'\n" |
| 743 'Initialized empty Git repository in %s\n') % | 744 "Initialized empty Git repository in %s\n") % ( |
| 744 (join(self.root_dir, '.', '.git'), join(root_dir, 'foo'), root_dir, | 745 join(self.root_dir, '.', '.git'), |
| 745 join(gclient_scm.os.path.realpath(root_dir), 'foo', '.git') + '/')) | 746 join(root_dir, 'foo'), |
| 747 root_dir, | |
| 748 join(gclient_scm.os.path.realpath(root_dir), 'foo', '.git') + '/') | |
| 749 msg2 = ( | |
| 750 "\n_____ foo at refs/heads/master\n\n" | |
| 751 "________ running 'git clone -b master --verbose %s %s' in '%s'\n" | |
| 752 "Cloning into %s...\ndone.\n") % ( | |
| 753 join(self.root_dir, '.', '.git'), | |
| 754 join(root_dir, 'foo'), | |
| 755 root_dir, | |
| 756 join(gclient_scm.os.path.realpath(root_dir), 'foo')) | |
| 757 out = sys.stdout.getvalue() | |
| 758 sys.stdout.close() | |
| 759 self.assertTrue(out in (msg1, msg2), (out, msg1, msg2)) | |
| 746 | 760 |
| 747 def testUpdateUpdate(self): | 761 def testUpdateUpdate(self): |
| 748 if not self.enabled: | 762 if not self.enabled: |
| 749 return | 763 return |
| 750 options = self.Options() | 764 options = self.Options() |
| 751 expected_file_list = [join(self.base_path, x) for x in ['a', 'b']] | 765 expected_file_list = [join(self.base_path, x) for x in ['a', 'b']] |
| 752 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 766 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 753 relpath=self.relpath) | 767 relpath=self.relpath) |
| 754 file_list = [] | 768 file_list = [] |
| 755 scm.update(options, (), file_list) | 769 scm.update(options, (), file_list) |
| 756 self.assertEquals(file_list, expected_file_list) | 770 self.assertEquals(file_list, expected_file_list) |
| 757 self.assertEquals(scm.revinfo(options, (), None), | 771 self.assertEquals(scm.revinfo(options, (), None), |
| 758 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 772 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 759 self.checkstdout( | 773 self.checkstdout( |
| 760 '\n_____ . at refs/heads/master\n' | 774 '\n_____ . at refs/heads/master\n' |
| 761 'Updating 069c602..a7142dc\nFast-forward\n a | 1 +\n b | 1 +\n' | 775 'Updating 069c602..a7142dc\nFast-forward\n a | 1 +\n b | 1 +\n' |
| 762 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n') | 776 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n') |
| 763 | 777 |
| 764 def testUpdateUnstagedConflict(self): | 778 def testUpdateUnstagedConflict(self): |
| 765 if not self.enabled: | 779 if not self.enabled: |
| 766 return | 780 return |
| 767 options = self.Options() | 781 options = self.Options() |
| 768 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 782 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 769 relpath=self.relpath) | 783 relpath=self.relpath) |
| 770 file_path = join(self.base_path, 'b') | 784 file_path = join(self.base_path, 'b') |
| 771 open(file_path, 'w').writelines('conflict\n') | 785 open(file_path, 'w').writelines('conflict\n') |
| 772 exception = ( | 786 try: |
| 773 "error: Your local changes to 'b' would be overwritten by merge. " | 787 scm.update(options, (), []) |
| 774 "Aborting.\n" | 788 self.fail() |
| 775 "Please, commit your changes or stash them before you can merge.\n") | 789 except gclient_scm.gclient_utils.CheckCallError: |
| 776 self.assertRaisesError(exception, scm.update, options, (), []) | 790 # The exact exception text varies across git versions so it's not worth |
| 777 self.checkstdout('\n_____ . at refs/heads/master\n') | 791 # verifying it. It's fine as long as it throws. |
| 792 pass | |
| 793 # Manually flush stdout since we can't verify it's content accurately across | |
| 794 # git versions. | |
| 795 sys.stdout.getvalue() | |
| 796 sys.stdout.close() | |
| 778 | 797 |
| 779 def testUpdateConflict(self): | 798 def testUpdateConflict(self): |
| 780 if not self.enabled: | 799 if not self.enabled: |
| 781 return | 800 return |
| 782 options = self.Options() | 801 options = self.Options() |
| 783 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 802 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 784 relpath=self.relpath) | 803 relpath=self.relpath) |
| 785 file_path = join(self.base_path, 'b') | 804 file_path = join(self.base_path, 'b') |
| 786 open(file_path, 'w').writelines('conflict\n') | 805 open(file_path, 'w').writelines('conflict\n') |
| 787 # pylint: disable=W0212 | 806 # pylint: disable=W0212 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 826 if not self.enabled: | 845 if not self.enabled: |
| 827 return | 846 return |
| 828 options = self.Options() | 847 options = self.Options() |
| 829 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 848 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 830 relpath=self.relpath) | 849 relpath=self.relpath) |
| 831 rev_info = scm.revinfo(options, (), None) | 850 rev_info = scm.revinfo(options, (), None) |
| 832 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') | 851 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') |
| 833 | 852 |
| 834 | 853 |
| 835 if __name__ == '__main__': | 854 if __name__ == '__main__': |
| 855 if '-v' in sys.argv: | |
| 856 logging.basicConfig( | |
| 857 level=logging.DEBUG, | |
| 858 format=("%(asctime).19s %(levelname)s %(filename)s:" | |
|
bradn
2011/02/09 04:24:30
" -> ', to match the rest of the file
| |
| 859 "%(lineno)s %(message)s ")) | |
| 836 unittest.main() | 860 unittest.main() |
| 837 | 861 |
| 838 # vim: ts=2:sw=2:tw=80:et: | 862 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |