Commit 2836bcf8 authored by tobiasjs's avatar tobiasjs Committed by Commit bot

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
parent d090526a
...@@ -494,7 +494,7 @@ class InputApi(object): ...@@ -494,7 +494,7 @@ class InputApi(object):
local_path = affected_file.LocalPath() local_path = affected_file.LocalPath()
for item in items: for item in items:
if self.re.match(item, local_path): 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 True
return False return False
return (Find(affected_file, white_list or self.DEFAULT_WHITE_LIST) and return (Find(affected_file, white_list or self.DEFAULT_WHITE_LIST) and
...@@ -646,7 +646,7 @@ class AffectedFile(object): ...@@ -646,7 +646,7 @@ class AffectedFile(object):
self._cached_changed_contents = None self._cached_changed_contents = None
self._cached_new_contents = None self._cached_new_contents = None
self._diff_cache = diff_cache 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): def ServerPath(self):
"""Returns a path string that identifies the file in the SCM system. """Returns a path string that identifies the file in the SCM system.
...@@ -1093,11 +1093,13 @@ def ListRelevantPresubmitFiles(files, root): ...@@ -1093,11 +1093,13 @@ def ListRelevantPresubmitFiles(files, root):
# Look for PRESUBMIT.py in all candidate directories. # Look for PRESUBMIT.py in all candidate directories.
results = [] results = []
for directory in sorted(list(candidates)): for directory in sorted(list(candidates)):
p = os.path.join(directory, 'PRESUBMIT.py') for f in os.listdir(directory):
if os.path.isfile(p): p = os.path.join(directory, f)
results.append(p) 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 return results
...@@ -1454,9 +1456,9 @@ class PresubmitExecuter(object): ...@@ -1454,9 +1456,9 @@ class PresubmitExecuter(object):
function_name = 'CheckChangeOnUpload' function_name = 'CheckChangeOnUpload'
if function_name in context: if function_name in context:
context['__args'] = (input_api, OutputApi(self.committing)) 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) 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 if not (isinstance(result, types.TupleType) or
isinstance(result, types.ListType)): isinstance(result, types.ListType)):
raise PresubmitFailure( raise PresubmitFailure(
...@@ -1609,7 +1611,7 @@ def ScanSubDirs(mask, recursive): ...@@ -1609,7 +1611,7 @@ def ScanSubDirs(mask, recursive):
def ParseFiles(args, recursive): def ParseFiles(args, recursive):
logging.debug('Searching for %s' % args) logging.debug('Searching for %s', args)
files = [] files = []
for arg in args: for arg in args:
files.extend([('M', f) for f in ScanSubDirs(arg, recursive)]) files.extend([('M', f) for f in ScanSubDirs(arg, recursive)])
...@@ -1632,7 +1634,7 @@ def load_files(options, args): ...@@ -1632,7 +1634,7 @@ def load_files(options, args):
if not files: if not files:
files = scm.GIT.CaptureStatus([], options.root, upstream) files = scm.GIT.CaptureStatus([], options.root, upstream)
else: 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: if not files:
return None, None return None, None
change_class = Change change_class = Change
...@@ -1754,7 +1756,7 @@ def main(argv=None): ...@@ -1754,7 +1756,7 @@ def main(argv=None):
change_class, files = load_files(options, args) change_class, files = load_files(options, args)
if not change_class: if not change_class:
parser.error('For unversioned directory, <files> is not optional.') parser.error('For unversioned directory, <files> 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 rietveld_obj, gerrit_obj = None, None
......
...@@ -112,7 +112,7 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): ...@@ -112,7 +112,7 @@ class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase):
TestCaseUtils.setUp(self) TestCaseUtils.setUp(self)
mox.MoxTestBase.setUp(self) mox.MoxTestBase.setUp(self)
os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', 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', 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4',
'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink',
'system', 'tmpfile', 'walk', 'write') 'system', 'tmpfile', 'walk', 'write')
......
...@@ -217,20 +217,25 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -217,20 +217,25 @@ class PresubmitUnittest(PresubmitTestsBase):
inherit_path = presubmit.os.path.join(self.fake_root_dir, inherit_path = presubmit.os.path.join(self.fake_root_dir,
self._INHERIT_SETTINGS) self._INHERIT_SETTINGS)
presubmit.os.path.isfile(inherit_path).AndReturn(False) 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.os.path.isfile(join(self.fake_root_dir,
'PRESUBMIT.py')).AndReturn(True) 'PRESUBMIT.py')).AndReturn(True)
presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', presubmit.os.listdir(join(self.fake_root_dir, 'foo')).AndReturn([])
'PRESUBMIT.py')).AndReturn(False) presubmit.os.listdir(join(self.fake_root_dir, 'foo',
presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', 'haspresubmit')).AndReturn(['PRESUBMIT.py'])
'PRESUBMIT.py')).AndReturn(True) presubmit.os.path.isfile(
presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', join(self.fake_root_dir, 'foo', 'haspresubmit',
'yodle', 'PRESUBMIT.py')).AndReturn(True) 'PRESUBMIT.py')).AndReturn(True)
presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', presubmit.os.listdir(
'PRESUBMIT.py')).AndReturn(False) join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle')).AndReturn(
presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', ['PRESUBMIT.py'])
'PRESUBMIT.py')).AndReturn(False) presubmit.os.path.isfile(
presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', 'gat', join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle',
'PRESUBMIT.py')).AndReturn(False) '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() self.mox.ReplayAll()
presubmit_files = presubmit.ListRelevantPresubmitFiles(files, presubmit_files = presubmit.ListRelevantPresubmitFiles(files,
...@@ -243,6 +248,29 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -243,6 +248,29 @@ class PresubmitUnittest(PresubmitTestsBase):
'PRESUBMIT.py') '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): def testListRelevantPresubmitFilesInheritSettings(self):
join = presubmit.os.path.join join = presubmit.os.path.join
sys_root_dir = self._OS_SEP sys_root_dir = self._OS_SEP
...@@ -254,16 +282,16 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -254,16 +282,16 @@ class PresubmitUnittest(PresubmitTestsBase):
] ]
inherit_path = presubmit.os.path.join(root_dir, self._INHERIT_SETTINGS) inherit_path = presubmit.os.path.join(root_dir, self._INHERIT_SETTINGS)
presubmit.os.path.isfile(inherit_path).AndReturn(True) presubmit.os.path.isfile(inherit_path).AndReturn(True)
presubmit.os.path.isfile(join(sys_root_dir, presubmit.os.listdir(sys_root_dir).AndReturn([])
'PRESUBMIT.py')).AndReturn(False) presubmit.os.listdir(join(sys_root_dir, 'foo')).AndReturn(['PRESUBMIT.py'])
presubmit.os.path.isfile(join(sys_root_dir, 'foo', presubmit.os.path.isfile(join(sys_root_dir, 'foo',
'PRESUBMIT.py')).AndReturn(True) 'PRESUBMIT.py')).AndReturn(True)
presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'bar', presubmit.os.listdir(join(sys_root_dir, 'foo', 'bar')).AndReturn([])
'PRESUBMIT.py')).AndReturn(False) presubmit.os.listdir(join(sys_root_dir, 'foo', 'bar', 'moo')).AndReturn(
presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'bar', 'moo', ['PRESUBMIT.py'])
'PRESUBMIT.py')).AndReturn(True) presubmit.os.path.isfile(
presubmit.os.path.isfile(join(sys_root_dir, 'foo', 'bar', 'zoo', join(sys_root_dir, 'foo', 'bar', 'moo', 'PRESUBMIT.py')).AndReturn(True)
'PRESUBMIT.py')).AndReturn(False) presubmit.os.listdir(join(sys_root_dir, 'foo', 'bar', 'zoo')).AndReturn([])
self.mox.ReplayAll() self.mox.ReplayAll()
presubmit_files = presubmit.ListRelevantPresubmitFiles(files, root_dir) presubmit_files = presubmit.ListRelevantPresubmitFiles(files, root_dir)
...@@ -682,7 +710,10 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -682,7 +710,10 @@ class PresubmitUnittest(PresubmitTestsBase):
inherit_path = presubmit.os.path.join(self.fake_root_dir, inherit_path = presubmit.os.path.join(self.fake_root_dir,
self._INHERIT_SETTINGS) self._INHERIT_SETTINGS)
presubmit.os.path.isfile(inherit_path).AndReturn(False) 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.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.os.path.isfile(haspresubmit_path).AndReturn(True)
presubmit.gclient_utils.FileRead(root_path, presubmit.gclient_utils.FileRead(root_path,
'rU').AndReturn(self.presubmit_text) 'rU').AndReturn(self.presubmit_text)
...@@ -721,7 +752,10 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -721,7 +752,10 @@ class PresubmitUnittest(PresubmitTestsBase):
self._INHERIT_SETTINGS) self._INHERIT_SETTINGS)
for _ in range(2): for _ in range(2):
presubmit.os.path.isfile(inherit_path).AndReturn(False) 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.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.os.path.isfile(haspresubmit_path).AndReturn(True)
presubmit.gclient_utils.FileRead(presubmit_path, 'rU' presubmit.gclient_utils.FileRead(presubmit_path, 'rU'
).AndReturn(self.presubmit_text) ).AndReturn(self.presubmit_text)
...@@ -768,7 +802,10 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -768,7 +802,10 @@ class PresubmitUnittest(PresubmitTestsBase):
inherit_path = presubmit.os.path.join(self.fake_root_dir, inherit_path = presubmit.os.path.join(self.fake_root_dir,
self._INHERIT_SETTINGS) self._INHERIT_SETTINGS)
presubmit.os.path.isfile(inherit_path).AndReturn(False) 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.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.os.path.isfile(haspresubmit_path).AndReturn(True)
presubmit.gclient_utils.FileRead(presubmit_path, 'rU' presubmit.gclient_utils.FileRead(presubmit_path, 'rU'
).AndReturn(self.presubmit_text) ).AndReturn(self.presubmit_text)
...@@ -810,8 +847,10 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -810,8 +847,10 @@ def CheckChangeOnCommit(input_api, output_api):
inherit_path = presubmit.os.path.join(self.fake_root_dir, inherit_path = presubmit.os.path.join(self.fake_root_dir,
self._INHERIT_SETTINGS) self._INHERIT_SETTINGS)
presubmit.os.path.isfile(inherit_path).AndReturn(False) presubmit.os.path.isfile(inherit_path).AndReturn(False)
presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') presubmit.os.listdir(join(self.fake_root_dir)
).AndReturn(False) ).AndReturn([])
presubmit.os.listdir(join(self.fake_root_dir, 'haspresubmit')
).AndReturn(['PRESUBMIT.py'])
presubmit.os.path.isfile(join(self.fake_root_dir, presubmit.os.path.isfile(join(self.fake_root_dir,
'haspresubmit', 'haspresubmit',
'PRESUBMIT.py')).AndReturn(False) 'PRESUBMIT.py')).AndReturn(False)
...@@ -998,12 +1037,16 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -998,12 +1037,16 @@ def CheckChangeOnCommit(input_api, output_api):
self._INHERIT_SETTINGS) self._INHERIT_SETTINGS)
presubmit.os.path.isfile(inherit_path).AndReturn(False) 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.path.isfile(root_presubmit).AndReturn(True)
presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn(
self.presubmit_tryslave % '["win"]') self.presubmit_tryslave % '["win"]')
presubmit.os.path.isfile(inherit_path).AndReturn(False) 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.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.os.path.isfile(linux_presubmit).AndReturn(True)
presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn( presubmit.gclient_utils.FileRead(root_presubmit, 'rU').AndReturn(
self.presubmit_tryslave % '["win"]') self.presubmit_tryslave % '["win"]')
...@@ -1105,6 +1148,7 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -1105,6 +1148,7 @@ def CheckChangeOnCommit(input_api, output_api):
join = presubmit.os.path.join join = presubmit.os.path.join
isfile = presubmit.os.path.isfile isfile = presubmit.os.path.isfile
listdir = presubmit.os.listdir
FileRead = presubmit.gclient_utils.FileRead FileRead = presubmit.gclient_utils.FileRead
filename = 'foo.cc' filename = 'foo.cc'
filename_linux = join('linux_only', 'penguin.cc') filename_linux = join('linux_only', 'penguin.cc')
...@@ -1113,11 +1157,14 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -1113,11 +1157,14 @@ def CheckChangeOnCommit(input_api, output_api):
inherit_path = join(self.fake_root_dir, self._INHERIT_SETTINGS) inherit_path = join(self.fake_root_dir, self._INHERIT_SETTINGS)
isfile(inherit_path).AndReturn(False) isfile(inherit_path).AndReturn(False)
listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py'])
isfile(root_presubmit).AndReturn(True) isfile(root_presubmit).AndReturn(True)
FileRead(root_presubmit, 'rU').AndReturn(root_text) FileRead(root_presubmit, 'rU').AndReturn(root_text)
isfile(inherit_path).AndReturn(False) isfile(inherit_path).AndReturn(False)
listdir(self.fake_root_dir).AndReturn(['PRESUBMIT.py'])
isfile(root_presubmit).AndReturn(True) isfile(root_presubmit).AndReturn(True)
listdir(join(self.fake_root_dir, 'linux_only')).AndReturn(['PRESUBMIT.py'])
isfile(linux_presubmit).AndReturn(True) isfile(linux_presubmit).AndReturn(True)
FileRead(root_presubmit, 'rU').AndReturn(root_text) FileRead(root_presubmit, 'rU').AndReturn(root_text)
FileRead(linux_presubmit, 'rU').AndReturn(linux_text) FileRead(linux_presubmit, 'rU').AndReturn(linux_text)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment