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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 | 22 |
23 class MockOutputApi(object): | 23 class MockOutputApi(object): |
24 class PresubmitResult(object): | 24 class PresubmitResult(object): |
25 def __init__(self, message, items=None, long_text=''): | 25 def __init__(self, message, items=None, long_text=''): |
26 self.message = message | 26 self.message = message |
27 self.items = items | 27 self.items = items |
28 self.long_text = long_text | 28 self.long_text = long_text |
29 | 29 |
30 class PresubmitError(PresubmitResult): | 30 class PresubmitError(PresubmitResult): |
31 pass | 31 def __init__(self, message, items, long_text=''): |
| 32 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
32 | 33 |
33 class PresubmitPromptWarning(PresubmitResult): | 34 class PresubmitPromptWarning(PresubmitResult): |
34 pass | 35 def __init__(self, message, items, long_text=''): |
| 36 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
35 | 37 |
36 class PresubmitNotifyResult(PresubmitResult): | 38 class PresubmitNotifyResult(PresubmitResult): |
37 pass | 39 def __init__(self, message, items, long_text=''): |
| 40 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
38 | 41 |
39 | 42 |
40 class MockFile(object): | 43 class MockFile(object): |
41 def __init__(self, local_path, new_contents): | 44 def __init__(self, local_path, new_contents): |
42 self._local_path = local_path | 45 self._local_path = local_path |
43 self._new_contents = new_contents | 46 self._new_contents = new_contents |
44 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] | 47 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] |
45 | 48 |
46 def ChangedContents(self): | 49 def ChangedContents(self): |
47 return self._changed_contents | 50 return self._changed_contents |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 self.assertTrue('15' in warnings[0]) | 116 self.assertTrue('15' in warnings[0]) |
114 self.assertTrue('25' in warnings[1]) | 117 self.assertTrue('25' in warnings[1]) |
115 self.assertTrue('35' in warnings[2]) | 118 self.assertTrue('35' in warnings[2]) |
116 | 119 |
117 def testSpecialFirstInclude1(self): | 120 def testSpecialFirstInclude1(self): |
118 mock_input_api = MockInputApi() | 121 mock_input_api = MockInputApi() |
119 contents = ['#include "some/path/foo.h"', | 122 contents = ['#include "some/path/foo.h"', |
120 '#include "a/header.h"'] | 123 '#include "a/header.h"'] |
121 mock_file = MockFile('some/path/foo.cc', contents) | 124 mock_file = MockFile('some/path/foo.cc', contents) |
122 warnings = PRESUBMIT._CheckIncludeOrderInFile( | 125 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
123 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | 126 mock_input_api, mock_file, range(1, len(contents) + 1)) |
124 self.assertEqual(0, len(warnings)) | 127 self.assertEqual(0, len(warnings)) |
125 | 128 |
126 def testSpecialFirstInclude2(self): | 129 def testSpecialFirstInclude2(self): |
127 mock_input_api = MockInputApi() | 130 mock_input_api = MockInputApi() |
128 contents = ['#include "some/other/path/foo.h"', | 131 contents = ['#include "some/other/path/foo.h"', |
129 '#include "a/header.h"'] | 132 '#include "a/header.h"'] |
130 mock_file = MockFile('some/path/foo.cc', contents) | 133 mock_file = MockFile('some/path/foo.cc', contents) |
131 warnings = PRESUBMIT._CheckIncludeOrderInFile( | 134 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
132 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | 135 mock_input_api, mock_file, range(1, len(contents) + 1)) |
133 self.assertEqual(0, len(warnings)) | 136 self.assertEqual(0, len(warnings)) |
134 | 137 |
135 def testSpecialFirstInclude3(self): | 138 def testSpecialFirstInclude3(self): |
136 mock_input_api = MockInputApi() | 139 mock_input_api = MockInputApi() |
137 contents = ['#include "some/path/foo.h"', | 140 contents = ['#include "some/path/foo.h"', |
138 '#include "a/header.h"'] | 141 '#include "a/header.h"'] |
139 mock_file = MockFile('some/path/foo_platform.cc', contents) | 142 mock_file = MockFile('some/path/foo_platform.cc', contents) |
140 warnings = PRESUBMIT._CheckIncludeOrderInFile( | 143 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
141 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | 144 mock_input_api, mock_file, range(1, len(contents) + 1)) |
142 self.assertEqual(0, len(warnings)) | 145 self.assertEqual(0, len(warnings)) |
143 | 146 |
144 def testSpecialFirstInclude4(self): | 147 def testSpecialFirstInclude4(self): |
145 mock_input_api = MockInputApi() | 148 mock_input_api = MockInputApi() |
146 contents = ['#include "some/path/bar.h"', | 149 contents = ['#include "some/path/bar.h"', |
147 '#include "a/header.h"'] | 150 '#include "a/header.h"'] |
148 mock_file = MockFile('some/path/foo_platform.cc', contents) | 151 mock_file = MockFile('some/path/foo_platform.cc', contents) |
149 warnings = PRESUBMIT._CheckIncludeOrderInFile( | 152 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
150 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | 153 mock_input_api, mock_file, range(1, len(contents) + 1)) |
151 self.assertEqual(1, len(warnings)) | 154 self.assertEqual(1, len(warnings)) |
152 self.assertTrue('2' in warnings[0]) | 155 self.assertTrue('2' in warnings[0]) |
153 | 156 |
| 157 def testSpecialFirstInclude5(self): |
| 158 mock_input_api = MockInputApi() |
| 159 contents = ['#include "some/other/path/foo.h"', |
| 160 '#include "a/header.h"'] |
| 161 mock_file = MockFile('some/path/foo-suffix.h', contents) |
| 162 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 163 mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 164 self.assertEqual(0, len(warnings)) |
| 165 |
154 def testOrderAlreadyWrong(self): | 166 def testOrderAlreadyWrong(self): |
155 scope = [(1, '#include "b.h"'), | 167 scope = [(1, '#include "b.h"'), |
156 (2, '#include "a.h"'), | 168 (2, '#include "a.h"'), |
157 (3, '#include "c.h"')] | 169 (3, '#include "c.h"')] |
158 mock_input_api = MockInputApi() | 170 mock_input_api = MockInputApi() |
159 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | 171 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
160 '', [3]) | 172 '', [3]) |
161 self.assertEqual(0, len(warnings)) | 173 self.assertEqual(0, len(warnings)) |
162 | 174 |
163 def testConflictAdded1(self): | 175 def testConflictAdded1(self): |
(...skipping 12 matching lines...) Expand all Loading... |
176 (3, '#include "d.h"')] | 188 (3, '#include "d.h"')] |
177 mock_input_api = MockInputApi() | 189 mock_input_api = MockInputApi() |
178 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | 190 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
179 '', [2]) | 191 '', [2]) |
180 self.assertEqual(1, len(warnings)) | 192 self.assertEqual(1, len(warnings)) |
181 self.assertTrue('2' in warnings[0]) | 193 self.assertTrue('2' in warnings[0]) |
182 | 194 |
183 def testIfElifElseEndif(self): | 195 def testIfElifElseEndif(self): |
184 mock_input_api = MockInputApi() | 196 mock_input_api = MockInputApi() |
185 contents = ['#include "e.h"', | 197 contents = ['#include "e.h"', |
| 198 '#define foo', |
| 199 '#include "f.h"', |
| 200 '#undef foo', |
| 201 '#include "e.h"', |
186 '#if foo', | 202 '#if foo', |
187 '#include "d.h"', | 203 '#include "d.h"', |
188 '#elif bar', | 204 '#elif bar', |
189 '#include "c.h"', | 205 '#include "c.h"', |
190 '#else', | 206 '#else', |
191 '#include "b.h"', | 207 '#include "b.h"', |
192 '#endif', | 208 '#endif', |
193 '#include "a.h"'] | 209 '#include "a.h"'] |
194 mock_file = MockFile('', contents) | 210 mock_file = MockFile('', contents) |
195 warnings = PRESUBMIT._CheckIncludeOrderInFile( | 211 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
196 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | 212 mock_input_api, mock_file, range(1, len(contents) + 1)) |
197 self.assertEqual(0, len(warnings)) | 213 self.assertEqual(0, len(warnings)) |
198 | 214 |
199 def testSysIncludes(self): | 215 def testSysIncludes(self): |
200 # #include <sys/...>'s can appear in any order. | 216 # #include <sys/...>'s can appear in any order. |
201 mock_input_api = MockInputApi() | 217 mock_input_api = MockInputApi() |
202 contents = ['#include <sys/b.h>', | 218 contents = ['#include <sys/b.h>', |
203 '#include <sys/a.h>'] | 219 '#include <sys/a.h>'] |
204 mock_file = MockFile('', contents) | 220 mock_file = MockFile('', contents) |
205 warnings = PRESUBMIT._CheckIncludeOrderInFile( | 221 warnings = PRESUBMIT._CheckIncludeOrderInFile( |
206 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | 222 mock_input_api, mock_file, range(1, len(contents) + 1)) |
207 self.assertEqual(0, len(warnings)) | 223 self.assertEqual(0, len(warnings)) |
208 | 224 |
| 225 def testCheckOnlyCFiles(self): |
| 226 mock_input_api = MockInputApi() |
| 227 mock_output_api = MockOutputApi() |
| 228 contents = ['#include <b.h>', |
| 229 '#include <a.h>'] |
| 230 mock_file_cc = MockFile('something.cc', contents) |
| 231 mock_file_h = MockFile('something.h', contents) |
| 232 mock_file_other = MockFile('something.py', contents) |
| 233 mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] |
| 234 warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) |
| 235 self.assertEqual(1, len(warnings)) |
| 236 self.assertEqual(2, len(warnings[0].items)) |
| 237 |
209 | 238 |
210 class VersionControlerConflictsTest(unittest.TestCase): | 239 class VersionControlerConflictsTest(unittest.TestCase): |
211 def testTypicalConflict(self): | 240 def testTypicalConflict(self): |
212 lines = ['<<<<<<< HEAD', | 241 lines = ['<<<<<<< HEAD', |
213 ' base::ScopedTempDir temp_dir_;', | 242 ' base::ScopedTempDir temp_dir_;', |
214 '=======', | 243 '=======', |
215 ' ScopedTempDir temp_dir_;', | 244 ' ScopedTempDir temp_dir_;', |
216 '>>>>>>> master'] | 245 '>>>>>>> master'] |
217 errors = PRESUBMIT._CheckForVersionControlConflictsInFile( | 246 errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
218 MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) | 247 MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 mock_input_api.files = [ | 284 mock_input_api.files = [ |
256 MockFile('other/path/qux.h', ''), | 285 MockFile('other/path/qux.h', ''), |
257 MockFile('other/path/qux.cc', ''), | 286 MockFile('other/path/qux.cc', ''), |
258 ] | 287 ] |
259 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) | 288 results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
260 self.assertEqual(0, len(results)) | 289 self.assertEqual(0, len(results)) |
261 | 290 |
262 | 291 |
263 if __name__ == '__main__': | 292 if __name__ == '__main__': |
264 unittest.main() | 293 unittest.main() |
OLD | NEW |