2439804c313fc63e254343908aa18c31b05711f3
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_fastexport.py
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
20 from cStringIO import StringIO
21 import stat
22
23 from dulwich.fastexport import (
24     FastExporter,
25     )
26 from dulwich.object_store import (
27     MemoryObjectStore,
28     )
29 from dulwich.objects import (
30     Blob,
31     Commit,
32     Tree,
33     )
34 from dulwich.tests import (
35     TestCase,
36     )
37
38
39 class FastExporterTests(TestCase):
40
41     def setUp(self):
42         super(FastExporterTests, self).setUp()
43         self.store = MemoryObjectStore()
44         self.stream = StringIO()
45         self.fastexporter = FastExporter(self.stream, self.store)
46
47     def test_export_blob(self):
48         b = Blob()
49         b.data = "fooBAR"
50         self.assertEquals(1, self.fastexporter.export_blob(b))
51         self.assertEquals('blob\nmark :1\ndata 6\nfooBAR\n',
52             self.stream.getvalue())
53
54     def test_export_commit(self):
55         b = Blob()
56         b.data = "FOO"
57         t = Tree()
58         t.add(stat.S_IFREG | 0644, "foo", b.id)
59         c = Commit()
60         c.committer = c.author = "Jelmer <jelmer@host>"
61         c.author_time = c.commit_time = 1271345553.47
62         c.author_timezone = c.commit_timezone = 0
63         c.message = "msg"
64         c.tree = t.id
65         self.store.add_objects([(b, None), (t, None), (c, None)])
66         self.assertEquals(2,
67                 self.fastexporter.export_commit(c, "refs/heads/master"))
68         self.assertEquals("""blob
69 mark :1
70 data 3
71 FOO
72 commit refs/heads/master
73 mark :2
74 author Jelmer <jelmer@host> 1271345553.47 +0000
75 committer Jelmer <jelmer@host> 1271345553.47 +0000
76 data 3
77 msg
78 M 100644 :1 foo
79
80 """, self.stream.getvalue())