[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 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 6 | import re |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 7 | import subprocess |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 8 | import unittest |
| 9 | |
| 10 | import PRESUBMIT |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 11 | from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 12 | from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 13 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 14 | _TEST_DATA_DIR = 'base/test/data/presubmit' |
| 15 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 16 | class IncludeOrderTest(unittest.TestCase): |
| 17 | def testSystemHeaderOrder(self): |
| 18 | scope = [(1, '#include <csystem.h>'), |
| 19 | (2, '#include <cppsystem>'), |
| 20 | (3, '#include "acustom.h"')] |
| 21 | all_linenums = [linenum for (linenum, _) in scope] |
| 22 | mock_input_api = MockInputApi() |
| 23 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 24 | '', all_linenums) |
| 25 | self.assertEqual(0, len(warnings)) |
| 26 | |
| 27 | def testSystemHeaderOrderMismatch1(self): |
| 28 | scope = [(10, '#include <cppsystem>'), |
| 29 | (20, '#include <csystem.h>'), |
| 30 | (30, '#include "acustom.h"')] |
| 31 | all_linenums = [linenum for (linenum, _) in scope] |
| 32 | mock_input_api = MockInputApi() |
| 33 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 34 | '', all_linenums) |
| 35 | self.assertEqual(1, len(warnings)) |
| 36 | self.assertTrue('20' in warnings[0]) |
| 37 | |
| 38 | def testSystemHeaderOrderMismatch2(self): |
| 39 | scope = [(10, '#include <cppsystem>'), |
| 40 | (20, '#include "acustom.h"'), |
| 41 | (30, '#include <csystem.h>')] |
| 42 | all_linenums = [linenum for (linenum, _) in scope] |
| 43 | mock_input_api = MockInputApi() |
| 44 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 45 | '', all_linenums) |
| 46 | self.assertEqual(1, len(warnings)) |
| 47 | self.assertTrue('30' in warnings[0]) |
| 48 | |
| 49 | def testSystemHeaderOrderMismatch3(self): |
| 50 | scope = [(10, '#include "acustom.h"'), |
| 51 | (20, '#include <csystem.h>'), |
| 52 | (30, '#include <cppsystem>')] |
| 53 | all_linenums = [linenum for (linenum, _) in scope] |
| 54 | mock_input_api = MockInputApi() |
| 55 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 56 | '', all_linenums) |
| 57 | self.assertEqual(2, len(warnings)) |
| 58 | self.assertTrue('20' in warnings[0]) |
| 59 | self.assertTrue('30' in warnings[1]) |
| 60 | |
| 61 | def testAlphabeticalOrderMismatch(self): |
| 62 | scope = [(10, '#include <csystem.h>'), |
| 63 | (15, '#include <bsystem.h>'), |
| 64 | (20, '#include <cppsystem>'), |
| 65 | (25, '#include <bppsystem>'), |
| 66 | (30, '#include "bcustom.h"'), |
| 67 | (35, '#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(3, len(warnings)) |
| 73 | self.assertTrue('15' in warnings[0]) |
| 74 | self.assertTrue('25' in warnings[1]) |
| 75 | self.assertTrue('35' in warnings[2]) |
| 76 | |
| 77 | def testSpecialFirstInclude1(self): |
| 78 | mock_input_api = MockInputApi() |
| 79 | contents = ['#include "some/path/foo.h"', |
| 80 | '#include "a/header.h"'] |
| 81 | mock_file = MockFile('some/path/foo.cc', contents) |
| 82 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 83 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 84 | self.assertEqual(0, len(warnings)) |
| 85 | |
| 86 | def testSpecialFirstInclude2(self): |
| 87 | mock_input_api = MockInputApi() |
| 88 | contents = ['#include "some/other/path/foo.h"', |
| 89 | '#include "a/header.h"'] |
| 90 | mock_file = MockFile('some/path/foo.cc', contents) |
| 91 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 92 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 93 | self.assertEqual(0, len(warnings)) |
| 94 | |
| 95 | def testSpecialFirstInclude3(self): |
| 96 | mock_input_api = MockInputApi() |
| 97 | contents = ['#include "some/path/foo.h"', |
| 98 | '#include "a/header.h"'] |
| 99 | mock_file = MockFile('some/path/foo_platform.cc', contents) |
| 100 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 101 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 102 | self.assertEqual(0, len(warnings)) |
| 103 | |
| 104 | def testSpecialFirstInclude4(self): |
| 105 | mock_input_api = MockInputApi() |
| 106 | contents = ['#include "some/path/bar.h"', |
| 107 | '#include "a/header.h"'] |
| 108 | mock_file = MockFile('some/path/foo_platform.cc', contents) |
| 109 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 110 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 111 | self.assertEqual(1, len(warnings)) |
| 112 | self.assertTrue('2' in warnings[0]) |
| 113 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 114 | def testSpecialFirstInclude5(self): |
| 115 | mock_input_api = MockInputApi() |
| 116 | contents = ['#include "some/other/path/foo.h"', |
| 117 | '#include "a/header.h"'] |
| 118 | mock_file = MockFile('some/path/foo-suffix.h', contents) |
| 119 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 120 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 121 | self.assertEqual(0, len(warnings)) |
| 122 | |
[email protected] | 3e83618c | 2013-10-09 22:32:33 | [diff] [blame] | 123 | def testSpecialFirstInclude6(self): |
| 124 | mock_input_api = MockInputApi() |
| 125 | contents = ['#include "some/other/path/foo_win.h"', |
| 126 | '#include <set>', |
| 127 | '#include "a/header.h"'] |
| 128 | mock_file = MockFile('some/path/foo_unittest_win.h', contents) |
| 129 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 130 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 131 | self.assertEqual(0, len(warnings)) |
| 132 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 133 | def testOrderAlreadyWrong(self): |
| 134 | scope = [(1, '#include "b.h"'), |
| 135 | (2, '#include "a.h"'), |
| 136 | (3, '#include "c.h"')] |
| 137 | mock_input_api = MockInputApi() |
| 138 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 139 | '', [3]) |
| 140 | self.assertEqual(0, len(warnings)) |
| 141 | |
| 142 | def testConflictAdded1(self): |
| 143 | scope = [(1, '#include "a.h"'), |
| 144 | (2, '#include "c.h"'), |
| 145 | (3, '#include "b.h"')] |
| 146 | mock_input_api = MockInputApi() |
| 147 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 148 | '', [2]) |
| 149 | self.assertEqual(1, len(warnings)) |
| 150 | self.assertTrue('3' in warnings[0]) |
| 151 | |
| 152 | def testConflictAdded2(self): |
| 153 | scope = [(1, '#include "c.h"'), |
| 154 | (2, '#include "b.h"'), |
| 155 | (3, '#include "d.h"')] |
| 156 | mock_input_api = MockInputApi() |
| 157 | warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, |
| 158 | '', [2]) |
| 159 | self.assertEqual(1, len(warnings)) |
| 160 | self.assertTrue('2' in warnings[0]) |
| 161 | |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 162 | def testIfElifElseEndif(self): |
| 163 | mock_input_api = MockInputApi() |
| 164 | contents = ['#include "e.h"', |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 165 | '#define foo', |
| 166 | '#include "f.h"', |
| 167 | '#undef foo', |
| 168 | '#include "e.h"', |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 169 | '#if foo', |
| 170 | '#include "d.h"', |
| 171 | '#elif bar', |
| 172 | '#include "c.h"', |
| 173 | '#else', |
| 174 | '#include "b.h"', |
| 175 | '#endif', |
| 176 | '#include "a.h"'] |
| 177 | mock_file = MockFile('', contents) |
| 178 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 179 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 180 | self.assertEqual(0, len(warnings)) |
| 181 | |
[email protected] | 23093b6 | 2013-09-20 12:16:30 | [diff] [blame] | 182 | def testExcludedIncludes(self): |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 183 | # #include <sys/...>'s can appear in any order. |
| 184 | mock_input_api = MockInputApi() |
| 185 | contents = ['#include <sys/b.h>', |
| 186 | '#include <sys/a.h>'] |
| 187 | mock_file = MockFile('', contents) |
| 188 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 189 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 190 | self.assertEqual(0, len(warnings)) |
| 191 | |
[email protected] | 23093b6 | 2013-09-20 12:16:30 | [diff] [blame] | 192 | contents = ['#include <atlbase.h>', |
| 193 | '#include <aaa.h>'] |
| 194 | mock_file = MockFile('', contents) |
| 195 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 196 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 197 | self.assertEqual(0, len(warnings)) |
| 198 | |
| 199 | contents = ['#include "build/build_config.h"', |
| 200 | '#include "aaa.h"'] |
| 201 | mock_file = MockFile('', contents) |
| 202 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 203 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
| 204 | self.assertEqual(0, len(warnings)) |
| 205 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 206 | def testCheckOnlyCFiles(self): |
| 207 | mock_input_api = MockInputApi() |
| 208 | mock_output_api = MockOutputApi() |
| 209 | contents = ['#include <b.h>', |
| 210 | '#include <a.h>'] |
| 211 | mock_file_cc = MockFile('something.cc', contents) |
| 212 | mock_file_h = MockFile('something.h', contents) |
| 213 | mock_file_other = MockFile('something.py', contents) |
| 214 | mock_input_api.files = [mock_file_cc, mock_file_h, mock_file_other] |
| 215 | warnings = PRESUBMIT._CheckIncludeOrder(mock_input_api, mock_output_api) |
| 216 | self.assertEqual(1, len(warnings)) |
| 217 | self.assertEqual(2, len(warnings[0].items)) |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 218 | self.assertEqual('promptOrNotify', warnings[0].type) |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 219 | |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 220 | def testUncheckableIncludes(self): |
| 221 | mock_input_api = MockInputApi() |
| 222 | contents = ['#include <windows.h>', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 223 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 224 | '#include "a.h"'] |
| 225 | mock_file = MockFile('', contents) |
| 226 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 227 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 228 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 229 | |
| 230 | contents = ['#include "gpu/command_buffer/gles_autogen.h"', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 231 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 232 | '#include "a.h"'] |
| 233 | mock_file = MockFile('', contents) |
| 234 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 235 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 236 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 237 | |
| 238 | contents = ['#include "gl_mock_autogen.h"', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 239 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 240 | '#include "a.h"'] |
| 241 | mock_file = MockFile('', contents) |
| 242 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 243 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 244 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 245 | |
| 246 | contents = ['#include "ipc/some_macros.h"', |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 247 | '#include "b.h"', |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 248 | '#include "a.h"'] |
| 249 | mock_file = MockFile('', contents) |
| 250 | warnings = PRESUBMIT._CheckIncludeOrderInFile( |
| 251 | mock_input_api, mock_file, range(1, len(contents) + 1)) |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 252 | self.assertEqual(1, len(warnings)) |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 253 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 254 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 255 | class VersionControlConflictsTest(unittest.TestCase): |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 256 | def testTypicalConflict(self): |
| 257 | lines = ['<<<<<<< HEAD', |
| 258 | ' base::ScopedTempDir temp_dir_;', |
| 259 | '=======', |
| 260 | ' ScopedTempDir temp_dir_;', |
| 261 | '>>>>>>> master'] |
| 262 | errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 263 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 264 | self.assertEqual(3, len(errors)) |
| 265 | self.assertTrue('1' in errors[0]) |
| 266 | self.assertTrue('3' in errors[1]) |
| 267 | self.assertTrue('5' in errors[2]) |
| 268 | |
dbeam | 95c35a2f | 2015-06-02 01:40:23 | [diff] [blame] | 269 | def testIgnoresReadmes(self): |
| 270 | lines = ['A First Level Header', |
| 271 | '====================', |
| 272 | '', |
| 273 | 'A Second Level Header', |
| 274 | '---------------------'] |
| 275 | errors = PRESUBMIT._CheckForVersionControlConflictsInFile( |
| 276 | MockInputApi(), MockFile('some/polymer/README.md', lines)) |
| 277 | self.assertEqual(0, len(errors)) |
| 278 | |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 279 | class UmaHistogramChangeMatchedOrNotTest(unittest.TestCase): |
| 280 | def testTypicalCorrectlyMatchedChange(self): |
| 281 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 282 | diff_xml = ['<histogram name="Bla.Foo.Dummy"> </histogram>'] |
| 283 | mock_input_api = MockInputApi() |
| 284 | mock_input_api.files = [ |
| 285 | MockFile('some/path/foo.cc', diff_cc), |
| 286 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 287 | ] |
| 288 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 289 | MockOutputApi()) |
| 290 | self.assertEqual(0, len(warnings)) |
| 291 | |
| 292 | def testTypicalNotMatchedChange(self): |
| 293 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 294 | mock_input_api = MockInputApi() |
| 295 | mock_input_api.files = [MockFile('some/path/foo.cc', diff_cc)] |
| 296 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 297 | MockOutputApi()) |
| 298 | self.assertEqual(1, len(warnings)) |
| 299 | self.assertEqual('warning', warnings[0].type) |
| 300 | |
| 301 | def testTypicalNotMatchedChangeViaSuffixes(self): |
| 302 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 303 | diff_xml = ['<histogram_suffixes name="SuperHistogram">', |
| 304 | ' <suffix name="Dummy"/>', |
| 305 | ' <affected-histogram name="Snafu.Dummy"/>', |
| 306 | '</histogram>'] |
| 307 | mock_input_api = MockInputApi() |
| 308 | mock_input_api.files = [ |
| 309 | MockFile('some/path/foo.cc', diff_cc), |
| 310 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 311 | ] |
| 312 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 313 | MockOutputApi()) |
| 314 | self.assertEqual(1, len(warnings)) |
| 315 | self.assertEqual('warning', warnings[0].type) |
| 316 | |
| 317 | def testTypicalCorrectlyMatchedChangeViaSuffixes(self): |
| 318 | diff_cc = ['UMA_HISTOGRAM_BOOL("Bla.Foo.Dummy", true)'] |
| 319 | diff_xml = ['<histogram_suffixes name="SuperHistogram">', |
| 320 | ' <suffix name="Dummy"/>', |
| 321 | ' <affected-histogram name="Bla.Foo"/>', |
| 322 | '</histogram>'] |
| 323 | mock_input_api = MockInputApi() |
| 324 | mock_input_api.files = [ |
| 325 | MockFile('some/path/foo.cc', diff_cc), |
| 326 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 327 | ] |
| 328 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 329 | MockOutputApi()) |
| 330 | self.assertEqual(0, len(warnings)) |
| 331 | |
| 332 | def testTypicalCorrectlyMatchedChangeViaSuffixesWithSeparator(self): |
| 333 | diff_cc = ['UMA_HISTOGRAM_BOOL("Snafu_Dummy", true)'] |
| 334 | diff_xml = ['<histogram_suffixes name="SuperHistogram" separator="_">', |
| 335 | ' <suffix name="Dummy"/>', |
| 336 | ' <affected-histogram name="Snafu"/>', |
| 337 | '</histogram>'] |
| 338 | mock_input_api = MockInputApi() |
| 339 | mock_input_api.files = [ |
| 340 | MockFile('some/path/foo.cc', diff_cc), |
| 341 | MockFile('tools/metrics/histograms/histograms.xml', diff_xml), |
| 342 | ] |
| 343 | warnings = PRESUBMIT._CheckUmaHistogramChanges(mock_input_api, |
| 344 | MockOutputApi()) |
| 345 | self.assertEqual(0, len(warnings)) |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 346 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 347 | class BadExtensionsTest(unittest.TestCase): |
| 348 | def testBadRejFile(self): |
| 349 | mock_input_api = MockInputApi() |
| 350 | mock_input_api.files = [ |
| 351 | MockFile('some/path/foo.cc', ''), |
| 352 | MockFile('some/path/foo.cc.rej', ''), |
| 353 | MockFile('some/path2/bar.h.rej', ''), |
| 354 | ] |
| 355 | |
| 356 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 357 | self.assertEqual(1, len(results)) |
| 358 | self.assertEqual(2, len(results[0].items)) |
| 359 | self.assertTrue('foo.cc.rej' in results[0].items[0]) |
| 360 | self.assertTrue('bar.h.rej' in results[0].items[1]) |
| 361 | |
| 362 | def testBadOrigFile(self): |
| 363 | mock_input_api = MockInputApi() |
| 364 | mock_input_api.files = [ |
| 365 | MockFile('other/path/qux.h.orig', ''), |
| 366 | MockFile('other/path/qux.h', ''), |
| 367 | MockFile('other/path/qux.cc', ''), |
| 368 | ] |
| 369 | |
| 370 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 371 | self.assertEqual(1, len(results)) |
| 372 | self.assertEqual(1, len(results[0].items)) |
| 373 | self.assertTrue('qux.h.orig' in results[0].items[0]) |
| 374 | |
| 375 | def testGoodFiles(self): |
| 376 | mock_input_api = MockInputApi() |
| 377 | mock_input_api.files = [ |
| 378 | MockFile('other/path/qux.h', ''), |
| 379 | MockFile('other/path/qux.cc', ''), |
| 380 | ] |
| 381 | results = PRESUBMIT._CheckPatchFiles(mock_input_api, MockOutputApi()) |
| 382 | self.assertEqual(0, len(results)) |
| 383 | |
| 384 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 385 | class CheckSingletonInHeadersTest(unittest.TestCase): |
| 386 | def testSingletonInArbitraryHeader(self): |
| 387 | diff_singleton_h = ['base::subtle::AtomicWord ' |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 388 | 'base::Singleton<Type, Traits, DifferentiatingType>::'] |
| 389 | diff_foo_h = ['// base::Singleton<Foo> in comment.', |
| 390 | 'friend class base::Singleton<Foo>'] |
oysteine | c430ad4 | 2015-10-22 20:55:24 | [diff] [blame] | 391 | diff_foo2_h = [' //Foo* bar = base::Singleton<Foo>::get();'] |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 392 | diff_bad_h = ['Foo* foo = base::Singleton<Foo>::get();'] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 393 | mock_input_api = MockInputApi() |
| 394 | mock_input_api.files = [MockAffectedFile('base/memory/singleton.h', |
| 395 | diff_singleton_h), |
| 396 | MockAffectedFile('foo.h', diff_foo_h), |
oysteine | c430ad4 | 2015-10-22 20:55:24 | [diff] [blame] | 397 | MockAffectedFile('foo2.h', diff_foo2_h), |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 398 | MockAffectedFile('bad.h', diff_bad_h)] |
| 399 | warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api, |
| 400 | MockOutputApi()) |
| 401 | self.assertEqual(1, len(warnings)) |
oysteine | c430ad4 | 2015-10-22 20:55:24 | [diff] [blame] | 402 | self.assertEqual(2, len(warnings[0].items)) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 403 | self.assertEqual('error', warnings[0].type) |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 404 | self.assertTrue('Found base::Singleton<T>' in warnings[0].message) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 405 | |
| 406 | def testSingletonInCC(self): |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 407 | diff_cc = ['Foo* foo = base::Singleton<Foo>::get();'] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 408 | mock_input_api = MockInputApi() |
| 409 | mock_input_api.files = [MockAffectedFile('some/path/foo.cc', diff_cc)] |
| 410 | warnings = PRESUBMIT._CheckSingletonInHeaders(mock_input_api, |
| 411 | MockOutputApi()) |
| 412 | self.assertEqual(0, len(warnings)) |
| 413 | |
| 414 | |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 415 | class CheckNoDeprecatedCompiledResourcesGYPTest(unittest.TestCase): |
| 416 | def testNoDeprecatedCompiledResourcsGYP(self): |
| 417 | mock_input_api = MockInputApi() |
| 418 | mock_input_api.files = [MockFile('some/js/compiled_resources.gyp', [])] |
| 419 | errors = PRESUBMIT._CheckNoDeprecatedCompiledResourcesGYP(mock_input_api, |
| 420 | MockOutputApi()) |
| 421 | self.assertEquals(1, len(errors)) |
| 422 | |
| 423 | mock_input_api.files = [MockFile('some/js/compiled_resources2.gyp', [])] |
| 424 | errors = PRESUBMIT._CheckNoDeprecatedCompiledResourcesGYP(mock_input_api, |
| 425 | MockOutputApi()) |
| 426 | self.assertEquals(0, len(errors)) |
| 427 | |
| 428 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 429 | class InvalidOSMacroNamesTest(unittest.TestCase): |
| 430 | def testInvalidOSMacroNames(self): |
| 431 | lines = ['#if defined(OS_WINDOWS)', |
| 432 | ' #elif defined(OS_WINDOW)', |
| 433 | ' # if defined(OS_MACOSX) || defined(OS_CHROME)', |
| 434 | '# else // defined(OS_MAC)', |
| 435 | '#endif // defined(OS_MACOS)'] |
| 436 | errors = PRESUBMIT._CheckForInvalidOSMacrosInFile( |
| 437 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 438 | self.assertEqual(len(lines), len(errors)) |
| 439 | self.assertTrue(':1 OS_WINDOWS' in errors[0]) |
| 440 | self.assertTrue('(did you mean OS_WIN?)' in errors[0]) |
| 441 | |
| 442 | def testValidOSMacroNames(self): |
| 443 | lines = ['#if defined(%s)' % m for m in PRESUBMIT._VALID_OS_MACROS] |
| 444 | errors = PRESUBMIT._CheckForInvalidOSMacrosInFile( |
| 445 | MockInputApi(), MockFile('some/path/foo_platform.cc', lines)) |
| 446 | self.assertEqual(0, len(errors)) |
| 447 | |
| 448 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 449 | class InvalidIfDefinedMacroNamesTest(unittest.TestCase): |
| 450 | def testInvalidIfDefinedMacroNames(self): |
| 451 | lines = ['#if defined(TARGET_IPHONE_SIMULATOR)', |
| 452 | '#if !defined(TARGET_IPHONE_SIMULATOR)', |
| 453 | '#elif defined(TARGET_IPHONE_SIMULATOR)', |
| 454 | '#ifdef TARGET_IPHONE_SIMULATOR', |
| 455 | ' # ifdef TARGET_IPHONE_SIMULATOR', |
| 456 | '# if defined(VALID) || defined(TARGET_IPHONE_SIMULATOR)', |
| 457 | '# else // defined(TARGET_IPHONE_SIMULATOR)', |
| 458 | '#endif // defined(TARGET_IPHONE_SIMULATOR)',] |
| 459 | errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile( |
| 460 | MockInputApi(), MockFile('some/path/source.mm', lines)) |
| 461 | self.assertEqual(len(lines), len(errors)) |
| 462 | |
| 463 | def testValidIfDefinedMacroNames(self): |
| 464 | lines = ['#if defined(FOO)', |
| 465 | '#ifdef BAR',] |
| 466 | errors = PRESUBMIT._CheckForInvalidIfDefinedMacrosInFile( |
| 467 | MockInputApi(), MockFile('some/path/source.cc', lines)) |
| 468 | self.assertEqual(0, len(errors)) |
| 469 | |
| 470 | |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 471 | class CheckAddedDepsHaveTetsApprovalsTest(unittest.TestCase): |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 472 | def testFilesToCheckForIncomingDeps(self): |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 473 | changed_lines = [ |
| 474 | '"+breakpad",', |
| 475 | '"+chrome/installer",', |
| 476 | '"+chrome/plugin/chrome_content_plugin_client.h",', |
| 477 | '"+chrome/utility/chrome_content_utility_client.h",', |
| 478 | '"+chromeos/chromeos_paths.h",', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 479 | '"+components/crash/content",', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 480 | '"+components/nacl/common",', |
| 481 | '"+content/public/browser/render_process_host.h",', |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 482 | '"+jni/fooblat.h",', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 483 | '"+grit", # For generated headers', |
| 484 | '"+grit/generated_resources.h",', |
| 485 | '"+grit/",', |
| 486 | '"+policy", # For generated headers and source', |
| 487 | '"+sandbox",', |
| 488 | '"+tools/memory_watcher",', |
| 489 | '"+third_party/lss/linux_syscall_support.h",', |
| 490 | ] |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 491 | files_to_check = PRESUBMIT._FilesToCheckForIncomingDeps(re, changed_lines) |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 492 | expected = set([ |
| 493 | 'breakpad/DEPS', |
| 494 | 'chrome/installer/DEPS', |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 495 | 'chrome/plugin/chrome_content_plugin_client.h', |
| 496 | 'chrome/utility/chrome_content_utility_client.h', |
| 497 | 'chromeos/chromeos_paths.h', |
sdefresne | 8ba0b88c | 2015-09-18 10:33:13 | [diff] [blame] | 498 | 'components/crash/content/DEPS', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 499 | 'components/nacl/common/DEPS', |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 500 | 'content/public/browser/render_process_host.h', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 501 | 'policy/DEPS', |
| 502 | 'sandbox/DEPS', |
| 503 | 'tools/memory_watcher/DEPS', |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 504 | 'third_party/lss/linux_syscall_support.h', |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 505 | ]) |
| 506 | self.assertEqual(expected, files_to_check); |
| 507 | |
| 508 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 509 | class JSONParsingTest(unittest.TestCase): |
| 510 | def testSuccess(self): |
| 511 | input_api = MockInputApi() |
| 512 | filename = 'valid_json.json' |
| 513 | contents = ['// This is a comment.', |
| 514 | '{', |
| 515 | ' "key1": ["value1", "value2"],', |
| 516 | ' "key2": 3 // This is an inline comment.', |
| 517 | '}' |
| 518 | ] |
| 519 | input_api.files = [MockFile(filename, contents)] |
| 520 | self.assertEqual(None, |
| 521 | PRESUBMIT._GetJSONParseError(input_api, filename)) |
| 522 | |
| 523 | def testFailure(self): |
| 524 | input_api = MockInputApi() |
| 525 | test_data = [ |
| 526 | ('invalid_json_1.json', |
| 527 | ['{ x }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 528 | 'Expecting property name:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 529 | ('invalid_json_2.json', |
| 530 | ['// Hello world!', |
| 531 | '{ "hello": "world }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 532 | 'Unterminated string starting at:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 533 | ('invalid_json_3.json', |
| 534 | ['{ "a": "b", "c": "d", }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 535 | 'Expecting property name:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 536 | ('invalid_json_4.json', |
| 537 | ['{ "a": "b" "c": "d" }'], |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 538 | 'Expecting , delimiter:'), |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 539 | ] |
| 540 | |
| 541 | input_api.files = [MockFile(filename, contents) |
| 542 | for (filename, contents, _) in test_data] |
| 543 | |
| 544 | for (filename, _, expected_error) in test_data: |
| 545 | actual_error = PRESUBMIT._GetJSONParseError(input_api, filename) |
[email protected] | a334327 | 2014-06-17 11:41:53 | [diff] [blame] | 546 | self.assertTrue(expected_error in str(actual_error), |
| 547 | "'%s' not found in '%s'" % (expected_error, actual_error)) |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 548 | |
| 549 | def testNoEatComments(self): |
| 550 | input_api = MockInputApi() |
| 551 | file_with_comments = 'file_with_comments.json' |
| 552 | contents_with_comments = ['// This is a comment.', |
| 553 | '{', |
| 554 | ' "key1": ["value1", "value2"],', |
| 555 | ' "key2": 3 // This is an inline comment.', |
| 556 | '}' |
| 557 | ] |
| 558 | file_without_comments = 'file_without_comments.json' |
| 559 | contents_without_comments = ['{', |
| 560 | ' "key1": ["value1", "value2"],', |
| 561 | ' "key2": 3', |
| 562 | '}' |
| 563 | ] |
| 564 | input_api.files = [MockFile(file_with_comments, contents_with_comments), |
| 565 | MockFile(file_without_comments, |
| 566 | contents_without_comments)] |
| 567 | |
| 568 | self.assertEqual('No JSON object could be decoded', |
| 569 | str(PRESUBMIT._GetJSONParseError(input_api, |
| 570 | file_with_comments, |
| 571 | eat_comments=False))) |
| 572 | self.assertEqual(None, |
| 573 | PRESUBMIT._GetJSONParseError(input_api, |
| 574 | file_without_comments, |
| 575 | eat_comments=False)) |
| 576 | |
| 577 | |
| 578 | class IDLParsingTest(unittest.TestCase): |
| 579 | def testSuccess(self): |
| 580 | input_api = MockInputApi() |
| 581 | filename = 'valid_idl_basics.idl' |
| 582 | contents = ['// Tests a valid IDL file.', |
| 583 | 'namespace idl_basics {', |
| 584 | ' enum EnumType {', |
| 585 | ' name1,', |
| 586 | ' name2', |
| 587 | ' };', |
| 588 | '', |
| 589 | ' dictionary MyType1 {', |
| 590 | ' DOMString a;', |
| 591 | ' };', |
| 592 | '', |
| 593 | ' callback Callback1 = void();', |
| 594 | ' callback Callback2 = void(long x);', |
| 595 | ' callback Callback3 = void(MyType1 arg);', |
| 596 | ' callback Callback4 = void(EnumType type);', |
| 597 | '', |
| 598 | ' interface Functions {', |
| 599 | ' static void function1();', |
| 600 | ' static void function2(long x);', |
| 601 | ' static void function3(MyType1 arg);', |
| 602 | ' static void function4(Callback1 cb);', |
| 603 | ' static void function5(Callback2 cb);', |
| 604 | ' static void function6(Callback3 cb);', |
| 605 | ' static void function7(Callback4 cb);', |
| 606 | ' };', |
| 607 | '', |
| 608 | ' interface Events {', |
| 609 | ' static void onFoo1();', |
| 610 | ' static void onFoo2(long x);', |
| 611 | ' static void onFoo2(MyType1 arg);', |
| 612 | ' static void onFoo3(EnumType type);', |
| 613 | ' };', |
| 614 | '};' |
| 615 | ] |
| 616 | input_api.files = [MockFile(filename, contents)] |
| 617 | self.assertEqual(None, |
| 618 | PRESUBMIT._GetIDLParseError(input_api, filename)) |
| 619 | |
| 620 | def testFailure(self): |
| 621 | input_api = MockInputApi() |
| 622 | test_data = [ |
| 623 | ('invalid_idl_1.idl', |
| 624 | ['//', |
| 625 | 'namespace test {', |
| 626 | ' dictionary {', |
| 627 | ' DOMString s;', |
| 628 | ' };', |
| 629 | '};'], |
| 630 | 'Unexpected "{" after keyword "dictionary".\n'), |
| 631 | # TODO(yoz): Disabled because it causes the IDL parser to hang. |
| 632 | # See crbug.com/363830. |
| 633 | # ('invalid_idl_2.idl', |
| 634 | # (['namespace test {', |
| 635 | # ' dictionary MissingSemicolon {', |
| 636 | # ' DOMString a', |
| 637 | # ' DOMString b;', |
| 638 | # ' };', |
| 639 | # '};'], |
| 640 | # 'Unexpected symbol DOMString after symbol a.'), |
| 641 | ('invalid_idl_3.idl', |
| 642 | ['//', |
| 643 | 'namespace test {', |
| 644 | ' enum MissingComma {', |
| 645 | ' name1', |
| 646 | ' name2', |
| 647 | ' };', |
| 648 | '};'], |
| 649 | 'Unexpected symbol name2 after symbol name1.'), |
| 650 | ('invalid_idl_4.idl', |
| 651 | ['//', |
| 652 | 'namespace test {', |
| 653 | ' enum TrailingComma {', |
| 654 | ' name1,', |
| 655 | ' name2,', |
| 656 | ' };', |
| 657 | '};'], |
| 658 | 'Trailing comma in block.'), |
| 659 | ('invalid_idl_5.idl', |
| 660 | ['//', |
| 661 | 'namespace test {', |
| 662 | ' callback Callback1 = void(;', |
| 663 | '};'], |
| 664 | 'Unexpected ";" after "(".'), |
| 665 | ('invalid_idl_6.idl', |
| 666 | ['//', |
| 667 | 'namespace test {', |
| 668 | ' callback Callback1 = void(long );', |
| 669 | '};'], |
| 670 | 'Unexpected ")" after symbol long.'), |
| 671 | ('invalid_idl_7.idl', |
| 672 | ['//', |
| 673 | 'namespace test {', |
| 674 | ' interace Events {', |
| 675 | ' static void onFoo1();', |
| 676 | ' };', |
| 677 | '};'], |
| 678 | 'Unexpected symbol Events after symbol interace.'), |
| 679 | ('invalid_idl_8.idl', |
| 680 | ['//', |
| 681 | 'namespace test {', |
| 682 | ' interface NotEvent {', |
| 683 | ' static void onFoo1();', |
| 684 | ' };', |
| 685 | '};'], |
| 686 | 'Did not process Interface Interface(NotEvent)'), |
| 687 | ('invalid_idl_9.idl', |
| 688 | ['//', |
| 689 | 'namespace test {', |
| 690 | ' interface {', |
| 691 | ' static void function1();', |
| 692 | ' };', |
| 693 | '};'], |
| 694 | 'Interface missing name.'), |
| 695 | ] |
| 696 | |
| 697 | input_api.files = [MockFile(filename, contents) |
| 698 | for (filename, contents, _) in test_data] |
| 699 | |
| 700 | for (filename, _, expected_error) in test_data: |
| 701 | actual_error = PRESUBMIT._GetIDLParseError(input_api, filename) |
| 702 | self.assertTrue(expected_error in str(actual_error), |
| 703 | "'%s' not found in '%s'" % (expected_error, actual_error)) |
| 704 | |
| 705 | |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 706 | class TryServerMasterTest(unittest.TestCase): |
| 707 | def testTryServerMasters(self): |
| 708 | bots = { |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 709 | 'tryserver.chromium.mac': [ |
| 710 | 'ios_dbg_simulator', |
| 711 | 'ios_rel_device', |
| 712 | 'ios_rel_device_ninja', |
| 713 | 'mac_asan', |
| 714 | 'mac_asan_64', |
| 715 | 'mac_chromium_compile_dbg', |
| 716 | 'mac_chromium_compile_rel', |
| 717 | 'mac_chromium_dbg', |
| 718 | 'mac_chromium_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 719 | 'mac_nacl_sdk', |
| 720 | 'mac_nacl_sdk_build', |
| 721 | 'mac_rel_naclmore', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 722 | 'mac_x64_rel', |
| 723 | 'mac_xcodebuild', |
| 724 | ], |
| 725 | 'tryserver.chromium.linux': [ |
| 726 | 'android_aosp', |
| 727 | 'android_chromium_gn_compile_dbg', |
| 728 | 'android_chromium_gn_compile_rel', |
| 729 | 'android_clang_dbg', |
| 730 | 'android_dbg', |
| 731 | 'android_dbg_recipe', |
| 732 | 'android_dbg_triggered_tests', |
| 733 | 'android_dbg_triggered_tests_recipe', |
| 734 | 'android_fyi_dbg', |
| 735 | 'android_fyi_dbg_triggered_tests', |
| 736 | 'android_rel', |
| 737 | 'android_rel_triggered_tests', |
| 738 | 'android_x86_dbg', |
| 739 | 'blink_android_compile_dbg', |
| 740 | 'blink_android_compile_rel', |
| 741 | 'blink_presubmit', |
| 742 | 'chromium_presubmit', |
| 743 | 'linux_arm_cross_compile', |
| 744 | 'linux_arm_tester', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 745 | 'linux_chromeos_asan', |
| 746 | 'linux_chromeos_browser_asan', |
| 747 | 'linux_chromeos_valgrind', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 748 | 'linux_chromium_chromeos_dbg', |
| 749 | 'linux_chromium_chromeos_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 750 | 'linux_chromium_compile_dbg', |
| 751 | 'linux_chromium_compile_rel', |
| 752 | 'linux_chromium_dbg', |
| 753 | 'linux_chromium_gn_dbg', |
| 754 | 'linux_chromium_gn_rel', |
| 755 | 'linux_chromium_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 756 | 'linux_chromium_trusty32_dbg', |
| 757 | 'linux_chromium_trusty32_rel', |
| 758 | 'linux_chromium_trusty_dbg', |
| 759 | 'linux_chromium_trusty_rel', |
| 760 | 'linux_clang_tsan', |
| 761 | 'linux_ecs_ozone', |
| 762 | 'linux_layout', |
| 763 | 'linux_layout_asan', |
| 764 | 'linux_layout_rel', |
| 765 | 'linux_layout_rel_32', |
| 766 | 'linux_nacl_sdk', |
| 767 | 'linux_nacl_sdk_bionic', |
| 768 | 'linux_nacl_sdk_bionic_build', |
| 769 | 'linux_nacl_sdk_build', |
| 770 | 'linux_redux', |
| 771 | 'linux_rel_naclmore', |
| 772 | 'linux_rel_precise32', |
| 773 | 'linux_valgrind', |
| 774 | 'tools_build_presubmit', |
| 775 | ], |
| 776 | 'tryserver.chromium.win': [ |
| 777 | 'win8_aura', |
| 778 | 'win8_chromium_dbg', |
| 779 | 'win8_chromium_rel', |
| 780 | 'win_chromium_compile_dbg', |
| 781 | 'win_chromium_compile_rel', |
| 782 | 'win_chromium_dbg', |
| 783 | 'win_chromium_rel', |
| 784 | 'win_chromium_rel', |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 785 | 'win_chromium_x64_dbg', |
| 786 | 'win_chromium_x64_rel', |
| 787 | 'win_drmemory', |
| 788 | 'win_nacl_sdk', |
| 789 | 'win_nacl_sdk_build', |
| 790 | 'win_rel_naclmore', |
| 791 | ], |
| 792 | } |
| 793 | for master, bots in bots.iteritems(): |
| 794 | for bot in bots: |
| 795 | self.assertEqual(master, PRESUBMIT.GetTryServerMasterForBot(bot), |
| 796 | 'bot=%s: expected %s, computed %s' % ( |
| 797 | bot, master, PRESUBMIT.GetTryServerMasterForBot(bot))) |
| 798 | |
| 799 | |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 800 | class UserMetricsActionTest(unittest.TestCase): |
| 801 | def testUserMetricsActionInActions(self): |
| 802 | input_api = MockInputApi() |
| 803 | file_with_user_action = 'file_with_user_action.cc' |
| 804 | contents_with_user_action = [ |
| 805 | 'base::UserMetricsAction("AboutChrome")' |
| 806 | ] |
| 807 | |
| 808 | input_api.files = [MockFile(file_with_user_action, |
| 809 | contents_with_user_action)] |
| 810 | |
| 811 | self.assertEqual( |
| 812 | [], PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi())) |
| 813 | |
| 814 | |
| 815 | def testUserMetricsActionNotAddedToActions(self): |
| 816 | input_api = MockInputApi() |
| 817 | file_with_user_action = 'file_with_user_action.cc' |
| 818 | contents_with_user_action = [ |
| 819 | 'base::UserMetricsAction("NotInActionsXml")' |
| 820 | ] |
| 821 | |
| 822 | input_api.files = [MockFile(file_with_user_action, |
| 823 | contents_with_user_action)] |
| 824 | |
| 825 | output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()) |
| 826 | self.assertEqual( |
| 827 | ('File %s line %d: %s is missing in ' |
| 828 | 'tools/metrics/actions/actions.xml. Please run ' |
| 829 | 'tools/metrics/actions/extract_actions.py to update.' |
| 830 | % (file_with_user_action, 1, 'NotInActionsXml')), |
| 831 | output[0].message) |
| 832 | |
| 833 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame^] | 834 | class PydepsNeedsUpdatingTest(unittest.TestCase): |
| 835 | |
| 836 | class MockSubprocess(object): |
| 837 | CalledProcessError = subprocess.CalledProcessError |
| 838 | |
| 839 | def setUp(self): |
| 840 | mock_all_pydeps = ['A.pydeps', 'B.pydeps'] |
| 841 | self.old_ALL_PYDEPS_FILES = PRESUBMIT._ALL_PYDEPS_FILES |
| 842 | PRESUBMIT._ALL_PYDEPS_FILES = mock_all_pydeps |
| 843 | self.mock_input_api = MockInputApi() |
| 844 | self.mock_output_api = MockOutputApi() |
| 845 | self.mock_input_api.subprocess = PydepsNeedsUpdatingTest.MockSubprocess() |
| 846 | self.checker = PRESUBMIT.PydepsChecker(self.mock_input_api, mock_all_pydeps) |
| 847 | self.checker._file_cache = { |
| 848 | 'A.pydeps': '# Generated by:\n# CMD A\nA.py\nC.py\n', |
| 849 | 'B.pydeps': '# Generated by:\n# CMD B\nB.py\nC.py\n', |
| 850 | } |
| 851 | |
| 852 | def tearDown(self): |
| 853 | PRESUBMIT._ALL_PYDEPS_FILES = self.old_ALL_PYDEPS_FILES |
| 854 | |
| 855 | def _RunCheck(self): |
| 856 | return PRESUBMIT._CheckPydepsNeedsUpdating(self.mock_input_api, |
| 857 | self.mock_output_api, |
| 858 | checker_for_tests=self.checker) |
| 859 | |
| 860 | def testAddedPydep(self): |
| 861 | self.mock_input_api.files = [ |
| 862 | MockAffectedFile('new.pydeps', [], action='A'), |
| 863 | ] |
| 864 | |
| 865 | results = self._RunCheck() |
| 866 | self.assertEqual(1, len(results)) |
| 867 | self.assertTrue('PYDEPS_FILES' in str(results[0])) |
| 868 | |
| 869 | def testRemovedPydep(self): |
| 870 | self.mock_input_api.files = [ |
| 871 | MockAffectedFile(PRESUBMIT._ALL_PYDEPS_FILES[0], [], action='D'), |
| 872 | ] |
| 873 | |
| 874 | results = self._RunCheck() |
| 875 | self.assertEqual(1, len(results)) |
| 876 | self.assertTrue('PYDEPS_FILES' in str(results[0])) |
| 877 | |
| 878 | def testRandomPyIgnored(self): |
| 879 | self.mock_input_api.files = [ |
| 880 | MockAffectedFile('random.py', []), |
| 881 | ] |
| 882 | |
| 883 | results = self._RunCheck() |
| 884 | self.assertEqual(0, len(results), 'Unexpected results: %r' % results) |
| 885 | |
| 886 | def testRelevantPyNoChange(self): |
| 887 | self.mock_input_api.files = [ |
| 888 | MockAffectedFile('A.py', []), |
| 889 | ] |
| 890 | |
| 891 | def mock_check_output(cmd, shell=False): |
| 892 | self.assertEqual('CMD A --output ""', cmd) |
| 893 | return self.checker._file_cache['A.pydeps'] |
| 894 | |
| 895 | self.mock_input_api.subprocess.check_output = mock_check_output |
| 896 | |
| 897 | results = self._RunCheck() |
| 898 | self.assertEqual(0, len(results), 'Unexpected results: %r' % results) |
| 899 | |
| 900 | def testRelevantPyOneChange(self): |
| 901 | self.mock_input_api.files = [ |
| 902 | MockAffectedFile('A.py', []), |
| 903 | ] |
| 904 | |
| 905 | def mock_check_output(cmd, shell=False): |
| 906 | self.assertEqual('CMD A --output ""', cmd) |
| 907 | return 'changed data' |
| 908 | |
| 909 | self.mock_input_api.subprocess.check_output = mock_check_output |
| 910 | |
| 911 | results = self._RunCheck() |
| 912 | self.assertEqual(1, len(results)) |
| 913 | self.assertTrue('File is stale' in str(results[0])) |
| 914 | |
| 915 | def testRelevantPyTwoChanges(self): |
| 916 | self.mock_input_api.files = [ |
| 917 | MockAffectedFile('C.py', []), |
| 918 | ] |
| 919 | |
| 920 | def mock_check_output(cmd, shell=False): |
| 921 | return 'changed data' |
| 922 | |
| 923 | self.mock_input_api.subprocess.check_output = mock_check_output |
| 924 | |
| 925 | results = self._RunCheck() |
| 926 | self.assertEqual(2, len(results)) |
| 927 | self.assertTrue('File is stale' in str(results[0])) |
| 928 | self.assertTrue('File is stale' in str(results[1])) |
| 929 | |
| 930 | |
dgn | 4401aa5 | 2015-04-29 16:26:17 | [diff] [blame] | 931 | class LogUsageTest(unittest.TestCase): |
| 932 | |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 933 | def testCheckAndroidCrLogUsage(self): |
| 934 | mock_input_api = MockInputApi() |
| 935 | mock_output_api = MockOutputApi() |
| 936 | |
| 937 | mock_input_api.files = [ |
| 938 | MockAffectedFile('RandomStuff.java', [ |
| 939 | 'random stuff' |
| 940 | ]), |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 941 | MockAffectedFile('HasAndroidLog.java', [ |
| 942 | 'import android.util.Log;', |
| 943 | 'some random stuff', |
| 944 | 'Log.d("TAG", "foo");', |
| 945 | ]), |
| 946 | MockAffectedFile('HasExplicitUtilLog.java', [ |
| 947 | 'some random stuff', |
| 948 | 'android.util.Log.d("TAG", "foo");', |
| 949 | ]), |
| 950 | MockAffectedFile('IsInBasePackage.java', [ |
| 951 | 'package org.chromium.base;', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 952 | 'private static final String TAG = "cr_Foo";', |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 953 | 'Log.d(TAG, "foo");', |
| 954 | ]), |
| 955 | MockAffectedFile('IsInBasePackageButImportsLog.java', [ |
| 956 | 'package org.chromium.base;', |
| 957 | 'import android.util.Log;', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 958 | 'private static final String TAG = "cr_Foo";', |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 959 | 'Log.d(TAG, "foo");', |
| 960 | ]), |
| 961 | MockAffectedFile('HasBothLog.java', [ |
| 962 | 'import org.chromium.base.Log;', |
| 963 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 964 | 'private static final String TAG = "cr_Foo";', |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 965 | 'Log.d(TAG, "foo");', |
| 966 | 'android.util.Log.d("TAG", "foo");', |
| 967 | ]), |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 968 | MockAffectedFile('HasCorrectTag.java', [ |
| 969 | 'import org.chromium.base.Log;', |
| 970 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 971 | 'private static final String TAG = "cr_Foo";', |
| 972 | 'Log.d(TAG, "foo");', |
| 973 | ]), |
| 974 | MockAffectedFile('HasOldTag.java', [ |
| 975 | 'import org.chromium.base.Log;', |
| 976 | 'some random stuff', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 977 | 'private static final String TAG = "cr.Foo";', |
| 978 | 'Log.d(TAG, "foo");', |
| 979 | ]), |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 980 | MockAffectedFile('HasDottedTag.java', [ |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 981 | 'import org.chromium.base.Log;', |
| 982 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 983 | 'private static final String TAG = "cr_foo.bar";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 984 | 'Log.d(TAG, "foo");', |
| 985 | ]), |
| 986 | MockAffectedFile('HasNoTagDecl.java', [ |
| 987 | 'import org.chromium.base.Log;', |
| 988 | 'some random stuff', |
| 989 | 'Log.d(TAG, "foo");', |
| 990 | ]), |
| 991 | MockAffectedFile('HasIncorrectTagDecl.java', [ |
| 992 | 'import org.chromium.base.Log;', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 993 | 'private static final String TAHG = "cr_Foo";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 994 | 'some random stuff', |
| 995 | 'Log.d(TAG, "foo");', |
| 996 | ]), |
| 997 | MockAffectedFile('HasInlineTag.java', [ |
| 998 | 'import org.chromium.base.Log;', |
| 999 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1000 | 'private static final String TAG = "cr_Foo";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1001 | 'Log.d("TAG", "foo");', |
| 1002 | ]), |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1003 | MockAffectedFile('HasUnprefixedTag.java', [ |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1004 | 'import org.chromium.base.Log;', |
| 1005 | 'some random stuff', |
| 1006 | 'private static final String TAG = "rubbish";', |
| 1007 | 'Log.d(TAG, "foo");', |
| 1008 | ]), |
| 1009 | MockAffectedFile('HasTooLongTag.java', [ |
| 1010 | 'import org.chromium.base.Log;', |
| 1011 | 'some random stuff', |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1012 | 'private static final String TAG = "21_charachers_long___";', |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1013 | 'Log.d(TAG, "foo");', |
| 1014 | ]), |
| 1015 | ] |
| 1016 | |
| 1017 | msgs = PRESUBMIT._CheckAndroidCrLogUsage( |
| 1018 | mock_input_api, mock_output_api) |
| 1019 | |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1020 | self.assertEqual(5, len(msgs), |
| 1021 | 'Expected %d items, found %d: %s' % (5, len(msgs), msgs)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1022 | |
| 1023 | # Declaration format |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1024 | nb = len(msgs[0].items) |
| 1025 | self.assertEqual(2, nb, |
| 1026 | 'Expected %d items, found %d: %s' % (2, nb, msgs[0].items)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1027 | self.assertTrue('HasNoTagDecl.java' in msgs[0].items) |
| 1028 | self.assertTrue('HasIncorrectTagDecl.java' in msgs[0].items) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1029 | |
| 1030 | # Tag length |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1031 | nb = len(msgs[1].items) |
| 1032 | self.assertEqual(1, nb, |
| 1033 | 'Expected %d items, found %d: %s' % (1, nb, msgs[1].items)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1034 | self.assertTrue('HasTooLongTag.java' in msgs[1].items) |
| 1035 | |
| 1036 | # Tag must be a variable named TAG |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1037 | nb = len(msgs[2].items) |
| 1038 | self.assertEqual(1, nb, |
| 1039 | 'Expected %d items, found %d: %s' % (1, nb, msgs[2].items)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1040 | self.assertTrue('HasInlineTag.java:4' in msgs[2].items) |
| 1041 | |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1042 | # Util Log usage |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1043 | nb = len(msgs[3].items) |
| 1044 | self.assertEqual(2, nb, |
| 1045 | 'Expected %d items, found %d: %s' % (2, nb, msgs[3].items)) |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1046 | self.assertTrue('HasAndroidLog.java:3' in msgs[3].items) |
| 1047 | self.assertTrue('IsInBasePackageButImportsLog.java:4' in msgs[3].items) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1048 | |
dgn | 38736db | 2015-09-18 19:20:51 | [diff] [blame] | 1049 | # Tag must not contain |
| 1050 | nb = len(msgs[4].items) |
| 1051 | self.assertEqual(2, nb, |
| 1052 | 'Expected %d items, found %d: %s' % (2, nb, msgs[4].items)) |
| 1053 | self.assertTrue('HasDottedTag.java' in msgs[4].items) |
| 1054 | self.assertTrue('HasOldTag.java' in msgs[4].items) |
| 1055 | |
reillyi | 3896573 | 2015-11-16 18:27:33 | [diff] [blame] | 1056 | class HardcodedGoogleHostsTest(unittest.TestCase): |
| 1057 | |
| 1058 | def testWarnOnAssignedLiterals(self): |
| 1059 | input_api = MockInputApi() |
| 1060 | input_api.files = [ |
| 1061 | MockFile('content/file.cc', |
| 1062 | ['char* host = "https://www.google.com";']), |
| 1063 | MockFile('content/file.cc', |
| 1064 | ['char* host = "https://www.googleapis.com";']), |
| 1065 | MockFile('content/file.cc', |
| 1066 | ['char* host = "https://clients1.google.com";']), |
| 1067 | ] |
| 1068 | |
| 1069 | warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( |
| 1070 | input_api, MockOutputApi()) |
| 1071 | self.assertEqual(1, len(warnings)) |
| 1072 | self.assertEqual(3, len(warnings[0].items)) |
| 1073 | |
| 1074 | def testAllowInComment(self): |
| 1075 | input_api = MockInputApi() |
| 1076 | input_api.files = [ |
| 1077 | MockFile('content/file.cc', |
| 1078 | ['char* host = "https://www.aol.com"; // google.com']) |
| 1079 | ] |
| 1080 | |
| 1081 | warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( |
| 1082 | input_api, MockOutputApi()) |
| 1083 | self.assertEqual(0, len(warnings)) |
| 1084 | |
dgn | 4401aa5 | 2015-04-29 16:26:17 | [diff] [blame] | 1085 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 1086 | if __name__ == '__main__': |
| 1087 | unittest.main() |