Commit c4b5e76d authored by maruel@chromium.org's avatar maruel@chromium.org

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
parent 512f1ef7
......@@ -246,8 +246,14 @@ class FilePatchDiff(FilePatchBase):
match = re.match(r'^--- ([^\t]+).*$', lines.pop(0))
if not match:
continue
if match.group(1) not in (self.filename, '/dev/null'):
self._fail('Unexpected diff: %s.' % match.group(1))
# For copy and renames, it's possible that the -- line doesn't match +++,
# 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))
if not match:
self._fail('Unexpected diff: --- not following +++.')
......
......@@ -253,24 +253,6 @@ class PatchTest(unittest.TestCase):
except patch.UnsupportedPatchFormat:
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):
# pylint: disable=R0201
# Method could be a function
......@@ -365,6 +347,52 @@ class PatchTest(unittest.TestCase):
patch.FilePatchDiff('foo', GIT_NEW, [])
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__':
unittest.main()
......@@ -21,24 +21,16 @@ import rietveld
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):
rietveld.Rietveld._send = lambda x, y, payload: '{}'
r = rietveld.Rietveld('url', 'email', 'password')
r._send = lambda *args, **kwargs: '{}'
patches = r.get_patch(123, 456)
self.assertTrue(isinstance(patches, patch.PatchSet))
self.assertEquals([], patches.patches)
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":'
' {'
......@@ -47,7 +39,6 @@ class RietveldTest(unittest.TestCase):
' }'
' }'
'}')
r = rietveld.Rietveld('url', 'email', 'password')
try:
r.get_patch(123, 456)
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._send = lambda *args, **kwargs: output
patches = r.get_patch(123, 456)
self.assertTrue(isinstance(patches, patch.PatchSet))
self.assertEquals(1, len(patches.patches))
......
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