| # Copyright 2026 The Chromium Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| import os | |
| import unittest | |
| from unittest import mock | |
| import PRESUBMIT | |
| class MockAffectedFile: | |
| def __init__(self, abs_path, action='M'): | |
| self._abs_path = abs_path | |
| self._action = action | |
| def AbsoluteLocalPath(self): | |
| return self._abs_path | |
| def Action(self): | |
| return self._action | |
| class MockChange: | |
| def __init__(self, description, footers=None): | |
| self._description = description | |
| self._footers = footers or {} | |
| def FullDescriptionText(self): | |
| return self._description | |
| def GitFootersFromDescription(self): | |
| return self._footers | |
| class MockInputApi: | |
| def __init__(self, | |
| description='', | |
| affected_files=None, | |
| root='/repo', | |
| footers=None): | |
| self.change = MockChange(description, footers=footers) | |
| self._affected_files = affected_files or [] | |
| self._root = root | |
| self.os_path = os.path | |
| def AffectedFiles(self): | |
| return self._affected_files | |
| def PresubmitLocalPath(self): | |
| return self._root | |
| class MockOutputApi: | |
| class PresubmitResult: | |
| def __init__(self, message, items=None, long_text=''): | |
| self.message = message | |
| self.items = items or [] | |
| self.long_text = long_text | |
| def __repr__(self): | |
| return f'{self.__class__.__name__}({self.message!r})' | |
| class PresubmitError(PresubmitResult): | |
| pass | |
| class PiperTagsPresubmitTest(unittest.TestCase): | |
| def testValidCleanDescription(self): | |
| description = ( | |
| '[devtools] Add cool feature\n\n' | |
| 'This is a clean commit description.\n\n' | |
| 'Bug: 123456\n' | |
| 'Change-Id: I1234567890abcdef1234567890abcdef12345678\n') | |
| input_api = MockInputApi(description) | |
| output_api = MockOutputApi() | |
| errors = PRESUBMIT.CheckNoPiperTagsInDescription(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| def testValidWithFixedAndNone(self): | |
| description = ('[devtools] Fix issue\n\n' | |
| 'Fixed: 123456, 7890\n') | |
| input_api = MockInputApi(description) | |
| output_api = MockOutputApi() | |
| errors = PRESUBMIT.CheckNoPiperTagsInDescription(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| description_none = ('[devtools] Minor cleanup\n\n' | |
| 'Bug: None\n') | |
| input_api_none = MockInputApi(description_none) | |
| errors_none = PRESUBMIT.CheckNoPiperTagsInDescription( | |
| input_api_none, output_api) | |
| self.assertEqual(errors_none, []) | |
| def testValidMidLineTags(self): | |
| description = ( | |
| '[devtools] Update docs\n\n' | |
| 'We previously fixed a bug where TAG=agy was incorrectly emitted.\n' | |
| 'Also noted that BUG=123 is discussed in comments.\n\n' | |
| 'Bug: 123456\n') | |
| input_api = MockInputApi(description) | |
| output_api = MockOutputApi() | |
| errors = PRESUBMIT.CheckNoPiperTagsInDescription(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| def testValidExcludedKeys(self): | |
| description = ( | |
| '[devtools] Roll deps\n\n' | |
| 'Roll third_party/something\n\n' | |
| 'R=chrome-devtools-waterfall-gardener-emea-oncall@google.com\n' | |
| 'TBR=someone@chromium.org\n' | |
| 'CC=someone@chromium.org\n' | |
| 'Bug: None\n') | |
| input_api = MockInputApi(description) | |
| output_api = MockOutputApi() | |
| errors = PRESUBMIT.CheckNoPiperTagsInDescription(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| def testRejectsPiperTags(self): | |
| rejected_keys = [ | |
| 'TAG=agy', | |
| 'CONV=c29008c7-cac9-4486-adb8-a74b42bd015e', | |
| 'BUG=123456', | |
| 'FIXED=123456', | |
| 'MARKDOWN=true', | |
| 'ORIGINAL_AUTHOR=author@example.com', | |
| 'WANT_LGTM=1', | |
| 'AUTOSUBMIT_BEHAVIOR=1', | |
| 'R_FUTURE=someone@example.com', | |
| ] | |
| output_api = MockOutputApi() | |
| for tag in rejected_keys: | |
| with self.subTest(tag=tag): | |
| description = f'[devtools] Change\n\nExplanation.\n\n{tag}\nBug: 123\n' | |
| input_api = MockInputApi(description) | |
| errors = PRESUBMIT.CheckNoPiperTagsInDescription( | |
| input_api, output_api) | |
| self.assertEqual(len(errors), 1, | |
| f'Expected {tag} to be rejected') | |
| self.assertIsInstance(errors[0], output_api.PresubmitError) | |
| self.assertIn(tag, errors[0].message) | |
| self.assertIn('Bug:', errors[0].message) | |
| self.assertIn('Fixed:', errors[0].message) | |
| self.assertIn('cl.upload-conversation-id', errors[0].message) | |
| def testRejectsMultipleOffendingLines(self): | |
| description = ('[devtools] Change\n\n' | |
| 'Explanation.\n\n' | |
| 'TAG=agy\n' | |
| 'CONV=c29008c7-cac9-4486-adb8-a74b42bd015e\n' | |
| 'BUG=12345\n' | |
| 'Bug: 12345\n') | |
| input_api = MockInputApi(description) | |
| output_api = MockOutputApi() | |
| errors = PRESUBMIT.CheckNoPiperTagsInDescription(input_api, output_api) | |
| self.assertEqual(len(errors), 1) | |
| self.assertIn('TAG=agy', errors[0].message) | |
| self.assertIn('CONV=c29008c7-cac9-4486-adb8-a74b42bd015e', | |
| errors[0].message) | |
| self.assertIn('BUG=12345', errors[0].message) | |
| class OrphanTestManifestPresubmitTest(unittest.TestCase): | |
| def testSkipsWhenNoRelevantFilesChanged(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', 'A.ts')), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript') as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_not_called() | |
| def testSkipsDeletedFiles(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', 'A.test.ts'), | |
| 'D'), | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', | |
| 'A.test.api.ts'), 'D'), | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', 'BUILD.gn'), | |
| 'D'), | |
| MockAffectedFile( | |
| os.path.join('/repo', 'scripts', 'verify_cl', 'cli.ts'), 'D'), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript') as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_not_called() | |
| def testSkipsWhenSkipFooterIsPresent(self): | |
| input_api = MockInputApi( | |
| affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', | |
| 'A.test.ts'), 'A'), | |
| ], | |
| footers={'Skip-Orphan-Test-Manifest-Check': ['temporary bypass']}, | |
| ) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript') as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_not_called() | |
| def testRunsVerifyClOnTestOrBuildGnEdits(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', 'A.test.ts'), | |
| 'A'), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript', | |
| return_value=[]) as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| expected_script_path = os.path.join('/repo', 'scripts', 'verify_cl', | |
| 'cli.ts') | |
| check_mock.assert_called_once_with( | |
| input_api, | |
| output_api, | |
| expected_script_path, | |
| script_arguments=['--root', '/repo'], | |
| message='Orphan test manifest check', | |
| ) | |
| def testRunsVerifyClOnApiTestEdits(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'models', 'a', | |
| 'A.test.api.ts'), 'A'), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript', | |
| return_value=[]) as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_called_once() | |
| def testRunsVerifyClOnVerifyClEdits(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'scripts', 'verify_cl', 'gn_reader.ts'), | |
| 'M'), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript', | |
| return_value=[]) as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_called_once() | |
| def testRunsVerifyClOnGnDepsVerifierEdits(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'scripts', 'gn_deps_verifier', 'utils', | |
| 'gn_label.ts'), 'M'), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript', | |
| return_value=[]) as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_called_once() | |
| def testRunsVerifyClOnBuildGniEdits(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'scripts', 'build', 'ninja', | |
| 'script_test.gni'), 'M'), | |
| ]) | |
| output_api = MockOutputApi() | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript', | |
| return_value=[]) as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| self.assertEqual(errors, []) | |
| check_mock.assert_called_once() | |
| def testReturnsPresubmitErrorOnFailure(self): | |
| input_api = MockInputApi(affected_files=[ | |
| MockAffectedFile( | |
| os.path.join('/repo', 'front_end', 'panels', 'a', 'BUILD.gn'), | |
| 'M'), | |
| ]) | |
| output_api = MockOutputApi() | |
| expected = [output_api.PresubmitError('Orphan test manifest check')] | |
| with mock.patch.object(PRESUBMIT, | |
| '_CheckWithNodeScript', | |
| return_value=expected) as check_mock: | |
| errors = PRESUBMIT.CheckOrphanTestManifest(input_api, output_api) | |
| check_mock.assert_called_once() | |
| self.assertEqual(errors, expected) | |
| if __name__ == '__main__': | |
| unittest.main() |