Web Speech API: Fix a race condition causing renderer crash.
[chromium-blink-merge.git] / PRESUBMIT_test.py
blob63500a5de7b8bf647ca2606e0767fe44e521c321
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'
46 class PresubmitPromptOrNotify(PresubmitResult):
47 def __init__(self, message, items, long_text=''):
48 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
49 self.type = 'promptOrNotify'
52 class MockFile(object):
53 def __init__(self, local_path, new_contents):
54 self._local_path = local_path
55 self._new_contents = new_contents
56 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
58 def ChangedContents(self):
59 return self._changed_contents
61 def NewContents(self):
62 return self._new_contents
64 def LocalPath(self):
65 return self._local_path
68 class MockChange(object):
69 def __init__(self, changed_files):
70 self._changed_files = changed_files
72 def LocalPaths(self):
73 return self._changed_files
76 class IncludeOrderTest(unittest.TestCase):
77 def testSystemHeaderOrder(self):
78 scope = [(1, '#include <csystem.h>'),
79 (2, '#include <cppsystem>'),
80 (3, '#include "acustom.h"')]
81 all_linenums = [linenum for (linenum, _) in scope]
82 mock_input_api = MockInputApi()
83 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
84 '', all_linenums)
85 self.assertEqual(0, len(warnings))
87 def testSystemHeaderOrderMismatch1(self):
88 scope = [(10, '#include <cppsystem>'),
89 (20, '#include <csystem.h>'),
90 (30, '#include "acustom.h"')]
91 all_linenums = [linenum for (linenum, _) in scope]
92 mock_input_api = MockInputApi()
93 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
94 '', all_linenums)
95 self.assertEqual(1, len(warnings))
96 self.assertTrue('20' in warnings[0])
98 def testSystemHeaderOrderMismatch2(self):
99 scope = [(10, '#include <cppsystem>'),
100 (20, '#include "acustom.h"'),
101 (30, '#include <csystem.h>')]
102 all_linenums = [linenum for (linenum, _) in scope]
103 mock_input_api = MockInputApi()
104 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
105 '', all_linenums)
106 self.assertEqual(1, len(warnings))
107 self.assertTrue('30' in warnings[0])
109 def testSystemHeaderOrderMismatch3(self):
110 scope = [(10, '#include "acustom.h"'),
111 (20, '#include <csystem.h>'),
112 (30, '#include <cppsystem>')]
113 all_linenums = [linenum for (linenum, _) in scope]
114 mock_input_api = MockInputApi()
115 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
116 '', all_linenums)
117 self.assertEqual(2, len(warnings))
118 self.assertTrue('20' in warnings[0])
119 self.assertTrue('30' in warnings[1])
121 def testAlphabeticalOrderMismatch(self):
122 scope = [(10, '#include <csystem.h>'),
123 (15, '#include <bsystem.h>'),
124 (20, '#include <cppsystem>'),
125 (25, '#include <bppsystem>'),
126 (30, '#include "bcustom.h"'),
127 (35, '#include "acustom.h"')]
128 all_linenums = [linenum for (linenum, _) in scope]
129 mock_input_api = MockInputApi()
130 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
131 '', all_linenums)
132 self.assertEqual(3, len(warnings))
133 self.assertTrue('15' in warnings[0])
134 self.assertTrue('25' in warnings[1])
135 self.assertTrue('35' in warnings[2])
137 def testSpecialFirstInclude1(self):
138 mock_input_api = MockInputApi()
139 contents = ['#include "some/path/foo.h"',
140 '#include "a/header.h"']
141 mock_file = MockFile('some/path/foo.cc', contents)
142 warnings = PRESUBMIT._CheckIncludeOrderInFile(
143 mock_input_api, mock_file, range(1, len(contents) + 1))
144 self.assertEqual(0, len(warnings))
146 def testSpecialFirstInclude2(self):
147 mock_input_api = MockInputApi()
148 contents = ['#include "some/other/path/foo.h"',
149 '#include "a/header.h"']
150 mock_file = MockFile('some/path/foo.cc', contents)
151 warnings = PRESUBMIT._CheckIncludeOrderInFile(
152 mock_input_api, mock_file, range(1, len(contents) + 1))
153 self.assertEqual(0, len(warnings))
155 def testSpecialFirstInclude3(self):
156 mock_input_api = MockInputApi()
157 contents = ['#include "some/path/foo.h"',
158 '#include "a/header.h"']
159 mock_file = MockFile('some/path/foo_platform.cc', contents)
160 warnings = PRESUBMIT._CheckIncludeOrderInFile(
161 mock_input_api, mock_file, range(1, len(contents) + 1))
162 self.assertEqual(0, len(warnings))
164 def testSpecialFirstInclude4(self):
165 mock_input_api = MockInputApi()
166 contents = ['#include "some/path/bar.h"',
167 '#include "a/header.h"']
168 mock_file = MockFile('some/path/foo_platform.cc', contents)
169 warnings = PRESUBMIT._CheckIncludeOrderInFile(
170 mock_input_api, mock_file, range(1, len(contents) + 1))
171 self.assertEqual(1, len(warnings))
172 self.assertTrue('2' in warnings[0])
174 def testSpecialFirstInclude5(self):
175 mock_input_api = MockInputApi()
176 contents = ['#include "some/other/path/foo.h"',
177 '#include "a/header.h"']
178 mock_file = MockFile('some/path/foo-suffix.h', contents)
179 warnings = PRESUBMIT._CheckIncludeOrderInFile(
180 mock_input_api, mock_file, range(1, len(contents) + 1))
181 self.assertEqual(0, len(warnings))
183 def testOrderAlreadyWrong(self):
184 scope = [(1, '#include "b.h"'),
185 (2, '#include "a.h"'),
186 (3, '#include "c.h"')]
187 mock_input_api = MockInputApi()
188 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
189 '', [3])
190 self.assertEqual(0, len(warnings))
192 def testConflictAdded1(self):
193 scope = [(1, '#include "a.h"'),
194 (2, '#include "c.h"'),
195 (3, '#include "b.h"')]
196 mock_input_api = MockInputApi()
197 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
198 '', [2])
199 self.assertEqual(1, len(warnings))
200 self.assertTrue('3' in warnings[0])
202 def testConflictAdded2(self):
203 scope = [(1, '#include "c.h"'),
204 (2, '#include "b.h"'),
205 (3, '#include "d.h"')]
206 mock_input_api = MockInputApi()
207 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
208 '', [2])
209 self.assertEqual(1, len(warnings))
210 self.assertTrue('2' in warnings[0])
212 def testIfElifElseEndif(self):
213 mock_input_api = MockInputApi()
214 contents = ['#include "e.h"',
215 '#define foo',
216 '#include "f.h"',
217 '#undef foo',
218 '#include "e.h"',
219 '#if foo',
220 '#include "d.h"',
221 '#elif bar',
222 '#include "c.h"',
223 '#else',
224 '#include "b.h"',
225 '#endif',
226 '#include "a.h"']
227 mock_file = MockFile('', contents)
228 warnings = PRESUBMIT._CheckIncludeOrderInFile(
229 mock_input_api, mock_file, range(1, len(contents) + 1))
230 self.assertEqual(0, len(warnings))
232 def testExcludedIncludes(self):
233 # #include <sys/...>'s can appear in any order.
234 mock_input_api = MockInputApi()
235 contents = ['#include <sys/b.h>',
236 '#include <sys/a.h>']
237 mock_file = MockFile('', contents)
238 warnings = PRESUBMIT._CheckIncludeOrderInFile(
239 mock_input_api, mock_file, range(1, len(contents) + 1))
240 self.assertEqual(0, len(warnings))
242 contents = ['#include <atlbase.h>',
243 '#include <aaa.h>']
244 mock_file = MockFile('', contents)
245 warnings = PRESUBMIT._CheckIncludeOrderInFile(
246 mock_input_api, mock_file, range(1, len(contents) + 1))
247 self.assertEqual(0, len(warnings))
249 contents = ['#include "build/build_config.h"',
250 '#include "aaa.h"']
251 mock_file = MockFile('', contents)
252 warnings = PRESUBMIT._CheckIncludeOrderInFile(
253 mock_input_api, mock_file, range(1, len(contents) + 1))
254 self.assertEqual(0, len(warnings))
256 def testCheckOnlyCFiles(self):
257 mock_input_api = MockInputApi()
258 mock_output_api = MockOutputApi()
259 contents = ['#include <b.h>',
260 '#include <a.h>']
261 mock_file_cc = MockFile('something.cc', contents)
262 mock_file_h = MockFile('something.h', contents)
263 mock_file_other = MockFile('something.py', contents)
264 mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other]
265 warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api)
266 self.assertEqual(1, len(warnings))
267 self.assertEqual(2, len(warnings[0].items))
268 self.assertEqual('promptOrNotify', warnings[0].type)
270 def testUncheckableIncludes(self):
271 mock_input_api = MockInputApi()
272 contents = ['#include <windows.h>',
273 '#include "b.h"'
274 '#include "a.h"']
275 mock_file = MockFile('', contents)
276 warnings = PRESUBMIT._CheckIncludeOrderInFile(
277 mock_input_api, mock_file, range(1, len(contents) + 1))
278 self.assertEqual(0, len(warnings))
280 contents = ['#include "gpu/command_buffer/gles_autogen.h"',
281 '#include "b.h"'
282 '#include "a.h"']
283 mock_file = MockFile('', contents)
284 warnings = PRESUBMIT._CheckIncludeOrderInFile(
285 mock_input_api, mock_file, range(1, len(contents) + 1))
286 self.assertEqual(0, len(warnings))
288 contents = ['#include "gl_mock_autogen.h"',
289 '#include "b.h"'
290 '#include "a.h"']
291 mock_file = MockFile('', contents)
292 warnings = PRESUBMIT._CheckIncludeOrderInFile(
293 mock_input_api, mock_file, range(1, len(contents) + 1))
294 self.assertEqual(0, len(warnings))
296 contents = ['#include "ipc/some_macros.h"',
297 '#include "b.h"'
298 '#include "a.h"']
299 mock_file = MockFile('', contents)
300 warnings = PRESUBMIT._CheckIncludeOrderInFile(
301 mock_input_api, mock_file, range(1, len(contents) + 1))
302 self.assertEqual(0, len(warnings))
305 class VersionControlConflictsTest(unittest.TestCase):
306 def testTypicalConflict(self):
307 lines = ['<<<<<<< HEAD',
308 ' base::ScopedTempDir temp_dir_;',
309 '=======',
310 ' ScopedTempDir temp_dir_;',
311 '>>>>>>> master']
312 errors = PRESUBMIT._CheckForVersionControlConflictsInFile(
313 MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
314 self.assertEqual(3, len(errors))
315 self.assertTrue('1' in errors[0])
316 self.assertTrue('3' in errors[1])
317 self.assertTrue('5' in errors[2])
320 class BadExtensionsTest(unittest.TestCase):
321 def testBadRejFile(self):
322 mock_input_api = MockInputApi()
323 mock_input_api.files = [
324 MockFile('some/path/foo.cc', ''),
325 MockFile('some/path/foo.cc.rej', ''),
326 MockFile('some/path2/bar.h.rej', ''),
329 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
330 self.assertEqual(1, len(results))
331 self.assertEqual(2, len(results[0].items))
332 self.assertTrue('foo.cc.rej' in results[0].items[0])
333 self.assertTrue('bar.h.rej' in results[0].items[1])
335 def testBadOrigFile(self):
336 mock_input_api = MockInputApi()
337 mock_input_api.files = [
338 MockFile('other/path/qux.h.orig', ''),
339 MockFile('other/path/qux.h', ''),
340 MockFile('other/path/qux.cc', ''),
343 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
344 self.assertEqual(1, len(results))
345 self.assertEqual(1, len(results[0].items))
346 self.assertTrue('qux.h.orig' in results[0].items[0])
348 def testGoodFiles(self):
349 mock_input_api = MockInputApi()
350 mock_input_api.files = [
351 MockFile('other/path/qux.h', ''),
352 MockFile('other/path/qux.cc', ''),
354 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi())
355 self.assertEqual(0, len(results))
357 def testOnlyOwnersFiles(self):
358 mock_change = MockChange([
359 'some/path/OWNERS',
360 'A\Windows\Path\OWNERS',
362 results = PRESUBMIT.GetPreferredTrySlaves(None, mock_change)
363 self.assertEqual(0, len(results))
366 class InvalidOSMacroNamesTest(unittest.TestCase):
367 def testInvalidOSMacroNames(self):
368 lines = ['#if defined(OS_WINDOWS)',
369 ' #elif defined(OS_WINDOW)',
370 ' # if defined(OS_MACOSX) || defined(OS_CHROME)',
371 '# else // defined(OS_MAC)',
372 '#endif // defined(OS_MACOS)']
373 errors = PRESUBMIT._CheckForInvalidOSMacrosInFile(
374 MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
375 self.assertEqual(len(lines), len(errors))
376 self.assertTrue(':1 OS_WINDOWS' in errors[0])
377 self.assertTrue('(did you mean OS_WIN?)' in errors[0])
379 def testValidOSMacroNames(self):
380 lines = ['#if defined(%s)' % m for m in PRESUBMIT._VALID_OS_MACROS]
381 errors = PRESUBMIT._CheckForInvalidOSMacrosInFile(
382 MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
383 self.assertEqual(0, len(errors))
386 class CheckAddedDepsHaveTetsApprovalsTest(unittest.TestCase):
387 def testDepsFilesToCheck(self):
388 changed_lines = [
389 '"+breakpad",',
390 '"+chrome/installer",',
391 '"+chrome/plugin/chrome_content_plugin_client.h",',
392 '"+chrome/utility/chrome_content_utility_client.h",',
393 '"+chromeos/chromeos_paths.h",',
394 '"+components/breakpad",',
395 '"+components/nacl/common",',
396 '"+content/public/browser/render_process_host.h",',
397 '"+grit", # For generated headers',
398 '"+grit/generated_resources.h",',
399 '"+grit/",',
400 '"+policy", # For generated headers and source',
401 '"+sandbox",',
402 '"+tools/memory_watcher",',
403 '"+third_party/lss/linux_syscall_support.h",',
405 files_to_check = PRESUBMIT._DepsFilesToCheck(re, changed_lines)
406 expected = set([
407 'breakpad/DEPS',
408 'chrome/installer/DEPS',
409 'chrome/plugin/DEPS',
410 'chrome/utility/DEPS',
411 'chromeos/DEPS',
412 'components/breakpad/DEPS',
413 'components/nacl/common/DEPS',
414 'content/public/browser/DEPS',
415 'policy/DEPS',
416 'sandbox/DEPS',
417 'tools/memory_watcher/DEPS',
418 'third_party/lss/DEPS',
420 self.assertEqual(expected, files_to_check);
423 if __name__ == '__main__':
424 unittest.main()