test_patch.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 unittest import TestCase
  21. from dulwich.objects import (
  22. Commit,
  23. Tree,
  24. )
  25. from dulwich.patch import (
  26. git_am_patch_split,
  27. write_commit_patch,
  28. )
  29. class WriteCommitPatchTests(TestCase):
  30. def test_simple(self):
  31. f = StringIO()
  32. c = Commit()
  33. c.committer = c.author = "Jelmer <jelmer@samba.org>"
  34. c.commit_time = c.author_time = 1271350201
  35. c.commit_timezone = c.author_timezone = 0
  36. c.message = "This is the first line\nAnd this is the second line.\n"
  37. c.tree = Tree().id
  38. write_commit_patch(f, c, "CONTENTS", (1, 1), version="custom")
  39. f.seek(0)
  40. lines = f.readlines()
  41. self.assertTrue(lines[0].startswith("From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
  42. self.assertEquals(lines[1], "From: Jelmer <jelmer@samba.org>\n")
  43. self.assertTrue(lines[2].startswith("Date: "))
  44. self.assertEquals([
  45. "Subject: [PATCH 1/1] This is the first line\n",
  46. "And this is the second line.\n",
  47. "\n",
  48. "\n",
  49. "---\n"], lines[3:8])
  50. self.assertEquals([
  51. "CONTENTS-- \n",
  52. "custom\n"], lines[-2:])
  53. if len(lines) >= 12:
  54. # diffstat may not be present
  55. self.assertEquals(lines[8], " 0 files changed\n")
  56. class ReadGitAmPatch(TestCase):
  57. def test_extract(self):
  58. text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  59. From: Jelmer Vernooij <jelmer@samba.org>
  60. Date: Thu, 15 Apr 2010 15:40:28 +0200
  61. Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a lintian warning).
  62. ---
  63. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  64. 1 files changed, 0 insertions(+), 0 deletions(-)
  65. mode change 100755 => 100644 pixmaps/prey.ico
  66. --
  67. 1.7.0.4
  68. """
  69. c, diff, version = git_am_patch_split(StringIO(text))
  70. self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.committer)
  71. self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.author)
  72. self.assertEquals(""" pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  73. 1 files changed, 0 insertions(+), 0 deletions(-)
  74. mode change 100755 => 100644 pixmaps/prey.ico
  75. """, diff)
  76. self.assertEquals("1.7.0.4", version)