1
# Copyright (C) 2005, 2006 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.
22
from StringIO import StringIO
25
import bzrlib.bzrdir as bzrdir
26
import bzrlib.errors as errors
27
from bzrlib.errors import (NotBranchError,
29
UnsupportedFormatError,
31
import bzrlib.repository as repository
32
from bzrlib.tests import TestCase, TestCaseWithTransport
33
from bzrlib.transport import get_transport
34
from bzrlib.transport.http import HttpServer
35
from bzrlib.transport.memory import MemoryServer
36
import bzrlib.workingtree as workingtree
39
class TestDefaultFormat(TestCase):
41
def test_get_set_default_format(self):
42
old_format = bzrdir.BzrDirFormat.get_default_format()
43
# default is BzrDirFormat6
44
self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
46
# creating a bzr dir should now create an instrumented dir.
48
result = bzrdir.BzrDir.create('memory:///')
49
self.failUnless(isinstance(result, SampleBzrDir))
51
bzrdir.BzrDirFormat.set_default_format(old_format)
52
self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
55
class SampleBranch(bzrlib.branch.Branch):
56
"""A dummy branch for guess what, dummy use."""
58
def __init__(self, dir):
62
class SampleBzrDir(bzrdir.BzrDir):
63
"""A sample BzrDir implementation to allow testing static methods."""
65
def create_repository(self):
66
"""See BzrDir.create_repository."""
69
def open_repository(self):
70
"""See BzrDir.open_repository."""
73
def create_branch(self):
74
"""See BzrDir.create_branch."""
75
return SampleBranch(self)
77
def create_workingtree(self):
78
"""See BzrDir.create_workingtree."""
82
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
85
this format is initializable, unsupported to aid in testing the
86
open and open_downlevel routines.
89
def get_format_string(self):
90
"""See BzrDirFormat.get_format_string()."""
91
return "Sample .bzr dir format."
93
def initialize(self, url):
94
"""Create a bzr dir."""
95
t = get_transport(url)
97
t.put('.bzr/branch-format', StringIO(self.get_format_string()))
98
return SampleBzrDir(t, self)
100
def is_supported(self):
103
def open(self, transport, _found=None):
104
return "opened branch."
107
class TestBzrDirFormat(TestCaseWithTransport):
108
"""Tests for the BzrDirFormat facility."""
110
def test_find_format(self):
111
# is the right format object found for a branch?
112
# create a branch with a few known format objects.
113
# this is not quite the same as
114
t = get_transport(self.get_url())
115
self.build_tree(["foo/", "bar/"], transport=t)
116
def check_format(format, url):
117
format.initialize(url)
118
t = get_transport(url)
119
found_format = bzrdir.BzrDirFormat.find_format(t)
120
self.failUnless(isinstance(found_format, format.__class__))
121
check_format(bzrdir.BzrDirFormat5(), "foo")
122
check_format(bzrdir.BzrDirFormat6(), "bar")
124
def test_find_format_nothing_there(self):
125
self.assertRaises(NotBranchError,
126
bzrdir.BzrDirFormat.find_format,
129
def test_find_format_unknown_format(self):
130
t = get_transport(self.get_url())
132
t.put('.bzr/branch-format', StringIO())
133
self.assertRaises(UnknownFormatError,
134
bzrdir.BzrDirFormat.find_format,
137
def test_register_unregister_format(self):
138
format = SampleBzrDirFormat()
141
format.initialize(url)
142
# register a format for it.
143
bzrdir.BzrDirFormat.register_format(format)
144
# which bzrdir.Open will refuse (not supported)
145
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
146
# which bzrdir.open_containing will refuse (not supported)
147
self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
148
# but open_downlevel will work
149
t = get_transport(url)
150
self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
151
# unregister the format
152
bzrdir.BzrDirFormat.unregister_format(format)
153
# now open_downlevel should fail too.
154
self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
156
def test_create_repository(self):
157
format = SampleBzrDirFormat()
158
old_format = bzrdir.BzrDirFormat.get_default_format()
159
bzrdir.BzrDirFormat.set_default_format(format)
161
repo = bzrdir.BzrDir.create_repository(self.get_url())
162
self.assertEqual('A repository', repo)
164
bzrdir.BzrDirFormat.set_default_format(old_format)
166
def test_create_repository_under_shared(self):
167
# an explicit create_repository always does so.
168
# we trust the format is right from the 'create_repository test'
169
old_format = bzrdir.BzrDirFormat.get_default_format()
170
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
172
self.make_repository('.', shared=True)
173
repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
174
self.assertTrue(isinstance(repo, repository.Repository))
175
self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
177
bzrdir.BzrDirFormat.set_default_format(old_format)
179
def test_create_branch_and_repo_uses_default(self):
180
format = SampleBzrDirFormat()
181
old_format = bzrdir.BzrDirFormat.get_default_format()
182
bzrdir.BzrDirFormat.set_default_format(format)
184
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
185
self.assertTrue(isinstance(branch, SampleBranch))
187
bzrdir.BzrDirFormat.set_default_format(old_format)
189
def test_create_branch_and_repo_under_shared(self):
190
# creating a branch and repo in a shared repo uses the
192
old_format = bzrdir.BzrDirFormat.get_default_format()
193
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
195
self.make_repository('.', shared=True)
196
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
197
self.assertRaises(errors.NoRepositoryPresent,
198
branch.bzrdir.open_repository)
200
bzrdir.BzrDirFormat.set_default_format(old_format)
202
def test_create_branch_and_repo_under_shared_force_new(self):
203
# creating a branch and repo in a shared repo can be forced to
205
old_format = bzrdir.BzrDirFormat.get_default_format()
206
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
208
self.make_repository('.', shared=True)
209
branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
211
branch.bzrdir.open_repository()
213
bzrdir.BzrDirFormat.set_default_format(old_format)
215
def test_create_standalone_working_tree(self):
216
format = SampleBzrDirFormat()
217
old_format = bzrdir.BzrDirFormat.get_default_format()
218
bzrdir.BzrDirFormat.set_default_format(format)
220
# note this is deliberately readonly, as this failure should
221
# occur before any writes.
222
self.assertRaises(errors.NotLocalUrl,
223
bzrdir.BzrDir.create_standalone_workingtree,
224
self.get_readonly_url())
225
tree = bzrdir.BzrDir.create_standalone_workingtree('.')
226
self.assertEqual('A tree', tree)
228
bzrdir.BzrDirFormat.set_default_format(old_format)
230
def test_create_standalone_working_tree_under_shared_repo(self):
231
# create standalone working tree always makes a repo.
232
old_format = bzrdir.BzrDirFormat.get_default_format()
233
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
235
self.make_repository('.', shared=True)
236
# note this is deliberately readonly, as this failure should
237
# occur before any writes.
238
self.assertRaises(errors.NotLocalUrl,
239
bzrdir.BzrDir.create_standalone_workingtree,
240
self.get_readonly_url('child'))
241
tree = bzrdir.BzrDir.create_standalone_workingtree('child')
242
tree.bzrdir.open_repository()
244
bzrdir.BzrDirFormat.set_default_format(old_format)
246
def test_create_branch_convenience(self):
247
# outside a repo the default convenience output is a repo+branch_tree
248
old_format = bzrdir.BzrDirFormat.get_default_format()
249
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
251
branch = bzrdir.BzrDir.create_branch_convenience('.')
252
branch.bzrdir.open_workingtree()
253
branch.bzrdir.open_repository()
255
bzrdir.BzrDirFormat.set_default_format(old_format)
257
def test_create_branch_convenience_root(self):
258
"""Creating a branch at the root of a fs should work."""
259
self.transport_server = MemoryServer
260
# outside a repo the default convenience output is a repo+branch_tree
261
old_format = bzrdir.BzrDirFormat.get_default_format()
262
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
264
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
265
self.assertRaises(errors.NoWorkingTree,
266
branch.bzrdir.open_workingtree)
267
branch.bzrdir.open_repository()
269
bzrdir.BzrDirFormat.set_default_format(old_format)
271
def test_create_branch_convenience_under_shared_repo(self):
272
# inside a repo the default convenience output is a branch+ follow the
274
old_format = bzrdir.BzrDirFormat.get_default_format()
275
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
277
self.make_repository('.', shared=True)
278
branch = bzrdir.BzrDir.create_branch_convenience('child')
279
branch.bzrdir.open_workingtree()
280
self.assertRaises(errors.NoRepositoryPresent,
281
branch.bzrdir.open_repository)
283
bzrdir.BzrDirFormat.set_default_format(old_format)
285
def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
286
# inside a repo the default convenience output is a branch+ follow the
287
# repo tree policy but we can override that
288
old_format = bzrdir.BzrDirFormat.get_default_format()
289
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
291
self.make_repository('.', shared=True)
292
branch = bzrdir.BzrDir.create_branch_convenience('child',
293
force_new_tree=False)
294
self.assertRaises(errors.NoWorkingTree,
295
branch.bzrdir.open_workingtree)
296
self.assertRaises(errors.NoRepositoryPresent,
297
branch.bzrdir.open_repository)
299
bzrdir.BzrDirFormat.set_default_format(old_format)
301
def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
302
# inside a repo the default convenience output is a branch+ follow the
304
old_format = bzrdir.BzrDirFormat.get_default_format()
305
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
307
repo = self.make_repository('.', shared=True)
308
repo.set_make_working_trees(False)
309
branch = bzrdir.BzrDir.create_branch_convenience('child')
310
self.assertRaises(errors.NoWorkingTree,
311
branch.bzrdir.open_workingtree)
312
self.assertRaises(errors.NoRepositoryPresent,
313
branch.bzrdir.open_repository)
315
bzrdir.BzrDirFormat.set_default_format(old_format)
317
def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
318
# inside a repo the default convenience output is a branch+ follow the
319
# repo tree policy but we can override that
320
old_format = bzrdir.BzrDirFormat.get_default_format()
321
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
323
repo = self.make_repository('.', shared=True)
324
repo.set_make_working_trees(False)
325
branch = bzrdir.BzrDir.create_branch_convenience('child',
327
branch.bzrdir.open_workingtree()
328
self.assertRaises(errors.NoRepositoryPresent,
329
branch.bzrdir.open_repository)
331
bzrdir.BzrDirFormat.set_default_format(old_format)
333
def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
334
# inside a repo the default convenience output is overridable to give
336
old_format = bzrdir.BzrDirFormat.get_default_format()
337
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
339
self.make_repository('.', shared=True)
340
branch = bzrdir.BzrDir.create_branch_convenience('child',
342
branch.bzrdir.open_repository()
343
branch.bzrdir.open_workingtree()
345
bzrdir.BzrDirFormat.set_default_format(old_format)
348
class ChrootedTests(TestCaseWithTransport):
349
"""A support class that provides readonly urls outside the local namespace.
351
This is done by checking if self.transport_server is a MemoryServer. if it
352
is then we are chrooted already, if it is not then an HttpServer is used
357
super(ChrootedTests, self).setUp()
358
if not self.transport_server == MemoryServer:
359
self.transport_readonly_server = HttpServer
361
def test_open_containing(self):
362
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
363
self.get_readonly_url(''))
364
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
365
self.get_readonly_url('g/p/q'))
366
control = bzrdir.BzrDir.create(self.get_url())
367
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
368
self.assertEqual('', relpath)
369
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
370
self.assertEqual('g/p/q', relpath)
372
def test_open_containing_from_transport(self):
373
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
374
get_transport(self.get_readonly_url('')))
375
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
376
get_transport(self.get_readonly_url('g/p/q')))
377
control = bzrdir.BzrDir.create(self.get_url())
378
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
379
get_transport(self.get_readonly_url('')))
380
self.assertEqual('', relpath)
381
branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
382
get_transport(self.get_readonly_url('g/p/q')))
383
self.assertEqual('g/p/q', relpath)
386
class TestMeta1DirFormat(TestCaseWithTransport):
387
"""Tests specific to the meta1 dir format."""
389
def test_right_base_dirs(self):
390
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
392
branch_base = t.clone('branch').base
393
self.assertEqual(branch_base, dir.get_branch_transport(None).base)
394
self.assertEqual(branch_base,
395
dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
396
repository_base = t.clone('repository').base
397
self.assertEqual(repository_base, dir.get_repository_transport(None).base)
398
self.assertEqual(repository_base,
399
dir.get_repository_transport(repository.RepositoryFormat7()).base)
400
checkout_base = t.clone('checkout').base
401
self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
402
self.assertEqual(checkout_base,
403
dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
405
def test_meta1dir_uses_lockdir(self):
406
"""Meta1 format uses a LockDir to guard the whole directory, not a file."""
407
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
409
self.assertIsDirectory('branch-lock', t)
412
class TestFormat5(TestCaseWithTransport):
413
"""Tests specific to the version 5 bzrdir format."""
415
def test_same_lockfiles_between_tree_repo_branch(self):
416
# this checks that only a single lockfiles instance is created
417
# for format 5 objects
418
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
419
def check_dir_components_use_same_lock(dir):
420
ctrl_1 = dir.open_repository().control_files
421
ctrl_2 = dir.open_branch().control_files
422
ctrl_3 = dir.open_workingtree()._control_files
423
self.assertTrue(ctrl_1 is ctrl_2)
424
self.assertTrue(ctrl_2 is ctrl_3)
425
check_dir_components_use_same_lock(dir)
426
# and if we open it normally.
427
dir = bzrdir.BzrDir.open(self.get_url())
428
check_dir_components_use_same_lock(dir)
430
def test_can_convert(self):
431
# format 5 dirs are convertable
432
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
433
self.assertTrue(dir.can_convert_format())
435
def test_needs_conversion(self):
436
# format 5 dirs need a conversion if they are not the default.
437
# and they start of not the default.
438
old_format = bzrdir.BzrDirFormat.get_default_format()
439
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
441
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
442
self.assertFalse(dir.needs_format_conversion())
444
bzrdir.BzrDirFormat.set_default_format(old_format)
445
self.assertTrue(dir.needs_format_conversion())
448
class TestFormat6(TestCaseWithTransport):
449
"""Tests specific to the version 6 bzrdir format."""
451
def test_same_lockfiles_between_tree_repo_branch(self):
452
# this checks that only a single lockfiles instance is created
453
# for format 6 objects
454
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
455
def check_dir_components_use_same_lock(dir):
456
ctrl_1 = dir.open_repository().control_files
457
ctrl_2 = dir.open_branch().control_files
458
ctrl_3 = dir.open_workingtree()._control_files
459
self.assertTrue(ctrl_1 is ctrl_2)
460
self.assertTrue(ctrl_2 is ctrl_3)
461
check_dir_components_use_same_lock(dir)
462
# and if we open it normally.
463
dir = bzrdir.BzrDir.open(self.get_url())
464
check_dir_components_use_same_lock(dir)
466
def test_can_convert(self):
467
# format 6 dirs are convertable
468
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
469
self.assertTrue(dir.can_convert_format())
471
def test_needs_conversion(self):
472
# format 6 dirs need an conversion if they are not the default.
473
old_format = bzrdir.BzrDirFormat.get_default_format()
474
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
476
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
477
self.assertTrue(dir.needs_format_conversion())
479
bzrdir.BzrDirFormat.set_default_format(old_format)
482
class NotBzrDir(bzrlib.bzrdir.BzrDir):
483
"""A non .bzr based control directory."""
485
def __init__(self, transport, format):
486
self._format = format
487
self.root_transport = transport
488
self.transport = transport.clone('.not')
491
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
492
"""A test class representing any non-.bzr based disk format."""
494
def initialize_on_transport(self, transport):
495
"""Initialize a new .not dir in the base directory of a Transport."""
496
transport.mkdir('.not')
497
return self.open(transport)
499
def open(self, transport):
500
"""Open this directory."""
501
return NotBzrDir(transport, self)
504
def _known_formats(self):
505
return set([NotBzrDirFormat()])
508
def probe_transport(self, transport):
509
"""Our format is present if the transport ends in '.not/'."""
510
if transport.has('.not'):
511
return NotBzrDirFormat()
514
class TestNotBzrDir(TestCaseWithTransport):
515
"""Tests for using the bzrdir api with a non .bzr based disk format.
517
If/when one of these is in the core, we can let the implementation tests
521
def test_create_and_find_format(self):
522
# create a .notbzr dir
523
format = NotBzrDirFormat()
524
dir = format.initialize(self.get_url())
525
self.assertIsInstance(dir, NotBzrDir)
527
bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
529
found = bzrlib.bzrdir.BzrDirFormat.find_format(
530
get_transport(self.get_url()))
531
self.assertIsInstance(found, NotBzrDirFormat)
533
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
535
def test_included_in_known_formats(self):
536
bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
538
formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
539
for format in formats:
540
if isinstance(format, NotBzrDirFormat):
542
self.fail("No NotBzrDirFormat in %s" % formats)
544
bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
547
class NonLocalTests(TestCaseWithTransport):
548
"""Tests for bzrdir static behaviour on non local paths."""
551
super(NonLocalTests, self).setUp()
552
self.transport_server = MemoryServer
554
def test_create_branch_convenience(self):
555
# outside a repo the default convenience output is a repo+branch_tree
556
old_format = bzrdir.BzrDirFormat.get_default_format()
557
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
559
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
560
self.assertRaises(errors.NoWorkingTree,
561
branch.bzrdir.open_workingtree)
562
branch.bzrdir.open_repository()
564
bzrdir.BzrDirFormat.set_default_format(old_format)
566
def test_create_branch_convenience_force_tree_not_local_fails(self):
567
# outside a repo the default convenience output is a repo+branch_tree
568
old_format = bzrdir.BzrDirFormat.get_default_format()
569
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
571
self.assertRaises(errors.NotLocalUrl,
572
bzrdir.BzrDir.create_branch_convenience,
575
t = get_transport(self.get_url('.'))
576
self.assertFalse(t.has('foo'))
578
bzrdir.BzrDirFormat.set_default_format(old_format)
580
def test_clone(self):
581
# clone into a nonlocal path works
582
old_format = bzrdir.BzrDirFormat.get_default_format()
583
bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
585
branch = bzrdir.BzrDir.create_branch_convenience('local')
587
bzrdir.BzrDirFormat.set_default_format(old_format)
588
branch.bzrdir.open_workingtree()
589
result = branch.bzrdir.clone(self.get_url('remote'))
590
self.assertRaises(errors.NoWorkingTree,
591
result.open_workingtree)
593
result.open_repository()