From 8a4a2bc611aee46b1d7a86b198ffe1deb3196868 Mon Sep 17 00:00:00 2001 From: "iannucci@chromium.org" Date: Fri, 8 Mar 2013 08:13:20 +0000 Subject: [PATCH] Add skip_canned option to presubmit_support. This will replace the hack in commit-queue/verification/presubmit_shim, and will be used on the presubmit trybot. R=maruel@chromium.org BUG= Review URL: https://codereview.chromium.org/12481002 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@186922 0039d316-1c4b-4281-b951-d872f2087c98 --- presubmit_support.py | 59 +++++++++++++++++++++++++++---------- tests/presubmit_unittest.py | 27 +++++++++++++---- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index 513e22ab8..3239d7b2e 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -14,6 +14,7 @@ __version__ = '1.6.1' import cPickle # Exposed through the API. import cStringIO # Exposed through the API. +import contextlib import fnmatch import glob import inspect @@ -1198,6 +1199,25 @@ def load_files(options, args): return change_class, files +class NonexistantCannedCheckFilter(Exception): + pass + + +@contextlib.contextmanager +def canned_check_filter(method_names): + filtered = {} + try: + for method_name in method_names: + if not hasattr(presubmit_canned_checks, method_name): + raise NonexistantCannedCheckFilter(method_name) + filtered[method_name] = getattr(presubmit_canned_checks, method_name) + setattr(presubmit_canned_checks, method_name, lambda *_a, **_kw: []) + yield + finally: + for name, method in filtered.iteritems(): + setattr(presubmit_canned_checks, name, method) + + def Main(argv): parser = optparse.OptionParser(usage="%prog [options] ", version="%prog " + str(__version__)) @@ -1221,6 +1241,10 @@ def Main(argv): "system directories will also be searched.") parser.add_option("--default_presubmit") parser.add_option("--may_prompt", action='store_true', default=False) + parser.add_option("--skip_canned", action='append', default=[], + help="A list of checks to skip which appear in " + "presubmit_canned_checks. Can be provided multiple times " + "to skip multiple canned checks.") parser.add_option("--rietveld_url", help=optparse.SUPPRESS_HELP) parser.add_option("--rietveld_email", help=optparse.SUPPRESS_HELP) parser.add_option("--rietveld_password", help=optparse.SUPPRESS_HELP) @@ -1242,22 +1266,27 @@ def Main(argv): options.rietveld_email, options.rietveld_password) try: - results = DoPresubmitChecks( - change_class(options.name, - options.description, - options.root, - files, - options.issue, - options.patchset, - options.author), - options.commit, - options.verbose, - sys.stdout, - sys.stdin, - options.default_presubmit, - options.may_prompt, - rietveld_obj) + with canned_check_filter(options.skip_canned): + results = DoPresubmitChecks( + change_class(options.name, + options.description, + options.root, + files, + options.issue, + options.patchset, + options.author), + options.commit, + options.verbose, + sys.stdout, + sys.stdin, + options.default_presubmit, + options.may_prompt, + rietveld_obj) return not results.should_continue() + except NonexistantCannedCheckFilter, e: + print >> sys.stderr, ( + 'Attempted to skip nonexistent canned presubmit check: %s' % e.message) + return 2 except PresubmitFailure, e: print >> sys.stderr, e print >> sys.stderr, 'Maybe your depot_tools is out of date?' diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index a85d3625a..13145493e 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -159,11 +159,11 @@ class PresubmitUnittest(PresubmitTestsBase): 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', 'GetTrySlavesExecuter', 'GitAffectedFile', 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', - 'OutputApi', 'ParseFiles', 'PresubmitFailure', - 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', - 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', - 'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'inspect', 'json', - 'load_files', + 'NonexistantCannedCheckFilter', 'OutputApi', 'ParseFiles', + 'PresubmitFailure', 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', + 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', 'contextlib', + 'canned_check_filter', 'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', + 'inspect', 'json', 'load_files', 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', 'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm', 'subprocess', @@ -173,6 +173,23 @@ class PresubmitUnittest(PresubmitTestsBase): # If this test fails, you should add the relevant test. self.compareMembers(presubmit, members) + def testCannedCheckFilter(self): + canned = presubmit.presubmit_canned_checks + orig = canned.CheckOwners + with presubmit.canned_check_filter(['CheckOwners']): + self.assertNotEqual(canned.CheckOwners, orig) + self.assertEqual(canned.CheckOwners(None, None), []) + self.assertEqual(canned.CheckOwners, orig) + + def testCannedCheckFilterFail(self): + canned = presubmit.presubmit_canned_checks + orig = canned.CheckOwners + def failAttempt(): + with presubmit.canned_check_filter(['CheckOwners', 'Spazfleem']): + pass + self.assertRaises(presubmit.NonexistantCannedCheckFilter, failAttempt) + self.assertEqual(canned.CheckOwners, orig) + def testListRelevantPresubmitFiles(self): join = presubmit.os.path.join files = [