1 # Copyright (C) 2008, 2009 Canonical Ltd
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.
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.
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/>.
16 """Direct tests of the revision_store classes."""
26 from bzrlib
.plugins
.fastimport
import (
29 from bzrlib
.plugins
.fastimport
.tests
import (
34 class Test_TreeShim(tests
.TestCase
):
36 _test_needs_features
= [FastimportFeature
]
38 def invAddEntry(self
, inv
, path
, file_id
=None):
39 if path
.endswith('/'):
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
))
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')
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')
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),
72 shim
= revision_store
._TreeShim
(repo
=None, basis_inv
=basis_inv
,
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')
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
87 self
.assertRaises(NotImplementedError, shim
.path2id
, 'bar')
89 def test_get_file_with_stat_content_in_stream(self
):
90 basis_inv
= self
.make_trivial_basis_inv()
92 def content_provider(file_id
):
93 return 'content of\n' + file_id
+ '\n'
95 shim
= revision_store
._TreeShim
(repo
=None, basis_inv
=basis_inv
,
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())
102 # TODO: Test when the content isn't in the stream, and we fall back to the
103 # repository that was passed in
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'
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'))
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
,
121 content_provider
=None)
122 self
.assertEqual(u
'link-target', shim
.get_symlink_target('link-id'))
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',
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
),
134 shim
= revision_store
._TreeShim
(repo
=None, basis_inv
=basis_inv
,
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)),
148 # from pprint import pformat
149 # self.assertEqualDiff(pformat(expected), pformat(changes))
150 self
.assertEqual(expected
, changes
)