test_fastexport.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # test_fastexport.py -- Fast export/import functionality
  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) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. from cStringIO import StringIO
  20. import stat
  21. from dulwich.object_store import (
  22. MemoryObjectStore,
  23. )
  24. from dulwich.objects import (
  25. Blob,
  26. Commit,
  27. Tree,
  28. )
  29. from dulwich.repo import (
  30. MemoryRepo,
  31. )
  32. from dulwich.tests import (
  33. SkipTest,
  34. TestCase,
  35. )
  36. class GitFastExporterTests(TestCase):
  37. """Tests for the GitFastExporter tests."""
  38. def setUp(self):
  39. super(GitFastExporterTests, self).setUp()
  40. self.store = MemoryObjectStore()
  41. self.stream = StringIO()
  42. try:
  43. from dulwich.fastexport import GitFastExporter
  44. except ImportError:
  45. raise SkipTest("python-fastimport not available")
  46. self.fastexporter = GitFastExporter(self.stream, self.store)
  47. def test_emit_blob(self):
  48. b = Blob()
  49. b.data = "fooBAR"
  50. self.fastexporter.emit_blob(b)
  51. self.assertEqual('blob\nmark :1\ndata 6\nfooBAR\n',
  52. self.stream.getvalue())
  53. def test_emit_commit(self):
  54. b = Blob()
  55. b.data = "FOO"
  56. t = Tree()
  57. t.add("foo", stat.S_IFREG | 0644, b.id)
  58. c = Commit()
  59. c.committer = c.author = "Jelmer <jelmer@host>"
  60. c.author_time = c.commit_time = 1271345553
  61. c.author_timezone = c.commit_timezone = 0
  62. c.message = "msg"
  63. c.tree = t.id
  64. self.store.add_objects([(b, None), (t, None), (c, None)])
  65. self.fastexporter.emit_commit(c, "refs/heads/master")
  66. self.assertEqual("""blob
  67. mark :1
  68. data 3
  69. FOO
  70. commit refs/heads/master
  71. mark :2
  72. author Jelmer <jelmer@host> 1271345553 +0000
  73. committer Jelmer <jelmer@host> 1271345553 +0000
  74. data 3
  75. msg
  76. M 644 1 foo
  77. """, self.stream.getvalue())
  78. class GitImportProcessorTests(TestCase):
  79. """Tests for the GitImportProcessor tests."""
  80. def setUp(self):
  81. super(GitImportProcessorTests, self).setUp()
  82. self.repo = MemoryRepo()
  83. try:
  84. from dulwich.fastexport import GitImportProcessor
  85. except ImportError:
  86. raise SkipTest("python-fastimport not available")
  87. self.processor = GitImportProcessor(self.repo)
  88. def test_commit_handler(self):
  89. from fastimport import commands
  90. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  91. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  92. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  93. "FOO", None, [], [])
  94. self.processor.commit_handler(cmd)
  95. commit = self.repo[self.processor.last_commit]
  96. self.assertEqual("Jelmer <jelmer@samba.org>", commit.author)
  97. self.assertEqual("Jelmer <jelmer@samba.org>", commit.committer)
  98. self.assertEqual("FOO", commit.message)
  99. self.assertEqual([], commit.parents)
  100. self.assertEqual(432432432.0, commit.commit_time)
  101. self.assertEqual(432432432.0, commit.author_time)
  102. self.assertEqual(3600, commit.commit_timezone)
  103. self.assertEqual(3600, commit.author_timezone)
  104. self.assertEqual(commit, self.repo["refs/heads/foo"])
  105. def test_import_stream(self):
  106. markers = self.processor.import_stream(StringIO("""blob
  107. mark :1
  108. data 11
  109. text for a
  110. commit refs/heads/master
  111. mark :2
  112. committer Joe Foo <joe@foo.com> 1288287382 +0000
  113. data 20
  114. <The commit message>
  115. M 100644 :1 a
  116. """))
  117. self.assertEqual(2, len(markers))
  118. self.assertTrue(isinstance(self.repo[markers["1"]], Blob))
  119. self.assertTrue(isinstance(self.repo[markers["2"]], Commit))
  120. def test_file_add(self):
  121. from fastimport import commands
  122. cmd = commands.BlobCommand("23", "data")
  123. self.processor.blob_handler(cmd)
  124. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  125. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  126. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  127. "FOO", None, [], [commands.FileModifyCommand("path", 0100644, ":23", None)])
  128. self.processor.commit_handler(cmd)
  129. commit = self.repo[self.processor.last_commit]
  130. self.assertEqual([
  131. ('path', 0100644, '6320cd248dd8aeaab759d5871f8781b5c0505172')],
  132. self.repo[commit.tree].items())
  133. def simple_commit(self):
  134. from fastimport import commands
  135. cmd = commands.BlobCommand("23", "data")
  136. self.processor.blob_handler(cmd)
  137. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  138. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  139. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  140. "FOO", None, [], [commands.FileModifyCommand("path", 0100644, ":23", None)])
  141. self.processor.commit_handler(cmd)
  142. commit = self.repo[self.processor.last_commit]
  143. return commit
  144. def make_file_commit(self, file_cmds):
  145. """Create a trivial commit with the specified file commands.
  146. :param file_cmds: File commands to run.
  147. :return: The created commit object
  148. """
  149. from fastimport import commands
  150. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  151. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  152. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  153. "FOO", None, [], file_cmds)
  154. self.processor.commit_handler(cmd)
  155. return self.repo[self.processor.last_commit]
  156. def test_file_copy(self):
  157. from fastimport import commands
  158. self.simple_commit()
  159. commit = self.make_file_commit([commands.FileCopyCommand("path", "new_path")])
  160. self.assertEqual([
  161. ('new_path', 0100644, '6320cd248dd8aeaab759d5871f8781b5c0505172'),
  162. ('path', 0100644, '6320cd248dd8aeaab759d5871f8781b5c0505172'),
  163. ], self.repo[commit.tree].items())
  164. def test_file_move(self):
  165. from fastimport import commands
  166. self.simple_commit()
  167. commit = self.make_file_commit([commands.FileRenameCommand("path", "new_path")])
  168. self.assertEqual([
  169. ('new_path', 0100644, '6320cd248dd8aeaab759d5871f8781b5c0505172'),
  170. ], self.repo[commit.tree].items())
  171. def test_file_delete(self):
  172. from fastimport import commands
  173. self.simple_commit()
  174. commit = self.make_file_commit([commands.FileDeleteCommand("path")])
  175. self.assertEqual([], self.repo[commit.tree].items())
  176. def test_file_deleteall(self):
  177. from fastimport import commands
  178. self.simple_commit()
  179. commit = self.make_file_commit([commands.FileDeleteAllCommand()])
  180. self.assertEqual([], self.repo[commit.tree].items())