OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 import os | 6 import os |
7 import re | 7 import re |
8 import unittest | 8 import unittest |
9 | 9 |
10 import PRESUBMIT | 10 import PRESUBMIT |
11 | 11 |
12 | 12 |
13 class MockInputApi(object): | 13 class MockInputApi(object): |
14 def __init__(self): | 14 def __init__(self): |
15 self.re = re | 15 self.re = re |
16 self.os_path = os.path | 16 self.os_path = os.path |
17 self.files = [] | 17 self.files = [] |
| 18 self.is_committing = False |
18 | 19 |
19 def AffectedFiles(self): | 20 def AffectedFiles(self): |
20 return self.files | 21 return self.files |
21 | 22 |
22 | 23 |
23 class MockOutputApi(object): | 24 class MockOutputApi(object): |
24 class PresubmitResult(object): | 25 class PresubmitResult(object): |
25 def __init__(self, message, items=None, long_text=''): | 26 def __init__(self, message, items=None, long_text=''): |
26 self.message = message | 27 self.message = message |
27 self.items = items | 28 self.items = items |
28 self.long_text = long_text | 29 self.long_text = long_text |
29 | 30 |
30 class PresubmitError(PresubmitResult): | 31 class PresubmitError(PresubmitResult): |
31 def __init__(self, message, items, long_text=''): | 32 def __init__(self, message, items, long_text=''): |
32 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | 33 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 34 self.type = 'error' |
33 | 35 |
34 class PresubmitPromptWarning(PresubmitResult): | 36 class PresubmitPromptWarning(PresubmitResult): |
35 def __init__(self, message, items, long_text=''): | 37 def __init__(self, message, items, long_text=''): |
36 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | 38 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 39 self.type = 'warning' |
37 | 40 |
38 class PresubmitNotifyResult(PresubmitResult): | 41 class PresubmitNotifyResult(PresubmitResult): |
39 def __init__(self, message, items, long_text=''): | 42 def __init__(self, message, items, long_text=''): |
40 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | 43 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 44 self.type = 'notify' |
41 | 45 |
42 | 46 |
43 class MockFile(object): | 47 class MockFile(object): |
44 def __init__(self, local_path, new_contents): | 48 def __init__(self, local_path, new_contents): |
45 self._local_path = local_path | 49 self._local_path = local_path |
46 self._new_contents = new_contents | 50 self._new_contents = new_contents |
47 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] | 51 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] |
48 | 52 |
49 def ChangedContents(self): | 53 def ChangedContents(self): |
50 return self._changed_contents | 54 return self._changed_contents |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 mock_output_api = MockOutputApi() | 231 mock_output_api = MockOutputApi() |
228 contents = ['#include <b.h>', | 232 contents = ['#include <b.h>', |
229 '#include <a.h>'] | 233 '#include <a.h>'] |
230 mock_file_cc = MockFile('something.cc', contents) | 234 mock_file_cc = MockFile('something.cc', contents) |
231 mock_file_h = MockFile('something.h', contents) | 235 mock_file_h = MockFile('something.h', contents) |
232 mock_file_other = MockFile('something.py', contents) | 236 mock_file_other = MockFile('something.py', contents) |
233 mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] | 237 mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] |
234 warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) | 238 warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) |
235 self.assertEqual(1, len(warnings)) | 239 self.assertEqual(1, len(warnings)) |
236 self.assertEqual(2, len(warnings[0].items)) | 240 self.assertEqual(2, len(warnings[0].items)) |
| 241 self.assertEqual('warning', warnings[0].type) |
| 242 |
| 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) |
237 | 254 |
238 | 255 |
239 class VersionControlerConflictsTest(unittest.TestCase): | 256 class VersionControlerConflictsTest(unittest.TestCase): |
240 def testTypicalConflict(self): | 257 def testTypicalConflict(self): |
241 lines = ['<<<<<<< HEAD', | 258 lines = ['<<<<<<< HEAD', |
242 ' base::ScopedTempDir temp_dir_;', | 259 ' base::ScopedTempDir temp_dir_;', |
243 '=======', | 260 '=======', |
244 ' ScopedTempDir temp_dir_;', | 261 ' ScopedTempDir temp_dir_;', |
245 '>>>>>>> master'] | 262 '>>>>>>> master'] |
246 errors = PRESUBMIT._CheckForVersionControlConflictsInFile( | 263 errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 mock_input_api.files = [ | 301 mock_input_api.files = [ |
285 MockFile('other/path/qux.h', ''), | 302 MockFile('other/path/qux.h', ''), |
286 MockFile('other/path/qux.cc', ''), | 303 MockFile('other/path/qux.cc', ''), |
287 ] | 304 ] |
288 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) | 305 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
289 self.assertEqual(0, len(results)) | 306 self.assertEqual(0, len(results)) |
290 | 307 |
291 | 308 |
292 if __name__ == '__main__': | 309 if __name__ == '__main__': |
293 unittest.main() | 310 unittest.main() |
OLD | NEW |