~bzr-pqm/bzr/bzr.dev

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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
import sys
19
20
import bzrlib
21
from bzrlib import (
22
    errors,
23
    repository,
3380.1.6 by Aaron Bentley
Ensure fetching munges sha1s
24
    osutils,
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
25
    )
26
from bzrlib.errors import (
27
    NoSuchRevision,
28
    )
29
from bzrlib.revision import (
30
    NULL_REVISION,
31
    Revision,
32
    )
33
from bzrlib.tests import (
34
    TestNotApplicable,
35
    )
36
from bzrlib.tests.interrepository_implementations import (
37
    TestCaseWithInterRepository,
38
    )
3616.2.3 by Mark Hammond
Fix test failures due to missing check_repo_format_for_funky_id_on_win32
39
from bzrlib.tests.interrepository_implementations.test_interrepository import (
40
    check_repo_format_for_funky_id_on_win32
41
    )
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
42
43
44
class TestInterRepository(TestCaseWithInterRepository):
45
46
    def test_fetch(self):
47
        tree_a = self.make_branch_and_tree('a')
48
        self.build_tree(['a/foo'])
49
        tree_a.add('foo', 'file1')
50
        tree_a.commit('rev1', rev_id='rev1')
51
        def check_push_rev1(repo):
52
            # ensure the revision is missing.
53
            self.assertRaises(NoSuchRevision, repo.get_revision, 'rev1')
54
            # fetch with a limit of NULL_REVISION and an explicit progress bar.
55
            repo.fetch(tree_a.branch.repository,
56
                       revision_id=NULL_REVISION,
57
                       pb=bzrlib.progress.DummyProgress())
58
            # nothing should have been pushed
59
            self.assertFalse(repo.has_revision('rev1'))
60
            # fetch with a default limit (grab everything)
61
            repo.fetch(tree_a.branch.repository)
62
            # check that b now has all the data from a's first commit.
63
            rev = repo.get_revision('rev1')
64
            tree = repo.revision_tree('rev1')
65
            tree.lock_read()
66
            self.addCleanup(tree.unlock)
67
            tree.get_file_text('file1')
68
            for file_id in tree:
69
                if tree.inventory[file_id].kind == "file":
70
                    tree.get_file(file_id).read()
71
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
72
        # makes a target version repo
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
73
        repo_b = self.make_to_repository('b')
74
        check_push_rev1(repo_b)
75
76
    def test_fetch_missing_basis_text(self):
77
        """If fetching a delta, we should die if a basis is not present."""
78
        tree = self.make_branch_and_tree('tree')
79
        self.build_tree(['tree/a'])
80
        tree.add(['a'], ['a-id'])
81
        tree.commit('one', rev_id='rev-one')
82
        self.build_tree_contents([('tree/a', 'new contents\n')])
83
        tree.commit('two', rev_id='rev-two')
84
85
        to_repo = self.make_to_repository('to_repo')
86
        # We build a broken revision so that we can test the fetch code dies
87
        # properly. So copy the inventory and revision, but not the text.
88
        to_repo.lock_write()
89
        try:
90
            to_repo.start_write_group()
91
            inv = tree.branch.repository.get_inventory('rev-one')
92
            to_repo.add_inventory('rev-one', inv, [])
93
            rev = tree.branch.repository.get_revision('rev-one')
94
            to_repo.add_revision('rev-one', rev, inv=inv)
95
            to_repo.commit_write_group()
96
        finally:
97
            to_repo.unlock()
98
3350.3.21 by Robert Collins
Merge bzr.dev.
99
        # Implementations can either ensure that the target of the delta is
100
        # reconstructable, or raise an exception (which stream based copies
101
        # generally do).
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
102
        try:
103
            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
104
        except (errors.BzrCheckError, errors.RevisionNotPresent), e:
105
            # If an exception is raised, the revision should not be in the
106
            # target.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
107
            #
3830.3.9 by Martin Pool
Simplify kvf insert_record_stream; add has_key shorthand methods; update stacking effort tests
108
            # Can also just raise a generic check errors; stream insertion
109
            # does this to include all the missing data
110
            self.assertRaises((errors.NoSuchRevision, errors.RevisionNotPresent),
111
                              to_repo.revision_tree, 'rev-two')
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
112
        else:
3350.3.21 by Robert Collins
Merge bzr.dev.
113
            # If not exception is raised, then the text should be
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
114
            # available.
115
            to_repo.lock_read()
116
            try:
3350.3.21 by Robert Collins
Merge bzr.dev.
117
                rt = to_repo.revision_tree('rev-two')
118
                self.assertEqual('new contents\n',
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
119
                                 rt.get_file_text('a-id'))
120
            finally:
121
                to_repo.unlock()
122
123
    def test_fetch_missing_revision_same_location_fails(self):
124
        repo_a = self.make_repository('.')
125
        repo_b = repository.Repository.open('.')
126
        try:
3350.3.21 by Robert Collins
Merge bzr.dev.
127
            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
128
        except errors.LockError, e:
129
            check_old_format_lock_error(self.repository_format)
130
131
    def test_fetch_same_location_trivial_works(self):
132
        repo_a = self.make_repository('.')
133
        repo_b = repository.Repository.open('.')
134
        try:
135
            repo_a.fetch(repo_b)
136
        except errors.LockError, e:
137
            check_old_format_lock_error(self.repository_format)
138
139
    def test_fetch_missing_text_other_location_fails(self):
140
        source_tree = self.make_branch_and_tree('source')
141
        source = source_tree.branch.repository
142
        target = self.make_to_repository('target')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
143
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
144
        # start by adding a file so the data knit for the file exists in
145
        # repositories that have specific files for each fileid.
146
        self.build_tree(['source/id'])
147
        source_tree.add(['id'], ['id'])
148
        source_tree.commit('a', rev_id='a')
149
        # now we manually insert a revision with an inventory referencing
150
        # 'id' at revision 'b', but we do not insert revision b.
151
        # this should ensure that the new versions of files are being checked
152
        # for during pull operations
153
        inv = source.get_inventory('a')
154
        source.lock_write()
3380.1.5 by Aaron Bentley
Merge with make-it-work
155
        self.addCleanup(source.unlock)
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
156
        source.start_write_group()
157
        inv['id'].revision = 'b'
158
        inv.revision_id = 'b'
159
        sha1 = source.add_inventory('b', inv, ['a'])
160
        rev = Revision(timestamp=0,
161
                       timezone=None,
162
                       committer="Foo Bar <foo@example.com>",
163
                       message="Message",
164
                       inventory_sha1=sha1,
165
                       revision_id='b')
166
        rev.parent_ids = ['a']
167
        source.add_revision('b', rev)
168
        source.commit_write_group()
169
        self.assertRaises(errors.RevisionNotPresent, target.fetch, source)
170
        self.assertFalse(target.has_revision('b'))
171
172
    def test_fetch_funky_file_id(self):
173
        from_tree = self.make_branch_and_tree('tree')
174
        if sys.platform == 'win32':
175
            from_repo = from_tree.branch.repository
176
            check_repo_format_for_funky_id_on_win32(from_repo)
177
        self.build_tree(['tree/filename'])
178
        from_tree.add('filename', 'funky-chars<>%&;"\'')
179
        from_tree.commit('commit filename')
180
        to_repo = self.make_to_repository('to')
3350.3.21 by Robert Collins
Merge bzr.dev.
181
        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
182
3380.1.6 by Aaron Bentley
Ensure fetching munges sha1s
183
    def test_fetch_revision_hash(self):
184
        """Ensure that inventory hashes are updated by fetch"""
185
        from_tree = self.make_branch_and_tree('tree')
186
        from_tree.commit('foo', rev_id='foo-id')
187
        to_repo = self.make_to_repository('to')
188
        to_repo.fetch(from_tree.branch.repository)
189
        recorded_inv_sha1 = to_repo.get_inventory_sha1('foo-id')
190
        xml = to_repo.get_inventory_xml('foo-id')
191
        computed_inv_sha1 = osutils.sha_string(xml)
192
        self.assertEqual(computed_inv_sha1, recorded_inv_sha1)
193
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
194
195
class TestFetchDependentData(TestCaseWithInterRepository):
196
197
    def test_reference(self):
198
        from_tree = self.make_branch_and_tree('tree')
199
        to_repo = self.make_to_repository('to')
200
        if (not from_tree.supports_tree_reference() or
201
            not from_tree.branch.repository._format.supports_tree_reference or
202
            not to_repo._format.supports_tree_reference):
203
            raise TestNotApplicable("Need subtree support.")
204
        subtree = self.make_branch_and_tree('tree/subtree')
205
        subtree.commit('subrev 1')
206
        from_tree.add_reference(subtree)
207
        tree_rev = from_tree.commit('foo')
208
        # now from_tree has a last-modified of subtree of the rev id of the
209
        # commit for foo, and a reference revision of the rev id of the commit
210
        # for subrev 1
211
        to_repo.fetch(from_tree.branch.repository, tree_rev)
212
        # to_repo should have a file_graph for from_tree.path2id('subtree') and
213
        # 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.
214
        file_id = from_tree.path2id('subtree')
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
215
        to_repo.lock_read()
216
        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.
217
            self.assertEqual({(file_id, tree_rev):()},
218
                to_repo.texts.get_parent_map([(file_id, tree_rev)]))
3380.1.4 by Aaron Bentley
Split interrepository fetch tests into their own file
219
        finally:
220
            to_repo.unlock()