From c4b5e76d7eb3794d9fc54ff6eea53a576284504f Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Wed, 20 Apr 2011 23:56:08 +0000 Subject: [PATCH] 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 --- patch.py | 10 +++++-- tests/patch_test.py | 64 ++++++++++++++++++++++++++++++------------ tests/rietveld_test.py | 17 +++-------- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/patch.py b/patch.py index f6b19aca2..316333f29 100644 --- a/patch.py +++ b/patch.py @@ -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 +++.') diff --git a/tests/patch_test.py b/tests/patch_test.py index b76c693d0..945d5d5e0 100755 --- a/tests/patch_test.py +++ b/tests/patch_test.py @@ -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() diff --git a/tests/rietveld_test.py b/tests/rietveld_test.py index 6a32f64e4..6c81454ac 100755 --- a/tests/rietveld_test.py +++ b/tests/rietveld_test.py @@ -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))