Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / tests / test_fastexport.py @ 959

History | View | Annotate | Download (7.75 KB)

1
# test_fastexport.py -- Fast export/import functionality
2
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
3
#
4
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
5
# General Public License as public by the Free Software Foundation; version 2.0
6
# or (at your option) any later version. You can redistribute it and/or
7
# modify it under the terms of either of these two licenses.
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
#
15
# You should have received a copy of the licenses; if not, see
16
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
17
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
18
# License, Version 2.0.
19
#
20

    
21
from io import BytesIO
22
import stat
23

    
24

    
25
from dulwich.object_store import (
26
    MemoryObjectStore,
27
    )
28
from dulwich.objects import (
29
    Blob,
30
    Commit,
31
    Tree,
32
    )
33
from dulwich.repo import (
34
    MemoryRepo,
35
    )
36
from dulwich.tests import (
37
    SkipTest,
38
    TestCase,
39
    )
40
from dulwich.tests.utils import (
41
    build_commit_graph,
42
    )
43

    
44

    
45
class GitFastExporterTests(TestCase):
46
    """Tests for the GitFastExporter tests."""
47

    
48
    def setUp(self):
49
        super(GitFastExporterTests, self).setUp()
50
        self.store = MemoryObjectStore()
51
        self.stream = BytesIO()
52
        try:
53
            from dulwich.fastexport import GitFastExporter
54
        except ImportError:
55
            raise SkipTest("python-fastimport not available")
56
        self.fastexporter = GitFastExporter(self.stream, self.store)
57

    
58
    def test_emit_blob(self):
59
        b = Blob()
60
        b.data = b"fooBAR"
61
        self.fastexporter.emit_blob(b)
62
        self.assertEqual(b'blob\nmark :1\ndata 6\nfooBAR\n',
63
            self.stream.getvalue())
64

    
65
    def test_emit_commit(self):
66
        b = Blob()
67
        b.data = b"FOO"
68
        t = Tree()
69
        t.add(b"foo", stat.S_IFREG | 0o644, b.id)
70
        c = Commit()
71
        c.committer = c.author = b"Jelmer <jelmer@host>"
72
        c.author_time = c.commit_time = 1271345553
73
        c.author_timezone = c.commit_timezone = 0
74
        c.message = b"msg"
75
        c.tree = t.id
76
        self.store.add_objects([(b, None), (t, None), (c, None)])
77
        self.fastexporter.emit_commit(c, b"refs/heads/master")
78
        self.assertEqual(b"""blob
79
mark :1
80
data 3
81
FOO
82
commit refs/heads/master
83
mark :2
84
author Jelmer <jelmer@host> 1271345553 +0000
85
committer Jelmer <jelmer@host> 1271345553 +0000
86
data 3
87
msg
88
M 644 :1 foo
89
""", self.stream.getvalue())
90

    
91

    
92
class GitImportProcessorTests(TestCase):
93
    """Tests for the GitImportProcessor tests."""
94

    
95
    def setUp(self):
96
        super(GitImportProcessorTests, self).setUp()
97
        self.repo = MemoryRepo()
98
        try:
99
            from dulwich.fastexport import GitImportProcessor
100
        except ImportError:
101
            raise SkipTest("python-fastimport not available")
102
        self.processor = GitImportProcessor(self.repo)
103

    
104
    def test_reset_handler(self):
105
        from fastimport import commands
106
        [c1] = build_commit_graph(self.repo.object_store, [[1]])
107
        cmd = commands.ResetCommand(b"refs/heads/foo", c1.id)
108
        self.processor.reset_handler(cmd)
109
        self.assertEqual(c1.id, self.repo.get_refs()[b"refs/heads/foo"])
110

    
111
    def test_commit_handler(self):
112
        from fastimport import commands
113
        cmd = commands.CommitCommand(b"refs/heads/foo",  b"mrkr",
114
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
115
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
116
            b"FOO", None, [], [])
117
        self.processor.commit_handler(cmd)
118
        commit = self.repo[self.processor.last_commit]
119
        self.assertEqual(b"Jelmer <jelmer@samba.org>", commit.author)
120
        self.assertEqual(b"Jelmer <jelmer@samba.org>", commit.committer)
121
        self.assertEqual(b"FOO", commit.message)
122
        self.assertEqual([], commit.parents)
123
        self.assertEqual(432432432.0, commit.commit_time)
124
        self.assertEqual(432432432.0, commit.author_time)
125
        self.assertEqual(3600, commit.commit_timezone)
126
        self.assertEqual(3600, commit.author_timezone)
127
        self.assertEqual(commit, self.repo[b"refs/heads/foo"])
128

    
129
    def test_import_stream(self):
130
        markers = self.processor.import_stream(BytesIO(b"""blob
131
mark :1
132
data 11
133
text for a
134

135
commit refs/heads/master
136
mark :2
137
committer Joe Foo <joe@foo.com> 1288287382 +0000
138
data 20
139
<The commit message>
140
M 100644 :1 a
141

142
"""))
143
        self.assertEqual(2, len(markers))
144
        self.assertTrue(isinstance(self.repo[markers[b"1"]], Blob))
145
        self.assertTrue(isinstance(self.repo[markers[b"2"]], Commit))
146

    
147
    def test_file_add(self):
148
        from fastimport import commands
149
        cmd = commands.BlobCommand(b"23", b"data")
150
        self.processor.blob_handler(cmd)
151
        cmd = commands.CommitCommand(b"refs/heads/foo", b"mrkr",
152
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
153
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
154
            b"FOO", None, [], [commands.FileModifyCommand(b"path", 0o100644, b":23", None)])
155
        self.processor.commit_handler(cmd)
156
        commit = self.repo[self.processor.last_commit]
157
        self.assertEqual([
158
            (b'path', 0o100644, b'6320cd248dd8aeaab759d5871f8781b5c0505172')],
159
            self.repo[commit.tree].items())
160

    
161
    def simple_commit(self):
162
        from fastimport import commands
163
        cmd = commands.BlobCommand(b"23", b"data")
164
        self.processor.blob_handler(cmd)
165
        cmd = commands.CommitCommand(b"refs/heads/foo", b"mrkr",
166
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
167
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
168
            b"FOO", None, [], [commands.FileModifyCommand(b"path", 0o100644, b":23", None)])
169
        self.processor.commit_handler(cmd)
170
        commit = self.repo[self.processor.last_commit]
171
        return commit
172

    
173
    def make_file_commit(self, file_cmds):
174
        """Create a trivial commit with the specified file commands.
175

176
        :param file_cmds: File commands to run.
177
        :return: The created commit object
178
        """
179
        from fastimport import commands
180
        cmd = commands.CommitCommand(b"refs/heads/foo", b"mrkr",
181
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
182
            (b"Jelmer", b"jelmer@samba.org", 432432432.0, 3600),
183
            b"FOO", None, [], file_cmds)
184
        self.processor.commit_handler(cmd)
185
        return self.repo[self.processor.last_commit]
186

    
187
    def test_file_copy(self):
188
        from fastimport import commands
189
        self.simple_commit()
190
        commit = self.make_file_commit([commands.FileCopyCommand(b"path", b"new_path")])
191
        self.assertEqual([
192
            (b'new_path', 0o100644, b'6320cd248dd8aeaab759d5871f8781b5c0505172'),
193
            (b'path', 0o100644, b'6320cd248dd8aeaab759d5871f8781b5c0505172'),
194
            ], self.repo[commit.tree].items())
195

    
196
    def test_file_move(self):
197
        from fastimport import commands
198
        self.simple_commit()
199
        commit = self.make_file_commit([commands.FileRenameCommand(b"path", b"new_path")])
200
        self.assertEqual([
201
            (b'new_path', 0o100644, b'6320cd248dd8aeaab759d5871f8781b5c0505172'),
202
            ], self.repo[commit.tree].items())
203

    
204
    def test_file_delete(self):
205
        from fastimport import commands
206
        self.simple_commit()
207
        commit = self.make_file_commit([commands.FileDeleteCommand(b"path")])
208
        self.assertEqual([], self.repo[commit.tree].items())
209

    
210
    def test_file_deleteall(self):
211
        from fastimport import commands
212
        self.simple_commit()
213
        commit = self.make_file_commit([commands.FileDeleteAllCommand()])
214
        self.assertEqual([], self.repo[commit.tree].items())