Commit 0018e731 authored by maruel@chromium.org's avatar maruel@chromium.org

Seems like revision 17554 busted the presubmit scripts, reverting.

TEST=None
BUG=None
TBR=joi
Review URL: http://codereview.chromium.org/119122

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@17561 0039d316-1c4b-4281-b951-d872f2087c98
parent 8b40a9b8
...@@ -230,9 +230,8 @@ class InputApi(object): ...@@ -230,9 +230,8 @@ class InputApi(object):
dir_with_slash = normpath("%s/" % self.PresubmitLocalPath()) dir_with_slash = normpath("%s/" % self.PresubmitLocalPath())
if len(dir_with_slash) == 1: if len(dir_with_slash) == 1:
dir_with_slash = '' dir_with_slash = ''
return filter( return filter(lambda x: normpath(x.LocalPath()).startswith(dir_with_slash),
lambda x: normpath(x.AbsoluteLocalPath()).startswith(dir_with_slash), self.change.AffectedFiles(include_dirs, include_deletes))
self.change.AffectedFiles(include_dirs, include_deletes))
def LocalPaths(self, include_dirs=False): def LocalPaths(self, include_dirs=False):
"""Returns local paths of input_api.AffectedFiles().""" """Returns local paths of input_api.AffectedFiles()."""
...@@ -549,29 +548,33 @@ class GclChange(object): ...@@ -549,29 +548,33 @@ class GclChange(object):
self.AffectedFiles(include_deletes=False))) self.AffectedFiles(include_deletes=False)))
def ListRelevantPresubmitFiles(files, root): def ListRelevantPresubmitFiles(files):
"""Finds all presubmit files that apply to a given set of source files. """Finds all presubmit files that apply to a given set of source files.
Args: Args:
files: An iterable container containing file paths. files: An iterable container containing file paths.
root: Path where to stop searching.
Return: Return:
List of absolute paths of the existing PRESUBMIT.py scripts. ['foo/blat/PRESUBMIT.py', 'mat/gat/PRESUBMIT.py']
""" """
entries = [] checked_dirs = {} # Keys are directory paths, values are ignored.
for f in files: source_dirs = [os.path.dirname(f) for f in files]
f = normpath(os.path.join(root, f)) presubmit_files = []
while f: for dir in source_dirs:
f = os.path.dirname(f) while (True):
if f in entries: if dir in checked_dirs:
break # We've already walked up from this directory.
test_path = os.path.join(dir, 'PRESUBMIT.py')
if os.path.isfile(test_path):
presubmit_files.append(normpath(test_path))
checked_dirs[dir] = ''
if dir in ['', '.']:
break break
entries.append(f) else:
if f == root: dir = os.path.dirname(dir)
break return presubmit_files
entries.sort()
entries = map(lambda x: os.path.join(x, 'PRESUBMIT.py'), entries)
return filter(lambda x: os.path.isfile(x), entries)
class PresubmitExecuter(object): class PresubmitExecuter(object):
...@@ -650,9 +653,7 @@ def DoPresubmitChecks(change_info, ...@@ -650,9 +653,7 @@ def DoPresubmitChecks(change_info,
Return: Return:
True if execution can continue, False if not. True if execution can continue, False if not.
""" """
checkout_root = gcl.GetRepositoryRoot() presubmit_files = ListRelevantPresubmitFiles(change_info.FileList())
presubmit_files = ListRelevantPresubmitFiles(change_info.FileList(),
checkout_root)
if not presubmit_files and verbose: if not presubmit_files and verbose:
output_stream.write("Warning, no presubmit.py found.\n") output_stream.write("Warning, no presubmit.py found.\n")
results = [] results = []
...@@ -660,8 +661,7 @@ def DoPresubmitChecks(change_info, ...@@ -660,8 +661,7 @@ def DoPresubmitChecks(change_info,
if default_presubmit: if default_presubmit:
if verbose: if verbose:
output_stream.write("Running default presubmit script.\n") output_stream.write("Running default presubmit script.\n")
fake_path = os.path.join(checkout_root, 'PRESUBMIT.py') results += executer.ExecPresubmitScript(default_presubmit, 'PRESUBMIT.py')
results += executer.ExecPresubmitScript(default_presubmit, fake_path)
for filename in presubmit_files: for filename in presubmit_files:
filename = os.path.abspath(filename) filename = os.path.abspath(filename)
if verbose: if verbose:
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
"""Unit tests for presubmit_support.py and presubmit_canned_checks.py.""" """Unit tests for presubmit_support.py and presubmit_canned_checks.py."""
import exceptions import exceptions
import random
import string
import StringIO import StringIO
import unittest import unittest
...@@ -17,30 +15,6 @@ import presubmit_support as presubmit ...@@ -17,30 +15,6 @@ import presubmit_support as presubmit
import presubmit_canned_checks import presubmit_canned_checks
mox = __init__.mox mox = __init__.mox
def String(max_length):
return ''.join([random.choice(string.letters)
for x in xrange(random.randint(1, max_length))])
def Strings(max_arg_count, max_arg_length):
return [String(max_arg_length) for x in xrange(max_arg_count)]
def Args(max_arg_count=8, max_arg_length=16):
return Strings(max_arg_count, random.randint(1, max_arg_length))
def _DirElts(max_elt_count=4, max_elt_length=8):
return presubmit.os.sep.join(Strings(max_elt_count, max_elt_length))
def Dir(max_elt_count=4, max_elt_length=8):
return (random.choice((presubmit_support.os.sep, '')) +
_DirElts(max_elt_count, max_elt_length))
def Url(max_elt_count=4, max_elt_length=8):
return ('svn://random_host:port/a' +
_DirElts(max_elt_count, max_elt_length).replace(os.sep, '/'))
def RootDir(max_elt_count=4, max_elt_length=8):
return presubmit.os.sep + _DirElts(max_elt_count, max_elt_length)
class PresubmitTestsBase(mox.MoxTestBase): class PresubmitTestsBase(mox.MoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is.""" """Setups and tear downs the mocks but doesn't test anything as-is."""
...@@ -60,27 +34,23 @@ def CheckChangeOnUpload(input_api, output_api): ...@@ -60,27 +34,23 @@ def CheckChangeOnUpload(input_api, output_api):
def setUp(self): def setUp(self):
mox.MoxTestBase.setUp(self) mox.MoxTestBase.setUp(self)
self.mox.StubOutWithMock(presubmit, 'warnings') self.mox.StubOutWithMock(presubmit, 'warnings')
# Stub out 'os' but keep os.path.dirname/join/normpath and os.sep. # Stub out 'os' but keep os.path.dirname/join/normpath.
os_sep = presubmit.os.sep path_join = presubmit.os.path.join
os_path_join = presubmit.os.path.join path_dirname = presubmit.os.path.dirname
os_path_dirname = presubmit.os.path.dirname path_normpath = presubmit.os.path.normpath
os_path_normpath = presubmit.os.path.normpath
self.mox.StubOutWithMock(presubmit, 'os') self.mox.StubOutWithMock(presubmit, 'os')
self.mox.StubOutWithMock(presubmit.os, 'path') self.mox.StubOutWithMock(presubmit.os, 'path')
presubmit.os.sep = os_sep presubmit.os.path.join = path_join
presubmit.os.path.join = os_path_join presubmit.os.path.dirname = path_dirname
presubmit.os.path.dirname = os_path_dirname presubmit.os.path.normpath = path_normpath
presubmit.os.path.normpath = os_path_normpath
self.mox.StubOutWithMock(presubmit, 'sys') self.mox.StubOutWithMock(presubmit, 'sys')
# Special mocks. # Special mocks.
def MockAbsPath(f): def MockAbsPath(f):
return f return f
presubmit.os.path.abspath = MockAbsPath presubmit.os.path.abspath = MockAbsPath
self.mox.StubOutWithMock(presubmit.gcl, 'GetRepositoryRoot') self.mox.StubOutWithMock(presubmit.gcl, 'GetRepositoryRoot')
fake_root_dir = RootDir()
self.fake_root_dir = fake_root_dir
def MockGetRepositoryRoot(): def MockGetRepositoryRoot():
return fake_root_dir return ''
presubmit.gcl.GetRepositoryRoot = MockGetRepositoryRoot presubmit.gcl.GetRepositoryRoot = MockGetRepositoryRoot
self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo') self.mox.StubOutWithMock(presubmit.gclient, 'CaptureSVNInfo')
self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty') self.mox.StubOutWithMock(presubmit.gcl, 'GetSVNFileProperty')
...@@ -104,7 +74,6 @@ def CheckChangeOnUpload(input_api, output_api): ...@@ -104,7 +74,6 @@ def CheckChangeOnUpload(input_api, output_api):
class PresubmitUnittest(PresubmitTestsBase): class PresubmitUnittest(PresubmitTestsBase):
"""General presubmit_support.py tests (excluding InputApi and OutputApi).""" """General presubmit_support.py tests (excluding InputApi and OutputApi)."""
def testMembersChanged(self): def testMembersChanged(self):
self.mox.ReplayAll()
members = [ members = [
'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi',
'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException',
...@@ -119,42 +88,35 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -119,42 +88,35 @@ class PresubmitUnittest(PresubmitTestsBase):
self.compareMembers(presubmit, members) self.compareMembers(presubmit, members)
def testListRelevantPresubmitFiles(self): def testListRelevantPresubmitFiles(self):
join = presubmit.os.path.join presubmit.os.path.isfile('PRESUBMIT.py').AndReturn(True)
files = [ presubmit.os.path.isfile(
'blat.cc', presubmit.os.path.join('foo/haspresubmit/yodle',
join('foo', 'haspresubmit', 'yodle', 'smart.h'), 'PRESUBMIT.py')).AndReturn(False)
join('moo', 'mat', 'gat', 'yo.h'), presubmit.os.path.isfile(
join('foo', 'luck.h'), presubmit.os.path.join('foo/haspresubmit',
] 'PRESUBMIT.py')).AndReturn(True)
presubmit.os.path.isfile(join(self.fake_root_dir, presubmit.os.path.isfile(
'PRESUBMIT.py')).AndReturn(True) presubmit.os.path.join('foo', 'PRESUBMIT.py')).AndReturn(False)
presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', presubmit.os.path.isfile(
'PRESUBMIT.py')).AndReturn(False) presubmit.os.path.join('moo/mat/gat',
presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', 'PRESUBMIT.py')).AndReturn(False)
'PRESUBMIT.py')).AndReturn(True) presubmit.os.path.isfile(
presubmit.os.path.isfile(join(self.fake_root_dir, 'foo', 'haspresubmit', presubmit.os.path.join('moo/mat',
'yodle', 'PRESUBMIT.py')).AndReturn(True) 'PRESUBMIT.py')).AndReturn(False)
presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', presubmit.os.path.isfile(
'PRESUBMIT.py')).AndReturn(False) presubmit.os.path.join('moo', 'PRESUBMIT.py')).AndReturn(False)
presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', self.mox.ReplayAll()
'PRESUBMIT.py')).AndReturn(False) presubmit_files = presubmit.ListRelevantPresubmitFiles([
presubmit.os.path.isfile(join(self.fake_root_dir, 'moo', 'mat', 'gat', 'blat.cc',
'PRESUBMIT.py')).AndReturn(False) 'foo/haspresubmit/yodle/smart.h',
#isfile(join('moo', 'PRESUBMIT.py')).AndReturn(False) 'moo/mat/gat/yo.h',
self.mox.ReplayAll() 'foo/luck.h'])
self.assertEqual(len(presubmit_files), 2)
presubmit_files = presubmit.ListRelevantPresubmitFiles(files, self.failUnless(presubmit.normpath('PRESUBMIT.py') in presubmit_files)
self.fake_root_dir) self.failUnless(presubmit.normpath('foo/haspresubmit/PRESUBMIT.py') in
self.assertEqual(presubmit_files, presubmit_files)
[
join(self.fake_root_dir, 'PRESUBMIT.py'),
join(self.fake_root_dir, 'foo', 'haspresubmit', 'PRESUBMIT.py'),
join(self.fake_root_dir, 'foo', 'haspresubmit', 'yodle',
'PRESUBMIT.py')
])
def testTagLineRe(self): def testTagLineRe(self):
self.mox.ReplayAll()
m = presubmit.GclChange._tag_line_re.match(' BUG =1223, 1445 \t') m = presubmit.GclChange._tag_line_re.match(' BUG =1223, 1445 \t')
self.failUnless(m) self.failUnless(m)
self.failUnlessEqual(m.group('key'), 'BUG') self.failUnlessEqual(m.group('key'), 'BUG')
...@@ -268,7 +230,6 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -268,7 +230,6 @@ class PresubmitUnittest(PresubmitTestsBase):
files = [ files = [
['A', 'foo\\blat.cc'], ['A', 'foo\\blat.cc'],
] ]
fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py')
self.mox.ReplayAll() self.mox.ReplayAll()
ci = presubmit.gcl.ChangeInfo(name='mychange', ci = presubmit.gcl.ChangeInfo(name='mychange',
...@@ -276,12 +237,12 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -276,12 +237,12 @@ class PresubmitUnittest(PresubmitTestsBase):
files=files) files=files)
executer = presubmit.PresubmitExecuter(ci, False) executer = presubmit.PresubmitExecuter(ci, False)
self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) self.failIf(executer.ExecPresubmitScript('', 'PRESUBMIT.py'))
# No error if no on-upload entry point # No error if no on-upload entry point
self.failIf(executer.ExecPresubmitScript( self.failIf(executer.ExecPresubmitScript(
('def CheckChangeOnCommit(input_api, output_api):\n' ('def CheckChangeOnCommit(input_api, output_api):\n'
' return (output_api.PresubmitError("!!"))\n'), ' return (output_api.PresubmitError("!!"))\n'),
fake_presubmit 'PRESUBMIT.py'
)) ))
executer = presubmit.PresubmitExecuter(ci, True) executer = presubmit.PresubmitExecuter(ci, True)
...@@ -289,7 +250,7 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -289,7 +250,7 @@ class PresubmitUnittest(PresubmitTestsBase):
self.failIf(executer.ExecPresubmitScript( self.failIf(executer.ExecPresubmitScript(
('def CheckChangeOnUpload(input_api, output_api):\n' ('def CheckChangeOnUpload(input_api, output_api):\n'
' return (output_api.PresubmitError("!!"))\n'), ' return (output_api.PresubmitError("!!"))\n'),
fake_presubmit 'PRESUBMIT.py'
)) ))
self.failIf(executer.ExecPresubmitScript( self.failIf(executer.ExecPresubmitScript(
...@@ -298,7 +259,7 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -298,7 +259,7 @@ class PresubmitUnittest(PresubmitTestsBase):
' return (output_api.PresubmitError("!!"))\n' ' return (output_api.PresubmitError("!!"))\n'
' else:\n' ' else:\n'
' return ()'), ' return ()'),
fake_presubmit 'PRESUBMIT.py'
)) ))
self.failUnless(executer.ExecPresubmitScript( self.failUnless(executer.ExecPresubmitScript(
...@@ -307,37 +268,33 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -307,37 +268,33 @@ class PresubmitUnittest(PresubmitTestsBase):
' return [output_api.PresubmitError("!!")]\n' ' return [output_api.PresubmitError("!!")]\n'
' else:\n' ' else:\n'
' return ()'), ' return ()'),
fake_presubmit 'PRESUBMIT.py'
)) ))
self.assertRaises(exceptions.RuntimeError, self.assertRaises(exceptions.RuntimeError,
executer.ExecPresubmitScript, executer.ExecPresubmitScript,
'def CheckChangeOnCommit(input_api, output_api):\n' 'def CheckChangeOnCommit(input_api, output_api):\n'
' return "foo"', ' return "foo"',
fake_presubmit) 'PRESUBMIT.py')
self.assertRaises(exceptions.RuntimeError, self.assertRaises(exceptions.RuntimeError,
executer.ExecPresubmitScript, executer.ExecPresubmitScript,
'def CheckChangeOnCommit(input_api, output_api):\n' 'def CheckChangeOnCommit(input_api, output_api):\n'
' return ["foo"]', ' return ["foo"]',
fake_presubmit) 'PRESUBMIT.py')
def testDoPresubmitChecks(self): def testDoPresubmitChecks(self):
join = presubmit.os.path.join
description_lines = ('Hello there', description_lines = ('Hello there',
'this is a change', 'this is a change',
'STORY=http://tracker/123') 'STORY=http://tracker/123')
files = [ files = [
['A', join('haspresubmit', 'blat.cc')], ['A', 'haspresubmit\\blat.cc'],
] ]
haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') path = presubmit.os.path.join('haspresubmit', 'PRESUBMIT.py')
root_path = join(self.fake_root_dir, 'PRESUBMIT.py') presubmit.os.path.isfile(path).AndReturn(True)
presubmit.os.path.isfile(root_path).AndReturn(True) presubmit.os.path.isfile('PRESUBMIT.py').AndReturn(True)
presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) presubmit.gcl.ReadFile(path, 'rU').AndReturn(self.presubmit_text)
presubmit.gcl.ReadFile(root_path, presubmit.gcl.ReadFile('PRESUBMIT.py', 'rU').AndReturn(self.presubmit_text)
'rU').AndReturn(self.presubmit_text)
presubmit.gcl.ReadFile(haspresubmit_path,
'rU').AndReturn(self.presubmit_text)
self.mox.ReplayAll() self.mox.ReplayAll()
ci = presubmit.gcl.ChangeInfo(name='mychange', ci = presubmit.gcl.ChangeInfo(name='mychange',
...@@ -352,22 +309,21 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -352,22 +309,21 @@ class PresubmitUnittest(PresubmitTestsBase):
self.assertEqual(output.getvalue().count('!!'), 2) self.assertEqual(output.getvalue().count('!!'), 2)
def testDoPresubmitChecksPromptsAfterWarnings(self): def testDoPresubmitChecksPromptsAfterWarnings(self):
join = presubmit.os.path.join
description_lines = ('Hello there', description_lines = ('Hello there',
'this is a change', 'this is a change',
'NOSUCHKEY=http://tracker/123') 'NOSUCHKEY=http://tracker/123')
files = [ files = [
['A', join('haspresubmit', 'blat.cc')], ['A', 'haspresubmit\\blat.cc'],
] ]
presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') path = presubmit.os.path.join('haspresubmit', 'PRESUBMIT.py')
haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') presubmit.os.path.isfile(path).AndReturn(True)
for i in range(2): presubmit.os.path.isfile('PRESUBMIT.py').AndReturn(True)
presubmit.os.path.isfile(presubmit_path).AndReturn(True) presubmit.gcl.ReadFile(path, 'rU').AndReturn(self.presubmit_text)
presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) presubmit.gcl.ReadFile('PRESUBMIT.py', 'rU').AndReturn(self.presubmit_text)
presubmit.gcl.ReadFile(presubmit_path, 'rU' presubmit.os.path.isfile(path).AndReturn(True)
).AndReturn(self.presubmit_text) presubmit.os.path.isfile('PRESUBMIT.py').AndReturn(True)
presubmit.gcl.ReadFile(haspresubmit_path, 'rU' presubmit.gcl.ReadFile(path, 'rU').AndReturn(self.presubmit_text)
).AndReturn(self.presubmit_text) presubmit.gcl.ReadFile('PRESUBMIT.py', 'rU').AndReturn(self.presubmit_text)
self.mox.ReplayAll() self.mox.ReplayAll()
ci = presubmit.gcl.ChangeInfo(name='mychange', ci = presubmit.gcl.ChangeInfo(name='mychange',
...@@ -391,29 +347,27 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -391,29 +347,27 @@ class PresubmitUnittest(PresubmitTestsBase):
self.assertEquals(output.getvalue().count('??'), 2) self.assertEquals(output.getvalue().count('??'), 2)
def testDoPresubmitChecksNoWarningPromptIfErrors(self): def testDoPresubmitChecksNoWarningPromptIfErrors(self):
join = presubmit.os.path.join
description_lines = ('Hello there', description_lines = ('Hello there',
'this is a change', 'this is a change',
'NOSUCHKEY=http://tracker/123', 'NOSUCHKEY=http://tracker/123',
'REALLYNOSUCHKEY=http://tracker/123') 'REALLYNOSUCHKEY=http://tracker/123')
files = [ files = [
['A', join('haspresubmit', 'blat.cc')], ['A', 'haspresubmit\\blat.cc'],
] ]
presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') path = presubmit.os.path.join('haspresubmit', 'PRESUBMIT.py')
haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', presubmit.os.path.isfile(path).AndReturn(True)
'PRESUBMIT.py') presubmit.os.path.isfile('PRESUBMIT.py').AndReturn(True)
presubmit.os.path.isfile(presubmit_path).AndReturn(True) presubmit.gcl.ReadFile(path, 'rU').AndReturn(self.presubmit_text)
presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) presubmit.gcl.ReadFile('PRESUBMIT.py', 'rU').AndReturn(self.presubmit_text)
presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text)
presubmit.gcl.ReadFile(haspresubmit_path, 'rU'
).AndReturn(self.presubmit_text)
self.mox.ReplayAll() self.mox.ReplayAll()
ci = presubmit.gcl.ChangeInfo(name='mychange', ci = presubmit.gcl.ChangeInfo(name='mychange',
description='\n'.join(description_lines), description='\n'.join(description_lines),
files=files) files=files)
output = StringIO.StringIO() output = StringIO.StringIO()
input = StringIO.StringIO() # should be unused input = StringIO.StringIO() # should be unused
self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input,
None)) None))
self.assertEqual(output.getvalue().count('??'), 2) self.assertEqual(output.getvalue().count('??'), 2)
...@@ -421,12 +375,11 @@ class PresubmitUnittest(PresubmitTestsBase): ...@@ -421,12 +375,11 @@ class PresubmitUnittest(PresubmitTestsBase):
self.assertEqual(output.getvalue().count('(y/N)'), 0) self.assertEqual(output.getvalue().count('(y/N)'), 0)
def testDoDefaultPresubmitChecks(self): def testDoDefaultPresubmitChecks(self):
join = presubmit.os.path.join
description_lines = ('Hello there', description_lines = ('Hello there',
'this is a change', 'this is a change',
'STORY=http://tracker/123') 'STORY=http://tracker/123')
files = [ files = [
['A', join('haspresubmit', 'blat.cc')], ['A', 'haspresubmit\\blat.cc'],
] ]
DEFAULT_SCRIPT = """ DEFAULT_SCRIPT = """
def CheckChangeOnUpload(input_api, output_api): def CheckChangeOnUpload(input_api, output_api):
...@@ -434,11 +387,9 @@ def CheckChangeOnUpload(input_api, output_api): ...@@ -434,11 +387,9 @@ def CheckChangeOnUpload(input_api, output_api):
def CheckChangeOnCommit(input_api, output_api): def CheckChangeOnCommit(input_api, output_api):
raise Exception("Test error") raise Exception("Test error")
""" """
presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') path = presubmit.os.path.join('haspresubmit', 'PRESUBMIT.py')
).AndReturn(False) presubmit.os.path.isfile(path).AndReturn(False)
presubmit.os.path.isfile(join(self.fake_root_dir, presubmit.os.path.isfile('PRESUBMIT.py').AndReturn(False)
'haspresubmit',
'PRESUBMIT.py')).AndReturn(False)
self.mox.ReplayAll() self.mox.ReplayAll()
ci = presubmit.gcl.ChangeInfo(name='mychange', ci = presubmit.gcl.ChangeInfo(name='mychange',
...@@ -497,7 +448,6 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -497,7 +448,6 @@ def CheckChangeOnCommit(input_api, output_api):
raise Exception("Test error") raise Exception("Test error")
""" """
self.mox.ReplayAll() self.mox.ReplayAll()
change = presubmit.gcl.ChangeInfo( change = presubmit.gcl.ChangeInfo(
name='foo', name='foo',
description="Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n") description="Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n")
...@@ -515,7 +465,6 @@ def CheckChangeOnCommit(input_api, output_api): ...@@ -515,7 +465,6 @@ def CheckChangeOnCommit(input_api, output_api):
class InputApiUnittest(PresubmitTestsBase): class InputApiUnittest(PresubmitTestsBase):
"""Tests presubmit.InputApi.""" """Tests presubmit.InputApi."""
def testMembersChanged(self): def testMembersChanged(self):
self.mox.ReplayAll()
members = [ members = [
'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles',
'DepotToLocalPath', 'LocalPaths', 'LocalToDepotPath', 'DepotToLocalPath', 'LocalPaths', 'LocalToDepotPath',
...@@ -532,7 +481,6 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -532,7 +481,6 @@ class InputApiUnittest(PresubmitTestsBase):
{'Path': 'prout'}) {'Path': 'prout'})
presubmit.gclient.CaptureSVNInfo('svn:/foo/notfound/burp').AndReturn({}) presubmit.gclient.CaptureSVNInfo('svn:/foo/notfound/burp').AndReturn({})
self.mox.ReplayAll() self.mox.ReplayAll()
path = presubmit.InputApi(None, './p').DepotToLocalPath('svn://foo/smurf') path = presubmit.InputApi(None, './p').DepotToLocalPath('svn://foo/smurf')
self.failUnless(path == 'prout') self.failUnless(path == 'prout')
path = presubmit.InputApi(None, './p').DepotToLocalPath( path = presubmit.InputApi(None, './p').DepotToLocalPath(
...@@ -543,40 +491,36 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -543,40 +491,36 @@ class InputApiUnittest(PresubmitTestsBase):
presubmit.gclient.CaptureSVNInfo('smurf').AndReturn({'URL': 'svn://foo'}) presubmit.gclient.CaptureSVNInfo('smurf').AndReturn({'URL': 'svn://foo'})
presubmit.gclient.CaptureSVNInfo('notfound-food').AndReturn({}) presubmit.gclient.CaptureSVNInfo('notfound-food').AndReturn({})
self.mox.ReplayAll() self.mox.ReplayAll()
path = presubmit.InputApi(None, './p').LocalToDepotPath('smurf') path = presubmit.InputApi(None, './p').LocalToDepotPath('smurf')
self.assertEqual(path, 'svn://foo') self.assertEqual(path, 'svn://foo')
path = presubmit.InputApi(None, './p').LocalToDepotPath('notfound-food') path = presubmit.InputApi(None, './p').LocalToDepotPath('notfound-food')
self.failUnless(path == None) self.failUnless(path == None)
def testInputApiConstruction(self): def testInputApiConstruction(self):
self.mox.ReplayAll()
# Fudge the change object, it's not used during construction anyway # Fudge the change object, it's not used during construction anyway
api = presubmit.InputApi(change=42, presubmit_path='foo/path/PRESUBMIT.py') api = presubmit.InputApi(change=42, presubmit_path='foo/path/PRESUBMIT.py')
self.assertEquals(api.PresubmitLocalPath(), 'foo/path') self.failUnless(api.PresubmitLocalPath() == 'foo/path')
self.assertEquals(api.change, 42) self.failUnless(api.change == 42)
def testInputApiPresubmitScriptFiltering(self): def testInputApiPresubmitScriptFiltering(self):
join = presubmit.os.path.join
description_lines = ('Hello there', description_lines = ('Hello there',
'this is a change', 'this is a change',
'BUG=123', 'BUG=123',
' STORY =http://foo/ \t', ' STORY =http://foo/ \t',
'and some more regular text') 'and some more regular text')
files = [ files = [
['A', join('foo', 'blat.cc')], ['A', presubmit.os.path.join('foo', 'blat.cc')],
['M', join('foo', 'blat', 'binary.dll')], ['M', presubmit.os.path.join('foo', 'blat', 'binary.dll')],
['D', 'foo/mat/beingdeleted.txt'], ['D', 'foo/mat/beingdeleted.txt'],
['M', 'flop/notfound.txt'], ['M', 'flop/notfound.txt'],
['A', 'boo/flap.h'], ['A', 'boo/flap.h'],
] ]
blat = presubmit.os.path.join('foo', 'blat.cc')
blat = join('foo', 'blat.cc') binary = presubmit.os.path.join('foo', 'blat', 'binary.dll')
binary = join('foo', 'blat', 'binary.dll') beingdeleted = presubmit.os.path.join('foo', 'mat', 'beingdeleted.txt')
beingdeleted = join('foo', 'mat', 'beingdeleted.txt') notfound = presubmit.os.path.join('flop', 'notfound.txt')
notfound = join('flop', 'notfound.txt') flap = presubmit.os.path.join('boo', 'flap.h')
flap = join('boo', 'flap.h')
presubmit.os.path.exists(blat).AndReturn(True) presubmit.os.path.exists(blat).AndReturn(True)
presubmit.os.path.isdir(blat).AndReturn(False) presubmit.os.path.isdir(blat).AndReturn(False)
presubmit.os.path.exists(binary).AndReturn(True) presubmit.os.path.exists(binary).AndReturn(True)
...@@ -597,7 +541,9 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -597,7 +541,9 @@ class InputApiUnittest(PresubmitTestsBase):
description='\n'.join(description_lines), description='\n'.join(description_lines),
files=files) files=files)
change = presubmit.GclChange(ci) change = presubmit.GclChange(ci)
api = presubmit.InputApi(change, 'foo/PRESUBMIT.py') api = presubmit.InputApi(change, 'foo/PRESUBMIT.py')
affected_files = api.AffectedFiles() affected_files = api.AffectedFiles()
self.assertEquals(len(affected_files), 3) self.assertEquals(len(affected_files), 3)
self.assertEquals(affected_files[0].LocalPath(), self.assertEquals(affected_files[0].LocalPath(),
...@@ -606,6 +552,7 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -606,6 +552,7 @@ class InputApiUnittest(PresubmitTestsBase):
presubmit.normpath('foo/blat/binary.dll')) presubmit.normpath('foo/blat/binary.dll'))
self.assertEquals(affected_files[2].LocalPath(), self.assertEquals(affected_files[2].LocalPath(),
presubmit.normpath('foo/mat/beingdeleted.txt')) presubmit.normpath('foo/mat/beingdeleted.txt'))
rhs_lines = [] rhs_lines = []
for line in api.RightHandSideLines(): for line in api.RightHandSideLines():
rhs_lines.append(line) rhs_lines.append(line)
...@@ -614,8 +561,6 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -614,8 +561,6 @@ class InputApiUnittest(PresubmitTestsBase):
presubmit.normpath('foo/blat.cc')) presubmit.normpath('foo/blat.cc'))
def testGetAbsoluteLocalPath(self): def testGetAbsoluteLocalPath(self):
join = presubmit.os.path.join
normpath = presubmit.normpath
# Regression test for bug of presubmit stuff that relies on invoking # Regression test for bug of presubmit stuff that relies on invoking
# SVN (e.g. to get mime type of file) not working unless gcl invoked # SVN (e.g. to get mime type of file) not working unless gcl invoked
# from the client root (e.g. if you were at 'src' and did 'cd base' before # from the client root (e.g. if you were at 'src' and did 'cd base' before
...@@ -623,37 +568,36 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -623,37 +568,36 @@ class InputApiUnittest(PresubmitTestsBase):
# the presubmit script was asking about). # the presubmit script was asking about).
files = [ files = [
['A', 'isdir'], ['A', 'isdir'],
['A', join('isdir', 'blat.cc')], ['A', presubmit.os.path.join('isdir', 'blat.cc')]
['M', join('elsewhere', 'ouf.cc')],
] ]
self.mox.ReplayAll() ci = presubmit.gcl.ChangeInfo(name='mychange',
description='',
ci = presubmit.gcl.ChangeInfo(name='mychange', description='', files=files) files=files)
# It doesn't make sense on non-Windows platform. This is somewhat hacky, # It doesn't make sense on non-Windows platform. This is somewhat hacky,
# but it is needed since we can't just use os.path.join('c:', 'temp'). # but it is needed since we can't just use os.path.join('c:', 'temp').
change = presubmit.GclChange(ci, self.fake_root_dir) change = presubmit.GclChange(ci, 'c:' + presubmit.os.sep + 'temp')
affected_files = change.AffectedFiles(include_dirs=True) affected_files = change.AffectedFiles(include_dirs=True)
# Local paths should remain the same # Local paths should remain the same
self.assertEquals(affected_files[0].LocalPath(), normpath('isdir')) self.failUnless(affected_files[0].LocalPath() ==
self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc')) presubmit.normpath('isdir'))
self.failUnless(affected_files[1].LocalPath() ==
presubmit.normpath('isdir/blat.cc'))
# Absolute paths should be prefixed # Absolute paths should be prefixed
self.assertEquals(affected_files[0].AbsoluteLocalPath(), self.failUnless(affected_files[0].AbsoluteLocalPath() ==
normpath(join(self.fake_root_dir, 'isdir'))) presubmit.normpath('c:/temp/isdir'))
self.assertEquals(affected_files[1].AbsoluteLocalPath(), self.failUnless(affected_files[1].AbsoluteLocalPath() ==
normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) presubmit.normpath('c:/temp/isdir/blat.cc'))
# New helper functions need to work # New helper functions need to work
paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) absolute_paths_from_change = change.AbsoluteLocalPaths(include_dirs=True)
self.assertEqual(len(paths_from_change), 3) api = presubmit.InputApi(change=change,
presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') presubmit_path='isdir/PRESUBMIT.py')
api = presubmit.InputApi(change=change, presubmit_path=presubmit_path) absolute_paths_from_api = api.AbsoluteLocalPaths(include_dirs=True)
paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) for absolute_paths in [absolute_paths_from_change,
self.assertEqual(len(paths_from_api), 2) absolute_paths_from_api]:
for absolute_paths in [paths_from_change, paths_from_api]: self.failUnless(absolute_paths[0] == presubmit.normpath('c:/temp/isdir'))
self.assertEqual(absolute_paths[0], self.failUnless(absolute_paths[1] ==
normpath(join(self.fake_root_dir, 'isdir'))) presubmit.normpath('c:/temp/isdir/blat.cc'))
self.assertEqual(absolute_paths[1],
normpath(join(self.fake_root_dir, 'isdir', 'blat.cc')))
def testDeprecated(self): def testDeprecated(self):
presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), presubmit.warnings.warn(mox.IgnoreArg(), category=mox.IgnoreArg(),
...@@ -668,7 +612,6 @@ class InputApiUnittest(PresubmitTestsBase): ...@@ -668,7 +612,6 @@ class InputApiUnittest(PresubmitTestsBase):
class OuputApiUnittest(PresubmitTestsBase): class OuputApiUnittest(PresubmitTestsBase):
"""Tests presubmit.OutputApi.""" """Tests presubmit.OutputApi."""
def testMembersChanged(self): def testMembersChanged(self):
self.mox.ReplayAll()
members = [ members = [
'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult', 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult',
'PresubmitPromptWarning', 'PresubmitResult', 'PresubmitPromptWarning', 'PresubmitResult',
...@@ -677,7 +620,6 @@ class OuputApiUnittest(PresubmitTestsBase): ...@@ -677,7 +620,6 @@ class OuputApiUnittest(PresubmitTestsBase):
self.compareMembers(presubmit.OutputApi(), members) self.compareMembers(presubmit.OutputApi(), members)
def testOutputApiBasics(self): def testOutputApiBasics(self):
self.mox.ReplayAll()
self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal())
self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt())
...@@ -691,7 +633,6 @@ class OuputApiUnittest(PresubmitTestsBase): ...@@ -691,7 +633,6 @@ class OuputApiUnittest(PresubmitTestsBase):
# TODO(joi) Test MailTextResult once implemented. # TODO(joi) Test MailTextResult once implemented.
def testOutputApiHandling(self): def testOutputApiHandling(self):
self.mox.ReplayAll()
output = StringIO.StringIO() output = StringIO.StringIO()
unused_input = StringIO.StringIO() unused_input = StringIO.StringIO()
error = presubmit.OutputApi.PresubmitError('!!!') error = presubmit.OutputApi.PresubmitError('!!!')
...@@ -724,7 +665,6 @@ class OuputApiUnittest(PresubmitTestsBase): ...@@ -724,7 +665,6 @@ class OuputApiUnittest(PresubmitTestsBase):
class AffectedFileUnittest(PresubmitTestsBase): class AffectedFileUnittest(PresubmitTestsBase):
def testMembersChanged(self): def testMembersChanged(self):
self.mox.ReplayAll()
members = [ members = [
'AbsoluteLocalPath', 'Action', 'IsDirectory', 'IsTextFile', 'LocalPath', 'AbsoluteLocalPath', 'Action', 'IsDirectory', 'IsTextFile', 'LocalPath',
'NewContents', 'OldContents', 'OldFileTempPath', 'Property', 'ServerPath', 'NewContents', 'OldContents', 'OldFileTempPath', 'Property', 'ServerPath',
...@@ -823,7 +763,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -823,7 +763,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
yield (presubmit.AffectedFile('bingo', 'M'), 1, line) yield (presubmit.AffectedFile('bingo', 'M'), 1, line)
def testMembersChanged(self): def testMembersChanged(self):
self.mox.ReplayAll()
members = [ members = [
'CheckChangeHasBugField', 'CheckChangeHasNoTabs', 'CheckChangeHasBugField', 'CheckChangeHasNoTabs',
'CheckChangeHasQaField', 'CheckChangeHasTestedField', 'CheckChangeHasQaField', 'CheckChangeHasTestedField',
...@@ -835,7 +774,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -835,7 +774,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
self.compareMembers(presubmit_canned_checks, members) self.compareMembers(presubmit_canned_checks, members)
def testCannedCheckChangeHasBugField(self): def testCannedCheckChangeHasBugField(self):
self.mox.ReplayAll()
change = self.MakeBasicChange('foo', change = self.MakeBasicChange('foo',
'Foo\nBUG=1234') 'Foo\nBUG=1234')
api = presubmit.InputApi(change, './PRESUBMIT.py') api = presubmit.InputApi(change, './PRESUBMIT.py')
...@@ -849,7 +787,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -849,7 +787,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
api, presubmit.OutputApi)) api, presubmit.OutputApi))
def testCannedCheckChangeHasTestField(self): def testCannedCheckChangeHasTestField(self):
self.mox.ReplayAll()
change = self.MakeBasicChange('foo', change = self.MakeBasicChange('foo',
'Foo\nTEST=did some stuff') 'Foo\nTEST=did some stuff')
api = presubmit.InputApi(change, './PRESUBMIT.py') api = presubmit.InputApi(change, './PRESUBMIT.py')
...@@ -863,7 +800,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -863,7 +800,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
api, presubmit.OutputApi)) api, presubmit.OutputApi))
def testCannedCheckChangeHasTestedField(self): def testCannedCheckChangeHasTestedField(self):
self.mox.ReplayAll()
change = self.MakeBasicChange('foo', change = self.MakeBasicChange('foo',
'Foo\nTESTED=did some stuff') 'Foo\nTESTED=did some stuff')
api = presubmit.InputApi(change, './PRESUBMIT.py') api = presubmit.InputApi(change, './PRESUBMIT.py')
...@@ -877,7 +813,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -877,7 +813,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
api, presubmit.OutputApi)) api, presubmit.OutputApi))
def testCannedCheckChangeHasQAField(self): def testCannedCheckChangeHasQAField(self):
self.mox.ReplayAll()
change = self.MakeBasicChange('foo', change = self.MakeBasicChange('foo',
'Foo\nQA=test floop feature very well') 'Foo\nQA=test floop feature very well')
api = presubmit.InputApi(change, './PRESUBMIT.py') api = presubmit.InputApi(change, './PRESUBMIT.py')
...@@ -891,7 +826,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -891,7 +826,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
api, presubmit.OutputApi)) api, presubmit.OutputApi))
def testCannedCheckDoNotSubmitInDescription(self): def testCannedCheckDoNotSubmitInDescription(self):
self.mox.ReplayAll()
change = self.MakeBasicChange('foo', 'hello') change = self.MakeBasicChange('foo', 'hello')
api = presubmit.InputApi(change, './PRESUBMIT.py') api = presubmit.InputApi(change, './PRESUBMIT.py')
self.failIf(presubmit_canned_checks.CheckDoNotSubmitInDescription( self.failIf(presubmit_canned_checks.CheckDoNotSubmitInDescription(
...@@ -904,7 +838,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -904,7 +838,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
api, presubmit.OutputApi)) api, presubmit.OutputApi))
def testCannedCheckDoNotSubmitInFiles(self): def testCannedCheckDoNotSubmitInFiles(self):
self.mox.ReplayAll()
self.failIf(presubmit_canned_checks.CheckDoNotSubmitInFiles( self.failIf(presubmit_canned_checks.CheckDoNotSubmitInFiles(
self.MockInputApi(['hello', 'there']), presubmit.OutputApi self.MockInputApi(['hello', 'there']), presubmit.OutputApi
)) ))
...@@ -913,7 +846,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -913,7 +846,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
presubmit.OutputApi)) presubmit.OutputApi))
def testCannedCheckChangeHasNoTabs(self): def testCannedCheckChangeHasNoTabs(self):
self.mox.ReplayAll()
self.failIf(presubmit_canned_checks.CheckChangeHasNoTabs( self.failIf(presubmit_canned_checks.CheckChangeHasNoTabs(
self.MockInputApi(['hello', 'there']), presubmit.OutputApi self.MockInputApi(['hello', 'there']), presubmit.OutputApi
)) ))
...@@ -922,7 +854,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -922,7 +854,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
)) ))
def testCannedCheckLongLines(self): def testCannedCheckLongLines(self):
self.mox.ReplayAll()
self.failIf(presubmit_canned_checks.CheckLongLines( self.failIf(presubmit_canned_checks.CheckLongLines(
self.MockInputApi(['hello', 'there']), presubmit.OutputApi, 5 self.MockInputApi(['hello', 'there']), presubmit.OutputApi, 5
)) ))
...@@ -931,7 +862,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -931,7 +862,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
)) ))
def testCannedCheckTreeIsOpen(self): def testCannedCheckTreeIsOpen(self):
self.mox.ReplayAll()
self.failIf(presubmit_canned_checks.CheckTreeIsOpen( self.failIf(presubmit_canned_checks.CheckTreeIsOpen(
self.MockInputApi(), presubmit.OutputApi, url='url_to_open', closed='0' self.MockInputApi(), presubmit.OutputApi, url='url_to_open', closed='0'
)) ))
...@@ -940,7 +870,6 @@ class CannedChecksUnittest(PresubmitTestsBase): ...@@ -940,7 +870,6 @@ class CannedChecksUnittest(PresubmitTestsBase):
)) ))
def RunPythonUnitTests(self): def RunPythonUnitTests(self):
self.mox.ReplayAll()
# TODO(maruel): Add real tests. # TODO(maruel): Add real tests.
self.failIf(presubmit_canned_checks.RunPythonUnitTests( self.failIf(presubmit_canned_checks.RunPythonUnitTests(
self.MockInputApi(), self.MockInputApi(),
......
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