| 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 # Import before super_mox to keep valid references. | 8 # Import before super_mox to keep valid references. |
| 9 from os import rename | 9 from os import rename |
| 10 from shutil import rmtree | 10 from shutil import rmtree |
| 11 import StringIO | 11 import StringIO |
| 12 from subprocess import Popen, PIPE, STDOUT | 12 from subprocess import Popen, PIPE, STDOUT |
| 13 import tempfile | 13 import tempfile |
| 14 import unittest |
| 14 import __builtin__ | 15 import __builtin__ |
| 15 | 16 |
| 16 # Fixes include path. | 17 # Fixes include path. |
| 17 from super_mox import mox, SuperMoxBaseTestBase, SuperMoxTestBase | 18 from super_mox import mox, TestCaseUtils, SuperMoxTestBase |
| 18 | 19 |
| 19 import gclient_scm | 20 import gclient_scm |
| 20 | 21 |
| 21 | 22 |
| 22 class GCBaseTestCase(SuperMoxTestBase): | 23 class GCBaseTestCase(object): |
| 23 # Like unittest's assertRaises, but checks for Gclient.Error. | |
| 24 def assertRaisesError(self, msg, fn, *args, **kwargs): | 24 def assertRaisesError(self, msg, fn, *args, **kwargs): |
| 25 """Like unittest's assertRaises() but checks for Gclient.Error.""" |
| 25 try: | 26 try: |
| 26 fn(*args, **kwargs) | 27 fn(*args, **kwargs) |
| 27 except gclient_scm.gclient_utils.Error, e: | 28 except gclient_scm.gclient_utils.Error, e: |
| 28 self.assertEquals(e.args[0], msg) | 29 self.assertEquals(e.args[0], msg) |
| 29 else: | 30 else: |
| 30 self.fail('%s not raised' % msg) | 31 self.fail('%s not raised' % msg) |
| 31 | 32 |
| 33 def setUp(self): |
| 34 self.stdout = StringIO.StringIO() |
| 32 | 35 |
| 33 class BaseTestCase(GCBaseTestCase): | 36 def tearDown(self): |
| 37 try: |
| 38 self.stdout.getvalue() |
| 39 self.fail() |
| 40 except AttributeError: |
| 41 pass |
| 42 |
| 43 def checkstdout(self, expected): |
| 44 value = self.stdout.getvalue() |
| 45 self.stdout.close() |
| 46 self.assertEquals(expected, value) |
| 47 |
| 48 |
| 49 class BaseTestCase(GCBaseTestCase, SuperMoxTestBase): |
| 34 def setUp(self): | 50 def setUp(self): |
| 35 GCBaseTestCase.setUp(self) | 51 GCBaseTestCase.setUp(self) |
| 52 SuperMoxTestBase.setUp(self) |
| 53 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCall') |
| 36 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCallAndFilter') | 54 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'CheckCallAndFilter') |
| 37 self.mox.StubOutWithMock(gclient_scm.gclient_utils, | 55 self.mox.StubOutWithMock(gclient_scm.gclient_utils, |
| 38 'CheckCallAndFilterAndHeader') | 56 'CheckCallAndFilterAndHeader') |
| 39 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') | 57 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileRead') |
| 40 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') | 58 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'FileWrite') |
| 59 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'Popen') |
| 41 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'RemoveDirectory') | 60 self.mox.StubOutWithMock(gclient_scm.gclient_utils, 'RemoveDirectory') |
| 42 self._CaptureSVNInfo = gclient_scm.scm.SVN.CaptureInfo | 61 self._CaptureSVNInfo = gclient_scm.scm.SVN.CaptureInfo |
| 43 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Capture') | 62 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'Capture') |
| 44 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureInfo') | 63 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureInfo') |
| 45 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureStatus') | 64 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'CaptureStatus') |
| 46 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'RunAndGetFileList') | 65 self.mox.StubOutWithMock(gclient_scm.scm.SVN, 'RunAndGetFileList') |
| 47 self._scm_wrapper = gclient_scm.CreateSCM | 66 self._scm_wrapper = gclient_scm.CreateSCM |
| 48 gclient_scm.sys.stdout.flush = lambda: None | 67 gclient_scm.sys.stdout.flush = lambda: None |
| 49 gclient_scm.scm.SVN.current_version = None | 68 gclient_scm.scm.SVN.current_version = None |
| 50 self.stdout = StringIO.StringIO() | |
| 51 | 69 |
| 52 def tearDown(self): | 70 def tearDown(self): |
| 53 GCBaseTestCase.tearDown(self) | 71 GCBaseTestCase.tearDown(self) |
| 54 try: | 72 SuperMoxTestBase.tearDown(self) |
| 55 self.stdout.getvalue() | |
| 56 self.fail() | |
| 57 except AttributeError: | |
| 58 pass | |
| 59 | |
| 60 def checkstdout(self, expected): | |
| 61 value = self.stdout.getvalue() | |
| 62 self.stdout.close() | |
| 63 self.assertEquals(expected, value) | |
| 64 | 73 |
| 65 | 74 |
| 66 class SVNWrapperTestCase(BaseTestCase): | 75 class SVNWrapperTestCase(BaseTestCase): |
| 67 class OptionsObject(object): | 76 class OptionsObject(object): |
| 68 def __init__(self, test_case, verbose=False, revision=None): | 77 def __init__(self, test_case, verbose=False, revision=None): |
| 69 self.verbose = verbose | 78 self.verbose = verbose |
| 70 self.revision = revision | 79 self.revision = revision |
| 71 self.manually_grab_svn_rev = True | 80 self.manually_grab_svn_rev = True |
| 72 self.deps_os = None | 81 self.deps_os = None |
| 73 self.force = False | 82 self.force = False |
| 74 self.reset = False | 83 self.reset = False |
| 75 self.nohooks = False | 84 self.nohooks = False |
| 76 self.stdout = test_case.stdout | 85 self.stdout = test_case.stdout |
| 77 | 86 |
| 78 def Options(self, *args, **kwargs): | 87 def Options(self, *args, **kwargs): |
| 79 return self.OptionsObject(self, *args, **kwargs) | 88 return self.OptionsObject(self, *args, **kwargs) |
| 80 | 89 |
| 81 def setUp(self): | 90 def setUp(self): |
| 82 BaseTestCase.setUp(self) | 91 BaseTestCase.setUp(self) |
| 83 self.root_dir = self.Dir() | 92 self.url = self.SvnUrl() |
| 84 self.args = self.Args() | |
| 85 self.url = self.Url() | |
| 86 self.relpath = 'asf' | |
| 87 | 93 |
| 88 def testDir(self): | 94 def testDir(self): |
| 89 members = [ | 95 members = [ |
| 90 'FullUrlForRelativeUrl', 'RunCommand', | 96 'FullUrlForRelativeUrl', 'RunCommand', |
| 91 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert', | 97 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert', |
| 92 'revinfo', 'runhooks', 'status', 'update', | 98 'revinfo', 'runhooks', 'status', 'update', |
| 93 'updatesingle', 'url', | 99 'updatesingle', 'url', |
| 94 ] | 100 ] |
| 95 | 101 |
| 96 # If you add a member, be sure to add the relevant test! | 102 # If you add a member, be sure to add the relevant test! |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 | 496 |
| 491 self.mox.ReplayAll() | 497 self.mox.ReplayAll() |
| 492 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | 498 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, |
| 493 relpath=self.relpath) | 499 relpath=self.relpath) |
| 494 file_list = [] | 500 file_list = [] |
| 495 scm.update(options, self.args, file_list) | 501 scm.update(options, self.args, file_list) |
| 496 self.checkstdout( | 502 self.checkstdout( |
| 497 ('________ found .git directory; skipping %s\n' % self.relpath)) | 503 ('________ found .git directory; skipping %s\n' % self.relpath)) |
| 498 | 504 |
| 499 | 505 |
| 500 class GitWrapperTestCase(BaseTestCase): | 506 class GitWrapperTestCase(GCBaseTestCase, TestCaseUtils, unittest.TestCase): |
| 501 """This class doesn't use pymox.""" | 507 """This class doesn't use pymox.""" |
| 502 class OptionsObject(object): | 508 class OptionsObject(object): |
| 503 def __init__(self, test_case, verbose=False, revision=None): | 509 def __init__(self, test_case, verbose=False, revision=None): |
| 504 self.verbose = verbose | 510 self.verbose = verbose |
| 505 self.revision = revision | 511 self.revision = revision |
| 506 self.manually_grab_svn_rev = True | 512 self.manually_grab_svn_rev = True |
| 507 self.deps_os = None | 513 self.deps_os = None |
| 508 self.force = False | 514 self.force = False |
| 509 self.reset = False | 515 self.reset = False |
| 510 self.nohooks = False | 516 self.nohooks = False |
| 511 self.stdout = StringIO.StringIO() | 517 self.stdout = test_case.stdout |
| 512 | 518 |
| 513 sample_git_import = """blob | 519 sample_git_import = """blob |
| 514 mark :1 | 520 mark :1 |
| 515 data 6 | 521 data 6 |
| 516 Hello | 522 Hello |
| 517 | 523 |
| 518 blob | 524 blob |
| 519 mark :2 | 525 mark :2 |
| 520 data 4 | 526 data 4 |
| 521 Bye | 527 Bye |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 | 560 |
| 555 reset refs/heads/master | 561 reset refs/heads/master |
| 556 from :3 | 562 from :3 |
| 557 """ | 563 """ |
| 558 def Options(self, *args, **kwargs): | 564 def Options(self, *args, **kwargs): |
| 559 return self.OptionsObject(self, *args, **kwargs) | 565 return self.OptionsObject(self, *args, **kwargs) |
| 560 | 566 |
| 561 def CreateGitRepo(self, git_import, path): | 567 def CreateGitRepo(self, git_import, path): |
| 562 """Do it for real.""" | 568 """Do it for real.""" |
| 563 try: | 569 try: |
| 564 Popen(['git', 'init'], stdout=PIPE, stderr=STDOUT, | 570 Popen(['git', 'init', '-q'], stdout=PIPE, stderr=STDOUT, |
| 565 cwd=path).communicate() | 571 cwd=path).communicate() |
| 566 except OSError: | 572 except OSError: |
| 567 # git is not available, skip this test. | 573 # git is not available, skip this test. |
| 568 return False | 574 return False |
| 569 Popen(['git', 'fast-import'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, | 575 Popen(['git', 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE, |
| 570 cwd=path).communicate(input=git_import) | 576 stderr=STDOUT, cwd=path).communicate(input=git_import) |
| 571 Popen(['git', 'checkout'], stdout=PIPE, stderr=STDOUT, | 577 Popen(['git', 'checkout', '-q'], stdout=PIPE, stderr=STDOUT, |
| 572 cwd=path).communicate() | 578 cwd=path).communicate() |
| 573 Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE, | 579 Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE, |
| 574 stderr=STDOUT, cwd=path).communicate() | 580 stderr=STDOUT, cwd=path).communicate() |
| 575 Popen(['git', 'checkout', '-b', 'new', 'origin/master'], stdout=PIPE, | 581 Popen(['git', 'checkout', '-b', 'new', 'origin/master', '-q'], stdout=PIPE, |
| 576 stderr=STDOUT, cwd=path).communicate() | 582 stderr=STDOUT, cwd=path).communicate() |
| 577 Popen(['git', 'push', 'origin', 'origin/origin:origin/master'], stdout=PIPE, | 583 Popen(['git', 'push', 'origin', 'origin/origin:origin/master', '-q'], |
| 578 stderr=STDOUT, cwd=path).communicate() | 584 stdout=PIPE, stderr=STDOUT, cwd=path).communicate() |
| 579 Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE, | 585 Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE, |
| 580 stderr=STDOUT, cwd=path).communicate() | 586 stderr=STDOUT, cwd=path).communicate() |
| 581 return True | 587 return True |
| 582 | 588 |
| 583 def setUp(self): | 589 def setUp(self): |
| 584 self.args = self.Args() | 590 GCBaseTestCase.setUp(self) |
| 591 TestCaseUtils.setUp(self) |
| 592 unittest.TestCase.setUp(self) |
| 585 self.url = 'git://foo' | 593 self.url = 'git://foo' |
| 586 self.root_dir = tempfile.mkdtemp() | 594 self.root_dir = tempfile.mkdtemp() |
| 587 self.relpath = '.' | 595 self.relpath = '.' |
| 588 self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) | 596 self.base_path = gclient_scm.os.path.join(self.root_dir, self.relpath) |
| 589 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) | 597 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
| 590 BaseTestBase.setUp(self) | |
| 591 | 598 |
| 592 def tearDown(self): | 599 def tearDown(self): |
| 593 BaseTestBase.tearDown(self) | 600 GCBaseTestCase.tearDown(self) |
| 601 TestCaseUtils.tearDown(self) |
| 602 unittest.TestCase.tearDown(self) |
| 594 rmtree(self.root_dir) | 603 rmtree(self.root_dir) |
| 595 | 604 |
| 596 def testDir(self): | 605 def testDir(self): |
| 597 members = [ | 606 members = [ |
| 598 'FullUrlForRelativeUrl', 'RunCommand', | 607 'FullUrlForRelativeUrl', 'RunCommand', |
| 599 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert', | 608 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert', |
| 600 'revinfo', 'runhooks', 'status', 'update', 'url', | 609 'revinfo', 'runhooks', 'status', 'update', 'url', |
| 601 ] | 610 ] |
| 602 | 611 |
| 603 # If you add a member, be sure to add the relevant test! | 612 # If you add a member, be sure to add the relevant test! |
| 604 self.compareMembers(gclient_scm.CreateSCM(url=self.url), members) | 613 self.compareMembers(gclient_scm.CreateSCM(url=self.url), members) |
| 614 self.checkstdout('') |
| 605 | 615 |
| 606 def testRevertMissing(self): | 616 def testRevertMissing(self): |
| 607 if not self.enabled: | 617 if not self.enabled: |
| 608 return | 618 return |
| 609 options = self.Options() | 619 options = self.Options() |
| 610 file_path = gclient_scm.os.path.join(self.base_path, 'a') | 620 file_path = gclient_scm.os.path.join(self.base_path, 'a') |
| 611 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 621 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 612 relpath=self.relpath) | 622 relpath=self.relpath) |
| 613 file_list = [] | 623 file_list = [] |
| 614 scm.update(options, None, file_list) | 624 scm.update(options, None, file_list) |
| 615 gclient_scm.os.remove(file_path) | 625 gclient_scm.os.remove(file_path) |
| 616 file_list = [] | 626 file_list = [] |
| 617 scm.revert(options, self.args, file_list) | 627 scm.revert(options, self.args, file_list) |
| 618 self.assertEquals(file_list, [file_path]) | 628 self.assertEquals(file_list, [file_path]) |
| 619 file_list = [] | 629 file_list = [] |
| 620 scm.diff(options, self.args, file_list) | 630 scm.diff(options, self.args, file_list) |
| 621 self.assertEquals(file_list, []) | 631 self.assertEquals(file_list, []) |
| 632 self.checkstdout( |
| 633 ('\n_____ . at refs/heads/master\n\n\n' |
| 634 '________ running \'git reset --hard origin/master\' in \'%s\'\n' |
| 635 'HEAD is now at a7142dc Personalized\n') % |
| 636 gclient_scm.os.path.join(self.root_dir, '.')) |
| 622 | 637 |
| 623 def testRevertNone(self): | 638 def testRevertNone(self): |
| 624 if not self.enabled: | 639 if not self.enabled: |
| 625 return | 640 return |
| 626 options = self.Options() | 641 options = self.Options() |
| 627 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 642 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 628 relpath=self.relpath) | 643 relpath=self.relpath) |
| 629 file_list = [] | 644 file_list = [] |
| 630 scm.update(options, None, file_list) | 645 scm.update(options, None, file_list) |
| 631 file_list = [] | 646 file_list = [] |
| 632 scm.revert(options, self.args, file_list) | 647 scm.revert(options, self.args, file_list) |
| 633 self.assertEquals(file_list, []) | 648 self.assertEquals(file_list, []) |
| 634 self.assertEquals(scm.revinfo(options, self.args, None), | 649 self.assertEquals(scm.revinfo(options, self.args, None), |
| 635 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 650 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 636 | 651 self.checkstdout( |
| 652 ('\n_____ . at refs/heads/master\n\n\n' |
| 653 '________ running \'git reset --hard origin/master\' in \'%s\'\n' |
| 654 'HEAD is now at a7142dc Personalized\n') % |
| 655 gclient_scm.os.path.join(self.root_dir, '.')) |
| 637 | 656 |
| 638 def testRevertModified(self): | 657 def testRevertModified(self): |
| 639 if not self.enabled: | 658 if not self.enabled: |
| 640 return | 659 return |
| 641 options = self.Options() | 660 options = self.Options() |
| 642 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 661 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 643 relpath=self.relpath) | 662 relpath=self.relpath) |
| 644 file_list = [] | 663 file_list = [] |
| 645 scm.update(options, None, file_list) | 664 scm.update(options, None, file_list) |
| 646 file_path = gclient_scm.os.path.join(self.base_path, 'a') | 665 file_path = gclient_scm.os.path.join(self.base_path, 'a') |
| 647 open(file_path, 'a').writelines('touched\n') | 666 open(file_path, 'a').writelines('touched\n') |
| 648 file_list = [] | 667 file_list = [] |
| 649 scm.revert(options, self.args, file_list) | 668 scm.revert(options, self.args, file_list) |
| 650 self.assertEquals(file_list, [file_path]) | 669 self.assertEquals(file_list, [file_path]) |
| 651 file_list = [] | 670 file_list = [] |
| 652 scm.diff(options, self.args, file_list) | 671 scm.diff(options, self.args, file_list) |
| 653 self.assertEquals(file_list, []) | 672 self.assertEquals(file_list, []) |
| 654 self.assertEquals(scm.revinfo(options, self.args, None), | 673 self.assertEquals(scm.revinfo(options, self.args, None), |
| 655 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 674 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 675 self.checkstdout( |
| 676 ('\n_____ . at refs/heads/master\n\n\n' |
| 677 '________ running \'git reset --hard origin/master\' in \'%s\'\n' |
| 678 'HEAD is now at a7142dc Personalized\n') % |
| 679 gclient_scm.os.path.join(self.root_dir, '.')) |
| 656 | 680 |
| 657 def testRevertNew(self): | 681 def testRevertNew(self): |
| 658 if not self.enabled: | 682 if not self.enabled: |
| 659 return | 683 return |
| 660 options = self.Options() | 684 options = self.Options() |
| 661 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 685 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 662 relpath=self.relpath) | 686 relpath=self.relpath) |
| 663 file_list = [] | 687 file_list = [] |
| 664 scm.update(options, None, file_list) | 688 scm.update(options, None, file_list) |
| 665 file_path = gclient_scm.os.path.join(self.base_path, 'c') | 689 file_path = gclient_scm.os.path.join(self.base_path, 'c') |
| 666 f = open(file_path, 'w') | 690 f = open(file_path, 'w') |
| 667 f.writelines('new\n') | 691 f.writelines('new\n') |
| 668 f.close() | 692 f.close() |
| 669 Popen(['git', 'add', 'c'], stdout=PIPE, | 693 Popen(['git', 'add', 'c'], stdout=PIPE, |
| 670 stderr=STDOUT, cwd=self.base_path).communicate() | 694 stderr=STDOUT, cwd=self.base_path).communicate() |
| 671 file_list = [] | 695 file_list = [] |
| 672 scm.revert(options, self.args, file_list) | 696 scm.revert(options, self.args, file_list) |
| 673 self.assertEquals(file_list, [file_path]) | 697 self.assertEquals(file_list, [file_path]) |
| 674 file_list = [] | 698 file_list = [] |
| 675 scm.diff(options, self.args, file_list) | 699 scm.diff(options, self.args, file_list) |
| 676 self.assertEquals(file_list, []) | 700 self.assertEquals(file_list, []) |
| 677 self.assertEquals(scm.revinfo(options, self.args, None), | 701 self.assertEquals(scm.revinfo(options, self.args, None), |
| 678 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 702 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 703 self.checkstdout( |
| 704 ('\n_____ . at refs/heads/master\n\n\n' |
| 705 '________ running \'git reset --hard origin/master\' in \'%s\'\n' |
| 706 'HEAD is now at a7142dc Personalized\n') % |
| 707 gclient_scm.os.path.join(self.root_dir, '.')) |
| 679 | 708 |
| 680 def testStatusNew(self): | 709 def testStatusNew(self): |
| 681 if not self.enabled: | 710 if not self.enabled: |
| 682 return | 711 return |
| 683 options = self.Options() | 712 options = self.Options() |
| 684 file_path = gclient_scm.os.path.join(self.base_path, 'a') | 713 file_path = gclient_scm.os.path.join(self.base_path, 'a') |
| 685 open(file_path, 'a').writelines('touched\n') | 714 open(file_path, 'a').writelines('touched\n') |
| 686 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 715 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 687 relpath=self.relpath) | 716 relpath=self.relpath) |
| 688 file_list = [] | 717 file_list = [] |
| 689 scm.status(options, self.args, file_list) | 718 scm.status(options, self.args, file_list) |
| 690 self.assertEquals(file_list, [file_path]) | 719 self.assertEquals(file_list, [file_path]) |
| 720 self.checkstdout( |
| 721 ('\n________ running \'git diff --name-status ' |
| 722 '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\nM\ta\n') % |
| 723 gclient_scm.os.path.join(self.root_dir, '.')) |
| 691 | 724 |
| 692 def testStatus2New(self): | 725 def testStatus2New(self): |
| 693 if not self.enabled: | 726 if not self.enabled: |
| 694 return | 727 return |
| 695 options = self.Options() | 728 options = self.Options() |
| 696 expected_file_list = [] | 729 expected_file_list = [] |
| 697 for f in ['a', 'b']: | 730 for f in ['a', 'b']: |
| 698 file_path = gclient_scm.os.path.join(self.base_path, f) | 731 file_path = gclient_scm.os.path.join(self.base_path, f) |
| 699 open(file_path, 'a').writelines('touched\n') | 732 open(file_path, 'a').writelines('touched\n') |
| 700 expected_file_list.extend([file_path]) | 733 expected_file_list.extend([file_path]) |
| 701 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 734 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 702 relpath=self.relpath) | 735 relpath=self.relpath) |
| 703 file_list = [] | 736 file_list = [] |
| 704 scm.status(options, self.args, file_list) | 737 scm.status(options, self.args, file_list) |
| 705 expected_file_list = [gclient_scm.os.path.join(self.base_path, x) | 738 expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 706 for x in ['a', 'b']] | 739 for x in ['a', 'b']] |
| 707 self.assertEquals(sorted(file_list), expected_file_list) | 740 self.assertEquals(sorted(file_list), expected_file_list) |
| 741 self.checkstdout( |
| 742 ('\n________ running \'git diff --name-status ' |
| 743 '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\nM\ta\nM\tb\n') % |
| 744 gclient_scm.os.path.join(self.root_dir, '.')) |
| 708 | 745 |
| 709 def testUpdateCheckout(self): | 746 def testUpdateCheckout(self): |
| 710 if not self.enabled: | 747 if not self.enabled: |
| 711 return | 748 return |
| 712 options = self.Options(verbose=True) | 749 options = self.Options(verbose=True) |
| 713 root_dir = tempfile.mkdtemp() | 750 root_dir = tempfile.mkdtemp() |
| 714 relpath = 'foo' | 751 relpath = 'foo' |
| 715 base_path = gclient_scm.os.path.join(root_dir, relpath) | 752 base_path = gclient_scm.os.path.join(root_dir, relpath) |
| 716 url = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') | 753 url = gclient_scm.os.path.join(self.root_dir, self.relpath, '.git') |
| 717 try: | 754 try: |
| 718 scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, | 755 scm = gclient_scm.CreateSCM(url=url, root_dir=root_dir, |
| 719 relpath=relpath) | 756 relpath=relpath) |
| 720 file_list = [] | 757 file_list = [] |
| 721 scm.update(options, (), file_list) | 758 scm.update(options, (), file_list) |
| 722 self.assertEquals(len(file_list), 2) | 759 self.assertEquals(len(file_list), 2) |
| 723 self.assert_(gclient_scm.os.path.isfile( | 760 self.assert_(gclient_scm.os.path.isfile( |
| 724 gclient_scm.os.path.join(base_path, 'a'))) | 761 gclient_scm.os.path.join(base_path, 'a'))) |
| 725 self.assertEquals(scm.revinfo(options, (), None), | 762 self.assertEquals(scm.revinfo(options, (), None), |
| 726 '069c602044c5388d2d15c3f875b057c852003458') | 763 '069c602044c5388d2d15c3f875b057c852003458') |
| 727 finally: | 764 finally: |
| 728 rmtree(root_dir) | 765 rmtree(root_dir) |
| 766 join = gclient_scm.os.path.join |
| 767 self.checkstdout( |
| 768 ('\n_____ foo at refs/heads/master\n\n' |
| 769 '________ running \'git clone -b master --verbose %s %s\' in \'%s\'\n' |
| 770 'Initialized empty Git repository in %s\n') % |
| 771 (join(self.root_dir, '.', '.git'), join(root_dir, 'foo'), root_dir, |
| 772 join(root_dir, 'foo', '.git') + '/')) |
| 729 | 773 |
| 730 def testUpdateUpdate(self): | 774 def testUpdateUpdate(self): |
| 731 if not self.enabled: | 775 if not self.enabled: |
| 732 return | 776 return |
| 733 options = self.Options() | 777 options = self.Options() |
| 734 expected_file_list = [gclient_scm.os.path.join(self.base_path, x) | 778 expected_file_list = [gclient_scm.os.path.join(self.base_path, x) |
| 735 for x in ['a', 'b']] | 779 for x in ['a', 'b']] |
| 736 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 780 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 737 relpath=self.relpath) | 781 relpath=self.relpath) |
| 738 file_list = [] | 782 file_list = [] |
| 739 scm.update(options, (), file_list) | 783 scm.update(options, (), file_list) |
| 740 self.assertEquals(file_list, expected_file_list) | 784 self.assertEquals(file_list, expected_file_list) |
| 741 self.assertEquals(scm.revinfo(options, (), None), | 785 self.assertEquals(scm.revinfo(options, (), None), |
| 742 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 786 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
| 787 self.checkstdout('\n_____ . at refs/heads/master\n\n') |
| 743 | 788 |
| 744 def testUpdateUnstagedConflict(self): | 789 def testUpdateUnstagedConflict(self): |
| 745 if not self.enabled: | 790 if not self.enabled: |
| 746 return | 791 return |
| 747 options = self.Options() | 792 options = self.Options() |
| 748 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 793 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 749 relpath=self.relpath) | 794 relpath=self.relpath) |
| 750 file_path = gclient_scm.os.path.join(self.base_path, 'b') | 795 file_path = gclient_scm.os.path.join(self.base_path, 'b') |
| 751 f = open(file_path, 'w').writelines('conflict\n') | 796 f = open(file_path, 'w').writelines('conflict\n') |
| 752 exception = ( | 797 exception = ( |
| 753 "error: Your local changes to 'b' would be overwritten by merge. " | 798 "error: Your local changes to 'b' would be overwritten by merge. " |
| 754 "Aborting.\n" | 799 "Aborting.\n" |
| 755 "Please, commit your changes or stash them before you can merge.\n") | 800 "Please, commit your changes or stash them before you can merge.\n") |
| 756 self.assertRaisesError(exception, scm.update, options, (), []) | 801 self.assertRaisesError(exception, scm.update, options, (), []) |
| 802 self.checkstdout('\n_____ . at refs/heads/master\n') |
| 757 | 803 |
| 758 def testUpdateConflict(self): | 804 def testUpdateConflict(self): |
| 759 if not self.enabled: | 805 if not self.enabled: |
| 760 return | 806 return |
| 761 options = self.Options() | 807 options = self.Options() |
| 762 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 808 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 763 relpath=self.relpath) | 809 relpath=self.relpath) |
| 764 file_path = gclient_scm.os.path.join(self.base_path, 'b') | 810 file_path = gclient_scm.os.path.join(self.base_path, 'b') |
| 765 f = open(file_path, 'w').writelines('conflict\n') | 811 f = open(file_path, 'w').writelines('conflict\n') |
| 766 scm._Run(['commit', '-am', 'test']) | 812 scm._Run(['commit', '-am', 'test'], options) |
| 767 self.mox.StubOutWithMock(__builtin__, 'raw_input') | 813 __builtin__.raw_input = lambda x: 'y' |
| 768 __builtin__.raw_input.__call__(mox.StrContains('Cannot fast-forward merge, ' | 814 exception = ('Conflict while rebasing this branch.\n' |
| 769 'attempt to rebase? (y)es / ' | 815 'Fix the conflict and run gclient again.\n' |
| 770 '(q)uit / (s)kip : ') | 816 'See \'man git-rebase\' for details.\n') |
| 771 ).AndReturn('y') | |
| 772 self.mox.ReplayAll() | |
| 773 exception = \ | |
| 774 'Conflict while rebasing this branch.\n' \ | |
| 775 'Fix the conflict and run gclient again.\n' \ | |
| 776 "See 'man git-rebase' for details.\n" | |
| 777 self.assertRaisesError(exception, scm.update, options, (), []) | 817 self.assertRaisesError(exception, scm.update, options, (), []) |
| 778 exception = \ | 818 exception = ('\n____ . at refs/heads/master\n' |
| 779 '\n____ . at refs/heads/master\n' \ | 819 '\tYou have unstaged changes.\n' |
| 780 '\tYou have unstaged changes.\n' \ | 820 '\tPlease commit, stash, or reset.\n') |
| 781 '\tPlease commit, stash, or reset.\n' | |
| 782 self.assertRaisesError(exception, scm.update, options, (), []) | 821 self.assertRaisesError(exception, scm.update, options, (), []) |
| 822 # The hash always changes. Use a cheap trick. |
| 823 start = ('\n________ running \'git commit -am test\' in \'%s\'\n' |
| 824 '[new ') % gclient_scm.os.path.join(self.root_dir, '.') |
| 825 end = ('] test\n 1 files changed, 1 insertions(+), ' |
| 826 '1 deletions(-)\n\n_____ . at refs/heads/master\n' |
| 827 'Attempting rebase onto refs/remotes/origin/master...\n') |
| 828 self.assertTrue(self.stdout.getvalue().startswith(start)) |
| 829 self.assertTrue(self.stdout.getvalue().endswith(end)) |
| 830 self.assertEquals(len(self.stdout.getvalue()), len(start) + len(end) + 7) |
| 831 self.stdout.close() |
| 783 | 832 |
| 784 def testUpdateNotGit(self): | 833 def testUpdateNotGit(self): |
| 785 if not self.enabled: | 834 if not self.enabled: |
| 786 return | 835 return |
| 787 options = self.Options() | 836 options = self.Options() |
| 788 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 837 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 789 relpath=self.relpath) | 838 relpath=self.relpath) |
| 790 git_path = gclient_scm.os.path.join(self.base_path, '.git') | 839 git_path = gclient_scm.os.path.join(self.base_path, '.git') |
| 791 rename(git_path, git_path + 'foo') | 840 rename(git_path, git_path + 'foo') |
| 792 exception = \ | 841 exception = ('\n____ . at refs/heads/master\n' |
| 793 '\n____ . at refs/heads/master\n' \ | 842 '\tPath is not a git repo. No .git dir.\n' |
| 794 '\tPath is not a git repo. No .git dir.\n' \ | 843 '\tTo resolve:\n' |
| 795 '\tTo resolve:\n' \ | 844 '\t\trm -rf .\n' |
| 796 '\t\trm -rf .\n' \ | 845 '\tAnd run gclient sync again\n') |
| 797 '\tAnd run gclient sync again\n' | |
| 798 self.assertRaisesError(exception, scm.update, options, (), []) | 846 self.assertRaisesError(exception, scm.update, options, (), []) |
| 847 self.checkstdout('') |
| 799 | 848 |
| 800 def testRevinfo(self): | 849 def testRevinfo(self): |
| 801 if not self.enabled: | 850 if not self.enabled: |
| 802 return | 851 return |
| 803 options = self.Options() | 852 options = self.Options() |
| 804 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 853 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
| 805 relpath=self.relpath) | 854 relpath=self.relpath) |
| 806 rev_info = scm.revinfo(options, (), None) | 855 rev_info = scm.revinfo(options, (), None) |
| 807 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') | 856 self.assertEquals(rev_info, '069c602044c5388d2d15c3f875b057c852003458') |
| 857 self.checkstdout('') |
| 808 | 858 |
| 809 | 859 |
| 810 if __name__ == '__main__': | 860 if __name__ == '__main__': |
| 811 import unittest | |
| 812 unittest.main() | 861 unittest.main() |
| 813 | 862 |
| 814 # vim: ts=2:sw=2:tw=80:et: | 863 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |