/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_repository.py

Default to using global user id rather than making one up.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for interfacing with a Git Repository"""
 
18
 
 
19
import dulwich
 
20
from dulwich.repo import (
 
21
    Repo as GitRepo,
 
22
    )
 
23
import os
 
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    inventory,
 
28
    revision,
 
29
    )
 
30
from bzrlib.repository import (
 
31
    InterRepository,
 
32
    Repository,
 
33
    )
 
34
 
 
35
from bzrlib.plugins.git import (
 
36
    dir,
 
37
    repository,
 
38
    tests,
 
39
    )
 
40
from bzrlib.plugins.git.mapping import (
 
41
    default_mapping,
 
42
    )
 
43
from bzrlib.plugins.git.push import (
 
44
    MissingObjectsIterator,
 
45
    )
 
46
 
 
47
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
 
48
    """Feature tests for GitRepository."""
 
49
 
 
50
    _test_needs_features = [tests.GitCommandFeature]
 
51
 
 
52
    def test_open_existing(self):
 
53
        GitRepo.init(self.test_dir)
 
54
 
 
55
        repo = Repository.open('.')
 
56
        self.assertIsInstance(repo, repository.GitRepository)
 
57
 
 
58
    def test_has_git_repo(self):
 
59
        GitRepo.init(self.test_dir)
 
60
 
 
61
        repo = Repository.open('.')
 
62
        self.assertIsInstance(repo._git, dulwich.repo.Repo)
 
63
 
 
64
    def test_get_revision(self):
 
65
        # GitRepository.get_revision gives a Revision object.
 
66
 
 
67
        # Create a git repository with a revision.
 
68
        GitRepo.init(self.test_dir)
 
69
        builder = tests.GitBranchBuilder()
 
70
        builder.set_file('a', 'text for a\n', False)
 
71
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
 
72
        mapping = builder.finish()
 
73
        commit_id = mapping[commit_handle]
 
74
 
 
75
        # Get the corresponding Revision object.
 
76
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
 
77
        repo = Repository.open('.')
 
78
        rev = repo.get_revision(revid)
 
79
        self.assertIsInstance(rev, revision.Revision)
 
80
 
 
81
    def test_get_revision_unknown(self):
 
82
        GitRepo.init(self.test_dir)
 
83
 
 
84
        repo = Repository.open('.')
 
85
        self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")
 
86
 
 
87
    def simple_commit(self):
 
88
        # Create a git repository with some interesting files in a revision.
 
89
        GitRepo.init(self.test_dir)
 
90
        builder = tests.GitBranchBuilder()
 
91
        builder.set_file('data', 'text\n', False)
 
92
        builder.set_file('executable', 'content', True)
 
93
        builder.set_link('link', 'broken')
 
94
        builder.set_file('subdir/subfile', 'subdir text\n', False)
 
95
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
 
96
            timestamp=1205433193)
 
97
        mapping = builder.finish()
 
98
        return mapping[commit_handle]
 
99
 
 
100
    def test_revision_tree(self):
 
101
        commit_id = self.simple_commit()
 
102
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
 
103
        repo = Repository.open('.')
 
104
        tree = repo.revision_tree(revid)
 
105
        self.assertEquals(tree.get_revision_id(), revid)
 
106
        self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
 
107
 
 
108
    def test_get_inventory(self):
 
109
        # GitRepository.get_inventory gives a GitInventory object with
 
110
        # plausible entries for typical cases.
 
111
 
 
112
        commit_id = self.simple_commit()
 
113
 
 
114
        # Get the corresponding Inventory object.
 
115
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
 
116
        repo = Repository.open('.')
 
117
        inv = repo.get_inventory(revid)
 
118
        self.assertIsInstance(inv, inventory.Inventory)
 
119
        printed_inv = '\n'.join(
 
120
            repr((path, entry.executable, entry))
 
121
            for path, entry in inv.iter_entries())
 
122
        self.assertEqualDiff(
 
123
            printed_inv,
 
124
            "('', False, GitInventoryDirectory('TREE_ROOT', u'', parent_id=None,"
 
125
            " revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
 
126
            "(u'data', False, GitInventoryFile('data', u'data',"
 
127
            " parent_id='TREE_ROOT',"
 
128
            " sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5, revision=git-v1:69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
 
129
            "(u'executable', True, GitInventoryFile('executable', u'executable',"
 
130
            " parent_id='TREE_ROOT',"
 
131
            " sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7, revision=git-v1:69c39cfa65962f3cf16b9b3eb08a15954e9d8590))\n"
 
132
            "(u'link', False, GitInventoryLink('link', u'link',"
 
133
            " parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
 
134
            "(u'subdir', False, GitInventoryDirectory('subdir', u'subdir',"
 
135
            " parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
 
136
            "(u'subdir/subfile', False, GitInventoryFile('subdir/subfile',"
 
137
            " u'subfile', parent_id='subdir',"
 
138
            " sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12, revision=git-v1:69c39cfa65962f3cf16b9b3eb08a15954e9d8590))")
 
139
 
 
140
 
 
141
class TestGitRepository(tests.TestCaseWithTransport):
 
142
 
 
143
    def setUp(self):
 
144
        tests.TestCaseWithTransport.setUp(self)
 
145
        dulwich.repo.Repo.create(self.test_dir)
 
146
        self.git_repo = Repository.open(self.test_dir)
 
147
 
 
148
    def test_supports_rich_root(self):
 
149
        repo = self.git_repo
 
150
        self.assertEqual(repo.supports_rich_root(), True)
 
151
 
 
152
    def test_get_signature_text(self):
 
153
        self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
 
154
 
 
155
    def test_has_signature_for_revision_id(self):
 
156
        self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
 
157
 
 
158
    def test_all_revision_ids_none(self):
 
159
        self.assertEquals(set([revision.NULL_REVISION]), self.git_repo.all_revision_ids())
 
160
 
 
161
    def test_get_ancestry_null(self):
 
162
        self.assertEquals([None, revision.NULL_REVISION], self.git_repo.get_ancestry(revision.NULL_REVISION))
 
163
 
 
164
    def assertIsNullInventory(self, inv):
 
165
        self.assertEqual(inv.root, None)
 
166
        self.assertEqual(inv.revision_id, revision.NULL_REVISION)
 
167
        self.assertEqual(list(inv.iter_entries()), [])
 
168
 
 
169
    def test_get_inventory_none(self):
 
170
        # GitRepository.get_inventory(None) returns the null inventory.
 
171
        repo = self.git_repo
 
172
        inv = repo.get_inventory(revision.NULL_REVISION)
 
173
        self.assertIsNullInventory(inv)
 
174
 
 
175
    def test_revision_tree_none(self):
 
176
        # GitRepository.revision_tree(None) returns the null tree.
 
177
        repo = self.git_repo
 
178
        tree = repo.revision_tree(revision.NULL_REVISION)
 
179
        self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
 
180
        self.assertIsNullInventory(tree.inventory)
 
181
 
 
182
    def test_get_parent_map_null(self):
 
183
        self.assertEquals({revision.NULL_REVISION: ()}, 
 
184
                           self.git_repo.get_parent_map([revision.NULL_REVISION]))
 
185
 
 
186
 
 
187
class GitRepositoryFormat(tests.TestCase):
 
188
 
 
189
    def setUp(self):
 
190
        super(GitRepositoryFormat, self).setUp()
 
191
        self.format = repository.GitRepositoryFormat()
 
192
 
 
193
    def test_get_format_description(self):
 
194
        self.assertEquals("Git Repository", self.format.get_format_description())
 
195
 
 
196
 
 
197
class RevisionGistImportTests(tests.TestCaseWithTransport):
 
198
 
 
199
    def setUp(self):
 
200
        tests.TestCaseWithTransport.setUp(self)
 
201
        self.git_path = os.path.join(self.test_dir, "git")
 
202
        os.mkdir(self.git_path)
 
203
        dulwich.repo.Repo.create(self.git_path)
 
204
        self.git_repo = Repository.open(self.git_path)
 
205
        self.bzr_tree = self.make_branch_and_tree("bzr")
 
206
 
 
207
    def get_inter(self):
 
208
        return InterRepository.get(self.bzr_tree.branch.repository, 
 
209
                                   self.git_repo)
 
210
 
 
211
    def object_iter(self):
 
212
        return MissingObjectsIterator(self.bzr_tree.branch.repository, 
 
213
                                      default_mapping)
 
214
 
 
215
    def import_rev(self, revid, parent_lookup=None):
 
216
        return self.object_iter().import_revision(revid)
 
217
 
 
218
    def test_pointless(self):
 
219
        revid = self.bzr_tree.commit("pointless", timestamp=1205433193,
 
220
                timezone=0,
 
221
                  committer="Jelmer Vernooij <jelmer@samba.org>")
 
222
        self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14", 
 
223
                self.import_rev(revid))
 
224
        self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14", 
 
225
                self.import_rev(revid))