1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the BzrDir facility and any format specific tests.
19
For interface contract tests, see tests/bzr_dir_implementations.
23
from StringIO import StringIO
38
from bzrlib.errors import (NotBranchError,
40
UnsupportedFormatError,
42
from bzrlib.symbol_versioning import (
45
from bzrlib.tests import (
47
TestCaseWithTransport,
51
from bzrlib.tests.http_server import HttpServer
52
from bzrlib.tests.http_utils import (
53
TestCaseWithTwoWebservers,
54
HTTPServerRedirecting,
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
57
from bzrlib.transport import get_transport
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
59
from bzrlib.transport.memory import MemoryServer
60
from bzrlib.repofmt import knitrepo, weaverepo
63
class TestDefaultFormat(TestCase):
65
def test_get_set_default_format(self):
66
old_format = bzrdir.BzrDirFormat.get_default_format()
67
# default is BzrDirFormat6
68
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
69
self.applyDeprecated(symbol_versioning.zero_fourteen,
70
bzrdir.BzrDirFormat.set_default_format,
72
# creating a bzr dir should now create an instrumented dir.
74
result = bzrdir.BzrDir.create('memory:///')
75
self.failUnless(isinstance(result, SampleBzrDir))
77
self.applyDeprecated(symbol_versioning.zero_fourteen,
78
bzrdir.BzrDirFormat.set_default_format, old_format)
79
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
82
class TestFormatRegistry(TestCase):
84
def make_format_registry(self):
85
my_format_registry = bzrdir.BzrDirFormatRegistry()
86
my_format_registry.register('weave', bzrdir.BzrDirFormat6,
87
'Pre-0.8 format. Slower and does not support checkouts or shared'
88
' repositories', deprecated=True)
89
my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
90
'BzrDirFormat6', 'Format registered lazily', deprecated=True)
91
my_format_registry.register_metadir('knit',
92
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
95
my_format_registry.set_default('knit')
96
my_format_registry.register_metadir(
98
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
99
'Experimental successor to knit. Use at your own risk.',
100
branch_format='bzrlib.branch.BzrBranchFormat6',
102
my_format_registry.register_metadir(
104
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
105
'Experimental successor to knit. Use at your own risk.',
106
branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
107
my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
108
'Pre-0.8 format. Slower and does not support checkouts or shared'
109
' repositories', hidden=True)
110
my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
111
'BzrDirFormat6', 'Format registered lazily', deprecated=True,
113
return my_format_registry
115
def test_format_registry(self):
116
my_format_registry = self.make_format_registry()
117
my_bzrdir = my_format_registry.make_bzrdir('lazy')
118
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
119
my_bzrdir = my_format_registry.make_bzrdir('weave')
120
self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
121
my_bzrdir = my_format_registry.make_bzrdir('default')
122
self.assertIsInstance(my_bzrdir.repository_format,
123
knitrepo.RepositoryFormatKnit1)
124
my_bzrdir = my_format_registry.make_bzrdir('knit')
125
self.assertIsInstance(my_bzrdir.repository_format,
126
knitrepo.RepositoryFormatKnit1)
127
my_bzrdir = my_format_registry.make_bzrdir('branch6')
128
self.assertIsInstance(my_bzrdir.get_branch_format(),
129
bzrlib.branch.BzrBranchFormat6)
131
def test_get_help(self):
132
my_format_registry = self.make_format_registry()
133
self.assertEqual('Format registered lazily',
134
my_format_registry.get_help('lazy'))
135
self.assertEqual('Format using knits',
136
my_format_registry.get_help('knit'))
137
self.assertEqual('Format using knits',
138
my_format_registry.get_help('default'))
139
self.assertEqual('Pre-0.8 format. Slower and does not support'
140
' checkouts or shared repositories',
141
my_format_registry.get_help('weave'))
143
def test_help_topic(self):
144
topics = help_topics.HelpTopicRegistry()
145
topics.register('formats', self.make_format_registry().help_topic,
147
topic = topics.get_detail('formats')
148
new, rest = topic.split('Experimental formats')
149
experimental, deprecated = rest.split('Deprecated formats')
150
self.assertContainsRe(new, 'These formats can be used')
151
self.assertContainsRe(new,
152
':knit:\n \(native\) \(default\) Format using knits\n')
153
self.assertContainsRe(experimental,
154
':branch6:\n \(native\) Experimental successor to knit')
155
self.assertContainsRe(deprecated,
156
':lazy:\n \(native\) Format registered lazily\n')
157
self.assertNotContainsRe(new, 'hidden')
159
def test_set_default_repository(self):
160
default_factory = bzrdir.format_registry.get('default')
161
old_default = [k for k, v in bzrdir.format_registry.iteritems()
162
if v == default_factory and k != 'default'][0]
163
bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
165
self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
166
bzrdir.format_registry.get('default'))
168
repository.RepositoryFormat.get_default_format().__class__,
169
knitrepo.RepositoryFormatKnit3)
171
bzrdir.format_registry.set_default_repository(old_default)
173
def test_aliases(self):
174
a_registry = bzrdir.BzrDirFormatRegistry()
175
a_registry.register('weave', bzrdir.BzrDirFormat6,
176
'Pre-0.8 format. Slower and does not support checkouts or shared'
177
' repositories', deprecated=True)
178
a_registry.register('weavealias', bzrdir.BzrDirFormat6,
179
'Pre-0.8 format. Slower and does not support checkouts or shared'
180
' repositories', deprecated=True, alias=True)
181
self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
184
class SampleBranch(bzrlib.branch.Branch):
185
"""A dummy branch for guess what, dummy use."""
187
def __init__(self, dir):
191
class SampleBzrDir(bzrdir.BzrDir):
192
"""A sample BzrDir implementation to allow testing static methods."""
194
def create_repository(self, shared=False):
195
"""See BzrDir.create_repository."""
196
return "A repository"
198
def open_repository(self):
199
"""See BzrDir.open_repository."""
200
return "A repository"
202
def create_branch(self):
203
"""See BzrDir.create_branch."""
204
return SampleBranch(self)
206
def create_workingtree(self):
207
"""See BzrDir.create_workingtree."""
211
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
214
this format is initializable, unsupported to aid in testing the
215
open and open_downlevel routines.
218
def get_format_string(self):
219
"""See BzrDirFormat.get_format_string()."""
220
return "Sample .bzr dir format."
222
def initialize_on_transport(self, t):
223
"""Create a bzr dir."""
225
t.put_bytes('.bzr/branch-format', self.get_format_string())
226
return SampleBzrDir(t, self)
228
def is_supported(self):
231
def open(self, transport, _found=None):
232
return "opened branch."
235
class TestBzrDirFormat(TestCaseWithTransport):
236
"""Tests for the BzrDirFormat facility."""
238
def test_find_format(self):
239
# is the right format object found for a branch?
240
# create a branch with a few known format objects.
241
# this is not quite the same as
242
t = get_transport(self.get_url())
243
self.build_tree(["foo/", "bar/"], transport=t)
244
def check_format(format, url):
245
format.initialize(url)
246
t = get_transport(url)
247
found_format = bzrdir.BzrDirFormat.find_format(t)
248
self.failUnless(isinstance(found_format, format.__class__))
249
check_format(bzrdir.BzrDirFormat5(), "foo")
250
check_format(bzrdir.BzrDirFormat6(), "bar")
252
def test_find_format_nothing_there(self):
253
self.assertRaises(NotBranchError,
254
bzrdir.BzrDirFormat.find_format,
257
def test_find_format_unknown_format(self):
258
t = get_transport(self.get_url())
260
t.put_bytes('.bzr/branch-format', '')
261
self.assertRaises(UnknownFormatError,
262
bzrdir.BzrDirFormat.find_format,
265
def test_register_unregister_format(self):
266
format = SampleBzrDirFormat()
269
format.initialize(url)
270
# register a format for it.
271
bzrdir.BzrDirFormat.register_format(format)
272
# which bzrdir.Open will refuse (not supported)
273
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
274
# which bzrdir.open_containing will refuse (not supported)
275
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
276
# but open_downlevel will work
277
t = get_transport(url)
278
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
279
# unregister the format
280
bzrdir.BzrDirFormat.unregister_format(format)
281
# now open_downlevel should fail too.
282
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
284
def test_create_repository_deprecated(self):
285
# new interface is to make the bzrdir, then a repository within that.
286
format = SampleBzrDirFormat()
287
repo = self.applyDeprecated(zero_ninetyone,
288
bzrdir.BzrDir.create_repository,
289
self.get_url(), format=format)
290
self.assertEqual('A repository', repo)
292
def test_create_repository_shared(self):
293
# new interface is to make the bzrdir, then a repository within that.
294
old_format = bzrdir.BzrDirFormat.get_default_format()
295
repo = self.applyDeprecated(zero_ninetyone,
296
bzrdir.BzrDir.create_repository,
298
self.assertTrue(repo.is_shared())
300
def test_create_repository_nonshared(self):
301
# new interface is to make the bzrdir, then a repository within that.
302
old_format = bzrdir.BzrDirFormat.get_default_format()
303
repo = self.applyDeprecated(zero_ninetyone,
304
bzrdir.BzrDir.create_repository,
306
self.assertFalse(repo.is_shared())
308
def test_create_repository_under_shared(self):
309
# an explicit create_repository always does so.
310
# we trust the format is right from the 'create_repository test'
311
# new interface is to make the bzrdir, then a repository within that.
312
format = bzrdir.format_registry.make_bzrdir('knit')
313
self.make_repository('.', shared=True, format=format)
314
repo = self.applyDeprecated(zero_ninetyone,
315
bzrdir.BzrDir.create_repository,
316
self.get_url('child'),
318
self.assertTrue(isinstance(repo, repository.Repository))
319
self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
321
def test_create_branch_and_repo_uses_default(self):
322
format = SampleBzrDirFormat()
323
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
325
self.assertTrue(isinstance(branch, SampleBranch))
327
def test_create_branch_and_repo_under_shared(self):
328
# creating a branch and repo in a shared repo uses the
330
format = bzrdir.format_registry.make_bzrdir('knit')
331
self.make_repository('.', shared=True, format=format)
332
branch = bzrdir.BzrDir.create_branch_and_repo(
333
self.get_url('child'), format=format)
334
self.assertRaises(errors.NoRepositoryPresent,
335
branch.bzrdir.open_repository)
337
def test_create_branch_and_repo_under_shared_force_new(self):
338
# creating a branch and repo in a shared repo can be forced to
340
format = bzrdir.format_registry.make_bzrdir('knit')
341
self.make_repository('.', shared=True, format=format)
342
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
345
branch.bzrdir.open_repository()
347
def test_create_standalone_working_tree(self):
348
format = SampleBzrDirFormat()
349
# note this is deliberately readonly, as this failure should
350
# occur before any writes.
351
self.assertRaises(errors.NotLocalUrl,
352
bzrdir.BzrDir.create_standalone_workingtree,
353
self.get_readonly_url(), format=format)
354
tree = bzrdir.BzrDir.create_standalone_workingtree('.',
356
self.assertEqual('A tree', tree)
358
def test_create_standalone_working_tree_under_shared_repo(self):
359
# create standalone working tree always makes a repo.
360
format = bzrdir.format_registry.make_bzrdir('knit')
361
self.make_repository('.', shared=True, format=format)
362
# note this is deliberately readonly, as this failure should
363
# occur before any writes.
364
self.assertRaises(errors.NotLocalUrl,
365
bzrdir.BzrDir.create_standalone_workingtree,
366
self.get_readonly_url('child'), format=format)
367
tree = bzrdir.BzrDir.create_standalone_workingtree('child',
369
tree.bzrdir.open_repository()
371
def test_create_branch_convenience(self):
372
# outside a repo the default convenience output is a repo+branch_tree
373
format = bzrdir.format_registry.make_bzrdir('knit')
374
branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
375
branch.bzrdir.open_workingtree()
376
branch.bzrdir.open_repository()
378
def test_create_branch_convenience_possible_transports(self):
379
"""Check that the optional 'possible_transports' is recognized"""
380
format = bzrdir.format_registry.make_bzrdir('knit')
381
t = self.get_transport()
382
branch = bzrdir.BzrDir.create_branch_convenience(
383
'.', format=format, possible_transports=[t])
384
branch.bzrdir.open_workingtree()
385
branch.bzrdir.open_repository()
387
def test_create_branch_convenience_root(self):
388
"""Creating a branch at the root of a fs should work."""
389
self.vfs_transport_factory = MemoryServer
390
# outside a repo the default convenience output is a repo+branch_tree
391
format = bzrdir.format_registry.make_bzrdir('knit')
392
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
394
self.assertRaises(errors.NoWorkingTree,
395
branch.bzrdir.open_workingtree)
396
branch.bzrdir.open_repository()
398
def test_create_branch_convenience_under_shared_repo(self):
399
# inside a repo the default convenience output is a branch+ follow the
401
format = bzrdir.format_registry.make_bzrdir('knit')
402
self.make_repository('.', shared=True, format=format)
403
branch = bzrdir.BzrDir.create_branch_convenience('child',
405
branch.bzrdir.open_workingtree()
406
self.assertRaises(errors.NoRepositoryPresent,
407
branch.bzrdir.open_repository)
409
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
410
# inside a repo the default convenience output is a branch+ follow the
411
# repo tree policy but we can override that
412
format = bzrdir.format_registry.make_bzrdir('knit')
413
self.make_repository('.', shared=True, format=format)
414
branch = bzrdir.BzrDir.create_branch_convenience('child',
415
force_new_tree=False, format=format)
416
self.assertRaises(errors.NoWorkingTree,
417
branch.bzrdir.open_workingtree)
418
self.assertRaises(errors.NoRepositoryPresent,
419
branch.bzrdir.open_repository)
421
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
422
# inside a repo the default convenience output is a branch+ follow the
424
format = bzrdir.format_registry.make_bzrdir('knit')
425
repo = self.make_repository('.', shared=True, format=format)
426
repo.set_make_working_trees(False)
427
branch = bzrdir.BzrDir.create_branch_convenience('child',
429
self.assertRaises(errors.NoWorkingTree,
430
branch.bzrdir.open_workingtree)
431
self.assertRaises(errors.NoRepositoryPresent,
432
branch.bzrdir.open_repository)
434
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
435
# inside a repo the default convenience output is a branch+ follow the
436
# repo tree policy but we can override that
437
format = bzrdir.format_registry.make_bzrdir('knit')
438
repo = self.make_repository('.', shared=True, format=format)
439
repo.set_make_working_trees(False)
440
branch = bzrdir.BzrDir.create_branch_convenience('child',
441
force_new_tree=True, format=format)
442
branch.bzrdir.open_workingtree()
443
self.assertRaises(errors.NoRepositoryPresent,
444
branch.bzrdir.open_repository)
446
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
447
# inside a repo the default convenience output is overridable to give
449
format = bzrdir.format_registry.make_bzrdir('knit')
450
self.make_repository('.', shared=True, format=format)
451
branch = bzrdir.BzrDir.create_branch_convenience('child',
452
force_new_repo=True, format=format)
453
branch.bzrdir.open_repository()
454
branch.bzrdir.open_workingtree()
457
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
459
def test_acquire_repository_standalone(self):
460
"""The default acquisition policy should create a standalone branch."""
461
my_bzrdir = self.make_bzrdir('.')
462
repo_policy = my_bzrdir.determine_repository_policy()
463
repo = repo_policy.acquire_repository()
464
self.assertEqual(repo.bzrdir.root_transport.base,
465
my_bzrdir.root_transport.base)
466
self.assertFalse(repo.is_shared())
469
class ChrootedTests(TestCaseWithTransport):
470
"""A support class that provides readonly urls outside the local namespace.
472
This is done by checking if self.transport_server is a MemoryServer. if it
473
is then we are chrooted already, if it is not then an HttpServer is used
478
super(ChrootedTests, self).setUp()
479
if not self.vfs_transport_factory == MemoryServer:
480
self.transport_readonly_server = HttpServer
482
def test_open_containing(self):
483
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
484
self.get_readonly_url(''))
485
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
486
self.get_readonly_url('g/p/q'))
487
control = bzrdir.BzrDir.create(self.get_url())
488
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
489
self.assertEqual('', relpath)
490
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
491
self.assertEqual('g/p/q', relpath)
493
def test_open_containing_from_transport(self):
494
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
495
get_transport(self.get_readonly_url('')))
496
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
497
get_transport(self.get_readonly_url('g/p/q')))
498
control = bzrdir.BzrDir.create(self.get_url())
499
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
500
get_transport(self.get_readonly_url('')))
501
self.assertEqual('', relpath)
502
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
503
get_transport(self.get_readonly_url('g/p/q')))
504
self.assertEqual('g/p/q', relpath)
506
def test_open_containing_tree_or_branch(self):
507
def local_branch_path(branch):
508
return os.path.realpath(
509
urlutils.local_path_from_url(branch.base))
511
self.make_branch_and_tree('topdir')
512
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
514
self.assertEqual(os.path.realpath('topdir'),
515
os.path.realpath(tree.basedir))
516
self.assertEqual(os.path.realpath('topdir'),
517
local_branch_path(branch))
518
self.assertIs(tree.bzrdir, branch.bzrdir)
519
self.assertEqual('foo', relpath)
520
# opening from non-local should not return the tree
521
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
522
self.get_readonly_url('topdir/foo'))
523
self.assertEqual(None, tree)
524
self.assertEqual('foo', relpath)
526
self.make_branch('topdir/foo')
527
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
529
self.assertIs(tree, None)
530
self.assertEqual(os.path.realpath('topdir/foo'),
531
local_branch_path(branch))
532
self.assertEqual('', relpath)
534
def test_open_tree_or_branch(self):
535
def local_branch_path(branch):
536
return os.path.realpath(
537
urlutils.local_path_from_url(branch.base))
539
self.make_branch_and_tree('topdir')
540
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
541
self.assertEqual(os.path.realpath('topdir'),
542
os.path.realpath(tree.basedir))
543
self.assertEqual(os.path.realpath('topdir'),
544
local_branch_path(branch))
545
self.assertIs(tree.bzrdir, branch.bzrdir)
546
# opening from non-local should not return the tree
547
tree, branch = bzrdir.BzrDir.open_tree_or_branch(
548
self.get_readonly_url('topdir'))
549
self.assertEqual(None, tree)
551
self.make_branch('topdir/foo')
552
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
553
self.assertIs(tree, None)
554
self.assertEqual(os.path.realpath('topdir/foo'),
555
local_branch_path(branch))
557
def test_open_from_transport(self):
558
# transport pointing at bzrdir should give a bzrdir with root transport
559
# set to the given transport
560
control = bzrdir.BzrDir.create(self.get_url())
561
transport = get_transport(self.get_url())
562
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
563
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
564
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
566
def test_open_from_transport_no_bzrdir(self):
567
transport = get_transport(self.get_url())
568
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
571
def test_open_from_transport_bzrdir_in_parent(self):
572
control = bzrdir.BzrDir.create(self.get_url())
573
transport = get_transport(self.get_url())
574
transport.mkdir('subdir')
575
transport = transport.clone('subdir')
576
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
579
def test_sprout_recursive(self):
580
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
581
sub_tree = self.make_branch_and_tree('tree1/subtree',
582
format='dirstate-with-subtree')
583
tree.add_reference(sub_tree)
584
self.build_tree(['tree1/subtree/file'])
586
tree.commit('Initial commit')
587
tree.bzrdir.sprout('tree2')
588
self.failUnlessExists('tree2/subtree/file')
590
def test_cloning_metadir(self):
591
"""Ensure that cloning metadir is suitable"""
592
bzrdir = self.make_bzrdir('bzrdir')
593
bzrdir.cloning_metadir()
594
branch = self.make_branch('branch', format='knit')
595
format = branch.bzrdir.cloning_metadir()
596
self.assertIsInstance(format.workingtree_format,
597
workingtree.WorkingTreeFormat3)
599
def test_sprout_recursive_treeless(self):
600
tree = self.make_branch_and_tree('tree1',
601
format='dirstate-with-subtree')
602
sub_tree = self.make_branch_and_tree('tree1/subtree',
603
format='dirstate-with-subtree')
604
tree.add_reference(sub_tree)
605
self.build_tree(['tree1/subtree/file'])
607
tree.commit('Initial commit')
608
tree.bzrdir.destroy_workingtree()
609
repo = self.make_repository('repo', shared=True,
610
format='dirstate-with-subtree')
611
repo.set_make_working_trees(False)
612
tree.bzrdir.sprout('repo/tree2')
613
self.failUnlessExists('repo/tree2/subtree')
614
self.failIfExists('repo/tree2/subtree/file')
616
def make_foo_bar_baz(self):
617
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
618
bar = self.make_branch('foo/bar').bzrdir
619
baz = self.make_branch('baz').bzrdir
622
def test_find_bzrdirs(self):
623
foo, bar, baz = self.make_foo_bar_baz()
624
transport = get_transport(self.get_url())
625
self.assertEqualBzrdirs([baz, foo, bar],
626
bzrdir.BzrDir.find_bzrdirs(transport))
628
def test_find_bzrdirs_list_current(self):
629
def list_current(transport):
630
return [s for s in transport.list_dir('') if s != 'baz']
632
foo, bar, baz = self.make_foo_bar_baz()
633
transport = get_transport(self.get_url())
634
self.assertEqualBzrdirs([foo, bar],
635
bzrdir.BzrDir.find_bzrdirs(transport,
636
list_current=list_current))
639
def test_find_bzrdirs_evaluate(self):
640
def evaluate(bzrdir):
642
repo = bzrdir.open_repository()
643
except NoRepositoryPresent:
644
return True, bzrdir.root_transport.base
646
return False, bzrdir.root_transport.base
648
foo, bar, baz = self.make_foo_bar_baz()
649
transport = get_transport(self.get_url())
650
self.assertEqual([baz.root_transport.base, foo.root_transport.base],
651
list(bzrdir.BzrDir.find_bzrdirs(transport,
654
def assertEqualBzrdirs(self, first, second):
656
second = list(second)
657
self.assertEqual(len(first), len(second))
658
for x, y in zip(first, second):
659
self.assertEqual(x.root_transport.base, y.root_transport.base)
661
def test_find_branches(self):
662
root = self.make_repository('', shared=True)
663
foo, bar, baz = self.make_foo_bar_baz()
664
qux = self.make_bzrdir('foo/qux')
665
transport = get_transport(self.get_url())
666
branches = bzrdir.BzrDir.find_branches(transport)
667
self.assertEqual(baz.root_transport.base, branches[0].base)
668
self.assertEqual(foo.root_transport.base, branches[1].base)
669
self.assertEqual(bar.root_transport.base, branches[2].base)
671
# ensure this works without a top-level repo
672
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
673
self.assertEqual(foo.root_transport.base, branches[0].base)
674
self.assertEqual(bar.root_transport.base, branches[1].base)
677
class TestMeta1DirFormat(TestCaseWithTransport):
678
"""Tests specific to the meta1 dir format."""
680
def test_right_base_dirs(self):
681
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
683
branch_base = t.clone('branch').base
684
self.assertEqual(branch_base, dir.get_branch_transport(None).base)
685
self.assertEqual(branch_base,
686
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
687
repository_base = t.clone('repository').base
688
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
689
self.assertEqual(repository_base,
690
dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
691
checkout_base = t.clone('checkout').base
692
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
693
self.assertEqual(checkout_base,
694
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
696
def test_meta1dir_uses_lockdir(self):
697
"""Meta1 format uses a LockDir to guard the whole directory, not a file."""
698
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
700
self.assertIsDirectory('branch-lock', t)
702
def test_comparison(self):
703
"""Equality and inequality behave properly.
705
Metadirs should compare equal iff they have the same repo, branch and
708
mydir = bzrdir.format_registry.make_bzrdir('knit')
709
self.assertEqual(mydir, mydir)
710
self.assertFalse(mydir != mydir)
711
otherdir = bzrdir.format_registry.make_bzrdir('knit')
712
self.assertEqual(otherdir, mydir)
713
self.assertFalse(otherdir != mydir)
714
otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
715
self.assertNotEqual(otherdir2, mydir)
716
self.assertFalse(otherdir2 == mydir)
718
def test_needs_conversion_different_working_tree(self):
719
# meta1dirs need an conversion if any element is not the default.
720
old_format = bzrdir.BzrDirFormat.get_default_format()
722
new_default = bzrdir.format_registry.make_bzrdir('dirstate')
723
bzrdir.BzrDirFormat._set_default_format(new_default)
725
tree = self.make_branch_and_tree('tree', format='knit')
726
self.assertTrue(tree.bzrdir.needs_format_conversion())
728
bzrdir.BzrDirFormat._set_default_format(old_format)
731
class TestFormat5(TestCaseWithTransport):
732
"""Tests specific to the version 5 bzrdir format."""
734
def test_same_lockfiles_between_tree_repo_branch(self):
735
# this checks that only a single lockfiles instance is created
736
# for format 5 objects
737
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
738
def check_dir_components_use_same_lock(dir):
739
ctrl_1 = dir.open_repository().control_files
740
ctrl_2 = dir.open_branch().control_files
741
ctrl_3 = dir.open_workingtree()._control_files
742
self.assertTrue(ctrl_1 is ctrl_2)
743
self.assertTrue(ctrl_2 is ctrl_3)
744
check_dir_components_use_same_lock(dir)
745
# and if we open it normally.
746
dir = bzrdir.BzrDir.open(self.get_url())
747
check_dir_components_use_same_lock(dir)
749
def test_can_convert(self):
750
# format 5 dirs are convertable
751
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
752
self.assertTrue(dir.can_convert_format())
754
def test_needs_conversion(self):
755
# format 5 dirs need a conversion if they are not the default.
756
# and they start of not the default.
757
old_format = bzrdir.BzrDirFormat.get_default_format()
758
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
760
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
761
self.assertFalse(dir.needs_format_conversion())
763
bzrdir.BzrDirFormat._set_default_format(old_format)
764
self.assertTrue(dir.needs_format_conversion())
767
class TestFormat6(TestCaseWithTransport):
768
"""Tests specific to the version 6 bzrdir format."""
770
def test_same_lockfiles_between_tree_repo_branch(self):
771
# this checks that only a single lockfiles instance is created
772
# for format 6 objects
773
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
774
def check_dir_components_use_same_lock(dir):
775
ctrl_1 = dir.open_repository().control_files
776
ctrl_2 = dir.open_branch().control_files
777
ctrl_3 = dir.open_workingtree()._control_files
778
self.assertTrue(ctrl_1 is ctrl_2)
779
self.assertTrue(ctrl_2 is ctrl_3)
780
check_dir_components_use_same_lock(dir)
781
# and if we open it normally.
782
dir = bzrdir.BzrDir.open(self.get_url())
783
check_dir_components_use_same_lock(dir)
785
def test_can_convert(self):
786
# format 6 dirs are convertable
787
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
788
self.assertTrue(dir.can_convert_format())
790
def test_needs_conversion(self):
791
# format 6 dirs need an conversion if they are not the default.
792
old_format = bzrdir.BzrDirFormat.get_default_format()
793
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
795
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
796
self.assertTrue(dir.needs_format_conversion())
798
bzrdir.BzrDirFormat._set_default_format(old_format)
801
class NotBzrDir(bzrlib.bzrdir.BzrDir):
802
"""A non .bzr based control directory."""
804
def __init__(self, transport, format):
805
self._format = format
806
self.root_transport = transport
807
self.transport = transport.clone('.not')
810
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
811
"""A test class representing any non-.bzr based disk format."""
813
def initialize_on_transport(self, transport):
814
"""Initialize a new .not dir in the base directory of a Transport."""
815
transport.mkdir('.not')
816
return self.open(transport)
818
def open(self, transport):
819
"""Open this directory."""
820
return NotBzrDir(transport, self)
823
def _known_formats(self):
824
return set([NotBzrDirFormat()])
827
def probe_transport(self, transport):
828
"""Our format is present if the transport ends in '.not/'."""
829
if transport.has('.not'):
830
return NotBzrDirFormat()
833
class TestNotBzrDir(TestCaseWithTransport):
834
"""Tests for using the bzrdir api with a non .bzr based disk format.
836
If/when one of these is in the core, we can let the implementation tests
840
def test_create_and_find_format(self):
841
# create a .notbzr dir
842
format = NotBzrDirFormat()
843
dir = format.initialize(self.get_url())
844
self.assertIsInstance(dir, NotBzrDir)
846
bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
848
found = bzrlib.bzrdir.BzrDirFormat.find_format(
849
get_transport(self.get_url()))
850
self.assertIsInstance(found, NotBzrDirFormat)
852
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
854
def test_included_in_known_formats(self):
855
bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
857
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
858
for format in formats:
859
if isinstance(format, NotBzrDirFormat):
861
self.fail("No NotBzrDirFormat in %s" % formats)
863
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
866
class NonLocalTests(TestCaseWithTransport):
867
"""Tests for bzrdir static behaviour on non local paths."""
870
super(NonLocalTests, self).setUp()
871
self.vfs_transport_factory = MemoryServer
873
def test_create_branch_convenience(self):
874
# outside a repo the default convenience output is a repo+branch_tree
875
format = bzrdir.format_registry.make_bzrdir('knit')
876
branch = bzrdir.BzrDir.create_branch_convenience(
877
self.get_url('foo'), format=format)
878
self.assertRaises(errors.NoWorkingTree,
879
branch.bzrdir.open_workingtree)
880
branch.bzrdir.open_repository()
882
def test_create_branch_convenience_force_tree_not_local_fails(self):
883
# outside a repo the default convenience output is a repo+branch_tree
884
format = bzrdir.format_registry.make_bzrdir('knit')
885
self.assertRaises(errors.NotLocalUrl,
886
bzrdir.BzrDir.create_branch_convenience,
890
t = get_transport(self.get_url('.'))
891
self.assertFalse(t.has('foo'))
893
def test_clone(self):
894
# clone into a nonlocal path works
895
format = bzrdir.format_registry.make_bzrdir('knit')
896
branch = bzrdir.BzrDir.create_branch_convenience('local',
898
branch.bzrdir.open_workingtree()
899
result = branch.bzrdir.clone(self.get_url('remote'))
900
self.assertRaises(errors.NoWorkingTree,
901
result.open_workingtree)
903
result.open_repository()
905
def test_checkout_metadir(self):
906
# checkout_metadir has reasonable working tree format even when no
907
# working tree is present
908
self.make_branch('branch-knit2', format='dirstate-with-subtree')
909
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
910
checkout_format = my_bzrdir.checkout_metadir()
911
self.assertIsInstance(checkout_format.workingtree_format,
912
workingtree.WorkingTreeFormat3)
915
class TestHTTPRedirectionLoop(object):
916
"""Test redirection loop between two http servers.
918
This MUST be used by daughter classes that also inherit from
919
TestCaseWithTwoWebservers.
921
We can't inherit directly from TestCaseWithTwoWebservers or the
922
test framework will try to create an instance which cannot
923
run, its implementation being incomplete.
926
# Should be defined by daughter classes to ensure redirection
927
# still use the same transport implementation (not currently
928
# enforced as it's a bit tricky to get right (see the FIXME
929
# in BzrDir.open_from_transport for the unique use case so
933
def create_transport_readonly_server(self):
934
return HTTPServerRedirecting()
936
def create_transport_secondary_server(self):
937
return HTTPServerRedirecting()
940
# Both servers redirect to each server creating a loop
941
super(TestHTTPRedirectionLoop, self).setUp()
942
# The redirections will point to the new server
943
self.new_server = self.get_readonly_server()
944
# The requests to the old server will be redirected
945
self.old_server = self.get_secondary_server()
946
# Configure the redirections
947
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
948
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
950
def _qualified_url(self, host, port):
951
return 'http+%s://%s:%s' % (self._qualifier, host, port)
954
# Starting from either server should loop
955
old_url = self._qualified_url(self.old_server.host,
956
self.old_server.port)
957
oldt = self._transport(old_url)
958
self.assertRaises(errors.NotBranchError,
959
bzrdir.BzrDir.open_from_transport, oldt)
960
new_url = self._qualified_url(self.new_server.host,
961
self.new_server.port)
962
newt = self._transport(new_url)
963
self.assertRaises(errors.NotBranchError,
964
bzrdir.BzrDir.open_from_transport, newt)
967
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
968
TestCaseWithTwoWebservers):
969
"""Tests redirections for urllib implementation"""
971
_qualifier = 'urllib'
972
_transport = HttpTransport_urllib
976
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
977
TestHTTPRedirectionLoop,
978
TestCaseWithTwoWebservers):
979
"""Tests redirections for pycurl implementation"""
981
_qualifier = 'pycurl'
984
class TestDotBzrHidden(TestCaseWithTransport):
987
if sys.platform == 'win32':
988
ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
991
f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
992
stderr=subprocess.PIPE)
993
out, err = f.communicate()
994
self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
996
return out.splitlines()
998
def test_dot_bzr_hidden(self):
999
if sys.platform == 'win32' and not win32utils.has_win32file:
1000
raise TestSkipped('unable to make file hidden without pywin32 library')
1001
b = bzrdir.BzrDir.create('.')
1002
self.build_tree(['a'])
1003
self.assertEquals(['a'], self.get_ls())
1005
def test_dot_bzr_hidden_with_url(self):
1006
if sys.platform == 'win32' and not win32utils.has_win32file:
1007
raise TestSkipped('unable to make file hidden without pywin32 library')
1008
b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1009
self.build_tree(['a'])
1010
self.assertEquals(['a'], self.get_ls())