test_patch.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # test_patch.py -- tests for patch.py
  2. # Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for patch.py."""
  19. from cStringIO import StringIO
  20. from dulwich.objects import (
  21. Commit,
  22. Tree,
  23. )
  24. from dulwich.patch import (
  25. git_am_patch_split,
  26. write_commit_patch,
  27. )
  28. from dulwich.tests import (
  29. TestCase,
  30. )
  31. class WriteCommitPatchTests(TestCase):
  32. def test_simple(self):
  33. f = StringIO()
  34. c = Commit()
  35. c.committer = c.author = "Jelmer <jelmer@samba.org>"
  36. c.commit_time = c.author_time = 1271350201
  37. c.commit_timezone = c.author_timezone = 0
  38. c.message = "This is the first line\nAnd this is the second line.\n"
  39. c.tree = Tree().id
  40. write_commit_patch(f, c, "CONTENTS", (1, 1), version="custom")
  41. f.seek(0)
  42. lines = f.readlines()
  43. self.assertTrue(lines[0].startswith("From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
  44. self.assertEquals(lines[1], "From: Jelmer <jelmer@samba.org>\n")
  45. self.assertTrue(lines[2].startswith("Date: "))
  46. self.assertEquals([
  47. "Subject: [PATCH 1/1] This is the first line\n",
  48. "And this is the second line.\n",
  49. "\n",
  50. "\n",
  51. "---\n"], lines[3:8])
  52. self.assertEquals([
  53. "CONTENTS-- \n",
  54. "custom\n"], lines[-2:])
  55. if len(lines) >= 12:
  56. # diffstat may not be present
  57. self.assertEquals(lines[8], " 0 files changed\n")
  58. class ReadGitAmPatch(TestCase):
  59. def test_extract(self):
  60. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  61. From: Jelmer Vernooij <jelmer@samba.org>
  62. Date: Thu, 15 Apr 2010 15:40:28 +0200
  63. Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a lintian warning).
  64. ---
  65. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  66. 1 files changed, 0 insertions(+), 0 deletions(-)
  67. mode change 100755 => 100644 pixmaps/prey.ico
  68. --
  69. 1.7.0.4
  70. """
  71. c, diff, version = git_am_patch_split(StringIO(text))
  72. self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.committer)
  73. self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.author)
  74. self.assertEquals("Remove executable bit from prey.ico "
  75. "(triggers a lintian warning).\n", c.message)
  76. self.assertEquals(""" pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  77. 1 files changed, 0 insertions(+), 0 deletions(-)
  78. mode change 100755 => 100644 pixmaps/prey.ico
  79. """, diff)
  80. self.assertEquals("1.7.0.4", version)
  81. def test_extract_spaces(self):
  82. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  83. From: Jelmer Vernooij <jelmer@samba.org>
  84. Date: Thu, 15 Apr 2010 15:40:28 +0200
  85. Subject: [Dulwich-users] [PATCH] Added unit tests for
  86. dulwich.object_store.tree_lookup_path.
  87. * dulwich/tests/test_object_store.py
  88. (TreeLookupPathTests): This test case contains a few tests that ensure the
  89. tree_lookup_path function works as expected.
  90. ---
  91. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  92. 1 files changed, 0 insertions(+), 0 deletions(-)
  93. mode change 100755 => 100644 pixmaps/prey.ico
  94. --
  95. 1.7.0.4
  96. """
  97. c, diff, version = git_am_patch_split(StringIO(text))
  98. self.assertEquals('Added unit tests for dulwich.object_store.tree_lookup_path.\n\n* dulwich/tests/test_object_store.py\n (TreeLookupPathTests): This test case contains a few tests that ensure the\n tree_lookup_path function works as expected.\n', c.message)
  99. def test_extract_pseudo_from_header(self):
  100. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  101. From: Jelmer Vernooij <jelmer@samba.org>
  102. Date: Thu, 15 Apr 2010 15:40:28 +0200
  103. Subject: [Dulwich-users] [PATCH] Added unit tests for
  104. dulwich.object_store.tree_lookup_path.
  105. From: Jelmer Vernooy <jelmer@debian.org>
  106. * dulwich/tests/test_object_store.py
  107. (TreeLookupPathTests): This test case contains a few tests that ensure the
  108. tree_lookup_path function works as expected.
  109. ---
  110. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  111. 1 files changed, 0 insertions(+), 0 deletions(-)
  112. mode change 100755 => 100644 pixmaps/prey.ico
  113. --
  114. 1.7.0.4
  115. """
  116. c, diff, version = git_am_patch_split(StringIO(text))
  117. self.assertEquals("Jelmer Vernooy <jelmer@debian.org>", c.author)
  118. self.assertEquals('Added unit tests for dulwich.object_store.tree_lookup_path.\n\n* dulwich/tests/test_object_store.py\n (TreeLookupPathTests): This test case contains a few tests that ensure the\n tree_lookup_path function works as expected.\n', c.message)