From ab2790a7d69f33ad3bf620ff10deb4190c0408da Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Wed, 17 Jun 2009 14:52:36 +0000 Subject: [PATCH] Fix revert.py which was broken on r18272. Also adding unit tests. TEST=new unit tests BUG=none TBR=nsylvain Review URL: http://codereview.chromium.org/125253 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@18618 0039d316-1c4b-4281-b951-d872f2087c98 --- revert.py | 7 ++-- tests/revert_unittest.py | 82 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/revert.py b/revert.py index 19d924e2c..3e5636399 100755 --- a/revert.py +++ b/revert.py @@ -92,7 +92,8 @@ def Revert(revisions, force=False, commit=True, send_email=True, message=None, # Move to the repository root and make the revision numbers sorted in # decreasing order. - os.chdir(gcl.GetRepositoryRoot()) + local_root = gcl.GetRepositoryRoot() + os.chdir(local_root) revisions.sort(reverse=True) revisions_string = ",".join([str(rev) for rev in revisions]) revisions_string_rev = ",".join([str(-rev) for rev in revisions]) @@ -221,8 +222,8 @@ def Revert(revisions, force=False, commit=True, send_email=True, message=None, description += "\n\n" description += message # Don't use gcl.Change() since it prompts the user for infos. - change_info = gcl.ChangeInfo(name=changename, issue='', - description=description, files=files_status) + change_info = gcl.ChangeInfo(changename, 0, 0, description, files_status, + local_root) change_info.Save() upload_args = ['--no_presubmit', '-r', ",".join(reviewers)] diff --git a/tests/revert_unittest.py b/tests/revert_unittest.py index e95d3c4fa..eee176d2e 100644 --- a/tests/revert_unittest.py +++ b/tests/revert_unittest.py @@ -16,7 +16,17 @@ from super_mox import mox class RevertTestsBase(super_mox.SuperMoxTestBase): """Setups and tear downs the mocks but doesn't test anything as-is.""" - pass + def setUp(self): + super_mox.SuperMoxTestBase.setUp(self) + self.mox.StubOutWithMock(revert, 'gcl') + self.mox.StubOutWithMock(revert, 'gclient') + self.mox.StubOutWithMock(revert, 'os') + self.mox.StubOutWithMock(revert.os, 'path') + self.mox.StubOutWithMock(revert.sys, 'stdout') + + # These functions are not tested. + self.mox.StubOutWithMock(revert, 'GetRepoBase') + self.mox.StubOutWithMock(revert, 'CaptureSVNLog') class RevertUnittest(RevertTestsBase): @@ -31,5 +41,75 @@ class RevertUnittest(RevertTestsBase): self.compareMembers(revert, members) +class RevertMainUnittest(RevertTestsBase): + def setUp(self): + RevertTestsBase.setUp(self) + self.mox.StubOutWithMock(revert, 'gcl') + self.mox.StubOutWithMock(revert, 'gclient') + self.mox.StubOutWithMock(revert, 'os') + self.mox.StubOutWithMock(revert.os, 'path') + self.mox.StubOutWithMock(revert, 'sys') + self.mox.StubOutWithMock(revert, 'Revert') + + def testMain(self): + revert.gcl.GetInfoDir().AndReturn('foo') + revert.os.path.exists('foo').AndReturn(True) + revert.Revert([42, 23], True, True, False, 'bleh', ['foo@example.com'] + ).AndReturn(31337) + self.mox.ReplayAll() + + self.assertEquals(revert.Main(['revert', '-c', '-f', '-n', '-m', 'bleh', + '-r', 'foo@example.com', '42', '23']), + 31337) + + +class RevertRevertUnittest(RevertTestsBase): + def setUp(self): + RevertTestsBase.setUp(self) + + def testRevert(self): + revert.gcl.GetRepositoryRoot().AndReturn('foo') + revert.os.chdir('foo') + entries = [{ + 'author': 'Georges', + 'paths': [ + {'path': 'proto://fqdn/repo/random_file'} + ], + }] + revert.CaptureSVNLog(['-r', '42', '-v']).AndReturn(entries) + revert.GetRepoBase().AndReturn('proto://fqdn/repo/') + revert.gclient.CaptureSVNStatus(['random_file']).AndReturn([]) + revert.gcl.RunShell(['svn', 'up', 'random_file']) + revert.os.path.isdir('random_file').AndReturn(False) + status = """--- Reverse-merging r42 into '.': +M random_file +""" + revert.gcl.RunShellWithReturnCode(['svn', 'merge', '-c', '-42', + 'random_file'], + print_output=True).AndReturn([status, 0]) + change = self.mox.CreateMockAnything() + revert.gcl.ChangeInfo('revert42', 0, 0, 'Reverting 42.\n\nbleh', + [('M ', 'random_file')], 'foo').AndReturn(change) + change.Save() + revert.gcl.UploadCL(change, + ['--no_presubmit', '-r', 'foo@example.com', '--no_try']) + revert.gcl.Commit(change, ['--no_presubmit', '--force']) + revert.gclient.Main(['gclient.py', 'sync']) + outputs = [ + 'Blaming Georges\n', + 'Emailing foo@example.com\n', + 'These files were modified in 42:', + 'random_file', + '', + 'Reverting 42 in ./' + ] + for line in outputs: + revert.sys.stdout.write(line) + revert.sys.stdout.write('\n') + self.mox.ReplayAll() + + revert.Revert([42], True, True, False, 'bleh', ['foo@example.com']) + + if __name__ == '__main__': unittest.main()