/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
1
# Copyright (C) 2005, 2006, 2008 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
16
17
18
import sys
19
20
import bzrlib
21
from bzrlib import (
22
    errors,
4098.4.1 by Robert Collins
Handle inconsistent inventory data more gracefully at a small performance cost during fetch.
23
    inventory,
24
    osutils,
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
25
    repository,
4098.4.1 by Robert Collins
Handle inconsistent inventory data more gracefully at a small performance cost during fetch.
26
    versionedfile,
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
27
    )
28
from bzrlib.errors import (
29
    NoSuchRevision,
30
    )
31
from bzrlib.revision import (
32
    NULL_REVISION,
33
    Revision,
34
    )
35
from bzrlib.tests import (
36
    TestNotApplicable,
37
    )
38
from bzrlib.tests.interrepository_implementations import (
39
    TestCaseWithInterRepository,
40
    )
3616.2.3 by Mark Hammond
Fix test failures due to missing check_repo_format_for_funky_id_on_win32
41
from bzrlib.tests.interrepository_implementations.test_interrepository import (
42
    check_repo_format_for_funky_id_on_win32
43
    )
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
44
45
46
class TestInterRepository(TestCaseWithInterRepository):
47
48
    def test_fetch(self):
49
        tree_a = self.make_branch_and_tree('a')
50
        self.build_tree(['a/foo'])
51
        tree_a.add('foo', 'file1')
52
        tree_a.commit('rev1', rev_id='rev1')
53
        def check_push_rev1(repo):
54
            # ensure the revision is missing.
55
            self.assertRaises(NoSuchRevision, repo.get_revision, 'rev1')
4110.2.5 by Martin Pool
Deprecate passing pbs in to fetch()
56
            # fetch with a limit of NULL_REVISION
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
57
            repo.fetch(tree_a.branch.repository,
4110.2.5 by Martin Pool
Deprecate passing pbs in to fetch()
58
                       revision_id=NULL_REVISION)
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
59
            # nothing should have been pushed
60
            self.assertFalse(repo.has_revision('rev1'))
61
            # fetch with a default limit (grab everything)
62
            repo.fetch(tree_a.branch.repository)
63
            # check that b now has all the data from a's first commit.
64
            rev = repo.get_revision('rev1')
65
            tree = repo.revision_tree('rev1')
66
            tree.lock_read()
67
            self.addCleanup(tree.unlock)
68
            tree.get_file_text('file1')
69
            for file_id in tree:
70
                if tree.inventory[file_id].kind == "file":
71
                    tree.get_file(file_id).read()
72
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
73
        # makes a target version repo
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
74
        repo_b = self.make_to_repository('b')
75
        check_push_rev1(repo_b)
76
4098.4.1 by Robert Collins
Handle inconsistent inventory data more gracefully at a small performance cost during fetch.
77
    def test_fetch_inconsistent_last_changed_entries(self):
78
        """If an inventory has odd data we should still get what it references.
4104.4.2 by Robert Collins
Fix test_source for 1.13 landing.
79
4098.4.1 by Robert Collins
Handle inconsistent inventory data more gracefully at a small performance cost during fetch.
80
        This test tests that we do fetch a file text created in a revision not
81
        being fetched, but referenced from the revision we are fetching when the
82
        adjacent revisions to the one being fetched do not reference that text.
83
        """
84
        tree = self.make_branch_and_tree('source')
85
        revid = tree.commit('old')
86
        to_repo = self.make_to_repository('to_repo')
87
        to_repo.fetch(tree.branch.repository, revid)
88
        # Make a broken revision and fetch it.
89
        source = tree.branch.repository
90
        source.lock_write()
91
        self.addCleanup(source.unlock)
92
        source.start_write_group()
93
        try:
94
            # We need two revisions: OLD and NEW. NEW will claim to need a file
95
            # 'FOO' changed in 'OLD'. OLD will not have that file at all.
96
            source.texts.insert_record_stream([
97
                versionedfile.FulltextContentFactory(('foo', revid), (), None,
98
                'contents')])
99
            basis = source.revision_tree(revid)
100
            parent_id = basis.path2id('')
101
            entry = inventory.make_entry('file', 'foo-path', parent_id, 'foo')
102
            entry.revision = revid
103
            entry.text_size = len('contents')
104
            entry.text_sha1 = osutils.sha_string('contents')
105
            inv_sha1, _ = source.add_inventory_by_delta(revid, [
106
                (None, 'foo-path', 'foo', entry)], 'new', [revid])
107
            rev = Revision(timestamp=0,
108
                           timezone=None,
109
                           committer="Foo Bar <foo@example.com>",
110
                           message="Message",
111
                           inventory_sha1=inv_sha1,
112
                           revision_id='new',
113
                           parent_ids=[revid])
114
            source.add_revision(rev.revision_id, rev)
115
        except:
116
            source.abort_write_group()
117
            raise
118
        else:
119
            source.commit_write_group()
120
        to_repo.fetch(source, 'new')
121
        to_repo.lock_read()
122
        self.addCleanup(to_repo.unlock)
123
        self.assertEqual('contents',
124
            to_repo.texts.get_record_stream([('foo', revid)],
125
            'unordered', True).next().get_bytes_as('fulltext'))
126
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
127
    def test_fetch_missing_basis_text(self):
128
        """If fetching a delta, we should die if a basis is not present."""
129
        tree = self.make_branch_and_tree('tree')
130
        self.build_tree(['tree/a'])
131
        tree.add(['a'], ['a-id'])
132
        tree.commit('one', rev_id='rev-one')
133
        self.build_tree_contents([('tree/a', 'new contents\n')])
134
        tree.commit('two', rev_id='rev-two')
135
136
        to_repo = self.make_to_repository('to_repo')
137
        # We build a broken revision so that we can test the fetch code dies
138
        # properly. So copy the inventory and revision, but not the text.
139
        to_repo.lock_write()
140
        try:
141
            to_repo.start_write_group()
142
            inv = tree.branch.repository.get_inventory('rev-one')
143
            to_repo.add_inventory('rev-one', inv, [])
144
            rev = tree.branch.repository.get_revision('rev-one')
145
            to_repo.add_revision('rev-one', rev, inv=inv)
146
            to_repo.commit_write_group()
147
        finally:
148
            to_repo.unlock()
149
3350.3.21 by Robert Collins
Merge bzr.dev.
150
        # Implementations can either ensure that the target of the delta is
151
        # reconstructable, or raise an exception (which stream based copies
152
        # generally do).
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
153
        try:
154
            to_repo.fetch(tree.branch.repository, 'rev-two')
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
155
        except (errors.BzrCheckError, errors.RevisionNotPresent), e:
156
            # If an exception is raised, the revision should not be in the
157
            # target.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
158
            #
3830.3.9 by Martin Pool
Simplify kvf insert_record_stream; add has_key shorthand methods; update stacking effort tests
159
            # Can also just raise a generic check errors; stream insertion
160
            # does this to include all the missing data
161
            self.assertRaises((errors.NoSuchRevision, errors.RevisionNotPresent),
162
                              to_repo.revision_tree, 'rev-two')
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
163
        else:
3350.3.21 by Robert Collins
Merge bzr.dev.
164
            # If not exception is raised, then the text should be
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
165
            # available.
166
            to_repo.lock_read()
167
            try:
3350.3.21 by Robert Collins
Merge bzr.dev.
168
                rt = to_repo.revision_tree('rev-two')
169
                self.assertEqual('new contents\n',
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
170
                                 rt.get_file_text('a-id'))
171
            finally:
172
                to_repo.unlock()
173
174
    def test_fetch_missing_revision_same_location_fails(self):
175
        repo_a = self.make_repository('.')
176
        repo_b = repository.Repository.open('.')
177
        try:
3350.3.21 by Robert Collins
Merge bzr.dev.
178
            self.assertRaises(errors.NoSuchRevision, repo_b.fetch, repo_a, revision_id='XXX')
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
179
        except errors.LockError, e:
180
            check_old_format_lock_error(self.repository_format)
181
182
    def test_fetch_same_location_trivial_works(self):
183
        repo_a = self.make_repository('.')
184
        repo_b = repository.Repository.open('.')
185
        try:
186
            repo_a.fetch(repo_b)
187
        except errors.LockError, e:
188
            check_old_format_lock_error(self.repository_format)
189
190
    def test_fetch_missing_text_other_location_fails(self):
191
        source_tree = self.make_branch_and_tree('source')
192
        source = source_tree.branch.repository
193
        target = self.make_to_repository('target')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
194
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
195
        # start by adding a file so the data knit for the file exists in
196
        # repositories that have specific files for each fileid.
197
        self.build_tree(['source/id'])
198
        source_tree.add(['id'], ['id'])
199
        source_tree.commit('a', rev_id='a')
200
        # now we manually insert a revision with an inventory referencing
201
        # 'id' at revision 'b', but we do not insert revision b.
202
        # this should ensure that the new versions of files are being checked
203
        # for during pull operations
204
        inv = source.get_inventory('a')
205
        source.lock_write()
3380.1.5 by Aaron Bentley
Merge with make-it-work
206
        self.addCleanup(source.unlock)
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
207
        source.start_write_group()
208
        inv['id'].revision = 'b'
209
        inv.revision_id = 'b'
210
        sha1 = source.add_inventory('b', inv, ['a'])
211
        rev = Revision(timestamp=0,
212
                       timezone=None,
213
                       committer="Foo Bar <foo@example.com>",
214
                       message="Message",
215
                       inventory_sha1=sha1,
216
                       revision_id='b')
217
        rev.parent_ids = ['a']
218
        source.add_revision('b', rev)
219
        source.commit_write_group()
220
        self.assertRaises(errors.RevisionNotPresent, target.fetch, source)
221
        self.assertFalse(target.has_revision('b'))
222
223
    def test_fetch_funky_file_id(self):
224
        from_tree = self.make_branch_and_tree('tree')
225
        if sys.platform == 'win32':
226
            from_repo = from_tree.branch.repository
227
            check_repo_format_for_funky_id_on_win32(from_repo)
228
        self.build_tree(['tree/filename'])
229
        from_tree.add('filename', 'funky-chars<>%&;"\'')
230
        from_tree.commit('commit filename')
231
        to_repo = self.make_to_repository('to')
3350.3.21 by Robert Collins
Merge bzr.dev.
232
        to_repo.fetch(from_tree.branch.repository, from_tree.get_parent_ids()[0])
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
233
3380.1.6 by Aaron Bentley
Ensure fetching munges sha1s
234
    def test_fetch_revision_hash(self):
235
        """Ensure that inventory hashes are updated by fetch"""
236
        from_tree = self.make_branch_and_tree('tree')
237
        from_tree.commit('foo', rev_id='foo-id')
238
        to_repo = self.make_to_repository('to')
239
        to_repo.fetch(from_tree.branch.repository)
240
        recorded_inv_sha1 = to_repo.get_inventory_sha1('foo-id')
241
        xml = to_repo.get_inventory_xml('foo-id')
242
        computed_inv_sha1 = osutils.sha_string(xml)
243
        self.assertEqual(computed_inv_sha1, recorded_inv_sha1)
244
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
245
246
class TestFetchDependentData(TestCaseWithInterRepository):
247
248
    def test_reference(self):
249
        from_tree = self.make_branch_and_tree('tree')
250
        to_repo = self.make_to_repository('to')
251
        if (not from_tree.supports_tree_reference() or
252
            not from_tree.branch.repository._format.supports_tree_reference or
253
            not to_repo._format.supports_tree_reference):
254
            raise TestNotApplicable("Need subtree support.")
255
        subtree = self.make_branch_and_tree('tree/subtree')
256
        subtree.commit('subrev 1')
257
        from_tree.add_reference(subtree)
258
        tree_rev = from_tree.commit('foo')
259
        # now from_tree has a last-modified of subtree of the rev id of the
260
        # commit for foo, and a reference revision of the rev id of the commit
261
        # for subrev 1
262
        to_repo.fetch(from_tree.branch.repository, tree_rev)
263
        # to_repo should have a file_graph for from_tree.path2id('subtree') and
264
        # revid tree_rev.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
265
        file_id = from_tree.path2id('subtree')
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
266
        to_repo.lock_read()
267
        try:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
268
            self.assertEqual({(file_id, tree_rev):()},
269
                to_repo.texts.get_parent_map([(file_id, tree_rev)]))
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
270
        finally:
271
            to_repo.unlock()