Commit 1487d53b authored by maruel@chromium.org's avatar maruel@chromium.org

Improve the presubmit_canned_checks testing by using a real mock and testing for more cases.

Remove a superfluous check in CheckLongLines().

Add unittest to InputApi.

Review URL: http://codereview.chromium.org/114082

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@17805 0039d316-1c4b-4281-b951-d872f2087c98
parent 79613219
...@@ -84,16 +84,12 @@ def CheckLongLines(input_api, output_api, maxlen=80): ...@@ -84,16 +84,12 @@ def CheckLongLines(input_api, output_api, maxlen=80):
"""Checks that there aren't any lines longer than maxlen characters in any of """Checks that there aren't any lines longer than maxlen characters in any of
the text files to be submitted. the text files to be submitted.
""" """
basename = input_api.basename
bad = [] bad = []
for f, line_num, line in input_api.RightHandSideLines(): for f, line_num, line in input_api.RightHandSideLines():
if line.endswith('\n'):
line = line[:-1]
if len(line) > maxlen: if len(line) > maxlen:
bad.append( bad.append(
'%s, line %s, %s chars' % '%s, line %s, %s chars' %
(basename(f.LocalPath()), line_num, len(line))) (f.LocalPath(), line_num, len(line)))
if len(bad) == 5: # Just show the first 5 errors. if len(bad) == 5: # Just show the first 5 errors.
break break
...@@ -120,27 +116,25 @@ def CheckTreeIsOpen(input_api, output_api, url, closed): ...@@ -120,27 +116,25 @@ def CheckTreeIsOpen(input_api, output_api, url, closed):
return [] return []
def RunPythonUnitTests(input_api, output_api, unit_tests): def _RunPythonUnitTests_LoadTests(input_api, module_name):
"""Imports the unit_tests modules and run them.""" """Meant to be stubbed out during unit testing."""
import unittest
tests_suite = []
test_loader = unittest.TestLoader()
def LoadTests(module_name):
module = __import__(module_name) module = __import__(module_name)
for part in module_name.split('.')[1:]: for part in module_name.split('.')[1:]:
module = getattr(module, part) module = getattr(module, part)
tests_suite.extend(test_loader.loadTestsFromModule(module)._tests) return input_api.unittest.TestLoader().loadTestsFromModule(module)._tests
def RunPythonUnitTests(input_api, output_api, unit_tests):
"""Imports the unit_tests modules and run them."""
tests_suite = []
outputs = [] outputs = []
for unit_test in unit_tests: for unit_test in unit_tests:
try: try:
LoadTests(unit_test) tests_suite.extend(_RunPythonUnitTests_LoadTests(unit_test))
except ImportError: except ImportError:
outputs.Append(output_api.PresubmitError("Failed to load %s" % unit_test)) outputs.append(output_api.PresubmitError("Failed to load %s" % unit_test))
raise
results = unittest.TextTestRunner(verbosity=0).run(unittest.TestSuite( results = input_api.unittest.TextTestRunner(verbosity=0).run(
tests_suite)) input_api.unittest.TestSuite(tests_suite))
if not results.wasSuccessful(): if not results.wasSuccessful():
outputs.append(output_api.PresubmitError( outputs.append(output_api.PresubmitError(
"%d unit tests failed." % (results.failures + results.errors))) "%d unit tests failed." % (results.failures + results.errors)))
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
"""Enables directory-specific presubmit checks to run at upload and/or commit. """Enables directory-specific presubmit checks to run at upload and/or commit.
""" """
__version__ = '1.1' __version__ = '1.2'
# TODO(joi) Add caching where appropriate/needed. The API is designed to allow # TODO(joi) Add caching where appropriate/needed. The API is designed to allow
# caching (between all different invocations of presubmit scripts for a given # caching (between all different invocations of presubmit scripts for a given
...@@ -26,6 +26,7 @@ import subprocess # Exposed through the API. ...@@ -26,6 +26,7 @@ import subprocess # Exposed through the API.
import sys # Parts exposed through API. import sys # Parts exposed through API.
import tempfile # Exposed through the API. import tempfile # Exposed through the API.
import types import types
import unittest # Exposed through the API.
import urllib2 # Exposed through the API. import urllib2 # Exposed through the API.
import warnings import warnings
...@@ -172,6 +173,7 @@ class InputApi(object): ...@@ -172,6 +173,7 @@ class InputApi(object):
self.re = re self.re = re
self.subprocess = subprocess self.subprocess = subprocess
self.tempfile = tempfile self.tempfile = tempfile
self.unittest = unittest
self.urllib2 = urllib2 self.urllib2 = urllib2
# InputApi.platform is the platform you're currently running on. # InputApi.platform is the platform you're currently running on.
...@@ -273,6 +275,8 @@ class InputApi(object): ...@@ -273,6 +275,8 @@ class InputApi(object):
the AffectedFile instance of the current file; the AffectedFile instance of the current file;
integer line number (1-based); and integer line number (1-based); and
the contents of the line as a string. the contents of the line as a string.
Note: The cariage return (LF or CR) is stripped off.
""" """
return InputApi._RightHandSideLinesImpl( return InputApi._RightHandSideLinesImpl(
filter(lambda x: x.IsTextFile(), filter(lambda x: x.IsTextFile(),
...@@ -349,6 +353,7 @@ class AffectedFile(object): ...@@ -349,6 +353,7 @@ class AffectedFile(object):
side". side".
Contents will be empty if the file is a directory or does not exist. Contents will be empty if the file is a directory or does not exist.
Note: The cariage returns (LF or CR) are stripped off.
""" """
if self.IsDirectory(): if self.IsDirectory():
return [] return []
......
This diff is collapsed.
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