/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

Fix GitInventory.__repr__ to include more information.

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 as git
 
20
import os
 
21
 
 
22
from bzrlib import (
 
23
    errors,
 
24
    inventory,
 
25
    revision,
 
26
    )
 
27
from bzrlib.repository import (
 
28
    InterRepository,
 
29
    Repository,
 
30
    )
 
31
 
 
32
from bzrlib.plugins.git import (
 
33
    dir,
 
34
    repository,
 
35
    tests,
 
36
    )
 
37
from bzrlib.plugins.git.mapping import (
 
38
    default_mapping,
 
39
    )
 
40
from bzrlib.plugins.git.push import (
 
41
    MissingObjectsIterator,
 
42
    )
 
43
 
 
44
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
 
45
    """Feature tests for GitRepository."""
 
46
 
 
47
    _test_needs_features = [tests.GitCommandFeature]
 
48
 
 
49
    def test_open_existing(self):
 
50
        tests.run_git('init')
 
51
 
 
52
        repo = Repository.open('.')
 
53
        self.assertIsInstance(repo, repository.GitRepository)
 
54
 
 
55
    def test_has_git_repo(self):
 
56
        tests.run_git('init')
 
57
 
 
58
        repo = Repository.open('.')
 
59
        self.assertIsInstance(repo._git, git.repo.Repo)
 
60
 
 
61
    def test_get_revision(self):
 
62
        # GitRepository.get_revision gives a Revision object.
 
63
 
 
64
        # Create a git repository with a revision.
 
65
        tests.run_git('init')
 
66
        builder = tests.GitBranchBuilder()
 
67
        builder.set_file('a', 'text for a\n', False)
 
68
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
 
69
        mapping = builder.finish()
 
70
        commit_id = mapping[commit_handle]
 
71
 
 
72
        # Get the corresponding Revision object.
 
73
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
 
74
        repo = Repository.open('.')
 
75
        rev = repo.get_revision(revid)
 
76
        self.assertIsInstance(rev, revision.Revision)
 
77
 
 
78
    def test_get_revision_unknown(self):
 
79
        tests.run_git('init')
 
80
 
 
81
        repo = Repository.open('.')
 
82
        self.assertRaises(errors.NoSuchRevision, repo.get_revision, "bla")
 
83
 
 
84
    def simple_commit(self):
 
85
        # Create a git repository with some interesting files in a revision.
 
86
        tests.run_git('init')
 
87
        builder = tests.GitBranchBuilder()
 
88
        builder.set_file('data', 'text\n', False)
 
89
        builder.set_file('executable', 'content', True)
 
90
        builder.set_link('link', 'broken')
 
91
        builder.set_file('subdir/subfile', 'subdir text\n', False)
 
92
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
 
93
            timestamp=1205433193)
 
94
        mapping = builder.finish()
 
95
        return mapping[commit_handle]
 
96
 
 
97
    def test_revision_tree(self):
 
98
        commit_id = self.simple_commit()
 
99
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
 
100
        repo = Repository.open('.')
 
101
        tree = repo.revision_tree(revid)
 
102
        self.assertEquals(tree.get_revision_id(), revid)
 
103
        self.assertEquals("text\n", tree.get_file_text(tree.path2id("data")))
 
104
 
 
105
    def test_get_inventory(self):
 
106
        # GitRepository.get_inventory gives a GitInventory object with
 
107
        # plausible entries for typical cases.
 
108
 
 
109
        commit_id = self.simple_commit()
 
110
 
 
111
        # Get the corresponding Inventory object.
 
112
        revid = default_mapping.revision_id_foreign_to_bzr(commit_id)
 
113
        repo = Repository.open('.')
 
114
        inv = repo.get_inventory(revid)
 
115
        self.assertIsInstance(inv, inventory.Inventory)
 
116
        printed_inv = '\n'.join(
 
117
            repr((path, entry.executable, entry))
 
118
            for path, entry in inv.iter_entries())
 
119
        self.assertEqualDiff(
 
120
            printed_inv,
 
121
            "('', False, GitInventoryDirectory('TREE_ROOT', u'', parent_id=None,"
 
122
            " revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
 
123
            "(u'data', False, GitInventoryFile('data', u'data',"
 
124
            " parent_id='TREE_ROOT',"
 
125
            " sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5))\n"
 
126
            "(u'executable', True, GitInventoryFile('executable', u'executable',"
 
127
            " parent_id='TREE_ROOT',"
 
128
            " sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7))\n"
 
129
            "(u'link', False, GitInventoryLink('link', u'link',"
 
130
            " parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
 
131
            "(u'subdir', False, GitInventoryDirectory('subdir', u'subdir',"
 
132
            " parent_id='TREE_ROOT', revision='"+default_mapping.revision_id_foreign_to_bzr("69c39cfa65962f3cf16b9b3eb08a15954e9d8590")+"'))\n"
 
133
            "(u'subdir/subfile', False, GitInventoryFile('subdir/subfile',"
 
134
            " u'subfile', parent_id='subdir',"
 
135
            " sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12))")
 
136
 
 
137
 
 
138
class TestGitRepository(tests.TestCaseWithTransport):
 
139
 
 
140
    def setUp(self):
 
141
        tests.TestCaseWithTransport.setUp(self)
 
142
        git.repo.Repo.create(self.test_dir)
 
143
        self.git_repo = Repository.open(self.test_dir)
 
144
 
 
145
    def test_supports_rich_root(self):
 
146
        repo = self.git_repo
 
147
        self.assertEqual(repo.supports_rich_root(), True)
 
148
 
 
149
    def test_get_signature_text(self):
 
150
        self.assertRaises(errors.NoSuchRevision, self.git_repo.get_signature_text, revision.NULL_REVISION)
 
151
 
 
152
    def test_has_signature_for_revision_id(self):
 
153
        self.assertEquals(False, self.git_repo.has_signature_for_revision_id(revision.NULL_REVISION))
 
154
 
 
155
    def test_all_revision_ids_none(self):
 
156
        self.assertEquals(set([revision.NULL_REVISION]), self.git_repo.all_revision_ids())
 
157
 
 
158
    def test_get_ancestry_null(self):
 
159
        self.assertEquals([None, revision.NULL_REVISION], self.git_repo.get_ancestry(revision.NULL_REVISION))
 
160
 
 
161
    def assertIsNullInventory(self, inv):
 
162
        self.assertEqual(inv.root, None)
 
163
        self.assertEqual(inv.revision_id, revision.NULL_REVISION)
 
164
        self.assertEqual(list(inv.iter_entries()), [])
 
165
 
 
166
    def test_get_inventory_none(self):
 
167
        # GitRepository.get_inventory(None) returns the null inventory.
 
168
        repo = self.git_repo
 
169
        inv = repo.get_inventory(revision.NULL_REVISION)
 
170
        self.assertIsNullInventory(inv)
 
171
 
 
172
    def test_revision_tree_none(self):
 
173
        # GitRepository.revision_tree(None) returns the null tree.
 
174
        repo = self.git_repo
 
175
        tree = repo.revision_tree(revision.NULL_REVISION)
 
176
        self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
 
177
        self.assertIsNullInventory(tree.inventory)
 
178
 
 
179
    def test_get_parent_map_null(self):
 
180
        self.assertEquals({revision.NULL_REVISION: ()}, 
 
181
                           self.git_repo.get_parent_map([revision.NULL_REVISION]))
 
182
 
 
183
 
 
184
class GitRepositoryFormat(tests.TestCase):
 
185
 
 
186
    def setUp(self):
 
187
        super(GitRepositoryFormat, self).setUp()
 
188
        self.format = repository.GitRepositoryFormat()
 
189
 
 
190
    def test_get_format_description(self):
 
191
        self.assertEquals("Git Repository", self.format.get_format_description())
 
192
 
 
193
 
 
194
class RevisionGistImportTests(tests.TestCaseWithTransport):
 
195
 
 
196
    def setUp(self):
 
197
        tests.TestCaseWithTransport.setUp(self)
 
198
        self.git_path = os.path.join(self.test_dir, "git")
 
199
        os.mkdir(self.git_path)
 
200
        git.repo.Repo.create(self.git_path)
 
201
        self.git_repo = Repository.open(self.git_path)
 
202
        self.bzr_tree = self.make_branch_and_tree("bzr")
 
203
 
 
204
    def get_inter(self):
 
205
        return InterRepository.get(self.bzr_tree.branch.repository, 
 
206
                                   self.git_repo)
 
207
 
 
208
    def object_iter(self):
 
209
        return MissingObjectsIterator(self.bzr_tree.branch.repository, 
 
210
                                      default_mapping)
 
211
 
 
212
    def import_rev(self, revid, parent_lookup=None):
 
213
        return self.object_iter().import_revision(revid)
 
214
 
 
215
    def test_pointless(self):
 
216
        revid = self.bzr_tree.commit("pointless", timestamp=1205433193,
 
217
                timezone=0,
 
218
                  committer="Jelmer Vernooij <jelmer@samba.org>")
 
219
        self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14", 
 
220
                self.import_rev(revid))
 
221
        self.assertEquals("2caa8094a5b794961cd9bf582e3e2bb090db0b14", 
 
222
                self.import_rev(revid))