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

Side by Side Diff: PRESUBMIT_unittest.py

Issue 118484: Use input_api.ReadFile() instead of rolling out its own. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' 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 | Annotate | Revision Log
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | 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) 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 top-level Chromium presubmit script. 6 """Unit tests for top-level Chromium presubmit script.
7 """ 7 """
8 8
9 9
10 import os 10 import os
11 import PRESUBMIT 11 import PRESUBMIT
12 import re 12 import re
13 import unittest 13 import unittest
14 14
15 15
16 class MockInputApi(object): 16 class MockInputApi(object):
17 def __init__(self): 17 def __init__(self, test):
18 self.affected_files = [] 18 self.affected_files = []
19 self.re = re 19 self.re = re
20 self.os_path = os.path 20 self.os_path = os.path
21 self._test = test
21 22
22 def AffectedFiles(self, include_deletes=True): 23 def AffectedFiles(self, include_deletes=True):
23 if include_deletes: 24 if include_deletes:
24 return self.affected_files 25 return self.affected_files
25 else: 26 else:
26 return filter(lambda x: x.Action() != 'D', self.affected_files) 27 return filter(lambda x: x.Action() != 'D', self.affected_files)
27 28
28 def AffectedTextFiles(self, include_deletes=True): 29 def AffectedTextFiles(self, include_deletes=True):
29 return self.affected_files 30 return self.affected_files
30 31
32 def ReadFile(self, file):
33 self._test.failIf(file.LocalPath().endswith('notsource'))
34 return file.file_contents
35
31 36
32 class MockAffectedFile(object): 37 class MockAffectedFile(object):
33 def __init__(self, path, action='A'): 38 def __init__(self, path, action='A'):
34 self.path = path 39 self.path = path
35 self.action = action 40 self.action = action
36 41
37 def Action(self): 42 def Action(self):
38 return self.action 43 return self.action
39 44
40 def LocalPath(self): 45 def LocalPath(self):
41 return self.path 46 return self.path
42 47
43 def AbsoluteLocalPath(self): 48 def AbsoluteLocalPath(self):
44 return self.path 49 return self.path
45 50
46 51
47 class MockOutputApi(object): 52 class MockOutputApi(object):
48 class PresubmitError(object): 53 class PresubmitError(object):
49 def __init__(self, msg, items=[], long_text=''): 54 def __init__(self, msg, items=[], long_text=''):
50 self.msg = msg 55 self.msg = msg
51 self.items = items 56 self.items = items
52 57
53 58
54 class PresubmitUnittest(unittest.TestCase): 59 class PresubmitUnittest(unittest.TestCase):
55 def setUp(self):
56 self.file_contents = ''
57 def MockReadFile(path):
58 self.failIf(path.endswith('notsource'))
59 return self.file_contents
60 self._ReadFile = PRESUBMIT.ReadFile
61 PRESUBMIT.ReadFile = MockReadFile
62
63 def tearDown(self):
64 PRESUBMIT.ReadFile = self._ReadFile
65
66 def testLocalChecks(self): 60 def testLocalChecks(self):
67 api = MockInputApi() 61 api = MockInputApi(self)
68 api.affected_files = [ 62 api.affected_files = [
69 MockAffectedFile('foo/blat/yoo.notsource'), 63 MockAffectedFile('foo/blat/yoo.notsource'),
70 MockAffectedFile('third_party/blat/source.cc'), 64 MockAffectedFile('third_party/blat/source.cc'),
71 MockAffectedFile('foo/blat/source.h'), 65 MockAffectedFile('foo/blat/source.h'),
72 MockAffectedFile('foo/blat/source.mm'), 66 MockAffectedFile('foo/blat/source.mm'),
73 MockAffectedFile('foo/blat/source.py'), 67 MockAffectedFile('foo/blat/source.py'),
74 ] 68 ]
75 self.file_contents = 'file with \n\terror\nhere\r\nyes there' 69 for item in api.affected_files:
70 item.file_contents = 'file with \n\terror\nhere\r\nyes there'
76 # 3 source files, 2 errors by file + 1 global CR + 1 global EOF error. 71 # 3 source files, 2 errors by file + 1 global CR + 1 global EOF error.
77 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 8) 72 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 8)
78 73
79 self.file_contents = 'file\twith\ttabs\n' 74 for item in api.affected_files:
75 item.file_contents = 'file\twith\ttabs\n'
80 # 3 source files, 1 error by file. 76 # 3 source files, 1 error by file.
81 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 3) 77 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 3)
82 78
83 self.file_contents = 'file\rusing\rCRs\n' 79 for item in api.affected_files:
80 item.file_contents = 'file\rusing\rCRs\n'
84 # One global CR error. 81 # One global CR error.
85 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 1) 82 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 1)
86 self.failUnless( 83 self.failUnless(
87 len(PRESUBMIT.LocalChecks(api, MockOutputApi)[0].items) == 3) 84 len(PRESUBMIT.LocalChecks(api, MockOutputApi)[0].items) == 3)
88 85
89 self.file_contents = 'both\ttabs and\r\nCRLF\n' 86 for item in api.affected_files:
87 item.file_contents = 'both\ttabs and\r\nCRLF\n'
90 # 3 source files, 1 error by file + 1 global CR error. 88 # 3 source files, 1 error by file + 1 global CR error.
91 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 4) 89 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 4)
92 90
93 self.file_contents = 'file with\nzero \\t errors \\r\\n\n' 91 for item in api.affected_files:
92 item.file_contents = 'file with\nzero \\t errors \\r\\n\n'
94 self.failIf(PRESUBMIT.LocalChecks(api, MockOutputApi)) 93 self.failIf(PRESUBMIT.LocalChecks(api, MockOutputApi))
95 94
96 def testLocalChecksDeletedFile(self): 95 def testLocalChecksDeletedFile(self):
97 api = MockInputApi() 96 api = MockInputApi(self)
98 api.affected_files = [ 97 api.affected_files = [
99 MockAffectedFile('foo/blat/source.py', 'D'), 98 MockAffectedFile('foo/blat/source.py', 'D'),
100 ] 99 ]
101 self.file_contents = 'file with \n\terror\nhere\r\nyes there' 100 api.affected_files[0].file_contents = (
101 'file with \n\terror\nhere\r\nyes there')
102 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 0) 102 self.failUnless(len(PRESUBMIT.LocalChecks(api, MockOutputApi)) == 0)
103 103
104 104
105 if __name__ == '__main__': 105 if __name__ == '__main__':
106 unittest.main() 106 unittest.main()
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698