patchutils.py: Add tests for read_patch function.

This commit is contained in:
Sebastian Lackner 2016-04-02 21:06:44 +02:00
parent 8bed2e61a6
commit d3a4c072f7

View File

@ -85,6 +85,10 @@ class PatchObject(object):
yield buf
i -= len(buf)
def read(self):
"""Return the full patch as a string."""
return "".join(chunk for chunk in self.read_chunks())
class _FileReader(object):
def __init__(self, filename, content=None):
self.filename = filename
@ -728,6 +732,114 @@ if __name__ == "__main__":
subject = _parse_subject("[PATCH] component: Subject (resend).")
self.assertEqual(subject, ("component: Subject", 1))
# Basic tests for read_patch()
class PatchReaderTests(unittest.TestCase):
def test_read(self):
source = ["From be07df750862699f2515c0ac0ceb7a6c25e9458a Mon Sep 17 00:00:00 2001",
"From: =?UTF-8?q?Author=20Name?= <author@email.com>",
"Subject: [PATCH v3] component: Replace arg1 with arg2.",
"",
"Signed-off-by: =?UTF-8?q?Author=20Name?= <author@email.com>",
"Signed-off-by: Other Developer <other@email.com>",
"---",
" test.txt | 2 +-",
" 1 file changed, 1 insertion(+), 1 deletion(-)",
"",
"diff --git a/test.txt b/test.txt",
"index d54375d..0078e66 100644",
"--- a/test.txt", "+++ b/test.txt",
"@@ -1,7 +1,7 @@",
" line1();", " line2();", " line3();",
"-function(arg1);",
"+function(arg2);",
" line5();", " line6();", " line7();",
"-- ",
"2.7.1"]
expected = ["diff --git a/test.txt b/test.txt",
"index d54375d..0078e66 100644",
"--- a/test.txt", "+++ b/test.txt",
"@@ -1,7 +1,7 @@",
" line1();", " line2();", " line3();",
"-function(arg1);",
"+function(arg2);",
" line5();", " line6();", " line7();"]
# Test formatted git patch with author and subject
patchfile = tempfile.NamedTemporaryFile()
patchfile.write("\n".join(source + [""]))
patchfile.flush()
patches = list(read_patch(patchfile.name))
self.assertEqual(len(patches), 1)
self.assertEqual(patches[0].patch_author, "Author Name")
self.assertEqual(patches[0].patch_email, "author@email.com")
self.assertEqual(patches[0].patch_subject, "component: Replace arg1 with arg2.")
self.assertEqual(patches[0].patch_revision, 3)
self.assertEqual(patches[0].signed_off_by, [("Author Name", "author@email.com"),
("Other Developer", "other@email.com")])
self.assertEqual(patches[0].filename, patchfile.name)
self.assertEqual(patches[0].isbinary, False)
self.assertEqual(patches[0].modified_file, "test.txt")
lines = patches[0].read().rstrip("\n").split("\n")
self.assertEqual(lines, expected)
# Test with git diff
del source[0:10]
self.assertTrue(source[0].startswith("diff --git"))
patchfile = tempfile.NamedTemporaryFile()
patchfile.write("\n".join(source + [""]))
patchfile.flush()
patches = list(read_patch(patchfile.name))
self.assertEqual(len(patches), 1)
self.assertEqual(patches[0].patch_author, None)
self.assertEqual(patches[0].patch_email, None)
self.assertEqual(patches[0].patch_subject, None)
self.assertEqual(patches[0].patch_revision, 1)
self.assertEqual(patches[0].signed_off_by, [])
self.assertEqual(patches[0].filename, patchfile.name)
self.assertEqual(patches[0].isbinary, False)
self.assertEqual(patches[0].modified_file, "test.txt")
lines = patches[0].read().rstrip("\n").split("\n")
self.assertEqual(lines, expected)
# Test with unified diff
del source[0:2]
self.assertTrue(source[0].startswith("---"))
patchfile = tempfile.NamedTemporaryFile()
patchfile.write("\n".join(source + [""]))
patchfile.flush()
patches = list(read_patch(patchfile.name))
self.assertEqual(len(patches), 1)
self.assertEqual(patches[0].patch_author, None)
self.assertEqual(patches[0].patch_email, None)
self.assertEqual(patches[0].patch_subject, None)
self.assertEqual(patches[0].patch_revision, 1)
self.assertEqual(patches[0].signed_off_by, [])
self.assertEqual(patches[0].filename, patchfile.name)
self.assertEqual(patches[0].isbinary, False)
self.assertEqual(patches[0].modified_file, "test.txt")
del expected[0:2]
lines = patches[0].read().rstrip("\n").split("\n")
self.assertEqual(lines, expected)
# Test with StringIO buffer
patches = list(read_patch("unknown.patch", "\n".join(source + [""])))
self.assertEqual(len(patches), 1)
self.assertEqual(patches[0].patch_author, None)
self.assertEqual(patches[0].patch_email, None)
self.assertEqual(patches[0].patch_subject, None)
self.assertEqual(patches[0].patch_revision, 1)
self.assertEqual(patches[0].signed_off_by, [])
self.assertEqual(patches[0].filename, "unknown.patch")
self.assertEqual(patches[0].isbinary, False)
self.assertEqual(patches[0].modified_file, "test.txt")
# Basic tests for apply_patch()
class PatchApplyTests(unittest.TestCase):
def test_apply(self):