~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2007-02-06 14:52:16 UTC
  • mfrom: (2266 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2268.
  • Revision ID: abentley@panoramicfeedback.com-20070206145216-fcpi8o3ufvuzwbp9
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
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
 
    bzrdir,
30
 
    errors,
31
26
    help_topics,
32
 
    repository,
33
27
    symbol_versioning,
34
28
    urlutils,
35
 
    win32utils,
36
 
    workingtree,
37
29
    )
38
30
import bzrlib.branch
 
31
import bzrlib.bzrdir as bzrdir
 
32
import bzrlib.errors as errors
39
33
from bzrlib.errors import (NotBranchError,
40
34
                           UnknownFormatError,
41
35
                           UnsupportedFormatError,
42
36
                           )
43
 
from bzrlib.tests import (
44
 
    TestCase,
45
 
    TestCaseWithTransport,
46
 
    TestSkipped,
47
 
    test_sftp_transport
48
 
    )
49
 
from bzrlib.tests.http_server import HttpServer
50
 
from bzrlib.tests.http_utils import (
51
 
    TestCaseWithTwoWebservers,
52
 
    HTTPServerRedirecting,
53
 
    )
54
 
from bzrlib.tests.test_http import TestWithTransport_pycurl
 
37
import bzrlib.repository as repository
 
38
from bzrlib.tests import TestCase, TestCaseWithTransport, test_sftp_transport
 
39
from bzrlib.tests.HttpServer import HttpServer
55
40
from bzrlib.transport import get_transport
56
 
from bzrlib.transport.http._urllib import HttpTransport_urllib
57
41
from bzrlib.transport.memory import MemoryServer
58
 
from bzrlib.repofmt import knitrepo, weaverepo
 
42
import bzrlib.workingtree as workingtree
59
43
 
60
44
 
61
45
class TestDefaultFormat(TestCase):
64
48
        old_format = bzrdir.BzrDirFormat.get_default_format()
65
49
        # default is BzrDirFormat6
66
50
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
67
 
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
 
51
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
 
52
                             bzrdir.BzrDirFormat.set_default_format, 
 
53
                             SampleBzrDirFormat())
68
54
        # creating a bzr dir should now create an instrumented dir.
69
55
        try:
70
56
            result = bzrdir.BzrDir.create('memory:///')
71
57
            self.failUnless(isinstance(result, SampleBzrDir))
72
58
        finally:
73
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
59
            self.applyDeprecated(symbol_versioning.zero_fourteen,
 
60
                bzrdir.BzrDirFormat.set_default_format, old_format)
74
61
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
75
62
 
76
63
 
83
70
            ' repositories', deprecated=True)
84
71
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
85
72
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
86
 
        my_format_registry.register_metadir('knit',
87
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
88
 
            'Format using knits',
89
 
            )
 
73
        my_format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
 
74
            'Format using knits')
90
75
        my_format_registry.set_default('knit')
91
 
        my_format_registry.register_metadir(
92
 
            'branch6',
93
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
94
 
            'Experimental successor to knit.  Use at your own risk.',
95
 
            branch_format='bzrlib.branch.BzrBranchFormat6',
96
 
            experimental=True)
97
 
        my_format_registry.register_metadir(
98
 
            'hidden format',
99
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
100
 
            'Experimental successor to knit.  Use at your own risk.',
101
 
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
102
 
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
103
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
104
 
            ' repositories', hidden=True)
105
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
106
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
107
 
            hidden=True)
 
76
        my_format_registry.register_metadir('metaweave', 'RepositoryFormat7',
 
77
            'Transitional format in 0.8.  Slower than knit.', deprecated=True)
 
78
        my_format_registry.register_metadir('experimental-knit2', 
 
79
                                            'RepositoryFormatKnit2',
 
80
            'Experimental successor to knit.  Use at your own risk.')
108
81
        return my_format_registry
109
82
 
110
83
    def test_format_registry(self):
115
88
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
116
89
        my_bzrdir = my_format_registry.make_bzrdir('default')
117
90
        self.assertIsInstance(my_bzrdir.repository_format, 
118
 
            knitrepo.RepositoryFormatKnit1)
 
91
            repository.RepositoryFormatKnit1)
119
92
        my_bzrdir = my_format_registry.make_bzrdir('knit')
120
93
        self.assertIsInstance(my_bzrdir.repository_format, 
121
 
            knitrepo.RepositoryFormatKnit1)
122
 
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
123
 
        self.assertIsInstance(my_bzrdir.get_branch_format(),
124
 
                              bzrlib.branch.BzrBranchFormat6)
 
94
            repository.RepositoryFormatKnit1)
 
95
        my_bzrdir = my_format_registry.make_bzrdir('metaweave')
 
96
        self.assertIsInstance(my_bzrdir.repository_format, 
 
97
            repository.RepositoryFormat7)
125
98
 
126
99
    def test_get_help(self):
127
100
        my_format_registry = self.make_format_registry()
140
113
        topics.register('formats', self.make_format_registry().help_topic, 
141
114
                        'Directory formats')
142
115
        topic = topics.get_detail('formats')
143
 
        new, rest = topic.split('Experimental formats')
144
 
        experimental, deprecated = rest.split('Deprecated formats')
145
 
        self.assertContainsRe(new, 'These formats can be used')
 
116
        new, deprecated = topic.split('Deprecated formats')
 
117
        self.assertContainsRe(new, 'Bazaar directory formats')
146
118
        self.assertContainsRe(new, 
147
 
                ':knit:\n    \(native\) \(default\) Format using knits\n')
148
 
        self.assertContainsRe(experimental, 
149
 
                ':branch6:\n    \(native\) Experimental successor to knit')
 
119
            '  knit/default:\n    \(native\) Format using knits\n')
150
120
        self.assertContainsRe(deprecated, 
151
 
                ':lazy:\n    \(native\) Format registered lazily\n')
152
 
        self.assertNotContainsRe(new, 'hidden')
 
121
            '  lazy:\n    \(native\) Format registered lazily\n')
153
122
 
154
123
    def test_set_default_repository(self):
155
124
        default_factory = bzrdir.format_registry.get('default')
156
125
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
157
126
                       if v == default_factory and k != 'default'][0]
158
 
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
 
127
        bzrdir.format_registry.set_default_repository('metaweave')
159
128
        try:
160
 
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
 
129
            self.assertIs(bzrdir.format_registry.get('metaweave'),
161
130
                          bzrdir.format_registry.get('default'))
162
131
            self.assertIs(
163
132
                repository.RepositoryFormat.get_default_format().__class__,
164
 
                knitrepo.RepositoryFormatKnit3)
 
133
                repository.RepositoryFormat7)
165
134
        finally:
166
135
            bzrdir.format_registry.set_default_repository(old_default)
167
136
 
168
 
    def test_aliases(self):
169
 
        a_registry = bzrdir.BzrDirFormatRegistry()
170
 
        a_registry.register('weave', bzrdir.BzrDirFormat6,
171
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
172
 
            ' repositories', deprecated=True)
173
 
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
174
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
175
 
            ' repositories', deprecated=True, alias=True)
176
 
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
177
 
    
178
 
 
179
137
class SampleBranch(bzrlib.branch.Branch):
180
138
    """A dummy branch for guess what, dummy use."""
181
139
 
214
172
        """See BzrDirFormat.get_format_string()."""
215
173
        return "Sample .bzr dir format."
216
174
 
217
 
    def initialize_on_transport(self, t):
 
175
    def initialize(self, url):
218
176
        """Create a bzr dir."""
 
177
        t = get_transport(url)
219
178
        t.mkdir('.bzr')
220
179
        t.put_bytes('.bzr/branch-format', self.get_format_string())
221
180
        return SampleBzrDir(t, self)
276
235
        # now open_downlevel should fail too.
277
236
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
278
237
 
 
238
    def test_create_repository(self):
 
239
        format = SampleBzrDirFormat()
 
240
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
 
241
        self.assertEqual('A repository', repo)
 
242
 
 
243
    def test_create_repository_shared(self):
 
244
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
245
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
 
246
        self.assertTrue(repo.is_shared())
 
247
 
 
248
    def test_create_repository_nonshared(self):
 
249
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
250
        repo = bzrdir.BzrDir.create_repository('.')
 
251
        self.assertFalse(repo.is_shared())
 
252
 
 
253
    def test_create_repository_under_shared(self):
 
254
        # an explicit create_repository always does so.
 
255
        # we trust the format is right from the 'create_repository test'
 
256
        format = bzrdir.format_registry.make_bzrdir('knit')
 
257
        self.make_repository('.', shared=True, format=format)
 
258
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
 
259
                                               format=format)
 
260
        self.assertTrue(isinstance(repo, repository.Repository))
 
261
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
262
 
279
263
    def test_create_branch_and_repo_uses_default(self):
280
264
        format = SampleBzrDirFormat()
281
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
 
265
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
282
266
                                                      format=format)
283
267
        self.assertTrue(isinstance(branch, SampleBranch))
284
268
 
333
317
        branch.bzrdir.open_workingtree()
334
318
        branch.bzrdir.open_repository()
335
319
 
336
 
    def test_create_branch_convenience_possible_transports(self):
337
 
        """Check that the optional 'possible_transports' is recognized"""
338
 
        format = bzrdir.format_registry.make_bzrdir('knit')
339
 
        t = self.get_transport()
340
 
        branch = bzrdir.BzrDir.create_branch_convenience(
341
 
            '.', format=format, possible_transports=[t])
342
 
        branch.bzrdir.open_workingtree()
343
 
        branch.bzrdir.open_repository()
344
 
 
345
320
    def test_create_branch_convenience_root(self):
346
321
        """Creating a branch at the root of a fs should work."""
347
 
        self.vfs_transport_factory = MemoryServer
 
322
        self.transport_server = MemoryServer
348
323
        # outside a repo the default convenience output is a repo+branch_tree
349
324
        format = bzrdir.format_registry.make_bzrdir('knit')
350
325
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
412
387
        branch.bzrdir.open_workingtree()
413
388
 
414
389
 
415
 
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
416
 
 
417
 
    def test_acquire_repository_standalone(self):
418
 
        """The default acquisition policy should create a standalone branch."""
419
 
        my_bzrdir = self.make_bzrdir('.')
420
 
        repo_policy = my_bzrdir.determine_repository_policy()
421
 
        repo = repo_policy.acquire_repository()
422
 
        self.assertEqual(repo.bzrdir.root_transport.base,
423
 
                         my_bzrdir.root_transport.base)
424
 
        self.assertFalse(repo.is_shared())
425
 
 
426
 
 
427
 
    def test_determine_stacking_policy(self):
428
 
        parent_bzrdir = self.make_bzrdir('.')
429
 
        child_bzrdir = self.make_bzrdir('child')
430
 
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
431
 
        repo_policy = child_bzrdir.determine_repository_policy()
432
 
        self.assertEqual('http://example.org', repo_policy._stack_on)
433
 
 
434
 
    def test_determine_stacking_policy_relative(self):
435
 
        parent_bzrdir = self.make_bzrdir('.')
436
 
        child_bzrdir = self.make_bzrdir('child')
437
 
        parent_bzrdir.get_config().set_default_stack_on('child2')
438
 
        repo_policy = child_bzrdir.determine_repository_policy()
439
 
        self.assertEqual('child2', repo_policy._stack_on)
440
 
        self.assertEqual(parent_bzrdir.root_transport.base,
441
 
                         repo_policy._stack_on_pwd)
442
 
 
443
 
    def prepare_default_stacking(self):
444
 
        parent_bzrdir = self.make_bzrdir('.')
445
 
        child_branch = self.make_branch('child', format='development1')
446
 
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
447
 
        new_child_transport = parent_bzrdir.transport.clone('child2')
448
 
        return child_branch, new_child_transport
449
 
 
450
 
    def test_clone_on_transport_obeys_stacking_policy(self):
451
 
        child_branch, new_child_transport = self.prepare_default_stacking()
452
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
453
 
        self.assertEqual(child_branch.base,
454
 
                         new_child.open_branch().get_stacked_on_url())
455
 
 
456
 
    def test_sprout_obeys_stacking_policy(self):
457
 
        child_branch, new_child_transport = self.prepare_default_stacking()
458
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
459
 
        self.assertEqual(child_branch.base,
460
 
                         new_child.open_branch().get_stacked_on_url())
461
 
 
462
 
    def test_add_fallback_repo_handles_absolute_urls(self):
463
 
        stack_on = self.make_branch('stack_on', format='development1')
464
 
        repo = self.make_repository('repo', format='development1')
465
 
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
466
 
        policy._add_fallback(repo)
467
 
 
468
 
    def test_add_fallback_repo_handles_relative_urls(self):
469
 
        stack_on = self.make_branch('stack_on', format='development1')
470
 
        repo = self.make_repository('repo', format='development1')
471
 
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
472
 
        policy._add_fallback(repo)
473
 
 
474
 
    def test_configure_relative_branch_stacking_url(self):
475
 
        stack_on = self.make_branch('stack_on', format='development1')
476
 
        stacked = self.make_branch('stack_on/stacked', format='development1')
477
 
        policy = bzrdir.UseExistingRepository(stacked.repository,
478
 
            '.', stack_on.base)
479
 
        policy.configure_branch(stacked)
480
 
        self.assertEqual('..', stacked.get_stacked_on_url())
481
 
 
482
 
    def test_relative_branch_stacking_to_absolute(self):
483
 
        stack_on = self.make_branch('stack_on', format='development1')
484
 
        stacked = self.make_branch('stack_on/stacked', format='development1')
485
 
        policy = bzrdir.UseExistingRepository(stacked.repository,
486
 
            '.', self.get_readonly_url('stack_on'))
487
 
        policy.configure_branch(stacked)
488
 
        self.assertEqual(self.get_readonly_url('stack_on'),
489
 
                         stacked.get_stacked_on_url())
490
 
 
491
 
 
492
390
class ChrootedTests(TestCaseWithTransport):
493
391
    """A support class that provides readonly urls outside the local namespace.
494
392
 
499
397
 
500
398
    def setUp(self):
501
399
        super(ChrootedTests, self).setUp()
502
 
        if not self.vfs_transport_factory == MemoryServer:
 
400
        if not self.transport_server == MemoryServer:
503
401
            self.transport_readonly_server = HttpServer
504
402
 
505
 
    def local_branch_path(self, branch):
506
 
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
507
 
 
508
403
    def test_open_containing(self):
509
404
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
510
405
                          self.get_readonly_url(''))
516
411
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
517
412
        self.assertEqual('g/p/q', relpath)
518
413
 
519
 
    def test_open_containing_tree_branch_or_repository_empty(self):
520
 
        self.assertRaises(errors.NotBranchError,
521
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
522
 
            self.get_readonly_url(''))
523
 
 
524
 
    def test_open_containing_tree_branch_or_repository_all(self):
525
 
        self.make_branch_and_tree('topdir')
526
 
        tree, branch, repo, relpath = \
527
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
528
 
                'topdir/foo')
529
 
        self.assertEqual(os.path.realpath('topdir'),
530
 
                         os.path.realpath(tree.basedir))
531
 
        self.assertEqual(os.path.realpath('topdir'),
532
 
                         self.local_branch_path(branch))
533
 
        self.assertEqual(
534
 
            os.path.realpath(os.path.join('topdir', '.bzr', 'repository')),
535
 
            repo.bzrdir.transport.local_abspath('repository'))
536
 
        self.assertEqual(relpath, 'foo')
537
 
 
538
 
    def test_open_containing_tree_branch_or_repository_no_tree(self):
539
 
        self.make_branch('branch')
540
 
        tree, branch, repo, relpath = \
541
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
542
 
                'branch/foo')
543
 
        self.assertEqual(tree, None)
544
 
        self.assertEqual(os.path.realpath('branch'),
545
 
                         self.local_branch_path(branch))
546
 
        self.assertEqual(
547
 
            os.path.realpath(os.path.join('branch', '.bzr', 'repository')),
548
 
            repo.bzrdir.transport.local_abspath('repository'))
549
 
        self.assertEqual(relpath, 'foo')
550
 
 
551
 
    def test_open_containing_tree_branch_or_repository_repo(self):
552
 
        self.make_repository('repo')
553
 
        tree, branch, repo, relpath = \
554
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
555
 
                'repo')
556
 
        self.assertEqual(tree, None)
557
 
        self.assertEqual(branch, None)
558
 
        self.assertEqual(
559
 
            os.path.realpath(os.path.join('repo', '.bzr', 'repository')),
560
 
            repo.bzrdir.transport.local_abspath('repository'))
561
 
        self.assertEqual(relpath, '')
562
 
 
563
 
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
564
 
        self.make_repository('shared', shared=True)
565
 
        bzrdir.BzrDir.create_branch_convenience('shared/branch',
566
 
                                                force_new_tree=False)
567
 
        tree, branch, repo, relpath = \
568
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
569
 
                'shared/branch')
570
 
        self.assertEqual(tree, None)
571
 
        self.assertEqual(os.path.realpath('shared/branch'),
572
 
                         self.local_branch_path(branch))
573
 
        self.assertEqual(
574
 
            os.path.realpath(os.path.join('shared', '.bzr', 'repository')),
575
 
            repo.bzrdir.transport.local_abspath('repository'))
576
 
        self.assertEqual(relpath, '')
577
 
 
578
 
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
579
 
        self.make_branch_and_tree('foo')
580
 
        self.build_tree(['foo/bar/'])
581
 
        tree, branch, repo, relpath = \
582
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
583
 
                'foo/bar')
584
 
        self.assertEqual(os.path.realpath('foo'),
585
 
                         os.path.realpath(tree.basedir))
586
 
        self.assertEqual(os.path.realpath('foo'),
587
 
                         self.local_branch_path(branch))
588
 
        self.assertEqual(
589
 
            os.path.realpath(os.path.join('foo', '.bzr', 'repository')),
590
 
            repo.bzrdir.transport.local_abspath('repository'))
591
 
        self.assertEqual(relpath, 'bar')
592
 
 
593
 
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
594
 
        self.make_repository('bar')
595
 
        self.build_tree(['bar/baz/'])
596
 
        tree, branch, repo, relpath = \
597
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
598
 
                'bar/baz')
599
 
        self.assertEqual(tree, None)
600
 
        self.assertEqual(branch, None)
601
 
        self.assertEqual(
602
 
            os.path.realpath(os.path.join('bar', '.bzr', 'repository')),
603
 
            repo.bzrdir.transport.local_abspath('repository'))
604
 
        self.assertEqual(relpath, 'baz')
605
 
 
606
414
    def test_open_containing_from_transport(self):
607
415
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
608
416
                          get_transport(self.get_readonly_url('')))
617
425
        self.assertEqual('g/p/q', relpath)
618
426
 
619
427
    def test_open_containing_tree_or_branch(self):
 
428
        def local_branch_path(branch):
 
429
             return os.path.realpath(
 
430
                urlutils.local_path_from_url(branch.base))
 
431
 
620
432
        self.make_branch_and_tree('topdir')
621
433
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
622
434
            'topdir/foo')
623
435
        self.assertEqual(os.path.realpath('topdir'),
624
436
                         os.path.realpath(tree.basedir))
625
437
        self.assertEqual(os.path.realpath('topdir'),
626
 
                         self.local_branch_path(branch))
 
438
                         local_branch_path(branch))
627
439
        self.assertIs(tree.bzrdir, branch.bzrdir)
628
440
        self.assertEqual('foo', relpath)
629
 
        # opening from non-local should not return the tree
630
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
631
 
            self.get_readonly_url('topdir/foo'))
632
 
        self.assertEqual(None, tree)
633
 
        self.assertEqual('foo', relpath)
634
 
        # without a tree:
635
441
        self.make_branch('topdir/foo')
636
442
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
637
443
            'topdir/foo')
638
444
        self.assertIs(tree, None)
639
445
        self.assertEqual(os.path.realpath('topdir/foo'),
640
 
                         self.local_branch_path(branch))
 
446
                         local_branch_path(branch))
641
447
        self.assertEqual('', relpath)
642
448
 
643
 
    def test_open_tree_or_branch(self):
644
 
        self.make_branch_and_tree('topdir')
645
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
646
 
        self.assertEqual(os.path.realpath('topdir'),
647
 
                         os.path.realpath(tree.basedir))
648
 
        self.assertEqual(os.path.realpath('topdir'),
649
 
                         self.local_branch_path(branch))
650
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
651
 
        # opening from non-local should not return the tree
652
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
653
 
            self.get_readonly_url('topdir'))
654
 
        self.assertEqual(None, tree)
655
 
        # without a tree:
656
 
        self.make_branch('topdir/foo')
657
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
658
 
        self.assertIs(tree, None)
659
 
        self.assertEqual(os.path.realpath('topdir/foo'),
660
 
                         self.local_branch_path(branch))
661
 
 
662
449
    def test_open_from_transport(self):
663
450
        # transport pointing at bzrdir should give a bzrdir with root transport
664
451
        # set to the given transport
681
468
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
682
469
                          transport)
683
470
 
684
 
    def test_sprout_recursive(self):
685
 
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
686
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
687
 
            format='dirstate-with-subtree')
688
 
        tree.add_reference(sub_tree)
689
 
        self.build_tree(['tree1/subtree/file'])
690
 
        sub_tree.add('file')
691
 
        tree.commit('Initial commit')
692
 
        tree.bzrdir.sprout('tree2')
693
 
        self.failUnlessExists('tree2/subtree/file')
694
 
 
695
 
    def test_cloning_metadir(self):
696
 
        """Ensure that cloning metadir is suitable"""
697
 
        bzrdir = self.make_bzrdir('bzrdir')
698
 
        bzrdir.cloning_metadir()
699
 
        branch = self.make_branch('branch', format='knit')
700
 
        format = branch.bzrdir.cloning_metadir()
701
 
        self.assertIsInstance(format.workingtree_format,
702
 
            workingtree.WorkingTreeFormat3)
703
 
 
704
 
    def test_sprout_recursive_treeless(self):
705
 
        tree = self.make_branch_and_tree('tree1',
706
 
            format='dirstate-with-subtree')
707
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
708
 
            format='dirstate-with-subtree')
709
 
        tree.add_reference(sub_tree)
710
 
        self.build_tree(['tree1/subtree/file'])
711
 
        sub_tree.add('file')
712
 
        tree.commit('Initial commit')
713
 
        tree.bzrdir.destroy_workingtree()
714
 
        repo = self.make_repository('repo', shared=True,
715
 
            format='dirstate-with-subtree')
716
 
        repo.set_make_working_trees(False)
717
 
        tree.bzrdir.sprout('repo/tree2')
718
 
        self.failUnlessExists('repo/tree2/subtree')
719
 
        self.failIfExists('repo/tree2/subtree/file')
720
 
 
721
 
    def make_foo_bar_baz(self):
722
 
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
723
 
        bar = self.make_branch('foo/bar').bzrdir
724
 
        baz = self.make_branch('baz').bzrdir
725
 
        return foo, bar, baz
726
 
 
727
 
    def test_find_bzrdirs(self):
728
 
        foo, bar, baz = self.make_foo_bar_baz()
729
 
        transport = get_transport(self.get_url())
730
 
        self.assertEqualBzrdirs([baz, foo, bar],
731
 
                                bzrdir.BzrDir.find_bzrdirs(transport))
732
 
 
733
 
    def test_find_bzrdirs_list_current(self):
734
 
        def list_current(transport):
735
 
            return [s for s in transport.list_dir('') if s != 'baz']
736
 
 
737
 
        foo, bar, baz = self.make_foo_bar_baz()
738
 
        transport = get_transport(self.get_url())
739
 
        self.assertEqualBzrdirs([foo, bar],
740
 
                                bzrdir.BzrDir.find_bzrdirs(transport,
741
 
                                    list_current=list_current))
742
 
 
743
 
 
744
 
    def test_find_bzrdirs_evaluate(self):
745
 
        def evaluate(bzrdir):
746
 
            try:
747
 
                repo = bzrdir.open_repository()
748
 
            except NoRepositoryPresent:
749
 
                return True, bzrdir.root_transport.base
750
 
            else:
751
 
                return False, bzrdir.root_transport.base
752
 
 
753
 
        foo, bar, baz = self.make_foo_bar_baz()
754
 
        transport = get_transport(self.get_url())
755
 
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
756
 
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
757
 
                                                         evaluate=evaluate)))
758
 
 
759
 
    def assertEqualBzrdirs(self, first, second):
760
 
        first = list(first)
761
 
        second = list(second)
762
 
        self.assertEqual(len(first), len(second))
763
 
        for x, y in zip(first, second):
764
 
            self.assertEqual(x.root_transport.base, y.root_transport.base)
765
 
 
766
 
    def test_find_branches(self):
767
 
        root = self.make_repository('', shared=True)
768
 
        foo, bar, baz = self.make_foo_bar_baz()
769
 
        qux = self.make_bzrdir('foo/qux')
770
 
        transport = get_transport(self.get_url())
771
 
        branches = bzrdir.BzrDir.find_branches(transport)
772
 
        self.assertEqual(baz.root_transport.base, branches[0].base)
773
 
        self.assertEqual(foo.root_transport.base, branches[1].base)
774
 
        self.assertEqual(bar.root_transport.base, branches[2].base)
775
 
 
776
 
        # ensure this works without a top-level repo
777
 
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
778
 
        self.assertEqual(foo.root_transport.base, branches[0].base)
779
 
        self.assertEqual(bar.root_transport.base, branches[1].base)
780
 
 
781
471
 
782
472
class TestMeta1DirFormat(TestCaseWithTransport):
783
473
    """Tests specific to the meta1 dir format."""
792
482
        repository_base = t.clone('repository').base
793
483
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
794
484
        self.assertEqual(repository_base,
795
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
485
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
796
486
        checkout_base = t.clone('checkout').base
797
487
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
798
488
        self.assertEqual(checkout_base,
804
494
        t = dir.transport
805
495
        self.assertIsDirectory('branch-lock', t)
806
496
 
807
 
    def test_comparison(self):
808
 
        """Equality and inequality behave properly.
809
 
 
810
 
        Metadirs should compare equal iff they have the same repo, branch and
811
 
        tree formats.
812
 
        """
813
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
814
 
        self.assertEqual(mydir, mydir)
815
 
        self.assertFalse(mydir != mydir)
816
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
817
 
        self.assertEqual(otherdir, mydir)
818
 
        self.assertFalse(otherdir != mydir)
819
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
820
 
        self.assertNotEqual(otherdir2, mydir)
821
 
        self.assertFalse(otherdir2 == mydir)
822
 
 
823
 
    def test_needs_conversion_different_working_tree(self):
824
 
        # meta1dirs need an conversion if any element is not the default.
825
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
826
 
        # test with 
827
 
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
828
 
        bzrdir.BzrDirFormat._set_default_format(new_default)
829
 
        try:
830
 
            tree = self.make_branch_and_tree('tree', format='knit')
831
 
            self.assertTrue(tree.bzrdir.needs_format_conversion())
832
 
        finally:
833
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
834
 
 
835
 
 
 
497
        
836
498
class TestFormat5(TestCaseWithTransport):
837
499
    """Tests specific to the version 5 bzrdir format."""
838
500
 
973
635
 
974
636
    def setUp(self):
975
637
        super(NonLocalTests, self).setUp()
976
 
        self.vfs_transport_factory = MemoryServer
 
638
        self.transport_server = MemoryServer
977
639
    
978
640
    def test_create_branch_convenience(self):
979
641
        # outside a repo the default convenience output is a repo+branch_tree
1007
669
        result.open_branch()
1008
670
        result.open_repository()
1009
671
 
1010
 
    def test_checkout_metadir(self):
1011
 
        # checkout_metadir has reasonable working tree format even when no
1012
 
        # working tree is present
1013
 
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
1014
 
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1015
 
        checkout_format = my_bzrdir.checkout_metadir()
1016
 
        self.assertIsInstance(checkout_format.workingtree_format,
1017
 
                              workingtree.WorkingTreeFormat3)
1018
 
 
1019
 
 
1020
 
class TestHTTPRedirectionLoop(object):
1021
 
    """Test redirection loop between two http servers.
1022
 
 
1023
 
    This MUST be used by daughter classes that also inherit from
1024
 
    TestCaseWithTwoWebservers.
1025
 
 
1026
 
    We can't inherit directly from TestCaseWithTwoWebservers or the
1027
 
    test framework will try to create an instance which cannot
1028
 
    run, its implementation being incomplete. 
1029
 
    """
1030
 
 
1031
 
    # Should be defined by daughter classes to ensure redirection
1032
 
    # still use the same transport implementation (not currently
1033
 
    # enforced as it's a bit tricky to get right (see the FIXME
1034
 
    # in BzrDir.open_from_transport for the unique use case so
1035
 
    # far)
1036
 
    _qualifier = None
1037
 
 
1038
 
    def create_transport_readonly_server(self):
1039
 
        return HTTPServerRedirecting()
1040
 
 
1041
 
    def create_transport_secondary_server(self):
1042
 
        return HTTPServerRedirecting()
1043
 
 
1044
 
    def setUp(self):
1045
 
        # Both servers redirect to each server creating a loop
1046
 
        super(TestHTTPRedirectionLoop, self).setUp()
1047
 
        # The redirections will point to the new server
1048
 
        self.new_server = self.get_readonly_server()
1049
 
        # The requests to the old server will be redirected
1050
 
        self.old_server = self.get_secondary_server()
1051
 
        # Configure the redirections
1052
 
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1053
 
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1054
 
 
1055
 
    def _qualified_url(self, host, port):
1056
 
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
1057
 
 
1058
 
    def test_loop(self):
1059
 
        # Starting from either server should loop
1060
 
        old_url = self._qualified_url(self.old_server.host, 
1061
 
                                      self.old_server.port)
1062
 
        oldt = self._transport(old_url)
1063
 
        self.assertRaises(errors.NotBranchError,
1064
 
                          bzrdir.BzrDir.open_from_transport, oldt)
1065
 
        new_url = self._qualified_url(self.new_server.host, 
1066
 
                                      self.new_server.port)
1067
 
        newt = self._transport(new_url)
1068
 
        self.assertRaises(errors.NotBranchError,
1069
 
                          bzrdir.BzrDir.open_from_transport, newt)
1070
 
 
1071
 
 
1072
 
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
1073
 
                                  TestCaseWithTwoWebservers):
1074
 
    """Tests redirections for urllib implementation"""
1075
 
 
1076
 
    _qualifier = 'urllib'
1077
 
    _transport = HttpTransport_urllib
1078
 
 
1079
 
 
1080
 
 
1081
 
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1082
 
                                  TestHTTPRedirectionLoop,
1083
 
                                  TestCaseWithTwoWebservers):
1084
 
    """Tests redirections for pycurl implementation"""
1085
 
 
1086
 
    _qualifier = 'pycurl'
1087
 
 
1088
 
 
1089
 
class TestDotBzrHidden(TestCaseWithTransport):
1090
 
 
1091
 
    ls = ['ls']
1092
 
    if sys.platform == 'win32':
1093
 
        ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
1094
 
 
1095
 
    def get_ls(self):
1096
 
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1097
 
            stderr=subprocess.PIPE)
1098
 
        out, err = f.communicate()
1099
 
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1100
 
                         % (self.ls, err))
1101
 
        return out.splitlines()
1102
 
 
1103
 
    def test_dot_bzr_hidden(self):
1104
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1105
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1106
 
        b = bzrdir.BzrDir.create('.')
1107
 
        self.build_tree(['a'])
1108
 
        self.assertEquals(['a'], self.get_ls())
1109
 
 
1110
 
    def test_dot_bzr_hidden_with_url(self):
1111
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1112
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1113
 
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1114
 
        self.build_tree(['a'])
1115
 
        self.assertEquals(['a'], self.get_ls())
 
672
 
 
673
class TestRemoteSFTP(test_sftp_transport.TestCaseWithSFTPServer):
 
674
 
 
675
    def test_open_containing_tree_or_branch(self):
 
676
        tree = self.make_branch_and_tree('tree')
 
677
        bzrdir.BzrDir.open_containing_tree_or_branch(self.get_url('tree'))