| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 return [output_api.PresubmitError( | 77 return [output_api.PresubmitError( |
| 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 basename = input_api.basename | |
| 88 | |
| 89 bad = [] | 87 bad = [] |
| 90 for f, line_num, line in input_api.RightHandSideLines(): | 88 for f, line_num, line in input_api.RightHandSideLines(): |
| 91 if line.endswith('\n'): | |
| 92 line = line[:-1] | |
| 93 if len(line) > maxlen: | 89 if len(line) > maxlen: |
| 94 bad.append( | 90 bad.append( |
| 95 '%s, line %s, %s chars' % | 91 '%s, line %s, %s chars' % |
| 96 (basename(f.LocalPath()), line_num, len(line))) | 92 (f.LocalPath(), line_num, len(line))) |
| 97 if len(bad) == 5: # Just show the first 5 errors. | 93 if len(bad) == 5: # Just show the first 5 errors. |
| 98 break | 94 break |
| 99 | 95 |
| 100 if bad: | 96 if bad: |
| 101 msg = "Found lines longer than %s characters (first 5 shown)." % maxlen | 97 msg = "Found lines longer than %s characters (first 5 shown)." % maxlen |
| 102 return [output_api.PresubmitPromptWarning(msg, items=bad)] | 98 return [output_api.PresubmitPromptWarning(msg, items=bad)] |
| 103 else: | 99 else: |
| 104 return [] | 100 return [] |
| 105 | 101 |
| 106 | 102 |
| 107 def CheckTreeIsOpen(input_api, output_api, url, closed): | 103 def CheckTreeIsOpen(input_api, output_api, url, closed): |
| 108 """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 |
| 109 the tree is closed.""" | 105 the tree is closed.""" |
| 110 try: | 106 try: |
| 111 connection = input_api.urllib2.urlopen(url) | 107 connection = input_api.urllib2.urlopen(url) |
| 112 status = connection.read() | 108 status = connection.read() |
| 113 connection.close() | 109 connection.close() |
| 114 if input_api.re.match(closed, status): | 110 if input_api.re.match(closed, status): |
| 115 long_text = status + '\n' + url | 111 long_text = status + '\n' + url |
| 116 return [output_api.PresubmitError("The tree is closed.", | 112 return [output_api.PresubmitError("The tree is closed.", |
| 117 long_text=long_text)] | 113 long_text=long_text)] |
| 118 except IOError: | 114 except IOError: |
| 119 pass | 115 pass |
| 120 return [] | 116 return [] |
| 121 | 117 |
| 122 | 118 |
| 119 def _RunPythonUnitTests_LoadTests(input_api, module_name): |
| 120 """Meant to be stubbed out during unit testing.""" |
| 121 module = __import__(module_name) |
| 122 for part in module_name.split('.')[1:]: |
| 123 module = getattr(module, part) |
| 124 return input_api.unittest.TestLoader().loadTestsFromModule(module)._tests |
| 125 |
| 123 def RunPythonUnitTests(input_api, output_api, unit_tests): | 126 def RunPythonUnitTests(input_api, output_api, unit_tests): |
| 124 """Imports the unit_tests modules and run them.""" | 127 """Imports the unit_tests modules and run them.""" |
| 125 import unittest | |
| 126 tests_suite = [] | 128 tests_suite = [] |
| 127 test_loader = unittest.TestLoader() | |
| 128 def LoadTests(module_name): | |
| 129 module = __import__(module_name) | |
| 130 for part in module_name.split('.')[1:]: | |
| 131 module = getattr(module, part) | |
| 132 tests_suite.extend(test_loader.loadTestsFromModule(module)._tests) | |
| 133 | |
| 134 outputs = [] | 129 outputs = [] |
| 135 for unit_test in unit_tests: | 130 for unit_test in unit_tests: |
| 136 try: | 131 try: |
| 137 LoadTests(unit_test) | 132 tests_suite.extend(_RunPythonUnitTests_LoadTests(unit_test)) |
| 138 except ImportError: | 133 except ImportError: |
| 139 outputs.Append(output_api.PresubmitError("Failed to load %s" % unit_test)) | 134 outputs.append(output_api.PresubmitError("Failed to load %s" % unit_test)) |
| 140 raise | |
| 141 | 135 |
| 142 results = unittest.TextTestRunner(verbosity=0).run(unittest.TestSuite( | 136 results = input_api.unittest.TextTestRunner(verbosity=0).run( |
| 143 tests_suite)) | 137 input_api.unittest.TestSuite(tests_suite)) |
| 144 if not results.wasSuccessful(): | 138 if not results.wasSuccessful(): |
| 145 outputs.append(output_api.PresubmitError( | 139 outputs.append(output_api.PresubmitError( |
| 146 "%d unit tests failed." % (results.failures + results.errors))) | 140 "%d unit tests failed." % (results.failures + results.errors))) |
| 147 return outputs | 141 return outputs |
| OLD | NEW |