Add tests for redirect responses from SafeBrowsingProtocolManager.
[chromium-blink-merge.git] / PRESUBMIT_test.py
blob979ad892f1a58278ef1f248fd462ffc67b7824ee
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.
6 import os
7 import re
8 import unittest
10 import PRESUBMIT
13 class MockInputApi(object):
14 def __init__(self):
15 self.re = re
16 self.os_path = os.path
17 self.files = []
18 self.is_committing = False
20 def AffectedFiles(self):
21 return self.files
24 class MockOutputApi(object):
25 class PresubmitResult(object):
26 def __init__(self, message, items=None, long_text=''):
27 self.message = message
28 self.items = items
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)
34 self.type = 'error'
36 class PresubmitPromptWarning(PresubmitResult):
37 def __init__(self, message, items, long_text=''):
38 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
39 self.type = 'warning'
41 class PresubmitNotifyResult(PresubmitResult):
42 def __init__(self, message, items, long_text=''):
43 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
44 self.type = 'notify'
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
59 def LocalPath(self):
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,
71 '', all_linenums)
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,
81 '', all_linenums)
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,
92 '', all_linenums)
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,
103 '', all_linenums)
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,
118 '', all_linenums)
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,
176 '', [3])
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,
185 '', [2])
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,
195 '', [2])
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"',
202 '#define foo',
203 '#include "f.h"',
204 '#undef foo',
205 '#include "e.h"',
206 '#if foo',
207 '#include "d.h"',
208 '#elif bar',
209 '#include "c.h"',
210 '#else',
211 '#include "b.h"',
212 '#endif',
213 '#include "a.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>',
233 '#include <a.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>',
248 '#include <a.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)
255 def testUncheckableIncludes(self):
256 mock_input_api = MockInputApi()
257 contents = ['#include <windows.h>',
258 '#include "b.h"'
259 '#include "a.h"']
260 mock_file = MockFile('', contents)
261 warnings = PRESUBMIT._CheckIncludeOrderInFile(
262 mock_input_api, mock_file, range(1, len(contents) + 1))
263 self.assertEqual(0, len(warnings))
265 contents = ['#include "gpu/command_buffer/gles_autogen.h"',
266 '#include "b.h"'
267 '#include "a.h"']
268 mock_file = MockFile('', contents)
269 warnings = PRESUBMIT._CheckIncludeOrderInFile(
270 mock_input_api, mock_file, range(1, len(contents) + 1))
271 self.assertEqual(0, len(warnings))
273 contents = ['#include "gl_mock_autogen.h"',
274 '#include "b.h"'
275 '#include "a.h"']
276 mock_file = MockFile('', contents)
277 warnings = PRESUBMIT._CheckIncludeOrderInFile(
278 mock_input_api, mock_file, range(1, len(contents) + 1))
279 self.assertEqual(0, len(warnings))
281 contents = ['#include "ipc/some_macros.h"',
282 '#include "b.h"'
283 '#include "a.h"']
284 mock_file = MockFile('', contents)
285 warnings = PRESUBMIT._CheckIncludeOrderInFile(
286 mock_input_api, mock_file, range(1, len(contents) + 1))
287 self.assertEqual(0, len(warnings))
290 class VersionControlerConflictsTest(unittest.TestCase):
291 def testTypicalConflict(self):
292 lines = ['<<<<<<< HEAD',
293 ' base::ScopedTempDir temp_dir_;',
294 '=======',
295 ' ScopedTempDir temp_dir_;',
296 '>>>>>>> master']
297 errors = PRESUBMIT._CheckForVersionControlConflictsInFile(
298 MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
299 self.assertEqual(3, len(errors))
300 self.assertTrue('1' in errors[0])
301 self.assertTrue('3' in errors[1])
302 self.assertTrue('5' in errors[2])
305 class BadExtensionsTest(unittest.TestCase):
306 def testBadRejFile(self):
307 mock_input_api = MockInputApi()
308 mock_input_api.files = [
309 MockFile('some/path/foo.cc', ''),
310 MockFile('some/path/foo.cc.rej', ''),
311 MockFile('some/path2/bar.h.rej', ''),
314 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
315 self.assertEqual(1, len(results))
316 self.assertEqual(2, len(results[0].items))
317 self.assertTrue('foo.cc.rej' in results[0].items[0])
318 self.assertTrue('bar.h.rej' in results[0].items[1])
320 def testBadOrigFile(self):
321 mock_input_api = MockInputApi()
322 mock_input_api.files = [
323 MockFile('other/path/qux.h.orig', ''),
324 MockFile('other/path/qux.h', ''),
325 MockFile('other/path/qux.cc', ''),
328 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
329 self.assertEqual(1, len(results))
330 self.assertEqual(1, len(results[0].items))
331 self.assertTrue('qux.h.orig' in results[0].items[0])
333 def testGoodFiles(self):
334 mock_input_api = MockInputApi()
335 mock_input_api.files = [
336 MockFile('other/path/qux.h', ''),
337 MockFile('other/path/qux.cc', ''),
339 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
340 self.assertEqual(0, len(results))
343 if __name__ == '__main__':
344 unittest.main()