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 glob | |
7 import json | |
8 import os | |
9 import re | 6 import re |
10 import subprocess | |
11 import sys | |
12 import unittest | 7 import unittest |
13 | 8 |
14 import PRESUBMIT | 9 import PRESUBMIT |
| 10 from PRESUBMIT_test_mocks import MockChange, MockFile |
| 11 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
15 | 12 |
16 | 13 |
17 _TEST_DATA_DIR = 'base/test/data/presubmit' | 14 _TEST_DATA_DIR = 'base/test/data/presubmit' |
18 | 15 |
19 | 16 |
20 class MockInputApi(object): | |
21 def __init__(self): | |
22 self.json = json | |
23 self.re = re | |
24 self.os_path = os.path | |
25 self.python_executable = sys.executable | |
26 self.subprocess = subprocess | |
27 self.files = [] | |
28 self.is_committing = False | |
29 | |
30 def AffectedFiles(self, file_filter=None): | |
31 return self.files | |
32 | |
33 def PresubmitLocalPath(self): | |
34 return os.path.dirname(__file__) | |
35 | |
36 def ReadFile(self, filename, mode='rU'): | |
37 for file_ in self.files: | |
38 if file_.LocalPath() == filename: | |
39 return '\n'.join(file_.NewContents()) | |
40 # Otherwise, file is not in our mock API. | |
41 raise IOError, "No such file or directory: '%s'" % filename | |
42 | |
43 | |
44 class MockOutputApi(object): | |
45 class PresubmitResult(object): | |
46 def __init__(self, message, items=None, long_text=''): | |
47 self.message = message | |
48 self.items = items | |
49 self.long_text = long_text | |
50 | |
51 class PresubmitError(PresubmitResult): | |
52 def __init__(self, message, items, long_text=''): | |
53 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | |
54 self.type = 'error' | |
55 | |
56 class PresubmitPromptWarning(PresubmitResult): | |
57 def __init__(self, message, items, long_text=''): | |
58 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | |
59 self.type = 'warning' | |
60 | |
61 class PresubmitNotifyResult(PresubmitResult): | |
62 def __init__(self, message, items, long_text=''): | |
63 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | |
64 self.type = 'notify' | |
65 | |
66 class PresubmitPromptOrNotify(PresubmitResult): | |
67 def __init__(self, message, items, long_text=''): | |
68 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) | |
69 self.type = 'promptOrNotify' | |
70 | |
71 | |
72 class MockFile(object): | |
73 def __init__(self, local_path, new_contents): | |
74 self._local_path = local_path | |
75 self._new_contents = new_contents | |
76 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] | |
77 | |
78 def ChangedContents(self): | |
79 return self._changed_contents | |
80 | |
81 def NewContents(self): | |
82 return self._new_contents | |
83 | |
84 def LocalPath(self): | |
85 return self._local_path | |
86 | |
87 | |
88 class MockChange(object): | |
89 def __init__(self, changed_files): | |
90 self._changed_files = changed_files | |
91 | |
92 def LocalPaths(self): | |
93 return self._changed_files | |
94 | |
95 | |
96 class IncludeOrderTest(unittest.TestCase): | 17 class IncludeOrderTest(unittest.TestCase): |
97 def testSystemHeaderOrder(self): | 18 def testSystemHeaderOrder(self): |
98 scope = [(1, '#include <csystem.h>'), | 19 scope = [(1, '#include <csystem.h>'), |
99 (2, '#include <cppsystem>'), | 20 (2, '#include <cppsystem>'), |
100 (3, '#include "acustom.h"')] | 21 (3, '#include "acustom.h"')] |
101 all_linenums = [linenum for (linenum, _) in scope] | 22 all_linenums = [linenum for (linenum, _) in scope] |
102 mock_input_api = MockInputApi() | 23 mock_input_api = MockInputApi() |
103 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | 24 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
104 '', all_linenums) | 25 '', all_linenums) |
105 self.assertEqual(0, len(warnings)) | 26 self.assertEqual(0, len(warnings)) |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 str(PRESUBMIT._GetJSONParseError(input_api, | 457 str(PRESUBMIT._GetJSONParseError(input_api, |
537 file_with_comments, | 458 file_with_comments, |
538 eat_comments=False))) | 459 eat_comments=False))) |
539 self.assertEqual(None, | 460 self.assertEqual(None, |
540 PRESUBMIT._GetJSONParseError(input_api, | 461 PRESUBMIT._GetJSONParseError(input_api, |
541 file_without_comments, | 462 file_without_comments, |
542 eat_comments=False)) | 463 eat_comments=False)) |
543 | 464 |
544 if __name__ == '__main__': | 465 if __name__ == '__main__': |
545 unittest.main() | 466 unittest.main() |
OLD | NEW |