[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 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. |
| 5 | |
| 6 | import os |
| 7 | import re |
| 8 | import unittest |
| 9 | |
| 10 | import PRESUBMIT |
| 11 | |
| 12 | |
| 13 | class MockInputApi(object): |
| 14 | def __init__(self): |
| 15 | self.re = re |
| 16 | self.os_path = os.path |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 17 | self.files = [] |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 18 | self.is_committing = False |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 19 | |
| 20 | def AffectedFiles(self): |
| 21 | return self.files |
| 22 | |
| 23 | |
| 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 |
| 30 | |
| 31 | class PresubmitError(PresubmitResult): |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 32 | def __init__(self, message, items, long_text=''): |
| 33 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 34 | self.type = 'error' |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 35 | |
| 36 | class PresubmitPromptWarning(PresubmitResult): |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 37 | def __init__(self, message, items, long_text=''): |
| 38 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 39 | self.type = 'warning' |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 40 | |
| 41 | class PresubmitNotifyResult(PresubmitResult): |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 42 | def __init__(self, message, items, long_text=''): |
| 43 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 44 | self.type = 'notify' |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 45 | |
| 46 | |
| 47 | class MockFile(object): |
| 48 | def __init__(self, local_path, new_contents): |
| 49 | self._local_path = local_path |
| 50 | self._new_contents = new_contents |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 51 | self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] |
| 52 | |
| 53 | def ChangedContents(self): |
| 54 | return self._changed_contents |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 55 | |
| 56 | def NewContents(self): |
| 57 | return self._new_contents |
| 58 | |
| 59 | def LocalPath(self): |
| 60 | return self._local_path |
| 61 | |
| 62 | |
[email protected] | 751b05f | 2013-01-10 23:12:17 | [diff] [blame] | 63 | class MockChange(object): |
| 64 | def __init__(self, changed_files): |
| 65 | self._changed_files = changed_files |
| 66 | |
| 67 | def LocalPaths(self): |
| 68 | return self._changed_files |
| 69 | |
| 70 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 71 | class IncludeOrderTest(unittest.TestCase): |
| 72 | def testSystemHeaderOrder(self): |
| 73 | scope = [(1, '#include <csystem.h>'), |
| 74 | (2, '#include <cppsystem>'), |
| 75 | (3, '#include "acustom.h"')] |
| 76 | all_linenums = [linenum for (linenum, _) in scope] |
| 77 | mock_input_api = MockInputApi() |
| 78 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 79 | '', all_linenums) |
| 80 | self.assertEqual(0, len(warnings)) |
| 81 | |
| 82 | def testSystemHeaderOrderMismatch1(self): |
| 83 | scope = [(10, '#include <cppsystem>'), |
| 84 | (20, '#include <csystem.h>'), |
| 85 | (30, '#include "acustom.h"')] |
| 86 | all_linenums = [linenum for (linenum, _) in scope] |
| 87 | mock_input_api = MockInputApi() |
| 88 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 89 | '', all_linenums) |
| 90 | self.assertEqual(1, len(warnings)) |
| 91 | self.assertTrue('20' in warnings[0]) |
| 92 | |
| 93 | def testSystemHeaderOrderMismatch2(self): |
| 94 | scope = [(10, '#include <cppsystem>'), |
| 95 | (20, '#include "acustom.h"'), |
| 96 | (30, '#include <csystem.h>')] |
| 97 | all_linenums = [linenum for (linenum, _) in scope] |
| 98 | mock_input_api = MockInputApi() |
| 99 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 100 | '', all_linenums) |
| 101 | self.assertEqual(1, len(warnings)) |
| 102 | self.assertTrue('30' in warnings[0]) |
| 103 | |
| 104 | def testSystemHeaderOrderMismatch3(self): |
| 105 | scope = [(10, '#include "acustom.h"'), |
| 106 | (20, '#include <csystem.h>'), |
| 107 | (30, '#include <cppsystem>')] |
| 108 | all_linenums = [linenum for (linenum, _) in scope] |
| 109 | mock_input_api = MockInputApi() |
| 110 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 111 | '', all_linenums) |
| 112 | self.assertEqual(2, len(warnings)) |
| 113 | self.assertTrue('20' in warnings[0]) |
| 114 | self.assertTrue('30' in warnings[1]) |
| 115 | |
| 116 | def testAlphabeticalOrderMismatch(self): |
| 117 | scope = [(10, '#include <csystem.h>'), |
| 118 | (15, '#include <bsystem.h>'), |
| 119 | (20, '#include <cppsystem>'), |
| 120 | (25, '#include <bppsystem>'), |
| 121 | (30, '#include "bcustom.h"'), |
| 122 | (35, '#include "acustom.h"')] |
| 123 | all_linenums = [linenum for (linenum, _) in scope] |
| 124 | mock_input_api = MockInputApi() |
| 125 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 126 | '', all_linenums) |
| 127 | self.assertEqual(3, len(warnings)) |
| 128 | self.assertTrue('15' in warnings[0]) |
| 129 | self.assertTrue('25' in warnings[1]) |
| 130 | self.assertTrue('35' in warnings[2]) |
| 131 | |
| 132 | def testSpecialFirstInclude1(self): |
| 133 | mock_input_api = MockInputApi() |
| 134 | contents = ['#include "some/path/foo.h"', |
| 135 | '#include "a/header.h"'] |
| 136 | mock_file = MockFile('some/path/foo.cc', contents) |
| 137 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 138 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 139 | self.assertEqual(0, len(warnings)) |
| 140 | |
| 141 | def testSpecialFirstInclude2(self): |
| 142 | mock_input_api = MockInputApi() |
| 143 | contents = ['#include "some/other/path/foo.h"', |
| 144 | '#include "a/header.h"'] |
| 145 | mock_file = MockFile('some/path/foo.cc', contents) |
| 146 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 147 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 148 | self.assertEqual(0, len(warnings)) |
| 149 | |
| 150 | def testSpecialFirstInclude3(self): |
| 151 | mock_input_api = MockInputApi() |
| 152 | contents = ['#include "some/path/foo.h"', |
| 153 | '#include "a/header.h"'] |
| 154 | mock_file = MockFile('some/path/foo_platform.cc', contents) |
| 155 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 156 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 157 | self.assertEqual(0, len(warnings)) |
| 158 | |
| 159 | def testSpecialFirstInclude4(self): |
| 160 | mock_input_api = MockInputApi() |
| 161 | contents = ['#include "some/path/bar.h"', |
| 162 | '#include "a/header.h"'] |
| 163 | mock_file = MockFile('some/path/foo_platform.cc', contents) |
| 164 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 165 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 166 | self.assertEqual(1, len(warnings)) |
| 167 | self.assertTrue('2' in warnings[0]) |
| 168 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 169 | def testSpecialFirstInclude5(self): |
| 170 | mock_input_api = MockInputApi() |
| 171 | contents = ['#include "some/other/path/foo.h"', |
| 172 | '#include "a/header.h"'] |
| 173 | mock_file = MockFile('some/path/foo-suffix.h', contents) |
| 174 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 175 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 176 | self.assertEqual(0, len(warnings)) |
| 177 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 178 | def testOrderAlreadyWrong(self): |
| 179 | scope = [(1, '#include "b.h"'), |
| 180 | (2, '#include "a.h"'), |
| 181 | (3, '#include "c.h"')] |
| 182 | mock_input_api = MockInputApi() |
| 183 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 184 | '', [3]) |
| 185 | self.assertEqual(0, len(warnings)) |
| 186 | |
| 187 | def testConflictAdded1(self): |
| 188 | scope = [(1, '#include "a.h"'), |
| 189 | (2, '#include "c.h"'), |
| 190 | (3, '#include "b.h"')] |
| 191 | mock_input_api = MockInputApi() |
| 192 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 193 | '', [2]) |
| 194 | self.assertEqual(1, len(warnings)) |
| 195 | self.assertTrue('3' in warnings[0]) |
| 196 | |
| 197 | def testConflictAdded2(self): |
| 198 | scope = [(1, '#include "c.h"'), |
| 199 | (2, '#include "b.h"'), |
| 200 | (3, '#include "d.h"')] |
| 201 | mock_input_api = MockInputApi() |
| 202 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 203 | '', [2]) |
| 204 | self.assertEqual(1, len(warnings)) |
| 205 | self.assertTrue('2' in warnings[0]) |
| 206 | |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 207 | def testIfElifElseEndif(self): |
| 208 | mock_input_api = MockInputApi() |
| 209 | contents = ['#include "e.h"', |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 210 | '#define foo', |
| 211 | '#include "f.h"', |
| 212 | '#undef foo', |
| 213 | '#include "e.h"', |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 214 | '#if foo', |
| 215 | '#include "d.h"', |
| 216 | '#elif bar', |
| 217 | '#include "c.h"', |
| 218 | '#else', |
| 219 | '#include "b.h"', |
| 220 | '#endif', |
| 221 | '#include "a.h"'] |
| 222 | mock_file = MockFile('', contents) |
| 223 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 224 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 225 | self.assertEqual(0, len(warnings)) |
| 226 | |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 227 | def testSysIncludes(self): |
| 228 | # #include <sys/...>'s can appear in any order. |
| 229 | mock_input_api = MockInputApi() |
| 230 | contents = ['#include <sys/b.h>', |
| 231 | '#include <sys/a.h>'] |
| 232 | mock_file = MockFile('', contents) |
| 233 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 234 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 235 | self.assertEqual(0, len(warnings)) |
| 236 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 237 | def testCheckOnlyCFiles(self): |
| 238 | mock_input_api = MockInputApi() |
| 239 | mock_output_api = MockOutputApi() |
| 240 | contents = ['#include <b.h>', |
| 241 | '#include <a.h>'] |
| 242 | mock_file_cc = MockFile('something.cc', contents) |
| 243 | mock_file_h = MockFile('something.h', contents) |
| 244 | mock_file_other = MockFile('something.py', contents) |
| 245 | mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] |
| 246 | warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) |
| 247 | self.assertEqual(1, len(warnings)) |
| 248 | self.assertEqual(2, len(warnings[0].items)) |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 249 | self.assertEqual('warning', warnings[0].type) |
| 250 | |
| 251 | def testOnlyNotifyOnCommit(self): |
| 252 | mock_input_api = MockInputApi() |
| 253 | mock_input_api.is_committing = True |
| 254 | mock_output_api = MockOutputApi() |
| 255 | contents = ['#include <b.h>', |
| 256 | '#include <a.h>'] |
| 257 | mock_input_api.files = [MockFile('something.cc', contents)] |
| 258 | warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) |
| 259 | self.assertEqual(1, len(warnings)) |
| 260 | self.assertEqual(1, len(warnings[0].items)) |
| 261 | self.assertEqual('notify', warnings[0].type) |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 262 | |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 263 | def testUncheckableIncludes(self): |
| 264 | mock_input_api = MockInputApi() |
| 265 | contents = ['#include <windows.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)) |
| 272 | |
| 273 | contents = ['#include "gpu/command_buffer/gles_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)) |
| 280 | |
| 281 | contents = ['#include "gl_mock_autogen.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)) |
| 288 | |
| 289 | contents = ['#include "ipc/some_macros.h"', |
| 290 | '#include "b.h"' |
| 291 | '#include "a.h"'] |
| 292 | mock_file = MockFile('', contents) |
| 293 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 294 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 295 | self.assertEqual(0, len(warnings)) |
| 296 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 297 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 298 | class VersionControlerConflictsTest(unittest.TestCase): |
| 299 | def testTypicalConflict(self): |
| 300 | lines = ['<<<<<<< HEAD', |
| 301 | ' base::ScopedTempDir temp_dir_;', |
| 302 | '=======', |
| 303 | ' ScopedTempDir temp_dir_;', |
| 304 | '>>>>>>> master'] |
| 305 | errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 306 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 307 | self.assertEqual(3, len(errors)) |
| 308 | self.assertTrue('1' in errors[0]) |
| 309 | self.assertTrue('3' in errors[1]) |
| 310 | self.assertTrue('5' in errors[2]) |
| 311 | |
| 312 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 313 | class BadExtensionsTest(unittest.TestCase): |
| 314 | def testBadRejFile(self): |
| 315 | mock_input_api = MockInputApi() |
| 316 | mock_input_api.files = [ |
| 317 | MockFile('some/path/foo.cc', ''), |
| 318 | MockFile('some/path/foo.cc.rej', ''), |
| 319 | MockFile('some/path2/bar.h.rej', ''), |
| 320 | ] |
| 321 | |
| 322 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 323 | self.assertEqual(1, len(results)) |
| 324 | self.assertEqual(2, len(results[0].items)) |
| 325 | self.assertTrue('foo.cc.rej' in results[0].items[0]) |
| 326 | self.assertTrue('bar.h.rej' in results[0].items[1]) |
| 327 | |
| 328 | def testBadOrigFile(self): |
| 329 | mock_input_api = MockInputApi() |
| 330 | mock_input_api.files = [ |
| 331 | MockFile('other/path/qux.h.orig', ''), |
| 332 | MockFile('other/path/qux.h', ''), |
| 333 | MockFile('other/path/qux.cc', ''), |
| 334 | ] |
| 335 | |
| 336 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 337 | self.assertEqual(1, len(results)) |
| 338 | self.assertEqual(1, len(results[0].items)) |
| 339 | self.assertTrue('qux.h.orig' in results[0].items[0]) |
| 340 | |
| 341 | def testGoodFiles(self): |
| 342 | mock_input_api = MockInputApi() |
| 343 | mock_input_api.files = [ |
| 344 | MockFile('other/path/qux.h', ''), |
| 345 | MockFile('other/path/qux.cc', ''), |
| 346 | ] |
| 347 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 348 | self.assertEqual(0, len(results)) |
| 349 | |
[email protected] | 751b05f | 2013-01-10 23:12:17 | [diff] [blame] | 350 | def testOnlyOwnersFiles(self): |
| 351 | mock_change = MockChange([ |
| 352 | 'some/path/OWNERS', |
| 353 | 'A\Windows\Path\OWNERS', |
| 354 | ]) |
| 355 | results = PRESUBMIT.GetPreferredTrySlaves(None, mock_change) |
| 356 | self.assertEqual(0, len(results)) |
| 357 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 358 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 359 | if __name__ == '__main__': |
| 360 | unittest.main() |