| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Generic presubmit checks that can be reused by other presubmit checks.""" | 6 """Generic presubmit checks that can be reused by other presubmit checks.""" |
| 7 | 7 |
| 8 | 8 |
| 9 def CheckChangeHasTestField(input_api, output_api): | 9 def CheckChangeHasTestField(input_api, output_api): |
| 10 """Requires that the changelist have a TEST= field.""" | 10 """Requires that the changelist have a TEST= field.""" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 CheckDoNotSubmitInFiles(input_api, output_api) | 67 CheckDoNotSubmitInFiles(input_api, output_api) |
| 68 ) | 68 ) |
| 69 | 69 |
| 70 | 70 |
| 71 def CheckChangeHasNoTabs(input_api, output_api): | 71 def CheckChangeHasNoTabs(input_api, output_api): |
| 72 """Checks that there are no tab characters in any of the text files to be | 72 """Checks that there are no tab characters in any of the text files to be |
| 73 submitted. | 73 submitted. |
| 74 """ | 74 """ |
| 75 for f, line_num, line in input_api.RightHandSideLines(): | 75 for f, line_num, line in input_api.RightHandSideLines(): |
| 76 if '\t' in line: | 76 if '\t' in line: |
| 77 return [output_api.PresubmitError( | 77 return [output_api.PresubmitPromptWarning( |
| 78 "Found a tab character in %s, line %s" % | 78 "Found a tab character in %s, line %s" % |
| 79 (f.LocalPath(), line_num))] | 79 (f.LocalPath(), line_num))] |
| 80 return [] | 80 return [] |
| 81 | 81 |
| 82 | 82 |
| 83 def CheckLongLines(input_api, output_api, maxlen=80): | 83 def CheckLongLines(input_api, output_api, maxlen=80): |
| 84 """Checks that there aren't any lines longer than maxlen characters in any of | 84 """Checks that there aren't any lines longer than maxlen characters in any of |
| 85 the text files to be submitted. | 85 the text files to be submitted. |
| 86 """ | 86 """ |
| 87 bad = [] | 87 bad = [] |
| 88 for f, line_num, line in input_api.RightHandSideLines(): | 88 for f, line_num, line in input_api.RightHandSideLines(): |
| 89 if len(line) > maxlen: | 89 if len(line) > maxlen: |
| 90 bad.append( | 90 bad.append( |
| 91 '%s, line %s, %s chars' % | 91 '%s, line %s, %s chars' % |
| 92 (f.LocalPath(), line_num, len(line))) | 92 (f.LocalPath(), line_num, len(line))) |
| 93 if len(bad) == 5: # Just show the first 5 errors. | 93 if len(bad) == 5: # Just show the first 5 errors. |
| 94 break | 94 break |
| 95 | 95 |
| 96 if bad: | 96 if bad: |
| 97 msg = "Found lines longer than %s characters (first 5 shown)." % maxlen | 97 msg = "Found lines longer than %s characters (first 5 shown)." % maxlen |
| 98 return [output_api.PresubmitPromptWarning(msg, items=bad)] | 98 return [output_api.PresubmitPromptWarning(msg, items=bad)] |
| 99 else: | 99 else: |
| 100 return [] | 100 return [] |
| 101 | 101 |
| 102 | 102 |
| 103 def CheckTreeIsOpen(input_api, output_api, url, closed): | 103 def CheckTreeIsOpen(input_api, output_api, url, closed): |
| 104 """Checks that an url's content doesn't match a regexp that would mean that | 104 """Checks that an url's content doesn't match a regexp that would mean that |
| 105 the tree is closed.""" | 105 the tree is closed.""" |
| 106 assert(input_api.is_committing) |
| 106 try: | 107 try: |
| 107 connection = input_api.urllib2.urlopen(url) | 108 connection = input_api.urllib2.urlopen(url) |
| 108 status = connection.read() | 109 status = connection.read() |
| 109 connection.close() | 110 connection.close() |
| 110 if input_api.re.match(closed, status): | 111 if input_api.re.match(closed, status): |
| 111 long_text = status + '\n' + url | 112 long_text = status + '\n' + url |
| 112 return [output_api.PresubmitError("The tree is closed.", | 113 return [output_api.PresubmitPromptWarning("The tree is closed.", |
| 113 long_text=long_text)] | 114 long_text=long_text)] |
| 114 except IOError: | 115 except IOError: |
| 115 pass | 116 pass |
| 116 return [] | 117 return [] |
| 117 | 118 |
| 118 | 119 |
| 119 def _RunPythonUnitTests_LoadTests(input_api, module_name): | 120 def _RunPythonUnitTests_LoadTests(input_api, module_name): |
| 120 """Meant to be stubbed out during unit testing.""" | 121 """Meant to be stubbed out during unit testing.""" |
| 121 module = __import__(module_name) | 122 module = __import__(module_name) |
| 122 for part in module_name.split('.')[1:]: | 123 for part in module_name.split('.')[1:]: |
| 123 module = getattr(module, part) | 124 module = getattr(module, part) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 138 except ImportError: | 139 except ImportError: |
| 139 outputs.append(message_type("Failed to load %s" % unit_test, | 140 outputs.append(message_type("Failed to load %s" % unit_test, |
| 140 long_text=input_api.traceback.format_exc())) | 141 long_text=input_api.traceback.format_exc())) |
| 141 | 142 |
| 142 results = input_api.unittest.TextTestRunner(verbosity=0).run( | 143 results = input_api.unittest.TextTestRunner(verbosity=0).run( |
| 143 input_api.unittest.TestSuite(tests_suite)) | 144 input_api.unittest.TestSuite(tests_suite)) |
| 144 if not results.wasSuccessful(): | 145 if not results.wasSuccessful(): |
| 145 outputs.append(message_type( | 146 outputs.append(message_type( |
| 146 "%d unit tests failed." % (results.failures + results.errors))) | 147 "%d unit tests failed." % (results.failures + results.errors))) |
| 147 return outputs | 148 return outputs |
| OLD | NEW |