From 17a0d843c1b914916258df6e35643325e023f41c Mon Sep 17 00:00:00 2001 From: Scott Lee Date: Fri, 4 Oct 2024 21:02:25 +0000 Subject: [PATCH] allow comment lines in inclusive_language_presubmit_exempt_dirs.txt inclusive_language_presubmit_exempt_dirs.txt is a list of paths to exclude from the inclusive word presubmit check. https://source.chromium.org/chromium/chromium/tools/depot_tools/+/main:presubmit_canned_checks.py;l=2602;drc=0bc7c4832e4f2d453e4826c9a2e1197e11bd6ec7 This CL allows comment lines, starting with \#, in the file. (Also, fixes a bug to allow an empty line) Bug: 369701326 Change-Id: I7647459281f7921362aa4070c26671f321b7cfea Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5906016 Auto-Submit: Scott Lee Commit-Queue: Scott Lee Reviewed-by: Josip Sokcevic --- presubmit_canned_checks.py | 16 ++++++++++--- tests/presubmit_canned_checks_test.py | 34 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 03608d185..466f551f3 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -2632,9 +2632,19 @@ def CheckInclusiveLanguage(input_api, f = input_api.ReadFile(dirs_file_path) for line in f.splitlines(): - path = line.split()[0] - if len(path) > 0: - excluded_paths.append(path) + words = line.split() + # if a line starts with #, followed by a whitespace or line-end, + # it's a comment line. + if len(words) == 0 or words[0] == '#' or words[0] == '': + continue + + # The first word in each line is a path. + # Some exempt_dirs.txt files may have additional words in each line + # (e.g., "third_party 1 2") + # + ## The additional words are present in legacy files for historical + # reasons only. DO NOT parse or require these additional words. + excluded_paths.append(words[0]) excluded_paths = set(excluded_paths) for f in input_api.AffectedFiles(): diff --git a/tests/presubmit_canned_checks_test.py b/tests/presubmit_canned_checks_test.py index 8d4a75f0b..96549f82e 100644 --- a/tests/presubmit_canned_checks_test.py +++ b/tests/presubmit_canned_checks_test.py @@ -298,6 +298,40 @@ class InclusiveLanguageCheckTest(unittest.TestCase): input_api, MockOutputApi()) self.assertEqual([], errors) + def testDirExemptWithComment(self): + input_api = MockInputApi() + input_api.change.RepositoryRoot = lambda: '' + input_api.presubmit_local_path = '' + + input_api.files = [ + MockFile( + os.path.normpath( + 'infra/inclusive_language_presubmit_exempt_dirs.txt'), [ + '# this is a comment', + 'dir1', + '# dir2', + ]), + + # this should be excluded + MockFile( + os.path.normpath('dir1/1.py'), + [ + 'TEST(SomeClassTest, SomeInteraction, blacklist) {', # nocheck + '}' + ]), + + # this should not be excluded + MockFile(os.path.normpath('dir2/2.py'), + ['- (void)testSth { V(whitelist); }']), # nocheck + ] + + errors = presubmit_canned_checks.CheckInclusiveLanguage( + input_api, MockOutputApi()) + self.assertEqual(1, len(errors)) + self.assertTrue(os.path.normpath('dir1/1.py') not in errors[0].message) + self.assertTrue(os.path.normpath('dir2/2.py') in errors[0].message) + + class DescriptionChecksTest(unittest.TestCase): def testCheckDescriptionUsesColonInsteadOfEquals(self):