1
# Copyright (C) 2006-2010 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
from itertools import izip
23
23
from stat import S_ISDIR
25
26
import bzrlib.branch
26
27
from bzrlib import (
33
35
revision as _mod_revision,
39
from bzrlib.errors import (NoSuchRevision,
42
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
43
from bzrlib.errors import (FileExists,
46
UninitializableFormat,
42
49
import bzrlib.revision
43
50
from bzrlib.tests import (
53
TestCaseWithTransport,
48
from bzrlib.tests.per_controldir import TestCaseWithControlDir
57
from bzrlib.tests.per_bzrdir import TestCaseWithBzrDir
58
from bzrlib.trace import mutter
59
from bzrlib.transport import get_transport
49
60
from bzrlib.transport.local import LocalTransport
50
61
from bzrlib.ui import (
51
62
CannedInputUIFactory,
64
from bzrlib.upgrade import upgrade
53
65
from bzrlib.remote import RemoteBzrDir, RemoteRepository
54
66
from bzrlib.repofmt import weaverepo
57
class TestControlDir(TestCaseWithControlDir):
69
class TestBzrDir(TestCaseWithBzrDir):
58
70
# Many of these tests test for disk equality rather than checking
59
71
# for semantic equivalence. This works well for some tests but
60
72
# is not good at handling changes in representation or the addition
191
203
A simple wrapper for from_bzrdir.sprout that translates NotLocalUrl into
192
204
TestSkipped. Returns the newly sprouted bzrdir.
194
to_transport = transport.get_transport(to_url)
206
to_transport = get_transport(to_url)
195
207
if not isinstance(to_transport, LocalTransport):
196
208
raise TestSkipped('Cannot sprout to remote bzrdirs.')
197
209
target = from_bzrdir.sprout(to_url, revision_id=revision_id,
237
249
bzrdir.destroy_branch()
238
250
except (errors.UnsupportedOperation, errors.TransportNotPossible):
239
raise TestNotApplicable('Format does not support destroying branch')
251
raise TestNotApplicable('Format does not support destroying tree')
240
252
self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
241
253
bzrdir.create_branch()
242
254
bzrdir.open_branch()
254
266
bzrdir.open_repository()
256
268
def test_open_workingtree_raises_no_working_tree(self):
257
"""ControlDir.open_workingtree() should raise NoWorkingTree (rather than
269
"""BzrDir.open_workingtree() should raise NoWorkingTree (rather than
258
270
e.g. NotLocalUrl) if there is no working tree.
260
272
dir = self.make_bzrdir('source')
261
273
vfs_dir = bzrdir.BzrDir.open(self.get_vfs_only_url('source'))
262
274
if vfs_dir.has_workingtree():
263
# This ControlDir format doesn't support ControlDirs without
264
# working trees, so this test is irrelevant.
275
# This BzrDir format doesn't support BzrDirs without working trees,
276
# so this test is irrelevant.
266
278
self.assertRaises(errors.NoWorkingTree, dir.open_workingtree)
512
524
dir = self.make_bzrdir('source')
514
526
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
515
target_branch=referenced_branch)
516
528
except errors.IncompatibleFormat:
517
529
# this is ok too, not all formats have to support references.
615
627
dir = self.make_bzrdir('source')
617
629
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
618
target_branch=referenced_branch)
619
631
except errors.IncompatibleFormat:
620
632
# this is ok too, not all formats have to support references.
680
692
dir = self.make_bzrdir('source')
682
694
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
683
target_branch=referenced_branch)
684
696
except errors.IncompatibleFormat:
685
697
# this is ok too, not all formats have to support references.
955
967
dir = self.make_bzrdir('source')
957
969
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
958
target_branch=referenced_branch)
959
971
except errors.IncompatibleFormat:
960
972
# this is ok too, not all formats have to support references.
975
987
dir = self.make_bzrdir('source')
977
989
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
978
target_branch=referenced_tree.branch)
990
referenced_tree.branch)
979
991
except errors.IncompatibleFormat:
980
992
# this is ok too, not all formats have to support references.
1001
1013
dir = self.make_bzrdir('source')
1003
1015
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1004
target_branch=referenced_tree.branch)
1016
referenced_tree.branch)
1005
1017
except errors.IncompatibleFormat:
1006
1018
# this is ok too, not all formats have to support references.
1066
1078
dir = self.make_bzrdir('source')
1068
1080
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1069
target_branch=referenced_branch)
1070
1082
except errors.IncompatibleFormat:
1071
1083
# this is ok too, not all formats have to support references.
1092
1104
dir = self.make_bzrdir('source')
1094
1106
reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1095
target_branch=referenced_branch)
1096
1108
except errors.IncompatibleFormat:
1097
1109
# this is ok too, not all formats have to support references.
1157
1169
self.assertEqual(tree.branch.last_revision(),
1158
1170
target.open_branch().last_revision())
1160
def test_sprout_with_revision_id_uses_default_stack_on(self):
1161
# Make a branch with three commits to stack on.
1162
builder = self.make_branch_builder('stack-on')
1163
builder.start_series()
1164
builder.build_commit(message='Rev 1.', rev_id='rev-1')
1165
builder.build_commit(message='Rev 2.', rev_id='rev-2')
1166
builder.build_commit(message='Rev 3.', rev_id='rev-3')
1167
builder.finish_series()
1168
stack_on = builder.get_branch()
1169
# Make a bzrdir with a default stacking policy to stack on that branch.
1170
config = self.make_bzrdir('policy-dir').get_config()
1172
config.set_default_stack_on(self.get_url('stack-on'))
1173
except errors.BzrError:
1174
raise TestNotApplicable('Only relevant for stackable formats.')
1175
# Sprout the stacked-on branch into the bzrdir.
1176
sprouted = stack_on.bzrdir.sprout(
1177
self.get_url('policy-dir/sprouted'), revision_id='rev-3')
1178
# Not all revisions are copied into the sprouted repository.
1179
repo = sprouted.open_repository()
1180
self.addCleanup(repo.lock_read().unlock)
1181
self.assertEqual(None, repo.get_parent_map(['rev-1']).get('rev-1'))
1183
1172
def test_format_initialize_find_open(self):
1184
1173
# loopback test to check the current format initializes to itself.
1185
1174
if not self.bzrdir_format.is_supported():
1190
1179
# for remote formats, there must be no prior assumption about the
1191
1180
# network name to use - it's possible that this may somehow have got
1192
1181
# in through an unisolated test though - see
1193
# <https://bugs.launchpad.net/bzr/+bug/504102>
1182
# <https://bugs.edge.launchpad.net/bzr/+bug/504102>
1194
1183
self.assertEquals(getattr(self.bzrdir_format,
1195
1184
'_network_name', None),
1197
1186
# supported formats must be able to init and open
1198
t = transport.get_transport(self.get_url())
1199
readonly_t = transport.get_transport(self.get_readonly_url())
1187
t = get_transport(self.get_url())
1188
readonly_t = get_transport(self.get_readonly_url())
1200
1189
made_control = self.bzrdir_format.initialize(t.base)
1201
self.failUnless(isinstance(made_control, controldir.ControlDir))
1190
self.failUnless(isinstance(made_control, bzrdir.BzrDir))
1202
1191
self.assertEqual(self.bzrdir_format,
1203
controldir.ControlDirFormat.find_format(readonly_t))
1192
bzrdir.BzrDirFormat.find_format(readonly_t))
1204
1193
direct_opened_dir = self.bzrdir_format.open(readonly_t)
1205
1194
opened_dir = bzrdir.BzrDir.open(t.base)
1206
1195
self.assertEqual(made_control._format,
1207
1196
opened_dir._format)
1208
1197
self.assertEqual(direct_opened_dir._format,
1209
1198
opened_dir._format)
1210
self.failUnless(isinstance(opened_dir, controldir.ControlDir))
1199
self.failUnless(isinstance(opened_dir, bzrdir.BzrDir))
1212
1201
def test_format_initialize_on_transport_ex(self):
1213
1202
t = self.get_transport('dir')
1406
1395
# test the formats specific behaviour for no-content or similar dirs.
1407
1396
self.assertRaises(NotBranchError,
1408
1397
self.bzrdir_format.open,
1409
transport.get_transport(self.get_readonly_url()))
1398
get_transport(self.get_readonly_url()))
1411
1400
def test_create_branch(self):
1412
1401
# a bzrdir can construct a branch and repository for itself.
1415
1404
# because the default open will not open them and
1416
1405
# they may not be initializable.
1418
t = transport.get_transport(self.get_url())
1407
t = get_transport(self.get_url())
1419
1408
made_control = self.bzrdir_format.initialize(t.base)
1420
1409
made_repo = made_control.create_repository()
1421
1410
made_branch = made_control.create_branch()
1428
1417
# because the default open will not open them and
1429
1418
# they may not be initializable.
1431
t = transport.get_transport(self.get_url())
1420
t = get_transport(self.get_url())
1432
1421
made_control = self.bzrdir_format.initialize(t.base)
1433
1422
made_repo = made_control.create_repository()
1434
1423
made_branch = made_control.create_branch()
1437
1426
self.failUnless(isinstance(opened_branch, made_branch.__class__))
1438
1427
self.failUnless(isinstance(opened_branch._format, made_branch._format.__class__))
1440
def test_list_branches(self):
1441
if not self.bzrdir_format.is_supported():
1442
# unsupported formats are not loopback testable
1443
# because the default open will not open them and
1444
# they may not be initializable.
1446
t = transport.get_transport(self.get_url())
1447
made_control = self.bzrdir_format.initialize(t.base)
1448
made_repo = made_control.create_repository()
1449
made_branch = made_control.create_branch()
1450
branches = made_control.list_branches()
1451
self.assertEquals(1, len(branches))
1452
self.assertEquals(made_branch.base, branches[0].base)
1454
made_control.destroy_branch()
1455
except errors.UnsupportedOperation:
1456
pass # Not all bzrdirs support destroying directories
1458
self.assertEquals([], made_control.list_branches())
1460
1429
def test_create_repository(self):
1461
1430
# a bzrdir can construct a repository for itself.
1462
1431
if not self.bzrdir_format.is_supported():
1464
1433
# because the default open will not open them and
1465
1434
# they may not be initializable.
1467
t = transport.get_transport(self.get_url())
1436
t = get_transport(self.get_url())
1468
1437
made_control = self.bzrdir_format.initialize(t.base)
1469
1438
made_repo = made_control.create_repository()
1470
1439
# Check that we have a repository object.
1479
1448
# because the default open will not open them and
1480
1449
# they may not be initializable.
1482
t = transport.get_transport(self.get_url())
1451
t = get_transport(self.get_url())
1483
1452
made_control = self.bzrdir_format.initialize(t.base)
1485
1454
made_repo = made_control.create_repository(shared=True)
1496
1465
# because the default open will not open them and
1497
1466
# they may not be initializable.
1499
t = transport.get_transport(self.get_url())
1468
t = get_transport(self.get_url())
1500
1469
made_control = self.bzrdir_format.initialize(t.base)
1501
1470
made_repo = made_control.create_repository(shared=False)
1502
1471
self.assertFalse(made_repo.is_shared())
1507
1476
# because the default open will not open them and
1508
1477
# they may not be initializable.
1510
t = transport.get_transport(self.get_url())
1479
t = get_transport(self.get_url())
1511
1480
made_control = self.bzrdir_format.initialize(t.base)
1512
1481
made_repo = made_control.create_repository()
1513
1482
opened_repo = made_control.open_repository()
1635
1604
def test_root_transport(self):
1636
1605
dir = self.make_bzrdir('.')
1637
1606
self.assertEqual(dir.root_transport.base,
1638
transport.get_transport(self.get_url('.')).base)
1607
get_transport(self.get_url('.')).base)
1640
1609
def test_find_repository_no_repo_under_standalone_branch(self):
1641
1610
# finding a repo stops at standalone branches even if there is a
1646
1615
# need a shared repository to test this.
1648
1617
url = self.get_url('intermediate')
1649
transport.get_transport(self.get_url()).mkdir('intermediate')
1650
transport.get_transport(self.get_url()).mkdir('intermediate/child')
1618
get_transport(self.get_url()).mkdir('intermediate')
1619
get_transport(self.get_url()).mkdir('intermediate/child')
1651
1620
made_control = self.bzrdir_format.initialize(url)
1652
1621
made_control.create_repository()
1653
1622
innermost_control = self.bzrdir_format.initialize(
1671
1640
# need a shared repository to test this.
1673
1642
url = self.get_url('childbzrdir')
1674
transport.get_transport(self.get_url()).mkdir('childbzrdir')
1643
get_transport(self.get_url()).mkdir('childbzrdir')
1675
1644
made_control = self.bzrdir_format.initialize(url)
1677
1646
child_repo = made_control.open_repository()
1705
1674
# need a shared repository to test this.
1707
1676
url = self.get_url('childrepo')
1708
transport.get_transport(self.get_url()).mkdir('childrepo')
1677
get_transport(self.get_url()).mkdir('childrepo')
1709
1678
child_control = self.bzrdir_format.initialize(url)
1710
1679
child_repo = child_control.create_repository(shared=True)
1711
1680
opened_control = bzrdir.BzrDir.open(self.get_url('childrepo'))
1724
1693
# need a shared repository to test this.
1726
1695
url = self.get_url('intermediate')
1727
transport.get_transport(self.get_url()).mkdir('intermediate')
1728
transport.get_transport(self.get_url()).mkdir('intermediate/child')
1696
get_transport(self.get_url()).mkdir('intermediate')
1697
get_transport(self.get_url()).mkdir('intermediate/child')
1729
1698
made_control = self.bzrdir_format.initialize(url)
1731
1700
child_repo = made_control.open_repository()
1757
1726
self.assertTrue(isinstance(dir._format.get_converter(
1758
1727
format=dir._format), bzrdir.Converter))
1759
1728
dir.needs_format_conversion(
1760
controldir.ControlDirFormat.get_default_format())
1729
bzrdir.BzrDirFormat.get_default_format())
1762
1731
def test_backup_copies_existing(self):
1763
1732
tree = self.make_branch_and_tree('test')
1826
1795
bd.retire_bzrdir, limit=0)
1829
class TestBreakLock(TestCaseWithControlDir):
1798
class TestBreakLock(TestCaseWithBzrDir):
1831
1800
def test_break_lock_empty(self):
1832
1801
# break lock on an empty bzrdir should work silently.
1870
1839
thisdir = self.make_bzrdir('this')
1872
1841
bzrlib.branch.BranchReferenceFormat().initialize(
1873
thisdir, target_branch=master)
1874
1843
except errors.IncompatibleFormat:
1876
1845
unused_repo = thisdir.create_repository()
1934
1903
self.assertRaises(errors.LockBroken, tree.unlock)
1937
class TestTransportConfig(TestCaseWithControlDir):
1906
class TestTransportConfig(TestCaseWithBzrDir):
1939
1908
def test_get_config(self):
1940
1909
my_dir = self.make_bzrdir('.')
1956
1925
self.assertEqual('http://example.com', config2.get_default_stack_on())
1959
class ChrootedControlDirTests(ChrootedTestCase):
1928
class ChrootedBzrDirTests(ChrootedTestCase):
1961
1930
def test_find_repository_no_repository(self):
1962
1931
# loopback test to check the current format fails to find a
1970
1939
# - do the vfs initialisation over the basic vfs transport
1971
1940
# XXX: TODO this should become a 'bzrdirlocation' api call.
1972
1941
url = self.get_vfs_only_url('subdir')
1973
transport.get_transport(self.get_vfs_only_url()).mkdir('subdir')
1942
get_transport(self.get_vfs_only_url()).mkdir('subdir')
1974
1943
made_control = self.bzrdir_format.initialize(self.get_url('subdir'))
1976
1945
repo = made_control.open_repository()
1983
1952
self.assertRaises(errors.NoRepositoryPresent,
1984
1953
made_control.find_repository)
1987
class TestControlDirControlComponent(TestCaseWithControlDir):
1988
"""ControlDir implementations adequately implement ControlComponent."""
1990
def test_urls(self):
1991
bd = self.make_bzrdir('bd')
1992
self.assertIsInstance(bd.user_url, str)
1993
self.assertEqual(bd.user_url, bd.user_transport.base)
1994
# for all current bzrdir implementations the user dir must be
1995
# above the control dir but we might need to relax that?
1996
self.assertEqual(bd.control_url.find(bd.user_url), 0)
1997
self.assertEqual(bd.control_url, bd.control_transport.base)