OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 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 revert.py.""" | 6 """Unit tests for revert.py.""" |
7 | 7 |
8 import os | 8 import os |
9 import unittest | 9 import unittest |
10 | 10 |
11 # Local imports | 11 # Local imports |
12 import revert | 12 import revert |
13 import super_mox | 13 import super_mox |
14 from super_mox import mox | 14 from super_mox import mox |
15 | 15 |
16 | 16 |
17 class RevertTestsBase(super_mox.SuperMoxTestBase): | 17 class RevertTestsBase(super_mox.SuperMoxTestBase): |
18 """Setups and tear downs the mocks but doesn't test anything as-is.""" | 18 """Setups and tear downs the mocks but doesn't test anything as-is.""" |
19 pass | 19 def setUp(self): |
| 20 super_mox.SuperMoxTestBase.setUp(self) |
| 21 self.mox.StubOutWithMock(revert, 'gcl') |
| 22 self.mox.StubOutWithMock(revert, 'gclient') |
| 23 self.mox.StubOutWithMock(revert, 'os') |
| 24 self.mox.StubOutWithMock(revert.os, 'path') |
| 25 self.mox.StubOutWithMock(revert.sys, 'stdout') |
| 26 |
| 27 # These functions are not tested. |
| 28 self.mox.StubOutWithMock(revert, 'GetRepoBase') |
| 29 self.mox.StubOutWithMock(revert, 'CaptureSVNLog') |
20 | 30 |
21 | 31 |
22 class RevertUnittest(RevertTestsBase): | 32 class RevertUnittest(RevertTestsBase): |
23 """General revert.py tests.""" | 33 """General revert.py tests.""" |
24 def testMembersChanged(self): | 34 def testMembersChanged(self): |
25 members = [ | 35 members = [ |
26 'CaptureSVNLog', 'GetRepoBase', 'Main', 'ModifiedFile', 'NoBlameList', | 36 'CaptureSVNLog', 'GetRepoBase', 'Main', 'ModifiedFile', 'NoBlameList', |
27 'NoModifiedFile', 'OutsideOfCheckout', 'Revert', 'UniqueFast', | 37 'NoModifiedFile', 'OutsideOfCheckout', 'Revert', 'UniqueFast', |
28 'exceptions', 'gcl', 'gclient', 'optparse', 'os', 'sys', 'xml' | 38 'exceptions', 'gcl', 'gclient', 'optparse', 'os', 'sys', 'xml' |
29 ] | 39 ] |
30 # If this test fails, you should add the relevant test. | 40 # If this test fails, you should add the relevant test. |
31 self.compareMembers(revert, members) | 41 self.compareMembers(revert, members) |
32 | 42 |
33 | 43 |
| 44 class RevertMainUnittest(RevertTestsBase): |
| 45 def setUp(self): |
| 46 RevertTestsBase.setUp(self) |
| 47 self.mox.StubOutWithMock(revert, 'gcl') |
| 48 self.mox.StubOutWithMock(revert, 'gclient') |
| 49 self.mox.StubOutWithMock(revert, 'os') |
| 50 self.mox.StubOutWithMock(revert.os, 'path') |
| 51 self.mox.StubOutWithMock(revert, 'sys') |
| 52 self.mox.StubOutWithMock(revert, 'Revert') |
| 53 |
| 54 def testMain(self): |
| 55 revert.gcl.GetInfoDir().AndReturn('foo') |
| 56 revert.os.path.exists('foo').AndReturn(True) |
| 57 revert.Revert([42, 23], True, True, False, 'bleh', ['foo@example.com'] |
| 58 ).AndReturn(31337) |
| 59 self.mox.ReplayAll() |
| 60 |
| 61 self.assertEquals(revert.Main(['revert', '-c', '-f', '-n', '-m', 'bleh', |
| 62 '-r', 'foo@example.com', '42', '23']), |
| 63 31337) |
| 64 |
| 65 |
| 66 class RevertRevertUnittest(RevertTestsBase): |
| 67 def setUp(self): |
| 68 RevertTestsBase.setUp(self) |
| 69 |
| 70 def testRevert(self): |
| 71 revert.gcl.GetRepositoryRoot().AndReturn('foo') |
| 72 revert.os.chdir('foo') |
| 73 entries = [{ |
| 74 'author': 'Georges', |
| 75 'paths': [ |
| 76 {'path': 'proto://fqdn/repo/random_file'} |
| 77 ], |
| 78 }] |
| 79 revert.CaptureSVNLog(['-r', '42', '-v']).AndReturn(entries) |
| 80 revert.GetRepoBase().AndReturn('proto://fqdn/repo/') |
| 81 revert.gclient.CaptureSVNStatus(['random_file']).AndReturn([]) |
| 82 revert.gcl.RunShell(['svn', 'up', 'random_file']) |
| 83 revert.os.path.isdir('random_file').AndReturn(False) |
| 84 status = """--- Reverse-merging r42 into '.': |
| 85 M random_file |
| 86 """ |
| 87 revert.gcl.RunShellWithReturnCode(['svn', 'merge', '-c', '-42', |
| 88 'random_file'], |
| 89 print_output=True).AndReturn([status, 0]) |
| 90 change = self.mox.CreateMockAnything() |
| 91 revert.gcl.ChangeInfo('revert42', 0, 0, 'Reverting 42.\n\nbleh', |
| 92 [('M ', 'random_file')], 'foo').AndReturn(change) |
| 93 change.Save() |
| 94 revert.gcl.UploadCL(change, |
| 95 ['--no_presubmit', '-r', 'foo@example.com', '--no_try']) |
| 96 revert.gcl.Commit(change, ['--no_presubmit', '--force']) |
| 97 revert.gclient.Main(['gclient.py', 'sync']) |
| 98 outputs = [ |
| 99 'Blaming Georges\n', |
| 100 'Emailing foo@example.com\n', |
| 101 'These files were modified in 42:', |
| 102 'random_file', |
| 103 '', |
| 104 'Reverting 42 in ./' |
| 105 ] |
| 106 for line in outputs: |
| 107 revert.sys.stdout.write(line) |
| 108 revert.sys.stdout.write('\n') |
| 109 self.mox.ReplayAll() |
| 110 |
| 111 revert.Revert([42], True, True, False, 'bleh', ['foo@example.com']) |
| 112 |
| 113 |
34 if __name__ == '__main__': | 114 if __name__ == '__main__': |
35 unittest.main() | 115 unittest.main() |
OLD | NEW |