/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.115.12 by John Arbash Meinel
Add a bunch of direct tests for the _TreeShim interface.
1
# Copyright (C) 2008, 2009 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
0.64.334 by Jelmer Vernooij
Remove old FSF address. Thanks Dan Callaghan.
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0.115.12 by John Arbash Meinel
Add a bunch of direct tests for the _TreeShim interface.
15
16
"""Direct tests of the revision_store classes."""
17
18
from bzrlib import (
19
    branch,
20
    errors,
21
    inventory,
22
    osutils,
23
    tests,
24
    )
25
26
from bzrlib.plugins.fastimport import (
27
    revision_store,
28
    )
0.123.13 by Jelmer Vernooij
Check for availability of fastimport before running tests.
29
from bzrlib.plugins.fastimport.tests import (
30
    FastimportFeature,
31
    )
0.115.12 by John Arbash Meinel
Add a bunch of direct tests for the _TreeShim interface.
32
33
34
class Test_TreeShim(tests.TestCase):
35
0.123.13 by Jelmer Vernooij
Check for availability of fastimport before running tests.
36
    _test_needs_features = [FastimportFeature]
37
0.115.12 by John Arbash Meinel
Add a bunch of direct tests for the _TreeShim interface.
38
    def invAddEntry(self, inv, path, file_id=None):
39
        if path.endswith('/'):
40
            path = path[:-1]
41
            kind = 'directory'
42
        else:
43
            kind = 'file'
44
        parent_path, basename = osutils.split(path)
45
        parent_id = inv.path2id(parent_path)
46
        inv.add(inventory.make_entry(kind, basename, parent_id, file_id))
47
48
    def make_trivial_basis_inv(self):
49
        basis_inv = inventory.Inventory('TREE_ROOT')
50
        self.invAddEntry(basis_inv, 'foo', 'foo-id')
51
        self.invAddEntry(basis_inv, 'bar/', 'bar-id')
52
        self.invAddEntry(basis_inv, 'bar/baz', 'baz-id')
53
        return basis_inv
0.123.13 by Jelmer Vernooij
Check for availability of fastimport before running tests.
54
0.115.12 by John Arbash Meinel
Add a bunch of direct tests for the _TreeShim interface.
55
    def test_id2path_no_delta(self):
56
        basis_inv = self.make_trivial_basis_inv()
57
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
58
                                        inv_delta=[], content_provider=None)
59
        self.assertEqual('', shim.id2path('TREE_ROOT'))
60
        self.assertEqual('foo', shim.id2path('foo-id'))
61
        self.assertEqual('bar', shim.id2path('bar-id'))
62
        self.assertEqual('bar/baz', shim.id2path('baz-id'))
63
        self.assertRaises(errors.NoSuchId, shim.id2path, 'qux-id')
64
65
    def test_id2path_with_delta(self):
66
        basis_inv = self.make_trivial_basis_inv()
67
        foo_entry = inventory.make_entry('file', 'foo2', 'TREE_ROOT', 'foo-id')
68
        inv_delta = [('foo', 'foo2', 'foo-id', foo_entry),
69
                     ('bar/baz', None, 'baz-id', None),
70
                    ]
71
72
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
73
                                        inv_delta=inv_delta,
74
                                        content_provider=None)
75
        self.assertEqual('', shim.id2path('TREE_ROOT'))
76
        self.assertEqual('foo2', shim.id2path('foo-id'))
77
        self.assertEqual('bar', shim.id2path('bar-id'))
78
        self.assertRaises(errors.NoSuchId, shim.id2path, 'baz-id')
79
80
    def test_path2id(self):
81
        basis_inv = self.make_trivial_basis_inv()
82
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
83
                                        inv_delta=[], content_provider=None)
84
        self.assertEqual('TREE_ROOT', shim.path2id(''))
85
        # We don't want to ever give a wrong value, so for now we just raise
86
        # NotImplementedError
87
        self.assertRaises(NotImplementedError, shim.path2id, 'bar')
88
89
    def test_get_file_with_stat_content_in_stream(self):
90
        basis_inv = self.make_trivial_basis_inv()
91
92
        def content_provider(file_id):
93
            return 'content of\n' + file_id + '\n'
0.123.13 by Jelmer Vernooij
Check for availability of fastimport before running tests.
94
0.115.12 by John Arbash Meinel
Add a bunch of direct tests for the _TreeShim interface.
95
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
96
                                        inv_delta=[],
97
                                        content_provider=content_provider)
98
        f_obj, stat_val = shim.get_file_with_stat('baz-id')
99
        self.assertIs(None, stat_val)
100
        self.assertEqualDiff('content of\nbaz-id\n', f_obj.read())
101
102
    # TODO: Test when the content isn't in the stream, and we fall back to the
103
    #       repository that was passed in
104
105
    def test_get_symlink_target(self):
106
        basis_inv = self.make_trivial_basis_inv()
107
        ie = inventory.make_entry('symlink', 'link', 'TREE_ROOT', 'link-id')
108
        ie.symlink_target = u'link-target'
109
        basis_inv.add(ie)
110
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
111
                                        inv_delta=[], content_provider=None)
112
        self.assertEqual(u'link-target', shim.get_symlink_target('link-id'))
113
114
    def test_get_symlink_target_from_delta(self):
115
        basis_inv = self.make_trivial_basis_inv()
116
        ie = inventory.make_entry('symlink', 'link', 'TREE_ROOT', 'link-id')
117
        ie.symlink_target = u'link-target'
118
        inv_delta = [(None, 'link', 'link-id', ie)]
119
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
120
                                        inv_delta=inv_delta,
121
                                        content_provider=None)
122
        self.assertEqual(u'link-target', shim.get_symlink_target('link-id'))
123
124
    def test__delta_to_iter_changes(self):
125
        basis_inv = self.make_trivial_basis_inv()
126
        foo_entry = inventory.make_entry('file', 'foo2', 'bar-id', 'foo-id')
127
        link_entry = inventory.make_entry('symlink', 'link', 'TREE_ROOT',
128
                                          'link-id')
129
        link_entry.symlink_target = u'link-target'
130
        inv_delta = [('foo', 'bar/foo2', 'foo-id', foo_entry),
131
                     ('bar/baz', None, 'baz-id', None),
132
                     (None, 'link', 'link-id', link_entry),
133
                    ]
134
        shim = revision_store._TreeShim(repo=None, basis_inv=basis_inv,
135
                                        inv_delta=inv_delta,
136
                                        content_provider=None)
137
        changes = list(shim._delta_to_iter_changes())
138
        expected = [('foo-id', ('foo', 'bar/foo2'), False, (True, True),
139
                     ('TREE_ROOT', 'bar-id'), ('foo', 'foo2'),
140
                     ('file', 'file'), (False, False)),
141
                    ('baz-id', ('bar/baz', None), True, (True, False),
142
                     ('bar-id', None), ('baz', None),
143
                     ('file', None), (False, None)),
144
                    ('link-id', (None, 'link'), True, (False, True),
145
                     (None, 'TREE_ROOT'), (None, 'link'),
146
                     (None, 'symlink'), (None, False)),
147
                   ]
148
        # from pprint import pformat
149
        # self.assertEqualDiff(pformat(expected), pformat(changes))
150
        self.assertEqual(expected, changes)
0.123.13 by Jelmer Vernooij
Check for availability of fastimport before running tests.
151