test_patch.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. Blob,
  22. Commit,
  23. Tree,
  24. )
  25. from dulwich.object_store import (
  26. MemoryObjectStore,
  27. )
  28. from dulwich.patch import (
  29. git_am_patch_split,
  30. write_blob_diff,
  31. write_commit_patch,
  32. write_tree_diff,
  33. )
  34. from dulwich.tests import (
  35. TestCase,
  36. )
  37. class WriteCommitPatchTests(TestCase):
  38. def test_simple(self):
  39. f = StringIO()
  40. c = Commit()
  41. c.committer = c.author = "Jelmer <jelmer@samba.org>"
  42. c.commit_time = c.author_time = 1271350201
  43. c.commit_timezone = c.author_timezone = 0
  44. c.message = "This is the first line\nAnd this is the second line.\n"
  45. c.tree = Tree().id
  46. write_commit_patch(f, c, "CONTENTS", (1, 1), version="custom")
  47. f.seek(0)
  48. lines = f.readlines()
  49. self.assertTrue(lines[0].startswith("From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
  50. self.assertEquals(lines[1], "From: Jelmer <jelmer@samba.org>\n")
  51. self.assertTrue(lines[2].startswith("Date: "))
  52. self.assertEquals([
  53. "Subject: [PATCH 1/1] This is the first line\n",
  54. "And this is the second line.\n",
  55. "\n",
  56. "\n",
  57. "---\n"], lines[3:8])
  58. self.assertEquals([
  59. "CONTENTS-- \n",
  60. "custom\n"], lines[-2:])
  61. if len(lines) >= 12:
  62. # diffstat may not be present
  63. self.assertEquals(lines[8], " 0 files changed\n")
  64. class ReadGitAmPatch(TestCase):
  65. def test_extract(self):
  66. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  67. From: Jelmer Vernooij <jelmer@samba.org>
  68. Date: Thu, 15 Apr 2010 15:40:28 +0200
  69. Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a lintian warning).
  70. ---
  71. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  72. 1 files changed, 0 insertions(+), 0 deletions(-)
  73. mode change 100755 => 100644 pixmaps/prey.ico
  74. --
  75. 1.7.0.4
  76. """
  77. c, diff, version = git_am_patch_split(StringIO(text))
  78. self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.committer)
  79. self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.author)
  80. self.assertEquals("Remove executable bit from prey.ico "
  81. "(triggers a lintian warning).\n", c.message)
  82. self.assertEquals(""" pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  83. 1 files changed, 0 insertions(+), 0 deletions(-)
  84. mode change 100755 => 100644 pixmaps/prey.ico
  85. """, diff)
  86. self.assertEquals("1.7.0.4", version)
  87. def test_extract_spaces(self):
  88. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  89. From: Jelmer Vernooij <jelmer@samba.org>
  90. Date: Thu, 15 Apr 2010 15:40:28 +0200
  91. Subject: [Dulwich-users] [PATCH] Added unit tests for
  92. dulwich.object_store.tree_lookup_path.
  93. * dulwich/tests/test_object_store.py
  94. (TreeLookupPathTests): This test case contains a few tests that ensure the
  95. tree_lookup_path function works as expected.
  96. ---
  97. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  98. 1 files changed, 0 insertions(+), 0 deletions(-)
  99. mode change 100755 => 100644 pixmaps/prey.ico
  100. --
  101. 1.7.0.4
  102. """
  103. c, diff, version = git_am_patch_split(StringIO(text))
  104. 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)
  105. def test_extract_pseudo_from_header(self):
  106. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  107. From: Jelmer Vernooij <jelmer@samba.org>
  108. Date: Thu, 15 Apr 2010 15:40:28 +0200
  109. Subject: [Dulwich-users] [PATCH] Added unit tests for
  110. dulwich.object_store.tree_lookup_path.
  111. From: Jelmer Vernooy <jelmer@debian.org>
  112. * dulwich/tests/test_object_store.py
  113. (TreeLookupPathTests): This test case contains a few tests that ensure the
  114. tree_lookup_path function works as expected.
  115. ---
  116. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  117. 1 files changed, 0 insertions(+), 0 deletions(-)
  118. mode change 100755 => 100644 pixmaps/prey.ico
  119. --
  120. 1.7.0.4
  121. """
  122. c, diff, version = git_am_patch_split(StringIO(text))
  123. self.assertEquals("Jelmer Vernooy <jelmer@debian.org>", c.author)
  124. 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)
  125. def test_extract_no_version_tail(self):
  126. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  127. From: Jelmer Vernooij <jelmer@samba.org>
  128. Date: Thu, 15 Apr 2010 15:40:28 +0200
  129. Subject: [Dulwich-users] [PATCH] Added unit tests for
  130. dulwich.object_store.tree_lookup_path.
  131. From: Jelmer Vernooy <jelmer@debian.org>
  132. ---
  133. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  134. 1 files changed, 0 insertions(+), 0 deletions(-)
  135. mode change 100755 => 100644 pixmaps/prey.ico
  136. """
  137. c, diff, version = git_am_patch_split(StringIO(text))
  138. self.assertIs(None, version)
  139. class DiffTests(TestCase):
  140. """Tests for write_blob_diff and write_tree_diff."""
  141. def test_blob_diff(self):
  142. f = StringIO()
  143. write_blob_diff(f, ("foo.txt", 0644, Blob.from_string("old\nsame\n")),
  144. ("bar.txt", 0644, Blob.from_string("new\nsame\n")))
  145. self.assertEquals([
  146. "diff --git a/foo.txt b/bar.txt",
  147. "index 3b0f961..a116b51 644",
  148. "--- a/foo.txt",
  149. "+++ b/bar.txt",
  150. "@@ -1,2 +1,2 @@",
  151. "-old",
  152. "+new",
  153. " same"
  154. ], f.getvalue().splitlines())
  155. def test_blob_add(self):
  156. f = StringIO()
  157. write_blob_diff(f, (None, None, None),
  158. ("bar.txt", 0644, Blob.from_string("new\nsame\n")))
  159. self.assertEquals([
  160. 'diff --git /dev/null b/bar.txt',
  161. 'new mode 644',
  162. 'index 0000000..a116b51 644',
  163. '--- /dev/null',
  164. '+++ b/bar.txt',
  165. '@@ -1,0 +1,2 @@',
  166. '+new',
  167. '+same'
  168. ], f.getvalue().splitlines())
  169. def test_blob_remove(self):
  170. f = StringIO()
  171. write_blob_diff(f, ("bar.txt", 0644, Blob.from_string("new\nsame\n")),
  172. (None, None, None))
  173. self.assertEquals([
  174. 'diff --git a/bar.txt /dev/null',
  175. 'deleted mode 644',
  176. 'index a116b51..0000000',
  177. '--- a/bar.txt',
  178. '+++ /dev/null',
  179. '@@ -1,2 +1,0 @@',
  180. '-new',
  181. '-same'
  182. ], f.getvalue().splitlines())
  183. def test_tree_diff(self):
  184. f = StringIO()
  185. store = MemoryObjectStore()
  186. added = Blob.from_string("add\n")
  187. removed = Blob.from_string("removed\n")
  188. changed1 = Blob.from_string("unchanged\nremoved\n")
  189. changed2 = Blob.from_string("unchanged\nadded\n")
  190. unchanged = Blob.from_string("unchanged\n")
  191. tree1 = Tree()
  192. tree1.add(0644, "removed.txt", removed.id)
  193. tree1.add(0644, "changed.txt", changed1.id)
  194. tree1.add(0644, "unchanged.txt", changed1.id)
  195. tree2 = Tree()
  196. tree2.add(0644, "added.txt", added.id)
  197. tree2.add(0644, "changed.txt", changed2.id)
  198. tree1.add(0644, "unchanged.txt", changed1.id)
  199. store.add_objects([(o, None) for o in [
  200. tree1, tree2, added, removed, changed1, changed2, unchanged]])
  201. write_tree_diff(f, store, tree1.id, tree2.id)
  202. self.assertEquals([
  203. 'diff --git a/changed.txt b/changed.txt',
  204. 'index bf84e48..1be2436 644',
  205. '--- a/changed.txt',
  206. '+++ b/changed.txt',
  207. '@@ -1,2 +1,2 @@',
  208. ' unchanged',
  209. '-removed',
  210. '+added',
  211. 'diff --git a/removed.txt /dev/null',
  212. 'deleted mode 644',
  213. 'index 2c3f0b3..e69de29',
  214. '--- a/removed.txt',
  215. '+++ /dev/null',
  216. '@@ -1,1 +1,0 @@',
  217. '-removed',
  218. 'diff --git a/unchanged.txt /dev/null',
  219. 'deleted mode 644',
  220. 'index bf84e48..e69de29',
  221. '--- a/unchanged.txt',
  222. '+++ /dev/null',
  223. '@@ -1,2 +1,0 @@',
  224. '-unchanged',
  225. '-removed',
  226. 'diff --git /dev/null b/added.txt',
  227. 'new mode 644',
  228. 'index e69de29..76d4bb8 644',
  229. '--- /dev/null',
  230. '+++ b/added.txt',
  231. '@@ -1,0 +1,1 @@',
  232. '+add'
  233. ], f.getvalue().splitlines())