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 ChrootedTests(TestCaseWithTransport):
458
"""A support class that provides readonly urls outside the local namespace.
460
This is done by checking if self.transport_server is a MemoryServer. if it
461
is then we are chrooted already, if it is not then an HttpServer is used
466
super(ChrootedTests, self).setUp()
467
if not self.vfs_transport_factory == MemoryServer:
468
self.transport_readonly_server = HttpServer
470
def test_open_containing(self):
471
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
472
self.get_readonly_url(''))
473
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
474
self.get_readonly_url('g/p/q'))
475
control = bzrdir.BzrDir.create(self.get_url())
476
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
477
self.assertEqual('', relpath)
478
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
479
self.assertEqual('g/p/q', relpath)
481
def test_open_containing_from_transport(self):
482
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
483
get_transport(self.get_readonly_url('')))
484
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
485
get_transport(self.get_readonly_url('g/p/q')))
486
control = bzrdir.BzrDir.create(self.get_url())
487
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
488
get_transport(self.get_readonly_url('')))
489
self.assertEqual('', relpath)
490
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
491
get_transport(self.get_readonly_url('g/p/q')))
492
self.assertEqual('g/p/q', relpath)
494
def test_open_containing_tree_or_branch(self):
495
def local_branch_path(branch):
496
return os.path.realpath(
497
urlutils.local_path_from_url(branch.base))
499
self.make_branch_and_tree('topdir')
500
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
502
self.assertEqual(os.path.realpath('topdir'),
503
os.path.realpath(tree.basedir))
504
self.assertEqual(os.path.realpath('topdir'),
505
local_branch_path(branch))
506
self.assertIs(tree.bzrdir, branch.bzrdir)
507
self.assertEqual('foo', relpath)
508
# opening from non-local should not return the tree
509
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
510
self.get_readonly_url('topdir/foo'))
511
self.assertEqual(None, tree)
512
self.assertEqual('foo', relpath)
514
self.make_branch('topdir/foo')
515
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
517
self.assertIs(tree, None)
518
self.assertEqual(os.path.realpath('topdir/foo'),
519
local_branch_path(branch))
520
self.assertEqual('', relpath)
522
def test_open_tree_or_branch(self):
523
def local_branch_path(branch):
524
return os.path.realpath(
525
urlutils.local_path_from_url(branch.base))
527
self.make_branch_and_tree('topdir')
528
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
529
self.assertEqual(os.path.realpath('topdir'),
530
os.path.realpath(tree.basedir))
531
self.assertEqual(os.path.realpath('topdir'),
532
local_branch_path(branch))
533
self.assertIs(tree.bzrdir, branch.bzrdir)
534
# opening from non-local should not return the tree
535
tree, branch = bzrdir.BzrDir.open_tree_or_branch(
536
self.get_readonly_url('topdir'))
537
self.assertEqual(None, tree)
539
self.make_branch('topdir/foo')
540
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
541
self.assertIs(tree, None)
542
self.assertEqual(os.path.realpath('topdir/foo'),
543
local_branch_path(branch))
545
def test_open_from_transport(self):
546
# transport pointing at bzrdir should give a bzrdir with root transport
547
# set to the given transport
548
control = bzrdir.BzrDir.create(self.get_url())
549
transport = get_transport(self.get_url())
550
opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
551
self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
552
self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
554
def test_open_from_transport_no_bzrdir(self):
555
transport = get_transport(self.get_url())
556
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
559
def test_open_from_transport_bzrdir_in_parent(self):
560
control = bzrdir.BzrDir.create(self.get_url())
561
transport = get_transport(self.get_url())
562
transport.mkdir('subdir')
563
transport = transport.clone('subdir')
564
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
567
def test_sprout_recursive(self):
568
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
569
sub_tree = self.make_branch_and_tree('tree1/subtree',
570
format='dirstate-with-subtree')
571
tree.add_reference(sub_tree)
572
self.build_tree(['tree1/subtree/file'])
574
tree.commit('Initial commit')
575
tree.bzrdir.sprout('tree2')
576
self.failUnlessExists('tree2/subtree/file')
578
def test_cloning_metadir(self):
579
"""Ensure that cloning metadir is suitable"""
580
bzrdir = self.make_bzrdir('bzrdir')
581
bzrdir.cloning_metadir()
582
branch = self.make_branch('branch', format='knit')
583
format = branch.bzrdir.cloning_metadir()
584
self.assertIsInstance(format.workingtree_format,
585
workingtree.WorkingTreeFormat3)
587
def test_sprout_recursive_treeless(self):
588
tree = self.make_branch_and_tree('tree1',
589
format='dirstate-with-subtree')
590
sub_tree = self.make_branch_and_tree('tree1/subtree',
591
format='dirstate-with-subtree')
592
tree.add_reference(sub_tree)
593
self.build_tree(['tree1/subtree/file'])
595
tree.commit('Initial commit')
596
tree.bzrdir.destroy_workingtree()
597
repo = self.make_repository('repo', shared=True,
598
format='dirstate-with-subtree')
599
repo.set_make_working_trees(False)
600
tree.bzrdir.sprout('repo/tree2')
601
self.failUnlessExists('repo/tree2/subtree')
602
self.failIfExists('repo/tree2/subtree/file')
604
def make_foo_bar_baz(self):
605
foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
606
bar = self.make_branch('foo/bar').bzrdir
607
baz = self.make_branch('baz').bzrdir
610
def test_find_bzrdirs(self):
611
foo, bar, baz = self.make_foo_bar_baz()
612
transport = get_transport(self.get_url())
613
self.assertEqualBzrdirs([baz, foo, bar],
614
bzrdir.BzrDir.find_bzrdirs(transport))
616
def test_find_bzrdirs_list_current(self):
617
def list_current(transport):
618
return [s for s in transport.list_dir('') if s != 'baz']
620
foo, bar, baz = self.make_foo_bar_baz()
621
transport = get_transport(self.get_url())
622
self.assertEqualBzrdirs([foo, bar],
623
bzrdir.BzrDir.find_bzrdirs(transport,
624
list_current=list_current))
627
def test_find_bzrdirs_evaluate(self):
628
def evaluate(bzrdir):
630
repo = bzrdir.open_repository()
631
except NoRepositoryPresent:
632
return True, bzrdir.root_transport.base
634
return False, bzrdir.root_transport.base
636
foo, bar, baz = self.make_foo_bar_baz()
637
transport = get_transport(self.get_url())
638
self.assertEqual([baz.root_transport.base, foo.root_transport.base],
639
list(bzrdir.BzrDir.find_bzrdirs(transport,
642
def assertEqualBzrdirs(self, first, second):
644
second = list(second)
645
self.assertEqual(len(first), len(second))
646
for x, y in zip(first, second):
647
self.assertEqual(x.root_transport.base, y.root_transport.base)
649
def test_find_branches(self):
650
root = self.make_repository('', shared=True)
651
foo, bar, baz = self.make_foo_bar_baz()
652
qux = self.make_bzrdir('foo/qux')
653
transport = get_transport(self.get_url())
654
branches = bzrdir.BzrDir.find_branches(transport)
655
self.assertEqual(baz.root_transport.base, branches[0].base)
656
self.assertEqual(foo.root_transport.base, branches[1].base)
657
self.assertEqual(bar.root_transport.base, branches[2].base)
659
# ensure this works without a top-level repo
660
branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
661
self.assertEqual(foo.root_transport.base, branches[0].base)
662
self.assertEqual(bar.root_transport.base, branches[1].base)
665
class TestMeta1DirFormat(TestCaseWithTransport):
666
"""Tests specific to the meta1 dir format."""
668
def test_right_base_dirs(self):
669
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
671
branch_base = t.clone('branch').base
672
self.assertEqual(branch_base, dir.get_branch_transport(None).base)
673
self.assertEqual(branch_base,
674
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
675
repository_base = t.clone('repository').base
676
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
677
self.assertEqual(repository_base,
678
dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
679
checkout_base = t.clone('checkout').base
680
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
681
self.assertEqual(checkout_base,
682
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
684
def test_meta1dir_uses_lockdir(self):
685
"""Meta1 format uses a LockDir to guard the whole directory, not a file."""
686
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
688
self.assertIsDirectory('branch-lock', t)
690
def test_comparison(self):
691
"""Equality and inequality behave properly.
693
Metadirs should compare equal iff they have the same repo, branch and
696
mydir = bzrdir.format_registry.make_bzrdir('knit')
697
self.assertEqual(mydir, mydir)
698
self.assertFalse(mydir != mydir)
699
otherdir = bzrdir.format_registry.make_bzrdir('knit')
700
self.assertEqual(otherdir, mydir)
701
self.assertFalse(otherdir != mydir)
702
otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
703
self.assertNotEqual(otherdir2, mydir)
704
self.assertFalse(otherdir2 == mydir)
706
def test_needs_conversion_different_working_tree(self):
707
# meta1dirs need an conversion if any element is not the default.
708
old_format = bzrdir.BzrDirFormat.get_default_format()
710
new_default = bzrdir.format_registry.make_bzrdir('dirstate')
711
bzrdir.BzrDirFormat._set_default_format(new_default)
713
tree = self.make_branch_and_tree('tree', format='knit')
714
self.assertTrue(tree.bzrdir.needs_format_conversion())
716
bzrdir.BzrDirFormat._set_default_format(old_format)
719
class TestFormat5(TestCaseWithTransport):
720
"""Tests specific to the version 5 bzrdir format."""
722
def test_same_lockfiles_between_tree_repo_branch(self):
723
# this checks that only a single lockfiles instance is created
724
# for format 5 objects
725
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
726
def check_dir_components_use_same_lock(dir):
727
ctrl_1 = dir.open_repository().control_files
728
ctrl_2 = dir.open_branch().control_files
729
ctrl_3 = dir.open_workingtree()._control_files
730
self.assertTrue(ctrl_1 is ctrl_2)
731
self.assertTrue(ctrl_2 is ctrl_3)
732
check_dir_components_use_same_lock(dir)
733
# and if we open it normally.
734
dir = bzrdir.BzrDir.open(self.get_url())
735
check_dir_components_use_same_lock(dir)
737
def test_can_convert(self):
738
# format 5 dirs are convertable
739
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
740
self.assertTrue(dir.can_convert_format())
742
def test_needs_conversion(self):
743
# format 5 dirs need a conversion if they are not the default.
744
# and they start of not the default.
745
old_format = bzrdir.BzrDirFormat.get_default_format()
746
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
748
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
749
self.assertFalse(dir.needs_format_conversion())
751
bzrdir.BzrDirFormat._set_default_format(old_format)
752
self.assertTrue(dir.needs_format_conversion())
755
class TestFormat6(TestCaseWithTransport):
756
"""Tests specific to the version 6 bzrdir format."""
758
def test_same_lockfiles_between_tree_repo_branch(self):
759
# this checks that only a single lockfiles instance is created
760
# for format 6 objects
761
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
762
def check_dir_components_use_same_lock(dir):
763
ctrl_1 = dir.open_repository().control_files
764
ctrl_2 = dir.open_branch().control_files
765
ctrl_3 = dir.open_workingtree()._control_files
766
self.assertTrue(ctrl_1 is ctrl_2)
767
self.assertTrue(ctrl_2 is ctrl_3)
768
check_dir_components_use_same_lock(dir)
769
# and if we open it normally.
770
dir = bzrdir.BzrDir.open(self.get_url())
771
check_dir_components_use_same_lock(dir)
773
def test_can_convert(self):
774
# format 6 dirs are convertable
775
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
776
self.assertTrue(dir.can_convert_format())
778
def test_needs_conversion(self):
779
# format 6 dirs need an conversion if they are not the default.
780
old_format = bzrdir.BzrDirFormat.get_default_format()
781
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
783
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
784
self.assertTrue(dir.needs_format_conversion())
786
bzrdir.BzrDirFormat._set_default_format(old_format)
789
class NotBzrDir(bzrlib.bzrdir.BzrDir):
790
"""A non .bzr based control directory."""
792
def __init__(self, transport, format):
793
self._format = format
794
self.root_transport = transport
795
self.transport = transport.clone('.not')
798
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
799
"""A test class representing any non-.bzr based disk format."""
801
def initialize_on_transport(self, transport):
802
"""Initialize a new .not dir in the base directory of a Transport."""
803
transport.mkdir('.not')
804
return self.open(transport)
806
def open(self, transport):
807
"""Open this directory."""
808
return NotBzrDir(transport, self)
811
def _known_formats(self):
812
return set([NotBzrDirFormat()])
815
def probe_transport(self, transport):
816
"""Our format is present if the transport ends in '.not/'."""
817
if transport.has('.not'):
818
return NotBzrDirFormat()
821
class TestNotBzrDir(TestCaseWithTransport):
822
"""Tests for using the bzrdir api with a non .bzr based disk format.
824
If/when one of these is in the core, we can let the implementation tests
828
def test_create_and_find_format(self):
829
# create a .notbzr dir
830
format = NotBzrDirFormat()
831
dir = format.initialize(self.get_url())
832
self.assertIsInstance(dir, NotBzrDir)
834
bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
836
found = bzrlib.bzrdir.BzrDirFormat.find_format(
837
get_transport(self.get_url()))
838
self.assertIsInstance(found, NotBzrDirFormat)
840
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
842
def test_included_in_known_formats(self):
843
bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
845
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
846
for format in formats:
847
if isinstance(format, NotBzrDirFormat):
849
self.fail("No NotBzrDirFormat in %s" % formats)
851
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
854
class NonLocalTests(TestCaseWithTransport):
855
"""Tests for bzrdir static behaviour on non local paths."""
858
super(NonLocalTests, self).setUp()
859
self.vfs_transport_factory = MemoryServer
861
def test_create_branch_convenience(self):
862
# outside a repo the default convenience output is a repo+branch_tree
863
format = bzrdir.format_registry.make_bzrdir('knit')
864
branch = bzrdir.BzrDir.create_branch_convenience(
865
self.get_url('foo'), format=format)
866
self.assertRaises(errors.NoWorkingTree,
867
branch.bzrdir.open_workingtree)
868
branch.bzrdir.open_repository()
870
def test_create_branch_convenience_force_tree_not_local_fails(self):
871
# outside a repo the default convenience output is a repo+branch_tree
872
format = bzrdir.format_registry.make_bzrdir('knit')
873
self.assertRaises(errors.NotLocalUrl,
874
bzrdir.BzrDir.create_branch_convenience,
878
t = get_transport(self.get_url('.'))
879
self.assertFalse(t.has('foo'))
881
def test_clone(self):
882
# clone into a nonlocal path works
883
format = bzrdir.format_registry.make_bzrdir('knit')
884
branch = bzrdir.BzrDir.create_branch_convenience('local',
886
branch.bzrdir.open_workingtree()
887
result = branch.bzrdir.clone(self.get_url('remote'))
888
self.assertRaises(errors.NoWorkingTree,
889
result.open_workingtree)
891
result.open_repository()
893
def test_checkout_metadir(self):
894
# checkout_metadir has reasonable working tree format even when no
895
# working tree is present
896
self.make_branch('branch-knit2', format='dirstate-with-subtree')
897
my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
898
checkout_format = my_bzrdir.checkout_metadir()
899
self.assertIsInstance(checkout_format.workingtree_format,
900
workingtree.WorkingTreeFormat3)
903
class TestHTTPRedirectionLoop(object):
904
"""Test redirection loop between two http servers.
906
This MUST be used by daughter classes that also inherit from
907
TestCaseWithTwoWebservers.
909
We can't inherit directly from TestCaseWithTwoWebservers or the
910
test framework will try to create an instance which cannot
911
run, its implementation being incomplete.
914
# Should be defined by daughter classes to ensure redirection
915
# still use the same transport implementation (not currently
916
# enforced as it's a bit tricky to get right (see the FIXME
917
# in BzrDir.open_from_transport for the unique use case so
921
def create_transport_readonly_server(self):
922
return HTTPServerRedirecting()
924
def create_transport_secondary_server(self):
925
return HTTPServerRedirecting()
928
# Both servers redirect to each server creating a loop
929
super(TestHTTPRedirectionLoop, self).setUp()
930
# The redirections will point to the new server
931
self.new_server = self.get_readonly_server()
932
# The requests to the old server will be redirected
933
self.old_server = self.get_secondary_server()
934
# Configure the redirections
935
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
936
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
938
def _qualified_url(self, host, port):
939
return 'http+%s://%s:%s' % (self._qualifier, host, port)
942
# Starting from either server should loop
943
old_url = self._qualified_url(self.old_server.host,
944
self.old_server.port)
945
oldt = self._transport(old_url)
946
self.assertRaises(errors.NotBranchError,
947
bzrdir.BzrDir.open_from_transport, oldt)
948
new_url = self._qualified_url(self.new_server.host,
949
self.new_server.port)
950
newt = self._transport(new_url)
951
self.assertRaises(errors.NotBranchError,
952
bzrdir.BzrDir.open_from_transport, newt)
955
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
956
TestCaseWithTwoWebservers):
957
"""Tests redirections for urllib implementation"""
959
_qualifier = 'urllib'
960
_transport = HttpTransport_urllib
964
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
965
TestHTTPRedirectionLoop,
966
TestCaseWithTwoWebservers):
967
"""Tests redirections for pycurl implementation"""
969
_qualifier = 'pycurl'
972
class TestDotBzrHidden(TestCaseWithTransport):
975
if sys.platform == 'win32':
976
ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
979
f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
980
stderr=subprocess.PIPE)
981
out, err = f.communicate()
982
self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
984
return out.splitlines()
986
def test_dot_bzr_hidden(self):
987
if sys.platform == 'win32' and not win32utils.has_win32file:
988
raise TestSkipped('unable to make file hidden without pywin32 library')
989
b = bzrdir.BzrDir.create('.')
990
self.build_tree(['a'])
991
self.assertEquals(['a'], self.get_ls())
993
def test_dot_bzr_hidden_with_url(self):
994
if sys.platform == 'win32' and not win32utils.has_win32file:
995
raise TestSkipped('unable to make file hidden without pywin32 library')
996
b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
997
self.build_tree(['a'])
998
self.assertEquals(['a'], self.get_ls())