/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 with walkdirs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
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
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
22
import os
 
23
import os.path
 
24
from StringIO import StringIO
23
25
import subprocess
24
26
import sys
25
27
 
29
31
    help_topics,
30
32
    repository,
31
33
    osutils,
32
 
    remote,
 
34
    symbol_versioning,
33
35
    urlutils,
34
36
    win32utils,
35
37
    workingtree,
36
38
    )
37
39
import bzrlib.branch
38
40
from bzrlib.errors import (NotBranchError,
39
 
                           NoColocatedBranchSupport,
40
41
                           UnknownFormatError,
41
42
                           UnsupportedFormatError,
42
43
                           )
45
46
    TestCaseWithMemoryTransport,
46
47
    TestCaseWithTransport,
47
48
    TestSkipped,
 
49
    test_sftp_transport
48
50
    )
49
 
from bzrlib.tests import(
50
 
    http_server,
51
 
    http_utils,
 
51
from bzrlib.tests.http_server import HttpServer
 
52
from bzrlib.tests.http_utils import (
 
53
    TestCaseWithTwoWebservers,
 
54
    HTTPServerRedirecting,
52
55
    )
53
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
54
 
from bzrlib.transport import (
55
 
    get_transport,
56
 
    memory,
57
 
    )
 
57
from bzrlib.transport import get_transport
58
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
 
59
from bzrlib.transport.memory import MemoryServer
 
60
from bzrlib.repofmt import knitrepo, weaverepo
62
61
 
63
62
 
64
63
class TestDefaultFormat(TestCase):
84
83
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
85
84
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
86
85
            ' repositories', deprecated=True)
87
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
86
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
88
87
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
89
88
        my_format_registry.register_metadir('knit',
90
89
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
117
116
        my_bzrdir = my_format_registry.make_bzrdir('weave')
118
117
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
119
118
        my_bzrdir = my_format_registry.make_bzrdir('default')
120
 
        self.assertIsInstance(my_bzrdir.repository_format,
 
119
        self.assertIsInstance(my_bzrdir.repository_format, 
121
120
            knitrepo.RepositoryFormatKnit1)
122
121
        my_bzrdir = my_format_registry.make_bzrdir('knit')
123
 
        self.assertIsInstance(my_bzrdir.repository_format,
 
122
        self.assertIsInstance(my_bzrdir.repository_format, 
124
123
            knitrepo.RepositoryFormatKnit1)
125
124
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
126
125
        self.assertIsInstance(my_bzrdir.get_branch_format(),
130
129
        my_format_registry = self.make_format_registry()
131
130
        self.assertEqual('Format registered lazily',
132
131
                         my_format_registry.get_help('lazy'))
133
 
        self.assertEqual('Format using knits',
 
132
        self.assertEqual('Format using knits', 
134
133
                         my_format_registry.get_help('knit'))
135
 
        self.assertEqual('Format using knits',
 
134
        self.assertEqual('Format using knits', 
136
135
                         my_format_registry.get_help('default'))
137
136
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
138
 
                         ' checkouts or shared repositories',
 
137
                         ' checkouts or shared repositories', 
139
138
                         my_format_registry.get_help('weave'))
140
 
 
 
139
        
141
140
    def test_help_topic(self):
142
141
        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')
 
142
        topics.register('formats', self.make_format_registry().help_topic, 
 
143
                        'Directory formats')
 
144
        topic = topics.get_detail('formats')
 
145
        new, rest = topic.split('Experimental formats')
150
146
        experimental, deprecated = rest.split('Deprecated formats')
151
 
        self.assertContainsRe(new, 'formats-help')
152
 
        self.assertContainsRe(new,
 
147
        self.assertContainsRe(new, 'These formats can be used')
 
148
        self.assertContainsRe(new, 
153
149
                ':knit:\n    \(native\) \(default\) Format using knits\n')
154
 
        self.assertContainsRe(experimental,
 
150
        self.assertContainsRe(experimental, 
155
151
                ':branch6:\n    \(native\) Experimental successor to knit')
156
 
        self.assertContainsRe(deprecated,
 
152
        self.assertContainsRe(deprecated, 
157
153
                ':lazy:\n    \(native\) Format registered lazily\n')
158
154
        self.assertNotContainsRe(new, 'hidden')
159
155
 
180
176
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
181
177
            ' repositories', deprecated=True, alias=True)
182
178
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
183
 
 
 
179
    
184
180
 
185
181
class SampleBranch(bzrlib.branch.Branch):
186
182
    """A dummy branch for guess what, dummy use."""
189
185
        self.bzrdir = dir
190
186
 
191
187
 
192
 
class SampleRepository(bzrlib.repository.Repository):
193
 
    """A dummy repo."""
194
 
 
195
 
    def __init__(self, dir):
196
 
        self.bzrdir = dir
197
 
 
198
 
 
199
188
class SampleBzrDir(bzrdir.BzrDir):
200
189
    """A sample BzrDir implementation to allow testing static methods."""
201
190
 
205
194
 
206
195
    def open_repository(self):
207
196
        """See BzrDir.open_repository."""
208
 
        return SampleRepository(self)
 
197
        return "A repository"
209
198
 
210
 
    def create_branch(self, name=None):
 
199
    def create_branch(self):
211
200
        """See BzrDir.create_branch."""
212
 
        if name is not None:
213
 
            raise NoColocatedBranchSupport(self)
214
201
        return SampleBranch(self)
215
202
 
216
203
    def create_workingtree(self):
221
208
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
222
209
    """A sample format
223
210
 
224
 
    this format is initializable, unsupported to aid in testing the
 
211
    this format is initializable, unsupported to aid in testing the 
225
212
    open and open_downlevel routines.
226
213
    """
227
214
 
248
235
    def test_find_format(self):
249
236
        # is the right format object found for a branch?
250
237
        # create a branch with a few known format objects.
251
 
        # this is not quite the same as
 
238
        # this is not quite the same as 
252
239
        t = get_transport(self.get_url())
253
240
        self.build_tree(["foo/", "bar/"], transport=t)
254
241
        def check_format(format, url):
258
245
            self.failUnless(isinstance(found_format, format.__class__))
259
246
        check_format(bzrdir.BzrDirFormat5(), "foo")
260
247
        check_format(bzrdir.BzrDirFormat6(), "bar")
261
 
 
 
248
        
262
249
    def test_find_format_nothing_there(self):
263
250
        self.assertRaises(NotBranchError,
264
251
                          bzrdir.BzrDirFormat.find_format,
308
295
                          branch.bzrdir.open_repository)
309
296
 
310
297
    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
 
298
        # creating a branch and repo in a shared repo can be forced to 
312
299
        # make a new repo
313
300
        format = bzrdir.format_registry.make_bzrdir('knit')
314
301
        self.make_repository('.', shared=True, format=format)
319
306
 
320
307
    def test_create_standalone_working_tree(self):
321
308
        format = SampleBzrDirFormat()
322
 
        # note this is deliberately readonly, as this failure should
 
309
        # note this is deliberately readonly, as this failure should 
323
310
        # occur before any writes.
324
311
        self.assertRaises(errors.NotLocalUrl,
325
312
                          bzrdir.BzrDir.create_standalone_workingtree,
326
313
                          self.get_readonly_url(), format=format)
327
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.',
 
314
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
328
315
                                                           format=format)
329
316
        self.assertEqual('A tree', tree)
330
317
 
332
319
        # create standalone working tree always makes a repo.
333
320
        format = bzrdir.format_registry.make_bzrdir('knit')
334
321
        self.make_repository('.', shared=True, format=format)
335
 
        # note this is deliberately readonly, as this failure should
 
322
        # note this is deliberately readonly, as this failure should 
336
323
        # occur before any writes.
337
324
        self.assertRaises(errors.NotLocalUrl,
338
325
                          bzrdir.BzrDir.create_standalone_workingtree,
339
326
                          self.get_readonly_url('child'), format=format)
340
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
 
327
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
341
328
            format=format)
342
329
        tree.bzrdir.open_repository()
343
330
 
359
346
 
360
347
    def test_create_branch_convenience_root(self):
361
348
        """Creating a branch at the root of a fs should work."""
362
 
        self.vfs_transport_factory = memory.MemoryServer
 
349
        self.vfs_transport_factory = MemoryServer
363
350
        # outside a repo the default convenience output is a repo+branch_tree
364
351
        format = bzrdir.format_registry.make_bzrdir('knit')
365
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
 
352
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
366
353
                                                         format=format)
367
354
        self.assertRaises(errors.NoWorkingTree,
368
355
                          branch.bzrdir.open_workingtree)
378
365
        branch.bzrdir.open_workingtree()
379
366
        self.assertRaises(errors.NoRepositoryPresent,
380
367
                          branch.bzrdir.open_repository)
381
 
 
 
368
            
382
369
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
383
370
        # inside a repo the default convenience output is a branch+ follow the
384
371
        # repo tree policy but we can override that
390
377
                          branch.bzrdir.open_workingtree)
391
378
        self.assertRaises(errors.NoRepositoryPresent,
392
379
                          branch.bzrdir.open_repository)
393
 
 
 
380
            
394
381
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
395
382
        # inside a repo the default convenience output is a branch+ follow the
396
383
        # repo tree policy
397
384
        format = bzrdir.format_registry.make_bzrdir('knit')
398
385
        repo = self.make_repository('.', shared=True, format=format)
399
386
        repo.set_make_working_trees(False)
400
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
387
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
401
388
                                                         format=format)
402
389
        self.assertRaises(errors.NoWorkingTree,
403
390
                          branch.bzrdir.open_workingtree)
433
420
        """The default acquisition policy should create a standalone branch."""
434
421
        my_bzrdir = self.make_bzrdir('.')
435
422
        repo_policy = my_bzrdir.determine_repository_policy()
436
 
        repo, is_new = repo_policy.acquire_repository()
 
423
        repo = repo_policy.acquire_repository()
437
424
        self.assertEqual(repo.bzrdir.root_transport.base,
438
425
                         my_bzrdir.root_transport.base)
439
426
        self.assertFalse(repo.is_shared())
440
427
 
 
428
 
441
429
    def test_determine_stacking_policy(self):
442
430
        parent_bzrdir = self.make_bzrdir('.')
443
431
        child_bzrdir = self.make_bzrdir('child')
454
442
        self.assertEqual(parent_bzrdir.root_transport.base,
455
443
                         repo_policy._stack_on_pwd)
456
444
 
457
 
    def prepare_default_stacking(self, child_format='1.6'):
 
445
    def prepare_default_stacking(self, child_format='development1'):
458
446
        parent_bzrdir = self.make_bzrdir('.')
459
447
        child_branch = self.make_branch('child', format=child_format)
460
448
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
467
455
        self.assertEqual(child_branch.base,
468
456
                         new_child.open_branch().get_stacked_on_url())
469
457
 
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
458
    def test_sprout_obeys_stacking_policy(self):
484
459
        child_branch, new_child_transport = self.prepare_default_stacking()
485
460
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
511
486
        self.assertTrue(repo._format.supports_external_lookups)
512
487
        self.assertFalse(repo.supports_rich_root())
513
488
 
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
489
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
526
490
        child_branch, new_child_transport = self.prepare_default_stacking(
527
491
            child_format='rich-root-pack')
528
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
529
 
                                               stacked=True)
 
492
        def do_sprout():
 
493
            try:
 
494
                return child_branch.bzrdir.sprout(new_child_transport.base,
 
495
                                                  stacked=True)
 
496
            except errors.IncompatibleRepositories:
 
497
                raise AssertionError(
 
498
                    'Rich root format should be sprout-compatible')
 
499
        self.expectFailure('Rich root format should be sprout-compatible',
 
500
                           do_sprout)
530
501
        repo = new_child.open_repository()
531
502
        self.assertTrue(repo._format.supports_external_lookups)
532
503
        self.assertTrue(repo.supports_rich_root())
533
504
 
534
505
    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')
 
506
        stack_on = self.make_branch('stack_on', format='development1')
 
507
        repo = self.make_repository('repo', format='development1')
537
508
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
538
509
        policy._add_fallback(repo)
539
510
 
540
511
    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')
 
512
        stack_on = self.make_branch('stack_on', format='development1')
 
513
        repo = self.make_repository('repo', format='development1')
543
514
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
544
515
        policy._add_fallback(repo)
545
516
 
546
517
    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')
 
518
        stack_on = self.make_branch('stack_on', format='development1')
 
519
        stacked = self.make_branch('stack_on/stacked', format='development1')
549
520
        policy = bzrdir.UseExistingRepository(stacked.repository,
550
521
            '.', stack_on.base)
551
522
        policy.configure_branch(stacked)
552
523
        self.assertEqual('..', stacked.get_stacked_on_url())
553
524
 
554
525
    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')
 
526
        stack_on = self.make_branch('stack_on', format='development1')
 
527
        stacked = self.make_branch('stack_on/stacked', format='development1')
557
528
        policy = bzrdir.UseExistingRepository(stacked.repository,
558
529
            '.', self.get_readonly_url('stack_on'))
559
530
        policy.configure_branch(stacked)
571
542
 
572
543
    def setUp(self):
573
544
        super(ChrootedTests, self).setUp()
574
 
        if not self.vfs_transport_factory == memory.MemoryServer:
575
 
            self.transport_readonly_server = http_server.HttpServer
 
545
        if not self.vfs_transport_factory == MemoryServer:
 
546
            self.transport_readonly_server = HttpServer
576
547
 
577
548
    def local_branch_path(self, branch):
578
549
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
739
710
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
740
711
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
741
712
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
742
 
 
 
713
        
743
714
    def test_open_from_transport_no_bzrdir(self):
744
715
        transport = get_transport(self.get_url())
745
716
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
754
725
                          transport)
755
726
 
756
727
    def test_sprout_recursive(self):
757
 
        tree = self.make_branch_and_tree('tree1',
758
 
                                         format='dirstate-with-subtree')
 
728
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
759
729
        sub_tree = self.make_branch_and_tree('tree1/subtree',
760
730
            format='dirstate-with-subtree')
761
 
        sub_tree.set_root_id('subtree-root')
762
731
        tree.add_reference(sub_tree)
763
732
        self.build_tree(['tree1/subtree/file'])
764
733
        sub_tree.add('file')
765
734
        tree.commit('Initial commit')
766
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
767
 
        tree2.lock_read()
768
 
        self.addCleanup(tree2.unlock)
 
735
        tree.bzrdir.sprout('tree2')
769
736
        self.failUnlessExists('tree2/subtree/file')
770
 
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
771
737
 
772
738
    def test_cloning_metadir(self):
773
739
        """Ensure that cloning metadir is suitable"""
899
865
 
900
866
    def test_needs_conversion_different_working_tree(self):
901
867
        # 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)
 
868
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
869
        # test with 
 
870
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
 
871
        bzrdir.BzrDirFormat._set_default_format(new_default)
 
872
        try:
 
873
            tree = self.make_branch_and_tree('tree', format='knit')
 
874
            self.assertTrue(tree.bzrdir.needs_format_conversion())
 
875
        finally:
 
876
            bzrdir.BzrDirFormat._set_default_format(old_format)
922
877
 
923
878
 
924
879
class TestFormat5(TestCaseWithTransport):
925
880
    """Tests specific to the version 5 bzrdir format."""
926
881
 
927
882
    def test_same_lockfiles_between_tree_repo_branch(self):
928
 
        # this checks that only a single lockfiles instance is created
 
883
        # this checks that only a single lockfiles instance is created 
929
884
        # for format 5 objects
930
885
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
931
886
        def check_dir_components_use_same_lock(dir):
938
893
        # and if we open it normally.
939
894
        dir = bzrdir.BzrDir.open(self.get_url())
940
895
        check_dir_components_use_same_lock(dir)
941
 
 
 
896
    
942
897
    def test_can_convert(self):
943
898
        # format 5 dirs are convertable
944
899
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
945
900
        self.assertTrue(dir.can_convert_format())
946
 
 
 
901
    
947
902
    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()))
 
903
        # format 5 dirs need a conversion if they are not the default.
 
904
        # and they start of not the default.
 
905
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
906
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
907
        try:
 
908
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
909
            self.assertFalse(dir.needs_format_conversion())
 
910
        finally:
 
911
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
912
        self.assertTrue(dir.needs_format_conversion())
956
913
 
957
914
 
958
915
class TestFormat6(TestCaseWithTransport):
959
916
    """Tests specific to the version 6 bzrdir format."""
960
917
 
961
918
    def test_same_lockfiles_between_tree_repo_branch(self):
962
 
        # this checks that only a single lockfiles instance is created
 
919
        # this checks that only a single lockfiles instance is created 
963
920
        # for format 6 objects
964
921
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
965
922
        def check_dir_components_use_same_lock(dir):
972
929
        # and if we open it normally.
973
930
        dir = bzrdir.BzrDir.open(self.get_url())
974
931
        check_dir_components_use_same_lock(dir)
975
 
 
 
932
    
976
933
    def test_can_convert(self):
977
934
        # format 6 dirs are convertable
978
935
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
979
936
        self.assertTrue(dir.can_convert_format())
980
 
 
 
937
    
981
938
    def test_needs_conversion(self):
982
939
        # 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()))
 
940
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
941
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
942
        try:
 
943
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
944
            self.assertTrue(dir.needs_format_conversion())
 
945
        finally:
 
946
            bzrdir.BzrDirFormat._set_default_format(old_format)
986
947
 
987
948
 
988
949
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1019
980
 
1020
981
class TestNotBzrDir(TestCaseWithTransport):
1021
982
    """Tests for using the bzrdir api with a non .bzr based disk format.
1022
 
 
 
983
    
1023
984
    If/when one of these is in the core, we can let the implementation tests
1024
985
    verify this works.
1025
986
    """
1026
987
 
1027
988
    def test_create_and_find_format(self):
1028
 
        # create a .notbzr dir
 
989
        # create a .notbzr dir 
1029
990
        format = NotBzrDirFormat()
1030
991
        dir = format.initialize(self.get_url())
1031
992
        self.assertIsInstance(dir, NotBzrDir)
1055
1016
 
1056
1017
    def setUp(self):
1057
1018
        super(NonLocalTests, self).setUp()
1058
 
        self.vfs_transport_factory = memory.MemoryServer
1059
 
 
 
1019
        self.vfs_transport_factory = MemoryServer
 
1020
    
1060
1021
    def test_create_branch_convenience(self):
1061
1022
        # outside a repo the default convenience output is a repo+branch_tree
1062
1023
        format = bzrdir.format_registry.make_bzrdir('knit')
1099
1060
                              workingtree.WorkingTreeFormat3)
1100
1061
 
1101
1062
 
1102
 
class TestHTTPRedirections(object):
1103
 
    """Test redirection between two http servers.
 
1063
class TestHTTPRedirectionLoop(object):
 
1064
    """Test redirection loop between two http servers.
1104
1065
 
1105
1066
    This MUST be used by daughter classes that also inherit from
1106
1067
    TestCaseWithTwoWebservers.
1107
1068
 
1108
1069
    We can't inherit directly from TestCaseWithTwoWebservers or the
1109
1070
    test framework will try to create an instance which cannot
1110
 
    run, its implementation being incomplete.
 
1071
    run, its implementation being incomplete. 
1111
1072
    """
1112
1073
 
 
1074
    # Should be defined by daughter classes to ensure redirection
 
1075
    # still use the same transport implementation (not currently
 
1076
    # enforced as it's a bit tricky to get right (see the FIXME
 
1077
    # in BzrDir.open_from_transport for the unique use case so
 
1078
    # far)
 
1079
    _qualifier = None
 
1080
 
1113
1081
    def create_transport_readonly_server(self):
1114
 
        return http_utils.HTTPServerRedirecting()
 
1082
        return HTTPServerRedirecting()
1115
1083
 
1116
1084
    def create_transport_secondary_server(self):
1117
 
        return http_utils.HTTPServerRedirecting()
 
1085
        return HTTPServerRedirecting()
1118
1086
 
1119
1087
    def setUp(self):
1120
 
        super(TestHTTPRedirections, self).setUp()
 
1088
        # Both servers redirect to each server creating a loop
 
1089
        super(TestHTTPRedirectionLoop, self).setUp()
1121
1090
        # The redirections will point to the new server
1122
1091
        self.new_server = self.get_readonly_server()
1123
1092
        # The requests to the old server will be redirected
1124
1093
        self.old_server = self.get_secondary_server()
1125
1094
        # Configure the redirections
1126
1095
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
 
1096
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
 
1097
 
 
1098
    def _qualified_url(self, host, port):
 
1099
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
1127
1100
 
1128
1101
    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
1102
        # Starting from either server should loop
1132
 
        old_url = self._qualified_url(self.old_server.host,
 
1103
        old_url = self._qualified_url(self.old_server.host, 
1133
1104
                                      self.old_server.port)
1134
1105
        oldt = self._transport(old_url)
1135
1106
        self.assertRaises(errors.NotBranchError,
1136
1107
                          bzrdir.BzrDir.open_from_transport, oldt)
1137
 
        new_url = self._qualified_url(self.new_server.host,
 
1108
        new_url = self._qualified_url(self.new_server.host, 
1138
1109
                                      self.new_server.port)
1139
1110
        newt = self._transport(new_url)
1140
1111
        self.assertRaises(errors.NotBranchError,
1141
1112
                          bzrdir.BzrDir.open_from_transport, newt)
1142
1113
 
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):
 
1114
 
 
1115
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
 
1116
                                  TestCaseWithTwoWebservers):
1156
1117
    """Tests redirections for urllib implementation"""
1157
1118
 
 
1119
    _qualifier = 'urllib'
1158
1120
    _transport = HttpTransport_urllib
1159
1121
 
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
1122
 
1166
1123
 
1167
1124
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1168
 
                                  TestHTTPRedirections,
1169
 
                                  http_utils.TestCaseWithTwoWebservers):
 
1125
                                  TestHTTPRedirectionLoop,
 
1126
                                  TestCaseWithTwoWebservers):
1170
1127
    """Tests redirections for pycurl implementation"""
1171
1128
 
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
 
1129
    _qualifier = 'pycurl'
1200
1130
 
1201
1131
 
1202
1132
class TestDotBzrHidden(TestCaseWithTransport):
1237
1167
 
1238
1168
class _TestBzrDir(bzrdir.BzrDirMeta1):
1239
1169
    """Test BzrDir implementation for TestBzrDirSprout.
1240
 
 
 
1170
    
1241
1171
    When created a _TestBzrDir already has repository and a branch.  The branch
1242
1172
    is a test double as well.
1243
1173
    """
1254
1184
        return _TestBzrDirFormat()
1255
1185
 
1256
1186
 
1257
 
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1258
 
    """Test Branch format for TestBzrDirSprout."""
1259
 
 
1260
 
 
1261
1187
class _TestBranch(bzrlib.branch.Branch):
1262
1188
    """Test Branch implementation for TestBzrDirSprout."""
1263
1189
 
1264
1190
    def __init__(self, *args, **kwargs):
1265
 
        self._format = _TestBranchFormat()
1266
1191
        super(_TestBranch, self).__init__(*args, **kwargs)
1267
1192
        self.calls = []
1268
1193
        self._parent = None
1289
1214
        Usually, BzrDir.sprout should delegate to the branch's sprout method
1290
1215
        for part of the work.  This allows the source branch to control the
1291
1216
        choice of format for the new branch.
1292
 
 
 
1217
        
1293
1218
        There are exceptions, but this tests avoids them:
1294
1219
          - if there's no branch in the source bzrdir,
1295
1220
          - or if the stacking has been requested and the format needs to be
1316
1241
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1317
1242
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1318
1243
        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'))