test_patch.py 11 KB

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