Improve patch handling and tests.

R=dpranke@chromium.org
BUG=
TEST=

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@82406 0039d316-1c4b-4281-b951-d872f2087c98
experimental/szager/collated-output
maruel@chromium.org 14 years ago
parent 512f1ef7fd
commit c4b5e76d7e

@ -246,8 +246,14 @@ class FilePatchDiff(FilePatchBase):
match = re.match(r'^--- ([^\t]+).*$', lines.pop(0)) match = re.match(r'^--- ([^\t]+).*$', lines.pop(0))
if not match: if not match:
continue continue
if match.group(1) not in (self.filename, '/dev/null'): # For copy and renames, it's possible that the -- line doesn't match +++,
self._fail('Unexpected diff: %s.' % match.group(1)) # so don't check match.group(1) to match self.filename or '/dev/null', it
# can be anything else.
# TODO(maruel): Handle rename/copy explicitly.
# if match.group(1) not in (self.filename, '/dev/null'):
# self.source_file = match.group(1)
if not lines:
self._fail('Nothing after header.')
match = re.match(r'^\+\+\+ ([^\t]+).*$', lines.pop(0)) match = re.match(r'^\+\+\+ ([^\t]+).*$', lines.pop(0))
if not match: if not match:
self._fail('Unexpected diff: --- not following +++.') self._fail('Unexpected diff: --- not following +++.')

@ -253,24 +253,6 @@ class PatchTest(unittest.TestCase):
except patch.UnsupportedPatchFormat: except patch.UnsupportedPatchFormat:
pass pass
def testInvalidFilePatchDiffSvn(self):
try:
patch.FilePatchDiff('svn_utils_test.txt', (
'--- svn_utils_test.txt2\n'
'+++ svn_utils_test.txt\n'
'@@ -3,6 +3,7 @@ bb\n'
'ccc\n'
'dd\n'
'e\n'
'+FOO!\n'
'ff\n'
'ggg\n'
'hh\n'),
[])
self.fail()
except patch.UnsupportedPatchFormat:
pass
def testValidSvn(self): def testValidSvn(self):
# pylint: disable=R0201 # pylint: disable=R0201
# Method could be a function # Method could be a function
@ -365,6 +347,52 @@ class PatchTest(unittest.TestCase):
patch.FilePatchDiff('foo', GIT_NEW, []) patch.FilePatchDiff('foo', GIT_NEW, [])
self.assertTrue(True) self.assertTrue(True)
def testOnlyHeader(self):
p = patch.FilePatchDiff('file_a', '--- file_a\n+++ file_a\n', [])
self.assertTrue(p)
def testSmallest(self):
p = patch.FilePatchDiff(
'file_a', '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n', [])
self.assertTrue(p)
def testInverted(self):
try:
patch.FilePatchDiff(
'file_a', '+++ file_a\n--- file_a\n@@ -0,0 +1 @@\n+foo\n', [])
self.fail()
except patch.UnsupportedPatchFormat:
pass
def testInvertedOnlyHeader(self):
try:
patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', [])
self.fail()
except patch.UnsupportedPatchFormat:
pass
def testRenameOnlyHeader(self):
p = patch.FilePatchDiff('file_b', '--- file_a\n+++ file_b\n', [])
self.assertTrue(p)
def testGitCopy(self):
diff = (
'diff --git a/wtf b/wtf2\n'
'similarity index 98%\n'
'copy from wtf\n'
'copy to wtf2\n'
'index 79fbaf3..3560689 100755\n'
'--- a/wtf\n'
'+++ b/wtf2\n'
'@@ -1,4 +1,4 @@\n'
'-#!/usr/bin/env python\n'
'+#!/usr/bin/env python1.3\n'
' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n'
' # blah blah blah as\n'
' # found in the LICENSE file.\n')
p = patch.FilePatchDiff('wtf2', diff, [])
self.assertTrue(p)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

@ -21,24 +21,16 @@ import rietveld
class RietveldTest(unittest.TestCase): class RietveldTest(unittest.TestCase):
def setUp(self):
super(RietveldTest, self).setUp()
self._rietveld_send = rietveld.Rietveld._send
rietveld.Rietveld._send = None
def tearDown(self):
super(RietveldTest, self).setUp()
rietveld.Rietveld._send = self._rietveld_send
def test_get_patch_empty(self): def test_get_patch_empty(self):
rietveld.Rietveld._send = lambda x, y, payload: '{}'
r = rietveld.Rietveld('url', 'email', 'password') r = rietveld.Rietveld('url', 'email', 'password')
r._send = lambda *args, **kwargs: '{}'
patches = r.get_patch(123, 456) patches = r.get_patch(123, 456)
self.assertTrue(isinstance(patches, patch.PatchSet)) self.assertTrue(isinstance(patches, patch.PatchSet))
self.assertEquals([], patches.patches) self.assertEquals([], patches.patches)
def test_get_patch_no_status(self): def test_get_patch_no_status(self):
rietveld.Rietveld._send = lambda x, y, payload: ( r = rietveld.Rietveld('url', 'email', 'password')
r._send = lambda *args, **kwargs: (
'{' '{'
' "files":' ' "files":'
' {' ' {'
@ -47,7 +39,6 @@ class RietveldTest(unittest.TestCase):
' }' ' }'
' }' ' }'
'}') '}')
r = rietveld.Rietveld('url', 'email', 'password')
try: try:
r.get_patch(123, 456) r.get_patch(123, 456)
self.fail() self.fail()
@ -68,8 +59,8 @@ class RietveldTest(unittest.TestCase):
' }' ' }'
' }' ' }'
'}') '}')
rietveld.Rietveld._send = lambda x, y, payload: output
r = rietveld.Rietveld('url', 'email', 'password') r = rietveld.Rietveld('url', 'email', 'password')
r._send = lambda *args, **kwargs: output
patches = r.get_patch(123, 456) patches = r.get_patch(123, 456)
self.assertTrue(isinstance(patches, patch.PatchSet)) self.assertTrue(isinstance(patches, patch.PatchSet))
self.assertEquals(1, len(patches.patches)) self.assertEquals(1, len(patches.patches))

Loading…
Cancel
Save