OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Unit tests for trychange.py.""" | |
7 | |
8 import os | |
9 import sys | |
10 import unittest | |
11 | |
12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
13 | |
14 from testing_support.super_mox import SuperMoxTestBase | |
15 | |
16 import subprocess2 | |
17 import trychange | |
18 | |
19 | |
20 class TryChangeTestsBase(SuperMoxTestBase): | |
21 """Setups and tear downs the mocks but doesn't test anything as-is.""" | |
22 def setUp(self): | |
23 SuperMoxTestBase.setUp(self) | |
24 self.mox.StubOutWithMock(subprocess2, 'communicate') | |
25 self.mox.StubOutWithMock(trychange, 'RunGit') | |
26 self.mox.StubOutWithMock(trychange.scm.GIT, 'Capture') | |
27 self.mox.StubOutWithMock(trychange.scm.GIT, 'GenerateDiff') | |
28 self.mox.StubOutWithMock(trychange.scm.GIT, 'GetCheckoutRoot') | |
29 self.mox.StubOutWithMock(trychange.scm.GIT, 'GetEmail') | |
30 self.mox.StubOutWithMock(trychange.scm.GIT, 'GetPatchName') | |
31 self.mox.StubOutWithMock(trychange.scm.GIT, 'GetUpstreamBranch') | |
32 self.mox.StubOutWithMock(trychange.scm.SVN, 'GenerateDiff') | |
33 self.mox.StubOutWithMock(trychange.scm.SVN, 'GetCheckoutRoot') | |
34 self.mox.StubOutWithMock(trychange.scm.SVN, 'GetEmail') | |
35 self.fake_root = self.Dir() | |
36 self.expected_files = ['foo.txt', 'bar.txt'] | |
37 self.options = trychange.optparse.Values() | |
38 self.options.files = self.expected_files | |
39 self.options.diff = None | |
40 self.options.name = None | |
41 self.options.email = None | |
42 self.options.exclude = [] | |
43 | |
44 | |
45 class TryChangeUnittest(TryChangeTestsBase): | |
46 """General trychange.py tests.""" | |
47 def testMembersChanged(self): | |
48 members = [ | |
49 'DieWithError', 'EPILOG', 'Escape', 'GIT', 'GIT_PATCH_DIR_BASENAME', | |
50 'GetMungedDiff', 'GuessVCS', 'GIT_BRANCH_FILE', | |
51 'HELP_STRING', 'Error', 'InvalidScript', 'NoTryServerAccess', | |
52 'OptionParser', 'PrintSuccess', | |
53 'RunCommand', 'RunGit', 'SCM', 'SVN', 'TryChange', 'USAGE', 'contextlib', | |
54 'datetime', 'errno', 'fix_encoding', 'gcl', 'gclient_utils', | |
55 'gerrit_util', 'gen_parser', | |
56 'getpass', 'itertools', 'json', 'logging', 'optparse', 'os', 'posixpath', | |
57 're', 'scm', 'shutil', 'subprocess2', 'sys', 'tempfile', 'urllib', | |
58 'urllib2', 'urlparse'] | |
59 # If this test fails, you should add the relevant test. | |
60 self.compareMembers(trychange, members) | |
61 | |
62 | |
63 class TryChangeSimpleTest(unittest.TestCase): | |
64 # Doesn't require supermox to run. | |
65 def test_flags(self): | |
66 cmd = [ | |
67 '--bot', 'bot1,bot2', | |
68 '--testfilter', 'test1', | |
69 '--testfilter', 'test2', | |
70 '--user', 'joe', | |
71 '--email', 'joe@example.com', | |
72 ] | |
73 options, args = trychange.gen_parser(None).parse_args(cmd) | |
74 self.assertEquals([], args) | |
75 # pylint: disable=W0212 | |
76 bot_spec = trychange._ParseBotList(options.bot, options.testfilter) | |
77 if options.testfilter: | |
78 bot_spec = trychange._ApplyTestFilter(options.testfilter, bot_spec) | |
79 values = trychange._ParseSendChangeOptions(bot_spec, options) | |
80 self.assertEquals( | |
81 [ | |
82 ('user', 'joe'), | |
83 ('name', None), | |
84 ('email', 'joe@example.com'), | |
85 ('bot', 'bot1:test1,test2'), | |
86 ('bot', 'bot2:test1,test2'), | |
87 ], | |
88 values) | |
89 | |
90 def test_flags_bad_combination(self): | |
91 cmd = [ | |
92 '--bot', 'bot1:test1', | |
93 '--testfilter', 'test2', | |
94 ] | |
95 options, args = trychange.gen_parser(None).parse_args(cmd) | |
96 self.assertEquals([], args) | |
97 try: | |
98 # pylint: disable=W0212 | |
99 trychange._ParseBotList(options.bot, options.testfilter) | |
100 self.fail() | |
101 except ValueError: | |
102 pass | |
103 | |
104 | |
105 class SVNUnittest(TryChangeTestsBase): | |
106 """trychange.SVN tests.""" | |
107 def testMembersChanged(self): | |
108 members = [ | |
109 'AutomagicalSettings', 'CaptureStatus', 'GetCodeReviewSetting', | |
110 'ReadRootFile', 'GenerateDiff', 'GetFileNames', 'files', 'file_tuples', | |
111 ] | |
112 # If this test fails, you should add the relevant test. | |
113 self.compareMembers(trychange.SVN, members) | |
114 | |
115 def testBasic(self): | |
116 # pylint: disable=E1103 | |
117 trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root) | |
118 trychange.scm.SVN.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root) | |
119 trychange.scm.SVN.GenerateDiff(['foo.txt', 'bar.txt'], | |
120 self.fake_root, | |
121 full_move=True, | |
122 revision=None).AndReturn('A diff') | |
123 trychange.scm.SVN.GetEmail(self.fake_root).AndReturn('georges@example.com') | |
124 self.mox.ReplayAll() | |
125 svn = trychange.SVN(self.options, self.fake_root, self.options.files) | |
126 self.assertEqual(svn.GetFileNames(), self.expected_files) | |
127 self.assertEqual(svn.checkout_root, self.fake_root) | |
128 self.assertEqual(svn.GenerateDiff(), 'A diff') | |
129 | |
130 | |
131 class GITUnittest(TryChangeTestsBase): | |
132 """trychange.GIT tests.""" | |
133 def testMembersChanged(self): | |
134 members = [ | |
135 'AutomagicalSettings', 'CaptureStatus', 'GetCodeReviewSetting', | |
136 'ReadRootFile', 'GenerateDiff', 'GetFileNames', 'files', 'file_tuples', | |
137 ] | |
138 # If this test fails, you should add the relevant test. | |
139 self.compareMembers(trychange.GIT, members) | |
140 | |
141 def testBasic(self): | |
142 # pylint: disable=E1103 | |
143 trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root) | |
144 trychange.scm.GIT.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root) | |
145 trychange.scm.GIT.GetUpstreamBranch(self.fake_root).AndReturn('somewhere') | |
146 trychange.RunGit(['diff-index', 'HEAD']) | |
147 trychange.scm.GIT.GenerateDiff(self.fake_root, | |
148 full_move=True, | |
149 files=['foo.txt', 'bar.txt'], | |
150 branch='somewhere').AndReturn('A diff') | |
151 trychange.scm.GIT.GetPatchName(self.fake_root).AndReturn('bleh-1233') | |
152 trychange.scm.GIT.GetEmail(self.fake_root).AndReturn('georges@example.com') | |
153 self.mox.ReplayAll() | |
154 git = trychange.GIT(self.options, self.fake_root, self.options.files) | |
155 self.assertEqual(git.GetFileNames(), self.expected_files) | |
156 self.assertEqual(git.checkout_root, self.fake_root) | |
157 self.assertEqual(git.GenerateDiff(), 'A diff') | |
158 | |
159 | |
160 if __name__ == '__main__': | |
161 unittest.main() | |
OLD | NEW |