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):
"""Checks that there aren't any lines longer than maxlen characters in any of
the text files to be submitted.
"""
basename = input_api.basename
bad = []
for f, line_num, line in input_api.RightHandSideLines():
if line.endswith('\n'):
line = line[:-1]
if len(line) > maxlen:
bad.append(
'%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.
break
......@@ -120,27 +116,25 @@ def CheckTreeIsOpen(input_api, output_api, url, closed):
return []
def RunPythonUnitTests(input_api, output_api, unit_tests):
"""Imports the unit_tests modules and run them."""
import unittest
tests_suite = []
test_loader = unittest.TestLoader()
def LoadTests(module_name):
def _RunPythonUnitTests_LoadTests(input_api, module_name):
"""Meant to be stubbed out during unit testing."""
module = __import__(module_name)
for part in module_name.split('.')[1:]:
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 = []
for unit_test in unit_tests:
try:
LoadTests(unit_test)
tests_suite.extend(_RunPythonUnitTests_LoadTests(unit_test))
except ImportError:
outputs.Append(output_api.PresubmitError("Failed to load %s" % unit_test))
raise
outputs.append(output_api.PresubmitError("Failed to load %s" % unit_test))
results = unittest.TextTestRunner(verbosity=0).run(unittest.TestSuite(
tests_suite))
results = input_api.unittest.TextTestRunner(verbosity=0).run(
input_api.unittest.TestSuite(tests_suite))
if not results.wasSuccessful():
outputs.append(output_api.PresubmitError(
"%d unit tests failed." % (results.failures + results.errors)))
......
......@@ -6,7 +6,7 @@
"""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
# caching (between all different invocations of presubmit scripts for a given
......@@ -26,6 +26,7 @@ import subprocess # Exposed through the API.
import sys # Parts exposed through API.
import tempfile # Exposed through the API.
import types
import unittest # Exposed through the API.
import urllib2 # Exposed through the API.
import warnings
......@@ -172,6 +173,7 @@ class InputApi(object):
self.re = re
self.subprocess = subprocess
self.tempfile = tempfile
self.unittest = unittest
self.urllib2 = urllib2
# InputApi.platform is the platform you're currently running on.
......@@ -273,6 +275,8 @@ class InputApi(object):
the AffectedFile instance of the current file;
integer line number (1-based); and
the contents of the line as a string.
Note: The cariage return (LF or CR) is stripped off.
"""
return InputApi._RightHandSideLinesImpl(
filter(lambda x: x.IsTextFile(),
......@@ -349,6 +353,7 @@ class AffectedFile(object):
side".
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():
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