| 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.path | 6 import os.path |
| 7 import re | 7 import re |
| 8 import subprocess | 8 import subprocess |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 import PRESUBMIT | 11 import PRESUBMIT |
| 12 from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile | 12 from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile |
| 13 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi | 13 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
| 14 | 14 |
| 15 _TEST_DATA_DIR = 'base/test/data/presubmit' | 15 _TEST_DATA_DIR = 'base/test/data/presubmit' |
| 16 | 16 |
| 17 class IncludeOrderTest(unittest.TestCase): | |
| 18 def testSystemHeaderOrder(self): | |
| 19 scope = [(1, '#include <csystem.h>'), | |
| 20 (2, '#include <cppsystem>'), | |
| 21 (3, '#include "acustom.h"')] | |
| 22 all_linenums = [linenum for (linenum, _) in scope] | |
| 23 mock_input_api = MockInputApi() | |
| 24 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 25 '', all_linenums) | |
| 26 self.assertEqual(0, len(warnings)) | |
| 27 | |
| 28 def testSystemHeaderOrderMismatch1(self): | |
| 29 scope = [(10, '#include <cppsystem>'), | |
| 30 (20, '#include <csystem.h>'), | |
| 31 (30, '#include "acustom.h"')] | |
| 32 all_linenums = [linenum for (linenum, _) in scope] | |
| 33 mock_input_api = MockInputApi() | |
| 34 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 35 '', all_linenums) | |
| 36 self.assertEqual(1, len(warnings)) | |
| 37 self.assertTrue('20' in warnings[0]) | |
| 38 | |
| 39 def testSystemHeaderOrderMismatch2(self): | |
| 40 scope = [(10, '#include <cppsystem>'), | |
| 41 (20, '#include "acustom.h"'), | |
| 42 (30, '#include <csystem.h>')] | |
| 43 all_linenums = [linenum for (linenum, _) in scope] | |
| 44 mock_input_api = MockInputApi() | |
| 45 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 46 '', all_linenums) | |
| 47 self.assertEqual(1, len(warnings)) | |
| 48 self.assertTrue('30' in warnings[0]) | |
| 49 | |
| 50 def testSystemHeaderOrderMismatch3(self): | |
| 51 scope = [(10, '#include "acustom.h"'), | |
| 52 (20, '#include <csystem.h>'), | |
| 53 (30, '#include <cppsystem>')] | |
| 54 all_linenums = [linenum for (linenum, _) in scope] | |
| 55 mock_input_api = MockInputApi() | |
| 56 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 57 '', all_linenums) | |
| 58 self.assertEqual(2, len(warnings)) | |
| 59 self.assertTrue('20' in warnings[0]) | |
| 60 self.assertTrue('30' in warnings[1]) | |
| 61 | |
| 62 def testAlphabeticalOrderMismatch(self): | |
| 63 scope = [(10, '#include <csystem.h>'), | |
| 64 (15, '#include <bsystem.h>'), | |
| 65 (20, '#include <cppsystem>'), | |
| 66 (25, '#include <bppsystem>'), | |
| 67 (30, '#include "bcustom.h"'), | |
| 68 (35, '#include "acustom.h"')] | |
| 69 all_linenums = [linenum for (linenum, _) in scope] | |
| 70 mock_input_api = MockInputApi() | |
| 71 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 72 '', all_linenums) | |
| 73 self.assertEqual(3, len(warnings)) | |
| 74 self.assertTrue('15' in warnings[0]) | |
| 75 self.assertTrue('25' in warnings[1]) | |
| 76 self.assertTrue('35' in warnings[2]) | |
| 77 | |
| 78 def testSpecialFirstInclude1(self): | |
| 79 mock_input_api = MockInputApi() | |
| 80 contents = ['#include "some/path/foo.h"', | |
| 81 '#include "a/header.h"'] | |
| 82 mock_file = MockFile('some/path/foo.cc', contents) | |
| 83 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 84 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 85 self.assertEqual(0, len(warnings)) | |
| 86 | |
| 87 def testSpecialFirstInclude2(self): | |
| 88 mock_input_api = MockInputApi() | |
| 89 contents = ['#include "some/other/path/foo.h"', | |
| 90 '#include "a/header.h"'] | |
| 91 mock_file = MockFile('some/path/foo.cc', contents) | |
| 92 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 93 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 94 self.assertEqual(0, len(warnings)) | |
| 95 | |
| 96 def testSpecialFirstInclude3(self): | |
| 97 mock_input_api = MockInputApi() | |
| 98 contents = ['#include "some/path/foo.h"', | |
| 99 '#include "a/header.h"'] | |
| 100 mock_file = MockFile('some/path/foo_platform.cc', contents) | |
| 101 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 102 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 103 self.assertEqual(0, len(warnings)) | |
| 104 | |
| 105 def testSpecialFirstInclude4(self): | |
| 106 mock_input_api = MockInputApi() | |
| 107 contents = ['#include "some/path/bar.h"', | |
| 108 '#include "a/header.h"'] | |
| 109 mock_file = MockFile('some/path/foo_platform.cc', contents) | |
| 110 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 111 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 112 self.assertEqual(1, len(warnings)) | |
| 113 self.assertTrue('2' in warnings[0]) | |
| 114 | |
| 115 def testSpecialFirstInclude5(self): | |
| 116 mock_input_api = MockInputApi() | |
| 117 contents = ['#include "some/other/path/foo.h"', | |
| 118 '#include "a/header.h"'] | |
| 119 mock_file = MockFile('some/path/foo-suffix.h', contents) | |
| 120 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 121 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 122 self.assertEqual(0, len(warnings)) | |
| 123 | |
| 124 def testSpecialFirstInclude6(self): | |
| 125 mock_input_api = MockInputApi() | |
| 126 contents = ['#include "some/other/path/foo_win.h"', | |
| 127 '#include <set>', | |
| 128 '#include "a/header.h"'] | |
| 129 mock_file = MockFile('some/path/foo_unittest_win.h', contents) | |
| 130 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 131 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 132 self.assertEqual(0, len(warnings)) | |
| 133 | |
| 134 def testOrderAlreadyWrong(self): | |
| 135 scope = [(1, '#include "b.h"'), | |
| 136 (2, '#include "a.h"'), | |
| 137 (3, '#include "c.h"')] | |
| 138 mock_input_api = MockInputApi() | |
| 139 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 140 '', [3]) | |
| 141 self.assertEqual(0, len(warnings)) | |
| 142 | |
| 143 def testConflictAdded1(self): | |
| 144 scope = [(1, '#include "a.h"'), | |
| 145 (2, '#include "c.h"'), | |
| 146 (3, '#include "b.h"')] | |
| 147 mock_input_api = MockInputApi() | |
| 148 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 149 '', [2]) | |
| 150 self.assertEqual(1, len(warnings)) | |
| 151 self.assertTrue('3' in warnings[0]) | |
| 152 | |
| 153 def testConflictAdded2(self): | |
| 154 scope = [(1, '#include "c.h"'), | |
| 155 (2, '#include "b.h"'), | |
| 156 (3, '#include "d.h"')] | |
| 157 mock_input_api = MockInputApi() | |
| 158 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
| 159 '', [2]) | |
| 160 self.assertEqual(1, len(warnings)) | |
| 161 self.assertTrue('2' in warnings[0]) | |
| 162 | |
| 163 def testIfElifElseEndif(self): | |
| 164 mock_input_api = MockInputApi() | |
| 165 contents = ['#include "e.h"', | |
| 166 '#define foo', | |
| 167 '#include "f.h"', | |
| 168 '#undef foo', | |
| 169 '#include "e.h"', | |
| 170 '#if foo', | |
| 171 '#include "d.h"', | |
| 172 '#elif bar', | |
| 173 '#include "c.h"', | |
| 174 '#else', | |
| 175 '#include "b.h"', | |
| 176 '#endif', | |
| 177 '#include "a.h"'] | |
| 178 mock_file = MockFile('', contents) | |
| 179 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 180 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 181 self.assertEqual(0, len(warnings)) | |
| 182 | |
| 183 def testExcludedIncludes(self): | |
| 184 # #include <sys/...>'s can appear in any order. | |
| 185 mock_input_api = MockInputApi() | |
| 186 contents = ['#include <sys/b.h>', | |
| 187 '#include <sys/a.h>'] | |
| 188 mock_file = MockFile('', contents) | |
| 189 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 190 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 191 self.assertEqual(0, len(warnings)) | |
| 192 | |
| 193 contents = ['#include <atlbase.h>', | |
| 194 '#include <aaa.h>'] | |
| 195 mock_file = MockFile('', contents) | |
| 196 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 197 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 198 self.assertEqual(0, len(warnings)) | |
| 199 | |
| 200 contents = ['#include "build/build_config.h"', | |
| 201 '#include "aaa.h"'] | |
| 202 mock_file = MockFile('', contents) | |
| 203 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 204 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 205 self.assertEqual(0, len(warnings)) | |
| 206 | |
| 207 def testCheckOnlyCFiles(self): | |
| 208 mock_input_api = MockInputApi() | |
| 209 mock_output_api = MockOutputApi() | |
| 210 contents = ['#include <b.h>', | |
| 211 '#include <a.h>'] | |
| 212 mock_file_cc = MockFile('something.cc', contents) | |
| 213 mock_file_h = MockFile('something.h', contents) | |
| 214 mock_file_other = MockFile('something.py', contents) | |
| 215 mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] | |
| 216 warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) | |
| 217 self.assertEqual(1, len(warnings)) | |
| 218 self.assertEqual(2, len(warnings[0].items)) | |
| 219 self.assertEqual('promptOrNotify', warnings[0].type) | |
| 220 | |
| 221 def testUncheckableIncludes(self): | |
| 222 mock_input_api = MockInputApi() | |
| 223 contents = ['#include <windows.h>', | |
| 224 '#include "b.h"', | |
| 225 '#include "a.h"'] | |
| 226 mock_file = MockFile('', contents) | |
| 227 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 228 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 229 self.assertEqual(1, len(warnings)) | |
| 230 | |
| 231 contents = ['#include "gpu/command_buffer/gles_autogen.h"', | |
| 232 '#include "b.h"', | |
| 233 '#include "a.h"'] | |
| 234 mock_file = MockFile('', contents) | |
| 235 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 236 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 237 self.assertEqual(1, len(warnings)) | |
| 238 | |
| 239 contents = ['#include "gl_mock_autogen.h"', | |
| 240 '#include "b.h"', | |
| 241 '#include "a.h"'] | |
| 242 mock_file = MockFile('', contents) | |
| 243 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 244 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 245 self.assertEqual(1, len(warnings)) | |
| 246 | |
| 247 contents = ['#include "ipc/some_macros.h"', | |
| 248 '#include "b.h"', | |
| 249 '#include "a.h"'] | |
| 250 mock_file = MockFile('', contents) | |
| 251 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
| 252 mock_input_api, mock_file, range(1, len(contents) + 1)) | |
| 253 self.assertEqual(1, len(warnings)) | |
| 254 | |
| 255 | |
| 256 class VersionControlConflictsTest(unittest.TestCase): | 17 class VersionControlConflictsTest(unittest.TestCase): |
| 257 def testTypicalConflict(self): | 18 def testTypicalConflict(self): |
| 258 lines = ['<<<<<<< HEAD', | 19 lines = ['<<<<<<< HEAD', |
| 259 ' base::ScopedTempDir temp_dir_;', | 20 ' base::ScopedTempDir temp_dir_;', |
| 260 '=======', | 21 '=======', |
| 261 ' ScopedTempDir temp_dir_;', | 22 ' ScopedTempDir temp_dir_;', |
| 262 '>>>>>>> master'] | 23 '>>>>>>> master'] |
| 263 errors = PRESUBMIT._CheckForVersionControlConflictsInFile( | 24 errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 264 MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) | 25 MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 265 self.assertEqual(3, len(errors)) | 26 self.assertEqual(3, len(errors)) |
| (...skipping 1116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1382 ] | 1143 ] |
| 1383 | 1144 |
| 1384 mock_output_api = MockOutputApi() | 1145 mock_output_api = MockOutputApi() |
| 1385 | 1146 |
| 1386 errors = PRESUBMIT._CheckForRelativeIncludes( | 1147 errors = PRESUBMIT._CheckForRelativeIncludes( |
| 1387 mock_input_api, mock_output_api) | 1148 mock_input_api, mock_output_api) |
| 1388 self.assertEqual(1, len(errors)) | 1149 self.assertEqual(1, len(errors)) |
| 1389 | 1150 |
| 1390 if __name__ == '__main__': | 1151 if __name__ == '__main__': |
| 1391 unittest.main() | 1152 unittest.main() |
| OLD | NEW |