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.
13 class MockInputApi(object):
16 self
.os_path
= os
.path
18 self
.is_committing
= False
20 def AffectedFiles(self
):
24 class MockOutputApi(object):
25 class PresubmitResult(object):
26 def __init__(self
, message
, items
=None, long_text
=''):
27 self
.message
= message
29 self
.long_text
= long_text
31 class PresubmitError(PresubmitResult
):
32 def __init__(self
, message
, items
, long_text
=''):
33 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
36 class PresubmitPromptWarning(PresubmitResult
):
37 def __init__(self
, message
, items
, long_text
=''):
38 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
41 class PresubmitNotifyResult(PresubmitResult
):
42 def __init__(self
, message
, items
, long_text
=''):
43 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
47 class MockFile(object):
48 def __init__(self
, local_path
, new_contents
):
49 self
._local
_path
= local_path
50 self
._new
_contents
= new_contents
51 self
._changed
_contents
= [(i
+ 1, l
) for i
, l
in enumerate(new_contents
)]
53 def ChangedContents(self
):
54 return self
._changed
_contents
56 def NewContents(self
):
57 return self
._new
_contents
60 return self
._local
_path
63 class IncludeOrderTest(unittest
.TestCase
):
64 def testSystemHeaderOrder(self
):
65 scope
= [(1, '#include <csystem.h>'),
66 (2, '#include <cppsystem>'),
67 (3, '#include "acustom.h"')]
68 all_linenums
= [linenum
for (linenum
, _
) in scope
]
69 mock_input_api
= MockInputApi()
70 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
72 self
.assertEqual(0, len(warnings
))
74 def testSystemHeaderOrderMismatch1(self
):
75 scope
= [(10, '#include <cppsystem>'),
76 (20, '#include <csystem.h>'),
77 (30, '#include "acustom.h"')]
78 all_linenums
= [linenum
for (linenum
, _
) in scope
]
79 mock_input_api
= MockInputApi()
80 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
82 self
.assertEqual(1, len(warnings
))
83 self
.assertTrue('20' in warnings
[0])
85 def testSystemHeaderOrderMismatch2(self
):
86 scope
= [(10, '#include <cppsystem>'),
87 (20, '#include "acustom.h"'),
88 (30, '#include <csystem.h>')]
89 all_linenums
= [linenum
for (linenum
, _
) in scope
]
90 mock_input_api
= MockInputApi()
91 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
93 self
.assertEqual(1, len(warnings
))
94 self
.assertTrue('30' in warnings
[0])
96 def testSystemHeaderOrderMismatch3(self
):
97 scope
= [(10, '#include "acustom.h"'),
98 (20, '#include <csystem.h>'),
99 (30, '#include <cppsystem>')]
100 all_linenums
= [linenum
for (linenum
, _
) in scope
]
101 mock_input_api
= MockInputApi()
102 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
104 self
.assertEqual(2, len(warnings
))
105 self
.assertTrue('20' in warnings
[0])
106 self
.assertTrue('30' in warnings
[1])
108 def testAlphabeticalOrderMismatch(self
):
109 scope
= [(10, '#include <csystem.h>'),
110 (15, '#include <bsystem.h>'),
111 (20, '#include <cppsystem>'),
112 (25, '#include <bppsystem>'),
113 (30, '#include "bcustom.h"'),
114 (35, '#include "acustom.h"')]
115 all_linenums
= [linenum
for (linenum
, _
) in scope
]
116 mock_input_api
= MockInputApi()
117 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
119 self
.assertEqual(3, len(warnings
))
120 self
.assertTrue('15' in warnings
[0])
121 self
.assertTrue('25' in warnings
[1])
122 self
.assertTrue('35' in warnings
[2])
124 def testSpecialFirstInclude1(self
):
125 mock_input_api
= MockInputApi()
126 contents
= ['#include "some/path/foo.h"',
127 '#include "a/header.h"']
128 mock_file
= MockFile('some/path/foo.cc', contents
)
129 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
130 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
131 self
.assertEqual(0, len(warnings
))
133 def testSpecialFirstInclude2(self
):
134 mock_input_api
= MockInputApi()
135 contents
= ['#include "some/other/path/foo.h"',
136 '#include "a/header.h"']
137 mock_file
= MockFile('some/path/foo.cc', contents
)
138 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
139 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
140 self
.assertEqual(0, len(warnings
))
142 def testSpecialFirstInclude3(self
):
143 mock_input_api
= MockInputApi()
144 contents
= ['#include "some/path/foo.h"',
145 '#include "a/header.h"']
146 mock_file
= MockFile('some/path/foo_platform.cc', contents
)
147 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
148 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
149 self
.assertEqual(0, len(warnings
))
151 def testSpecialFirstInclude4(self
):
152 mock_input_api
= MockInputApi()
153 contents
= ['#include "some/path/bar.h"',
154 '#include "a/header.h"']
155 mock_file
= MockFile('some/path/foo_platform.cc', contents
)
156 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
157 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
158 self
.assertEqual(1, len(warnings
))
159 self
.assertTrue('2' in warnings
[0])
161 def testSpecialFirstInclude5(self
):
162 mock_input_api
= MockInputApi()
163 contents
= ['#include "some/other/path/foo.h"',
164 '#include "a/header.h"']
165 mock_file
= MockFile('some/path/foo-suffix.h', contents
)
166 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
167 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
168 self
.assertEqual(0, len(warnings
))
170 def testOrderAlreadyWrong(self
):
171 scope
= [(1, '#include "b.h"'),
172 (2, '#include "a.h"'),
173 (3, '#include "c.h"')]
174 mock_input_api
= MockInputApi()
175 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
177 self
.assertEqual(0, len(warnings
))
179 def testConflictAdded1(self
):
180 scope
= [(1, '#include "a.h"'),
181 (2, '#include "c.h"'),
182 (3, '#include "b.h"')]
183 mock_input_api
= MockInputApi()
184 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
186 self
.assertEqual(1, len(warnings
))
187 self
.assertTrue('3' in warnings
[0])
189 def testConflictAdded2(self
):
190 scope
= [(1, '#include "c.h"'),
191 (2, '#include "b.h"'),
192 (3, '#include "d.h"')]
193 mock_input_api
= MockInputApi()
194 warnings
= PRESUBMIT
._CheckIncludeOrderForScope
(scope
, mock_input_api
,
196 self
.assertEqual(1, len(warnings
))
197 self
.assertTrue('2' in warnings
[0])
199 def testIfElifElseEndif(self
):
200 mock_input_api
= MockInputApi()
201 contents
= ['#include "e.h"',
214 mock_file
= MockFile('', contents
)
215 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
216 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
217 self
.assertEqual(0, len(warnings
))
219 def testSysIncludes(self
):
220 # #include <sys/...>'s can appear in any order.
221 mock_input_api
= MockInputApi()
222 contents
= ['#include <sys/b.h>',
223 '#include <sys/a.h>']
224 mock_file
= MockFile('', contents
)
225 warnings
= PRESUBMIT
._CheckIncludeOrderInFile
(
226 mock_input_api
, mock_file
, range(1, len(contents
) + 1))
227 self
.assertEqual(0, len(warnings
))
229 def testCheckOnlyCFiles(self
):
230 mock_input_api
= MockInputApi()
231 mock_output_api
= MockOutputApi()
232 contents
= ['#include <b.h>',
234 mock_file_cc
= MockFile('something.cc', contents
)
235 mock_file_h
= MockFile('something.h', contents
)
236 mock_file_other
= MockFile('something.py', contents
)
237 mock_input_api
.files
= [mock_file_cc
, mock_file_h
, mock_file_other
]
238 warnings
= PRESUBMIT
._CheckIncludeOrder
(mock_input_api
, mock_output_api
)
239 self
.assertEqual(1, len(warnings
))
240 self
.assertEqual(2, len(warnings
[0].items
))
241 self
.assertEqual('warning', warnings
[0].type)
243 def testOnlyNotifyOnCommit(self
):
244 mock_input_api
= MockInputApi()
245 mock_input_api
.is_committing
= True
246 mock_output_api
= MockOutputApi()
247 contents
= ['#include <b.h>',
249 mock_input_api
.files
= [MockFile('something.cc', contents
)]
250 warnings
= PRESUBMIT
._CheckIncludeOrder
(mock_input_api
, mock_output_api
)
251 self
.assertEqual(1, len(warnings
))
252 self
.assertEqual(1, len(warnings
[0].items
))
253 self
.assertEqual('notify', warnings
[0].type)
256 class VersionControlerConflictsTest(unittest
.TestCase
):
257 def testTypicalConflict(self
):
258 lines
= ['<<<<<<< HEAD',
259 ' base::ScopedTempDir temp_dir_;',
261 ' ScopedTempDir temp_dir_;',
263 errors
= PRESUBMIT
._CheckForVersionControlConflictsInFile
(
264 MockInputApi(), MockFile('some/path/foo_platform.cc', lines
))
265 self
.assertEqual(3, len(errors
))
266 self
.assertTrue('1' in errors
[0])
267 self
.assertTrue('3' in errors
[1])
268 self
.assertTrue('5' in errors
[2])
271 class BadExtensionsTest(unittest
.TestCase
):
272 def testBadRejFile(self
):
273 mock_input_api
= MockInputApi()
274 mock_input_api
.files
= [
275 MockFile('some/path/foo.cc', ''),
276 MockFile('some/path/foo.cc.rej', ''),
277 MockFile('some/path2/bar.h.rej', ''),
280 results
= PRESUBMIT
._CheckPatchFiles
(mock_input_api
, MockOutputApi())
281 self
.assertEqual(1, len(results
))
282 self
.assertEqual(2, len(results
[0].items
))
283 self
.assertTrue('foo.cc.rej' in results
[0].items
[0])
284 self
.assertTrue('bar.h.rej' in results
[0].items
[1])
286 def testBadOrigFile(self
):
287 mock_input_api
= MockInputApi()
288 mock_input_api
.files
= [
289 MockFile('other/path/qux.h.orig', ''),
290 MockFile('other/path/qux.h', ''),
291 MockFile('other/path/qux.cc', ''),
294 results
= PRESUBMIT
._CheckPatchFiles
(mock_input_api
, MockOutputApi())
295 self
.assertEqual(1, len(results
))
296 self
.assertEqual(1, len(results
[0].items
))
297 self
.assertTrue('qux.h.orig' in results
[0].items
[0])
299 def testGoodFiles(self
):
300 mock_input_api
= MockInputApi()
301 mock_input_api
.files
= [
302 MockFile('other/path/qux.h', ''),
303 MockFile('other/path/qux.cc', ''),
305 results
= PRESUBMIT
._CheckPatchFiles
(mock_input_api
, MockOutputApi())
306 self
.assertEqual(0, len(results
))
309 if __name__
== '__main__':