~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-08-09 15:19:06 UTC
  • mfrom: (2681.1.7 send-bundle)
  • Revision ID: pqm@pqm.ubuntu.com-20070809151906-hdn9oyslf2qib2op
Allow omitting -o for bundle, add --format

Show diffs side-by-side

added added

removed removed

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