/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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 subprocess
20
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
21
from bzrlib import (
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
22
    inventory,
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
23
    repository,
24
    revision,
25
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
26
27
from bzrlib.plugins.git import tests
0.200.27 by David Allouche
Flat is better than nested, remove the gitlib hierarchy.
28
from bzrlib.plugins.git import (
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
29
    git_dir,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
30
    git_repository,
0.200.21 by John Arbash Meinel
Fix Repository.get_revision_graph()
31
    ids,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
32
    model,
33
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
34
35
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
36
class TestGitRepositoryFeatures(tests.TestCaseInTempDir):
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
37
    """Feature tests for GitRepository."""
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
38
39
    _test_needs_features = [tests.GitCommandFeature]
40
41
    def test_open_existing(self):
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
42
        tests.run_git('init')
43
44
        repo = repository.Repository.open('.')
45
        self.assertIsInstance(repo, git_repository.GitRepository)
46
47
    def test_has_git_model(self):
48
        tests.run_git('init')
49
50
        repo = repository.Repository.open('.')
51
        self.assertIsInstance(repo._git, model.GitModel)
0.200.21 by John Arbash Meinel
Fix Repository.get_revision_graph()
52
53
    def test_revision_graph(self):
54
        tests.run_git('init')
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
55
        builder = tests.GitBranchBuilder()
0.200.31 by David Allouche
GitBranchBuilder.set_file returns None, do not save its return value.
56
        builder.set_file('a', 'text for a\n', False)
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
57
        commit1_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
0.200.31 by David Allouche
GitBranchBuilder.set_file returns None, do not save its return value.
58
        builder.set_file('a', 'new a\n', False)
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
59
        commit2_handle = builder.commit('Joe Foo <joe@foo.com>', u'new a')
0.200.31 by David Allouche
GitBranchBuilder.set_file returns None, do not save its return value.
60
        builder.set_file('b', 'text for b\n', False)
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
61
        commit3_handle = builder.commit('Jerry Bar <jerry@foo.com>', u'b',
62
                                        base=commit1_handle)
63
        commit4_handle = builder.commit('Jerry Bar <jerry@foo.com>', u'merge',
64
                                        base=commit3_handle,
65
                                        merge=[commit2_handle],)
66
67
        mapping = builder.finish()
68
        commit1_id = mapping[commit1_handle]
69
        commit2_id = mapping[commit2_handle]
70
        commit3_id = mapping[commit3_handle]
71
        commit4_id = mapping[commit4_handle]
72
73
        revisions = tests.run_git('rev-list', '--topo-order',
74
                                  commit4_id)
75
        revisions = revisions.splitlines()
76
        self.assertEqual([commit4_id, commit2_id, commit3_id, commit1_id],
77
                         revisions)
78
        bzr_revisions = [ids.convert_revision_id_git_to_bzr(r) for r in revisions]
79
        graph = {bzr_revisions[0]:[bzr_revisions[2], bzr_revisions[1]],
80
                 bzr_revisions[1]:[bzr_revisions[3]],
81
                 bzr_revisions[2]:[bzr_revisions[3]],
82
                 bzr_revisions[3]:[],
0.200.21 by John Arbash Meinel
Fix Repository.get_revision_graph()
83
                }
84
85
        repo = repository.Repository.open('.')
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
86
        self.assertEqual(graph, repo.get_revision_graph(bzr_revisions[0]))
87
        self.assertEqual({bzr_revisions[3]:[]},
88
                         repo.get_revision_graph(bzr_revisions[3]))
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
89
90
    def test_get_revision(self):
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
91
        # GitRepository.get_revision gives a Revision object.
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
92
93
        # Create a git repository with a revision.
94
        tests.run_git('init')
95
        builder = tests.GitBranchBuilder()
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
96
        builder.set_file('a', 'text for a\n', False)
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
97
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message')
98
        mapping = builder.finish()
99
        commit_id = mapping[commit_handle]
100
101
        # Get the corresponding Revision object.
102
        revid = ids.convert_revision_id_git_to_bzr(commit_id)
103
        repo = repository.Repository.open('.')
104
        rev = repo.get_revision(revid)
105
        self.assertIsInstance(rev, revision.Revision)
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
106
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
107
    def test_get_inventory(self):
108
        # GitRepository.get_inventory gives a GitInventory object with
109
        # plausible entries for typical cases.
110
111
        # Create a git repository with some interesting files in a revision.
112
        tests.run_git('init')
113
        builder = tests.GitBranchBuilder()
114
        builder.set_file('data', 'text\n', False)
115
        builder.set_file('executable', 'content', True)
116
        builder.set_link('link', 'broken')
117
        builder.set_file('subdir/subfile', 'subdir text\n', False)
0.204.4 by James Westby
Make the inventory test match what the code produces.
118
        commit_handle = builder.commit('Joe Foo <joe@foo.com>', u'message',
119
            timestamp=1205433193)
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
120
        mapping = builder.finish()
121
        commit_id = mapping[commit_handle]
122
123
        # Get the corresponding Inventory object.
124
        revid = ids.convert_revision_id_git_to_bzr(commit_id)
125
        repo = repository.Repository.open('.')
126
        inv = repo.get_inventory(revid)
127
        self.assertIsInstance(inv, inventory.Inventory)
128
        printed_inv = '\n'.join(
129
            repr((path, entry.executable, entry))
130
            for path, entry in inv.iter_entries())
131
        self.assertEqualDiff(
132
            printed_inv,
133
            "('', False, InventoryDirectory('TREE_ROOT', u'', parent_id=None,"
0.204.4 by James Westby
Make the inventory test match what the code produces.
134
            " revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
135
            "(u'data', False, InventoryFile('data', u'data',"
136
            " parent_id='TREE_ROOT',"
0.204.4 by James Westby
Make the inventory test match what the code produces.
137
            " sha1='aa785adca3fcdfe1884ae840e13c6d294a2414e8', len=5))\n"
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
138
            "(u'executable', True, InventoryFile('executable', u'executable',"
139
            " parent_id='TREE_ROOT',"
0.204.4 by James Westby
Make the inventory test match what the code produces.
140
            " sha1='040f06fd774092478d450774f5ba30c5da78acc8', len=7))\n"
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
141
            "(u'link', False, InventoryLink('link', u'link',"
0.204.4 by James Westby
Make the inventory test match what the code produces.
142
            " parent_id='TREE_ROOT', revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
143
            "(u'subdir', False, InventoryDirectory('subdir', u'subdir',"
0.204.4 by James Westby
Make the inventory test match what the code produces.
144
            " parent_id='TREE_ROOT', revision='git-experimental-r:69c39cfa65962f3cf16b9b3eb08a15954e9d8590'))\n"
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
145
            "(u'subdir/subfile', False, InventoryFile('subdir/subfile',"
146
            " u'subfile', parent_id='subdir',"
0.204.4 by James Westby
Make the inventory test match what the code produces.
147
            " sha1='67b75c3e49f31fcadddbf9df6a1d8be8c3e44290', len=12))")
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
148
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
149
150
class MemoryGitRepository(git_repository.GitRepository):
151
    """A git repository without real git data on disk."""
152
153
    @classmethod
154
    def _make_model(klass, transport):
155
        return None
156
157
158
class MemoryGitDir(git_dir.GitDir):
159
    """A git tree with real data on disk."""
160
161
    _gitrepository_class = MemoryGitRepository
162
163
164
class MemoryGitBzrDirFormat(git_dir.GitBzrDirFormat):
165
    """Format for a git tree without real data on disk."""
166
167
    _gitdir_class = MemoryGitDir
168
169
0.204.2 by James Westby
Switch to TestCaseWithTransport so that the cache dir can be
170
class TestGitRepository(tests.TestCaseWithTransport):
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
171
172
    def setUp(self):
0.204.2 by James Westby
Switch to TestCaseWithTransport so that the cache dir can be
173
        tests.TestCaseWithTransport.setUp(self)
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
174
        self.transport = self.get_transport()
175
        self.transport.mkdir('.git')
176
        self.git_dir = MemoryGitBzrDirFormat().open(self.transport)
177
        self.git_repo = self.git_dir.open_repository()
178
0.200.40 by David Allouche
GitRepository.supports_rich_root() => False
179
    def test_supports_rich_root(self):
180
        # GitRepository.supports_rich_root is False, at least for now.
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
181
        repo = self.git_repo
0.200.40 by David Allouche
GitRepository.supports_rich_root() => False
182
        self.assertEqual(repo.supports_rich_root(), False)
183
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
184
    def assertIsNullInventory(self, inv):
185
        self.assertEqual(inv.root, None)
186
        self.assertEqual(inv.revision_id, revision.NULL_REVISION)
187
        self.assertEqual(list(inv.iter_entries()), [])
188
189
    def test_get_inventory_none(self):
190
        # GitRepository.get_inventory(None) returns the null inventory.
191
        repo = self.git_repo
192
        inv = repo.get_inventory(None)
193
        self.assertIsNullInventory(inv)
194
195
    def test_revision_tree_none(self):
196
        # GitRepository.revision_tree(None) returns the null tree.
197
        repo = self.git_repo
198
        tree = repo.revision_tree(None)
199
        self.assertEqual(tree.get_revision_id(), revision.NULL_REVISION)
200
        self.assertIsNullInventory(tree.inventory)
201
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
202
203
class TestGitRepositoryParseRev(tests.TestCase):
204
    """Unit tests for GitRepository._parse_rev."""
205
206
    def test_base_commit(self):
207
        # GitRepository._parse_rev works for a simple base commit.
208
        rev = git_repository.GitRepository._parse_rev([
209
            "873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
210
            "tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
211
            "author Jane Bar <jane@bar.com> 1198784533 +0200\n",
212
            "committer Joe Foo <joe@foo.com> 1198784532 +0100\n",
213
            "\n",
214
            "    message\n",
215
            "\x00"])
0.204.3 by James Westby
More corrections for revision id change.
216
        self.assertEqual(rev.revision_id,
217
            'git-experimental-r:873a8ae0d682b0e63e9795bc53056d32ed3de93f')
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
218
        self.assertEqual(rev.parent_ids, [])
219
        self.assertEqual(rev.committer, 'Joe Foo <joe@foo.com>')
0.202.1 by David Allouche
GitRepository._parse_tz returns an int instead of a float.
220
        self.assertEqual(repr(rev.timestamp), '1198784532.0')
221
        self.assertEqual(repr(rev.timezone), '3600')
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
222
        self.assertEqual(rev.message, 'message\n')
223
        self.assertEqual(
224
            rev.properties,
225
            {'git-tree-id': 'aaff74984cccd156a469afa7d9ab10e4777beb24',
226
             'author': 'Jane Bar <jane@bar.com>',
227
             'git-author-timestamp': '1198784533',
228
             'git-author-timezone': '+0200'})
229
230
    def test_merge_commit(self):
231
        # Multi-parent commits (merges) are parsed correctly.
232
        rev = git_repository.GitRepository._parse_rev([
233
            "873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
234
            "tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
235
            "parent 263ed20f0d4898be994404ca418bafe8e89abb8a\n",
236
            "parent 546563eb8f3e94a557f3bb779b6e5a2bd9658752\n",
237
            "parent 3116d42db7b5c5e69e58f651721e179791479c23\n",
238
            "author Jane Bar <jane@bar.com> 1198784533 +0200\n",
239
            "committer Joe Foo <joe@foo.com> 1198784532 +0100\n",
240
            "\n",
241
            "    message\n",
242
            "\x00"])
243
        # Git records merges in the same way as bzr. The first parent is the
244
        # commit base, the following parents are the ordered merged revisions.
245
        self.assertEqual(
246
            rev.parent_ids,
0.204.3 by James Westby
More corrections for revision id change.
247
            ['git-experimental-r:263ed20f0d4898be994404ca418bafe8e89abb8a',
248
             'git-experimental-r:546563eb8f3e94a557f3bb779b6e5a2bd9658752',
249
             'git-experimental-r:3116d42db7b5c5e69e58f651721e179791479c23'])
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
250
251
    def test_redundant_spaces(self):
252
        # Redundant spaces in author and committer are preserved.
253
        rev = git_repository.GitRepository._parse_rev([
254
            "873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
255
            "tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
256
            "author  Jane  Bar  <jane@bar.com>  1198784533 +0200\n",
257
            "committer  Joe  Foo  <joe@foo.com>  1198784532 +0100\n",
258
            "\n",
259
            "    message\n",
260
            "\x00"])
261
        self.assertEqual(rev.committer, ' Joe  Foo  <joe@foo.com> ')
262
        self.assertEqual(
263
            rev.properties['author'], ' Jane  Bar  <jane@bar.com> ')
264
265
    def test_no_committer(self):
266
        # If committer is not set, then author is used.
267
        #
268
        # Folks in #git say that git fsck would likely accept commits that do
269
        # not set committer, but that author is a mandatory value.
270
        rev = git_repository.GitRepository._parse_rev([
271
            "873a8ae0d682b0e63e9795bc53056d32ed3de93f\n",
272
            "tree aaff74984cccd156a469afa7d9ab10e4777beb24\n",
273
            "author Jane Bar <jane@bar.com> 1198784533 +0200\n",
274
            "\n",
275
            "    message\n",
276
            "\x00"])
277
        self.assertEqual(rev.committer, 'Jane Bar <jane@bar.com>')
0.202.1 by David Allouche
GitRepository._parse_tz returns an int instead of a float.
278
        self.assertEqual(repr(rev.timestamp), '1198784533.0')
279
        self.assertEqual(repr(rev.timezone), '7200')
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
280
        self.assertEqual(rev.properties['author'], 'Jane Bar <jane@bar.com>')
281
        self.assertEqual(rev.properties['git-author-timestamp'], '1198784533')
282
        self.assertEqual(rev.properties['git-author-timezone'], '+0200')
0.200.35 by David Allouche
GitRepository._parse_rev sets Revision.timezone to a float instead of a string.
283
284
    def test_parse_tz(self):
285
        # Simple tests for the _parse_tz helper.
286
        parse_tz = git_repository.GitRepository._parse_tz
0.202.1 by David Allouche
GitRepository._parse_tz returns an int instead of a float.
287
        self.assertEqual(repr(parse_tz('+0000')), '0')
288
        self.assertEqual(repr(parse_tz('+0001')), '60')
289
        self.assertEqual(repr(parse_tz('-0001')), '-60')
290
        self.assertEqual(repr(parse_tz('+0100')), '3600')
291
        self.assertEqual(repr(parse_tz('-0100')), '-3600')
292
        self.assertEqual(repr(parse_tz('+9959')), '359940')
293
        self.assertEqual(repr(parse_tz('-9959')), '-359940')
0.200.35 by David Allouche
GitRepository._parse_rev sets Revision.timezone to a float instead of a string.
294