~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Robert Collins
  • Date: 2008-09-25 01:54:42 UTC
  • mfrom: (3709.4.1 iter-changes.less-sha1)
  • mto: This revision was merged to the branch mainline in revision 3741.
  • Revision ID: robertc@robertcollins.net-20080925015442-p9mtzse65w5gy2uv
Integrate less aggressive sha logic with C iter-changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,
44
46
    TestCaseWithMemoryTransport,
45
47
    TestCaseWithTransport,
46
48
    TestSkipped,
 
49
    test_sftp_transport
47
50
    )
48
 
from bzrlib.tests import(
49
 
    http_server,
50
 
    http_utils,
 
51
from bzrlib.tests.http_server import HttpServer
 
52
from bzrlib.tests.http_utils import (
 
53
    TestCaseWithTwoWebservers,
 
54
    HTTPServerRedirecting,
51
55
    )
52
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
53
57
from bzrlib.transport import get_transport
54
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
55
59
from bzrlib.transport.memory import MemoryServer
56
 
from bzrlib.transport.nosmart import NoSmartTransportDecorator
57
 
from bzrlib.transport.readonly import ReadonlyTransportDecorator
58
 
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
 
60
from bzrlib.repofmt import knitrepo, weaverepo
59
61
 
60
62
 
61
63
class TestDefaultFormat(TestCase):
81
83
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
82
84
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
83
85
            ' repositories', deprecated=True)
84
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
86
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
85
87
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
86
88
        my_format_registry.register_metadir('knit',
87
89
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
114
116
        my_bzrdir = my_format_registry.make_bzrdir('weave')
115
117
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
116
118
        my_bzrdir = my_format_registry.make_bzrdir('default')
117
 
        self.assertIsInstance(my_bzrdir.repository_format,
 
119
        self.assertIsInstance(my_bzrdir.repository_format, 
118
120
            knitrepo.RepositoryFormatKnit1)
119
121
        my_bzrdir = my_format_registry.make_bzrdir('knit')
120
 
        self.assertIsInstance(my_bzrdir.repository_format,
 
122
        self.assertIsInstance(my_bzrdir.repository_format, 
121
123
            knitrepo.RepositoryFormatKnit1)
122
124
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
123
125
        self.assertIsInstance(my_bzrdir.get_branch_format(),
127
129
        my_format_registry = self.make_format_registry()
128
130
        self.assertEqual('Format registered lazily',
129
131
                         my_format_registry.get_help('lazy'))
130
 
        self.assertEqual('Format using knits',
 
132
        self.assertEqual('Format using knits', 
131
133
                         my_format_registry.get_help('knit'))
132
 
        self.assertEqual('Format using knits',
 
134
        self.assertEqual('Format using knits', 
133
135
                         my_format_registry.get_help('default'))
134
136
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
135
 
                         ' checkouts or shared repositories',
 
137
                         ' checkouts or shared repositories', 
136
138
                         my_format_registry.get_help('weave'))
137
 
 
 
139
        
138
140
    def test_help_topic(self):
139
141
        topics = help_topics.HelpTopicRegistry()
140
 
        registry = self.make_format_registry()
141
 
        topics.register('current-formats', registry.help_topic,
142
 
                        'Current formats')
143
 
        topics.register('other-formats', registry.help_topic,
144
 
                        'Other formats')
145
 
        new = topics.get_detail('current-formats')
146
 
        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')
147
146
        experimental, deprecated = rest.split('Deprecated formats')
148
 
        self.assertContainsRe(new, 'formats-help')
149
 
        self.assertContainsRe(new,
 
147
        self.assertContainsRe(new, 'These formats can be used')
 
148
        self.assertContainsRe(new, 
150
149
                ':knit:\n    \(native\) \(default\) Format using knits\n')
151
 
        self.assertContainsRe(experimental,
 
150
        self.assertContainsRe(experimental, 
152
151
                ':branch6:\n    \(native\) Experimental successor to knit')
153
 
        self.assertContainsRe(deprecated,
 
152
        self.assertContainsRe(deprecated, 
154
153
                ':lazy:\n    \(native\) Format registered lazily\n')
155
154
        self.assertNotContainsRe(new, 'hidden')
156
155
 
177
176
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
178
177
            ' repositories', deprecated=True, alias=True)
179
178
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
180
 
 
 
179
    
181
180
 
182
181
class SampleBranch(bzrlib.branch.Branch):
183
182
    """A dummy branch for guess what, dummy use."""
186
185
        self.bzrdir = dir
187
186
 
188
187
 
189
 
class SampleRepository(bzrlib.repository.Repository):
190
 
    """A dummy repo."""
191
 
 
192
 
    def __init__(self, dir):
193
 
        self.bzrdir = dir
194
 
 
195
 
 
196
188
class SampleBzrDir(bzrdir.BzrDir):
197
189
    """A sample BzrDir implementation to allow testing static methods."""
198
190
 
202
194
 
203
195
    def open_repository(self):
204
196
        """See BzrDir.open_repository."""
205
 
        return SampleRepository(self)
 
197
        return "A repository"
206
198
 
207
199
    def create_branch(self):
208
200
        """See BzrDir.create_branch."""
216
208
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
217
209
    """A sample format
218
210
 
219
 
    this format is initializable, unsupported to aid in testing the
 
211
    this format is initializable, unsupported to aid in testing the 
220
212
    open and open_downlevel routines.
221
213
    """
222
214
 
243
235
    def test_find_format(self):
244
236
        # is the right format object found for a branch?
245
237
        # create a branch with a few known format objects.
246
 
        # this is not quite the same as
 
238
        # this is not quite the same as 
247
239
        t = get_transport(self.get_url())
248
240
        self.build_tree(["foo/", "bar/"], transport=t)
249
241
        def check_format(format, url):
253
245
            self.failUnless(isinstance(found_format, format.__class__))
254
246
        check_format(bzrdir.BzrDirFormat5(), "foo")
255
247
        check_format(bzrdir.BzrDirFormat6(), "bar")
256
 
 
 
248
        
257
249
    def test_find_format_nothing_there(self):
258
250
        self.assertRaises(NotBranchError,
259
251
                          bzrdir.BzrDirFormat.find_format,
303
295
                          branch.bzrdir.open_repository)
304
296
 
305
297
    def test_create_branch_and_repo_under_shared_force_new(self):
306
 
        # 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 
307
299
        # make a new repo
308
300
        format = bzrdir.format_registry.make_bzrdir('knit')
309
301
        self.make_repository('.', shared=True, format=format)
314
306
 
315
307
    def test_create_standalone_working_tree(self):
316
308
        format = SampleBzrDirFormat()
317
 
        # note this is deliberately readonly, as this failure should
 
309
        # note this is deliberately readonly, as this failure should 
318
310
        # occur before any writes.
319
311
        self.assertRaises(errors.NotLocalUrl,
320
312
                          bzrdir.BzrDir.create_standalone_workingtree,
321
313
                          self.get_readonly_url(), format=format)
322
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.',
 
314
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
323
315
                                                           format=format)
324
316
        self.assertEqual('A tree', tree)
325
317
 
327
319
        # create standalone working tree always makes a repo.
328
320
        format = bzrdir.format_registry.make_bzrdir('knit')
329
321
        self.make_repository('.', shared=True, format=format)
330
 
        # note this is deliberately readonly, as this failure should
 
322
        # note this is deliberately readonly, as this failure should 
331
323
        # occur before any writes.
332
324
        self.assertRaises(errors.NotLocalUrl,
333
325
                          bzrdir.BzrDir.create_standalone_workingtree,
334
326
                          self.get_readonly_url('child'), format=format)
335
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
 
327
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
336
328
            format=format)
337
329
        tree.bzrdir.open_repository()
338
330
 
357
349
        self.vfs_transport_factory = MemoryServer
358
350
        # outside a repo the default convenience output is a repo+branch_tree
359
351
        format = bzrdir.format_registry.make_bzrdir('knit')
360
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
 
352
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
361
353
                                                         format=format)
362
354
        self.assertRaises(errors.NoWorkingTree,
363
355
                          branch.bzrdir.open_workingtree)
373
365
        branch.bzrdir.open_workingtree()
374
366
        self.assertRaises(errors.NoRepositoryPresent,
375
367
                          branch.bzrdir.open_repository)
376
 
 
 
368
            
377
369
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
378
370
        # inside a repo the default convenience output is a branch+ follow the
379
371
        # repo tree policy but we can override that
385
377
                          branch.bzrdir.open_workingtree)
386
378
        self.assertRaises(errors.NoRepositoryPresent,
387
379
                          branch.bzrdir.open_repository)
388
 
 
 
380
            
389
381
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
390
382
        # inside a repo the default convenience output is a branch+ follow the
391
383
        # repo tree policy
392
384
        format = bzrdir.format_registry.make_bzrdir('knit')
393
385
        repo = self.make_repository('.', shared=True, format=format)
394
386
        repo.set_make_working_trees(False)
395
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
387
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
396
388
                                                         format=format)
397
389
        self.assertRaises(errors.NoWorkingTree,
398
390
                          branch.bzrdir.open_workingtree)
428
420
        """The default acquisition policy should create a standalone branch."""
429
421
        my_bzrdir = self.make_bzrdir('.')
430
422
        repo_policy = my_bzrdir.determine_repository_policy()
431
 
        repo, is_new = repo_policy.acquire_repository()
 
423
        repo = repo_policy.acquire_repository()
432
424
        self.assertEqual(repo.bzrdir.root_transport.base,
433
425
                         my_bzrdir.root_transport.base)
434
426
        self.assertFalse(repo.is_shared())
435
427
 
 
428
 
436
429
    def test_determine_stacking_policy(self):
437
430
        parent_bzrdir = self.make_bzrdir('.')
438
431
        child_bzrdir = self.make_bzrdir('child')
449
442
        self.assertEqual(parent_bzrdir.root_transport.base,
450
443
                         repo_policy._stack_on_pwd)
451
444
 
452
 
    def prepare_default_stacking(self, child_format='1.6'):
 
445
    def prepare_default_stacking(self, child_format='development1'):
453
446
        parent_bzrdir = self.make_bzrdir('.')
454
447
        child_branch = self.make_branch('child', format=child_format)
455
448
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
462
455
        self.assertEqual(child_branch.base,
463
456
                         new_child.open_branch().get_stacked_on_url())
464
457
 
465
 
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
466
 
        # Make stackable source branch with an unstackable repo format.
467
 
        source_bzrdir = self.make_bzrdir('source')
468
 
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
469
 
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(source_bzrdir)
470
 
        # Make a directory with a default stacking policy
471
 
        parent_bzrdir = self.make_bzrdir('parent')
472
 
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
473
 
        parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
474
 
        # Clone source into directory
475
 
        target = source_bzrdir.clone(self.get_url('parent/target'))
476
 
 
477
458
    def test_sprout_obeys_stacking_policy(self):
478
459
        child_branch, new_child_transport = self.prepare_default_stacking()
479
460
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
526
507
        self.assertTrue(repo.supports_rich_root())
527
508
 
528
509
    def test_add_fallback_repo_handles_absolute_urls(self):
529
 
        stack_on = self.make_branch('stack_on', format='1.6')
530
 
        repo = self.make_repository('repo', format='1.6')
 
510
        stack_on = self.make_branch('stack_on', format='development1')
 
511
        repo = self.make_repository('repo', format='development1')
531
512
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
532
513
        policy._add_fallback(repo)
533
514
 
534
515
    def test_add_fallback_repo_handles_relative_urls(self):
535
 
        stack_on = self.make_branch('stack_on', format='1.6')
536
 
        repo = self.make_repository('repo', format='1.6')
 
516
        stack_on = self.make_branch('stack_on', format='development1')
 
517
        repo = self.make_repository('repo', format='development1')
537
518
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
538
519
        policy._add_fallback(repo)
539
520
 
540
521
    def test_configure_relative_branch_stacking_url(self):
541
 
        stack_on = self.make_branch('stack_on', format='1.6')
542
 
        stacked = self.make_branch('stack_on/stacked', format='1.6')
 
522
        stack_on = self.make_branch('stack_on', format='development1')
 
523
        stacked = self.make_branch('stack_on/stacked', format='development1')
543
524
        policy = bzrdir.UseExistingRepository(stacked.repository,
544
525
            '.', stack_on.base)
545
526
        policy.configure_branch(stacked)
546
527
        self.assertEqual('..', stacked.get_stacked_on_url())
547
528
 
548
529
    def test_relative_branch_stacking_to_absolute(self):
549
 
        stack_on = self.make_branch('stack_on', format='1.6')
550
 
        stacked = self.make_branch('stack_on/stacked', format='1.6')
 
530
        stack_on = self.make_branch('stack_on', format='development1')
 
531
        stacked = self.make_branch('stack_on/stacked', format='development1')
551
532
        policy = bzrdir.UseExistingRepository(stacked.repository,
552
533
            '.', self.get_readonly_url('stack_on'))
553
534
        policy.configure_branch(stacked)
566
547
    def setUp(self):
567
548
        super(ChrootedTests, self).setUp()
568
549
        if not self.vfs_transport_factory == MemoryServer:
569
 
            self.transport_readonly_server = http_server.HttpServer
 
550
            self.transport_readonly_server = HttpServer
570
551
 
571
552
    def local_branch_path(self, branch):
572
553
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
733
714
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
734
715
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
735
716
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
736
 
 
 
717
        
737
718
    def test_open_from_transport_no_bzrdir(self):
738
719
        transport = get_transport(self.get_url())
739
720
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
748
729
                          transport)
749
730
 
750
731
    def test_sprout_recursive(self):
751
 
        tree = self.make_branch_and_tree('tree1',
752
 
                                         format='dirstate-with-subtree')
 
732
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
753
733
        sub_tree = self.make_branch_and_tree('tree1/subtree',
754
734
            format='dirstate-with-subtree')
755
 
        sub_tree.set_root_id('subtree-root')
756
735
        tree.add_reference(sub_tree)
757
736
        self.build_tree(['tree1/subtree/file'])
758
737
        sub_tree.add('file')
759
738
        tree.commit('Initial commit')
760
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
761
 
        tree2.lock_read()
762
 
        self.addCleanup(tree2.unlock)
 
739
        tree.bzrdir.sprout('tree2')
763
740
        self.failUnlessExists('tree2/subtree/file')
764
 
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
765
741
 
766
742
    def test_cloning_metadir(self):
767
743
        """Ensure that cloning metadir is suitable"""
893
869
 
894
870
    def test_needs_conversion_different_working_tree(self):
895
871
        # meta1dirs need an conversion if any element is not the default.
896
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
897
 
        tree = self.make_branch_and_tree('tree', format='knit')
898
 
        self.assertTrue(tree.bzrdir.needs_format_conversion(
899
 
            new_format))
900
 
 
901
 
    def test_initialize_on_format_uses_smart_transport(self):
902
 
        self.setup_smart_server_with_call_log()
903
 
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
904
 
        transport = self.get_transport('target')
905
 
        transport.ensure_base()
906
 
        self.reset_smart_call_log()
907
 
        instance = new_format.initialize_on_transport(transport)
908
 
        self.assertIsInstance(instance, remote.RemoteBzrDir)
909
 
        rpc_count = len(self.hpss_calls)
910
 
        # This figure represent the amount of work to perform this use case. It
911
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
912
 
        # being too low. If rpc_count increases, more network roundtrips have
913
 
        # become necessary for this use case. Please do not adjust this number
914
 
        # upwards without agreement from bzr's network support maintainers.
915
 
        self.assertEqual(2, rpc_count)
 
872
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
873
        # test with 
 
874
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
 
875
        bzrdir.BzrDirFormat._set_default_format(new_default)
 
876
        try:
 
877
            tree = self.make_branch_and_tree('tree', format='knit')
 
878
            self.assertTrue(tree.bzrdir.needs_format_conversion())
 
879
        finally:
 
880
            bzrdir.BzrDirFormat._set_default_format(old_format)
916
881
 
917
882
 
918
883
class TestFormat5(TestCaseWithTransport):
919
884
    """Tests specific to the version 5 bzrdir format."""
920
885
 
921
886
    def test_same_lockfiles_between_tree_repo_branch(self):
922
 
        # this checks that only a single lockfiles instance is created
 
887
        # this checks that only a single lockfiles instance is created 
923
888
        # for format 5 objects
924
889
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
925
890
        def check_dir_components_use_same_lock(dir):
932
897
        # and if we open it normally.
933
898
        dir = bzrdir.BzrDir.open(self.get_url())
934
899
        check_dir_components_use_same_lock(dir)
935
 
 
 
900
    
936
901
    def test_can_convert(self):
937
902
        # format 5 dirs are convertable
938
903
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
939
904
        self.assertTrue(dir.can_convert_format())
940
 
 
 
905
    
941
906
    def test_needs_conversion(self):
942
 
        # format 5 dirs need a conversion if they are not the default,
943
 
        # and they aren't
944
 
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
945
 
        # don't need to convert it to itself
946
 
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
947
 
        # do need to convert it to the current default
948
 
        self.assertTrue(dir.needs_format_conversion(
949
 
            bzrdir.BzrDirFormat.get_default_format()))
 
907
        # format 5 dirs need a conversion if they are not the default.
 
908
        # and they start of not the default.
 
909
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
910
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
911
        try:
 
912
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
913
            self.assertFalse(dir.needs_format_conversion())
 
914
        finally:
 
915
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
916
        self.assertTrue(dir.needs_format_conversion())
950
917
 
951
918
 
952
919
class TestFormat6(TestCaseWithTransport):
953
920
    """Tests specific to the version 6 bzrdir format."""
954
921
 
955
922
    def test_same_lockfiles_between_tree_repo_branch(self):
956
 
        # this checks that only a single lockfiles instance is created
 
923
        # this checks that only a single lockfiles instance is created 
957
924
        # for format 6 objects
958
925
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
959
926
        def check_dir_components_use_same_lock(dir):
966
933
        # and if we open it normally.
967
934
        dir = bzrdir.BzrDir.open(self.get_url())
968
935
        check_dir_components_use_same_lock(dir)
969
 
 
 
936
    
970
937
    def test_can_convert(self):
971
938
        # format 6 dirs are convertable
972
939
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
973
940
        self.assertTrue(dir.can_convert_format())
974
 
 
 
941
    
975
942
    def test_needs_conversion(self):
976
943
        # format 6 dirs need an conversion if they are not the default.
977
 
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
978
 
        self.assertTrue(dir.needs_format_conversion(
979
 
            bzrdir.BzrDirFormat.get_default_format()))
 
944
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
945
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
946
        try:
 
947
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
948
            self.assertTrue(dir.needs_format_conversion())
 
949
        finally:
 
950
            bzrdir.BzrDirFormat._set_default_format(old_format)
980
951
 
981
952
 
982
953
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1013
984
 
1014
985
class TestNotBzrDir(TestCaseWithTransport):
1015
986
    """Tests for using the bzrdir api with a non .bzr based disk format.
1016
 
 
 
987
    
1017
988
    If/when one of these is in the core, we can let the implementation tests
1018
989
    verify this works.
1019
990
    """
1020
991
 
1021
992
    def test_create_and_find_format(self):
1022
 
        # create a .notbzr dir
 
993
        # create a .notbzr dir 
1023
994
        format = NotBzrDirFormat()
1024
995
        dir = format.initialize(self.get_url())
1025
996
        self.assertIsInstance(dir, NotBzrDir)
1050
1021
    def setUp(self):
1051
1022
        super(NonLocalTests, self).setUp()
1052
1023
        self.vfs_transport_factory = MemoryServer
1053
 
 
 
1024
    
1054
1025
    def test_create_branch_convenience(self):
1055
1026
        # outside a repo the default convenience output is a repo+branch_tree
1056
1027
        format = bzrdir.format_registry.make_bzrdir('knit')
1093
1064
                              workingtree.WorkingTreeFormat3)
1094
1065
 
1095
1066
 
1096
 
class TestHTTPRedirections(object):
1097
 
    """Test redirection between two http servers.
 
1067
class TestHTTPRedirectionLoop(object):
 
1068
    """Test redirection loop between two http servers.
1098
1069
 
1099
1070
    This MUST be used by daughter classes that also inherit from
1100
1071
    TestCaseWithTwoWebservers.
1101
1072
 
1102
1073
    We can't inherit directly from TestCaseWithTwoWebservers or the
1103
1074
    test framework will try to create an instance which cannot
1104
 
    run, its implementation being incomplete.
 
1075
    run, its implementation being incomplete. 
1105
1076
    """
1106
1077
 
 
1078
    # Should be defined by daughter classes to ensure redirection
 
1079
    # still use the same transport implementation (not currently
 
1080
    # enforced as it's a bit tricky to get right (see the FIXME
 
1081
    # in BzrDir.open_from_transport for the unique use case so
 
1082
    # far)
 
1083
    _qualifier = None
 
1084
 
1107
1085
    def create_transport_readonly_server(self):
1108
 
        return http_utils.HTTPServerRedirecting()
 
1086
        return HTTPServerRedirecting()
1109
1087
 
1110
1088
    def create_transport_secondary_server(self):
1111
 
        return http_utils.HTTPServerRedirecting()
 
1089
        return HTTPServerRedirecting()
1112
1090
 
1113
1091
    def setUp(self):
1114
 
        super(TestHTTPRedirections, self).setUp()
 
1092
        # Both servers redirect to each server creating a loop
 
1093
        super(TestHTTPRedirectionLoop, self).setUp()
1115
1094
        # The redirections will point to the new server
1116
1095
        self.new_server = self.get_readonly_server()
1117
1096
        # The requests to the old server will be redirected
1118
1097
        self.old_server = self.get_secondary_server()
1119
1098
        # Configure the redirections
1120
1099
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
 
1100
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
 
1101
 
 
1102
    def _qualified_url(self, host, port):
 
1103
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
1121
1104
 
1122
1105
    def test_loop(self):
1123
 
        # Both servers redirect to each other creating a loop
1124
 
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1125
1106
        # Starting from either server should loop
1126
 
        old_url = self._qualified_url(self.old_server.host,
 
1107
        old_url = self._qualified_url(self.old_server.host, 
1127
1108
                                      self.old_server.port)
1128
1109
        oldt = self._transport(old_url)
1129
1110
        self.assertRaises(errors.NotBranchError,
1130
1111
                          bzrdir.BzrDir.open_from_transport, oldt)
1131
 
        new_url = self._qualified_url(self.new_server.host,
 
1112
        new_url = self._qualified_url(self.new_server.host, 
1132
1113
                                      self.new_server.port)
1133
1114
        newt = self._transport(new_url)
1134
1115
        self.assertRaises(errors.NotBranchError,
1135
1116
                          bzrdir.BzrDir.open_from_transport, newt)
1136
1117
 
1137
 
    def test_qualifier_preserved(self):
1138
 
        wt = self.make_branch_and_tree('branch')
1139
 
        old_url = self._qualified_url(self.old_server.host,
1140
 
                                      self.old_server.port)
1141
 
        start = self._transport(old_url).clone('branch')
1142
 
        bdir = bzrdir.BzrDir.open_from_transport(start)
1143
 
        # Redirection should preserve the qualifier, hence the transport class
1144
 
        # itself.
1145
 
        self.assertIsInstance(bdir.root_transport, type(start))
1146
 
 
1147
 
 
1148
 
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1149
 
                                  http_utils.TestCaseWithTwoWebservers):
 
1118
 
 
1119
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
 
1120
                                  TestCaseWithTwoWebservers):
1150
1121
    """Tests redirections for urllib implementation"""
1151
1122
 
 
1123
    _qualifier = 'urllib'
1152
1124
    _transport = HttpTransport_urllib
1153
1125
 
1154
 
    def _qualified_url(self, host, port):
1155
 
        result = 'http+urllib://%s:%s' % (host, port)
1156
 
        self.permit_url(result)
1157
 
        return result
1158
 
 
1159
1126
 
1160
1127
 
1161
1128
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1162
 
                                  TestHTTPRedirections,
1163
 
                                  http_utils.TestCaseWithTwoWebservers):
 
1129
                                  TestHTTPRedirectionLoop,
 
1130
                                  TestCaseWithTwoWebservers):
1164
1131
    """Tests redirections for pycurl implementation"""
1165
1132
 
1166
 
    def _qualified_url(self, host, port):
1167
 
        result = 'http+pycurl://%s:%s' % (host, port)
1168
 
        self.permit_url(result)
1169
 
        return result
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
 
        result = 'nosmart+http://%s:%s' % (host, port)
1180
 
        self.permit_url(result)
1181
 
        return result
1182
 
 
1183
 
 
1184
 
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1185
 
                                    http_utils.TestCaseWithTwoWebservers):
1186
 
    """Tests redirections for readonly decoratror"""
1187
 
 
1188
 
    _transport = ReadonlyTransportDecorator
1189
 
 
1190
 
    def _qualified_url(self, host, port):
1191
 
        result = 'readonly+http://%s:%s' % (host, port)
1192
 
        self.permit_url(result)
1193
 
        return result
 
1133
    _qualifier = 'pycurl'
1194
1134
 
1195
1135
 
1196
1136
class TestDotBzrHidden(TestCaseWithTransport):
1231
1171
 
1232
1172
class _TestBzrDir(bzrdir.BzrDirMeta1):
1233
1173
    """Test BzrDir implementation for TestBzrDirSprout.
1234
 
 
 
1174
    
1235
1175
    When created a _TestBzrDir already has repository and a branch.  The branch
1236
1176
    is a test double as well.
1237
1177
    """
1248
1188
        return _TestBzrDirFormat()
1249
1189
 
1250
1190
 
1251
 
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1252
 
    """Test Branch format for TestBzrDirSprout."""
1253
 
 
1254
 
 
1255
1191
class _TestBranch(bzrlib.branch.Branch):
1256
1192
    """Test Branch implementation for TestBzrDirSprout."""
1257
1193
 
1258
1194
    def __init__(self, *args, **kwargs):
1259
 
        self._format = _TestBranchFormat()
1260
1195
        super(_TestBranch, self).__init__(*args, **kwargs)
1261
1196
        self.calls = []
1262
1197
        self._parent = None
1283
1218
        Usually, BzrDir.sprout should delegate to the branch's sprout method
1284
1219
        for part of the work.  This allows the source branch to control the
1285
1220
        choice of format for the new branch.
1286
 
 
 
1221
        
1287
1222
        There are exceptions, but this tests avoids them:
1288
1223
          - if there's no branch in the source bzrdir,
1289
1224
          - or if the stacking has been requested and the format needs to be
1310
1245
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1311
1246
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1312
1247
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1313
 
 
1314
 
 
1315
 
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1316
 
 
1317
 
    def test_pre_open_called(self):
1318
 
        calls = []
1319
 
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1320
 
        transport = self.get_transport('foo')
1321
 
        url = transport.base
1322
 
        self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1323
 
        self.assertEqual([transport.base], [t.base for t in calls])
1324
 
 
1325
 
    def test_pre_open_actual_exceptions_raised(self):
1326
 
        count = [0]
1327
 
        def fail_once(transport):
1328
 
            count[0] += 1
1329
 
            if count[0] == 1:
1330
 
                raise errors.BzrError("fail")
1331
 
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1332
 
        transport = self.get_transport('foo')
1333
 
        url = transport.base
1334
 
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1335
 
        self.assertEqual('fail', err._preformatted_string)