/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the BzrDir facility and any format specific tests.
18
18
 
19
 
For interface contract tests, see tests/per_bzr_dir.
 
19
For interface contract tests, see tests/bzr_dir_implementations.
20
20
"""
21
21
 
22
 
import os
23
 
import subprocess
24
 
import sys
 
22
import os.path
 
23
from StringIO import StringIO
25
24
 
26
25
from bzrlib import (
27
26
    bzrdir,
28
27
    errors,
29
28
    help_topics,
30
29
    repository,
31
 
    osutils,
32
 
    remote,
 
30
    symbol_versioning,
33
31
    urlutils,
34
 
    win32utils,
35
32
    workingtree,
36
33
    )
37
34
import bzrlib.branch
38
35
from bzrlib.errors import (NotBranchError,
39
 
                           NoColocatedBranchSupport,
40
36
                           UnknownFormatError,
41
37
                           UnsupportedFormatError,
42
38
                           )
43
 
from bzrlib.tests import (
44
 
    TestCase,
45
 
    TestCaseWithMemoryTransport,
46
 
    TestCaseWithTransport,
47
 
    TestSkipped,
48
 
    )
49
 
from bzrlib.tests import(
50
 
    http_server,
51
 
    http_utils,
52
 
    )
53
 
from bzrlib.tests.test_http import TestWithTransport_pycurl
54
 
from bzrlib.transport import (
55
 
    get_transport,
56
 
    memory,
57
 
    )
58
 
from bzrlib.transport.http._urllib import HttpTransport_urllib
59
 
from bzrlib.transport.nosmart import NoSmartTransportDecorator
60
 
from bzrlib.transport.readonly import ReadonlyTransportDecorator
61
 
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
 
39
from bzrlib.tests import TestCase, TestCaseWithTransport, test_sftp_transport
 
40
from bzrlib.tests.HttpServer import HttpServer
 
41
from bzrlib.transport import get_transport
 
42
from bzrlib.transport.memory import MemoryServer
 
43
from bzrlib.repofmt import knitrepo, weaverepo
62
44
 
63
45
 
64
46
class TestDefaultFormat(TestCase):
67
49
        old_format = bzrdir.BzrDirFormat.get_default_format()
68
50
        # default is BzrDirFormat6
69
51
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
70
 
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
 
52
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
 
53
                             bzrdir.BzrDirFormat.set_default_format, 
 
54
                             SampleBzrDirFormat())
71
55
        # creating a bzr dir should now create an instrumented dir.
72
56
        try:
73
57
            result = bzrdir.BzrDir.create('memory:///')
74
58
            self.failUnless(isinstance(result, SampleBzrDir))
75
59
        finally:
76
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
60
            self.applyDeprecated(symbol_versioning.zero_fourteen,
 
61
                bzrdir.BzrDirFormat.set_default_format, old_format)
77
62
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
78
63
 
79
64
 
84
69
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
85
70
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
86
71
            ' repositories', deprecated=True)
87
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
72
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
88
73
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
89
74
        my_format_registry.register_metadir('knit',
90
75
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
95
80
            'branch6',
96
81
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
97
82
            'Experimental successor to knit.  Use at your own risk.',
98
 
            branch_format='bzrlib.branch.BzrBranchFormat6',
99
 
            experimental=True)
100
 
        my_format_registry.register_metadir(
101
 
            'hidden format',
102
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
103
 
            'Experimental successor to knit.  Use at your own risk.',
104
 
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
105
 
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
106
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
107
 
            ' repositories', hidden=True)
108
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
109
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
110
 
            hidden=True)
 
83
            branch_format='bzrlib.branch.BzrBranchFormat6')
111
84
        return my_format_registry
112
85
 
113
86
    def test_format_registry(self):
117
90
        my_bzrdir = my_format_registry.make_bzrdir('weave')
118
91
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
119
92
        my_bzrdir = my_format_registry.make_bzrdir('default')
120
 
        self.assertIsInstance(my_bzrdir.repository_format,
 
93
        self.assertIsInstance(my_bzrdir.repository_format, 
121
94
            knitrepo.RepositoryFormatKnit1)
122
95
        my_bzrdir = my_format_registry.make_bzrdir('knit')
123
 
        self.assertIsInstance(my_bzrdir.repository_format,
 
96
        self.assertIsInstance(my_bzrdir.repository_format, 
124
97
            knitrepo.RepositoryFormatKnit1)
125
98
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
126
99
        self.assertIsInstance(my_bzrdir.get_branch_format(),
130
103
        my_format_registry = self.make_format_registry()
131
104
        self.assertEqual('Format registered lazily',
132
105
                         my_format_registry.get_help('lazy'))
133
 
        self.assertEqual('Format using knits',
 
106
        self.assertEqual('Format using knits', 
134
107
                         my_format_registry.get_help('knit'))
135
 
        self.assertEqual('Format using knits',
 
108
        self.assertEqual('Format using knits', 
136
109
                         my_format_registry.get_help('default'))
137
110
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
138
 
                         ' checkouts or shared repositories',
 
111
                         ' checkouts or shared repositories', 
139
112
                         my_format_registry.get_help('weave'))
140
 
 
 
113
        
141
114
    def test_help_topic(self):
142
115
        topics = help_topics.HelpTopicRegistry()
143
 
        registry = self.make_format_registry()
144
 
        topics.register('current-formats', registry.help_topic,
145
 
                        'Current formats')
146
 
        topics.register('other-formats', registry.help_topic,
147
 
                        'Other formats')
148
 
        new = topics.get_detail('current-formats')
149
 
        rest = topics.get_detail('other-formats')
150
 
        experimental, deprecated = rest.split('Deprecated formats')
151
 
        self.assertContainsRe(new, 'formats-help')
152
 
        self.assertContainsRe(new,
153
 
                ':knit:\n    \(native\) \(default\) Format using knits\n')
154
 
        self.assertContainsRe(experimental,
155
 
                ':branch6:\n    \(native\) Experimental successor to knit')
156
 
        self.assertContainsRe(deprecated,
157
 
                ':lazy:\n    \(native\) Format registered lazily\n')
158
 
        self.assertNotContainsRe(new, 'hidden')
 
116
        topics.register('formats', self.make_format_registry().help_topic, 
 
117
                        'Directory formats')
 
118
        topic = topics.get_detail('formats')
 
119
        new, deprecated = topic.split('Deprecated formats')
 
120
        self.assertContainsRe(new, 'Bazaar directory formats')
 
121
        self.assertContainsRe(new, 
 
122
            '  knit/default:\n    \(native\) Format using knits\n')
 
123
        self.assertContainsRe(deprecated, 
 
124
            '  lazy:\n    \(native\) Format registered lazily\n')
159
125
 
160
126
    def test_set_default_repository(self):
161
127
        default_factory = bzrdir.format_registry.get('default')
171
137
        finally:
172
138
            bzrdir.format_registry.set_default_repository(old_default)
173
139
 
174
 
    def test_aliases(self):
175
 
        a_registry = bzrdir.BzrDirFormatRegistry()
176
 
        a_registry.register('weave', bzrdir.BzrDirFormat6,
177
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
178
 
            ' repositories', deprecated=True)
179
 
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
180
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
181
 
            ' repositories', deprecated=True, alias=True)
182
 
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
183
 
 
184
140
 
185
141
class SampleBranch(bzrlib.branch.Branch):
186
142
    """A dummy branch for guess what, dummy use."""
189
145
        self.bzrdir = dir
190
146
 
191
147
 
192
 
class SampleRepository(bzrlib.repository.Repository):
193
 
    """A dummy repo."""
194
 
 
195
 
    def __init__(self, dir):
196
 
        self.bzrdir = dir
197
 
 
198
 
 
199
148
class SampleBzrDir(bzrdir.BzrDir):
200
149
    """A sample BzrDir implementation to allow testing static methods."""
201
150
 
205
154
 
206
155
    def open_repository(self):
207
156
        """See BzrDir.open_repository."""
208
 
        return SampleRepository(self)
 
157
        return "A repository"
209
158
 
210
 
    def create_branch(self, name=None):
 
159
    def create_branch(self):
211
160
        """See BzrDir.create_branch."""
212
 
        if name is not None:
213
 
            raise NoColocatedBranchSupport(self)
214
161
        return SampleBranch(self)
215
162
 
216
163
    def create_workingtree(self):
221
168
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
222
169
    """A sample format
223
170
 
224
 
    this format is initializable, unsupported to aid in testing the
 
171
    this format is initializable, unsupported to aid in testing the 
225
172
    open and open_downlevel routines.
226
173
    """
227
174
 
229
176
        """See BzrDirFormat.get_format_string()."""
230
177
        return "Sample .bzr dir format."
231
178
 
232
 
    def initialize_on_transport(self, t):
 
179
    def initialize(self, url):
233
180
        """Create a bzr dir."""
 
181
        t = get_transport(url)
234
182
        t.mkdir('.bzr')
235
183
        t.put_bytes('.bzr/branch-format', self.get_format_string())
236
184
        return SampleBzrDir(t, self)
248
196
    def test_find_format(self):
249
197
        # is the right format object found for a branch?
250
198
        # create a branch with a few known format objects.
251
 
        # this is not quite the same as
 
199
        # this is not quite the same as 
252
200
        t = get_transport(self.get_url())
253
201
        self.build_tree(["foo/", "bar/"], transport=t)
254
202
        def check_format(format, url):
258
206
            self.failUnless(isinstance(found_format, format.__class__))
259
207
        check_format(bzrdir.BzrDirFormat5(), "foo")
260
208
        check_format(bzrdir.BzrDirFormat6(), "bar")
261
 
 
 
209
        
262
210
    def test_find_format_nothing_there(self):
263
211
        self.assertRaises(NotBranchError,
264
212
                          bzrdir.BzrDirFormat.find_format,
291
239
        # now open_downlevel should fail too.
292
240
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
293
241
 
 
242
    def test_create_repository(self):
 
243
        format = SampleBzrDirFormat()
 
244
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
 
245
        self.assertEqual('A repository', repo)
 
246
 
 
247
    def test_create_repository_shared(self):
 
248
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
249
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
 
250
        self.assertTrue(repo.is_shared())
 
251
 
 
252
    def test_create_repository_nonshared(self):
 
253
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
254
        repo = bzrdir.BzrDir.create_repository('.')
 
255
        self.assertFalse(repo.is_shared())
 
256
 
 
257
    def test_create_repository_under_shared(self):
 
258
        # an explicit create_repository always does so.
 
259
        # we trust the format is right from the 'create_repository test'
 
260
        format = bzrdir.format_registry.make_bzrdir('knit')
 
261
        self.make_repository('.', shared=True, format=format)
 
262
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
 
263
                                               format=format)
 
264
        self.assertTrue(isinstance(repo, repository.Repository))
 
265
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
266
 
294
267
    def test_create_branch_and_repo_uses_default(self):
295
268
        format = SampleBzrDirFormat()
296
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
 
269
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
297
270
                                                      format=format)
298
271
        self.assertTrue(isinstance(branch, SampleBranch))
299
272
 
308
281
                          branch.bzrdir.open_repository)
309
282
 
310
283
    def test_create_branch_and_repo_under_shared_force_new(self):
311
 
        # creating a branch and repo in a shared repo can be forced to
 
284
        # creating a branch and repo in a shared repo can be forced to 
312
285
        # make a new repo
313
286
        format = bzrdir.format_registry.make_bzrdir('knit')
314
287
        self.make_repository('.', shared=True, format=format)
319
292
 
320
293
    def test_create_standalone_working_tree(self):
321
294
        format = SampleBzrDirFormat()
322
 
        # note this is deliberately readonly, as this failure should
 
295
        # note this is deliberately readonly, as this failure should 
323
296
        # occur before any writes.
324
297
        self.assertRaises(errors.NotLocalUrl,
325
298
                          bzrdir.BzrDir.create_standalone_workingtree,
326
299
                          self.get_readonly_url(), format=format)
327
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.',
 
300
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
328
301
                                                           format=format)
329
302
        self.assertEqual('A tree', tree)
330
303
 
332
305
        # create standalone working tree always makes a repo.
333
306
        format = bzrdir.format_registry.make_bzrdir('knit')
334
307
        self.make_repository('.', shared=True, format=format)
335
 
        # note this is deliberately readonly, as this failure should
 
308
        # note this is deliberately readonly, as this failure should 
336
309
        # occur before any writes.
337
310
        self.assertRaises(errors.NotLocalUrl,
338
311
                          bzrdir.BzrDir.create_standalone_workingtree,
339
312
                          self.get_readonly_url('child'), format=format)
340
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
 
313
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
341
314
            format=format)
342
315
        tree.bzrdir.open_repository()
343
316
 
348
321
        branch.bzrdir.open_workingtree()
349
322
        branch.bzrdir.open_repository()
350
323
 
351
 
    def test_create_branch_convenience_possible_transports(self):
352
 
        """Check that the optional 'possible_transports' is recognized"""
353
 
        format = bzrdir.format_registry.make_bzrdir('knit')
354
 
        t = self.get_transport()
355
 
        branch = bzrdir.BzrDir.create_branch_convenience(
356
 
            '.', format=format, possible_transports=[t])
357
 
        branch.bzrdir.open_workingtree()
358
 
        branch.bzrdir.open_repository()
359
 
 
360
324
    def test_create_branch_convenience_root(self):
361
325
        """Creating a branch at the root of a fs should work."""
362
 
        self.vfs_transport_factory = memory.MemoryServer
 
326
        self.transport_server = MemoryServer
363
327
        # outside a repo the default convenience output is a repo+branch_tree
364
328
        format = bzrdir.format_registry.make_bzrdir('knit')
365
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
 
329
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
366
330
                                                         format=format)
367
331
        self.assertRaises(errors.NoWorkingTree,
368
332
                          branch.bzrdir.open_workingtree)
378
342
        branch.bzrdir.open_workingtree()
379
343
        self.assertRaises(errors.NoRepositoryPresent,
380
344
                          branch.bzrdir.open_repository)
381
 
 
 
345
            
382
346
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
383
347
        # inside a repo the default convenience output is a branch+ follow the
384
348
        # repo tree policy but we can override that
390
354
                          branch.bzrdir.open_workingtree)
391
355
        self.assertRaises(errors.NoRepositoryPresent,
392
356
                          branch.bzrdir.open_repository)
393
 
 
 
357
            
394
358
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
395
359
        # inside a repo the default convenience output is a branch+ follow the
396
360
        # repo tree policy
397
361
        format = bzrdir.format_registry.make_bzrdir('knit')
398
362
        repo = self.make_repository('.', shared=True, format=format)
399
363
        repo.set_make_working_trees(False)
400
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
364
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
401
365
                                                         format=format)
402
366
        self.assertRaises(errors.NoWorkingTree,
403
367
                          branch.bzrdir.open_workingtree)
427
391
        branch.bzrdir.open_workingtree()
428
392
 
429
393
 
430
 
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
431
 
 
432
 
    def test_acquire_repository_standalone(self):
433
 
        """The default acquisition policy should create a standalone branch."""
434
 
        my_bzrdir = self.make_bzrdir('.')
435
 
        repo_policy = my_bzrdir.determine_repository_policy()
436
 
        repo, is_new = repo_policy.acquire_repository()
437
 
        self.assertEqual(repo.bzrdir.root_transport.base,
438
 
                         my_bzrdir.root_transport.base)
439
 
        self.assertFalse(repo.is_shared())
440
 
 
441
 
    def test_determine_stacking_policy(self):
442
 
        parent_bzrdir = self.make_bzrdir('.')
443
 
        child_bzrdir = self.make_bzrdir('child')
444
 
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
445
 
        repo_policy = child_bzrdir.determine_repository_policy()
446
 
        self.assertEqual('http://example.org', repo_policy._stack_on)
447
 
 
448
 
    def test_determine_stacking_policy_relative(self):
449
 
        parent_bzrdir = self.make_bzrdir('.')
450
 
        child_bzrdir = self.make_bzrdir('child')
451
 
        parent_bzrdir.get_config().set_default_stack_on('child2')
452
 
        repo_policy = child_bzrdir.determine_repository_policy()
453
 
        self.assertEqual('child2', repo_policy._stack_on)
454
 
        self.assertEqual(parent_bzrdir.root_transport.base,
455
 
                         repo_policy._stack_on_pwd)
456
 
 
457
 
    def prepare_default_stacking(self, child_format='1.6'):
458
 
        parent_bzrdir = self.make_bzrdir('.')
459
 
        child_branch = self.make_branch('child', format=child_format)
460
 
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
461
 
        new_child_transport = parent_bzrdir.transport.clone('child2')
462
 
        return child_branch, new_child_transport
463
 
 
464
 
    def test_clone_on_transport_obeys_stacking_policy(self):
465
 
        child_branch, new_child_transport = self.prepare_default_stacking()
466
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
467
 
        self.assertEqual(child_branch.base,
468
 
                         new_child.open_branch().get_stacked_on_url())
469
 
 
470
 
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
471
 
        # Make stackable source branch with an unstackable repo format.
472
 
        source_bzrdir = self.make_bzrdir('source')
473
 
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
474
 
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
475
 
            source_bzrdir)
476
 
        # Make a directory with a default stacking policy
477
 
        parent_bzrdir = self.make_bzrdir('parent')
478
 
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
479
 
        parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
480
 
        # Clone source into directory
481
 
        target = source_bzrdir.clone(self.get_url('parent/target'))
482
 
 
483
 
    def test_sprout_obeys_stacking_policy(self):
484
 
        child_branch, new_child_transport = self.prepare_default_stacking()
485
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
486
 
        self.assertEqual(child_branch.base,
487
 
                         new_child.open_branch().get_stacked_on_url())
488
 
 
489
 
    def test_clone_ignores_policy_for_unsupported_formats(self):
490
 
        child_branch, new_child_transport = self.prepare_default_stacking(
491
 
            child_format='pack-0.92')
492
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
493
 
        self.assertRaises(errors.UnstackableBranchFormat,
494
 
                          new_child.open_branch().get_stacked_on_url)
495
 
 
496
 
    def test_sprout_ignores_policy_for_unsupported_formats(self):
497
 
        child_branch, new_child_transport = self.prepare_default_stacking(
498
 
            child_format='pack-0.92')
499
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
500
 
        self.assertRaises(errors.UnstackableBranchFormat,
501
 
                          new_child.open_branch().get_stacked_on_url)
502
 
 
503
 
    def test_sprout_upgrades_format_if_stacked_specified(self):
504
 
        child_branch, new_child_transport = self.prepare_default_stacking(
505
 
            child_format='pack-0.92')
506
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
507
 
                                               stacked=True)
508
 
        self.assertEqual(child_branch.bzrdir.root_transport.base,
509
 
                         new_child.open_branch().get_stacked_on_url())
510
 
        repo = new_child.open_repository()
511
 
        self.assertTrue(repo._format.supports_external_lookups)
512
 
        self.assertFalse(repo.supports_rich_root())
513
 
 
514
 
    def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
515
 
        child_branch, new_child_transport = self.prepare_default_stacking(
516
 
            child_format='pack-0.92')
517
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
518
 
            stacked_on=child_branch.bzrdir.root_transport.base)
519
 
        self.assertEqual(child_branch.bzrdir.root_transport.base,
520
 
                         new_child.open_branch().get_stacked_on_url())
521
 
        repo = new_child.open_repository()
522
 
        self.assertTrue(repo._format.supports_external_lookups)
523
 
        self.assertFalse(repo.supports_rich_root())
524
 
 
525
 
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
526
 
        child_branch, new_child_transport = self.prepare_default_stacking(
527
 
            child_format='rich-root-pack')
528
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
529
 
                                               stacked=True)
530
 
        repo = new_child.open_repository()
531
 
        self.assertTrue(repo._format.supports_external_lookups)
532
 
        self.assertTrue(repo.supports_rich_root())
533
 
 
534
 
    def test_add_fallback_repo_handles_absolute_urls(self):
535
 
        stack_on = self.make_branch('stack_on', format='1.6')
536
 
        repo = self.make_repository('repo', format='1.6')
537
 
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
538
 
        policy._add_fallback(repo)
539
 
 
540
 
    def test_add_fallback_repo_handles_relative_urls(self):
541
 
        stack_on = self.make_branch('stack_on', format='1.6')
542
 
        repo = self.make_repository('repo', format='1.6')
543
 
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
544
 
        policy._add_fallback(repo)
545
 
 
546
 
    def test_configure_relative_branch_stacking_url(self):
547
 
        stack_on = self.make_branch('stack_on', format='1.6')
548
 
        stacked = self.make_branch('stack_on/stacked', format='1.6')
549
 
        policy = bzrdir.UseExistingRepository(stacked.repository,
550
 
            '.', stack_on.base)
551
 
        policy.configure_branch(stacked)
552
 
        self.assertEqual('..', stacked.get_stacked_on_url())
553
 
 
554
 
    def test_relative_branch_stacking_to_absolute(self):
555
 
        stack_on = self.make_branch('stack_on', format='1.6')
556
 
        stacked = self.make_branch('stack_on/stacked', format='1.6')
557
 
        policy = bzrdir.UseExistingRepository(stacked.repository,
558
 
            '.', self.get_readonly_url('stack_on'))
559
 
        policy.configure_branch(stacked)
560
 
        self.assertEqual(self.get_readonly_url('stack_on'),
561
 
                         stacked.get_stacked_on_url())
562
 
 
563
 
 
564
394
class ChrootedTests(TestCaseWithTransport):
565
395
    """A support class that provides readonly urls outside the local namespace.
566
396
 
571
401
 
572
402
    def setUp(self):
573
403
        super(ChrootedTests, self).setUp()
574
 
        if not self.vfs_transport_factory == memory.MemoryServer:
575
 
            self.transport_readonly_server = http_server.HttpServer
576
 
 
577
 
    def local_branch_path(self, branch):
578
 
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
 
404
        if not self.transport_server == MemoryServer:
 
405
            self.transport_readonly_server = HttpServer
579
406
 
580
407
    def test_open_containing(self):
581
408
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
588
415
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
589
416
        self.assertEqual('g/p/q', relpath)
590
417
 
591
 
    def test_open_containing_tree_branch_or_repository_empty(self):
592
 
        self.assertRaises(errors.NotBranchError,
593
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
594
 
            self.get_readonly_url(''))
595
 
 
596
 
    def test_open_containing_tree_branch_or_repository_all(self):
597
 
        self.make_branch_and_tree('topdir')
598
 
        tree, branch, repo, relpath = \
599
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
600
 
                'topdir/foo')
601
 
        self.assertEqual(os.path.realpath('topdir'),
602
 
                         os.path.realpath(tree.basedir))
603
 
        self.assertEqual(os.path.realpath('topdir'),
604
 
                         self.local_branch_path(branch))
605
 
        self.assertEqual(
606
 
            osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
607
 
            repo.bzrdir.transport.local_abspath('repository'))
608
 
        self.assertEqual(relpath, 'foo')
609
 
 
610
 
    def test_open_containing_tree_branch_or_repository_no_tree(self):
611
 
        self.make_branch('branch')
612
 
        tree, branch, repo, relpath = \
613
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
614
 
                'branch/foo')
615
 
        self.assertEqual(tree, None)
616
 
        self.assertEqual(os.path.realpath('branch'),
617
 
                         self.local_branch_path(branch))
618
 
        self.assertEqual(
619
 
            osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
620
 
            repo.bzrdir.transport.local_abspath('repository'))
621
 
        self.assertEqual(relpath, 'foo')
622
 
 
623
 
    def test_open_containing_tree_branch_or_repository_repo(self):
624
 
        self.make_repository('repo')
625
 
        tree, branch, repo, relpath = \
626
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
627
 
                'repo')
628
 
        self.assertEqual(tree, None)
629
 
        self.assertEqual(branch, None)
630
 
        self.assertEqual(
631
 
            osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
632
 
            repo.bzrdir.transport.local_abspath('repository'))
633
 
        self.assertEqual(relpath, '')
634
 
 
635
 
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
636
 
        self.make_repository('shared', shared=True)
637
 
        bzrdir.BzrDir.create_branch_convenience('shared/branch',
638
 
                                                force_new_tree=False)
639
 
        tree, branch, repo, relpath = \
640
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
641
 
                'shared/branch')
642
 
        self.assertEqual(tree, None)
643
 
        self.assertEqual(os.path.realpath('shared/branch'),
644
 
                         self.local_branch_path(branch))
645
 
        self.assertEqual(
646
 
            osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
647
 
            repo.bzrdir.transport.local_abspath('repository'))
648
 
        self.assertEqual(relpath, '')
649
 
 
650
 
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
651
 
        self.make_branch_and_tree('foo')
652
 
        self.build_tree(['foo/bar/'])
653
 
        tree, branch, repo, relpath = \
654
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
655
 
                'foo/bar')
656
 
        self.assertEqual(os.path.realpath('foo'),
657
 
                         os.path.realpath(tree.basedir))
658
 
        self.assertEqual(os.path.realpath('foo'),
659
 
                         self.local_branch_path(branch))
660
 
        self.assertEqual(
661
 
            osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
662
 
            repo.bzrdir.transport.local_abspath('repository'))
663
 
        self.assertEqual(relpath, 'bar')
664
 
 
665
 
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
666
 
        self.make_repository('bar')
667
 
        self.build_tree(['bar/baz/'])
668
 
        tree, branch, repo, relpath = \
669
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
670
 
                'bar/baz')
671
 
        self.assertEqual(tree, None)
672
 
        self.assertEqual(branch, None)
673
 
        self.assertEqual(
674
 
            osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
675
 
            repo.bzrdir.transport.local_abspath('repository'))
676
 
        self.assertEqual(relpath, 'baz')
677
 
 
678
418
    def test_open_containing_from_transport(self):
679
419
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
680
420
                          get_transport(self.get_readonly_url('')))
689
429
        self.assertEqual('g/p/q', relpath)
690
430
 
691
431
    def test_open_containing_tree_or_branch(self):
 
432
        def local_branch_path(branch):
 
433
             return os.path.realpath(
 
434
                urlutils.local_path_from_url(branch.base))
 
435
 
692
436
        self.make_branch_and_tree('topdir')
693
437
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
694
438
            'topdir/foo')
695
439
        self.assertEqual(os.path.realpath('topdir'),
696
440
                         os.path.realpath(tree.basedir))
697
441
        self.assertEqual(os.path.realpath('topdir'),
698
 
                         self.local_branch_path(branch))
 
442
                         local_branch_path(branch))
699
443
        self.assertIs(tree.bzrdir, branch.bzrdir)
700
444
        self.assertEqual('foo', relpath)
701
 
        # opening from non-local should not return the tree
702
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
703
 
            self.get_readonly_url('topdir/foo'))
704
 
        self.assertEqual(None, tree)
705
 
        self.assertEqual('foo', relpath)
706
 
        # without a tree:
707
445
        self.make_branch('topdir/foo')
708
446
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
709
447
            'topdir/foo')
710
448
        self.assertIs(tree, None)
711
449
        self.assertEqual(os.path.realpath('topdir/foo'),
712
 
                         self.local_branch_path(branch))
 
450
                         local_branch_path(branch))
713
451
        self.assertEqual('', relpath)
714
452
 
715
 
    def test_open_tree_or_branch(self):
716
 
        self.make_branch_and_tree('topdir')
717
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
718
 
        self.assertEqual(os.path.realpath('topdir'),
719
 
                         os.path.realpath(tree.basedir))
720
 
        self.assertEqual(os.path.realpath('topdir'),
721
 
                         self.local_branch_path(branch))
722
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
723
 
        # opening from non-local should not return the tree
724
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
725
 
            self.get_readonly_url('topdir'))
726
 
        self.assertEqual(None, tree)
727
 
        # without a tree:
728
 
        self.make_branch('topdir/foo')
729
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
730
 
        self.assertIs(tree, None)
731
 
        self.assertEqual(os.path.realpath('topdir/foo'),
732
 
                         self.local_branch_path(branch))
733
 
 
734
453
    def test_open_from_transport(self):
735
454
        # transport pointing at bzrdir should give a bzrdir with root transport
736
455
        # set to the given transport
739
458
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
740
459
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
741
460
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
742
 
 
 
461
        
743
462
    def test_open_from_transport_no_bzrdir(self):
744
463
        transport = get_transport(self.get_url())
745
464
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
754
473
                          transport)
755
474
 
756
475
    def test_sprout_recursive(self):
757
 
        tree = self.make_branch_and_tree('tree1',
758
 
                                         format='dirstate-with-subtree')
 
476
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
759
477
        sub_tree = self.make_branch_and_tree('tree1/subtree',
760
478
            format='dirstate-with-subtree')
761
 
        sub_tree.set_root_id('subtree-root')
762
479
        tree.add_reference(sub_tree)
763
480
        self.build_tree(['tree1/subtree/file'])
764
481
        sub_tree.add('file')
765
482
        tree.commit('Initial commit')
766
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
767
 
        tree2.lock_read()
768
 
        self.addCleanup(tree2.unlock)
 
483
        tree.bzrdir.sprout('tree2')
769
484
        self.failUnlessExists('tree2/subtree/file')
770
 
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
771
485
 
772
486
    def test_cloning_metadir(self):
773
487
        """Ensure that cloning metadir is suitable"""
795
509
        self.failUnlessExists('repo/tree2/subtree')
796
510
        self.failIfExists('repo/tree2/subtree/file')
797
511
 
798
 
    def make_foo_bar_baz(self):
799
 
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
800
 
        bar = self.make_branch('foo/bar').bzrdir
801
 
        baz = self.make_branch('baz').bzrdir
802
 
        return foo, bar, baz
803
 
 
804
 
    def test_find_bzrdirs(self):
805
 
        foo, bar, baz = self.make_foo_bar_baz()
806
 
        transport = get_transport(self.get_url())
807
 
        self.assertEqualBzrdirs([baz, foo, bar],
808
 
                                bzrdir.BzrDir.find_bzrdirs(transport))
809
 
 
810
 
    def test_find_bzrdirs_list_current(self):
811
 
        def list_current(transport):
812
 
            return [s for s in transport.list_dir('') if s != 'baz']
813
 
 
814
 
        foo, bar, baz = self.make_foo_bar_baz()
815
 
        transport = get_transport(self.get_url())
816
 
        self.assertEqualBzrdirs([foo, bar],
817
 
                                bzrdir.BzrDir.find_bzrdirs(transport,
818
 
                                    list_current=list_current))
819
 
 
820
 
 
821
 
    def test_find_bzrdirs_evaluate(self):
822
 
        def evaluate(bzrdir):
823
 
            try:
824
 
                repo = bzrdir.open_repository()
825
 
            except NoRepositoryPresent:
826
 
                return True, bzrdir.root_transport.base
827
 
            else:
828
 
                return False, bzrdir.root_transport.base
829
 
 
830
 
        foo, bar, baz = self.make_foo_bar_baz()
831
 
        transport = get_transport(self.get_url())
832
 
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
833
 
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
834
 
                                                         evaluate=evaluate)))
835
 
 
836
 
    def assertEqualBzrdirs(self, first, second):
837
 
        first = list(first)
838
 
        second = list(second)
839
 
        self.assertEqual(len(first), len(second))
840
 
        for x, y in zip(first, second):
841
 
            self.assertEqual(x.root_transport.base, y.root_transport.base)
842
 
 
843
 
    def test_find_branches(self):
844
 
        root = self.make_repository('', shared=True)
845
 
        foo, bar, baz = self.make_foo_bar_baz()
846
 
        qux = self.make_bzrdir('foo/qux')
847
 
        transport = get_transport(self.get_url())
848
 
        branches = bzrdir.BzrDir.find_branches(transport)
849
 
        self.assertEqual(baz.root_transport.base, branches[0].base)
850
 
        self.assertEqual(foo.root_transport.base, branches[1].base)
851
 
        self.assertEqual(bar.root_transport.base, branches[2].base)
852
 
 
853
 
        # ensure this works without a top-level repo
854
 
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
855
 
        self.assertEqual(foo.root_transport.base, branches[0].base)
856
 
        self.assertEqual(bar.root_transport.base, branches[1].base)
857
 
 
858
512
 
859
513
class TestMeta1DirFormat(TestCaseWithTransport):
860
514
    """Tests specific to the meta1 dir format."""
899
553
 
900
554
    def test_needs_conversion_different_working_tree(self):
901
555
        # meta1dirs need an conversion if any element is not the default.
902
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
903
 
        tree = self.make_branch_and_tree('tree', format='knit')
904
 
        self.assertTrue(tree.bzrdir.needs_format_conversion(
905
 
            new_format))
906
 
 
907
 
    def test_initialize_on_format_uses_smart_transport(self):
908
 
        self.setup_smart_server_with_call_log()
909
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
910
 
        transport = self.get_transport('target')
911
 
        transport.ensure_base()
912
 
        self.reset_smart_call_log()
913
 
        instance = new_format.initialize_on_transport(transport)
914
 
        self.assertIsInstance(instance, remote.RemoteBzrDir)
915
 
        rpc_count = len(self.hpss_calls)
916
 
        # This figure represent the amount of work to perform this use case. It
917
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
918
 
        # being too low. If rpc_count increases, more network roundtrips have
919
 
        # become necessary for this use case. Please do not adjust this number
920
 
        # upwards without agreement from bzr's network support maintainers.
921
 
        self.assertEqual(2, rpc_count)
 
556
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
557
        # test with 
 
558
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
 
559
        bzrdir.BzrDirFormat._set_default_format(new_default)
 
560
        try:
 
561
            tree = self.make_branch_and_tree('tree', format='knit')
 
562
            self.assertTrue(tree.bzrdir.needs_format_conversion())
 
563
        finally:
 
564
            bzrdir.BzrDirFormat._set_default_format(old_format)
922
565
 
923
566
 
924
567
class TestFormat5(TestCaseWithTransport):
925
568
    """Tests specific to the version 5 bzrdir format."""
926
569
 
927
570
    def test_same_lockfiles_between_tree_repo_branch(self):
928
 
        # this checks that only a single lockfiles instance is created
 
571
        # this checks that only a single lockfiles instance is created 
929
572
        # for format 5 objects
930
573
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
931
574
        def check_dir_components_use_same_lock(dir):
938
581
        # and if we open it normally.
939
582
        dir = bzrdir.BzrDir.open(self.get_url())
940
583
        check_dir_components_use_same_lock(dir)
941
 
 
 
584
    
942
585
    def test_can_convert(self):
943
586
        # format 5 dirs are convertable
944
587
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
945
588
        self.assertTrue(dir.can_convert_format())
946
 
 
 
589
    
947
590
    def test_needs_conversion(self):
948
 
        # format 5 dirs need a conversion if they are not the default,
949
 
        # and they aren't
950
 
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
951
 
        # don't need to convert it to itself
952
 
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
953
 
        # do need to convert it to the current default
954
 
        self.assertTrue(dir.needs_format_conversion(
955
 
            bzrdir.BzrDirFormat.get_default_format()))
 
591
        # format 5 dirs need a conversion if they are not the default.
 
592
        # and they start of not the default.
 
593
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
594
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
595
        try:
 
596
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
597
            self.assertFalse(dir.needs_format_conversion())
 
598
        finally:
 
599
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
600
        self.assertTrue(dir.needs_format_conversion())
956
601
 
957
602
 
958
603
class TestFormat6(TestCaseWithTransport):
959
604
    """Tests specific to the version 6 bzrdir format."""
960
605
 
961
606
    def test_same_lockfiles_between_tree_repo_branch(self):
962
 
        # this checks that only a single lockfiles instance is created
 
607
        # this checks that only a single lockfiles instance is created 
963
608
        # for format 6 objects
964
609
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
965
610
        def check_dir_components_use_same_lock(dir):
972
617
        # and if we open it normally.
973
618
        dir = bzrdir.BzrDir.open(self.get_url())
974
619
        check_dir_components_use_same_lock(dir)
975
 
 
 
620
    
976
621
    def test_can_convert(self):
977
622
        # format 6 dirs are convertable
978
623
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
979
624
        self.assertTrue(dir.can_convert_format())
980
 
 
 
625
    
981
626
    def test_needs_conversion(self):
982
627
        # format 6 dirs need an conversion if they are not the default.
983
 
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
984
 
        self.assertTrue(dir.needs_format_conversion(
985
 
            bzrdir.BzrDirFormat.get_default_format()))
 
628
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
629
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
630
        try:
 
631
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
632
            self.assertTrue(dir.needs_format_conversion())
 
633
        finally:
 
634
            bzrdir.BzrDirFormat._set_default_format(old_format)
986
635
 
987
636
 
988
637
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1019
668
 
1020
669
class TestNotBzrDir(TestCaseWithTransport):
1021
670
    """Tests for using the bzrdir api with a non .bzr based disk format.
1022
 
 
 
671
    
1023
672
    If/when one of these is in the core, we can let the implementation tests
1024
673
    verify this works.
1025
674
    """
1026
675
 
1027
676
    def test_create_and_find_format(self):
1028
 
        # create a .notbzr dir
 
677
        # create a .notbzr dir 
1029
678
        format = NotBzrDirFormat()
1030
679
        dir = format.initialize(self.get_url())
1031
680
        self.assertIsInstance(dir, NotBzrDir)
1055
704
 
1056
705
    def setUp(self):
1057
706
        super(NonLocalTests, self).setUp()
1058
 
        self.vfs_transport_factory = memory.MemoryServer
1059
 
 
 
707
        self.transport_server = MemoryServer
 
708
    
1060
709
    def test_create_branch_convenience(self):
1061
710
        # outside a repo the default convenience output is a repo+branch_tree
1062
711
        format = bzrdir.format_registry.make_bzrdir('knit')
1099
748
                              workingtree.WorkingTreeFormat3)
1100
749
 
1101
750
 
1102
 
class TestHTTPRedirections(object):
1103
 
    """Test redirection between two http servers.
1104
 
 
1105
 
    This MUST be used by daughter classes that also inherit from
1106
 
    TestCaseWithTwoWebservers.
1107
 
 
1108
 
    We can't inherit directly from TestCaseWithTwoWebservers or the
1109
 
    test framework will try to create an instance which cannot
1110
 
    run, its implementation being incomplete.
1111
 
    """
1112
 
 
1113
 
    def create_transport_readonly_server(self):
1114
 
        return http_utils.HTTPServerRedirecting()
1115
 
 
1116
 
    def create_transport_secondary_server(self):
1117
 
        return http_utils.HTTPServerRedirecting()
1118
 
 
1119
 
    def setUp(self):
1120
 
        super(TestHTTPRedirections, self).setUp()
1121
 
        # The redirections will point to the new server
1122
 
        self.new_server = self.get_readonly_server()
1123
 
        # The requests to the old server will be redirected
1124
 
        self.old_server = self.get_secondary_server()
1125
 
        # Configure the redirections
1126
 
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1127
 
 
1128
 
    def test_loop(self):
1129
 
        # Both servers redirect to each other creating a loop
1130
 
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1131
 
        # Starting from either server should loop
1132
 
        old_url = self._qualified_url(self.old_server.host,
1133
 
                                      self.old_server.port)
1134
 
        oldt = self._transport(old_url)
1135
 
        self.assertRaises(errors.NotBranchError,
1136
 
                          bzrdir.BzrDir.open_from_transport, oldt)
1137
 
        new_url = self._qualified_url(self.new_server.host,
1138
 
                                      self.new_server.port)
1139
 
        newt = self._transport(new_url)
1140
 
        self.assertRaises(errors.NotBranchError,
1141
 
                          bzrdir.BzrDir.open_from_transport, newt)
1142
 
 
1143
 
    def test_qualifier_preserved(self):
1144
 
        wt = self.make_branch_and_tree('branch')
1145
 
        old_url = self._qualified_url(self.old_server.host,
1146
 
                                      self.old_server.port)
1147
 
        start = self._transport(old_url).clone('branch')
1148
 
        bdir = bzrdir.BzrDir.open_from_transport(start)
1149
 
        # Redirection should preserve the qualifier, hence the transport class
1150
 
        # itself.
1151
 
        self.assertIsInstance(bdir.root_transport, type(start))
1152
 
 
1153
 
 
1154
 
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1155
 
                                  http_utils.TestCaseWithTwoWebservers):
1156
 
    """Tests redirections for urllib implementation"""
1157
 
 
1158
 
    _transport = HttpTransport_urllib
1159
 
 
1160
 
    def _qualified_url(self, host, port):
1161
 
        result = 'http+urllib://%s:%s' % (host, port)
1162
 
        self.permit_url(result)
1163
 
        return result
1164
 
 
1165
 
 
1166
 
 
1167
 
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1168
 
                                  TestHTTPRedirections,
1169
 
                                  http_utils.TestCaseWithTwoWebservers):
1170
 
    """Tests redirections for pycurl implementation"""
1171
 
 
1172
 
    def _qualified_url(self, host, port):
1173
 
        result = 'http+pycurl://%s:%s' % (host, port)
1174
 
        self.permit_url(result)
1175
 
        return result
1176
 
 
1177
 
 
1178
 
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1179
 
                                  http_utils.TestCaseWithTwoWebservers):
1180
 
    """Tests redirections for the nosmart decorator"""
1181
 
 
1182
 
    _transport = NoSmartTransportDecorator
1183
 
 
1184
 
    def _qualified_url(self, host, port):
1185
 
        result = 'nosmart+http://%s:%s' % (host, port)
1186
 
        self.permit_url(result)
1187
 
        return result
1188
 
 
1189
 
 
1190
 
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1191
 
                                    http_utils.TestCaseWithTwoWebservers):
1192
 
    """Tests redirections for readonly decoratror"""
1193
 
 
1194
 
    _transport = ReadonlyTransportDecorator
1195
 
 
1196
 
    def _qualified_url(self, host, port):
1197
 
        result = 'readonly+http://%s:%s' % (host, port)
1198
 
        self.permit_url(result)
1199
 
        return result
1200
 
 
1201
 
 
1202
 
class TestDotBzrHidden(TestCaseWithTransport):
1203
 
 
1204
 
    ls = ['ls']
1205
 
    if sys.platform == 'win32':
1206
 
        ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
1207
 
 
1208
 
    def get_ls(self):
1209
 
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1210
 
            stderr=subprocess.PIPE)
1211
 
        out, err = f.communicate()
1212
 
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1213
 
                         % (self.ls, err))
1214
 
        return out.splitlines()
1215
 
 
1216
 
    def test_dot_bzr_hidden(self):
1217
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1218
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1219
 
        b = bzrdir.BzrDir.create('.')
1220
 
        self.build_tree(['a'])
1221
 
        self.assertEquals(['a'], self.get_ls())
1222
 
 
1223
 
    def test_dot_bzr_hidden_with_url(self):
1224
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1225
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1226
 
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1227
 
        self.build_tree(['a'])
1228
 
        self.assertEquals(['a'], self.get_ls())
1229
 
 
1230
 
 
1231
 
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1232
 
    """Test BzrDirFormat implementation for TestBzrDirSprout."""
1233
 
 
1234
 
    def _open(self, transport):
1235
 
        return _TestBzrDir(transport, self)
1236
 
 
1237
 
 
1238
 
class _TestBzrDir(bzrdir.BzrDirMeta1):
1239
 
    """Test BzrDir implementation for TestBzrDirSprout.
1240
 
 
1241
 
    When created a _TestBzrDir already has repository and a branch.  The branch
1242
 
    is a test double as well.
1243
 
    """
1244
 
 
1245
 
    def __init__(self, *args, **kwargs):
1246
 
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1247
 
        self.test_branch = _TestBranch()
1248
 
        self.test_branch.repository = self.create_repository()
1249
 
 
1250
 
    def open_branch(self, unsupported=False):
1251
 
        return self.test_branch
1252
 
 
1253
 
    def cloning_metadir(self, require_stacking=False):
1254
 
        return _TestBzrDirFormat()
1255
 
 
1256
 
 
1257
 
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1258
 
    """Test Branch format for TestBzrDirSprout."""
1259
 
 
1260
 
 
1261
 
class _TestBranch(bzrlib.branch.Branch):
1262
 
    """Test Branch implementation for TestBzrDirSprout."""
1263
 
 
1264
 
    def __init__(self, *args, **kwargs):
1265
 
        self._format = _TestBranchFormat()
1266
 
        super(_TestBranch, self).__init__(*args, **kwargs)
1267
 
        self.calls = []
1268
 
        self._parent = None
1269
 
 
1270
 
    def sprout(self, *args, **kwargs):
1271
 
        self.calls.append('sprout')
1272
 
        return _TestBranch()
1273
 
 
1274
 
    def copy_content_into(self, destination, revision_id=None):
1275
 
        self.calls.append('copy_content_into')
1276
 
 
1277
 
    def get_parent(self):
1278
 
        return self._parent
1279
 
 
1280
 
    def set_parent(self, parent):
1281
 
        self._parent = parent
1282
 
 
1283
 
 
1284
 
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1285
 
 
1286
 
    def test_sprout_uses_branch_sprout(self):
1287
 
        """BzrDir.sprout calls Branch.sprout.
1288
 
 
1289
 
        Usually, BzrDir.sprout should delegate to the branch's sprout method
1290
 
        for part of the work.  This allows the source branch to control the
1291
 
        choice of format for the new branch.
1292
 
 
1293
 
        There are exceptions, but this tests avoids them:
1294
 
          - if there's no branch in the source bzrdir,
1295
 
          - or if the stacking has been requested and the format needs to be
1296
 
            overridden to satisfy that.
1297
 
        """
1298
 
        # Make an instrumented bzrdir.
1299
 
        t = self.get_transport('source')
1300
 
        t.ensure_base()
1301
 
        source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1302
 
        # The instrumented bzrdir has a test_branch attribute that logs calls
1303
 
        # made to the branch contained in that bzrdir.  Initially the test
1304
 
        # branch exists but no calls have been made to it.
1305
 
        self.assertEqual([], source_bzrdir.test_branch.calls)
1306
 
 
1307
 
        # Sprout the bzrdir
1308
 
        target_url = self.get_url('target')
1309
 
        result = source_bzrdir.sprout(target_url, recurse='no')
1310
 
 
1311
 
        # The bzrdir called the branch's sprout method.
1312
 
        self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
1313
 
 
1314
 
    def test_sprout_parent(self):
1315
 
        grandparent_tree = self.make_branch('grandparent')
1316
 
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1317
 
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1318
 
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1319
 
 
1320
 
 
1321
 
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1322
 
 
1323
 
    def test_pre_open_called(self):
1324
 
        calls = []
1325
 
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1326
 
        transport = self.get_transport('foo')
1327
 
        url = transport.base
1328
 
        self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1329
 
        self.assertEqual([transport.base], [t.base for t in calls])
1330
 
 
1331
 
    def test_pre_open_actual_exceptions_raised(self):
1332
 
        count = [0]
1333
 
        def fail_once(transport):
1334
 
            count[0] += 1
1335
 
            if count[0] == 1:
1336
 
                raise errors.BzrError("fail")
1337
 
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1338
 
        transport = self.get_transport('foo')
1339
 
        url = transport.base
1340
 
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1341
 
        self.assertEqual('fail', err._preformatted_string)
1342
 
 
1343
 
    def test_post_repo_init(self):
1344
 
        from bzrlib.bzrdir import RepoInitHookParams
1345
 
        calls = []
1346
 
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1347
 
            calls.append, None)
1348
 
        self.make_repository('foo')
1349
 
        self.assertLength(1, calls)
1350
 
        params = calls[0]
1351
 
        self.assertIsInstance(params, RepoInitHookParams)
1352
 
        self.assertTrue(hasattr(params, 'bzrdir'))
1353
 
        self.assertTrue(hasattr(params, 'repository'))
 
751
class TestRemoteSFTP(test_sftp_transport.TestCaseWithSFTPServer):
 
752
 
 
753
    def test_open_containing_tree_or_branch(self):
 
754
        tree = self.make_branch_and_tree('tree')
 
755
        bzrdir.BzrDir.open_containing_tree_or_branch(self.get_url('tree'))