Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tests/presubmit_unittest.py

Issue 122040: Refactor the unit tests. (Closed)
Patch Set: even more cleanup Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/gclient_test.py ('k') | tests/revert_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2006-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 presubmit_support.py and presubmit_canned_checks.py.""" 6 """Unit tests for presubmit_support.py and presubmit_canned_checks.py."""
7 7
8 import exceptions 8 import exceptions
9 import random
10 import string
11 import StringIO 9 import StringIO
12 import unittest 10 import unittest
13 11
14 # Local imports 12 # Local imports
15 import __init__
16 import presubmit_support as presubmit 13 import presubmit_support as presubmit
17 import presubmit_canned_checks 14 import presubmit_canned_checks
18 mox = __init__.mox 15 import super_mox
19 16 from super_mox import mox
20 def String(max_length):
21 return ''.join([random.choice(string.letters)
22 for x in xrange(random.randint(1, max_length))])
23
24 def Strings(max_arg_count, max_arg_length):
25 return [String(max_arg_length) for x in xrange(max_arg_count)]
26
27 def Args(max_arg_count=8, max_arg_length=16):
28 return Strings(max_arg_count, random.randint(1, max_arg_length))
29
30 def _DirElts(max_elt_count=4, max_elt_length=8):
31 return presubmit.os.sep.join(Strings(max_elt_count, max_elt_length))
32
33 def Dir(max_elt_count=4, max_elt_length=8):
34 return (random.choice((presubmit_support.os.sep, '')) +
35 _DirElts(max_elt_count, max_elt_length))
36
37 def Url(max_elt_count=4, max_elt_length=8):
38 return ('svn://random_host:port/a' +
39 _DirElts(max_elt_count, max_elt_length).replace(os.sep, '/'))
40
41 def RootDir(max_elt_count=4, max_elt_length=8):
42 return presubmit.os.sep + _DirElts(max_elt_count, max_elt_length)
43 17
44 18
45 class PresubmitTestsBase(mox.MoxTestBase): 19 class PresubmitTestsBase(super_mox.SuperMoxTestBase):
46 """Setups and tear downs the mocks but doesn't test anything as-is.""" 20 """Setups and tear downs the mocks but doesn't test anything as-is."""
47 presubmit_text = """ 21 presubmit_text = """
48 def CheckChangeOnUpload(input_api, output_api): 22 def CheckChangeOnUpload(input_api, output_api):
49 if not input_api.change.NOSUCHKEY: 23 if not input_api.change.NOSUCHKEY:
50 return [output_api.PresubmitError("!!")] 24 return [output_api.PresubmitError("!!")]
51 elif not input_api.change.REALLYNOSUCHKEY: 25 elif not input_api.change.REALLYNOSUCHKEY:
52 return [output_api.PresubmitPromptWarning("??")] 26 return [output_api.PresubmitPromptWarning("??")]
53 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY: 27 elif not input_api.change.REALLYABSOLUTELYNOSUCHKEY:
54 return [output_api.PresubmitPromptWarning("??"), 28 return [output_api.PresubmitPromptWarning("??"),
55 output_api.PresubmitError("XX!!XX")] 29 output_api.PresubmitError("XX!!XX")]
56 else: 30 else:
57 return () 31 return ()
58 """ 32 """
59 33
60 def setUp(self): 34 def setUp(self):
61 mox.MoxTestBase.setUp(self) 35 super_mox.SuperMoxTestBase.setUp(self)
62 self.mox.StubOutWithMock(presubmit, 'warnings') 36 self.mox.StubOutWithMock(presubmit, 'warnings')
63 # Stub out 'os' but keep os.path.dirname/join/normpath/splitext and os.sep. 37 # Stub out 'os' but keep os.path.dirname/join/normpath/splitext and os.sep.
64 os_sep = presubmit.os.sep 38 os_sep = presubmit.os.sep
65 os_path_join = presubmit.os.path.join 39 os_path_join = presubmit.os.path.join
66 os_path_dirname = presubmit.os.path.dirname 40 os_path_dirname = presubmit.os.path.dirname
67 os_path_normpath = presubmit.os.path.normpath 41 os_path_normpath = presubmit.os.path.normpath
68 os_path_splitext = presubmit.os.path.splitext 42 os_path_splitext = presubmit.os.path.splitext
69 self.mox.StubOutWithMock(presubmit, 'os') 43 self.mox.StubOutWithMock(presubmit, 'os')
70 self.mox.StubOutWithMock(presubmit.os, 'path') 44 self.mox.StubOutWithMock(presubmit.os, 'path')
71 presubmit.os.sep = os_sep 45 presubmit.os.sep = os_sep
72 presubmit.os.path.join = os_path_join 46 presubmit.os.path.join = os_path_join
73 presubmit.os.path.dirname = os_path_dirname 47 presubmit.os.path.dirname = os_path_dirname
74 presubmit.os.path.normpath = os_path_normpath 48 presubmit.os.path.normpath = os_path_normpath
75 presubmit.os.path.splitext = os_path_splitext 49 presubmit.os.path.splitext = os_path_splitext
76 self.mox.StubOutWithMock(presubmit, 'sys') 50 self.mox.StubOutWithMock(presubmit, 'sys')
77 # Special mocks. 51 # Special mocks.
78 def MockAbsPath(f): 52 def MockAbsPath(f):
79 return f 53 return f
80 presubmit.os.path.abspath = MockAbsPath 54 presubmit.os.path.abspath = MockAbsPath
81 self.mox.StubOutWithMock(presubmit.gcl, 'GetRepositoryRoot') 55 self.mox.StubOutWithMock(presubmit.gcl, 'GetRepositoryRoot')
82 fake_root_dir = RootDir() 56 fake_root_dir = self.RootDir()
83 self.fake_root_dir = fake_root_dir 57 self.fake_root_dir = fake_root_dir
84 def MockGetRepositoryRoot(): 58 def MockGetRepositoryRoot():
85 return fake_root_dir 59 return fake_root_dir
86 presubmit.gcl.GetRepositoryRoot = MockGetRepositoryRoot 60 presubmit.gcl.GetRepositoryRoot = MockGetRepositoryRoot
87 self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo') 61 self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo')
88 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty') 62 self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty')
89 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') 63 self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile')
90 64
91 def compareMembers(self, object, members):
92 """If you add a member, be sure to add the relevant test!"""
93 # Skip over members starting with '_' since they are usually not meant to
94 # be for public use.
95 actual_members = [x for x in sorted(dir(object))
96 if not x.startswith('_')]
97 self.assertEqual(actual_members, sorted(members))
98
99 def MakeBasicChange(self, name, description, root=None): 65 def MakeBasicChange(self, name, description, root=None):
100 ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None) 66 ci = presubmit.gcl.ChangeInfo(name, 0, 0, description, None)
101 if root is None: 67 if root is None:
102 root = self.fake_root_dir 68 root = self.fake_root_dir
103 return presubmit.GclChange(ci, root) 69 return presubmit.GclChange(ci, root)
104 70
105 71
106 class PresubmitUnittest(PresubmitTestsBase): 72 class PresubmitUnittest(PresubmitTestsBase):
107 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" 73 """General presubmit_support.py tests (excluding InputApi and OutputApi)."""
108 def testMembersChanged(self): 74 def testMembersChanged(self):
(...skipping 1277 matching lines...) Expand 10 before | Expand all | Expand 10 after
1386 test_result.errors = 0 1352 test_result.errors = 0
1387 self.mox.ReplayAll() 1353 self.mox.ReplayAll()
1388 1354
1389 results = presubmit_canned_checks.RunPythonUnitTests( 1355 results = presubmit_canned_checks.RunPythonUnitTests(
1390 input_api, presubmit.OutputApi, ['test_module']) 1356 input_api, presubmit.OutputApi, ['test_module'])
1391 self.assertEquals(len(results), 0) 1357 self.assertEquals(len(results), 0)
1392 1358
1393 1359
1394 if __name__ == '__main__': 1360 if __name__ == '__main__':
1395 unittest.main() 1361 unittest.main()
OLDNEW
« no previous file with comments | « tests/gclient_test.py ('k') | tests/revert_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698