/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3221.11.3 by Robert Collins
Add missing test script.
1
# Copyright (C) 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
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
17
"""Tests for Branch.get_stacked_on_url and set_stacked_on_url."""
3221.11.3 by Robert Collins
Add missing test script.
18
3517.4.12 by Martin Pool
Clearer per-branch-format tests for stacking
19
from bzrlib import (
3770.2.1 by Michael Hudson
test and feature
20
    branch,
3517.4.12 by Martin Pool
Clearer per-branch-format tests for stacking
21
    bzrdir,
22
    errors,
23
    )
24
from bzrlib.revision import NULL_REVISION
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
25
from bzrlib.tests import TestNotApplicable, KnownFailure
3221.11.3 by Robert Collins
Add missing test script.
26
from bzrlib.tests.branch_implementations import TestCaseWithBranch
27
28
29
class TestStacking(TestCaseWithBranch):
30
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
31
    def test_get_set_stacked_on_url(self):
3221.11.3 by Robert Collins
Add missing test script.
32
        # branches must either:
33
        # raise UnstackableBranchFormat or
34
        # raise UnstackableRepositoryFormat or
35
        # permit stacking to be done and then return the stacked location.
36
        branch = self.make_branch('branch')
37
        target = self.make_branch('target')
38
        old_format_errors = (
39
            errors.UnstackableBranchFormat,
40
            errors.UnstackableRepositoryFormat,
41
            )
42
        try:
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
43
            branch.set_stacked_on_url(target.base)
3221.11.3 by Robert Collins
Add missing test script.
44
        except old_format_errors:
45
            # if the set failed, so must the get
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
46
            self.assertRaises(old_format_errors, branch.get_stacked_on_url)
3221.11.3 by Robert Collins
Add missing test script.
47
            return
48
        # now we have a stacked branch:
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
49
        self.assertEqual(target.base, branch.get_stacked_on_url())
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
50
        branch.set_stacked_on_url(None)
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
51
        self.assertRaises(errors.NotStacked, branch.get_stacked_on_url)
3221.13.2 by Robert Collins
Add a shallow parameter to bzrdir.sprout, which involved fixing a lateny bug in pack to pack fetching with ghost discovery.
52
3242.5.1 by Jonathan Lange
Allow stacked-on branch locations to be stored as relative URLs.
53
    def test_get_set_stacked_on_relative(self):
54
        # Branches can be stacked on other branches using relative paths.
55
        branch = self.make_branch('branch')
56
        target = self.make_branch('target')
57
        old_format_errors = (
58
            errors.UnstackableBranchFormat,
59
            errors.UnstackableRepositoryFormat,
60
            )
61
        try:
3537.3.5 by Martin Pool
merge trunk including stacking policy
62
            branch.set_stacked_on_url('../target')
3242.5.1 by Jonathan Lange
Allow stacked-on branch locations to be stored as relative URLs.
63
        except old_format_errors:
64
            # if the set failed, so must the get
3537.3.5 by Martin Pool
merge trunk including stacking policy
65
            self.assertRaises(old_format_errors, branch.get_stacked_on_url)
3242.5.1 by Jonathan Lange
Allow stacked-on branch locations to be stored as relative URLs.
66
            return
3537.3.5 by Martin Pool
merge trunk including stacking policy
67
        self.assertEqual('../target', branch.get_stacked_on_url())
3242.5.1 by Jonathan Lange
Allow stacked-on branch locations to be stored as relative URLs.
68
3517.4.12 by Martin Pool
Clearer per-branch-format tests for stacking
69
    def assertRevisionInRepository(self, repo_path, revid):
70
        """Check that a revision is in a repository, disregarding stacking."""
71
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
72
        self.assertTrue(repo.has_revision(revid))
73
74
    def assertRevisionNotInRepository(self, repo_path, revid):
75
        """Check that a revision is not in a repository, disregarding stacking."""
76
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
77
        self.assertFalse(repo.has_revision(revid))
78
79
    def test_get_graph_stacked(self):
80
        """A stacked repository shows the graph of its parent."""
81
        trunk_tree = self.make_branch_and_tree('mainline')
82
        trunk_revid = trunk_tree.commit('mainline')
83
        # make a new branch, and stack on the existing one.  we don't use
84
        # sprout(stacked=True) here because if that is buggy and copies data
85
        # it would cause a false pass of this test.
86
        new_branch = self.make_branch('new_branch')
87
        try:
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
88
            new_branch.set_stacked_on_url(trunk_tree.branch.base)
3517.4.12 by Martin Pool
Clearer per-branch-format tests for stacking
89
        except (errors.UnstackableBranchFormat,
90
            errors.UnstackableRepositoryFormat), e:
91
            raise TestNotApplicable(e)
92
        # reading the graph from the stacked branch's repository should see
93
        # data from the stacked-on branch
94
        new_repo = new_branch.repository
95
        new_repo.lock_read()
96
        try:
97
            self.assertEqual(new_repo.get_parent_map([trunk_revid]),
98
                {trunk_revid: (NULL_REVISION, )})
99
        finally:
100
            new_repo.unlock()
101
102
    def test_sprout_stacked(self):
3221.13.2 by Robert Collins
Add a shallow parameter to bzrdir.sprout, which involved fixing a lateny bug in pack to pack fetching with ghost discovery.
103
        # We have a mainline
104
        trunk_tree = self.make_branch_and_tree('mainline')
105
        trunk_revid = trunk_tree.commit('mainline')
3221.18.4 by Ian Clatworthy
shallow -> stacked
106
        # and make branch from it which is stacked
3221.13.2 by Robert Collins
Add a shallow parameter to bzrdir.sprout, which involved fixing a lateny bug in pack to pack fetching with ghost discovery.
107
        try:
3221.18.4 by Ian Clatworthy
shallow -> stacked
108
            new_dir = trunk_tree.bzrdir.sprout('newbranch', stacked=True)
109
        except (errors.UnstackableBranchFormat,
3517.4.12 by Martin Pool
Clearer per-branch-format tests for stacking
110
            errors.UnstackableRepositoryFormat), e:
111
            raise TestNotApplicable(e)
112
        # stacked repository
113
        self.assertRevisionNotInRepository('newbranch', trunk_revid)
3221.13.2 by Robert Collins
Add a shallow parameter to bzrdir.sprout, which involved fixing a lateny bug in pack to pack fetching with ghost discovery.
114
        new_tree = new_dir.open_workingtree()
3537.3.4 by Martin Pool
Improved branch stacking tests
115
        new_branch_revid = new_tree.commit('something local')
116
        self.assertRevisionNotInRepository('mainline', new_branch_revid)
117
        self.assertRevisionInRepository('newbranch', new_branch_revid)
118
119
    def test_unstack_fetches(self):
120
        """Removing the stacked-on branch pulls across all data"""
121
        # We have a mainline
122
        trunk_tree = self.make_branch_and_tree('mainline')
123
        trunk_revid = trunk_tree.commit('revision on mainline')
124
        # and make branch from it which is stacked
125
        try:
126
            new_dir = trunk_tree.bzrdir.sprout('newbranch', stacked=True)
127
        except (errors.UnstackableBranchFormat,
128
            errors.UnstackableRepositoryFormat), e:
129
            raise TestNotApplicable(e)
130
        # stacked repository
131
        self.assertRevisionNotInRepository('newbranch', trunk_revid)
132
        # now when we unstack that should implicitly fetch, to make sure that
133
        # the branch will still work
134
        new_branch = new_dir.open_branch()
135
        new_branch.set_stacked_on_url(None)
136
        self.assertRevisionInRepository('newbranch', trunk_revid)
137
        # of course it's still in the mainline
138
        self.assertRevisionInRepository('mainline', trunk_revid)
139
        # and now we're no longer stacked
3537.3.5 by Martin Pool
merge trunk including stacking policy
140
        self.assertRaises(errors.NotStacked,
141
            new_branch.get_stacked_on_url)
3221.13.2 by Robert Collins
Add a shallow parameter to bzrdir.sprout, which involved fixing a lateny bug in pack to pack fetching with ghost discovery.
142
3567.3.4 by Michael Hudson
make test a lot better
143
    def make_stacked_bzrdir(self, in_directory=None):
3567.3.7 by Michael Hudson
better docstring
144
        """Create a stacked branch and return its bzrdir.
3567.3.4 by Michael Hudson
make test a lot better
145
146
        :param in_directory: If not None, create a directory of this
147
            name and create the stacking and stacked-on bzrdirs in
148
            this directory.
149
        """
150
        if in_directory is not None:
151
            self.get_transport().mkdir(in_directory)
152
            prefix = in_directory + '/'
153
        else:
154
            prefix = ''
155
        tree = self.make_branch_and_tree(prefix + 'stacked-on')
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
156
        tree.commit('Added foo')
157
        stacked_bzrdir = tree.branch.bzrdir.sprout(
3567.3.4 by Michael Hudson
make test a lot better
158
            prefix + 'stacked', tree.branch.last_revision(), stacked=True)
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
159
        return stacked_bzrdir
160
161
    def test_clone_from_stacked_branch_preserve_stacking(self):
3242.3.24 by Aaron Bentley
Fix test failures
162
        # We can clone from the bzrdir of a stacked branch. If
163
        # preserve_stacking is True, the cloned branch is stacked on the
164
        # same branch as the original.
3242.3.21 by Jonathan Lange
Preserve stacking in clone
165
        try:
3567.3.4 by Michael Hudson
make test a lot better
166
            stacked_bzrdir = self.make_stacked_bzrdir()
3242.3.21 by Jonathan Lange
Preserve stacking in clone
167
        except (errors.UnstackableBranchFormat,
3567.3.5 by Michael Hudson
use TestNotApplicable in all the stacking clone() tests.
168
                errors.UnstackableRepositoryFormat), e:
3242.3.21 by Jonathan Lange
Preserve stacking in clone
169
            # not a testable combination.
3567.3.5 by Michael Hudson
use TestNotApplicable in all the stacking clone() tests.
170
            raise TestNotApplicable(e)
3242.3.24 by Aaron Bentley
Fix test failures
171
        cloned_bzrdir = stacked_bzrdir.clone('cloned', preserve_stacking=True)
3242.3.21 by Jonathan Lange
Preserve stacking in clone
172
        try:
173
            self.assertEqual(
3537.3.5 by Martin Pool
merge trunk including stacking policy
174
                stacked_bzrdir.open_branch().get_stacked_on_url(),
175
                cloned_bzrdir.open_branch().get_stacked_on_url())
3242.3.21 by Jonathan Lange
Preserve stacking in clone
176
        except (errors.UnstackableBranchFormat,
177
                errors.UnstackableRepositoryFormat):
178
            pass
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
179
3567.3.4 by Michael Hudson
make test a lot better
180
    def test_clone_from_branch_stacked_on_relative_url_preserve_stacking(self):
181
        # If a branch's stacked-on url is relative, we can still clone
182
        # from it with preserve_stacking True and get a branch stacked
183
        # on an appropriately adjusted relative url.
3567.3.3 by Michael Hudson
very hackish test
184
        try:
3567.3.4 by Michael Hudson
make test a lot better
185
            stacked_bzrdir = self.make_stacked_bzrdir(in_directory='dir')
3567.3.3 by Michael Hudson
very hackish test
186
        except (errors.UnstackableBranchFormat,
3567.3.6 by Michael Hudson
oops
187
                errors.UnstackableRepositoryFormat), e:
3567.3.3 by Michael Hudson
very hackish test
188
            # not a testable combination.
3567.3.5 by Michael Hudson
use TestNotApplicable in all the stacking clone() tests.
189
            raise TestNotApplicable(e)
3567.3.4 by Michael Hudson
make test a lot better
190
        stacked_bzrdir.open_branch().set_stacked_on_url('../stacked-on')
3567.3.3 by Michael Hudson
very hackish test
191
        cloned_bzrdir = stacked_bzrdir.clone('cloned', preserve_stacking=True)
3567.3.4 by Michael Hudson
make test a lot better
192
        self.assertEqual(
193
            '../dir/stacked-on',
194
            cloned_bzrdir.open_branch().get_stacked_on_url())
3567.3.3 by Michael Hudson
very hackish test
195
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
196
    def test_clone_from_stacked_branch_no_preserve_stacking(self):
197
        try:
3567.3.4 by Michael Hudson
make test a lot better
198
            stacked_bzrdir = self.make_stacked_bzrdir()
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
199
        except (errors.UnstackableBranchFormat,
3567.3.6 by Michael Hudson
oops
200
                errors.UnstackableRepositoryFormat), e:
3242.3.40 by Aaron Bentley
Turn failing test into KnownFailure
201
            # not a testable combination.
3567.3.5 by Michael Hudson
use TestNotApplicable in all the stacking clone() tests.
202
            raise TestNotApplicable(e)
1551.19.46 by Aaron Bentley
Fix fetch from stacked respositories (#248506)
203
        cloned_unstacked_bzrdir = stacked_bzrdir.clone('cloned-unstacked',
204
            preserve_stacking=False)
3242.3.24 by Aaron Bentley
Fix test failures
205
        unstacked_branch = cloned_unstacked_bzrdir.open_branch()
3242.3.37 by Aaron Bentley
Updates from reviews
206
        self.assertRaises((errors.NotStacked, errors.UnstackableBranchFormat),
3537.3.5 by Martin Pool
merge trunk including stacking policy
207
                          unstacked_branch.get_stacked_on_url)
3242.3.37 by Aaron Bentley
Updates from reviews
208
209
    def test_no_op_preserve_stacking(self):
210
        """With no stacking, preserve_stacking should be a no-op."""
211
        branch = self.make_branch('source')
212
        cloned_bzrdir = branch.bzrdir.clone('cloned', preserve_stacking=True)
213
        self.assertRaises((errors.NotStacked, errors.UnstackableBranchFormat),
3537.3.5 by Martin Pool
merge trunk including stacking policy
214
                          cloned_bzrdir.open_branch().get_stacked_on_url)
3242.3.37 by Aaron Bentley
Updates from reviews
215
216
    def test_sprout_stacking_policy_handling(self):
217
        """Obey policy where possible, ignore otherwise."""
218
        stack_on = self.make_branch('stack-on')
219
        parent_bzrdir = self.make_bzrdir('.', format='default')
220
        parent_bzrdir.get_config().set_default_stack_on('stack-on')
221
        source = self.make_branch('source')
222
        target = source.bzrdir.sprout('target').open_branch()
3641.1.1 by John Arbash Meinel
Merge in 1.6rc5 and revert disabling default stack on policy
223
        try:
224
            self.assertEqual('../stack-on', target.get_stacked_on_url())
225
        except errors.UnstackableBranchFormat:
226
            pass
3242.3.38 by Aaron Bentley
Enhance tests
227
228
    def test_clone_stacking_policy_handling(self):
229
        """Obey policy where possible, ignore otherwise."""
230
        stack_on = self.make_branch('stack-on')
231
        parent_bzrdir = self.make_bzrdir('.', format='default')
232
        parent_bzrdir.get_config().set_default_stack_on('stack-on')
233
        source = self.make_branch('source')
234
        target = source.bzrdir.clone('target').open_branch()
3641.1.1 by John Arbash Meinel
Merge in 1.6rc5 and revert disabling default stack on policy
235
        try:
236
            self.assertEqual('../stack-on', target.get_stacked_on_url())
237
        except errors.UnstackableBranchFormat:
238
            pass
1551.19.46 by Aaron Bentley
Fix fetch from stacked respositories (#248506)
239
1551.19.47 by Aaron Bentley
Add test per JAM
240
    def prepare_stacked_on_fetch(self):
1551.19.46 by Aaron Bentley
Fix fetch from stacked respositories (#248506)
241
        stack_on = self.make_branch_and_tree('stack-on')
242
        stack_on.commit('first commit', rev_id='rev1')
243
        try:
244
            stacked_dir = stack_on.bzrdir.sprout('stacked', stacked=True)
245
        except (errors.UnstackableRepositoryFormat,
246
                errors.UnstackableBranchFormat):
247
            raise TestNotApplicable('Format does not support stacking.')
248
        unstacked = self.make_repository('unstacked')
1551.19.47 by Aaron Bentley
Add test per JAM
249
        return stacked_dir.open_workingtree(), unstacked
250
251
    def test_fetch_copies_from_stacked_on(self):
252
        stacked, unstacked = self.prepare_stacked_on_fetch()
253
        unstacked.fetch(stacked.branch.repository, 'rev1')
254
        unstacked.get_revision('rev1')
255
256
    def test_fetch_copies_from_stacked_on_and_stacked(self):
257
        stacked, unstacked = self.prepare_stacked_on_fetch()
258
        stacked.commit('second commit', rev_id='rev2')
259
        unstacked.fetch(stacked.branch.repository, 'rev2')
260
        unstacked.get_revision('rev1')
261
        unstacked.get_revision('rev2')
3582.2.1 by Jonathan Lange
Fix up problems with fetching revisions. Almost entirely abentley's work.
262
3582.1.8 by Martin Pool
Add test for repeated commits into packed repository
263
    def test_autopack_when_stacked(self):
264
        # in bzr.dev as of 20080730, autopack was reported to fail in stacked
265
        # repositories because of problems with text deltas spanning physical
3582.1.10 by Martin Pool
Add failing tets for pull into stacked repository
266
        # repository boundaries.  however, i didn't actually get this test to
267
        # fail on that code. -- mbp
3582.1.11 by Martin Pool
doc
268
        # see https://bugs.launchpad.net/bzr/+bug/252821
3582.1.8 by Martin Pool
Add test for repeated commits into packed repository
269
        if not self.branch_format.supports_stacking():
270
            raise TestNotApplicable("%r does not support stacking"
271
                % self.branch_format)
272
        stack_on = self.make_branch_and_tree('stack-on')
3582.1.9 by Martin Pool
Make autopack-stacked test use larger files; still does not fail
273
        text_lines = ['line %d blah blah blah\n' % i for i in range(20)]
274
        self.build_tree_contents([('stack-on/a', ''.join(text_lines))])
3582.1.8 by Martin Pool
Add test for repeated commits into packed repository
275
        stack_on.add('a')
276
        stack_on.commit('base commit')
277
        stacked_dir = stack_on.bzrdir.sprout('stacked', stacked=True)
278
        stacked_tree = stacked_dir.open_workingtree()
279
        for i in range(20):
3582.1.9 by Martin Pool
Make autopack-stacked test use larger files; still does not fail
280
            text_lines[0] = 'changed in %d\n' % i
281
            self.build_tree_contents([('stacked/a', ''.join(text_lines))])
3582.1.8 by Martin Pool
Add test for repeated commits into packed repository
282
            stacked_tree.commit('commit %d' % i)
283
        stacked_tree.branch.repository.pack()
3582.1.10 by Martin Pool
Add failing tets for pull into stacked repository
284
        stacked_tree.branch.check()
285
286
    def test_pull_delta_when_stacked(self):
287
        if not self.branch_format.supports_stacking():
288
            raise TestNotApplicable("%r does not support stacking"
289
                % self.branch_format)
290
        stack_on = self.make_branch_and_tree('stack-on')
291
        text_lines = ['line %d blah blah blah\n' % i for i in range(20)]
292
        self.build_tree_contents([('stack-on/a', ''.join(text_lines))])
293
        stack_on.add('a')
294
        stack_on.commit('base commit')
295
        # make a stacked branch from the mainline
296
        stacked_dir = stack_on.bzrdir.sprout('stacked', stacked=True)
297
        stacked_tree = stacked_dir.open_workingtree()
298
        # make a second non-stacked branch from the mainline
299
        other_dir = stack_on.bzrdir.sprout('other')
300
        other_tree = other_dir.open_workingtree()
301
        text_lines[9] = 'changed in other\n'
302
        self.build_tree_contents([('other/a', ''.join(text_lines))])
303
        other_tree.commit('commit in other')
304
        # this should have generated a delta; try to pull that across
3582.1.15 by Martin Pool
Comments and tests for repository stacking
305
        # bug 252821 caused a RevisionNotPresent here...
306
        stacked_tree.pull(other_tree.branch)
3582.1.10 by Martin Pool
Add failing tets for pull into stacked repository
307
        stacked_tree.branch.repository.pack()
308
        stacked_tree.branch.check()
3582.1.12 by Martin Pool
merge from trunk
309
3582.2.1 by Jonathan Lange
Fix up problems with fetching revisions. Almost entirely abentley's work.
310
    def test_fetch_revisions_with_file_changes(self):
311
        # Fetching revisions including file changes into a stacked branch
312
        # works without error.
313
        # Make the source tree.
314
        src_tree = self.make_branch_and_tree('src')
315
        self.build_tree_contents([('src/a', 'content')])
316
        src_tree.add('a')
317
        src_tree.commit('first commit')
318
319
        # Make the stacked-on branch.
320
        src_tree.bzrdir.sprout('stacked-on')
321
322
        # Make a branch stacked on it.
323
        target = self.make_branch('target')
324
        try:
325
            target.set_stacked_on_url('../stacked-on')
326
        except (errors.UnstackableRepositoryFormat,
327
                errors.UnstackableBranchFormat):
328
            raise TestNotApplicable('Format does not support stacking.')
329
330
        # Change the source branch.
331
        self.build_tree_contents([('src/a', 'new content')])
332
        src_tree.commit('second commit', rev_id='rev2')
333
334
        # Fetch changes to the target.
335
        target.fetch(src_tree.branch)
336
        rtree = target.repository.revision_tree('rev2')
337
        rtree.lock_read()
338
        self.addCleanup(rtree.unlock)
339
        self.assertEqual('new content', rtree.get_file_by_path('a').read())
3770.2.1 by Michael Hudson
test and feature
340
341
    def test_transform_fallback_location_hook(self):
342
        # The 'transform_fallback_location' branch hook allows us to inspect
343
        # and transform the URL of the fallback location for the branch.
344
        stack_on = self.make_branch('stack-on')
345
        stacked = self.make_branch('stacked')
346
        try:
347
            stacked.set_stacked_on_url('../stack-on')
348
        except (errors.UnstackableRepositoryFormat,
349
                errors.UnstackableBranchFormat):
350
            raise TestNotApplicable('Format does not support stacking.')
351
        self.get_transport().rename('stack-on', 'new-stack-on')
352
        hook_calls = []
353
        def hook(stacked_branch, url):
354
            hook_calls.append(url)
355
            return '../new-stack-on'
356
        branch.Branch.hooks.install_named_hook(
357
            'transform_fallback_location', hook, None)
358
        branch.Branch.open('stacked')
359
        self.assertEqual(['../stack-on'], hook_calls)