~bzr-pqm/bzr/bzr.dev

6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.4.39 by Robert Collins
Basic BzrDir support.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.4.39 by Robert Collins
Basic BzrDir support.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for control directory implementations - tests a controldir format."""
18
19
from itertools import izip
20
21
import bzrlib.branch
22
from bzrlib import (
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
23
    bzrdir as _mod_bzrdir,
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
24
    check,
25
    controldir,
26
    errors,
27
    gpg,
28
    osutils,
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
29
    revision as _mod_revision,
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
30
    transport,
31
    ui,
32
    urlutils,
33
    workingtree,
34
    )
35
from bzrlib.tests import (
36
    fixtures,
37
    ChrootedTestCase,
38
    TestNotApplicable,
39
    TestSkipped,
40
    )
41
from bzrlib.tests.per_controldir import TestCaseWithControlDir
42
from bzrlib.transport.local import LocalTransport
43
from bzrlib.ui import (
44
    CannedInputUIFactory,
45
    )
46
from bzrlib.remote import (
47
    RemoteBzrDir,
48
    RemoteBzrDirFormat,
49
    RemoteRepository,
50
    )
51
52
53
class TestControlDir(TestCaseWithControlDir):
54
55
    def skipIfNoWorkingTree(self, a_bzrdir):
56
        """Raises TestSkipped if a_bzrdir doesn't have a working tree.
57
58
        If the bzrdir does have a workingtree, this is a no-op.
59
        """
60
        try:
61
            a_bzrdir.open_workingtree()
62
        except (errors.NotLocalUrl, errors.NoWorkingTree):
63
            raise TestSkipped("bzrdir on transport %r has no working tree"
64
                              % a_bzrdir.transport)
65
66
    def openWorkingTreeIfLocal(self, a_bzrdir):
67
        """If a_bzrdir is on a local transport, call open_workingtree() on it.
68
        """
69
        if not isinstance(a_bzrdir.root_transport, LocalTransport):
70
            # it's not local, but that's ok
71
            return
72
        a_bzrdir.open_workingtree()
73
74
    def createWorkingTreeOrSkip(self, a_bzrdir):
75
        """Create a working tree on a_bzrdir, or raise TestSkipped.
76
77
        A simple wrapper for create_workingtree that translates NotLocalUrl into
78
        TestSkipped.  Returns the newly created working tree.
79
        """
80
        try:
81
            return a_bzrdir.create_workingtree()
82
        except (errors.NotLocalUrl, errors.UnsupportedOperation):
83
            raise TestSkipped("cannot make working tree with transport %r"
84
                              % a_bzrdir.transport)
85
86
    def sproutOrSkip(self, from_bzrdir, to_url, revision_id=None,
87
                     force_new_repo=False, accelerator_tree=None,
88
                     create_tree_if_local=True):
89
        """Sprout from_bzrdir into to_url, or raise TestSkipped.
90
91
        A simple wrapper for from_bzrdir.sprout that translates NotLocalUrl into
92
        TestSkipped.  Returns the newly sprouted bzrdir.
93
        """
94
        to_transport = transport.get_transport(to_url)
95
        if not isinstance(to_transport, LocalTransport):
96
            raise TestSkipped('Cannot sprout to remote bzrdirs.')
97
        target = from_bzrdir.sprout(to_url, revision_id=revision_id,
98
                                    force_new_repo=force_new_repo,
99
                                    possible_transports=[to_transport],
100
                                    accelerator_tree=accelerator_tree,
101
                                    create_tree_if_local=create_tree_if_local)
102
        return target
103
6162.3.6 by Jelmer Vernooij
Add test for uninitializable formats.
104
    def test_uninitializable(self):
105
        if self.bzrdir_format.is_initializable():
106
            raise TestNotApplicable("format is initializable")
107
        t = self.get_transport()
108
        self.assertRaises(errors.UninitializableFormat,
109
            self.bzrdir_format.initialize, t.base)
110
6437.10.2 by Jelmer Vernooij
Raise AlreadyControlDirError.
111
    def test_multiple_initialization(self):
112
        # loopback test to check the current format initializes to itself.
113
        if not self.bzrdir_format.is_initializable():
114
            # unsupported formats are not loopback testable
115
            # because the default open will not open them and
116
            # they may not be initializable.
117
            raise TestNotApplicable("format is not initializable")
118
        self.bzrdir_format.initialize('.')
119
        self.assertRaises(errors.AlreadyControlDirError,
120
            self.bzrdir_format.initialize, '.')
121
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
122
    def test_create_null_workingtree(self):
123
        dir = self.make_bzrdir('dir1')
124
        dir.create_repository()
125
        dir.create_branch()
126
        try:
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
127
            wt = dir.create_workingtree(revision_id=_mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
128
        except (errors.NotLocalUrl, errors.UnsupportedOperation):
129
            raise TestSkipped("cannot make working tree with transport %r"
130
                              % dir.transport)
131
        self.assertEqual([], wt.get_parent_ids())
132
133
    def test_destroy_workingtree(self):
134
        tree = self.make_branch_and_tree('tree')
135
        self.build_tree(['tree/file'])
136
        tree.add('file')
137
        tree.commit('first commit')
138
        bzrdir = tree.bzrdir
139
        try:
140
            bzrdir.destroy_workingtree()
141
        except errors.UnsupportedOperation:
142
            raise TestSkipped('Format does not support destroying tree')
143
        self.assertPathDoesNotExist('tree/file')
144
        self.assertRaises(errors.NoWorkingTree, bzrdir.open_workingtree)
145
        bzrdir.create_workingtree()
146
        self.assertPathExists('tree/file')
147
        bzrdir.destroy_workingtree_metadata()
148
        self.assertPathExists('tree/file')
149
        self.assertRaises(errors.NoWorkingTree, bzrdir.open_workingtree)
150
151
    def test_destroy_branch(self):
152
        branch = self.make_branch('branch')
153
        bzrdir = branch.bzrdir
154
        try:
155
            bzrdir.destroy_branch()
156
        except (errors.UnsupportedOperation, errors.TransportNotPossible):
157
            raise TestNotApplicable('Format does not support destroying branch')
158
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
159
        bzrdir.create_branch()
160
        bzrdir.open_branch()
161
6437.22.1 by Jelmer Vernooij
Except ControlDir.destroy_branch to raise NotBranchError if the branch did not exist.
162
    def test_destroy_branch_no_branch(self):
163
        branch = self.make_repository('branch')
164
        bzrdir = branch.bzrdir
165
        try:
166
            self.assertRaises(errors.NotBranchError, bzrdir.destroy_branch)
167
        except (errors.UnsupportedOperation, errors.TransportNotPossible):
168
            raise TestNotApplicable('Format does not support destroying branch')
169
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
170
    def test_destroy_repository(self):
171
        repo = self.make_repository('repository')
172
        bzrdir = repo.bzrdir
173
        try:
174
            bzrdir.destroy_repository()
175
        except (errors.UnsupportedOperation, errors.TransportNotPossible):
176
            raise TestNotApplicable('Format does not support destroying'
177
                                    ' repository')
6266.2.2 by Jelmer Vernooij
Fix tests.
178
        self.assertRaises(errors.NoRepositoryPresent,
179
            bzrdir.destroy_repository)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
180
        self.assertRaises(errors.NoRepositoryPresent, bzrdir.open_repository)
181
        bzrdir.create_repository()
182
        bzrdir.open_repository()
183
184
    def test_open_workingtree_raises_no_working_tree(self):
185
        """ControlDir.open_workingtree() should raise NoWorkingTree (rather than
186
        e.g. NotLocalUrl) if there is no working tree.
187
        """
188
        dir = self.make_bzrdir('source')
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
189
        vfs_dir = controldir.ControlDir.open(self.get_vfs_only_url('source'))
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
190
        if vfs_dir.has_workingtree():
191
            # This ControlDir format doesn't support ControlDirs without
192
            # working trees, so this test is irrelevant.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
193
            raise TestNotApplicable("format does not support "
194
                "control directories without working tree")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
195
        self.assertRaises(errors.NoWorkingTree, dir.open_workingtree)
196
197
    def test_clone_bzrdir_repository_under_shared(self):
198
        tree = self.make_branch_and_tree('commit_tree')
199
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
200
        tree.add('foo')
201
        tree.commit('revision 1', rev_id='1')
202
        dir = self.make_bzrdir('source')
203
        repo = dir.create_repository()
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
204
        if not repo._format.supports_nesting_repositories:
205
            raise TestNotApplicable("repository format does not support "
206
                "nesting")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
207
        repo.fetch(tree.branch.repository)
208
        self.assertTrue(repo.has_revision('1'))
209
        try:
210
            self.make_repository('target', shared=True)
211
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
212
            raise TestNotApplicable("repository format does not support "
213
                "shared repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
214
        target = dir.clone(self.get_url('target/child'))
215
        self.assertNotEqual(dir.transport.base, target.transport.base)
216
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
217
218
    def test_clone_bzrdir_repository_branch_both_under_shared(self):
219
        # Create a shared repository
220
        try:
221
            shared_repo = self.make_repository('shared', shared=True)
222
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
223
            raise TestNotApplicable("repository format does not support "
224
                "shared repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
225
        if not shared_repo._format.supports_nesting_repositories:
226
            raise TestNotApplicable("format does not support nesting "
227
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
228
        # Make a branch, 'commit_tree', and working tree outside of the shared
229
        # repository, and commit some revisions to it.
230
        tree = self.make_branch_and_tree('commit_tree')
231
        self.build_tree(['foo'], transport=tree.bzrdir.root_transport)
232
        tree.add('foo')
233
        tree.commit('revision 1', rev_id='1')
234
        tree.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
235
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
236
        tree.set_parent_trees([])
237
        tree.commit('revision 2', rev_id='2')
238
        # Copy the content (i.e. revisions) from the 'commit_tree' branch's
239
        # repository into the shared repository.
240
        tree.branch.repository.copy_content_into(shared_repo)
241
        # Make a branch 'source' inside the shared repository.
242
        dir = self.make_bzrdir('shared/source')
243
        dir.create_branch()
244
        # Clone 'source' to 'target', also inside the shared repository.
245
        target = dir.clone(self.get_url('shared/target'))
246
        # 'source', 'target', and the shared repo all have distinct bzrdirs.
247
        self.assertNotEqual(dir.transport.base, target.transport.base)
248
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
249
        # The shared repository will contain revisions from the 'commit_tree'
250
        # repository, even revisions that are not part of the history of the
251
        # 'commit_tree' branch.
252
        self.assertTrue(shared_repo.has_revision('1'))
253
254
    def test_clone_bzrdir_repository_branch_only_source_under_shared(self):
255
        try:
256
            shared_repo = self.make_repository('shared', shared=True)
257
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
258
            raise TestNotApplicable("repository format does not support "
259
                "shared repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
260
        if not shared_repo._format.supports_nesting_repositories:
261
            raise TestNotApplicable("format does not support nesting "
262
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
263
        tree = self.make_branch_and_tree('commit_tree')
264
        self.build_tree(['commit_tree/foo'])
265
        tree.add('foo')
266
        tree.commit('revision 1', rev_id='1')
267
        tree.branch.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
268
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
269
        tree.set_parent_trees([])
270
        tree.commit('revision 2', rev_id='2')
271
        tree.branch.repository.copy_content_into(shared_repo)
272
        if shared_repo.make_working_trees():
273
            shared_repo.set_make_working_trees(False)
274
            self.assertFalse(shared_repo.make_working_trees())
275
        self.assertTrue(shared_repo.has_revision('1'))
276
        dir = self.make_bzrdir('shared/source')
277
        dir.create_branch()
278
        target = dir.clone(self.get_url('target'))
279
        self.assertNotEqual(dir.transport.base, target.transport.base)
280
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
281
        branch = target.open_branch()
282
        self.assertTrue(branch.repository.has_revision('1'))
283
        self.assertFalse(branch.repository.make_working_trees())
284
        self.assertTrue(branch.repository.is_shared())
285
286
    def test_clone_bzrdir_repository_revision(self):
287
        # test for revision limiting, [smoke test, not corner case checks].
288
        # make a repository with some revisions,
289
        # and clone it with a revision limit.
290
        #
291
        tree = self.make_branch_and_tree('commit_tree')
292
        self.build_tree(['commit_tree/foo'])
293
        tree.add('foo')
294
        tree.commit('revision 1', rev_id='1')
295
        tree.branch.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
296
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
297
        tree.set_parent_trees([])
298
        tree.commit('revision 2', rev_id='2')
299
        source = self.make_repository('source')
300
        tree.branch.repository.copy_content_into(source)
301
        dir = source.bzrdir
302
        target = dir.clone(self.get_url('target'), revision_id='2')
303
        raise TestSkipped('revision limiting not strict yet')
304
305
    def test_clone_bzrdir_branch_and_repo_fixed_user_id(self):
306
        # Bug #430868 is about an email containing '.sig'
307
        self.overrideEnv('BZR_EMAIL', 'murphy@host.sighup.org')
308
        tree = self.make_branch_and_tree('commit_tree')
309
        self.build_tree(['commit_tree/foo'])
310
        tree.add('foo')
311
        rev1 = tree.commit('revision 1')
312
        tree_repo = tree.branch.repository
313
        tree_repo.lock_write()
314
        tree_repo.start_write_group()
315
        tree_repo.sign_revision(rev1, gpg.LoopbackGPGStrategy(None))
316
        tree_repo.commit_write_group()
317
        tree_repo.unlock()
318
        target = self.make_branch('target')
319
        tree.branch.repository.copy_content_into(target.repository)
320
        tree.branch.copy_content_into(target)
321
        self.assertTrue(target.repository.has_revision(rev1))
322
        self.assertEqual(
323
            tree_repo.get_signature_text(rev1),
324
            target.repository.get_signature_text(rev1))
325
326
    def test_clone_bzrdir_branch_and_repo_into_shared_repo(self):
327
        # by default cloning into a shared repo uses the shared repo.
328
        tree = self.make_branch_and_tree('commit_tree')
329
        self.build_tree(['commit_tree/foo'])
330
        tree.add('foo')
331
        tree.commit('revision 1')
332
        source = self.make_branch('source')
333
        tree.branch.repository.copy_content_into(source.repository)
334
        tree.branch.copy_content_into(source)
335
        try:
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
336
            shared_repo = self.make_repository('target', shared=True)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
337
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
338
            raise TestNotApplicable("repository format does not support "
339
                "shared repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
340
        if not shared_repo._format.supports_nesting_repositories:
341
            raise TestNotApplicable("format does not support nesting "
342
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
343
        dir = source.bzrdir
344
        target = dir.clone(self.get_url('target/child'))
345
        self.assertNotEqual(dir.transport.base, target.transport.base)
346
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
6165.4.4 by Jelmer Vernooij
Avoid .revision_history().
347
        self.assertEqual(source.last_revision(),
348
                         target.open_branch().last_revision())
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
349
350
    def test_clone_bzrdir_branch_revision(self):
351
        # test for revision limiting, [smoke test, not corner case checks].
352
        # make a branch with some revisions,
353
        # and clone it with a revision limit.
354
        #
355
        tree = self.make_branch_and_tree('commit_tree')
356
        self.build_tree(['commit_tree/foo'])
357
        tree.add('foo')
358
        tree.commit('revision 1', rev_id='1')
359
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
360
        source = self.make_branch('source')
361
        tree.branch.repository.copy_content_into(source.repository)
362
        tree.branch.copy_content_into(source)
363
        dir = source.bzrdir
364
        target = dir.clone(self.get_url('target'), revision_id='1')
365
        self.assertEqual('1', target.open_branch().last_revision())
366
367
    def test_clone_on_transport_preserves_repo_format(self):
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
368
        if self.bzrdir_format == controldir.format_registry.make_bzrdir('default'):
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
369
            format = 'knit'
370
        else:
371
            format = None
372
        source_branch = self.make_branch('source', format=format)
373
        # Ensure no format data is cached
374
        a_dir = bzrlib.branch.Branch.open_from_transport(
375
            self.get_transport('source')).bzrdir
376
        target_transport = self.get_transport('target')
377
        target_bzrdir = a_dir.clone_on_transport(target_transport)
378
        target_repo = target_bzrdir.open_repository()
379
        source_branch = bzrlib.branch.Branch.open(
380
            self.get_vfs_only_url('source'))
381
        if isinstance(target_repo, RemoteRepository):
382
            target_repo._ensure_real()
383
            target_repo = target_repo._real_repository
384
        self.assertEqual(target_repo._format, source_branch.repository._format)
385
386
    def test_clone_bzrdir_tree_revision(self):
387
        # test for revision limiting, [smoke test, not corner case checks].
388
        # make a tree with a revision with a last-revision
389
        # and clone it with a revision limit.
390
        # This smoke test just checks the revision-id is right. Tree specific
391
        # tests will check corner cases.
392
        tree = self.make_branch_and_tree('source')
393
        self.build_tree(['source/foo'])
394
        tree.add('foo')
395
        tree.commit('revision 1', rev_id='1')
396
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
397
        dir = tree.bzrdir
398
        target = dir.clone(self.get_url('target'), revision_id='1')
399
        self.skipIfNoWorkingTree(target)
400
        self.assertEqual(['1'], target.open_workingtree().get_parent_ids())
401
402
    def test_clone_bzrdir_into_notrees_repo(self):
403
        """Cloning into a no-trees repo should not create a working tree"""
404
        tree = self.make_branch_and_tree('source')
405
        self.build_tree(['source/foo'])
406
        tree.add('foo')
407
        tree.commit('revision 1')
408
409
        try:
410
            repo = self.make_repository('repo', shared=True)
411
        except errors.IncompatibleFormat:
412
            raise TestNotApplicable('must support shared repositories')
413
        if repo.make_working_trees():
414
            repo.set_make_working_trees(False)
415
            self.assertFalse(repo.make_working_trees())
416
6182.1.11 by Jelmer Vernooij
Simplify.
417
        a_dir = tree.bzrdir.clone(self.get_url('repo/a'))
6182.1.13 by Jelmer Vernooij
Cope with repository existing in different location.
418
        a_branch = a_dir.open_branch()
419
        # If the new control dir actually uses the repository, it should
420
        # not have a working tree.
421
        if not a_branch.repository.has_same_location(repo):
422
            raise TestNotApplicable('new control dir does not use repository')
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
423
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
424
425
    def test_clone_respects_stacked(self):
426
        branch = self.make_branch('parent')
427
        child_transport = self.get_transport('child')
6162.3.1 by Jelmer Vernooij
Skip a few tests for uninitializable formats.
428
        try:
429
            child = branch.bzrdir.clone_on_transport(child_transport,
430
                                                     stacked_on=branch.base)
431
        except (errors.UnstackableBranchFormat,
432
                errors.UnstackableRepositoryFormat):
6162.5.5 by Jelmer Vernooij
Review feedback from John.
433
            raise TestNotApplicable("branch or repository format does "
6162.3.1 by Jelmer Vernooij
Skip a few tests for uninitializable formats.
434
                "not support stacking")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
435
        self.assertEqual(child.open_branch().get_stacked_on_url(), branch.base)
436
6437.7.1 by Jelmer Vernooij
Add ControlDir.set_branch_reference.
437
    def test_set_branch_reference(self):
438
        """set_branch_reference creates a branch reference"""
439
        referenced_branch = self.make_branch('referenced')
440
        dir = self.make_bzrdir('source')
441
        try:
442
            reference = dir.set_branch_reference(referenced_branch)
443
        except errors.IncompatibleFormat:
444
            # this is ok too, not all formats have to support references.
445
            raise TestNotApplicable("control directory does not "
446
                "support branch references")
447
        self.assertEqual(
448
            referenced_branch.bzrdir.root_transport.abspath('') + '/',
449
            dir.get_branch_reference())
450
451
    def test_set_branch_reference_on_existing_reference(self):
452
        """set_branch_reference creates a branch reference"""
453
        referenced_branch1 = self.make_branch('old-referenced')
454
        referenced_branch2 = self.make_branch('new-referenced')
455
        dir = self.make_bzrdir('source')
456
        try:
457
            reference = dir.set_branch_reference(referenced_branch1)
458
        except errors.IncompatibleFormat:
459
            # this is ok too, not all formats have to support references.
460
            raise TestNotApplicable("control directory does not "
461
                "support branch references")
462
        reference = dir.set_branch_reference(referenced_branch2)
463
        self.assertEqual(
464
            referenced_branch2.bzrdir.root_transport.abspath('') + '/',
465
            dir.get_branch_reference())
466
467
    def test_set_branch_reference_on_existing_branch(self):
468
        """set_branch_reference creates a branch reference"""
469
        referenced_branch = self.make_branch('referenced')
470
        dir = self.make_branch('source').bzrdir
471
        try:
472
            reference = dir.set_branch_reference(referenced_branch)
473
        except errors.IncompatibleFormat:
474
            # this is ok too, not all formats have to support references.
475
            raise TestNotApplicable("control directory does not "
476
                "support branch references")
477
        self.assertEqual(
478
            referenced_branch.bzrdir.root_transport.abspath('') + '/',
479
            dir.get_branch_reference())
480
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
481
    def test_get_branch_reference_on_reference(self):
482
        """get_branch_reference should return the right url."""
483
        referenced_branch = self.make_branch('referenced')
484
        dir = self.make_bzrdir('source')
485
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
486
            dir.set_branch_reference(referenced_branch)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
487
        except errors.IncompatibleFormat:
488
            # this is ok too, not all formats have to support references.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
489
            raise TestNotApplicable("control directory does not "
490
                "support branch references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
491
        self.assertEqual(referenced_branch.bzrdir.root_transport.abspath('') + '/',
492
            dir.get_branch_reference())
493
494
    def test_get_branch_reference_on_non_reference(self):
495
        """get_branch_reference should return None for non-reference branches."""
496
        branch = self.make_branch('referenced')
497
        self.assertEqual(None, branch.bzrdir.get_branch_reference())
498
499
    def test_get_branch_reference_no_branch(self):
500
        """get_branch_reference should not mask NotBranchErrors."""
501
        dir = self.make_bzrdir('source')
502
        if dir.has_branch():
503
            # this format does not support branchless bzrdirs.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
504
            raise TestNotApplicable("format does not support "
505
                "branchless control directories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
506
        self.assertRaises(errors.NotBranchError, dir.get_branch_reference)
507
508
    def test_sprout_bzrdir_empty(self):
509
        dir = self.make_bzrdir('source')
510
        target = dir.sprout(self.get_url('target'))
6050.3.1 by Jelmer Vernooij
Use control_transport rather than transport.
511
        self.assertNotEqual(dir.control_transport.base, target.control_transport.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
512
        # creates a new repository branch and tree
513
        target.open_repository()
514
        target.open_branch()
515
        self.openWorkingTreeIfLocal(target)
516
517
    def test_sprout_bzrdir_empty_under_shared_repo(self):
518
        # sprouting an empty dir into a repo uses the repo
519
        dir = self.make_bzrdir('source')
520
        try:
521
            self.make_repository('target', shared=True)
522
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
523
            raise TestNotApplicable("format does not support shared "
524
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
525
        target = dir.sprout(self.get_url('target/child'))
526
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
527
        target.open_branch()
528
        try:
529
            target.open_workingtree()
530
        except errors.NoWorkingTree:
531
            # Some bzrdirs can never have working trees.
6162.5.2 by Jelmer Vernooij
Skip test that requires nesting repositories, check supports workingtrees.
532
            repo = target.find_repository()
533
            self.assertFalse(repo.bzrdir._format.supports_workingtrees)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
534
535
    def test_sprout_bzrdir_empty_under_shared_repo_force_new(self):
536
        # the force_new_repo parameter should force use of a new repo in an empty
537
        # bzrdir's sprout logic
538
        dir = self.make_bzrdir('source')
539
        try:
540
            self.make_repository('target', shared=True)
541
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
542
            raise TestNotApplicable("format does not support shared "
543
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
544
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
545
        target.open_repository()
546
        target.open_branch()
547
        self.openWorkingTreeIfLocal(target)
548
549
    def test_sprout_bzrdir_with_repository_to_shared(self):
550
        tree = self.make_branch_and_tree('commit_tree')
551
        self.build_tree(['commit_tree/foo'])
552
        tree.add('foo')
553
        tree.commit('revision 1', rev_id='1')
554
        tree.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
555
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
556
        tree.set_parent_trees([])
557
        tree.commit('revision 2', rev_id='2')
558
        source = self.make_repository('source')
559
        tree.branch.repository.copy_content_into(source)
560
        dir = source.bzrdir
561
        try:
562
            shared_repo = self.make_repository('target', shared=True)
563
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
564
            raise TestNotApplicable("format does not support "
565
                "shared repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
566
        target = dir.sprout(self.get_url('target/child'))
6127.1.4 by Jelmer Vernooij
Use user_transport.
567
        self.assertNotEqual(dir.user_transport.base, target.user_transport.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
568
        self.assertTrue(shared_repo.has_revision('1'))
569
570
    def test_sprout_bzrdir_repository_branch_both_under_shared(self):
571
        try:
572
            shared_repo = self.make_repository('shared', shared=True)
573
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
574
            raise TestNotApplicable("format does not support shared "
575
                "repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
576
        if not shared_repo._format.supports_nesting_repositories:
577
            raise TestNotApplicable("format does not support nesting "
578
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
579
        tree = self.make_branch_and_tree('commit_tree')
580
        self.build_tree(['commit_tree/foo'])
581
        tree.add('foo')
582
        tree.commit('revision 1', rev_id='1')
583
        tree.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
584
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
585
        tree.set_parent_trees([])
586
        tree.commit('revision 2', rev_id='2')
587
        tree.branch.repository.copy_content_into(shared_repo)
588
        dir = self.make_bzrdir('shared/source')
589
        dir.create_branch()
590
        target = dir.sprout(self.get_url('shared/target'))
591
        self.assertNotEqual(dir.transport.base, target.transport.base)
592
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
593
        self.assertTrue(shared_repo.has_revision('1'))
594
595
    def test_sprout_bzrdir_repository_branch_only_source_under_shared(self):
596
        try:
597
            shared_repo = self.make_repository('shared', shared=True)
598
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
599
            raise TestNotApplicable("format does not support shared "
600
                "repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
601
        if not shared_repo._format.supports_nesting_repositories:
602
            raise TestNotApplicable("format does not support nesting "
603
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
604
        tree = self.make_branch_and_tree('commit_tree')
605
        self.build_tree(['commit_tree/foo'])
606
        tree.add('foo')
607
        tree.commit('revision 1', rev_id='1')
608
        tree.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
609
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
610
        tree.set_parent_trees([])
611
        tree.commit('revision 2', rev_id='2')
612
        tree.branch.repository.copy_content_into(shared_repo)
613
        if shared_repo.make_working_trees():
614
            shared_repo.set_make_working_trees(False)
615
            self.assertFalse(shared_repo.make_working_trees())
616
        self.assertTrue(shared_repo.has_revision('1'))
617
        dir = self.make_bzrdir('shared/source')
618
        dir.create_branch()
619
        target = dir.sprout(self.get_url('target'))
620
        self.assertNotEqual(dir.transport.base, target.transport.base)
621
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
622
        branch = target.open_branch()
623
        # The sprouted bzrdir has a branch, so only revisions referenced by
624
        # that branch are copied, rather than the whole repository.  It's an
625
        # empty branch, so none are copied.
626
        self.assertEqual([], branch.repository.all_revision_ids())
627
        if branch.bzrdir._format.supports_workingtrees:
628
            self.assertTrue(branch.repository.make_working_trees())
629
        self.assertFalse(branch.repository.is_shared())
630
631
    def test_sprout_bzrdir_repository_under_shared_force_new_repo(self):
632
        tree = self.make_branch_and_tree('commit_tree')
633
        self.build_tree(['commit_tree/foo'])
634
        tree.add('foo')
635
        tree.commit('revision 1', rev_id='1')
636
        tree.bzrdir.open_branch().generate_revision_history(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
637
            _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
638
        tree.set_parent_trees([])
639
        tree.commit('revision 2', rev_id='2')
640
        source = self.make_repository('source')
641
        tree.branch.repository.copy_content_into(source)
642
        dir = source.bzrdir
643
        try:
644
            shared_repo = self.make_repository('target', shared=True)
645
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
646
            raise TestNotApplicable("format does not support shared "
647
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
648
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
6150.2.1 by Jelmer Vernooij
Use user_transport rather than deprecated .transport.
649
        self.assertNotEqual(
650
            dir.control_transport.base,
651
            target.control_transport.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
652
        self.assertFalse(shared_repo.has_revision('1'))
653
654
    def test_sprout_bzrdir_repository_revision(self):
655
        # test for revision limiting, [smoke test, not corner case checks].
656
        # make a repository with some revisions,
657
        # and sprout it with a revision limit.
658
        #
659
        tree = self.make_branch_and_tree('commit_tree')
660
        self.build_tree(['commit_tree/foo'])
661
        tree.add('foo')
662
        tree.commit('revision 1', rev_id='1')
663
        br = tree.bzrdir.open_branch()
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
664
        br.set_last_revision_info(0, _mod_revision.NULL_REVISION)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
665
        tree.set_parent_trees([])
666
        tree.commit('revision 2', rev_id='2')
667
        source = self.make_repository('source')
668
        tree.branch.repository.copy_content_into(source)
669
        dir = source.bzrdir
670
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='2')
671
        raise TestSkipped('revision limiting not strict yet')
672
673
    def test_sprout_bzrdir_branch_and_repo_shared(self):
674
        # sprouting a branch with a repo into a shared repo uses the shared
675
        # repo
676
        tree = self.make_branch_and_tree('commit_tree')
677
        self.build_tree(['commit_tree/foo'])
678
        tree.add('foo')
679
        tree.commit('revision 1', rev_id='1')
680
        source = self.make_branch('source')
681
        tree.branch.repository.copy_content_into(source.repository)
682
        tree.bzrdir.open_branch().copy_content_into(source)
683
        dir = source.bzrdir
684
        try:
685
            shared_repo = self.make_repository('target', shared=True)
686
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
687
            raise TestNotApplicable("format does not support shared "
688
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
689
        target = dir.sprout(self.get_url('target/child'))
690
        self.assertTrue(shared_repo.has_revision('1'))
691
692
    def test_sprout_bzrdir_branch_and_repo_shared_force_new_repo(self):
693
        # sprouting a branch with a repo into a shared repo uses the shared
694
        # repo
695
        tree = self.make_branch_and_tree('commit_tree')
696
        self.build_tree(['commit_tree/foo'])
697
        tree.add('foo')
698
        tree.commit('revision 1', rev_id='1')
699
        source = self.make_branch('source')
700
        tree.branch.repository.copy_content_into(source.repository)
701
        tree.bzrdir.open_branch().copy_content_into(source)
702
        dir = source.bzrdir
703
        try:
704
            shared_repo = self.make_repository('target', shared=True)
705
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
706
            raise TestNotApplicable("format does not support shared "
707
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
708
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
6127.1.4 by Jelmer Vernooij
Use user_transport.
709
        self.assertNotEqual(dir.control_transport.base, target.control_transport.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
710
        self.assertFalse(shared_repo.has_revision('1'))
711
712
    def test_sprout_bzrdir_branch_reference(self):
713
        # sprouting should create a repository if needed and a sprouted branch.
714
        referenced_branch = self.make_branch('referenced')
715
        dir = self.make_bzrdir('source')
716
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
717
            dir.set_branch_reference(referenced_branch)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
718
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
719
            raise TestNotApplicable("format does not support branch "
720
                "references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
721
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
722
        target = dir.sprout(self.get_url('target'))
723
        self.assertNotEqual(dir.transport.base, target.transport.base)
724
        # we want target to have a branch that is in-place.
725
        self.assertEqual(target, target.open_branch().bzrdir)
726
        # and as we dont support repositories being detached yet, a repo in
727
        # place
728
        target.open_repository()
729
730
    def test_sprout_bzrdir_branch_reference_shared(self):
731
        # sprouting should create a repository if needed and a sprouted branch.
732
        referenced_tree = self.make_branch_and_tree('referenced')
733
        referenced_tree.commit('1', rev_id='1', allow_pointless=True)
734
        dir = self.make_bzrdir('source')
735
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
736
            dir.set_branch_reference(referenced_tree.branch)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
737
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
738
            raise TestNotApplicable("format does not support branch "
739
                "references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
740
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
741
        try:
742
            shared_repo = self.make_repository('target', shared=True)
743
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
744
            raise TestNotApplicable("format does not support "
745
                "shared repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
746
        target = dir.sprout(self.get_url('target/child'))
747
        self.assertNotEqual(dir.transport.base, target.transport.base)
748
        # we want target to have a branch that is in-place.
749
        self.assertEqual(target, target.open_branch().bzrdir)
750
        # and we want no repository as the target is shared
751
        self.assertRaises(errors.NoRepositoryPresent,
752
                          target.open_repository)
753
        # and we want revision '1' in the shared repo
754
        self.assertTrue(shared_repo.has_revision('1'))
755
756
    def test_sprout_bzrdir_branch_reference_shared_force_new_repo(self):
757
        # sprouting should create a repository if needed and a sprouted branch.
758
        referenced_tree = self.make_branch_and_tree('referenced')
759
        referenced_tree.commit('1', rev_id='1', allow_pointless=True)
760
        dir = self.make_bzrdir('source')
761
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
762
            dir.set_branch_reference(referenced_tree.branch)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
763
        except errors.IncompatibleFormat:
764
            # this is ok too, not all formats have to support references.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
765
            raise TestNotApplicable("format does not support "
766
                "branch references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
767
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
768
        try:
769
            shared_repo = self.make_repository('target', shared=True)
770
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
771
            raise TestNotApplicable("format does not support shared "
772
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
773
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
774
        self.assertNotEqual(dir.transport.base, target.transport.base)
775
        # we want target to have a branch that is in-place.
776
        self.assertEqual(target, target.open_branch().bzrdir)
777
        # and we want revision '1' in the new repo
778
        self.assertTrue(target.open_repository().has_revision('1'))
779
        # but not the shared one
780
        self.assertFalse(shared_repo.has_revision('1'))
781
782
    def test_sprout_bzrdir_branch_revision(self):
783
        # test for revision limiting, [smoke test, not corner case checks].
784
        # make a repository with some revisions,
785
        # and sprout it with a revision limit.
786
        #
787
        tree = self.make_branch_and_tree('commit_tree')
788
        self.build_tree(['commit_tree/foo'])
789
        tree.add('foo')
790
        tree.commit('revision 1', rev_id='1')
791
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
792
        source = self.make_branch('source')
793
        tree.branch.repository.copy_content_into(source.repository)
794
        tree.bzrdir.open_branch().copy_content_into(source)
795
        dir = source.bzrdir
796
        target = dir.sprout(self.get_url('target'), revision_id='1')
797
        self.assertEqual('1', target.open_branch().last_revision())
798
799
    def test_sprout_bzrdir_branch_with_tags(self):
800
        # when sprouting a branch all revisions named in the tags are copied
801
        # too.
802
        builder = self.make_branch_builder('source')
803
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
804
        try:
805
            source.tags.set_tag('tag-a', 'rev-2')
806
        except errors.TagsNotSupported:
807
            raise TestNotApplicable('Branch format does not support tags.')
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
808
        source.get_config_stack().set('branch.fetch_tags', True)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
809
        # Now source has a tag not in its ancestry.  Sprout its controldir.
810
        dir = source.bzrdir
811
        target = dir.sprout(self.get_url('target'))
812
        # The tag is present, and so is its revision.
813
        new_branch = target.open_branch()
814
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
815
        new_branch.repository.get_revision('rev-2')
816
817
    def test_sprout_bzrdir_branch_with_absent_tag(self):
818
        # tags referencing absent revisions are copied (and those absent
819
        # revisions do not prevent the sprout.)
820
        builder = self.make_branch_builder('source')
821
        builder.build_commit(message="Rev 1", rev_id='rev-1')
822
        source = builder.get_branch()
823
        try:
824
            source.tags.set_tag('tag-a', 'missing-rev')
6123.9.10 by Jelmer Vernooij
Improve tag handling.
825
        except (errors.TagsNotSupported, errors.GhostTagsNotSupported):
826
            raise TestNotApplicable('Branch format does not support tags '
827
                'or tags referencing ghost revisions.')
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
828
        # Now source has a tag pointing to an absent revision.  Sprout its
829
        # controldir.
830
        dir = source.bzrdir
831
        target = dir.sprout(self.get_url('target'))
832
        # The tag is present in the target
833
        new_branch = target.open_branch()
834
        self.assertEqual('missing-rev', new_branch.tags.lookup_tag('tag-a'))
835
836
    def test_sprout_bzrdir_passing_source_branch_with_absent_tag(self):
837
        # tags referencing absent revisions are copied (and those absent
838
        # revisions do not prevent the sprout.)
839
        builder = self.make_branch_builder('source')
840
        builder.build_commit(message="Rev 1", rev_id='rev-1')
841
        source = builder.get_branch()
842
        try:
843
            source.tags.set_tag('tag-a', 'missing-rev')
6123.9.10 by Jelmer Vernooij
Improve tag handling.
844
        except (errors.TagsNotSupported, errors.GhostTagsNotSupported):
845
            raise TestNotApplicable('Branch format does not support tags '
846
                'or tags referencing missing revisions.')
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
847
        # Now source has a tag pointing to an absent revision.  Sprout its
848
        # controldir.
849
        dir = source.bzrdir
850
        target = dir.sprout(self.get_url('target'), source_branch=source)
851
        # The tag is present in the target
852
        new_branch = target.open_branch()
853
        self.assertEqual('missing-rev', new_branch.tags.lookup_tag('tag-a'))
854
855
    def test_sprout_bzrdir_passing_rev_not_source_branch_copies_tags(self):
856
        # dir.sprout(..., revision_id='rev1') copies rev1, and all the tags of
857
        # the branch at that bzrdir, the ancestry of all of those, but no other
858
        # revs (not even the tip of the source branch).
859
        builder = self.make_branch_builder('source')
860
        builder.build_commit(message="Base", rev_id='base-rev')
861
        # Make three parallel lines of ancestry off this base.
862
        source = builder.get_branch()
863
        builder.build_commit(message="Rev A1", rev_id='rev-a1')
864
        builder.build_commit(message="Rev A2", rev_id='rev-a2')
865
        builder.build_commit(message="Rev A3", rev_id='rev-a3')
866
        source.set_last_revision_info(1, 'base-rev')
867
        builder.build_commit(message="Rev B1", rev_id='rev-b1')
868
        builder.build_commit(message="Rev B2", rev_id='rev-b2')
869
        builder.build_commit(message="Rev B3", rev_id='rev-b3')
870
        source.set_last_revision_info(1, 'base-rev')
871
        builder.build_commit(message="Rev C1", rev_id='rev-c1')
872
        builder.build_commit(message="Rev C2", rev_id='rev-c2')
873
        builder.build_commit(message="Rev C3", rev_id='rev-c3')
874
        # Set the branch tip to A2
875
        source.set_last_revision_info(3, 'rev-a2')
876
        try:
877
            # Create a tag for B2, and for an absent rev
878
            source.tags.set_tag('tag-non-ancestry', 'rev-b2')
6123.9.10 by Jelmer Vernooij
Improve tag handling.
879
        except errors.TagsNotSupported:
880
            raise TestNotApplicable('Branch format does not support tags ')
881
        try:
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
882
            source.tags.set_tag('tag-absent', 'absent-rev')
6123.9.10 by Jelmer Vernooij
Improve tag handling.
883
        except errors.GhostTagsNotSupported:
884
            has_ghost_tag = False
885
        else:
886
            has_ghost_tag = True
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
887
        source.get_config_stack().set('branch.fetch_tags', True)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
888
        # And ask sprout for C2
889
        dir = source.bzrdir
890
        target = dir.sprout(self.get_url('target'), revision_id='rev-c2')
891
        # The tags are present
892
        new_branch = target.open_branch()
6123.9.10 by Jelmer Vernooij
Improve tag handling.
893
        if has_ghost_tag:
894
            self.assertEqual(
895
                {'tag-absent': 'absent-rev', 'tag-non-ancestry': 'rev-b2'},
896
                new_branch.tags.get_tag_dict())
897
        else:
898
            self.assertEqual(
899
                {'tag-non-ancestry': 'rev-b2'},
900
                new_branch.tags.get_tag_dict())
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
901
        # And the revs for A2, B2 and C2's ancestries are present, but no
902
        # others.
903
        self.assertEqual(
904
            ['base-rev', 'rev-b1', 'rev-b2', 'rev-c1', 'rev-c2'],
905
            sorted(new_branch.repository.all_revision_ids()))
906
907
    def test_sprout_bzrdir_tree_branch_reference(self):
908
        # sprouting should create a repository if needed and a sprouted branch.
909
        # the tree state should not be copied.
910
        referenced_branch = self.make_branch('referencced')
911
        dir = self.make_bzrdir('source')
912
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
913
            dir.set_branch_reference(referenced_branch)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
914
        except errors.IncompatibleFormat:
915
            # this is ok too, not all formats have to support references.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
916
            raise TestNotApplicable("format does not support "
917
                "branch references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
918
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
919
        tree = self.createWorkingTreeOrSkip(dir)
920
        self.build_tree(['source/subdir/'])
921
        tree.add('subdir')
922
        target = dir.sprout(self.get_url('target'))
923
        self.assertNotEqual(dir.transport.base, target.transport.base)
924
        # we want target to have a branch that is in-place.
925
        self.assertEqual(target, target.open_branch().bzrdir)
926
        # and as we dont support repositories being detached yet, a repo in
927
        # place
928
        target.open_repository()
929
        result_tree = target.open_workingtree()
930
        self.assertFalse(result_tree.has_filename('subdir'))
931
932
    def test_sprout_bzrdir_tree_branch_reference_revision(self):
933
        # sprouting should create a repository if needed and a sprouted branch.
934
        # the tree state should not be copied but the revision changed,
935
        # and the likewise the new branch should be truncated too
936
        referenced_branch = self.make_branch('referencced')
937
        dir = self.make_bzrdir('source')
938
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
939
            dir.set_branch_reference(referenced_branch)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
940
        except errors.IncompatibleFormat:
941
            # this is ok too, not all formats have to support references.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
942
            raise TestNotApplicable("format does not support "
943
                "branch references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
944
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
945
        tree = self.createWorkingTreeOrSkip(dir)
946
        self.build_tree(['source/foo'])
947
        tree.add('foo')
948
        tree.commit('revision 1', rev_id='1')
949
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
950
        target = dir.sprout(self.get_url('target'), revision_id='1')
951
        self.skipIfNoWorkingTree(target)
952
        self.assertNotEqual(dir.transport.base, target.transport.base)
953
        # we want target to have a branch that is in-place.
954
        self.assertEqual(target, target.open_branch().bzrdir)
955
        # and as we dont support repositories being detached yet, a repo in
956
        # place
957
        target.open_repository()
958
        # we trust that the working tree sprouting works via the other tests.
959
        self.assertEqual(['1'], target.open_workingtree().get_parent_ids())
960
        self.assertEqual('1', target.open_branch().last_revision())
961
962
    def test_sprout_bzrdir_tree_revision(self):
963
        # test for revision limiting, [smoke test, not corner case checks].
964
        # make a tree with a revision with a last-revision
965
        # and sprout it with a revision limit.
966
        # This smoke test just checks the revision-id is right. Tree specific
967
        # tests will check corner cases.
968
        tree = self.make_branch_and_tree('source')
969
        self.build_tree(['source/foo'])
970
        tree.add('foo')
971
        tree.commit('revision 1', rev_id='1')
972
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
973
        dir = tree.bzrdir
974
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='1')
975
        self.assertEqual(['1'], target.open_workingtree().get_parent_ids())
976
977
    def test_sprout_takes_accelerator(self):
978
        tree = self.make_branch_and_tree('source')
979
        self.build_tree(['source/foo'])
980
        tree.add('foo')
981
        tree.commit('revision 1', rev_id='1')
982
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
983
        dir = tree.bzrdir
984
        target = self.sproutOrSkip(dir, self.get_url('target'),
985
                                   accelerator_tree=tree)
986
        self.assertEqual(['2'], target.open_workingtree().get_parent_ids())
987
988
    def test_sprout_branch_no_tree(self):
989
        tree = self.make_branch_and_tree('source')
990
        self.build_tree(['source/foo'])
991
        tree.add('foo')
992
        tree.commit('revision 1', rev_id='1')
993
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
994
        dir = tree.bzrdir
995
        try:
996
            target = dir.sprout(self.get_url('target'),
997
                create_tree_if_local=False)
998
        except errors.MustHaveWorkingTree:
999
            raise TestNotApplicable("control dir format requires working tree")
1000
        self.assertPathDoesNotExist('target/foo')
1001
        self.assertEqual(tree.branch.last_revision(),
1002
                         target.open_branch().last_revision())
1003
1004
    def test_sprout_with_revision_id_uses_default_stack_on(self):
1005
        # Make a branch with three commits to stack on.
1006
        builder = self.make_branch_builder('stack-on')
1007
        builder.start_series()
1008
        builder.build_commit(message='Rev 1.', rev_id='rev-1')
1009
        builder.build_commit(message='Rev 2.', rev_id='rev-2')
1010
        builder.build_commit(message='Rev 3.', rev_id='rev-3')
1011
        builder.finish_series()
1012
        stack_on = builder.get_branch()
1013
        # Make a bzrdir with a default stacking policy to stack on that branch.
1014
        config = self.make_bzrdir('policy-dir').get_config()
1015
        try:
1016
            config.set_default_stack_on(self.get_url('stack-on'))
1017
        except errors.BzrError:
1018
            raise TestNotApplicable('Only relevant for stackable formats.')
1019
        # Sprout the stacked-on branch into the bzrdir.
1020
        sprouted = stack_on.bzrdir.sprout(
1021
            self.get_url('policy-dir/sprouted'), revision_id='rev-3')
1022
        # Not all revisions are copied into the sprouted repository.
1023
        repo = sprouted.open_repository()
1024
        self.addCleanup(repo.lock_read().unlock)
1025
        self.assertEqual(None, repo.get_parent_map(['rev-1']).get('rev-1'))
1026
1027
    def test_format_initialize_find_open(self):
1028
        # loopback test to check the current format initializes to itself.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1029
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1030
            # unsupported formats are not loopback testable
1031
            # because the default open will not open them and
1032
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1033
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1034
        # for remote formats, there must be no prior assumption about the
1035
        # network name to use - it's possible that this may somehow have got
1036
        # in through an unisolated test though - see
1037
        # <https://bugs.launchpad.net/bzr/+bug/504102>
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1038
        self.assertEqual(getattr(self.bzrdir_format,
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1039
            '_network_name', None),
1040
            None)
1041
        # supported formats must be able to init and open
1042
        t = self.get_transport()
1043
        readonly_t = self.get_readonly_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1044
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1045
        self.assertIsInstance(made_control, controldir.ControlDir)
1046
        if isinstance(self.bzrdir_format, RemoteBzrDirFormat):
1047
            return
1048
        self.assertEqual(self.bzrdir_format,
1049
                         controldir.ControlDirFormat.find_format(readonly_t))
1050
        direct_opened_dir = self.bzrdir_format.open(readonly_t)
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1051
        opened_dir = controldir.ControlDir.open(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1052
        self.assertEqual(made_control._format,
1053
                         opened_dir._format)
1054
        self.assertEqual(direct_opened_dir._format,
1055
                         opened_dir._format)
1056
        self.assertIsInstance(opened_dir, controldir.ControlDir)
1057
1058
    def test_format_initialize_on_transport_ex(self):
1059
        t = self.get_transport('dir')
1060
        self.assertInitializeEx(t)
1061
1062
    def test_format_initialize_on_transport_ex_use_existing_dir_True(self):
1063
        t = self.get_transport('dir')
1064
        t.ensure_base()
1065
        self.assertInitializeEx(t, use_existing_dir=True)
1066
1067
    def test_format_initialize_on_transport_ex_use_existing_dir_False(self):
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1068
        if not self.bzrdir_format.is_initializable():
1069
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1070
        t = self.get_transport('dir')
1071
        t.ensure_base()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1072
        self.assertRaises(errors.FileExists,
1073
            self.bzrdir_format.initialize_on_transport_ex, t,
1074
            use_existing_dir=False)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1075
1076
    def test_format_initialize_on_transport_ex_create_prefix_True(self):
1077
        t = self.get_transport('missing/dir')
1078
        self.assertInitializeEx(t, create_prefix=True)
1079
1080
    def test_format_initialize_on_transport_ex_create_prefix_False(self):
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1081
        if not self.bzrdir_format.is_initializable():
1082
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1083
        t = self.get_transport('missing/dir')
1084
        self.assertRaises(errors.NoSuchFile, self.assertInitializeEx, t,
1085
            create_prefix=False)
1086
1087
    def test_format_initialize_on_transport_ex_force_new_repo_True(self):
1088
        t = self.get_transport('repo')
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1089
        repo_fmt = controldir.format_registry.make_bzrdir('1.9')
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1090
        repo_name = repo_fmt.repository_format.network_name()
1091
        repo = repo_fmt.initialize_on_transport_ex(t,
1092
            repo_format_name=repo_name, shared_repo=True)[0]
1093
        made_repo, control = self.assertInitializeEx(t.clone('branch'),
1094
            force_new_repo=True, repo_format_name=repo_name)
1095
        self.assertNotEqual(repo.bzrdir.root_transport.base,
1096
            made_repo.bzrdir.root_transport.base)
1097
1098
    def test_format_initialize_on_transport_ex_force_new_repo_False(self):
1099
        t = self.get_transport('repo')
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1100
        repo_fmt = controldir.format_registry.make_bzrdir('1.9')
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1101
        repo_name = repo_fmt.repository_format.network_name()
1102
        repo = repo_fmt.initialize_on_transport_ex(t,
1103
            repo_format_name=repo_name, shared_repo=True)[0]
1104
        made_repo, control = self.assertInitializeEx(t.clone('branch'),
1105
            force_new_repo=False, repo_format_name=repo_name)
1106
        if not control._format.fixed_components:
1107
            self.assertEqual(repo.bzrdir.root_transport.base,
1108
                made_repo.bzrdir.root_transport.base)
1109
1110
    def test_format_initialize_on_transport_ex_repo_fmt_name_None(self):
1111
        t = self.get_transport('dir')
1112
        repo, control = self.assertInitializeEx(t)
1113
        self.assertEqual(None, repo)
1114
1115
    def test_format_initialize_on_transport_ex_repo_fmt_name_followed(self):
1116
        t = self.get_transport('dir')
1117
        # 1.6 is likely to never be default
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1118
        fmt = controldir.format_registry.make_bzrdir('1.6')
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1119
        repo_name = fmt.repository_format.network_name()
1120
        repo, control = self.assertInitializeEx(t, repo_format_name=repo_name)
1121
        if self.bzrdir_format.fixed_components:
1122
            # must stay with the all-in-one-format.
1123
            repo_name = self.bzrdir_format.network_name()
1124
        self.assertEqual(repo_name, repo._format.network_name())
1125
6164.2.5 by Jelmer Vernooij
Move some stacking tests specific to bzr formats to per_bzrdir.
1126
    def assertInitializeEx(self, t, **kwargs):
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1127
        """Execute initialize_on_transport_ex and check it succeeded correctly.
1128
1129
        This involves checking that the disk objects were created, open with
1130
        the same format returned, and had the expected disk format.
1131
1132
        :param t: The transport to initialize on.
1133
        :param **kwargs: Additional arguments to pass to
1134
            initialize_on_transport_ex.
1135
        :return: the resulting repo, control dir tuple.
1136
        """
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1137
        if not self.bzrdir_format.is_initializable():
1138
            raise TestNotApplicable("control dir format is not "
1139
                "initializable")
1140
        repo, control, require_stacking, repo_policy = \
1141
            self.bzrdir_format.initialize_on_transport_ex(t, **kwargs)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1142
        if repo is not None:
1143
            # Repositories are open write-locked
1144
            self.assertTrue(repo.is_write_locked())
1145
            self.addCleanup(repo.unlock)
1146
        self.assertIsInstance(control, controldir.ControlDir)
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1147
        opened = controldir.ControlDir.open(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1148
        expected_format = self.bzrdir_format
1149
        if not isinstance(expected_format, RemoteBzrDirFormat):
1150
            self.assertEqual(control._format.network_name(),
1151
                expected_format.network_name())
1152
            self.assertEqual(control._format.network_name(),
1153
                opened._format.network_name())
1154
        self.assertEqual(control.__class__, opened.__class__)
1155
        return repo, control
1156
1157
    def test_format_network_name(self):
1158
        # All control formats must have a network name.
1159
        dir = self.make_bzrdir('.')
1160
        format = dir._format
1161
        # We want to test that the network_name matches the actual format on
1162
        # disk. For local control dirsthat means that using network_name as a
1163
        # key in the registry gives back the same format. For remote obects
1164
        # we check that the network_name of the RemoteBzrDirFormat we have
1165
        # locally matches the actual format present on disk.
1166
        if isinstance(format, RemoteBzrDirFormat):
1167
            dir._ensure_real()
1168
            real_dir = dir._real_bzrdir
1169
            network_name = format.network_name()
1170
            self.assertEqual(real_dir._format.network_name(), network_name)
1171
        else:
1172
            registry = controldir.network_format_registry
1173
            network_name = format.network_name()
1174
            looked_up_format = registry.get(network_name)
1175
            self.assertTrue(
1176
                issubclass(format.__class__, looked_up_format.__class__))
1177
        # The network name must be a byte string.
1178
        self.assertIsInstance(network_name, str)
1179
1180
    def test_open_not_bzrdir(self):
1181
        # test the formats specific behaviour for no-content or similar dirs.
1182
        self.assertRaises(errors.NotBranchError,
1183
                          self.bzrdir_format.open,
1184
                          transport.get_transport_from_url(self.get_readonly_url()))
1185
1186
    def test_create_branch(self):
1187
        # a bzrdir can construct a branch and repository for itself.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1188
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1189
            # unsupported formats are not loopback testable
1190
            # because the default open will not open them and
1191
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1192
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1193
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1194
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1195
        made_repo = made_control.create_repository()
1196
        made_branch = made_control.create_branch()
1197
        self.assertIsInstance(made_branch, bzrlib.branch.Branch)
1198
        self.assertEqual(made_control, made_branch.bzrdir)
1199
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
1200
    def test_create_branch_append_revisions_only(self):
1201
        # a bzrdir can construct a branch and repository for itself.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1202
        if not self.bzrdir_format.is_initializable():
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
1203
            # unsupported formats are not loopback testable
1204
            # because the default open will not open them and
1205
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1206
            raise TestNotApplicable("format is not initializable")
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
1207
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1208
        made_control = self.bzrdir_format.initialize(t.base)
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
1209
        made_repo = made_control.create_repository()
1210
        try:
1211
            made_branch = made_control.create_branch(
1212
                append_revisions_only=True)
1213
        except errors.UpgradeRequired:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1214
            raise TestNotApplicable("format does not support "
1215
                "append_revisions_only setting")
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
1216
        self.assertIsInstance(made_branch, bzrlib.branch.Branch)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1217
        self.assertEqual(True, made_branch.get_append_revisions_only())
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
1218
        self.assertEqual(made_control, made_branch.bzrdir)
1219
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1220
    def test_open_branch(self):
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1221
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1222
            # unsupported formats are not loopback testable
1223
            # because the default open will not open them and
1224
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1225
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1226
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1227
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1228
        made_repo = made_control.create_repository()
1229
        made_branch = made_control.create_branch()
1230
        opened_branch = made_control.open_branch()
1231
        self.assertEqual(made_control, opened_branch.bzrdir)
1232
        self.assertIsInstance(opened_branch, made_branch.__class__)
1233
        self.assertIsInstance(opened_branch._format, made_branch._format.__class__)
1234
1235
    def test_list_branches(self):
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1236
        if not self.bzrdir_format.is_initializable():
1237
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1238
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1239
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1240
        made_repo = made_control.create_repository()
1241
        made_branch = made_control.create_branch()
1242
        branches = made_control.list_branches()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1243
        self.assertEqual(1, len(branches))
1244
        self.assertEqual(made_branch.base, branches[0].base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1245
        try:
1246
            made_control.destroy_branch()
1247
        except errors.UnsupportedOperation:
1248
            pass # Not all bzrdirs support destroying directories
1249
        else:
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1250
            self.assertEqual([], made_control.list_branches())
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1251
6282.5.4 by Neil Martinsen-Burrell
add get_branches method to return a dictionary of names and branches
1252
    def test_get_branches(self):
1253
        repo = self.make_repository('branch-1')
1254
        target_branch = repo.bzrdir.create_branch()
6436.1.1 by Jelmer Vernooij
Change default branch name to "".
1255
        self.assertEqual([""], repo.bzrdir.get_branches().keys())
6282.5.4 by Neil Martinsen-Burrell
add get_branches method to return a dictionary of names and branches
1256
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1257
    def test_create_repository(self):
1258
        # a bzrdir can construct a repository for itself.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1259
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1260
            # unsupported formats are not loopback testable
1261
            # because the default open will not open them and
1262
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1263
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1264
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1265
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1266
        made_repo = made_control.create_repository()
1267
        # Check that we have a repository object.
1268
        made_repo.has_revision('foo')
1269
        self.assertEqual(made_control, made_repo.bzrdir)
1270
1271
    def test_create_repository_shared(self):
1272
        # a bzrdir can create a shared repository or
1273
        # fail appropriately
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1274
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1275
            # unsupported formats are not loopback testable
1276
            # because the default open will not open them and
1277
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1278
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1279
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1280
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1281
        try:
1282
            made_repo = made_control.create_repository(shared=True)
1283
        except errors.IncompatibleFormat:
1284
            # Old bzrdir formats don't support shared repositories
1285
            # and should raise IncompatibleFormat
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1286
            raise TestNotApplicable("format does not support shared "
1287
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1288
        self.assertTrue(made_repo.is_shared())
1289
1290
    def test_create_repository_nonshared(self):
1291
        # a bzrdir can create a non-shared repository
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1292
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1293
            # unsupported formats are not loopback testable
1294
            # because the default open will not open them and
1295
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1296
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1297
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1298
        made_control = self.bzrdir_format.initialize(t.base)
6123.9.10 by Jelmer Vernooij
Improve tag handling.
1299
        try:
1300
            made_repo = made_control.create_repository(shared=False)
1301
        except errors.IncompatibleFormat:
1302
            # Some control dir formats don't support non-shared repositories
1303
            # and should raise IncompatibleFormat
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1304
            raise TestNotApplicable("format does not support shared "
1305
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1306
        self.assertFalse(made_repo.is_shared())
1307
1308
    def test_open_repository(self):
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1309
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1310
            # unsupported formats are not loopback testable
1311
            # because the default open will not open them and
1312
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1313
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1314
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1315
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1316
        made_repo = made_control.create_repository()
1317
        opened_repo = made_control.open_repository()
1318
        self.assertEqual(made_control, opened_repo.bzrdir)
1319
        self.assertIsInstance(opened_repo, made_repo.__class__)
1320
        self.assertIsInstance(opened_repo._format, made_repo._format.__class__)
1321
1322
    def test_create_workingtree(self):
1323
        # a bzrdir can construct a working tree for itself.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1324
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1325
            # unsupported formats are not loopback testable
1326
            # because the default open will not open them and
1327
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1328
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1329
        t = self.get_transport()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1330
        made_control = self.bzrdir_format.initialize(t.base)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1331
        made_repo = made_control.create_repository()
1332
        made_branch = made_control.create_branch()
1333
        made_tree = self.createWorkingTreeOrSkip(made_control)
1334
        self.assertIsInstance(made_tree, workingtree.WorkingTree)
1335
        self.assertEqual(made_control, made_tree.bzrdir)
1336
1337
    def test_create_workingtree_revision(self):
1338
        # a bzrdir can construct a working tree for itself @ a specific revision.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1339
        if not self.bzrdir_format.is_initializable():
1340
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1341
        t = self.get_transport()
1342
        source = self.make_branch_and_tree('source')
1343
        source.commit('a', rev_id='a', allow_pointless=True)
1344
        source.commit('b', rev_id='b', allow_pointless=True)
1345
        t.mkdir('new')
1346
        t_new = t.clone('new')
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1347
        made_control = self.bzrdir_format.initialize_on_transport(t_new)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1348
        source.branch.repository.clone(made_control)
1349
        source.branch.clone(made_control)
1350
        try:
1351
            made_tree = made_control.create_workingtree(revision_id='a')
1352
        except (errors.NotLocalUrl, errors.UnsupportedOperation):
1353
            raise TestSkipped("Can't make working tree on transport %r" % t)
1354
        self.assertEqual(['a'], made_tree.get_parent_ids())
1355
1356
    def test_open_workingtree(self):
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1357
        if not self.bzrdir_format.is_initializable():
1358
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1359
        # this has to be tested with local access as we still support creating
1360
        # format 6 bzrdirs
1361
        t = self.get_transport()
1362
        try:
1363
            made_control = self.bzrdir_format.initialize(t.base)
1364
            made_repo = made_control.create_repository()
1365
            made_branch = made_control.create_branch()
1366
            made_tree = made_control.create_workingtree()
1367
        except (errors.NotLocalUrl, errors.UnsupportedOperation):
1368
            raise TestSkipped("Can't initialize %r on transport %r"
1369
                              % (self.bzrdir_format, t))
1370
        opened_tree = made_control.open_workingtree()
1371
        self.assertEqual(made_control, opened_tree.bzrdir)
1372
        self.assertIsInstance(opened_tree, made_tree.__class__)
1373
        self.assertIsInstance(opened_tree._format, made_tree._format.__class__)
1374
1375
    def test_get_selected_branch(self):
1376
        # The segment parameters are accessible from the root transport
1377
        # if a URL with segment parameters is opened.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1378
        if not self.bzrdir_format.is_initializable():
1379
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1380
        t = self.get_transport()
1381
        try:
1382
            made_control = self.bzrdir_format.initialize(t.base)
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1383
        except (errors.NotLocalUrl, errors.UnsupportedOperation):
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1384
            raise TestSkipped("Can't initialize %r on transport %r"
1385
                              % (self.bzrdir_format, t))
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1386
        dir = controldir.ControlDir.open(t.base+",branch=foo")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1387
        self.assertEqual({"branch": "foo"},
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1388
            dir.user_transport.get_segment_parameters())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1389
        self.assertEqual("foo", dir._get_selected_branch())
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1390
1391
    def test_get_selected_branch_none_selected(self):
1392
        # _get_selected_branch defaults to None
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1393
        if not self.bzrdir_format.is_initializable():
1394
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1395
        t = self.get_transport()
1396
        try:
1397
            made_control = self.bzrdir_format.initialize(t.base)
1398
        except (errors.NotLocalUrl, errors.UnsupportedOperation):
1399
            raise TestSkipped("Can't initialize %r on transport %r"
1400
                              % (self.bzrdir_format, t))
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1401
        dir = controldir.ControlDir.open(t.base)
6436.1.1 by Jelmer Vernooij
Change default branch name to "".
1402
        self.assertEqual(u"", dir._get_selected_branch())
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1403
1404
    def test_root_transport(self):
1405
        dir = self.make_bzrdir('.')
1406
        self.assertEqual(dir.root_transport.base,
1407
                         self.get_transport().base)
1408
1409
    def test_find_repository_no_repo_under_standalone_branch(self):
1410
        # finding a repo stops at standalone branches even if there is a
1411
        # higher repository available.
1412
        try:
1413
            repo = self.make_repository('.', shared=True)
1414
        except errors.IncompatibleFormat:
1415
            # need a shared repository to test this.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1416
            raise TestNotApplicable("requires shared repository support")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
1417
        if not repo._format.supports_nesting_repositories:
1418
            raise TestNotApplicable("requires nesting repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1419
        url = self.get_url('intermediate')
1420
        t = self.get_transport()
1421
        t.mkdir('intermediate')
1422
        t.mkdir('intermediate/child')
1423
        made_control = self.bzrdir_format.initialize(url)
1424
        made_control.create_repository()
1425
        innermost_control = self.bzrdir_format.initialize(
1426
            self.get_url('intermediate/child'))
1427
        try:
1428
            child_repo = innermost_control.open_repository()
1429
            # if there is a repository, then the format cannot ever hit this
1430
            # code path.
1431
            return
1432
        except errors.NoRepositoryPresent:
1433
            pass
1434
        self.assertRaises(errors.NoRepositoryPresent,
1435
                          innermost_control.find_repository)
1436
1437
    def test_find_repository_containing_shared_repository(self):
1438
        # find repo inside a shared repo with an empty control dir
1439
        # returns the shared repo.
1440
        try:
1441
            repo = self.make_repository('.', shared=True)
1442
        except errors.IncompatibleFormat:
1443
            # need a shared repository to test this.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1444
            raise TestNotApplicable("requires format with shared repository "
1445
                "support")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
1446
        if not repo._format.supports_nesting_repositories:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1447
            raise TestNotApplicable("requires support for nesting "
1448
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1449
        url = self.get_url('childbzrdir')
1450
        self.get_transport().mkdir('childbzrdir')
1451
        made_control = self.bzrdir_format.initialize(url)
1452
        try:
1453
            child_repo = made_control.open_repository()
1454
            # if there is a repository, then the format cannot ever hit this
1455
            # code path.
1456
            return
1457
        except errors.NoRepositoryPresent:
1458
            pass
1459
        found_repo = made_control.find_repository()
1460
        self.assertEqual(repo.bzrdir.root_transport.base,
1461
                         found_repo.bzrdir.root_transport.base)
1462
1463
    def test_find_repository_standalone_with_containing_shared_repository(self):
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
1464
        # find repo inside a standalone repo inside a shared repo finds the
1465
        # standalone repo
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1466
        try:
1467
            containing_repo = self.make_repository('.', shared=True)
1468
        except errors.IncompatibleFormat:
1469
            # need a shared repository to test this.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1470
            raise TestNotApplicable("requires support for shared "
1471
                "repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
1472
        if not containing_repo._format.supports_nesting_repositories:
1473
            raise TestNotApplicable("format does not support "
1474
                "nesting repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1475
        child_repo = self.make_repository('childrepo')
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1476
        opened_control = controldir.ControlDir.open(self.get_url('childrepo'))
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1477
        found_repo = opened_control.find_repository()
1478
        self.assertEqual(child_repo.bzrdir.root_transport.base,
1479
                         found_repo.bzrdir.root_transport.base)
1480
1481
    def test_find_repository_shared_within_shared_repository(self):
1482
        # find repo at a shared repo inside a shared repo finds the inner repo
1483
        try:
1484
            containing_repo = self.make_repository('.', shared=True)
1485
        except errors.IncompatibleFormat:
1486
            # need a shared repository to test this.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1487
            raise TestNotApplicable("requires support for shared "
1488
                "repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
1489
        if not containing_repo._format.supports_nesting_repositories:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1490
            raise TestNotApplicable("requires support for nesting "
1491
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1492
        url = self.get_url('childrepo')
1493
        self.get_transport().mkdir('childrepo')
1494
        child_control = self.bzrdir_format.initialize(url)
1495
        child_repo = child_control.create_repository(shared=True)
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1496
        opened_control = controldir.ControlDir.open(self.get_url('childrepo'))
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1497
        found_repo = opened_control.find_repository()
1498
        self.assertEqual(child_repo.bzrdir.root_transport.base,
1499
                         found_repo.bzrdir.root_transport.base)
1500
        self.assertNotEqual(child_repo.bzrdir.root_transport.base,
1501
                            containing_repo.bzrdir.root_transport.base)
1502
1503
    def test_find_repository_with_nested_dirs_works(self):
1504
        # find repo inside a bzrdir inside a bzrdir inside a shared repo
1505
        # finds the outer shared repo.
1506
        try:
1507
            repo = self.make_repository('.', shared=True)
1508
        except errors.IncompatibleFormat:
1509
            # need a shared repository to test this.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1510
            raise TestNotApplicable("requires support for shared "
1511
                "repositories")
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
1512
        if not repo._format.supports_nesting_repositories:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1513
            raise TestNotApplicable("requires support for nesting "
1514
                "repositories")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1515
        url = self.get_url('intermediate')
1516
        t = self.get_transport()
1517
        t.mkdir('intermediate')
1518
        t.mkdir('intermediate/child')
1519
        made_control = self.bzrdir_format.initialize(url)
1520
        try:
1521
            child_repo = made_control.open_repository()
1522
            # if there is a repository, then the format cannot ever hit this
1523
            # code path.
1524
            return
1525
        except errors.NoRepositoryPresent:
1526
            pass
1527
        innermost_control = self.bzrdir_format.initialize(
1528
            self.get_url('intermediate/child'))
1529
        try:
1530
            child_repo = innermost_control.open_repository()
1531
            # if there is a repository, then the format cannot ever hit this
1532
            # code path.
1533
            return
1534
        except errors.NoRepositoryPresent:
1535
            pass
1536
        found_repo = innermost_control.find_repository()
1537
        self.assertEqual(repo.bzrdir.root_transport.base,
1538
                         found_repo.bzrdir.root_transport.base)
1539
1540
    def test_can_and_needs_format_conversion(self):
1541
        # check that we can ask an instance if its upgradable
1542
        dir = self.make_bzrdir('.')
1543
        if dir.can_convert_format():
1544
            # if its default updatable there must be an updater
1545
            # (we force the latest known format as downgrades may not be
1546
            # available
1547
            self.assertTrue(isinstance(dir._format.get_converter(
1548
                format=dir._format), controldir.Converter))
1549
        dir.needs_format_conversion(
1550
            controldir.ControlDirFormat.get_default_format())
1551
1552
    def test_backup_copies_existing(self):
1553
        tree = self.make_branch_and_tree('test')
1554
        self.build_tree(['test/a'])
1555
        tree.add(['a'], ['a-id'])
1556
        tree.commit('some data to be copied.')
1557
        old_url, new_url = tree.bzrdir.backup_bzrdir()
1558
        old_path = urlutils.local_path_from_url(old_url)
1559
        new_path = urlutils.local_path_from_url(new_url)
1560
        self.assertPathExists(old_path)
1561
        self.assertPathExists(new_path)
1562
        for (((dir_relpath1, _), entries1),
1563
             ((dir_relpath2, _), entries2)) in izip(
1564
                osutils.walkdirs(old_path),
1565
                osutils.walkdirs(new_path)):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1566
            self.assertEqual(dir_relpath1, dir_relpath2)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1567
            for f1, f2 in zip(entries1, entries2):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1568
                self.assertEqual(f1[0], f2[0])
1569
                self.assertEqual(f1[2], f2[2])
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1570
                if f1[2] == "file":
1571
                    osutils.compare_files(open(f1[4]), open(f2[4]))
1572
1573
    def test_upgrade_new_instance(self):
1574
        """Does an available updater work?"""
1575
        dir = self.make_bzrdir('.')
1576
        # for now, upgrade is not ready for partial bzrdirs.
1577
        dir.create_repository()
1578
        dir.create_branch()
1579
        self.createWorkingTreeOrSkip(dir)
1580
        if dir.can_convert_format():
1581
            # if its default updatable there must be an updater
1582
            # (we force the latest known format as downgrades may not be
1583
            # available
1584
            pb = ui.ui_factory.nested_progress_bar()
1585
            try:
1586
                dir._format.get_converter(format=dir._format).convert(dir, pb)
1587
            finally:
1588
                pb.finished()
1589
            # and it should pass 'check' now.
1590
            check.check_dwim(self.get_url('.'), False, True, True)
1591
1592
    def test_format_description(self):
1593
        dir = self.make_bzrdir('.')
1594
        text = dir._format.get_format_description()
1595
        self.assertTrue(len(text))
1596
1597
1598
class TestBreakLock(TestCaseWithControlDir):
1599
1600
    def test_break_lock_empty(self):
1601
        # break lock on an empty bzrdir should work silently.
1602
        dir = self.make_bzrdir('.')
1603
        try:
1604
            dir.break_lock()
1605
        except NotImplementedError:
1606
            pass
1607
1608
    def test_break_lock_repository(self):
1609
        # break lock with just a repo should unlock the repo.
1610
        repo = self.make_repository('.')
1611
        repo.lock_write()
1612
        lock_repo = repo.bzrdir.open_repository()
1613
        if not lock_repo.get_physical_lock_status():
1614
            # This bzrdir's default repository does not physically lock things
1615
            # and thus this interaction cannot be tested at the interface
1616
            # level.
1617
            repo.unlock()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1618
            raise TestNotApplicable("format does not physically lock")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1619
        # only one yes needed here: it should only be unlocking
1620
        # the repo
1621
        bzrlib.ui.ui_factory = CannedInputUIFactory([True])
1622
        try:
1623
            repo.bzrdir.break_lock()
1624
        except NotImplementedError:
1625
            # this bzrdir does not implement break_lock - so we cant test it.
1626
            repo.unlock()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1627
            raise TestNotApplicable("format does not support breaking locks")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1628
        lock_repo.lock_write()
1629
        lock_repo.unlock()
1630
        self.assertRaises(errors.LockBroken, repo.unlock)
1631
1632
    def test_break_lock_branch(self):
1633
        # break lock with just a repo should unlock the branch.
1634
        # and not directly try the repository.
1635
        # we test this by making a branch reference to a branch
1636
        # and repository in another bzrdir
1637
        # for pre-metadir formats this will fail, thats ok.
1638
        master = self.make_branch('branch')
1639
        thisdir = self.make_bzrdir('this')
1640
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
1641
            thisdir.set_branch_reference(master)
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1642
        except errors.IncompatibleFormat:
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1643
            raise TestNotApplicable("format does not support "
1644
                "branch references")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1645
        unused_repo = thisdir.create_repository()
1646
        master.lock_write()
1647
        unused_repo.lock_write()
1648
        try:
1649
            # two yes's : branch and repository. If the repo in this
1650
            # dir is inappropriately accessed, 3 will be needed, and
1651
            # we'll see that because the stream will be fully consumed
1652
            bzrlib.ui.ui_factory = CannedInputUIFactory([True, True, True])
1653
            # determine if the repository will have been locked;
1654
            this_repo_locked = \
1655
                thisdir.open_repository().get_physical_lock_status()
1656
            master.bzrdir.break_lock()
1657
            if this_repo_locked:
1658
                # only two ys should have been read
1659
                self.assertEqual([True],
1660
                    bzrlib.ui.ui_factory.responses)
1661
            else:
1662
                # only one y should have been read
1663
                self.assertEqual([True, True],
1664
                    bzrlib.ui.ui_factory.responses)
1665
            # we should be able to lock a newly opened branch now
1666
            branch = master.bzrdir.open_branch()
1667
            branch.lock_write()
1668
            branch.unlock()
1669
            if this_repo_locked:
1670
                # we should not be able to lock the repository in thisdir as
1671
                # its still held by the explicit lock we took, and the break
1672
                # lock should not have touched it.
1673
                repo = thisdir.open_repository()
1674
                self.assertRaises(errors.LockContention, repo.lock_write)
1675
        finally:
1676
            unused_repo.unlock()
1677
        self.assertRaises(errors.LockBroken, master.unlock)
1678
1679
    def test_break_lock_tree(self):
1680
        # break lock with a tree should unlock the tree but not try the
1681
        # branch explicitly. However this is very hard to test for as we
1682
        # dont have a tree reference class, nor is one needed;
1683
        # the worst case if this code unlocks twice is an extra question
1684
        # being asked.
1685
        tree = self.make_branch_and_tree('.')
1686
        tree.lock_write()
1687
        # three yes's : tree, branch and repository.
1688
        bzrlib.ui.ui_factory = CannedInputUIFactory([True, True, True])
1689
        try:
1690
            tree.bzrdir.break_lock()
1691
        except (NotImplementedError, errors.LockActive):
1692
            # bzrdir does not support break_lock
1693
            # or one of the locked objects (currently only tree does this)
1694
            # raised a LockActive because we do still have a live locked
1695
            # object.
1696
            tree.unlock()
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1697
            raise TestNotApplicable("format does not support breaking locks")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1698
        self.assertEqual([True],
1699
                bzrlib.ui.ui_factory.responses)
1700
        lock_tree = tree.bzrdir.open_workingtree()
1701
        lock_tree.lock_write()
1702
        lock_tree.unlock()
1703
        self.assertRaises(errors.LockBroken, tree.unlock)
1704
1705
1706
class TestTransportConfig(TestCaseWithControlDir):
1707
1708
    def test_get_config(self):
1709
        my_dir = self.make_bzrdir('.')
1710
        config = my_dir.get_config()
1711
        try:
1712
            config.set_default_stack_on('http://example.com')
1713
        except errors.BzrError, e:
1714
            if 'Cannot set config' in str(e):
1715
                self.assertFalse(
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1716
                    isinstance(my_dir, (_mod_bzrdir.BzrDirMeta1, RemoteBzrDir)),
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1717
                    "%r should support configs" % my_dir)
1718
                raise TestNotApplicable(
1719
                    'This BzrDir format does not support configs.')
1720
            else:
1721
                raise
1722
        self.assertEqual('http://example.com', config.get_default_stack_on())
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1723
        my_dir2 = controldir.ControlDir.open(self.get_url('.'))
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1724
        config2 = my_dir2.get_config()
1725
        self.assertEqual('http://example.com', config2.get_default_stack_on())
1726
1727
1728
class ChrootedControlDirTests(ChrootedTestCase):
1729
1730
    def test_find_repository_no_repository(self):
1731
        # loopback test to check the current format fails to find a
1732
        # share repository correctly.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1733
        if not self.bzrdir_format.is_initializable():
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1734
            # unsupported formats are not loopback testable
1735
            # because the default open will not open them and
1736
            # they may not be initializable.
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1737
            raise TestNotApplicable("format is not initializable")
6083.1.2 by Jelmer Vernooij
Revert unnecessary changes.
1738
        # supported formats must be able to init and open
1739
        # - do the vfs initialisation over the basic vfs transport
1740
        # XXX: TODO this should become a 'bzrdirlocation' api call.
1741
        url = self.get_vfs_only_url('subdir')
1742
        transport.get_transport_from_url(self.get_vfs_only_url()).mkdir('subdir')
6162.3.4 by Jelmer Vernooij
Use TestNotApplicable rather than returning directly.
1743
        made_control = self.bzrdir_format.initialize(self.get_url('subdir'))
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1744
        try:
1745
            repo = made_control.open_repository()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1746
            # if there is a repository, then the format cannot ever hit this
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1747
            # code path.
1748
            return
1749
        except errors.NoRepositoryPresent:
1750
            pass
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
1751
        made_control = controldir.ControlDir.open(self.get_readonly_url('subdir'))
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1752
        self.assertRaises(errors.NoRepositoryPresent,
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1753
                          made_control.find_repository)
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1754
5158.6.1 by Martin Pool
Add ControlComponent interface and make BzrDir implement it
1755
5363.2.22 by Jelmer Vernooij
Provide bzrlib.bzrdir.format_registry.
1756
class TestControlDirControlComponent(TestCaseWithControlDir):
1757
    """ControlDir implementations adequately implement ControlComponent."""
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1758
5158.6.1 by Martin Pool
Add ControlComponent interface and make BzrDir implement it
1759
    def test_urls(self):
1760
        bd = self.make_bzrdir('bd')
1761
        self.assertIsInstance(bd.user_url, str)
1762
        self.assertEqual(bd.user_url, bd.user_transport.base)
1763
        # for all current bzrdir implementations the user dir must be 
1764
        # above the control dir but we might need to relax that?
1765
        self.assertEqual(bd.control_url.find(bd.user_url), 0)
1766
        self.assertEqual(bd.control_url, bd.control_transport.base)