/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

  • Committer: mbp at sourcefrog
  • Date: 2005-03-09 04:51:05 UTC
  • Revision ID: mbp@sourcefrog.net-20050309045105-d02cd410a115da2c
import all docs from arch

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