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):