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

Side by Side Diff: PRESUBMIT_unittest.py

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