From d3a4c072f72389eead011fef4b4c2917438cdb45 Mon Sep 17 00:00:00 2001 From: Sebastian Lackner Date: Sat, 2 Apr 2016 21:06:44 +0200 Subject: [PATCH] patchutils.py: Add tests for read_patch function. --- staging/patchutils.py | 112 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/staging/patchutils.py b/staging/patchutils.py index d79063e7..96553cc2 100644 --- a/staging/patchutils.py +++ b/staging/patchutils.py @@ -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?= ", + "Subject: [PATCH v3] component: Replace arg1 with arg2.", + "", + "Signed-off-by: =?UTF-8?q?Author=20Name?= ", + "Signed-off-by: Other Developer ", + "---", + " 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):