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
17
17
"""Tests for the BzrDir facility and any format specific tests.
19
For interface contract tests, see tests/per_bzr_dir.
19
For interface contract tests, see tests/bzr_dir_implementations.
23
from StringIO import StringIO
26
27
from bzrlib import (
35
revision as _mod_revision,
38
transport as _mod_transport,
44
37
import bzrlib.branch
45
from bzrlib.branchfmt.fullhistory import BzrBranchFormat5
46
from bzrlib.errors import (
48
NoColocatedBranchSupport,
50
UnsupportedFormatError,
38
from bzrlib.errors import (NotBranchError,
40
UnsupportedFormatError,
52
42
from bzrlib.tests import (
54
TestCaseWithMemoryTransport,
55
44
TestCaseWithTransport,
58
from bzrlib.tests import(
48
from bzrlib.tests.http_server import HttpServer
49
from bzrlib.tests.http_utils import (
50
TestCaseWithTwoWebservers,
51
HTTPServerRedirecting,
62
53
from bzrlib.tests.test_http import TestWithTransport_pycurl
63
from bzrlib.transport import (
54
from bzrlib.transport import get_transport
67
55
from bzrlib.transport.http._urllib import HttpTransport_urllib
68
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
from bzrlib.repofmt import knitrepo, knitpack_repo
56
from bzrlib.transport.memory import MemoryServer
57
from bzrlib.repofmt import knitrepo, weaverepo
73
60
class TestDefaultFormat(TestCase):
75
62
def test_get_set_default_format(self):
76
63
old_format = bzrdir.BzrDirFormat.get_default_format()
77
# default is BzrDirMetaFormat1
78
self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
79
controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
64
# default is BzrDirFormat6
65
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
66
bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
80
67
# creating a bzr dir should now create an instrumented dir.
82
69
result = bzrdir.BzrDir.create('memory:///')
83
self.assertIsInstance(result, SampleBzrDir)
70
self.failUnless(isinstance(result, SampleBzrDir))
85
controldir.ControlDirFormat._set_default_format(old_format)
72
bzrdir.BzrDirFormat._set_default_format(old_format)
86
73
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
89
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
90
"""A deprecated bzr dir format."""
93
76
class TestFormatRegistry(TestCase):
95
78
def make_format_registry(self):
96
my_format_registry = controldir.ControlDirFormatRegistry()
97
my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
98
'Some format. Slower and unawesome and deprecated.',
100
my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
101
'DeprecatedBzrDirFormat', 'Format registered lazily',
103
bzrdir.register_metadir(my_format_registry, 'knit',
79
my_format_registry = bzrdir.BzrDirFormatRegistry()
80
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
81
'Pre-0.8 format. Slower and does not support checkouts or shared'
82
' repositories', deprecated=True)
83
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
84
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
85
my_format_registry.register_metadir('knit',
104
86
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
105
87
'Format using knits',
107
89
my_format_registry.set_default('knit')
108
bzrdir.register_metadir(my_format_registry,
90
my_format_registry.register_metadir(
110
92
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
111
93
'Experimental successor to knit. Use at your own risk.',
112
94
branch_format='bzrlib.branch.BzrBranchFormat6',
113
95
experimental=True)
114
bzrdir.register_metadir(my_format_registry,
96
my_format_registry.register_metadir(
116
98
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
117
99
'Experimental successor to knit. Use at your own risk.',
118
100
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
119
my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
120
'Old format. Slower and does not support things. ', hidden=True)
121
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
122
'DeprecatedBzrDirFormat', 'Format registered lazily',
123
deprecated=True, hidden=True)
101
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
102
'Pre-0.8 format. Slower and does not support checkouts or shared'
103
' repositories', hidden=True)
104
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
105
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
124
107
return my_format_registry
126
109
def test_format_registry(self):
127
110
my_format_registry = self.make_format_registry()
128
111
my_bzrdir = my_format_registry.make_bzrdir('lazy')
129
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
130
my_bzrdir = my_format_registry.make_bzrdir('deprecated')
131
self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
112
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
113
my_bzrdir = my_format_registry.make_bzrdir('weave')
114
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
132
115
my_bzrdir = my_format_registry.make_bzrdir('default')
133
self.assertIsInstance(my_bzrdir.repository_format,
116
self.assertIsInstance(my_bzrdir.repository_format,
134
117
knitrepo.RepositoryFormatKnit1)
135
118
my_bzrdir = my_format_registry.make_bzrdir('knit')
136
self.assertIsInstance(my_bzrdir.repository_format,
119
self.assertIsInstance(my_bzrdir.repository_format,
137
120
knitrepo.RepositoryFormatKnit1)
138
121
my_bzrdir = my_format_registry.make_bzrdir('branch6')
139
122
self.assertIsInstance(my_bzrdir.get_branch_format(),
143
126
my_format_registry = self.make_format_registry()
144
127
self.assertEqual('Format registered lazily',
145
128
my_format_registry.get_help('lazy'))
146
self.assertEqual('Format using knits',
129
self.assertEqual('Format using knits',
147
130
my_format_registry.get_help('knit'))
148
self.assertEqual('Format using knits',
131
self.assertEqual('Format using knits',
149
132
my_format_registry.get_help('default'))
150
self.assertEqual('Some format. Slower and unawesome and deprecated.',
151
my_format_registry.get_help('deprecated'))
133
self.assertEqual('Pre-0.8 format. Slower and does not support'
134
' checkouts or shared repositories',
135
my_format_registry.get_help('weave'))
153
137
def test_help_topic(self):
154
138
topics = help_topics.HelpTopicRegistry()
155
registry = self.make_format_registry()
156
topics.register('current-formats', registry.help_topic,
158
topics.register('other-formats', registry.help_topic,
160
new = topics.get_detail('current-formats')
161
rest = topics.get_detail('other-formats')
139
topics.register('formats', self.make_format_registry().help_topic,
141
topic = topics.get_detail('formats')
142
new, rest = topic.split('Experimental formats')
162
143
experimental, deprecated = rest.split('Deprecated formats')
163
self.assertContainsRe(new, 'formats-help')
164
self.assertContainsRe(new,
144
self.assertContainsRe(new, 'These formats can be used')
145
self.assertContainsRe(new,
165
146
':knit:\n \(native\) \(default\) Format using knits\n')
166
self.assertContainsRe(experimental,
147
self.assertContainsRe(experimental,
167
148
':branch6:\n \(native\) Experimental successor to knit')
168
self.assertContainsRe(deprecated,
149
self.assertContainsRe(deprecated,
169
150
':lazy:\n \(native\) Format registered lazily\n')
170
151
self.assertNotContainsRe(new, 'hidden')
172
153
def test_set_default_repository(self):
173
default_factory = controldir.format_registry.get('default')
174
old_default = [k for k, v in controldir.format_registry.iteritems()
154
default_factory = bzrdir.format_registry.get('default')
155
old_default = [k for k, v in bzrdir.format_registry.iteritems()
175
156
if v == default_factory and k != 'default'][0]
176
controldir.format_registry.set_default_repository('dirstate-with-subtree')
157
bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
178
self.assertIs(controldir.format_registry.get('dirstate-with-subtree'),
179
controldir.format_registry.get('default'))
159
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
160
bzrdir.format_registry.get('default'))
181
repository.format_registry.get_default().__class__,
162
repository.RepositoryFormat.get_default_format().__class__,
182
163
knitrepo.RepositoryFormatKnit3)
184
controldir.format_registry.set_default_repository(old_default)
165
bzrdir.format_registry.set_default_repository(old_default)
186
167
def test_aliases(self):
187
a_registry = controldir.ControlDirFormatRegistry()
188
a_registry.register('deprecated', DeprecatedBzrDirFormat,
189
'Old format. Slower and does not support stuff',
191
a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
192
'Old format. Slower and does not support stuff',
193
deprecated=True, alias=True)
194
self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
168
a_registry = bzrdir.BzrDirFormatRegistry()
169
a_registry.register('weave', bzrdir.BzrDirFormat6,
170
'Pre-0.8 format. Slower and does not support checkouts or shared'
171
' repositories', deprecated=True)
172
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
173
'Pre-0.8 format. Slower and does not support checkouts or shared'
174
' repositories', deprecated=True, alias=True)
175
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
197
178
class SampleBranch(bzrlib.branch.Branch):
198
179
"""A dummy branch for guess what, dummy use."""
470
417
"""The default acquisition policy should create a standalone branch."""
471
418
my_bzrdir = self.make_bzrdir('.')
472
419
repo_policy = my_bzrdir.determine_repository_policy()
473
repo, is_new = repo_policy.acquire_repository()
420
repo = repo_policy.acquire_repository()
474
421
self.assertEqual(repo.bzrdir.root_transport.base,
475
422
my_bzrdir.root_transport.base)
476
423
self.assertFalse(repo.is_shared())
478
def test_determine_stacking_policy(self):
479
parent_bzrdir = self.make_bzrdir('.')
480
child_bzrdir = self.make_bzrdir('child')
481
parent_bzrdir.get_config().set_default_stack_on('http://example.org')
482
repo_policy = child_bzrdir.determine_repository_policy()
483
self.assertEqual('http://example.org', repo_policy._stack_on)
485
def test_determine_stacking_policy_relative(self):
486
parent_bzrdir = self.make_bzrdir('.')
487
child_bzrdir = self.make_bzrdir('child')
488
parent_bzrdir.get_config().set_default_stack_on('child2')
489
repo_policy = child_bzrdir.determine_repository_policy()
490
self.assertEqual('child2', repo_policy._stack_on)
491
self.assertEqual(parent_bzrdir.root_transport.base,
492
repo_policy._stack_on_pwd)
494
def prepare_default_stacking(self, child_format='1.6'):
495
parent_bzrdir = self.make_bzrdir('.')
496
child_branch = self.make_branch('child', format=child_format)
497
parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
498
new_child_transport = parent_bzrdir.transport.clone('child2')
499
return child_branch, new_child_transport
501
def test_clone_on_transport_obeys_stacking_policy(self):
502
child_branch, new_child_transport = self.prepare_default_stacking()
503
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
504
self.assertEqual(child_branch.base,
505
new_child.open_branch().get_stacked_on_url())
507
def test_default_stacking_with_stackable_branch_unstackable_repo(self):
508
# Make stackable source branch with an unstackable repo format.
509
source_bzrdir = self.make_bzrdir('source')
510
knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
511
source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
513
# Make a directory with a default stacking policy
514
parent_bzrdir = self.make_bzrdir('parent')
515
stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
516
parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
517
# Clone source into directory
518
target = source_bzrdir.clone(self.get_url('parent/target'))
520
def test_format_initialize_on_transport_ex_stacked_on(self):
521
# trunk is a stackable format. Note that its in the same server area
522
# which is what launchpad does, but not sufficient to exercise the
524
trunk = self.make_branch('trunk', format='1.9')
525
t = self.get_transport('stacked')
526
old_fmt = controldir.format_registry.make_bzrdir('pack-0.92')
527
repo_name = old_fmt.repository_format.network_name()
528
# Should end up with a 1.9 format (stackable)
529
repo, control, require_stacking, repo_policy = \
530
old_fmt.initialize_on_transport_ex(t,
531
repo_format_name=repo_name, stacked_on='../trunk',
534
# Repositories are open write-locked
535
self.assertTrue(repo.is_write_locked())
536
self.addCleanup(repo.unlock)
538
repo = control.open_repository()
539
self.assertIsInstance(control, bzrdir.BzrDir)
540
opened = bzrdir.BzrDir.open(t.base)
541
if not isinstance(old_fmt, remote.RemoteBzrDirFormat):
542
self.assertEqual(control._format.network_name(),
543
old_fmt.network_name())
544
self.assertEqual(control._format.network_name(),
545
opened._format.network_name())
546
self.assertEqual(control.__class__, opened.__class__)
547
self.assertLength(1, repo._fallback_repositories)
549
def test_sprout_obeys_stacking_policy(self):
550
child_branch, new_child_transport = self.prepare_default_stacking()
551
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
552
self.assertEqual(child_branch.base,
553
new_child.open_branch().get_stacked_on_url())
555
def test_clone_ignores_policy_for_unsupported_formats(self):
556
child_branch, new_child_transport = self.prepare_default_stacking(
557
child_format='pack-0.92')
558
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
559
self.assertRaises(errors.UnstackableBranchFormat,
560
new_child.open_branch().get_stacked_on_url)
562
def test_sprout_ignores_policy_for_unsupported_formats(self):
563
child_branch, new_child_transport = self.prepare_default_stacking(
564
child_format='pack-0.92')
565
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
566
self.assertRaises(errors.UnstackableBranchFormat,
567
new_child.open_branch().get_stacked_on_url)
569
def test_sprout_upgrades_format_if_stacked_specified(self):
570
child_branch, new_child_transport = self.prepare_default_stacking(
571
child_format='pack-0.92')
572
new_child = child_branch.bzrdir.sprout(new_child_transport.base,
574
self.assertEqual(child_branch.bzrdir.root_transport.base,
575
new_child.open_branch().get_stacked_on_url())
576
repo = new_child.open_repository()
577
self.assertTrue(repo._format.supports_external_lookups)
578
self.assertFalse(repo.supports_rich_root())
580
def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
581
child_branch, new_child_transport = self.prepare_default_stacking(
582
child_format='pack-0.92')
583
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
584
stacked_on=child_branch.bzrdir.root_transport.base)
585
self.assertEqual(child_branch.bzrdir.root_transport.base,
586
new_child.open_branch().get_stacked_on_url())
587
repo = new_child.open_repository()
588
self.assertTrue(repo._format.supports_external_lookups)
589
self.assertFalse(repo.supports_rich_root())
591
def test_sprout_upgrades_to_rich_root_format_if_needed(self):
592
child_branch, new_child_transport = self.prepare_default_stacking(
593
child_format='rich-root-pack')
594
new_child = child_branch.bzrdir.sprout(new_child_transport.base,
596
repo = new_child.open_repository()
597
self.assertTrue(repo._format.supports_external_lookups)
598
self.assertTrue(repo.supports_rich_root())
600
def test_add_fallback_repo_handles_absolute_urls(self):
601
stack_on = self.make_branch('stack_on', format='1.6')
602
repo = self.make_repository('repo', format='1.6')
603
policy = bzrdir.UseExistingRepository(repo, stack_on.base)
604
policy._add_fallback(repo)
606
def test_add_fallback_repo_handles_relative_urls(self):
607
stack_on = self.make_branch('stack_on', format='1.6')
608
repo = self.make_repository('repo', format='1.6')
609
policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
610
policy._add_fallback(repo)
612
def test_configure_relative_branch_stacking_url(self):
613
stack_on = self.make_branch('stack_on', format='1.6')
614
stacked = self.make_branch('stack_on/stacked', format='1.6')
615
policy = bzrdir.UseExistingRepository(stacked.repository,
617
policy.configure_branch(stacked)
618
self.assertEqual('..', stacked.get_stacked_on_url())
620
def test_relative_branch_stacking_to_absolute(self):
621
stack_on = self.make_branch('stack_on', format='1.6')
622
stacked = self.make_branch('stack_on/stacked', format='1.6')
623
policy = bzrdir.UseExistingRepository(stacked.repository,
624
'.', self.get_readonly_url('stack_on'))
625
policy.configure_branch(stacked)
626
self.assertEqual(self.get_readonly_url('stack_on'),
627
stacked.get_stacked_on_url())
630
426
class ChrootedTests(TestCaseWithTransport):
631
427
"""A support class that provides readonly urls outside the local namespace.
654
447
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
655
448
self.assertEqual('g/p/q', relpath)
657
def test_open_containing_tree_branch_or_repository_empty(self):
658
self.assertRaises(errors.NotBranchError,
659
bzrdir.BzrDir.open_containing_tree_branch_or_repository,
660
self.get_readonly_url(''))
662
def test_open_containing_tree_branch_or_repository_all(self):
663
self.make_branch_and_tree('topdir')
664
tree, branch, repo, relpath = \
665
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
667
self.assertEqual(os.path.realpath('topdir'),
668
os.path.realpath(tree.basedir))
669
self.assertEqual(os.path.realpath('topdir'),
670
self.local_branch_path(branch))
672
osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
673
repo.bzrdir.transport.local_abspath('repository'))
674
self.assertEqual(relpath, 'foo')
676
def test_open_containing_tree_branch_or_repository_no_tree(self):
677
self.make_branch('branch')
678
tree, branch, repo, relpath = \
679
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
681
self.assertEqual(tree, None)
682
self.assertEqual(os.path.realpath('branch'),
683
self.local_branch_path(branch))
685
osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
686
repo.bzrdir.transport.local_abspath('repository'))
687
self.assertEqual(relpath, 'foo')
689
def test_open_containing_tree_branch_or_repository_repo(self):
690
self.make_repository('repo')
691
tree, branch, repo, relpath = \
692
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
694
self.assertEqual(tree, None)
695
self.assertEqual(branch, None)
697
osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
698
repo.bzrdir.transport.local_abspath('repository'))
699
self.assertEqual(relpath, '')
701
def test_open_containing_tree_branch_or_repository_shared_repo(self):
702
self.make_repository('shared', shared=True)
703
bzrdir.BzrDir.create_branch_convenience('shared/branch',
704
force_new_tree=False)
705
tree, branch, repo, relpath = \
706
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
708
self.assertEqual(tree, None)
709
self.assertEqual(os.path.realpath('shared/branch'),
710
self.local_branch_path(branch))
712
osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
713
repo.bzrdir.transport.local_abspath('repository'))
714
self.assertEqual(relpath, '')
716
def test_open_containing_tree_branch_or_repository_branch_subdir(self):
717
self.make_branch_and_tree('foo')
718
self.build_tree(['foo/bar/'])
719
tree, branch, repo, relpath = \
720
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
722
self.assertEqual(os.path.realpath('foo'),
723
os.path.realpath(tree.basedir))
724
self.assertEqual(os.path.realpath('foo'),
725
self.local_branch_path(branch))
727
osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
728
repo.bzrdir.transport.local_abspath('repository'))
729
self.assertEqual(relpath, 'bar')
731
def test_open_containing_tree_branch_or_repository_repo_subdir(self):
732
self.make_repository('bar')
733
self.build_tree(['bar/baz/'])
734
tree, branch, repo, relpath = \
735
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
737
self.assertEqual(tree, None)
738
self.assertEqual(branch, None)
740
osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
741
repo.bzrdir.transport.local_abspath('repository'))
742
self.assertEqual(relpath, 'baz')
744
450
def test_open_containing_from_transport(self):
745
self.assertRaises(NotBranchError,
746
bzrdir.BzrDir.open_containing_from_transport,
747
_mod_transport.get_transport_from_url(self.get_readonly_url('')))
748
self.assertRaises(NotBranchError,
749
bzrdir.BzrDir.open_containing_from_transport,
750
_mod_transport.get_transport_from_url(
751
self.get_readonly_url('g/p/q')))
451
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
452
get_transport(self.get_readonly_url('')))
453
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
454
get_transport(self.get_readonly_url('g/p/q')))
752
455
control = bzrdir.BzrDir.create(self.get_url())
753
456
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
754
_mod_transport.get_transport_from_url(
755
self.get_readonly_url('')))
457
get_transport(self.get_readonly_url('')))
756
458
self.assertEqual('', relpath)
757
459
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
758
_mod_transport.get_transport_from_url(
759
self.get_readonly_url('g/p/q')))
460
get_transport(self.get_readonly_url('g/p/q')))
760
461
self.assertEqual('g/p/q', relpath)
762
463
def test_open_containing_tree_or_branch(self):
464
def local_branch_path(branch):
465
return os.path.realpath(
466
urlutils.local_path_from_url(branch.base))
763
468
self.make_branch_and_tree('topdir')
764
469
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
766
471
self.assertEqual(os.path.realpath('topdir'),
767
472
os.path.realpath(tree.basedir))
768
473
self.assertEqual(os.path.realpath('topdir'),
769
self.local_branch_path(branch))
474
local_branch_path(branch))
770
475
self.assertIs(tree.bzrdir, branch.bzrdir)
771
476
self.assertEqual('foo', relpath)
772
477
# opening from non-local should not return the tree
800
509
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
801
510
self.assertIs(tree, None)
802
511
self.assertEqual(os.path.realpath('topdir/foo'),
803
self.local_branch_path(branch))
512
local_branch_path(branch))
805
514
def test_open_from_transport(self):
806
515
# transport pointing at bzrdir should give a bzrdir with root transport
807
516
# set to the given transport
808
517
control = bzrdir.BzrDir.create(self.get_url())
809
t = self.get_transport()
810
opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
811
self.assertEqual(t.base, opened_bzrdir.root_transport.base)
518
transport = get_transport(self.get_url())
519
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
520
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
812
521
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
814
523
def test_open_from_transport_no_bzrdir(self):
815
t = self.get_transport()
816
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
524
transport = get_transport(self.get_url())
525
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
818
528
def test_open_from_transport_bzrdir_in_parent(self):
819
529
control = bzrdir.BzrDir.create(self.get_url())
820
t = self.get_transport()
822
t = t.clone('subdir')
823
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
530
transport = get_transport(self.get_url())
531
transport.mkdir('subdir')
532
transport = transport.clone('subdir')
533
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
825
536
def test_sprout_recursive(self):
826
tree = self.make_branch_and_tree('tree1',
827
format='development-subtree')
537
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
828
538
sub_tree = self.make_branch_and_tree('tree1/subtree',
829
format='development-subtree')
830
sub_tree.set_root_id('subtree-root')
539
format='dirstate-with-subtree')
831
540
tree.add_reference(sub_tree)
832
541
self.build_tree(['tree1/subtree/file'])
833
542
sub_tree.add('file')
834
543
tree.commit('Initial commit')
835
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
837
self.addCleanup(tree2.unlock)
838
self.assertPathExists('tree2/subtree/file')
839
self.assertEqual('tree-reference', tree2.kind('subtree-root'))
544
tree.bzrdir.sprout('tree2')
545
self.failUnlessExists('tree2/subtree/file')
841
547
def test_cloning_metadir(self):
842
548
"""Ensure that cloning metadir is suitable"""
887
579
def test_find_bzrdirs(self):
888
580
foo, bar, baz = self.make_foo_bar_baz()
889
t = self.get_transport()
890
self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_bzrdirs(t))
892
def make_fake_permission_denied_transport(self, transport, paths):
893
"""Create a transport that raises PermissionDenied for some paths."""
896
raise errors.PermissionDenied(path)
898
path_filter_server = pathfilter.PathFilteringServer(transport, filter)
899
path_filter_server.start_server()
900
self.addCleanup(path_filter_server.stop_server)
901
path_filter_transport = pathfilter.PathFilteringTransport(
902
path_filter_server, '.')
903
return (path_filter_server, path_filter_transport)
905
def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
906
"""Check that each branch url ends with the given suffix."""
907
for actual_bzrdir in actual_bzrdirs:
908
self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
910
def test_find_bzrdirs_permission_denied(self):
911
foo, bar, baz = self.make_foo_bar_baz()
912
t = self.get_transport()
913
path_filter_server, path_filter_transport = \
914
self.make_fake_permission_denied_transport(t, ['foo'])
916
self.assertBranchUrlsEndWith('/baz/',
917
bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
919
smart_transport = self.make_smart_server('.',
920
backing_server=path_filter_server)
921
self.assertBranchUrlsEndWith('/baz/',
922
bzrdir.BzrDir.find_bzrdirs(smart_transport))
581
transport = get_transport(self.get_url())
582
self.assertEqualBzrdirs([baz, foo, bar],
583
bzrdir.BzrDir.find_bzrdirs(transport))
924
585
def test_find_bzrdirs_list_current(self):
925
586
def list_current(transport):
926
587
return [s for s in transport.list_dir('') if s != 'baz']
928
589
foo, bar, baz = self.make_foo_bar_baz()
929
t = self.get_transport()
930
self.assertEqualBzrdirs(
932
bzrdir.BzrDir.find_bzrdirs(t, list_current=list_current))
590
transport = get_transport(self.get_url())
591
self.assertEqualBzrdirs([foo, bar],
592
bzrdir.BzrDir.find_bzrdirs(transport,
593
list_current=list_current))
934
596
def test_find_bzrdirs_evaluate(self):
935
597
def evaluate(bzrdir):
937
599
repo = bzrdir.open_repository()
938
except errors.NoRepositoryPresent:
600
except NoRepositoryPresent:
939
601
return True, bzrdir.root_transport.base
941
603
return False, bzrdir.root_transport.base
943
605
foo, bar, baz = self.make_foo_bar_baz()
944
t = self.get_transport()
606
transport = get_transport(self.get_url())
945
607
self.assertEqual([baz.root_transport.base, foo.root_transport.base],
946
list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
608
list(bzrdir.BzrDir.find_bzrdirs(transport,
948
611
def assertEqualBzrdirs(self, first, second):
949
612
first = list(first)
1015
662
Metadirs should compare equal iff they have the same repo, branch and
1018
mydir = controldir.format_registry.make_bzrdir('knit')
665
mydir = bzrdir.format_registry.make_bzrdir('knit')
1019
666
self.assertEqual(mydir, mydir)
1020
667
self.assertFalse(mydir != mydir)
1021
otherdir = controldir.format_registry.make_bzrdir('knit')
668
otherdir = bzrdir.format_registry.make_bzrdir('knit')
1022
669
self.assertEqual(otherdir, mydir)
1023
670
self.assertFalse(otherdir != mydir)
1024
otherdir2 = controldir.format_registry.make_bzrdir('development-subtree')
671
otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
1025
672
self.assertNotEqual(otherdir2, mydir)
1026
673
self.assertFalse(otherdir2 == mydir)
1028
def test_with_features(self):
1029
tree = self.make_branch_and_tree('tree', format='2a')
1030
tree.bzrdir.update_feature_flags({"bar": "required"})
1031
self.assertRaises(errors.MissingFeature, bzrdir.BzrDir.open, 'tree')
1032
bzrdir.BzrDirMetaFormat1.register_feature('bar')
1033
self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, 'bar')
1034
dir = bzrdir.BzrDir.open('tree')
1035
self.assertEqual("required", dir._format.features.get("bar"))
1036
tree.bzrdir.update_feature_flags({"bar": None, "nonexistant": None})
1037
dir = bzrdir.BzrDir.open('tree')
1038
self.assertEqual({}, dir._format.features)
1040
675
def test_needs_conversion_different_working_tree(self):
1041
676
# meta1dirs need an conversion if any element is not the default.
1042
new_format = controldir.format_registry.make_bzrdir('dirstate')
1043
tree = self.make_branch_and_tree('tree', format='knit')
1044
self.assertTrue(tree.bzrdir.needs_format_conversion(
1047
def test_initialize_on_format_uses_smart_transport(self):
1048
self.setup_smart_server_with_call_log()
1049
new_format = controldir.format_registry.make_bzrdir('dirstate')
1050
transport = self.get_transport('target')
1051
transport.ensure_base()
1052
self.reset_smart_call_log()
1053
instance = new_format.initialize_on_transport(transport)
1054
self.assertIsInstance(instance, remote.RemoteBzrDir)
1055
rpc_count = len(self.hpss_calls)
1056
# This figure represent the amount of work to perform this use case. It
1057
# is entirely ok to reduce this number if a test fails due to rpc_count
1058
# being too low. If rpc_count increases, more network roundtrips have
1059
# become necessary for this use case. Please do not adjust this number
1060
# upwards without agreement from bzr's network support maintainers.
1061
self.assertEqual(2, rpc_count)
677
old_format = bzrdir.BzrDirFormat.get_default_format()
679
new_default = bzrdir.format_registry.make_bzrdir('dirstate')
680
bzrdir.BzrDirFormat._set_default_format(new_default)
682
tree = self.make_branch_and_tree('tree', format='knit')
683
self.assertTrue(tree.bzrdir.needs_format_conversion())
685
bzrdir.BzrDirFormat._set_default_format(old_format)
688
class TestFormat5(TestCaseWithTransport):
689
"""Tests specific to the version 5 bzrdir format."""
691
def test_same_lockfiles_between_tree_repo_branch(self):
692
# this checks that only a single lockfiles instance is created
693
# for format 5 objects
694
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
695
def check_dir_components_use_same_lock(dir):
696
ctrl_1 = dir.open_repository().control_files
697
ctrl_2 = dir.open_branch().control_files
698
ctrl_3 = dir.open_workingtree()._control_files
699
self.assertTrue(ctrl_1 is ctrl_2)
700
self.assertTrue(ctrl_2 is ctrl_3)
701
check_dir_components_use_same_lock(dir)
702
# and if we open it normally.
703
dir = bzrdir.BzrDir.open(self.get_url())
704
check_dir_components_use_same_lock(dir)
706
def test_can_convert(self):
707
# format 5 dirs are convertable
708
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
709
self.assertTrue(dir.can_convert_format())
711
def test_needs_conversion(self):
712
# format 5 dirs need a conversion if they are not the default.
713
# and they start of not the default.
714
old_format = bzrdir.BzrDirFormat.get_default_format()
715
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
717
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
718
self.assertFalse(dir.needs_format_conversion())
720
bzrdir.BzrDirFormat._set_default_format(old_format)
721
self.assertTrue(dir.needs_format_conversion())
724
class TestFormat6(TestCaseWithTransport):
725
"""Tests specific to the version 6 bzrdir format."""
727
def test_same_lockfiles_between_tree_repo_branch(self):
728
# this checks that only a single lockfiles instance is created
729
# for format 6 objects
730
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
731
def check_dir_components_use_same_lock(dir):
732
ctrl_1 = dir.open_repository().control_files
733
ctrl_2 = dir.open_branch().control_files
734
ctrl_3 = dir.open_workingtree()._control_files
735
self.assertTrue(ctrl_1 is ctrl_2)
736
self.assertTrue(ctrl_2 is ctrl_3)
737
check_dir_components_use_same_lock(dir)
738
# and if we open it normally.
739
dir = bzrdir.BzrDir.open(self.get_url())
740
check_dir_components_use_same_lock(dir)
742
def test_can_convert(self):
743
# format 6 dirs are convertable
744
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
745
self.assertTrue(dir.can_convert_format())
747
def test_needs_conversion(self):
748
# format 6 dirs need an conversion if they are not the default.
749
old_format = bzrdir.BzrDirFormat.get_default_format()
750
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
752
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
753
self.assertTrue(dir.needs_format_conversion())
755
bzrdir.BzrDirFormat._set_default_format(old_format)
758
class NotBzrDir(bzrlib.bzrdir.BzrDir):
759
"""A non .bzr based control directory."""
761
def __init__(self, transport, format):
762
self._format = format
763
self.root_transport = transport
764
self.transport = transport.clone('.not')
767
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
768
"""A test class representing any non-.bzr based disk format."""
770
def initialize_on_transport(self, transport):
771
"""Initialize a new .not dir in the base directory of a Transport."""
772
transport.mkdir('.not')
773
return self.open(transport)
775
def open(self, transport):
776
"""Open this directory."""
777
return NotBzrDir(transport, self)
780
def _known_formats(self):
781
return set([NotBzrDirFormat()])
784
def probe_transport(self, transport):
785
"""Our format is present if the transport ends in '.not/'."""
786
if transport.has('.not'):
787
return NotBzrDirFormat()
790
class TestNotBzrDir(TestCaseWithTransport):
791
"""Tests for using the bzrdir api with a non .bzr based disk format.
793
If/when one of these is in the core, we can let the implementation tests
797
def test_create_and_find_format(self):
798
# create a .notbzr dir
799
format = NotBzrDirFormat()
800
dir = format.initialize(self.get_url())
801
self.assertIsInstance(dir, NotBzrDir)
803
bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
805
found = bzrlib.bzrdir.BzrDirFormat.find_format(
806
get_transport(self.get_url()))
807
self.assertIsInstance(found, NotBzrDirFormat)
809
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
811
def test_included_in_known_formats(self):
812
bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
814
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
815
for format in formats:
816
if isinstance(format, NotBzrDirFormat):
818
self.fail("No NotBzrDirFormat in %s" % formats)
820
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
1064
823
class NonLocalTests(TestCaseWithTransport):
1107
866
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1108
867
checkout_format = my_bzrdir.checkout_metadir()
1109
868
self.assertIsInstance(checkout_format.workingtree_format,
1110
workingtree_4.WorkingTreeFormat4)
1113
class TestHTTPRedirections(object):
1114
"""Test redirection between two http servers.
869
workingtree.WorkingTreeFormat3)
872
class TestHTTPRedirectionLoop(object):
873
"""Test redirection loop between two http servers.
1116
875
This MUST be used by daughter classes that also inherit from
1117
876
TestCaseWithTwoWebservers.
1119
878
We can't inherit directly from TestCaseWithTwoWebservers or the
1120
879
test framework will try to create an instance which cannot
1121
run, its implementation being incomplete.
880
run, its implementation being incomplete.
883
# Should be defined by daughter classes to ensure redirection
884
# still use the same transport implementation (not currently
885
# enforced as it's a bit tricky to get right (see the FIXME
886
# in BzrDir.open_from_transport for the unique use case so
1124
890
def create_transport_readonly_server(self):
1125
# We don't set the http protocol version, relying on the default
1126
return http_utils.HTTPServerRedirecting()
891
return HTTPServerRedirecting()
1128
893
def create_transport_secondary_server(self):
1129
# We don't set the http protocol version, relying on the default
1130
return http_utils.HTTPServerRedirecting()
894
return HTTPServerRedirecting()
1132
896
def setUp(self):
1133
super(TestHTTPRedirections, self).setUp()
897
# Both servers redirect to each server creating a loop
898
super(TestHTTPRedirectionLoop, self).setUp()
1134
899
# The redirections will point to the new server
1135
900
self.new_server = self.get_readonly_server()
1136
901
# The requests to the old server will be redirected
1137
902
self.old_server = self.get_secondary_server()
1138
903
# Configure the redirections
1139
904
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
905
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
907
def _qualified_url(self, host, port):
908
return 'http+%s://%s:%s' % (self._qualifier, host, port)
1141
910
def test_loop(self):
1142
# Both servers redirect to each other creating a loop
1143
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1144
911
# Starting from either server should loop
1145
old_url = self._qualified_url(self.old_server.host,
912
old_url = self._qualified_url(self.old_server.host,
1146
913
self.old_server.port)
1147
914
oldt = self._transport(old_url)
1148
915
self.assertRaises(errors.NotBranchError,
1149
916
bzrdir.BzrDir.open_from_transport, oldt)
1150
new_url = self._qualified_url(self.new_server.host,
917
new_url = self._qualified_url(self.new_server.host,
1151
918
self.new_server.port)
1152
919
newt = self._transport(new_url)
1153
920
self.assertRaises(errors.NotBranchError,
1154
921
bzrdir.BzrDir.open_from_transport, newt)
1156
def test_qualifier_preserved(self):
1157
wt = self.make_branch_and_tree('branch')
1158
old_url = self._qualified_url(self.old_server.host,
1159
self.old_server.port)
1160
start = self._transport(old_url).clone('branch')
1161
bdir = bzrdir.BzrDir.open_from_transport(start)
1162
# Redirection should preserve the qualifier, hence the transport class
1164
self.assertIsInstance(bdir.root_transport, type(start))
1167
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1168
http_utils.TestCaseWithTwoWebservers):
924
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
925
TestCaseWithTwoWebservers):
1169
926
"""Tests redirections for urllib implementation"""
928
_qualifier = 'urllib'
1171
929
_transport = HttpTransport_urllib
1173
def _qualified_url(self, host, port):
1174
result = 'http+urllib://%s:%s' % (host, port)
1175
self.permit_url(result)
1180
933
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1181
TestHTTPRedirections,
1182
http_utils.TestCaseWithTwoWebservers):
934
TestHTTPRedirectionLoop,
935
TestCaseWithTwoWebservers):
1183
936
"""Tests redirections for pycurl implementation"""
1185
def _qualified_url(self, host, port):
1186
result = 'http+pycurl://%s:%s' % (host, port)
1187
self.permit_url(result)
1191
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1192
http_utils.TestCaseWithTwoWebservers):
1193
"""Tests redirections for the nosmart decorator"""
1195
_transport = NoSmartTransportDecorator
1197
def _qualified_url(self, host, port):
1198
result = 'nosmart+http://%s:%s' % (host, port)
1199
self.permit_url(result)
1203
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1204
http_utils.TestCaseWithTwoWebservers):
1205
"""Tests redirections for readonly decoratror"""
1207
_transport = ReadonlyTransportDecorator
1209
def _qualified_url(self, host, port):
1210
result = 'readonly+http://%s:%s' % (host, port)
1211
self.permit_url(result)
938
_qualifier = 'pycurl'
1215
941
class TestDotBzrHidden(TestCaseWithTransport):
1231
957
raise TestSkipped('unable to make file hidden without pywin32 library')
1232
958
b = bzrdir.BzrDir.create('.')
1233
959
self.build_tree(['a'])
1234
self.assertEqual(['a'], self.get_ls())
960
self.assertEquals(['a'], self.get_ls())
1236
962
def test_dot_bzr_hidden_with_url(self):
1237
963
if sys.platform == 'win32' and not win32utils.has_win32file:
1238
964
raise TestSkipped('unable to make file hidden without pywin32 library')
1239
965
b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1240
966
self.build_tree(['a'])
1241
self.assertEqual(['a'], self.get_ls())
1244
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1245
"""Test BzrDirFormat implementation for TestBzrDirSprout."""
1247
def _open(self, transport):
1248
return _TestBzrDir(transport, self)
1251
class _TestBzrDir(bzrdir.BzrDirMeta1):
1252
"""Test BzrDir implementation for TestBzrDirSprout.
1254
When created a _TestBzrDir already has repository and a branch. The branch
1255
is a test double as well.
1258
def __init__(self, *args, **kwargs):
1259
super(_TestBzrDir, self).__init__(*args, **kwargs)
1260
self.test_branch = _TestBranch(self.transport)
1261
self.test_branch.repository = self.create_repository()
1263
def open_branch(self, unsupported=False, possible_transports=None):
1264
return self.test_branch
1266
def cloning_metadir(self, require_stacking=False):
1267
return _TestBzrDirFormat()
1270
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1271
"""Test Branch format for TestBzrDirSprout."""
1274
class _TestBranch(bzrlib.branch.Branch):
1275
"""Test Branch implementation for TestBzrDirSprout."""
1277
def __init__(self, transport, *args, **kwargs):
1278
self._format = _TestBranchFormat()
1279
self._transport = transport
1280
self.base = transport.base
1281
super(_TestBranch, self).__init__(*args, **kwargs)
1285
def sprout(self, *args, **kwargs):
1286
self.calls.append('sprout')
1287
return _TestBranch(self._transport)
1289
def copy_content_into(self, destination, revision_id=None):
1290
self.calls.append('copy_content_into')
1292
def last_revision(self):
1293
return _mod_revision.NULL_REVISION
1295
def get_parent(self):
1298
def _get_config(self):
1299
return config.TransportConfig(self._transport, 'branch.conf')
1301
def _get_config_store(self):
1302
return config.BranchStore(self)
1304
def set_parent(self, parent):
1305
self._parent = parent
1307
def lock_read(self):
1308
return lock.LogicalLockResult(self.unlock)
1314
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1316
def test_sprout_uses_branch_sprout(self):
1317
"""BzrDir.sprout calls Branch.sprout.
1319
Usually, BzrDir.sprout should delegate to the branch's sprout method
1320
for part of the work. This allows the source branch to control the
1321
choice of format for the new branch.
1323
There are exceptions, but this tests avoids them:
1324
- if there's no branch in the source bzrdir,
1325
- or if the stacking has been requested and the format needs to be
1326
overridden to satisfy that.
1328
# Make an instrumented bzrdir.
1329
t = self.get_transport('source')
1331
source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1332
# The instrumented bzrdir has a test_branch attribute that logs calls
1333
# made to the branch contained in that bzrdir. Initially the test
1334
# branch exists but no calls have been made to it.
1335
self.assertEqual([], source_bzrdir.test_branch.calls)
1338
target_url = self.get_url('target')
1339
result = source_bzrdir.sprout(target_url, recurse='no')
1341
# The bzrdir called the branch's sprout method.
1342
self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
1344
def test_sprout_parent(self):
1345
grandparent_tree = self.make_branch('grandparent')
1346
parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1347
branch_tree = parent.bzrdir.sprout('branch').open_branch()
1348
self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1351
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1353
def test_pre_open_called(self):
1355
bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1356
transport = self.get_transport('foo')
1357
url = transport.base
1358
self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1359
self.assertEqual([transport.base], [t.base for t in calls])
1361
def test_pre_open_actual_exceptions_raised(self):
1363
def fail_once(transport):
1366
raise errors.BzrError("fail")
1367
bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1368
transport = self.get_transport('foo')
1369
url = transport.base
1370
err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1371
self.assertEqual('fail', err._preformatted_string)
1373
def test_post_repo_init(self):
1374
from bzrlib.controldir import RepoInitHookParams
1376
bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1378
self.make_repository('foo')
1379
self.assertLength(1, calls)
1381
self.assertIsInstance(params, RepoInitHookParams)
1382
self.assertTrue(hasattr(params, 'bzrdir'))
1383
self.assertTrue(hasattr(params, 'repository'))
1385
def test_post_repo_init_hook_repr(self):
1387
bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1388
lambda params: param_reprs.append(repr(params)), None)
1389
self.make_repository('foo')
1390
self.assertLength(1, param_reprs)
1391
param_repr = param_reprs[0]
1392
self.assertStartsWith(param_repr, '<RepoInitHookParams for ')
1395
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1396
# FIXME: This may need to be unified with test_osutils.TestBackupNames or
1397
# moved to per_bzrdir or per_transport for better coverage ?
1401
super(TestGenerateBackupName, self).setUp()
1402
self._transport = self.get_transport()
1403
bzrdir.BzrDir.create(self.get_url(),
1404
possible_transports=[self._transport])
1405
self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1408
self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
1410
def test_exiting(self):
1411
self._transport.put_bytes("a.~1~", "some content")
1412
self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1415
class TestMeta1DirColoFormat(TestCaseWithTransport):
1416
"""Tests specific to the meta1 dir with colocated branches format."""
1418
def test_supports_colo(self):
1419
format = bzrdir.BzrDirMetaFormat1Colo()
1420
self.assertTrue(format.colocated_branches)
1422
def test_upgrade_from_2a(self):
1423
tree = self.make_branch_and_tree('.', format='2a')
1424
format = bzrdir.BzrDirMetaFormat1Colo()
1425
self.assertTrue(tree.bzrdir.needs_format_conversion(format))
1426
converter = tree.bzrdir._format.get_converter(format)
1427
result = converter.convert(tree.bzrdir, None)
1428
self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1Colo)
1429
self.assertFalse(result.needs_format_conversion(format))
1431
def test_downgrade_to_2a(self):
1432
tree = self.make_branch_and_tree('.', format='development-colo')
1433
format = bzrdir.BzrDirMetaFormat1()
1434
self.assertTrue(tree.bzrdir.needs_format_conversion(format))
1435
converter = tree.bzrdir._format.get_converter(format)
1436
result = converter.convert(tree.bzrdir, None)
1437
self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1438
self.assertFalse(result.needs_format_conversion(format))
1440
def test_downgrade_to_2a_too_many_branches(self):
1441
tree = self.make_branch_and_tree('.', format='development-colo')
1442
tree.bzrdir.create_branch(name="another-colocated-branch")
1443
converter = tree.bzrdir._format.get_converter(
1444
bzrdir.BzrDirMetaFormat1())
1445
result = converter.convert(tree.bzrdir, bzrdir.BzrDirMetaFormat1())
1446
self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1448
def test_nested(self):
1449
tree = self.make_branch_and_tree('.', format='development-colo')
1450
tree.bzrdir.create_branch(name='foo')
1451
tree.bzrdir.create_branch(name='fool/bla')
1453
errors.ParentBranchExists, tree.bzrdir.create_branch,
1456
def test_parent(self):
1457
tree = self.make_branch_and_tree('.', format='development-colo')
1458
tree.bzrdir.create_branch(name='fool/bla')
1459
tree.bzrdir.create_branch(name='foo/bar')
1461
errors.AlreadyBranchError, tree.bzrdir.create_branch,
1465
class SampleBzrFormat(bzrdir.BzrFormat):
1468
def get_format_string(cls):
1469
return "First line\n"
1472
class TestBzrFormat(TestCase):
1473
"""Tests for BzrFormat."""
1475
def test_as_string(self):
1476
format = SampleBzrFormat()
1477
format.features = {"foo": "required"}
1478
self.assertEqual(format.as_string(),
1481
format.features["another"] = "optional"
1482
self.assertEqual(format.as_string(),
1485
"optional another\n")
1487
def test_network_name(self):
1488
# The network string should include the feature info
1489
format = SampleBzrFormat()
1490
format.features = {"foo": "required"}
1492
"First line\nrequired foo\n",
1493
format.network_name())
1495
def test_from_string_no_features(self):
1497
format = SampleBzrFormat.from_string(
1499
self.assertEqual({}, format.features)
1501
def test_from_string_with_feature(self):
1503
format = SampleBzrFormat.from_string(
1504
"First line\nrequired foo\n")
1505
self.assertEqual("required", format.features.get("foo"))
1507
def test_from_string_format_string_mismatch(self):
1508
# The first line has to match the format string
1509
self.assertRaises(AssertionError, SampleBzrFormat.from_string,
1510
"Second line\nrequired foo\n")
1512
def test_from_string_missing_space(self):
1513
# At least one space is required in the feature lines
1514
self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
1515
"First line\nfoo\n")
1517
def test_from_string_with_spaces(self):
1518
# Feature with spaces (in case we add stuff like this in the future)
1519
format = SampleBzrFormat.from_string(
1520
"First line\nrequired foo with spaces\n")
1521
self.assertEqual("required", format.features.get("foo with spaces"))
1524
format1 = SampleBzrFormat()
1525
format1.features = {"nested-trees": "optional"}
1526
format2 = SampleBzrFormat()
1527
format2.features = {"nested-trees": "optional"}
1528
self.assertEqual(format1, format1)
1529
self.assertEqual(format1, format2)
1530
format3 = SampleBzrFormat()
1531
self.assertNotEqual(format1, format3)
1533
def test_check_support_status_optional(self):
1534
# Optional, so silently ignore
1535
format = SampleBzrFormat()
1536
format.features = {"nested-trees": "optional"}
1537
format.check_support_status(True)
1538
self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1539
SampleBzrFormat.register_feature("nested-trees")
1540
format.check_support_status(True)
1542
def test_check_support_status_required(self):
1543
# Optional, so trigger an exception
1544
format = SampleBzrFormat()
1545
format.features = {"nested-trees": "required"}
1546
self.assertRaises(errors.MissingFeature, format.check_support_status,
1548
self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1549
SampleBzrFormat.register_feature("nested-trees")
1550
format.check_support_status(True)
1552
def test_check_support_status_unknown(self):
1553
# treat unknown necessity as required
1554
format = SampleBzrFormat()
1555
format.features = {"nested-trees": "unknown"}
1556
self.assertRaises(errors.MissingFeature, format.check_support_status,
1558
self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1559
SampleBzrFormat.register_feature("nested-trees")
1560
format.check_support_status(True)
1562
def test_feature_already_registered(self):
1563
# a feature can only be registered once
1564
self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1565
SampleBzrFormat.register_feature("nested-trees")
1566
self.assertRaises(errors.FeatureAlreadyRegistered,
1567
SampleBzrFormat.register_feature, "nested-trees")
1569
def test_feature_with_space(self):
1570
# spaces are not allowed in feature names
1571
self.assertRaises(ValueError, SampleBzrFormat.register_feature,
967
self.assertEquals(['a'], self.get_ls())