test_fastexport.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.tests import (
  30. TestCase,
  31. TestSkipped,
  32. )
  33. class GitFastExporterTests(TestCase):
  34. def setUp(self):
  35. super(GitFastExporterTests, self).setUp()
  36. self.store = MemoryObjectStore()
  37. self.stream = StringIO()
  38. try:
  39. from dulwich.fastexport import GitFastExporter
  40. except ImportError:
  41. raise TestSkipped("python-fastimport not available")
  42. self.fastexporter = GitFastExporter(self.stream, self.store)
  43. def test_emit_blob(self):
  44. b = Blob()
  45. b.data = "fooBAR"
  46. self.fastexporter.emit_blob(b)
  47. self.assertEquals('blob\nmark :1\ndata 6\nfooBAR\n',
  48. self.stream.getvalue())
  49. def test_emit_commit(self):
  50. b = Blob()
  51. b.data = "FOO"
  52. t = Tree()
  53. t.add(stat.S_IFREG | 0644, "foo", b.id)
  54. c = Commit()
  55. c.committer = c.author = "Jelmer <jelmer@host>"
  56. c.author_time = c.commit_time = 1271345553
  57. c.author_timezone = c.commit_timezone = 0
  58. c.message = "msg"
  59. c.tree = t.id
  60. self.store.add_objects([(b, None), (t, None), (c, None)])
  61. self.fastexporter.emit_commit(c, "refs/heads/master")
  62. self.assertEquals("""blob
  63. mark :1
  64. data 3
  65. FOO
  66. commit refs/heads/master
  67. mark :2
  68. author Jelmer <jelmer@host> 1271345553 +0000
  69. committer Jelmer <jelmer@host> 1271345553 +0000
  70. data 3
  71. msg
  72. M 644 1 foo
  73. """, self.stream.getvalue())