[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 | |
| 63 | class IncludeOrderTest(unittest.TestCase): |
| 64 | def testSystemHeaderOrder(self): |
| 65 | scope = [(1, '#include <csystem.h>'), |
| 66 | (2, '#include <cppsystem>'), |
| 67 | (3, '#include "acustom.h"')] |
| 68 | all_linenums = [linenum for (linenum, _) in scope] |
| 69 | mock_input_api = MockInputApi() |
| 70 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 71 | '', all_linenums) |
| 72 | self.assertEqual(0, len(warnings)) |
| 73 | |
| 74 | def testSystemHeaderOrderMismatch1(self): |
| 75 | scope = [(10, '#include <cppsystem>'), |
| 76 | (20, '#include <csystem.h>'), |
| 77 | (30, '#include "acustom.h"')] |
| 78 | all_linenums = [linenum for (linenum, _) in scope] |
| 79 | mock_input_api = MockInputApi() |
| 80 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 81 | '', all_linenums) |
| 82 | self.assertEqual(1, len(warnings)) |
| 83 | self.assertTrue('20' in warnings[0]) |
| 84 | |
| 85 | def testSystemHeaderOrderMismatch2(self): |
| 86 | scope = [(10, '#include <cppsystem>'), |
| 87 | (20, '#include "acustom.h"'), |
| 88 | (30, '#include <csystem.h>')] |
| 89 | all_linenums = [linenum for (linenum, _) in scope] |
| 90 | mock_input_api = MockInputApi() |
| 91 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 92 | '', all_linenums) |
| 93 | self.assertEqual(1, len(warnings)) |
| 94 | self.assertTrue('30' in warnings[0]) |
| 95 | |
| 96 | def testSystemHeaderOrderMismatch3(self): |
| 97 | scope = [(10, '#include "acustom.h"'), |
| 98 | (20, '#include <csystem.h>'), |
| 99 | (30, '#include <cppsystem>')] |
| 100 | all_linenums = [linenum for (linenum, _) in scope] |
| 101 | mock_input_api = MockInputApi() |
| 102 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 103 | '', all_linenums) |
| 104 | self.assertEqual(2, len(warnings)) |
| 105 | self.assertTrue('20' in warnings[0]) |
| 106 | self.assertTrue('30' in warnings[1]) |
| 107 | |
| 108 | def testAlphabeticalOrderMismatch(self): |
| 109 | scope = [(10, '#include <csystem.h>'), |
| 110 | (15, '#include <bsystem.h>'), |
| 111 | (20, '#include <cppsystem>'), |
| 112 | (25, '#include <bppsystem>'), |
| 113 | (30, '#include "bcustom.h"'), |
| 114 | (35, '#include "acustom.h"')] |
| 115 | all_linenums = [linenum for (linenum, _) in scope] |
| 116 | mock_input_api = MockInputApi() |
| 117 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 118 | '', all_linenums) |
| 119 | self.assertEqual(3, len(warnings)) |
| 120 | self.assertTrue('15' in warnings[0]) |
| 121 | self.assertTrue('25' in warnings[1]) |
| 122 | self.assertTrue('35' in warnings[2]) |
| 123 | |
| 124 | def testSpecialFirstInclude1(self): |
| 125 | mock_input_api = MockInputApi() |
| 126 | contents = ['#include "some/path/foo.h"', |
| 127 | '#include "a/header.h"'] |
| 128 | mock_file = MockFile('some/path/foo.cc', contents) |
| 129 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 130 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 131 | self.assertEqual(0, len(warnings)) |
| 132 | |
| 133 | def testSpecialFirstInclude2(self): |
| 134 | mock_input_api = MockInputApi() |
| 135 | contents = ['#include "some/other/path/foo.h"', |
| 136 | '#include "a/header.h"'] |
| 137 | mock_file = MockFile('some/path/foo.cc', contents) |
| 138 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 139 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 140 | self.assertEqual(0, len(warnings)) |
| 141 | |
| 142 | def testSpecialFirstInclude3(self): |
| 143 | mock_input_api = MockInputApi() |
| 144 | contents = ['#include "some/path/foo.h"', |
| 145 | '#include "a/header.h"'] |
| 146 | mock_file = MockFile('some/path/foo_platform.cc', contents) |
| 147 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 148 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 149 | self.assertEqual(0, len(warnings)) |
| 150 | |
| 151 | def testSpecialFirstInclude4(self): |
| 152 | mock_input_api = MockInputApi() |
| 153 | contents = ['#include "some/path/bar.h"', |
| 154 | '#include "a/header.h"'] |
| 155 | mock_file = MockFile('some/path/foo_platform.cc', contents) |
| 156 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 157 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 158 | self.assertEqual(1, len(warnings)) |
| 159 | self.assertTrue('2' in warnings[0]) |
| 160 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 161 | def testSpecialFirstInclude5(self): |
| 162 | mock_input_api = MockInputApi() |
| 163 | contents = ['#include "some/other/path/foo.h"', |
| 164 | '#include "a/header.h"'] |
| 165 | mock_file = MockFile('some/path/foo-suffix.h', contents) |
| 166 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 167 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 168 | self.assertEqual(0, len(warnings)) |
| 169 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 170 | def testOrderAlreadyWrong(self): |
| 171 | scope = [(1, '#include "b.h"'), |
| 172 | (2, '#include "a.h"'), |
| 173 | (3, '#include "c.h"')] |
| 174 | mock_input_api = MockInputApi() |
| 175 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 176 | '', [3]) |
| 177 | self.assertEqual(0, len(warnings)) |
| 178 | |
| 179 | def testConflictAdded1(self): |
| 180 | scope = [(1, '#include "a.h"'), |
| 181 | (2, '#include "c.h"'), |
| 182 | (3, '#include "b.h"')] |
| 183 | mock_input_api = MockInputApi() |
| 184 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 185 | '', [2]) |
| 186 | self.assertEqual(1, len(warnings)) |
| 187 | self.assertTrue('3' in warnings[0]) |
| 188 | |
| 189 | def testConflictAdded2(self): |
| 190 | scope = [(1, '#include "c.h"'), |
| 191 | (2, '#include "b.h"'), |
| 192 | (3, '#include "d.h"')] |
| 193 | mock_input_api = MockInputApi() |
| 194 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 195 | '', [2]) |
| 196 | self.assertEqual(1, len(warnings)) |
| 197 | self.assertTrue('2' in warnings[0]) |
| 198 | |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 199 | def testIfElifElseEndif(self): |
| 200 | mock_input_api = MockInputApi() |
| 201 | contents = ['#include "e.h"', |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 202 | '#define foo', |
| 203 | '#include "f.h"', |
| 204 | '#undef foo', |
| 205 | '#include "e.h"', |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 206 | '#if foo', |
| 207 | '#include "d.h"', |
| 208 | '#elif bar', |
| 209 | '#include "c.h"', |
| 210 | '#else', |
| 211 | '#include "b.h"', |
| 212 | '#endif', |
| 213 | '#include "a.h"'] |
| 214 | mock_file = MockFile('', contents) |
| 215 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 216 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 217 | self.assertEqual(0, len(warnings)) |
| 218 | |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 219 | def testSysIncludes(self): |
| 220 | # #include <sys/...>'s can appear in any order. |
| 221 | mock_input_api = MockInputApi() |
| 222 | contents = ['#include <sys/b.h>', |
| 223 | '#include <sys/a.h>'] |
| 224 | mock_file = MockFile('', contents) |
| 225 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 226 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 227 | self.assertEqual(0, len(warnings)) |
| 228 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 229 | def testCheckOnlyCFiles(self): |
| 230 | mock_input_api = MockInputApi() |
| 231 | mock_output_api = MockOutputApi() |
| 232 | contents = ['#include <b.h>', |
| 233 | '#include <a.h>'] |
| 234 | mock_file_cc = MockFile('something.cc', contents) |
| 235 | mock_file_h = MockFile('something.h', contents) |
| 236 | mock_file_other = MockFile('something.py', contents) |
| 237 | mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] |
| 238 | warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) |
| 239 | self.assertEqual(1, len(warnings)) |
| 240 | self.assertEqual(2, len(warnings[0].items)) |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 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) |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 254 | |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame^] | 255 | def testUncheckableIncludes(self): |
| 256 | mock_input_api = MockInputApi() |
| 257 | contents = ['#include <windows.h>', |
| 258 | '#include "b.h"' |
| 259 | '#include "a.h"'] |
| 260 | mock_file = MockFile('', contents) |
| 261 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 262 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 263 | self.assertEqual(0, len(warnings)) |
| 264 | |
| 265 | contents = ['#include "gpu/command_buffer/gles_autogen.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 "gl_mock_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 "ipc/some_macros.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 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 289 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 290 | class VersionControlerConflictsTest(unittest.TestCase): |
| 291 | def testTypicalConflict(self): |
| 292 | lines = ['<<<<<<< HEAD', |
| 293 | ' base::ScopedTempDir temp_dir_;', |
| 294 | '=======', |
| 295 | ' ScopedTempDir temp_dir_;', |
| 296 | '>>>>>>> master'] |
| 297 | errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 298 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 299 | self.assertEqual(3, len(errors)) |
| 300 | self.assertTrue('1' in errors[0]) |
| 301 | self.assertTrue('3' in errors[1]) |
| 302 | self.assertTrue('5' in errors[2]) |
| 303 | |
| 304 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 305 | class BadExtensionsTest(unittest.TestCase): |
| 306 | def testBadRejFile(self): |
| 307 | mock_input_api = MockInputApi() |
| 308 | mock_input_api.files = [ |
| 309 | MockFile('some/path/foo.cc', ''), |
| 310 | MockFile('some/path/foo.cc.rej', ''), |
| 311 | MockFile('some/path2/bar.h.rej', ''), |
| 312 | ] |
| 313 | |
| 314 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 315 | self.assertEqual(1, len(results)) |
| 316 | self.assertEqual(2, len(results[0].items)) |
| 317 | self.assertTrue('foo.cc.rej' in results[0].items[0]) |
| 318 | self.assertTrue('bar.h.rej' in results[0].items[1]) |
| 319 | |
| 320 | def testBadOrigFile(self): |
| 321 | mock_input_api = MockInputApi() |
| 322 | mock_input_api.files = [ |
| 323 | MockFile('other/path/qux.h.orig', ''), |
| 324 | MockFile('other/path/qux.h', ''), |
| 325 | MockFile('other/path/qux.cc', ''), |
| 326 | ] |
| 327 | |
| 328 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 329 | self.assertEqual(1, len(results)) |
| 330 | self.assertEqual(1, len(results[0].items)) |
| 331 | self.assertTrue('qux.h.orig' in results[0].items[0]) |
| 332 | |
| 333 | def testGoodFiles(self): |
| 334 | mock_input_api = MockInputApi() |
| 335 | mock_input_api.files = [ |
| 336 | MockFile('other/path/qux.h', ''), |
| 337 | MockFile('other/path/qux.cc', ''), |
| 338 | ] |
| 339 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 340 | self.assertEqual(0, len(results)) |
| 341 | |
| 342 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 343 | if __name__ == '__main__': |
| 344 | unittest.main() |