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 trychange.py.""" | 6 """Unit tests for trychange.py.""" |
7 | 7 |
8 import optparse | 8 import optparse |
9 | 9 |
10 # Local imports | 10 # Local imports |
11 import trychange | 11 import trychange |
12 from super_mox import mox, SuperMoxTestBase | 12 from super_mox import mox, SuperMoxTestBase |
13 | 13 |
14 | 14 |
15 class TryChangeTestsBase(SuperMoxTestBase): | 15 class TryChangeTestsBase(SuperMoxTestBase): |
16 """Setups and tear downs the mocks but doesn't test anything as-is.""" | 16 """Setups and tear downs the mocks but doesn't test anything as-is.""" |
17 def setUp(self): | 17 def setUp(self): |
18 SuperMoxTestBase.setUp(self) | 18 SuperMoxTestBase.setUp(self) |
19 self.mox.StubOutWithMock(trychange.gclient_utils, 'CheckCall') | 19 self.mox.StubOutWithMock(trychange.gclient_utils, 'CheckCall') |
20 self.mox.StubOutWithMock(trychange.scm.GIT, 'Capture') | 20 self.mox.StubOutWithMock(trychange.scm.GIT, 'Capture') |
| 21 self.mox.StubOutWithMock(trychange.scm.GIT, 'GenerateDiff') |
21 self.mox.StubOutWithMock(trychange.scm.GIT, 'GetEmail') | 22 self.mox.StubOutWithMock(trychange.scm.GIT, 'GetEmail') |
22 self.mox.StubOutWithMock(trychange.scm.SVN, 'DiffItem') | 23 self.mox.StubOutWithMock(trychange.scm.SVN, 'DiffItem') |
| 24 self.mox.StubOutWithMock(trychange.scm.SVN, 'GenerateDiff') |
23 self.mox.StubOutWithMock(trychange.scm.SVN, 'GetCheckoutRoot') | 25 self.mox.StubOutWithMock(trychange.scm.SVN, 'GetCheckoutRoot') |
24 self.mox.StubOutWithMock(trychange.scm.SVN, 'GetEmail') | 26 self.mox.StubOutWithMock(trychange.scm.SVN, 'GetEmail') |
25 self.fake_root = self.Dir() | 27 self.fake_root = self.Dir() |
26 self.expected_files = ['foo.txt', 'bar.txt'] | 28 self.expected_files = ['foo.txt', 'bar.txt'] |
27 self.options = optparse.Values() | 29 self.options = optparse.Values() |
28 self.options.files = self.expected_files | 30 self.options.files = self.expected_files |
29 self.options.diff = None | 31 self.options.diff = None |
30 self.options.name = None | 32 self.options.name = None |
31 self.options.email = None | 33 self.options.email = None |
32 | 34 |
(...skipping 18 matching lines...) Expand all Loading... |
51 def testMembersChanged(self): | 53 def testMembersChanged(self): |
52 members = [ | 54 members = [ |
53 'GetBots', 'GetFileNames', 'GetLocalRoot', | 55 'GetBots', 'GetFileNames', 'GetLocalRoot', |
54 ] | 56 ] |
55 # If this test fails, you should add the relevant test. | 57 # If this test fails, you should add the relevant test. |
56 self.compareMembers(trychange.SVN, members) | 58 self.compareMembers(trychange.SVN, members) |
57 | 59 |
58 def testBasic(self): | 60 def testBasic(self): |
59 trychange.os.getcwd().AndReturn(self.fake_root) | 61 trychange.os.getcwd().AndReturn(self.fake_root) |
60 trychange.scm.SVN.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root) | 62 trychange.scm.SVN.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root) |
61 trychange.os.getcwd().AndReturn('pro') | 63 trychange.scm.SVN.GenerateDiff(['foo.txt', 'bar.txt'], |
62 trychange.os.chdir(self.fake_root) | 64 full_move=True).AndReturn('A diff') |
63 trychange.scm.SVN.DiffItem(self.expected_files[0]).AndReturn('bleh') | |
64 trychange.scm.SVN.DiffItem(self.expected_files[1]).AndReturn('blew') | |
65 trychange.os.chdir('pro') | |
66 trychange.scm.SVN.GetEmail(self.fake_root).AndReturn('georges@example.com') | 65 trychange.scm.SVN.GetEmail(self.fake_root).AndReturn('georges@example.com') |
67 self.mox.ReplayAll() | 66 self.mox.ReplayAll() |
68 svn = trychange.SVN(self.options) | 67 svn = trychange.SVN(self.options) |
69 self.assertEqual(svn.GetFileNames(), self.expected_files) | 68 self.assertEqual(svn.GetFileNames(), self.expected_files) |
70 self.assertEqual(svn.GetLocalRoot(), self.fake_root) | 69 self.assertEqual(svn.GetLocalRoot(), self.fake_root) |
71 | 70 |
72 | 71 |
73 class GITUnittest(TryChangeTestsBase): | 72 class GITUnittest(TryChangeTestsBase): |
74 """trychange.GIT tests.""" | 73 """trychange.GIT tests.""" |
75 def testMembersChanged(self): | 74 def testMembersChanged(self): |
76 members = [ | 75 members = [ |
77 'GetBots', 'GetFileNames', 'GetLocalRoot', | 76 'GetBots', 'GetFileNames', 'GetLocalRoot', |
78 ] | 77 ] |
79 # If this test fails, you should add the relevant test. | 78 # If this test fails, you should add the relevant test. |
80 self.compareMembers(trychange.GIT, members) | 79 self.compareMembers(trychange.GIT, members) |
81 | 80 |
82 def testBasic(self): | 81 def testBasic(self): |
83 trychange.gclient_utils.CheckCall( | 82 trychange.gclient_utils.CheckCall( |
84 ['git', 'rev-parse', '--show-cdup']).AndReturn(self.fake_root) | 83 ['git', 'rev-parse', '--show-cdup']).AndReturn(self.fake_root) |
85 trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root) | 84 trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root) |
| 85 trychange.scm.GIT.GenerateDiff(self.fake_root).AndReturn('a diff') |
86 trychange.gclient_utils.CheckCall( | 86 trychange.gclient_utils.CheckCall( |
87 ['git', 'cl', 'upstream']).AndReturn('random branch') | 87 ['git', 'symbolic-ref', 'HEAD']).AndReturn('refs/heads/random branch') |
88 trychange.gclient_utils.CheckCall( | |
89 ['git', 'diff-tree', '-p', '--no-prefix', 'random branch', 'HEAD'] | |
90 ).AndReturn('This is a dummy diff\n+3\n-4\n') | |
91 trychange.gclient_utils.CheckCall( | |
92 ['git', 'symbolic-ref', 'HEAD']).AndReturn('refs/heads/another branch') | |
93 trychange.scm.GIT.GetEmail('.').AndReturn('georges@example.com') | 88 trychange.scm.GIT.GetEmail('.').AndReturn('georges@example.com') |
94 self.mox.ReplayAll() | 89 self.mox.ReplayAll() |
95 git = trychange.GIT(self.options) | 90 git = trychange.GIT(self.options) |
96 self.assertEqual(git.GetFileNames(), self.expected_files) | 91 self.assertEqual(git.GetFileNames(), self.expected_files) |
97 self.assertEqual(git.GetLocalRoot(), self.fake_root) | 92 self.assertEqual(git.GetLocalRoot(), self.fake_root) |
98 | 93 |
99 | 94 |
100 if __name__ == '__main__': | 95 if __name__ == '__main__': |
101 import unittest | 96 import unittest |
102 unittest.main() | 97 unittest.main() |
OLD | NEW |