From 2836bcf8102969bbbe56708d3dd6690bed271d5d Mon Sep 17 00:00:00 2001 From: tobiasjs Date: Tue, 16 Aug 2016 04:08:16 -0700 Subject: [PATCH] Support additional user presubmit scripts named PRESUBMIT*.py. This allows users to specify additional local presubmit tests that do not need to exist as locally maintained changes to PRESUBMIT.py files. BUG= Review-Url: https://codereview.chromium.org/2232203002 --- presubmit_support.py | 24 +++++----- testing_support/super_mox.py | 2 +- tests/presubmit_unittest.py | 91 +++++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 34 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index 807da4a05d..2922b4e329 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -494,7 +494,7 @@ class InputApi(object): local_path = affected_file.LocalPath() for item in items: if self.re.match(item, local_path): - logging.debug("%s matched %s" % (item, local_path)) + logging.debug("%s matched %s", item, local_path) return True return False return (Find(affected_file, white_list or self.DEFAULT_WHITE_LIST) and @@ -646,7 +646,7 @@ class AffectedFile(object): self._cached_changed_contents = None self._cached_new_contents = None self._diff_cache = diff_cache - logging.debug('%s(%s)' % (self.__class__.__name__, self._path)) + logging.debug('%s(%s)', self.__class__.__name__, self._path) def ServerPath(self): """Returns a path string that identifies the file in the SCM system. @@ -1093,11 +1093,13 @@ def ListRelevantPresubmitFiles(files, root): # Look for PRESUBMIT.py in all candidate directories. results = [] for directory in sorted(list(candidates)): - p = os.path.join(directory, 'PRESUBMIT.py') - if os.path.isfile(p): - results.append(p) + for f in os.listdir(directory): + p = os.path.join(directory, f) + if os.path.isfile(p) and re.match( + r'PRESUBMIT.*\.py$', f) and not f.startswith('PRESUBMIT_test'): + results.append(p) - logging.debug('Presubmit files: %s' % ','.join(results)) + logging.debug('Presubmit files: %s', ','.join(results)) return results @@ -1454,9 +1456,9 @@ class PresubmitExecuter(object): function_name = 'CheckChangeOnUpload' if function_name in context: context['__args'] = (input_api, OutputApi(self.committing)) - logging.debug('Running %s in %s' % (function_name, presubmit_path)) + logging.debug('Running %s in %s', function_name, presubmit_path) result = eval(function_name + '(*__args)', context) - logging.debug('Running %s done.' % function_name) + logging.debug('Running %s done.', function_name) if not (isinstance(result, types.TupleType) or isinstance(result, types.ListType)): raise PresubmitFailure( @@ -1609,7 +1611,7 @@ def ScanSubDirs(mask, recursive): def ParseFiles(args, recursive): - logging.debug('Searching for %s' % args) + logging.debug('Searching for %s', args) files = [] for arg in args: files.extend([('M', f) for f in ScanSubDirs(arg, recursive)]) @@ -1632,7 +1634,7 @@ def load_files(options, args): if not files: files = scm.GIT.CaptureStatus([], options.root, upstream) else: - logging.info('Doesn\'t seem under source control. Got %d files' % len(args)) + logging.info('Doesn\'t seem under source control. Got %d files', len(args)) if not files: return None, None change_class = Change @@ -1754,7 +1756,7 @@ def main(argv=None): change_class, files = load_files(options, args) if not change_class: parser.error('For unversioned directory, is not optional.') - logging.info('Found %d file(s).' % len(files)) + logging.info('Found %d file(s).', len(files)) rietveld_obj, gerrit_obj = None, None diff --git a/testing_support/super_mox.py b/testing_support/super_mox.py index 36abab42d6..cbb6df4785 100644 --- a/testing_support/super_mox.py +++ b/testing_support/super_mox.py @@ -112,7 +112,7 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): TestCaseUtils.setUp(self) mox.MoxTestBase.setUp(self) os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', - 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'lseek', + 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'listdir', 'lseek', 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', 'system', 'tmpfile', 'walk', 'write') diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index 1f65eb97cf..7629602833 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -217,20 +217,25 @@ class PresubmitUnittest(PresubmitTestsBase): inherit_path = presubmit.os.path.join(self.fake_root_dir, self._INHERIT_SETTINGS) presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py')).AndReturn(True) - presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', - 'PRESUBMIT.py')).AndReturn(False) - presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', - 'PRESUBMIT.py')).AndReturn(True) - presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', - 'yodle', 'PRESUBMIT.py')).AndReturn(True) - presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', - 'PRESUBMIT.py')).AndReturn(False) - presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', - 'PRESUBMIT.py')).AndReturn(False) - presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', 'gat', - 'PRESUBMIT.py')).AndReturn(False) + presubmit.os.listdir(join(self.fake_root_dir, 'foo')).AndReturn([]) + presubmit.os.listdir(join(self.fake_root_dir, 'foo', + 'haspresubmit')).AndReturn(['PRESUBMIT.py']) + presubmit.os.path.isfile( + join(self.fake_root_dir, 'foo', 'haspresubmit', + 'PRESUBMIT.py')).AndReturn(True) + presubmit.os.listdir( + join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle')).AndReturn( + ['PRESUBMIT.py']) + presubmit.os.path.isfile( + join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle', + 'PRESUBMIT.py')).AndReturn(True) + presubmit.os.listdir(join(self.fake_root_dir, 'moo')).AndReturn([]) + presubmit.os.listdir(join(self.fake_root_dir, 'moo', 'mat')).AndReturn([]) + presubmit.os.listdir(join(self.fake_root_dir, 'moo', 'mat', + 'gat')).AndReturn([]) self.mox.ReplayAll() presubmit_files = presubmit.ListRelevantPresubmitFiles(files, @@ -243,6 +248,29 @@ class PresubmitUnittest(PresubmitTestsBase): 'PRESUBMIT.py') ]) + def testListUserPresubmitFiles(self): + join = presubmit.os.path.join + files = ['blat.cc',] + inherit_path = presubmit.os.path.join(self.fake_root_dir, + self._INHERIT_SETTINGS) + presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn( + ['PRESUBMIT.py', 'PRESUBMIT_test.py', 'PRESUBMIT-user.py']) + presubmit.os.path.isfile(join(self.fake_root_dir, + 'PRESUBMIT.py')).AndReturn(True) + presubmit.os.path.isfile(join(self.fake_root_dir, + 'PRESUBMIT_test.py')).AndReturn(True) + presubmit.os.path.isfile(join(self.fake_root_dir, + 'PRESUBMIT-user.py')).AndReturn(True) + self.mox.ReplayAll() + + presubmit_files = presubmit.ListRelevantPresubmitFiles(files, + self.fake_root_dir) + self.assertEqual(presubmit_files, [ + join(self.fake_root_dir, 'PRESUBMIT.py'), + join(self.fake_root_dir, 'PRESUBMIT-user.py'), + ]) + def testListRelevantPresubmitFilesInheritSettings(self): join = presubmit.os.path.join sys_root_dir = self._OS_SEP @@ -254,16 +282,16 @@ class PresubmitUnittest(PresubmitTestsBase): ] inherit_path = presubmit.os.path.join(root_dir, self._INHERIT_SETTINGS) presubmit.os.path.isfile(inherit_path).AndReturn(True) - presubmit.os.path.isfile(join(sys_root_dir, - 'PRESUBMIT.py')).AndReturn(False) + presubmit.os.listdir(sys_root_dir).AndReturn([]) + presubmit.os.listdir(join(sys_root_dir, 'foo')).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'PRESUBMIT.py')).AndReturn(True) - presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'bar', - 'PRESUBMIT.py')).AndReturn(False) - presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'bar', 'moo', - 'PRESUBMIT.py')).AndReturn(True) - presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'bar', 'zoo', - 'PRESUBMIT.py')).AndReturn(False) + presubmit.os.listdir(join(sys_root_dir, 'foo', 'bar')).AndReturn([]) + presubmit.os.listdir(join(sys_root_dir, 'foo', 'bar', 'moo')).AndReturn( + ['PRESUBMIT.py']) + presubmit.os.path.isfile( + join(sys_root_dir, 'foo', 'bar', 'moo', 'PRESUBMIT.py')).AndReturn(True) + presubmit.os.listdir(join(sys_root_dir, 'foo', 'bar', 'zoo')).AndReturn([]) self.mox.ReplayAll() presubmit_files = presubmit.ListRelevantPresubmitFiles(files, root_dir) @@ -682,7 +710,10 @@ class PresubmitUnittest(PresubmitTestsBase): inherit_path = presubmit.os.path.join(self.fake_root_dir, self._INHERIT_SETTINGS) presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(root_path).AndReturn(True) + presubmit.os.listdir(os.path.join( + self.fake_root_dir, 'haspresubmit')).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) presubmit.gclient_utils.FileRead(root_path, 'rU').AndReturn(self.presubmit_text) @@ -721,7 +752,10 @@ class PresubmitUnittest(PresubmitTestsBase): self._INHERIT_SETTINGS) for _ in range(2): presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(presubmit_path).AndReturn(True) + presubmit.os.listdir(join(self.fake_root_dir, 'haspresubmit')).AndReturn( + ['PRESUBMIT.py']) presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) presubmit.gclient_utils.FileRead(presubmit_path, 'rU' ).AndReturn(self.presubmit_text) @@ -768,7 +802,10 @@ class PresubmitUnittest(PresubmitTestsBase): inherit_path = presubmit.os.path.join(self.fake_root_dir, self._INHERIT_SETTINGS) presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(presubmit_path).AndReturn(True) + presubmit.os.listdir(join(self.fake_root_dir, 'haspresubmit')).AndReturn( + ['PRESUBMIT.py']) presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) presubmit.gclient_utils.FileRead(presubmit_path, 'rU' ).AndReturn(self.presubmit_text) @@ -810,8 +847,10 @@ def CheckChangeOnCommit(input_api, output_api): inherit_path = presubmit.os.path.join(self.fake_root_dir, self._INHERIT_SETTINGS) presubmit.os.path.isfile(inherit_path).AndReturn(False) - presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') - ).AndReturn(False) + presubmit.os.listdir(join(self.fake_root_dir) + ).AndReturn([]) + presubmit.os.listdir(join(self.fake_root_dir, 'haspresubmit') + ).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py')).AndReturn(False) @@ -998,12 +1037,16 @@ def CheckChangeOnCommit(input_api, output_api): self._INHERIT_SETTINGS) presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(root_presubmit).AndReturn(True) presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( self.presubmit_tryslave % '["win"]') presubmit.os.path.isfile(inherit_path).AndReturn(False) + presubmit.os.listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) presubmit.os.path.isfile(root_presubmit).AndReturn(True) + presubmit.os.listdir(join(self.fake_root_dir, 'linux_only')).AndReturn( + ['PRESUBMIT.py']) presubmit.os.path.isfile(linux_presubmit).AndReturn(True) presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( self.presubmit_tryslave % '["win"]') @@ -1105,6 +1148,7 @@ def CheckChangeOnCommit(input_api, output_api): join = presubmit.os.path.join isfile = presubmit.os.path.isfile + listdir = presubmit.os.listdir FileRead = presubmit.gclient_utils.FileRead filename = 'foo.cc' filename_linux = join('linux_only', 'penguin.cc') @@ -1113,11 +1157,14 @@ def CheckChangeOnCommit(input_api, output_api): inherit_path = join(self.fake_root_dir, self._INHERIT_SETTINGS) isfile(inherit_path).AndReturn(False) + listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) isfile(root_presubmit).AndReturn(True) FileRead(root_presubmit, 'rU').AndReturn(root_text) isfile(inherit_path).AndReturn(False) + listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py']) isfile(root_presubmit).AndReturn(True) + listdir(join(self.fake_root_dir, 'linux_only')).AndReturn(['PRESUBMIT.py']) isfile(linux_presubmit).AndReturn(True) FileRead(root_presubmit, 'rU').AndReturn(root_text) FileRead(linux_presubmit, 'rU').AndReturn(linux_text)