diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 47753d12ee..e30960e4a3 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -36,7 +36,7 @@ OFF_BY_DEFAULT_LINT_FILTERS = [ # they are undesirable in some way. # # Justifications for each filter: -# - build/c++11 : Include file and feature blacklists are +# - build/c++11 : Include file and feature blocklists are # google3-specific # - runtime/references : No longer banned by Google style guide OFF_UNLESS_MANUALLY_ENABLED_LINT_FILTERS = [ @@ -359,7 +359,7 @@ def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None): """Checks that there are no tab characters in any of the text files to be submitted. """ - # In addition to the filter, make sure that makefiles are blacklisted. + # In addition to the filter, make sure that makefiles are blocklisted. if not source_file_filter: # It's the default filter. source_file_filter = input_api.FilterSourceFile @@ -608,13 +608,15 @@ def CheckTreeIsOpen(input_api, output_api, return [] def GetUnitTestsInDirectory( - input_api, output_api, directory, whitelist=None, blacklist=None, env=None, - run_on_python2=True, run_on_python3=True): + input_api, output_api, directory, allowlist=None, blocklist=None, env=None, + run_on_python2=True, run_on_python3=True, whitelist=None, blacklist=None): """Lists all files in a directory and runs them. Doesn't recurse. - It's mainly a wrapper for RunUnitTests. Use whitelist and blacklist to filter + It's mainly a wrapper for RunUnitTests. Use allowlist and blocklist to filter tests accordingly. """ + allowlist = allowlist or whitelist + blocklist = blocklist or blacklist unit_tests = [] test_path = input_api.os_path.abspath( input_api.os_path.join(input_api.PresubmitLocalPath(), directory)) @@ -628,9 +630,9 @@ def GetUnitTestsInDirectory( fullpath = input_api.os_path.join(test_path, filename) if not input_api.os_path.isfile(fullpath): continue - if whitelist and not check(filename, whitelist): + if allowlist and not check(filename, allowlist): continue - if blacklist and check(filename, blacklist): + if blocklist and check(filename, blocklist): continue unit_tests.append(input_api.os_path.join(directory, filename)) to_run += 1 @@ -640,7 +642,7 @@ def GetUnitTestsInDirectory( return [ output_api.PresubmitPromptWarning( 'Out of %d files, found none that matched w=%r, b=%r in directory %s' - % (found, whitelist, blacklist, directory)) + % (found, allowlist, blocklist, directory)) ] return GetUnitTests( input_api, output_api, unit_tests, env, run_on_python2, run_on_python3) @@ -698,16 +700,22 @@ def GetUnitTests( def GetUnitTestsRecursively(input_api, output_api, directory, - whitelist, blacklist, run_on_python2=True, - run_on_python3=True): + allowlist=None, blocklist=None, run_on_python2=True, + run_on_python3=True, whitelist=None, + blacklist=None): """Gets all files in the directory tree (git repo) that match the whitelist. Restricts itself to only find files within the Change's source repo, not dependencies. """ + allowlist = allowlist or whitelist + blocklist = blocklist or blacklist + assert allowlist is not None + assert blocklist is not None + def check(filename): - return (any(input_api.re.match(f, filename) for f in whitelist) and - not any(input_api.re.match(f, filename) for f in blacklist)) + return (any(input_api.re.match(f, filename) for f in allowlist) and + not any(input_api.re.match(f, filename) for f in blocklist)) tests = [] @@ -722,7 +730,7 @@ def GetUnitTestsRecursively(input_api, output_api, directory, return [ output_api.PresubmitPromptWarning( 'Out of %d files, found none that matched w=%r, b=%r in directory %s' - % (found, whitelist, blacklist, directory)) + % (found, allowlist, blocklist, directory)) ] return GetUnitTests(input_api, output_api, tests, @@ -806,7 +814,7 @@ def RunPythonUnitTests(input_api, *args, **kwargs): GetPythonUnitTests(input_api, *args, **kwargs), False) -def _FetchAllFiles(input_api, white_list, black_list): +def _FetchAllFiles(input_api, allow_list, block_list): """Hack to fetch all files.""" # We cannot use AffectedFiles here because we want to test every python # file on each single python change. It's because a change in a python file @@ -825,26 +833,27 @@ def _FetchAllFiles(input_api, white_list, black_list): path_len = len(input_api.PresubmitLocalPath()) for dirpath, dirnames, filenames in input_api.os_walk( input_api.PresubmitLocalPath()): - # Passes dirnames in black list to speed up search. + # Passes dirnames in block list to speed up search. for item in dirnames[:]: filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] - if Find(filepath, black_list): + if Find(filepath, block_list): dirnames.remove(item) for item in filenames: filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] - if Find(filepath, white_list) and not Find(filepath, black_list): + if Find(filepath, allow_list) and not Find(filepath, block_list): files.append(filepath) return files -def GetPylint(input_api, output_api, white_list=None, black_list=None, - disabled_warnings=None, extra_paths_list=None, pylintrc=None): +def GetPylint(input_api, output_api, allow_list=None, block_list=None, + disabled_warnings=None, extra_paths_list=None, pylintrc=None, + white_list=None, black_list=None): """Run pylint on python files. - The default white_list enforces looking only at *.py files. + The default allow_list enforces looking only at *.py files. """ - white_list = tuple(white_list or (r'.*\.py$',)) - black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) + allow_list = tuple(allow_list or white_list or (r'.*\.py$',)) + block_list = tuple(block_list or black_list or input_api.DEFAULT_BLOCK_LIST) extra_paths_list = extra_paths_list or [] if input_api.is_committing: @@ -868,7 +877,7 @@ def GetPylint(input_api, output_api, white_list=None, black_list=None, input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()), '') return input_api.re.escape(prefix) + regex src_filter = lambda x: input_api.FilterSourceFile( - x, map(rel_path, white_list), map(rel_path, black_list)) + x, map(rel_path, allow_list), map(rel_path, block_list)) if not input_api.AffectedSourceFiles(src_filter): input_api.logging.info('Skipping pylint: no matching changes.') return [] @@ -881,7 +890,7 @@ def GetPylint(input_api, output_api, white_list=None, black_list=None, if disabled_warnings: extra_args.extend(['-d', ','.join(disabled_warnings)]) - files = _FetchAllFiles(input_api, white_list, black_list) + files = _FetchAllFiles(input_api, allow_list, block_list) if not files: return [] files.sort() @@ -1175,15 +1184,15 @@ def PanProjectChecks(input_api, output_api, } results = [] - # This code loads the default black list (e.g. third_party, experimental, etc) - # and add our black list (breakpad, skia and v8 are still not following + # This code loads the default block list (e.g. third_party, experimental, etc) + # and add our block list (breakpad, skia and v8 are still not following # google style and are not really living this repository). # See presubmit_support.py InputApi.FilterSourceFile for the (simple) usage. - black_list = input_api.DEFAULT_BLACK_LIST + excluded_paths - white_list = input_api.DEFAULT_WHITE_LIST + text_files - sources = lambda x: input_api.FilterSourceFile(x, black_list=black_list) + block_list = input_api.DEFAULT_BLOCK_LIST + excluded_paths + allow_list = input_api.DEFAULT_ALLOW_LIST + text_files + sources = lambda x: input_api.FilterSourceFile(x, block_list=block_list) text_files = lambda x: input_api.FilterSourceFile( - x, black_list=black_list, white_list=white_list) + x, block_list=block_list, allow_list=allow_list) snapshot_memory = [] def snapshot(msg): diff --git a/presubmit_support.py b/presubmit_support.py index f58530e0d3..a31dbab354 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -491,9 +491,9 @@ class InputApi(object): # perspective. Don't modify this list from a presubmit script! # # Files without an extension aren't included in the list. If you want to - # filter them as source files, add r'(^|.*?[\\\/])[^.]+$' to the white list. - # Note that ALL CAPS files are black listed in DEFAULT_BLACK_LIST below. - DEFAULT_WHITE_LIST = ( + # filter them as source files, add r'(^|.*?[\\\/])[^.]+$' to the allow list. + # Note that ALL CAPS files are blocked in DEFAULT_BLOCK_LIST below. + DEFAULT_ALLOW_LIST = ( # C++ and friends r'.+\.c$', r'.+\.cc$', r'.+\.cpp$', r'.+\.h$', r'.+\.m$', r'.+\.mm$', r'.+\.inl$', r'.+\.asm$', r'.+\.hxx$', r'.+\.hpp$', r'.+\.s$', r'.+\.S$', @@ -506,7 +506,7 @@ class InputApi(object): # Path regexp that should be excluded from being considered containing source # files. Don't modify this list from a presubmit script! - DEFAULT_BLACK_LIST = ( + DEFAULT_BLOCK_LIST = ( r'testing_support[\\\/]google_appengine[\\\/].*', r'.*\bexperimental[\\\/].*', # Exclude third_party/.* but NOT third_party/{WebKit,blink} @@ -527,6 +527,16 @@ class InputApi(object): r'.+\.patch$', ) + # TODO(https://crbug.com/1098562): Remove once no longer used + @property + def DEFAULT_WHITE_LIST(self): + return self.DEFAULT_ALLOW_LIST + + # TODO(https://crbug.com/1098562): Remove once no longer used + @property + def DEFAULT_BLACK_LIST(self): + return self.DEFAULT_BLOCK_LIST + def __init__(self, change, presubmit_path, is_committing, verbose, gerrit_obj, dry_run=None, thread_pool=None, parallel=False): """Builds an InputApi object. @@ -673,25 +683,35 @@ class InputApi(object): """An alias to AffectedTestableFiles for backwards compatibility.""" return self.AffectedTestableFiles(include_deletes=include_deletes) - def FilterSourceFile(self, affected_file, white_list=None, black_list=None): + def FilterSourceFile(self, affected_file, allow_list=None, block_list=None, + white_list=None, black_list=None): """Filters out files that aren't considered 'source file'. - If white_list or black_list is None, InputApi.DEFAULT_WHITE_LIST - and InputApi.DEFAULT_BLACK_LIST is used respectively. + If allow_list or block_list is None, InputApi.DEFAULT_ALLOW_LIST + and InputApi.DEFAULT_BLOCK_LIST is used respectively. The lists will be compiled as regular expression and AffectedFile.LocalPath() needs to pass both list. + Note: if allow_list or block_list is not set, and white_list or black_list + is, then those values are used. This is used for backward compatibility + reasons. + Note: Copy-paste this function to suit your needs or use a lambda function. """ + if allow_list is None: + allow_list = white_list + if block_list is None: + block_list = black_list + def Find(affected_file, items): local_path = affected_file.LocalPath() for item in items: if self.re.match(item, local_path): return True return False - return (Find(affected_file, white_list or self.DEFAULT_WHITE_LIST) and - not Find(affected_file, black_list or self.DEFAULT_BLACK_LIST)) + return (Find(affected_file, allow_list or self.DEFAULT_ALLOW_LIST) and + not Find(affected_file, block_list or self.DEFAULT_BLOCK_LIST)) def AffectedSourceFiles(self, source_file): """Filter the list of AffectedTestableFiles by the function source_file. diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index 7389bd5a1a..f52669569f 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -1376,8 +1376,10 @@ class InputApiUnittest(PresubmitTestsBase): input_api = presubmit.InputApi( self.fake_change, './PRESUBMIT.py', False, None, False) - self.assertEqual(len(input_api.DEFAULT_WHITE_LIST), 24) - self.assertEqual(len(input_api.DEFAULT_BLACK_LIST), 12) + self.assertEqual(len(input_api.DEFAULT_ALLOW_LIST), 24) + self.assertEqual(len(input_api.DEFAULT_BLOCK_LIST), 12) + self.assertEqual(input_api.DEFAULT_ALLOW_LIST, input_api.DEFAULT_WHITE_LIST) + self.assertEqual(input_api.DEFAULT_BLOCK_LIST, input_api.DEFAULT_BLACK_LIST) for item in files: results = list(filter(input_api.FilterSourceFile, item[0])) for i in range(len(results)): @@ -1409,8 +1411,8 @@ class InputApiUnittest(PresubmitTestsBase): self.assertEqual(got_files[1].LocalPath(), 'eeabee') def testLambdaFilter(self): - white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) - black_list = [r".*?b.*?"] + allow_list = presubmit.InputApi.DEFAULT_BLOCK_LIST + (r".*?a.*?",) + block_list = [r".*?b.*?"] files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] known_files = [ os.path.join(self.fake_root_dir, item) @@ -1423,7 +1425,7 @@ class InputApiUnittest(PresubmitTestsBase): change, './PRESUBMIT.py', False, None, False) # Sample usage of overriding the default white and black lists. got_files = input_api.AffectedSourceFiles( - lambda x: input_api.FilterSourceFile(x, white_list, black_list)) + lambda x: input_api.FilterSourceFile(x, allow_list, block_list)) self.assertEqual(len(got_files), 2) self.assertEqual(got_files[0].LocalPath(), 'eeaee') self.assertEqual(got_files[1].LocalPath(), 'eecaee')