Presubmit tests: allow 100columns limit for .java files.

Chrome for Android introduced the requirement for java files.
Android's style guide:
http://source.android.com/source/code-style.html#limit-line-length
defines 100 columns for .java files.
This patch changes the presubmit tests which are also used by chromium's CQ.

TEST=testCannedCheckJavaLongLines

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@122938 0039d316-1c4b-4281-b951-d872f2087c98
experimental/szager/collated-output
bulach@chromium.org 13 years ago
parent 8fab6b66ff
commit bfffd45e89

@ -79,7 +79,7 @@ def CheckDoNotSubmitInFiles(input_api, output_api):
# We want to check every text file, not just source files. # We want to check every text file, not just source files.
file_filter = lambda x : x file_filter = lambda x : x
keyword = 'DO NOT ' + 'SUBMIT' keyword = 'DO NOT ' + 'SUBMIT'
errors = _FindNewViolationsOfRule(lambda line : keyword not in line, errors = _FindNewViolationsOfRule(lambda _, line : keyword not in line,
input_api, file_filter) input_api, file_filter)
text = '\n'.join('Found %s in %s' % (keyword, loc) for loc in errors) text = '\n'.join('Found %s in %s' % (keyword, loc) for loc in errors)
if text: if text:
@ -234,8 +234,8 @@ def _FindNewViolationsOfRule(callable_rule, input_api, source_file_filter=None,
"""Find all newly introduced violations of a per-line rule (a callable). """Find all newly introduced violations of a per-line rule (a callable).
Arguments: Arguments:
callable_rule: a callable taking a line of input and returning True callable_rule: a callable taking a file extension and line of input and
if the rule is satisfied and False if there was a problem. returning True if the rule is satisfied and False if there was a problem.
input_api: object to enumerate the affected files. input_api: object to enumerate the affected files.
source_file_filter: a filter to be passed to the input api. source_file_filter: a filter to be passed to the input api.
error_formatter: a callable taking (filename, line_number, line) and error_formatter: a callable taking (filename, line_number, line) and
@ -251,11 +251,12 @@ def _FindNewViolationsOfRule(callable_rule, input_api, source_file_filter=None,
# to the SCM to determine the changed region can be quite expensive on # to the SCM to determine the changed region can be quite expensive on
# Win32. Assuming that most files will be kept problem-free, we can # Win32. Assuming that most files will be kept problem-free, we can
# skip the SCM operations most of the time. # skip the SCM operations most of the time.
if all(callable_rule(line) for line in f.NewContents()): extension = str(f.LocalPath()).rsplit('.', 1)[-1]
if all(callable_rule(extension, line) for line in f.NewContents()):
continue # No violation found in full text: can skip considering diff. continue # No violation found in full text: can skip considering diff.
for line_num, line in f.ChangedContents(): for line_num, line in f.ChangedContents():
if not callable_rule(line): if not callable_rule(extension, line):
errors.append(error_formatter(f.LocalPath(), line_num, line)) errors.append(error_formatter(f.LocalPath(), line_num, line))
return errors return errors
@ -274,7 +275,7 @@ def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None):
('Makefile', 'makefile') and ('Makefile', 'makefile') and
source_file_filter(affected_file)) source_file_filter(affected_file))
tabs = _FindNewViolationsOfRule(lambda line : '\t' not in line, tabs = _FindNewViolationsOfRule(lambda _, line : '\t' not in line,
input_api, filter_more) input_api, filter_more)
if tabs: if tabs:
@ -287,7 +288,7 @@ def CheckChangeTodoHasOwner(input_api, output_api, source_file_filter=None):
"""Checks that the user didn't add TODO(name) without an owner.""" """Checks that the user didn't add TODO(name) without an owner."""
unowned_todo = input_api.re.compile('TO' + 'DO[^(]') unowned_todo = input_api.re.compile('TO' + 'DO[^(]')
errors = _FindNewViolationsOfRule(lambda x : not unowned_todo.search(x), errors = _FindNewViolationsOfRule(lambda _, x : not unowned_todo.search(x),
input_api, source_file_filter) input_api, source_file_filter)
errors = ['Found TO' + 'DO with no owner in ' + x for x in errors] errors = ['Found TO' + 'DO with no owner in ' + x for x in errors]
if errors: if errors:
@ -298,7 +299,7 @@ def CheckChangeTodoHasOwner(input_api, output_api, source_file_filter=None):
def CheckChangeHasNoStrayWhitespace(input_api, output_api, def CheckChangeHasNoStrayWhitespace(input_api, output_api,
source_file_filter=None): source_file_filter=None):
"""Checks that there is no stray whitespace at source lines end.""" """Checks that there is no stray whitespace at source lines end."""
errors = _FindNewViolationsOfRule(lambda line : line.rstrip() == line, errors = _FindNewViolationsOfRule(lambda _, line : line.rstrip() == line,
input_api, source_file_filter) input_api, source_file_filter)
if errors: if errors:
return [output_api.PresubmitPromptWarning( return [output_api.PresubmitPromptWarning(
@ -311,18 +312,25 @@ def CheckLongLines(input_api, output_api, maxlen=80, source_file_filter=None):
"""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.
""" """
# Stupidly long symbols that needs to be worked around if takes 66% of line. maxlens = {
long_symbol = maxlen * 2 / 3 'java': 100,
# Hard line length limit at 50% more. '': maxlen,
extra_maxlen = maxlen * 3 / 2 }
# Note: these are C++ specific but processed on all languages. :( # Note: these are C++ specific but processed on all languages. :(
MACROS = ('#define', '#include', '#import', '#pragma', '#if', '#endif') MACROS = ('#define', '#include', '#import', '#pragma', '#if', '#endif')
def no_long_lines(line): def no_long_lines(file_extension, line):
if len(line) <= maxlen: file_maxlen = maxlens.get(file_extension, maxlens[''])
# Stupidly long symbols that needs to be worked around if takes 66% of line.
long_symbol = file_maxlen * 2 / 3
# Hard line length limit at 50% more.
extra_maxlen = file_maxlen * 3 / 2
line_len = len(line)
if line_len <= file_maxlen:
return True return True
if len(line) > extra_maxlen: if line_len > extra_maxlen:
return False return False
return ( return (

@ -1498,13 +1498,16 @@ class CannedChecksUnittest(PresubmitTestsBase):
self.assertEquals(len(results2), 1) self.assertEquals(len(results2), 1)
self.assertEquals(results2[0].__class__, error_type) self.assertEquals(results2[0].__class__, error_type)
def ContentTest(self, check, content1, content2, error_type): def ContentTest(self, check, content1, content1_path, content2,
content2_path, error_type):
"""Runs a test of a content-checking rule. """Runs a test of a content-checking rule.
Args: Args:
check: the check to run. check: the check to run.
content1: content which is expected to pass the check. content1: content which is expected to pass the check.
content1_path: file path for content1.
content2: content which is expected to fail the check. content2: content which is expected to fail the check.
content2_path: file path for content2.
error_type: the type of the error expected for content2. error_type: the type of the error expected for content2.
""" """
change1 = presubmit.Change( change1 = presubmit.Change(
@ -1514,12 +1517,13 @@ class CannedChecksUnittest(PresubmitTestsBase):
input_api1.AffectedFiles( input_api1.AffectedFiles(
include_deletes=False, include_deletes=False,
file_filter=mox.IgnoreArg()).AndReturn([affected_file]) file_filter=mox.IgnoreArg()).AndReturn([affected_file])
affected_file.LocalPath().AndReturn(content1_path)
affected_file.NewContents().AndReturn([ affected_file.NewContents().AndReturn([
'ahoy', 'afoo',
content1, content1,
'hay', 'bfoo',
'yer', 'cfoo',
'ya']) 'dfoo'])
change2 = presubmit.Change( change2 = presubmit.Change(
'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None)
@ -1528,19 +1532,20 @@ class CannedChecksUnittest(PresubmitTestsBase):
input_api2.AffectedFiles( input_api2.AffectedFiles(
include_deletes=False, include_deletes=False,
file_filter=mox.IgnoreArg()).AndReturn([affected_file]) file_filter=mox.IgnoreArg()).AndReturn([affected_file])
affected_file.LocalPath().AndReturn(content2_path)
affected_file.NewContents().AndReturn([ affected_file.NewContents().AndReturn([
'ahoy', 'dfoo',
content2, content2,
'hay', 'efoo',
'yer', 'ffoo',
'ya']) 'gfoo'])
# It falls back to ChangedContents when there is a failure. This is an # It falls back to ChangedContents when there is a failure. This is an
# optimization since NewContents() is much faster to execute than # optimization since NewContents() is much faster to execute than
# ChangedContents(). # ChangedContents().
affected_file.ChangedContents().AndReturn([ affected_file.ChangedContents().AndReturn([
(42, content2), (42, content2),
(43, 'yer'), (43, 'hfoo'),
(23, 'ya')]) (23, 'ifoo')])
affected_file.LocalPath().AndReturn('foo.cc') affected_file.LocalPath().AndReturn('foo.cc')
self.mox.ReplayAll() self.mox.ReplayAll()
@ -1663,14 +1668,14 @@ class CannedChecksUnittest(PresubmitTestsBase):
def testCannedCheckDoNotSubmitInFiles(self): def testCannedCheckDoNotSubmitInFiles(self):
self.ContentTest( self.ContentTest(
lambda x,y,z: presubmit_canned_checks.CheckDoNotSubmitInFiles(x, y), lambda x,y,z: presubmit_canned_checks.CheckDoNotSubmitInFiles(x, y),
'DO NOTSUBMIT', 'DO NOT ' + 'SUBMIT', 'DO NOTSUBMIT', None, 'DO NOT ' + 'SUBMIT', None,
presubmit.OutputApi.PresubmitError) presubmit.OutputApi.PresubmitError)
def testCheckChangeHasNoStrayWhitespace(self): def testCheckChangeHasNoStrayWhitespace(self):
self.ContentTest( self.ContentTest(
lambda x,y,z: lambda x,y,z:
presubmit_canned_checks.CheckChangeHasNoStrayWhitespace(x, y), presubmit_canned_checks.CheckChangeHasNoStrayWhitespace(x, y),
'Foo', 'Foo ', 'Foo', None, 'Foo ', None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCheckChangeHasOnlyOneEol(self): def testCheckChangeHasOnlyOneEol(self):
@ -1696,12 +1701,12 @@ class CannedChecksUnittest(PresubmitTestsBase):
def testCheckChangeTodoHasOwner(self): def testCheckChangeTodoHasOwner(self):
self.ContentTest(presubmit_canned_checks.CheckChangeTodoHasOwner, self.ContentTest(presubmit_canned_checks.CheckChangeTodoHasOwner,
"TODO(foo): bar", "TODO: bar", "TODO(foo): bar", None, "TODO: bar", None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCannedCheckChangeHasNoTabs(self): def testCannedCheckChangeHasNoTabs(self):
self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs,
'blah blah', 'blah\tblah', 'blah blah', None, 'blah\tblah', None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
# Make sure makefiles are ignored. # Make sure makefiles are ignored.
@ -1716,8 +1721,10 @@ class CannedChecksUnittest(PresubmitTestsBase):
affected_file3.LocalPath().AndReturn('makefile') affected_file3.LocalPath().AndReturn('makefile')
# Only this one will trigger. # Only this one will trigger.
affected_file4 = self.mox.CreateMock(presubmit.SvnAffectedFile) affected_file4 = self.mox.CreateMock(presubmit.SvnAffectedFile)
affected_file4.LocalPath().AndReturn('makefile.foo') affected_file1.LocalPath().AndReturn('foo.cc')
affected_file1.NewContents().AndReturn(['yo, ']) affected_file1.NewContents().AndReturn(['yo, '])
affected_file4.LocalPath().AndReturn('makefile.foo')
affected_file4.LocalPath().AndReturn('makefile.foo')
affected_file4.NewContents().AndReturn(['ye\t']) affected_file4.NewContents().AndReturn(['ye\t'])
affected_file4.ChangedContents().AndReturn([(46, 'ye\t')]) affected_file4.ChangedContents().AndReturn([(46, 'ye\t')])
affected_file4.LocalPath().AndReturn('makefile.foo') affected_file4.LocalPath().AndReturn('makefile.foo')
@ -1744,12 +1751,17 @@ class CannedChecksUnittest(PresubmitTestsBase):
def testCannedCheckLongLines(self): def testCannedCheckLongLines(self):
check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z)
self.ContentTest(check, '0123456789', '01234567890', self.ContentTest(check, '0123456789', None, '01234567890', None,
presubmit.OutputApi.PresubmitPromptWarning)
def testCannedCheckJavaLongLines(self):
check = lambda x, y, _: presubmit_canned_checks.CheckLongLines(x, y)
self.ContentTest(check, 'A ' * 50, 'foo.java', 'A ' * 50 + 'B', 'foo.java',
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCannedCheckLongLinesLF(self): def testCannedCheckLongLinesLF(self):
check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z) check = lambda x, y, z: presubmit_canned_checks.CheckLongLines(x, y, 10, z)
self.ContentTest(check, '012345678\n', '0123456789\n', self.ContentTest(check, '012345678\n', None, '0123456789\n', None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCannedCheckLongLinesMacro(self): def testCannedCheckLongLinesMacro(self):
@ -1758,7 +1770,9 @@ class CannedChecksUnittest(PresubmitTestsBase):
check, check,
# Put a space in so it doesn't trigger long symbols. Allow 1/3 more. # Put a space in so it doesn't trigger long symbols. Allow 1/3 more.
'#if 56 89 12 45', '#if 56 89 12 45',
None,
'#if 56 89 12 456', '#if 56 89 12 456',
None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCannedCheckLongLinesHttp(self): def testCannedCheckLongLinesHttp(self):
@ -1766,7 +1780,9 @@ class CannedChecksUnittest(PresubmitTestsBase):
self.ContentTest( self.ContentTest(
check, check,
' http:// 0 23 5', ' http:// 0 23 5',
None,
' http:// 0 23 56', ' http:// 0 23 56',
None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCannedCheckLongLinesLongSymbol(self): def testCannedCheckLongLinesLongSymbol(self):
@ -1774,7 +1790,9 @@ class CannedChecksUnittest(PresubmitTestsBase):
self.ContentTest( self.ContentTest(
check, check,
' TUP5D_LoNG_SY ', ' TUP5D_LoNG_SY ',
None,
' TUP5D_LoNG_SY5 ', ' TUP5D_LoNG_SY5 ',
None,
presubmit.OutputApi.PresubmitPromptWarning) presubmit.OutputApi.PresubmitPromptWarning)
def testCheckChangeSvnEolStyleCommit(self): def testCheckChangeSvnEolStyleCommit(self):
@ -2257,6 +2275,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
for _ in range(3): for _ in range(3):
input_api.AffectedFiles(file_filter=mox.IgnoreArg(), include_deletes=False input_api.AffectedFiles(file_filter=mox.IgnoreArg(), include_deletes=False
).AndReturn([affected_file]) ).AndReturn([affected_file])
affected_file.LocalPath()
affected_file.NewContents().AndReturn('Hey!\nHo!\nHey!\nHo!\n\n') affected_file.NewContents().AndReturn('Hey!\nHo!\nHey!\nHo!\n\n')
affected_file.ChangedContents().AndReturn([ affected_file.ChangedContents().AndReturn([
(0, 'Hey!\n'), (0, 'Hey!\n'),

Loading…
Cancel
Save