[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 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame^] | 6 | import os.path |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 7 | import re |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 8 | import subprocess |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 9 | import unittest |
| 10 | |
| 11 | import PRESUBMIT |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 12 | from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 13 | from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 14 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 15 | _TEST_DATA_DIR = 'base/test/data/presubmit' |
| 16 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 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( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 84 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 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( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 93 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 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( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 102 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 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( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 111 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 112 | self.assertEqual(1, len(warnings)) |
| 113 | self.assertTrue('2' in warnings[0]) |
| 114 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 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 | |
[email protected] | 3e83618c | 2013-10-09 22:32:33 | [diff] [blame] | 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 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 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 | |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 163 | def testIfElifElseEndif(self): |
| 164 | mock_input_api = MockInputApi() |
| 165 | contents = ['#include "e.h"', |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 166 | '#define foo', |
| 167 | '#include "f.h"', |
| 168 | '#undef foo', |
| 169 | '#include "e.h"', |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 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( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 180 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 181 | self.assertEqual(0, len(warnings)) |
| 182 | |
[email protected] | 23093b6 | 2013-09-20 12:16:30 | [diff] [blame] | 183 | def testExcludedIncludes(self): |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 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( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 190 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 191 | self.assertEqual(0, len(warnings)) |
| 192 | |
[email protected] | 23093b6 | 2013-09-20 12:16:30 | [diff] [blame] | 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 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 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)) |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 219 | self.assertEqual('promptOrNotify', warnings[0].type) |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 220 | |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 221 | def testUncheckableIncludes(self): |
| 222 | mock_input_api = MockInputApi() |
| 223 | contents = ['#include <windows.h>', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 224 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 225 | '#include "a.h"'] |
| 226 | mock_file = MockFile('', contents) |
| 227 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 228 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 229 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 230 | |
| 231 | contents = ['#include "gpu/command_buffer/gles_autogen.h"', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 232 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 233 | '#include "a.h"'] |
| 234 | mock_file = MockFile('', contents) |
| 235 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 236 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 237 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 238 | |
| 239 | contents = ['#include "gl_mock_autogen.h"', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 240 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 241 | '#include "a.h"'] |
| 242 | mock_file = MockFile('', contents) |
| 243 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 244 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 245 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 246 | |
| 247 | contents = ['#include "ipc/some_macros.h"', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 248 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 249 | '#include "a.h"'] |
| 250 | mock_file = MockFile('', contents) |
| 251 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 252 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 253 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 254 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 255 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 256 | class VersionControlConflictsTest(unittest.TestCase): |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 257 | def testTypicalConflict(self): |
| 258 | lines = ['<<<<<<< HEAD', |
| 259 | ' base::ScopedTempDir temp_dir_;', |
| 260 | '=======', |
| 261 | ' ScopedTempDir temp_dir_;', |
| 262 | '>>>>>>> master'] |
| 263 | errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 264 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 265 | self.assertEqual(3, len(errors)) |
| 266 | self.assertTrue('1' in errors[0]) |
| 267 | self.assertTrue('3' in errors[1]) |
| 268 | self.assertTrue('5' in errors[2]) |
| 269 | |
dbeam | 95c35a2f | 2015-06-02 01:40:23 | [diff] [blame] | 270 | def testIgnoresReadmes(self): |
| 271 | lines = ['A First Level Header', |
| 272 | '====================', |
| 273 | '', |
| 274 | 'A Second Level Header', |
| 275 | '---------------------'] |
| 276 | errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 277 | MockInputApi(), MockFile('some/polymer/README.md', lines)) |
| 278 | self.assertEqual(0, len(errors)) |
| 279 | |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 280 | class UmaHistogramChangeMatchedOrNotTest(unittest.TestCase): |
| 281 | def testTypicalCorrectlyMatchedChange(self): |
| 282 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 283 | diff_xml = ['<histogram name="Bla.Foo.Dummy"> </histogram>'] |
| 284 | mock_input_api = MockInputApi() |
| 285 | mock_input_api.files = [ |
| 286 | MockFile('some/path/foo.cc', diff_cc), |
| 287 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 288 | ] |
| 289 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 290 | MockOutputApi()) |
| 291 | self.assertEqual(0, len(warnings)) |
| 292 | |
| 293 | def testTypicalNotMatchedChange(self): |
| 294 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 295 | mock_input_api = MockInputApi() |
| 296 | mock_input_api.files = [MockFile('some/path/foo.cc', diff_cc)] |
| 297 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 298 | MockOutputApi()) |
| 299 | self.assertEqual(1, len(warnings)) |
| 300 | self.assertEqual('warning', warnings[0].type) |
| 301 | |
| 302 | def testTypicalNotMatchedChangeViaSuffixes(self): |
| 303 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 304 | diff_xml = ['<histogram_suffixes name="SuperHistogram">', |
| 305 | ' <suffix name="Dummy"/>', |
| 306 | ' <affected-histogram name="Snafu.Dummy"/>', |
| 307 | '</histogram>'] |
| 308 | mock_input_api = MockInputApi() |
| 309 | mock_input_api.files = [ |
| 310 | MockFile('some/path/foo.cc', diff_cc), |
| 311 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 312 | ] |
| 313 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 314 | MockOutputApi()) |
| 315 | self.assertEqual(1, len(warnings)) |
| 316 | self.assertEqual('warning', warnings[0].type) |
| 317 | |
| 318 | def testTypicalCorrectlyMatchedChangeViaSuffixes(self): |
| 319 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 320 | diff_xml = ['<histogram_suffixes name="SuperHistogram">', |
| 321 | ' <suffix name="Dummy"/>', |
| 322 | ' <affected-histogram name="Bla.Foo"/>', |
| 323 | '</histogram>'] |
| 324 | mock_input_api = MockInputApi() |
| 325 | mock_input_api.files = [ |
| 326 | MockFile('some/path/foo.cc', diff_cc), |
| 327 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 328 | ] |
| 329 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 330 | MockOutputApi()) |
| 331 | self.assertEqual(0, len(warnings)) |
| 332 | |
| 333 | def testTypicalCorrectlyMatchedChangeViaSuffixesWithSeparator(self): |
| 334 | diff_cc = ['UMA_HISTOGRAM_BOOL("Snafu_Dummy", true)'] |
| 335 | diff_xml = ['<histogram_suffixes name="SuperHistogram" separator="_">', |
| 336 | ' <suffix name="Dummy"/>', |
| 337 | ' <affected-histogram name="Snafu"/>', |
| 338 | '</histogram>'] |
| 339 | mock_input_api = MockInputApi() |
| 340 | mock_input_api.files = [ |
| 341 | MockFile('some/path/foo.cc', diff_cc), |
| 342 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 343 | ] |
| 344 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 345 | MockOutputApi()) |
| 346 | self.assertEqual(0, len(warnings)) |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 347 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 348 | class BadExtensionsTest(unittest.TestCase): |
| 349 | def testBadRejFile(self): |
| 350 | mock_input_api = MockInputApi() |
| 351 | mock_input_api.files = [ |
| 352 | MockFile('some/path/foo.cc', ''), |
| 353 | MockFile('some/path/foo.cc.rej', ''), |
| 354 | MockFile('some/path2/bar.h.rej', ''), |
| 355 | ] |
| 356 | |
| 357 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 358 | self.assertEqual(1, len(results)) |
| 359 | self.assertEqual(2, len(results[0].items)) |
| 360 | self.assertTrue('foo.cc.rej' in results[0].items[0]) |
| 361 | self.assertTrue('bar.h.rej' in results[0].items[1]) |
| 362 | |
| 363 | def testBadOrigFile(self): |
| 364 | mock_input_api = MockInputApi() |
| 365 | mock_input_api.files = [ |
| 366 | MockFile('other/path/qux.h.orig', ''), |
| 367 | MockFile('other/path/qux.h', ''), |
| 368 | MockFile('other/path/qux.cc', ''), |
| 369 | ] |
| 370 | |
| 371 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 372 | self.assertEqual(1, len(results)) |
| 373 | self.assertEqual(1, len(results[0].items)) |
| 374 | self.assertTrue('qux.h.orig' in results[0].items[0]) |
| 375 | |
| 376 | def testGoodFiles(self): |
| 377 | mock_input_api = MockInputApi() |
| 378 | mock_input_api.files = [ |
| 379 | MockFile('other/path/qux.h', ''), |
| 380 | MockFile('other/path/qux.cc', ''), |
| 381 | ] |
| 382 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 383 | self.assertEqual(0, len(results)) |
| 384 | |
| 385 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 386 | class CheckSingletonInHeadersTest(unittest.TestCase): |
| 387 | def testSingletonInArbitraryHeader(self): |
| 388 | diff_singleton_h = ['base::subtle::AtomicWord ' |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 389 | 'base::Singleton<Type, Traits, DifferentiatingType>::'] |
| 390 | diff_foo_h = ['// base::Singleton<Foo> in comment.', |
| 391 | 'friend class base::Singleton<Foo>'] |
oysteine | c430ad4 | 2015-10-22 20:55:24 | [diff] [blame] | 392 | diff_foo2_h = [' //Foo* bar = base::Singleton<Foo>::get();'] |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 393 | diff_bad_h = ['Foo* foo = base::Singleton<Foo>::get();'] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 394 | mock_input_api = MockInputApi() |
| 395 | mock_input_api.files = [MockAffectedFile('base/memory/singleton.h', |
| 396 | diff_singleton_h), |
| 397 | MockAffectedFile('foo.h', diff_foo_h), |
oysteine | c430ad4 | 2015-10-22 20:55:24 | [diff] [blame] | 398 | MockAffectedFile('foo2.h', diff_foo2_h), |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 399 | MockAffectedFile('bad.h', diff_bad_h)] |
| 400 | warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api, |
| 401 | MockOutputApi()) |
| 402 | self.assertEqual(1, len(warnings)) |
oysteine | c430ad4 | 2015-10-22 20:55:24 | [diff] [blame] | 403 | self.assertEqual(2, len(warnings[0].items)) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 404 | self.assertEqual('error', warnings[0].type) |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 405 | self.assertTrue('Found base::Singleton<T>' in warnings[0].message) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 406 | |
| 407 | def testSingletonInCC(self): |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 408 | diff_cc = ['Foo* foo = base::Singleton<Foo>::get();'] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 409 | mock_input_api = MockInputApi() |
| 410 | mock_input_api.files = [MockAffectedFile('some/path/foo.cc', diff_cc)] |
| 411 | warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api, |
| 412 | MockOutputApi()) |
| 413 | self.assertEqual(0, len(warnings)) |
| 414 | |
| 415 | |
dbeam | 1ec68ac | 2016-12-15 05:22:24 | [diff] [blame] | 416 | class CheckNoDeprecatedCompiledResourcesGypTest(unittest.TestCase): |
| 417 | def testNoDeprecatedCompiledResourcsGyp(self): |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 418 | mock_input_api = MockInputApi() |
| 419 | mock_input_api.files = [MockFile('some/js/compiled_resources.gyp', [])] |
dbeam | 1ec68ac | 2016-12-15 05:22:24 | [diff] [blame] | 420 | errors = PRESUBMIT._CheckNoDeprecatedCompiledResourcesGyp(mock_input_api, |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 421 | MockOutputApi()) |
| 422 | self.assertEquals(1, len(errors)) |
| 423 | |
| 424 | mock_input_api.files = [MockFile('some/js/compiled_resources2.gyp', [])] |
dbeam | 1ec68ac | 2016-12-15 05:22:24 | [diff] [blame] | 425 | errors = PRESUBMIT._CheckNoDeprecatedCompiledResourcesGyp(mock_input_api, |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 426 | MockOutputApi()) |
| 427 | self.assertEquals(0, len(errors)) |
| 428 | |
| 429 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 430 | class InvalidOSMacroNamesTest(unittest.TestCase): |
| 431 | def testInvalidOSMacroNames(self): |
| 432 | lines = ['#if defined(OS_WINDOWS)', |
| 433 | ' #elif defined(OS_WINDOW)', |
| 434 | ' # if defined(OS_MACOSX) || defined(OS_CHROME)', |
| 435 | '# else // defined(OS_MAC)', |
| 436 | '#endif // defined(OS_MACOS)'] |
| 437 | errors = PRESUBMIT._CheckForInvalidOSMacrosInFile( |
| 438 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 439 | self.assertEqual(len(lines), len(errors)) |
| 440 | self.assertTrue(':1 OS_WINDOWS' in errors[0]) |
| 441 | self.assertTrue('(did you mean OS_WIN?)' in errors[0]) |
| 442 | |
| 443 | def testValidOSMacroNames(self): |
| 444 | lines = ['#if defined(%s)' % m for m in PRESUBMIT._VALID_OS_MACROS] |
| 445 | errors = PRESUBMIT._CheckForInvalidOSMacrosInFile( |
| 446 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 447 | self.assertEqual(0, len(errors)) |
| 448 | |
| 449 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 450 | class InvalidIfDefinedMacroNamesTest(unittest.TestCase): |
| 451 | def testInvalidIfDefinedMacroNames(self): |
| 452 | lines = ['#if defined(TARGET_IPHONE_SIMULATOR)', |
| 453 | '#if !defined(TARGET_IPHONE_SIMULATOR)', |
| 454 | '#elif defined(TARGET_IPHONE_SIMULATOR)', |
| 455 | '#ifdef TARGET_IPHONE_SIMULATOR', |
| 456 | ' # ifdef TARGET_IPHONE_SIMULATOR', |
| 457 | '# if defined(VALID) || defined(TARGET_IPHONE_SIMULATOR)', |
| 458 | '# else // defined(TARGET_IPHONE_SIMULATOR)', |
| 459 | '#endif // defined(TARGET_IPHONE_SIMULATOR)',] |
| 460 | errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile( |
| 461 | MockInputApi(), MockFile('some/path/source.mm', lines)) |
| 462 | self.assertEqual(len(lines), len(errors)) |
| 463 | |
| 464 | def testValidIfDefinedMacroNames(self): |
| 465 | lines = ['#if defined(FOO)', |
| 466 | '#ifdef BAR',] |
| 467 | errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile( |
| 468 | MockInputApi(), MockFile('some/path/source.cc', lines)) |
| 469 | self.assertEqual(0, len(errors)) |
| 470 | |
| 471 | |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 472 | class CheckAddedDepsHaveTetsApprovalsTest(unittest.TestCase): |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame^] | 473 | |
| 474 | def calculate(self, old_include_rules, old_specific_include_rules, |
| 475 | new_include_rules, new_specific_include_rules): |
| 476 | return PRESUBMIT._CalculateAddedDeps( |
| 477 | os.path, 'include_rules = %r\nspecific_include_rules = %r' % ( |
| 478 | old_include_rules, old_specific_include_rules), |
| 479 | 'include_rules = %r\nspecific_include_rules = %r' % ( |
| 480 | new_include_rules, new_specific_include_rules)) |
| 481 | |
| 482 | def testCalculateAddedDeps(self): |
| 483 | old_include_rules = [ |
| 484 | '+base', |
| 485 | '-chrome', |
| 486 | '+content', |
| 487 | '-grit', |
| 488 | '-grit/",', |
| 489 | '+jni/fooblat.h', |
| 490 | '!sandbox', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 491 | ] |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame^] | 492 | old_specific_include_rules = { |
| 493 | 'compositor\.*': { |
| 494 | '+cc', |
| 495 | }, |
| 496 | } |
| 497 | |
| 498 | new_include_rules = [ |
| 499 | '-ash', |
| 500 | '+base', |
| 501 | '+chrome', |
| 502 | '+components', |
| 503 | '+content', |
| 504 | '+grit', |
| 505 | '+grit/generated_resources.h",', |
| 506 | '+grit/",', |
| 507 | '+jni/fooblat.h', |
| 508 | '+policy', |
| 509 | '+third_party/WebKit', |
| 510 | ] |
| 511 | new_specific_include_rules = { |
| 512 | 'compositor\.*': { |
| 513 | '+cc', |
| 514 | }, |
| 515 | 'widget\.*': { |
| 516 | '+gpu', |
| 517 | }, |
| 518 | } |
| 519 | |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 520 | expected = set([ |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame^] | 521 | 'chrome/DEPS', |
| 522 | 'gpu/DEPS', |
| 523 | 'components/DEPS', |
| 524 | 'policy/DEPS', |
| 525 | 'third_party/WebKit/DEPS', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 526 | ]) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame^] | 527 | self.assertEqual( |
| 528 | expected, |
| 529 | self.calculate(old_include_rules, old_specific_include_rules, |
| 530 | new_include_rules, new_specific_include_rules)) |
| 531 | |
| 532 | def testCalculateAddedDepsIgnoresPermutations(self): |
| 533 | old_include_rules = [ |
| 534 | '+base', |
| 535 | '+chrome', |
| 536 | ] |
| 537 | new_include_rules = [ |
| 538 | '+chrome', |
| 539 | '+base', |
| 540 | ] |
| 541 | self.assertEqual(set(), |
| 542 | self.calculate(old_include_rules, {}, new_include_rules, |
| 543 | {})) |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 544 | |
| 545 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 546 | class JSONParsingTest(unittest.TestCase): |
| 547 | def testSuccess(self): |
| 548 | input_api = MockInputApi() |
| 549 | filename = 'valid_json.json' |
| 550 | contents = ['// This is a comment.', |
| 551 | '{', |
| 552 | ' "key1": ["value1", "value2"],', |
| 553 | ' "key2": 3 // This is an inline comment.', |
| 554 | '}' |
| 555 | ] |
| 556 | input_api.files = [MockFile(filename, contents)] |
| 557 | self.assertEqual(None, |
| 558 | PRESUBMIT._GetJSONParseError(input_api, filename)) |
| 559 | |
| 560 | def testFailure(self): |
| 561 | input_api = MockInputApi() |
| 562 | test_data = [ |
| 563 | ('invalid_json_1.json', |
| 564 | ['{ x }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 565 | 'Expecting property name:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 566 | ('invalid_json_2.json', |
| 567 | ['// Hello world!', |
| 568 | '{ "hello": "world }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 569 | 'Unterminated string starting at:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 570 | ('invalid_json_3.json', |
| 571 | ['{ "a": "b", "c": "d", }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 572 | 'Expecting property name:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 573 | ('invalid_json_4.json', |
| 574 | ['{ "a": "b" "c": "d" }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 575 | 'Expecting , delimiter:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 576 | ] |
| 577 | |
| 578 | input_api.files = [MockFile(filename, contents) |
| 579 | for (filename, contents, _) in test_data] |
| 580 | |
| 581 | for (filename, _, expected_error) in test_data: |
| 582 | actual_error = PRESUBMIT._GetJSONParseError(input_api, filename) |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 583 | self.assertTrue(expected_error in str(actual_error), |
| 584 | "'%s' not found in '%s'" % (expected_error, actual_error)) |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 585 | |
| 586 | def testNoEatComments(self): |
| 587 | input_api = MockInputApi() |
| 588 | file_with_comments = 'file_with_comments.json' |
| 589 | contents_with_comments = ['// This is a comment.', |
| 590 | '{', |
| 591 | ' "key1": ["value1", "value2"],', |
| 592 | ' "key2": 3 // This is an inline comment.', |
| 593 | '}' |
| 594 | ] |
| 595 | file_without_comments = 'file_without_comments.json' |
| 596 | contents_without_comments = ['{', |
| 597 | ' "key1": ["value1", "value2"],', |
| 598 | ' "key2": 3', |
| 599 | '}' |
| 600 | ] |
| 601 | input_api.files = [MockFile(file_with_comments, contents_with_comments), |
| 602 | MockFile(file_without_comments, |
| 603 | contents_without_comments)] |
| 604 | |
| 605 | self.assertEqual('No JSON object could be decoded', |
| 606 | str(PRESUBMIT._GetJSONParseError(input_api, |
| 607 | file_with_comments, |
| 608 | eat_comments=False))) |
| 609 | self.assertEqual(None, |
| 610 | PRESUBMIT._GetJSONParseError(input_api, |
| 611 | file_without_comments, |
| 612 | eat_comments=False)) |
| 613 | |
| 614 | |
| 615 | class IDLParsingTest(unittest.TestCase): |
| 616 | def testSuccess(self): |
| 617 | input_api = MockInputApi() |
| 618 | filename = 'valid_idl_basics.idl' |
| 619 | contents = ['// Tests a valid IDL file.', |
| 620 | 'namespace idl_basics {', |
| 621 | ' enum EnumType {', |
| 622 | ' name1,', |
| 623 | ' name2', |
| 624 | ' };', |
| 625 | '', |
| 626 | ' dictionary MyType1 {', |
| 627 | ' DOMString a;', |
| 628 | ' };', |
| 629 | '', |
| 630 | ' callback Callback1 = void();', |
| 631 | ' callback Callback2 = void(long x);', |
| 632 | ' callback Callback3 = void(MyType1 arg);', |
| 633 | ' callback Callback4 = void(EnumType type);', |
| 634 | '', |
| 635 | ' interface Functions {', |
| 636 | ' static void function1();', |
| 637 | ' static void function2(long x);', |
| 638 | ' static void function3(MyType1 arg);', |
| 639 | ' static void function4(Callback1 cb);', |
| 640 | ' static void function5(Callback2 cb);', |
| 641 | ' static void function6(Callback3 cb);', |
| 642 | ' static void function7(Callback4 cb);', |
| 643 | ' };', |
| 644 | '', |
| 645 | ' interface Events {', |
| 646 | ' static void onFoo1();', |
| 647 | ' static void onFoo2(long x);', |
| 648 | ' static void onFoo2(MyType1 arg);', |
| 649 | ' static void onFoo3(EnumType type);', |
| 650 | ' };', |
| 651 | '};' |
| 652 | ] |
| 653 | input_api.files = [MockFile(filename, contents)] |
| 654 | self.assertEqual(None, |
| 655 | PRESUBMIT._GetIDLParseError(input_api, filename)) |
| 656 | |
| 657 | def testFailure(self): |
| 658 | input_api = MockInputApi() |
| 659 | test_data = [ |
| 660 | ('invalid_idl_1.idl', |
| 661 | ['//', |
| 662 | 'namespace test {', |
| 663 | ' dictionary {', |
| 664 | ' DOMString s;', |
| 665 | ' };', |
| 666 | '};'], |
| 667 | 'Unexpected "{" after keyword "dictionary".\n'), |
| 668 | # TODO(yoz): Disabled because it causes the IDL parser to hang. |
| 669 | # See crbug.com/363830. |
| 670 | # ('invalid_idl_2.idl', |
| 671 | # (['namespace test {', |
| 672 | # ' dictionary MissingSemicolon {', |
| 673 | # ' DOMString a', |
| 674 | # ' DOMString b;', |
| 675 | # ' };', |
| 676 | # '};'], |
| 677 | # 'Unexpected symbol DOMString after symbol a.'), |
| 678 | ('invalid_idl_3.idl', |
| 679 | ['//', |
| 680 | 'namespace test {', |
| 681 | ' enum MissingComma {', |
| 682 | ' name1', |
| 683 | ' name2', |
| 684 | ' };', |
| 685 | '};'], |
| 686 | 'Unexpected symbol name2 after symbol name1.'), |
| 687 | ('invalid_idl_4.idl', |
| 688 | ['//', |
| 689 | 'namespace test {', |
| 690 | ' enum TrailingComma {', |
| 691 | ' name1,', |
| 692 | ' name2,', |
| 693 | ' };', |
| 694 | '};'], |
| 695 | 'Trailing comma in block.'), |
| 696 | ('invalid_idl_5.idl', |
| 697 | ['//', |
| 698 | 'namespace test {', |
| 699 | ' callback Callback1 = void(;', |
| 700 | '};'], |
| 701 | 'Unexpected ";" after "(".'), |
| 702 | ('invalid_idl_6.idl', |
| 703 | ['//', |
| 704 | 'namespace test {', |
| 705 | ' callback Callback1 = void(long );', |
| 706 | '};'], |
| 707 | 'Unexpected ")" after symbol long.'), |
| 708 | ('invalid_idl_7.idl', |
| 709 | ['//', |
| 710 | 'namespace test {', |
| 711 | ' interace Events {', |
| 712 | ' static void onFoo1();', |
| 713 | ' };', |
| 714 | '};'], |
| 715 | 'Unexpected symbol Events after symbol interace.'), |
| 716 | ('invalid_idl_8.idl', |
| 717 | ['//', |
| 718 | 'namespace test {', |
| 719 | ' interface NotEvent {', |
| 720 | ' static void onFoo1();', |
| 721 | ' };', |
| 722 | '};'], |
| 723 | 'Did not process Interface Interface(NotEvent)'), |
| 724 | ('invalid_idl_9.idl', |
| 725 | ['//', |
| 726 | 'namespace test {', |
| 727 | ' interface {', |
| 728 | ' static void function1();', |
| 729 | ' };', |
| 730 | '};'], |
| 731 | 'Interface missing name.'), |
| 732 | ] |
| 733 | |
| 734 | input_api.files = [MockFile(filename, contents) |
| 735 | for (filename, contents, _) in test_data] |
| 736 | |
| 737 | for (filename, _, expected_error) in test_data: |
| 738 | actual_error = PRESUBMIT._GetIDLParseError(input_api, filename) |
| 739 | self.assertTrue(expected_error in str(actual_error), |
| 740 | "'%s' not found in '%s'" % (expected_error, actual_error)) |
| 741 | |
| 742 | |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 743 | class TryServerMasterTest(unittest.TestCase): |
| 744 | def testTryServerMasters(self): |
| 745 | bots = { |
tandrii | e558779 | 2016-07-14 00:34:50 | [diff] [blame] | 746 | 'master.tryserver.chromium.android': [ |
jbudorick | 3ae7a77 | 2016-05-20 02:36:04 | [diff] [blame] | 747 | 'android_archive_rel_ng', |
| 748 | 'android_arm64_dbg_recipe', |
| 749 | 'android_blink_rel', |
| 750 | 'android_chromium_variable', |
| 751 | 'android_chromium_variable_archive', |
| 752 | 'android_chromium_variable_arm64', |
| 753 | 'android_chromium_variable_cast_shell', |
| 754 | 'android_chromium_variable_clang', |
| 755 | 'android_chromium_variable_gn', |
| 756 | 'android_chromium_variable_nexus4', |
| 757 | 'android_clang_dbg_recipe', |
| 758 | 'android_compile_dbg', |
| 759 | 'android_compile_mips_dbg', |
| 760 | 'android_compile_rel', |
| 761 | 'android_compile_x64_dbg', |
| 762 | 'android_compile_x86_dbg', |
| 763 | 'android_coverage', |
| 764 | 'android_cronet_tester' |
| 765 | 'android_swarming_rel', |
| 766 | 'cast_shell_android', |
| 767 | 'linux_android_dbg_ng', |
| 768 | 'linux_android_rel_ng', |
| 769 | ], |
tandrii | e558779 | 2016-07-14 00:34:50 | [diff] [blame] | 770 | 'master.tryserver.chromium.mac': [ |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 771 | 'ios_dbg_simulator', |
| 772 | 'ios_rel_device', |
| 773 | 'ios_rel_device_ninja', |
| 774 | 'mac_asan', |
| 775 | 'mac_asan_64', |
| 776 | 'mac_chromium_compile_dbg', |
| 777 | 'mac_chromium_compile_rel', |
| 778 | 'mac_chromium_dbg', |
| 779 | 'mac_chromium_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 780 | 'mac_nacl_sdk', |
| 781 | 'mac_nacl_sdk_build', |
| 782 | 'mac_rel_naclmore', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 783 | 'mac_x64_rel', |
| 784 | 'mac_xcodebuild', |
| 785 | ], |
tandrii | e558779 | 2016-07-14 00:34:50 | [diff] [blame] | 786 | 'master.tryserver.chromium.linux': [ |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 787 | 'chromium_presubmit', |
| 788 | 'linux_arm_cross_compile', |
| 789 | 'linux_arm_tester', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 790 | 'linux_chromeos_asan', |
| 791 | 'linux_chromeos_browser_asan', |
| 792 | 'linux_chromeos_valgrind', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 793 | 'linux_chromium_chromeos_dbg', |
| 794 | 'linux_chromium_chromeos_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 795 | 'linux_chromium_compile_dbg', |
| 796 | 'linux_chromium_compile_rel', |
| 797 | 'linux_chromium_dbg', |
| 798 | 'linux_chromium_gn_dbg', |
| 799 | 'linux_chromium_gn_rel', |
| 800 | 'linux_chromium_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 801 | 'linux_chromium_trusty32_dbg', |
| 802 | 'linux_chromium_trusty32_rel', |
| 803 | 'linux_chromium_trusty_dbg', |
| 804 | 'linux_chromium_trusty_rel', |
| 805 | 'linux_clang_tsan', |
| 806 | 'linux_ecs_ozone', |
| 807 | 'linux_layout', |
| 808 | 'linux_layout_asan', |
| 809 | 'linux_layout_rel', |
| 810 | 'linux_layout_rel_32', |
| 811 | 'linux_nacl_sdk', |
| 812 | 'linux_nacl_sdk_bionic', |
| 813 | 'linux_nacl_sdk_bionic_build', |
| 814 | 'linux_nacl_sdk_build', |
| 815 | 'linux_redux', |
| 816 | 'linux_rel_naclmore', |
| 817 | 'linux_rel_precise32', |
| 818 | 'linux_valgrind', |
| 819 | 'tools_build_presubmit', |
| 820 | ], |
tandrii | e558779 | 2016-07-14 00:34:50 | [diff] [blame] | 821 | 'master.tryserver.chromium.win': [ |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 822 | 'win8_aura', |
| 823 | 'win8_chromium_dbg', |
| 824 | 'win8_chromium_rel', |
| 825 | 'win_chromium_compile_dbg', |
| 826 | 'win_chromium_compile_rel', |
| 827 | 'win_chromium_dbg', |
| 828 | 'win_chromium_rel', |
| 829 | 'win_chromium_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 830 | 'win_chromium_x64_dbg', |
| 831 | 'win_chromium_x64_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 832 | 'win_nacl_sdk', |
| 833 | 'win_nacl_sdk_build', |
| 834 | 'win_rel_naclmore', |
| 835 | ], |
| 836 | } |
| 837 | for master, bots in bots.iteritems(): |
| 838 | for bot in bots: |
| 839 | self.assertEqual(master, PRESUBMIT.GetTryServerMasterForBot(bot), |
| 840 | 'bot=%s: expected %s, computed %s' % ( |
| 841 | bot, master, PRESUBMIT.GetTryServerMasterForBot(bot))) |
| 842 | |
| 843 | |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 844 | class UserMetricsActionTest(unittest.TestCase): |
| 845 | def testUserMetricsActionInActions(self): |
| 846 | input_api = MockInputApi() |
| 847 | file_with_user_action = 'file_with_user_action.cc' |
| 848 | contents_with_user_action = [ |
| 849 | 'base::UserMetricsAction("AboutChrome")' |
| 850 | ] |
| 851 | |
| 852 | input_api.files = [MockFile(file_with_user_action, |
| 853 | contents_with_user_action)] |
| 854 | |
| 855 | self.assertEqual( |
| 856 | [], PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi())) |
| 857 | |
| 858 | |
| 859 | def testUserMetricsActionNotAddedToActions(self): |
| 860 | input_api = MockInputApi() |
| 861 | file_with_user_action = 'file_with_user_action.cc' |
| 862 | contents_with_user_action = [ |
| 863 | 'base::UserMetricsAction("NotInActionsXml")' |
| 864 | ] |
| 865 | |
| 866 | input_api.files = [MockFile(file_with_user_action, |
| 867 | contents_with_user_action)] |
| 868 | |
| 869 | output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()) |
| 870 | self.assertEqual( |
| 871 | ('File %s line %d: %s is missing in ' |
| 872 | 'tools/metrics/actions/actions.xml. Please run ' |
| 873 | 'tools/metrics/actions/extract_actions.py to update.' |
| 874 | % (file_with_user_action, 1, 'NotInActionsXml')), |
| 875 | output[0].message) |
| 876 | |
| 877 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 878 | class PydepsNeedsUpdatingTest(unittest.TestCase): |
| 879 | |
| 880 | class MockSubprocess(object): |
| 881 | CalledProcessError = subprocess.CalledProcessError |
| 882 | |
| 883 | def setUp(self): |
| 884 | mock_all_pydeps = ['A.pydeps', 'B.pydeps'] |
| 885 | self.old_ALL_PYDEPS_FILES = PRESUBMIT._ALL_PYDEPS_FILES |
| 886 | PRESUBMIT._ALL_PYDEPS_FILES = mock_all_pydeps |
| 887 | self.mock_input_api = MockInputApi() |
| 888 | self.mock_output_api = MockOutputApi() |
| 889 | self.mock_input_api.subprocess = PydepsNeedsUpdatingTest.MockSubprocess() |
| 890 | self.checker = PRESUBMIT.PydepsChecker(self.mock_input_api, mock_all_pydeps) |
| 891 | self.checker._file_cache = { |
| 892 | 'A.pydeps': '# Generated by:\n# CMD A\nA.py\nC.py\n', |
| 893 | 'B.pydeps': '# Generated by:\n# CMD B\nB.py\nC.py\n', |
| 894 | } |
| 895 | |
| 896 | def tearDown(self): |
| 897 | PRESUBMIT._ALL_PYDEPS_FILES = self.old_ALL_PYDEPS_FILES |
| 898 | |
| 899 | def _RunCheck(self): |
| 900 | return PRESUBMIT._CheckPydepsNeedsUpdating(self.mock_input_api, |
| 901 | self.mock_output_api, |
| 902 | checker_for_tests=self.checker) |
| 903 | |
| 904 | def testAddedPydep(self): |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 905 | # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android. |
| 906 | if self.mock_input_api.platform != 'linux2': |
| 907 | return [] |
| 908 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 909 | self.mock_input_api.files = [ |
| 910 | MockAffectedFile('new.pydeps', [], action='A'), |
| 911 | ] |
| 912 | |
| 913 | results = self._RunCheck() |
| 914 | self.assertEqual(1, len(results)) |
| 915 | self.assertTrue('PYDEPS_FILES' in str(results[0])) |
| 916 | |
| 917 | def testRemovedPydep(self): |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 918 | # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android. |
| 919 | if self.mock_input_api.platform != 'linux2': |
| 920 | return [] |
| 921 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 922 | self.mock_input_api.files = [ |
| 923 | MockAffectedFile(PRESUBMIT._ALL_PYDEPS_FILES[0], [], action='D'), |
| 924 | ] |
| 925 | |
| 926 | results = self._RunCheck() |
| 927 | self.assertEqual(1, len(results)) |
| 928 | self.assertTrue('PYDEPS_FILES' in str(results[0])) |
| 929 | |
| 930 | def testRandomPyIgnored(self): |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 931 | # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android. |
| 932 | if self.mock_input_api.platform != 'linux2': |
| 933 | return [] |
| 934 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 935 | self.mock_input_api.files = [ |
| 936 | MockAffectedFile('random.py', []), |
| 937 | ] |
| 938 | |
| 939 | results = self._RunCheck() |
| 940 | self.assertEqual(0, len(results), 'Unexpected results: %r' % results) |
| 941 | |
| 942 | def testRelevantPyNoChange(self): |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 943 | # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android. |
| 944 | if self.mock_input_api.platform != 'linux2': |
| 945 | return [] |
| 946 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 947 | self.mock_input_api.files = [ |
| 948 | MockAffectedFile('A.py', []), |
| 949 | ] |
| 950 | |
| 951 | def mock_check_output(cmd, shell=False): |
| 952 | self.assertEqual('CMD A --output ""', cmd) |
| 953 | return self.checker._file_cache['A.pydeps'] |
| 954 | |
| 955 | self.mock_input_api.subprocess.check_output = mock_check_output |
| 956 | |
| 957 | results = self._RunCheck() |
| 958 | self.assertEqual(0, len(results), 'Unexpected results: %r' % results) |
| 959 | |
| 960 | def testRelevantPyOneChange(self): |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 961 | # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android. |
| 962 | if self.mock_input_api.platform != 'linux2': |
| 963 | return [] |
| 964 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 965 | self.mock_input_api.files = [ |
| 966 | MockAffectedFile('A.py', []), |
| 967 | ] |
| 968 | |
| 969 | def mock_check_output(cmd, shell=False): |
| 970 | self.assertEqual('CMD A --output ""', cmd) |
| 971 | return 'changed data' |
| 972 | |
| 973 | self.mock_input_api.subprocess.check_output = mock_check_output |
| 974 | |
| 975 | results = self._RunCheck() |
| 976 | self.assertEqual(1, len(results)) |
| 977 | self.assertTrue('File is stale' in str(results[0])) |
| 978 | |
| 979 | def testRelevantPyTwoChanges(self): |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 980 | # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android. |
| 981 | if self.mock_input_api.platform != 'linux2': |
| 982 | return [] |
| 983 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 984 | self.mock_input_api.files = [ |
| 985 | MockAffectedFile('C.py', []), |
| 986 | ] |
| 987 | |
| 988 | def mock_check_output(cmd, shell=False): |
| 989 | return 'changed data' |
| 990 | |
| 991 | self.mock_input_api.subprocess.check_output = mock_check_output |
| 992 | |
| 993 | results = self._RunCheck() |
| 994 | self.assertEqual(2, len(results)) |
| 995 | self.assertTrue('File is stale' in str(results[0])) |
| 996 | self.assertTrue('File is stale' in str(results[1])) |
| 997 | |
| 998 | |
yolandyan | 4500147 | 2016-12-21 21:12:42 | [diff] [blame] | 999 | class AndroidDeprecatedTestAnnotationTest(unittest.TestCase): |
| 1000 | def testCheckAndroidTestAnnotationUsage(self): |
| 1001 | mock_input_api = MockInputApi() |
| 1002 | mock_output_api = MockOutputApi() |
| 1003 | |
| 1004 | mock_input_api.files = [ |
| 1005 | MockAffectedFile('LalaLand.java', [ |
| 1006 | 'random stuff' |
| 1007 | ]), |
| 1008 | MockAffectedFile('CorrectUsage.java', [ |
| 1009 | 'import android.support.test.filters.LargeTest;', |
| 1010 | 'import android.support.test.filters.MediumTest;', |
| 1011 | 'import android.support.test.filters.SmallTest;', |
| 1012 | ]), |
| 1013 | MockAffectedFile('UsedDeprecatedLargeTestAnnotation.java', [ |
| 1014 | 'import android.test.suitebuilder.annotation.LargeTest;', |
| 1015 | ]), |
| 1016 | MockAffectedFile('UsedDeprecatedMediumTestAnnotation.java', [ |
| 1017 | 'import android.test.suitebuilder.annotation.MediumTest;', |
| 1018 | ]), |
| 1019 | MockAffectedFile('UsedDeprecatedSmallTestAnnotation.java', [ |
| 1020 | 'import android.test.suitebuilder.annotation.SmallTest;', |
| 1021 | ]), |
| 1022 | MockAffectedFile('UsedDeprecatedSmokeAnnotation.java', [ |
| 1023 | 'import android.test.suitebuilder.annotation.Smoke;', |
| 1024 | ]) |
| 1025 | ] |
| 1026 | msgs = PRESUBMIT._CheckAndroidTestAnnotationUsage( |
| 1027 | mock_input_api, mock_output_api) |
| 1028 | self.assertEqual(1, len(msgs), |
| 1029 | 'Expected %d items, found %d: %s' |
| 1030 | % (1, len(msgs), msgs)) |
| 1031 | self.assertEqual(4, len(msgs[0].items), |
| 1032 | 'Expected %d items, found %d: %s' |
| 1033 | % (4, len(msgs[0].items), msgs[0].items)) |
| 1034 | self.assertTrue('UsedDeprecatedLargeTestAnnotation.java:1' in msgs[0].items, |
| 1035 | 'UsedDeprecatedLargeTestAnnotation not found in errors') |
| 1036 | self.assertTrue('UsedDeprecatedMediumTestAnnotation.java:1' |
| 1037 | in msgs[0].items, |
| 1038 | 'UsedDeprecatedMediumTestAnnotation not found in errors') |
| 1039 | self.assertTrue('UsedDeprecatedSmallTestAnnotation.java:1' in msgs[0].items, |
| 1040 | 'UsedDeprecatedSmallTestAnnotation not found in errors') |
| 1041 | self.assertTrue('UsedDeprecatedSmokeAnnotation.java:1' in msgs[0].items, |
| 1042 | 'UsedDeprecatedSmokeAnnotation not found in errors') |
| 1043 | |
| 1044 | |
| 1045 | |
dgn | 4401aa5 | 2015-04-29 16:26:17 | [diff] [blame] | 1046 | class LogUsageTest(unittest.TestCase): |
| 1047 | |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1048 | def testCheckAndroidCrLogUsage(self): |
| 1049 | mock_input_api = MockInputApi() |
| 1050 | mock_output_api = MockOutputApi() |
| 1051 | |
| 1052 | mock_input_api.files = [ |
| 1053 | MockAffectedFile('RandomStuff.java', [ |
| 1054 | 'random stuff' |
| 1055 | ]), |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1056 | MockAffectedFile('HasAndroidLog.java', [ |
| 1057 | 'import android.util.Log;', |
| 1058 | 'some random stuff', |
| 1059 | 'Log.d("TAG", "foo");', |
| 1060 | ]), |
| 1061 | MockAffectedFile('HasExplicitUtilLog.java', [ |
| 1062 | 'some random stuff', |
| 1063 | 'android.util.Log.d("TAG", "foo");', |
| 1064 | ]), |
| 1065 | MockAffectedFile('IsInBasePackage.java', [ |
| 1066 | 'package org.chromium.base;', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1067 | 'private static final String TAG = "cr_Foo";', |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1068 | 'Log.d(TAG, "foo");', |
| 1069 | ]), |
| 1070 | MockAffectedFile('IsInBasePackageButImportsLog.java', [ |
| 1071 | 'package org.chromium.base;', |
| 1072 | 'import android.util.Log;', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1073 | 'private static final String TAG = "cr_Foo";', |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1074 | 'Log.d(TAG, "foo");', |
| 1075 | ]), |
| 1076 | MockAffectedFile('HasBothLog.java', [ |
| 1077 | 'import org.chromium.base.Log;', |
| 1078 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1079 | 'private static final String TAG = "cr_Foo";', |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1080 | 'Log.d(TAG, "foo");', |
| 1081 | 'android.util.Log.d("TAG", "foo");', |
| 1082 | ]), |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1083 | MockAffectedFile('HasCorrectTag.java', [ |
| 1084 | 'import org.chromium.base.Log;', |
| 1085 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1086 | 'private static final String TAG = "cr_Foo";', |
| 1087 | 'Log.d(TAG, "foo");', |
| 1088 | ]), |
| 1089 | MockAffectedFile('HasOldTag.java', [ |
| 1090 | 'import org.chromium.base.Log;', |
| 1091 | 'some random stuff', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1092 | 'private static final String TAG = "cr.Foo";', |
| 1093 | 'Log.d(TAG, "foo");', |
| 1094 | ]), |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1095 | MockAffectedFile('HasDottedTag.java', [ |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1096 | 'import org.chromium.base.Log;', |
| 1097 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1098 | 'private static final String TAG = "cr_foo.bar";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1099 | 'Log.d(TAG, "foo");', |
| 1100 | ]), |
| 1101 | MockAffectedFile('HasNoTagDecl.java', [ |
| 1102 | 'import org.chromium.base.Log;', |
| 1103 | 'some random stuff', |
| 1104 | 'Log.d(TAG, "foo");', |
| 1105 | ]), |
| 1106 | MockAffectedFile('HasIncorrectTagDecl.java', [ |
| 1107 | 'import org.chromium.base.Log;', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1108 | 'private static final String TAHG = "cr_Foo";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1109 | 'some random stuff', |
| 1110 | 'Log.d(TAG, "foo");', |
| 1111 | ]), |
| 1112 | MockAffectedFile('HasInlineTag.java', [ |
| 1113 | 'import org.chromium.base.Log;', |
| 1114 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1115 | 'private static final String TAG = "cr_Foo";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1116 | 'Log.d("TAG", "foo");', |
| 1117 | ]), |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1118 | MockAffectedFile('HasUnprefixedTag.java', [ |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1119 | 'import org.chromium.base.Log;', |
| 1120 | 'some random stuff', |
| 1121 | 'private static final String TAG = "rubbish";', |
| 1122 | 'Log.d(TAG, "foo");', |
| 1123 | ]), |
| 1124 | MockAffectedFile('HasTooLongTag.java', [ |
| 1125 | 'import org.chromium.base.Log;', |
| 1126 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1127 | 'private static final String TAG = "21_charachers_long___";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1128 | 'Log.d(TAG, "foo");', |
| 1129 | ]), |
| 1130 | ] |
| 1131 | |
| 1132 | msgs = PRESUBMIT._CheckAndroidCrLogUsage( |
| 1133 | mock_input_api, mock_output_api) |
| 1134 | |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1135 | self.assertEqual(5, len(msgs), |
| 1136 | 'Expected %d items, found %d: %s' % (5, len(msgs), msgs)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1137 | |
| 1138 | # Declaration format |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1139 | nb = len(msgs[0].items) |
| 1140 | self.assertEqual(2, nb, |
| 1141 | 'Expected %d items, found %d: %s' % (2, nb, msgs[0].items)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1142 | self.assertTrue('HasNoTagDecl.java' in msgs[0].items) |
| 1143 | self.assertTrue('HasIncorrectTagDecl.java' in msgs[0].items) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1144 | |
| 1145 | # Tag length |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1146 | nb = len(msgs[1].items) |
| 1147 | self.assertEqual(1, nb, |
| 1148 | 'Expected %d items, found %d: %s' % (1, nb, msgs[1].items)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1149 | self.assertTrue('HasTooLongTag.java' in msgs[1].items) |
| 1150 | |
| 1151 | # Tag must be a variable named TAG |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1152 | nb = len(msgs[2].items) |
| 1153 | self.assertEqual(1, nb, |
| 1154 | 'Expected %d items, found %d: %s' % (1, nb, msgs[2].items)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1155 | self.assertTrue('HasInlineTag.java:4' in msgs[2].items) |
| 1156 | |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1157 | # Util Log usage |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1158 | nb = len(msgs[3].items) |
| 1159 | self.assertEqual(2, nb, |
| 1160 | 'Expected %d items, found %d: %s' % (2, nb, msgs[3].items)) |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1161 | self.assertTrue('HasAndroidLog.java:3' in msgs[3].items) |
| 1162 | self.assertTrue('IsInBasePackageButImportsLog.java:4' in msgs[3].items) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1163 | |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1164 | # Tag must not contain |
| 1165 | nb = len(msgs[4].items) |
| 1166 | self.assertEqual(2, nb, |
| 1167 | 'Expected %d items, found %d: %s' % (2, nb, msgs[4].items)) |
| 1168 | self.assertTrue('HasDottedTag.java' in msgs[4].items) |
| 1169 | self.assertTrue('HasOldTag.java' in msgs[4].items) |
| 1170 | |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 1171 | class GoogleAnswerUrlFormatTest(unittest.TestCase): |
| 1172 | |
| 1173 | def testCatchAnswerUrlId(self): |
| 1174 | input_api = MockInputApi() |
| 1175 | input_api.files = [ |
| 1176 | MockFile('somewhere/file.cc', |
| 1177 | ['char* host = ' |
| 1178 | ' "https://support.google.com/chrome/answer/123456";']), |
| 1179 | MockFile('somewhere_else/file.cc', |
| 1180 | ['char* host = ' |
| 1181 | ' "https://support.google.com/chrome/a/answer/123456";']), |
| 1182 | ] |
| 1183 | |
| 1184 | warnings = PRESUBMIT._CheckGoogleSupportAnswerUrl( |
| 1185 | input_api, MockOutputApi()) |
| 1186 | self.assertEqual(1, len(warnings)) |
| 1187 | self.assertEqual(2, len(warnings[0].items)) |
| 1188 | |
| 1189 | def testAllowAnswerUrlParam(self): |
| 1190 | input_api = MockInputApi() |
| 1191 | input_api.files = [ |
| 1192 | MockFile('somewhere/file.cc', |
| 1193 | ['char* host = ' |
| 1194 | ' "https://support.google.com/chrome/?p=cpn_crash_reports";']), |
| 1195 | ] |
| 1196 | |
| 1197 | warnings = PRESUBMIT._CheckGoogleSupportAnswerUrl( |
| 1198 | input_api, MockOutputApi()) |
| 1199 | self.assertEqual(0, len(warnings)) |
| 1200 | |
reillyi | 3896573 | 2015-11-16 18:27:33 | [diff] [blame] | 1201 | class HardcodedGoogleHostsTest(unittest.TestCase): |
| 1202 | |
| 1203 | def testWarnOnAssignedLiterals(self): |
| 1204 | input_api = MockInputApi() |
| 1205 | input_api.files = [ |
| 1206 | MockFile('content/file.cc', |
| 1207 | ['char* host = "https://www.google.com";']), |
| 1208 | MockFile('content/file.cc', |
| 1209 | ['char* host = "https://www.googleapis.com";']), |
| 1210 | MockFile('content/file.cc', |
| 1211 | ['char* host = "https://clients1.google.com";']), |
| 1212 | ] |
| 1213 | |
| 1214 | warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( |
| 1215 | input_api, MockOutputApi()) |
| 1216 | self.assertEqual(1, len(warnings)) |
| 1217 | self.assertEqual(3, len(warnings[0].items)) |
| 1218 | |
| 1219 | def testAllowInComment(self): |
| 1220 | input_api = MockInputApi() |
| 1221 | input_api.files = [ |
| 1222 | MockFile('content/file.cc', |
| 1223 | ['char* host = "https://www.aol.com"; // google.com']) |
| 1224 | ] |
| 1225 | |
| 1226 | warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( |
| 1227 | input_api, MockOutputApi()) |
| 1228 | self.assertEqual(0, len(warnings)) |
| 1229 | |
dgn | 4401aa5 | 2015-04-29 16:26:17 | [diff] [blame] | 1230 | |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 1231 | class ForwardDeclarationTest(unittest.TestCase): |
jbriance | 2c51e821a | 2016-12-12 08:24:31 | [diff] [blame] | 1232 | def testCheckHeadersOnlyOutsideThirdParty(self): |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 1233 | mock_input_api = MockInputApi() |
| 1234 | mock_input_api.files = [ |
| 1235 | MockAffectedFile('somewhere/file.cc', [ |
| 1236 | 'class DummyClass;' |
jbriance | 2c51e821a | 2016-12-12 08:24:31 | [diff] [blame] | 1237 | ]), |
| 1238 | MockAffectedFile('third_party/header.h', [ |
| 1239 | 'class DummyClass;' |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 1240 | ]) |
| 1241 | ] |
| 1242 | warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api, |
| 1243 | MockOutputApi()) |
| 1244 | self.assertEqual(0, len(warnings)) |
| 1245 | |
| 1246 | def testNoNestedDeclaration(self): |
| 1247 | mock_input_api = MockInputApi() |
| 1248 | mock_input_api.files = [ |
| 1249 | MockAffectedFile('somewhere/header.h', [ |
jbriance | 2c51e821a | 2016-12-12 08:24:31 | [diff] [blame] | 1250 | 'class SomeClass {', |
| 1251 | ' protected:', |
| 1252 | ' class NotAMatch;', |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 1253 | '};' |
| 1254 | ]) |
| 1255 | ] |
| 1256 | warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api, |
| 1257 | MockOutputApi()) |
| 1258 | self.assertEqual(0, len(warnings)) |
| 1259 | |
| 1260 | def testSubStrings(self): |
| 1261 | mock_input_api = MockInputApi() |
| 1262 | mock_input_api.files = [ |
| 1263 | MockAffectedFile('somewhere/header.h', [ |
| 1264 | 'class NotUsefulClass;', |
| 1265 | 'struct SomeStruct;', |
| 1266 | 'UsefulClass *p1;', |
| 1267 | 'SomeStructPtr *p2;' |
| 1268 | ]) |
| 1269 | ] |
| 1270 | warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api, |
| 1271 | MockOutputApi()) |
| 1272 | self.assertEqual(2, len(warnings)) |
| 1273 | |
| 1274 | def testUselessForwardDeclaration(self): |
| 1275 | mock_input_api = MockInputApi() |
| 1276 | mock_input_api.files = [ |
| 1277 | MockAffectedFile('somewhere/header.h', [ |
| 1278 | 'class DummyClass;', |
| 1279 | 'struct DummyStruct;', |
| 1280 | 'class UsefulClass;', |
| 1281 | 'std::unique_ptr<UsefulClass> p;' |
jbriance | 2c51e821a | 2016-12-12 08:24:31 | [diff] [blame] | 1282 | ]) |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 1283 | ] |
| 1284 | warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api, |
| 1285 | MockOutputApi()) |
| 1286 | self.assertEqual(2, len(warnings)) |
| 1287 | |
jbriance | 2c51e821a | 2016-12-12 08:24:31 | [diff] [blame] | 1288 | def testBlinkHeaders(self): |
| 1289 | mock_input_api = MockInputApi() |
| 1290 | mock_input_api.files = [ |
| 1291 | MockAffectedFile('third_party/WebKit/header.h', [ |
| 1292 | 'class DummyClass;', |
| 1293 | 'struct DummyStruct;', |
| 1294 | ]), |
| 1295 | MockAffectedFile('third_party\\WebKit\\header.h', [ |
| 1296 | 'class DummyClass;', |
| 1297 | 'struct DummyStruct;', |
| 1298 | ]) |
| 1299 | ] |
| 1300 | warnings = PRESUBMIT._CheckUselessForwardDeclarations(mock_input_api, |
| 1301 | MockOutputApi()) |
| 1302 | self.assertEqual(4, len(warnings)) |
| 1303 | |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 1304 | |
dbeam | 1ec68ac | 2016-12-15 05:22:24 | [diff] [blame] | 1305 | class RiskyJsTest(unittest.TestCase): |
| 1306 | def testArrowWarnInIos9Code(self): |
| 1307 | mock_input_api = MockInputApi() |
| 1308 | mock_output_api = MockOutputApi() |
| 1309 | |
| 1310 | mock_input_api.files = [ |
| 1311 | MockAffectedFile('components/blah.js', ["shouldn't use => here"]), |
| 1312 | ] |
| 1313 | warnings = PRESUBMIT._CheckForRiskyJsFeatures( |
| 1314 | mock_input_api, mock_output_api) |
| 1315 | self.assertEqual(1, len(warnings)) |
| 1316 | |
| 1317 | mock_input_api.files = [ |
| 1318 | MockAffectedFile('ios/blee.js', ['might => break folks']), |
| 1319 | ] |
| 1320 | warnings = PRESUBMIT._CheckForRiskyJsFeatures( |
| 1321 | mock_input_api, mock_output_api) |
| 1322 | self.assertEqual(1, len(warnings)) |
| 1323 | |
| 1324 | mock_input_api.files = [ |
| 1325 | MockAffectedFile('ui/webui/resources/blarg.js', ['on => iOS9']), |
| 1326 | ] |
| 1327 | warnings = PRESUBMIT._CheckForRiskyJsFeatures( |
| 1328 | mock_input_api, mock_output_api) |
| 1329 | self.assertEqual(1, len(warnings)) |
| 1330 | |
| 1331 | def testArrowsAllowedInChromeCode(self): |
| 1332 | mock_input_api = MockInputApi() |
| 1333 | mock_input_api.files = [ |
| 1334 | MockAffectedFile('chrome/browser/resources/blah.js', 'arrow => OK here'), |
| 1335 | ] |
| 1336 | warnings = PRESUBMIT._CheckForRiskyJsFeatures( |
| 1337 | mock_input_api, MockOutputApi()) |
| 1338 | self.assertEqual(0, len(warnings)) |
| 1339 | |
| 1340 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 1341 | if __name__ == '__main__': |
| 1342 | unittest.main() |