/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

Viewing changes to breezy/plugins/fastimport/tests/test_revision_store.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

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