test_fastexport.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 io import BytesIO
  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. from dulwich.tests.utils import (
  37. build_commit_graph,
  38. )
  39. class GitFastExporterTests(TestCase):
  40. """Tests for the GitFastExporter tests."""
  41. def setUp(self):
  42. super(GitFastExporterTests, self).setUp()
  43. self.store = MemoryObjectStore()
  44. self.stream = BytesIO()
  45. try:
  46. from dulwich.fastexport import GitFastExporter
  47. except ImportError:
  48. raise SkipTest("python-fastimport not available")
  49. self.fastexporter = GitFastExporter(self.stream, self.store)
  50. def test_emit_blob(self):
  51. b = Blob()
  52. b.data = "fooBAR"
  53. self.fastexporter.emit_blob(b)
  54. self.assertEqual('blob\nmark :1\ndata 6\nfooBAR\n',
  55. self.stream.getvalue())
  56. def test_emit_commit(self):
  57. b = Blob()
  58. b.data = "FOO"
  59. t = Tree()
  60. t.add("foo", stat.S_IFREG | 0o644, b.id)
  61. c = Commit()
  62. c.committer = c.author = "Jelmer <jelmer@host>"
  63. c.author_time = c.commit_time = 1271345553
  64. c.author_timezone = c.commit_timezone = 0
  65. c.message = "msg"
  66. c.tree = t.id
  67. self.store.add_objects([(b, None), (t, None), (c, None)])
  68. self.fastexporter.emit_commit(c, "refs/heads/master")
  69. self.assertEqual("""blob
  70. mark :1
  71. data 3
  72. FOO
  73. commit refs/heads/master
  74. mark :2
  75. author Jelmer <jelmer@host> 1271345553 +0000
  76. committer Jelmer <jelmer@host> 1271345553 +0000
  77. data 3
  78. msg
  79. M 644 1 foo
  80. """, self.stream.getvalue())
  81. class GitImportProcessorTests(TestCase):
  82. """Tests for the GitImportProcessor tests."""
  83. def setUp(self):
  84. super(GitImportProcessorTests, self).setUp()
  85. self.repo = MemoryRepo()
  86. try:
  87. from dulwich.fastexport import GitImportProcessor
  88. except ImportError:
  89. raise SkipTest("python-fastimport not available")
  90. self.processor = GitImportProcessor(self.repo)
  91. def test_reset_handler(self):
  92. from fastimport import commands
  93. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  94. cmd = commands.ResetCommand("refs/heads/foo", c1.id)
  95. self.processor.reset_handler(cmd)
  96. self.assertEqual(c1.id, self.repo.get_refs()["refs/heads/foo"])
  97. def test_commit_handler(self):
  98. from fastimport import commands
  99. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  100. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  101. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  102. "FOO", None, [], [])
  103. self.processor.commit_handler(cmd)
  104. commit = self.repo[self.processor.last_commit]
  105. self.assertEqual("Jelmer <jelmer@samba.org>", commit.author)
  106. self.assertEqual("Jelmer <jelmer@samba.org>", commit.committer)
  107. self.assertEqual("FOO", commit.message)
  108. self.assertEqual([], commit.parents)
  109. self.assertEqual(432432432.0, commit.commit_time)
  110. self.assertEqual(432432432.0, commit.author_time)
  111. self.assertEqual(3600, commit.commit_timezone)
  112. self.assertEqual(3600, commit.author_timezone)
  113. self.assertEqual(commit, self.repo["refs/heads/foo"])
  114. def test_import_stream(self):
  115. markers = self.processor.import_stream(BytesIO("""blob
  116. mark :1
  117. data 11
  118. text for a
  119. commit refs/heads/master
  120. mark :2
  121. committer Joe Foo <joe@foo.com> 1288287382 +0000
  122. data 20
  123. <The commit message>
  124. M 100644 :1 a
  125. """))
  126. self.assertEqual(2, len(markers))
  127. self.assertTrue(isinstance(self.repo[markers["1"]], Blob))
  128. self.assertTrue(isinstance(self.repo[markers["2"]], Commit))
  129. def test_file_add(self):
  130. from fastimport import commands
  131. cmd = commands.BlobCommand("23", "data")
  132. self.processor.blob_handler(cmd)
  133. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  134. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  135. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  136. "FOO", None, [], [commands.FileModifyCommand("path", 0o100644, ":23", None)])
  137. self.processor.commit_handler(cmd)
  138. commit = self.repo[self.processor.last_commit]
  139. self.assertEqual([
  140. ('path', 0o100644, '6320cd248dd8aeaab759d5871f8781b5c0505172')],
  141. self.repo[commit.tree].items())
  142. def simple_commit(self):
  143. from fastimport import commands
  144. cmd = commands.BlobCommand("23", "data")
  145. self.processor.blob_handler(cmd)
  146. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  147. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  148. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  149. "FOO", None, [], [commands.FileModifyCommand("path", 0o100644, ":23", None)])
  150. self.processor.commit_handler(cmd)
  151. commit = self.repo[self.processor.last_commit]
  152. return commit
  153. def make_file_commit(self, file_cmds):
  154. """Create a trivial commit with the specified file commands.
  155. :param file_cmds: File commands to run.
  156. :return: The created commit object
  157. """
  158. from fastimport import commands
  159. cmd = commands.CommitCommand("refs/heads/foo", "mrkr",
  160. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  161. ("Jelmer", "jelmer@samba.org", 432432432.0, 3600),
  162. "FOO", None, [], file_cmds)
  163. self.processor.commit_handler(cmd)
  164. return self.repo[self.processor.last_commit]
  165. def test_file_copy(self):
  166. from fastimport import commands
  167. self.simple_commit()
  168. commit = self.make_file_commit([commands.FileCopyCommand("path", "new_path")])
  169. self.assertEqual([
  170. ('new_path', 0o100644, '6320cd248dd8aeaab759d5871f8781b5c0505172'),
  171. ('path', 0o100644, '6320cd248dd8aeaab759d5871f8781b5c0505172'),
  172. ], self.repo[commit.tree].items())
  173. def test_file_move(self):
  174. from fastimport import commands
  175. self.simple_commit()
  176. commit = self.make_file_commit([commands.FileRenameCommand("path", "new_path")])
  177. self.assertEqual([
  178. ('new_path', 0o100644, '6320cd248dd8aeaab759d5871f8781b5c0505172'),
  179. ], self.repo[commit.tree].items())
  180. def test_file_delete(self):
  181. from fastimport import commands
  182. self.simple_commit()
  183. commit = self.make_file_commit([commands.FileDeleteCommand("path")])
  184. self.assertEqual([], self.repo[commit.tree].items())
  185. def test_file_deleteall(self):
  186. from fastimport import commands
  187. self.simple_commit()
  188. commit = self.make_file_commit([commands.FileDeleteAllCommand()])
  189. self.assertEqual([], self.repo[commit.tree].items())