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
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the BzrDir facility and any format specific tests.
19
For interface contract tests, see tests/per_bzr_dir.
19
For interface contract tests, see tests/bzr_dir_implementations.
24
from StringIO import StringIO
45
47
TestCaseWithMemoryTransport,
46
48
TestCaseWithTransport,
49
52
from bzrlib.tests import(
53
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
54
from bzrlib.transport import (
57
from bzrlib.transport import get_transport
58
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
59
from bzrlib.transport.memory import MemoryServer
59
60
from bzrlib.transport.nosmart import NoSmartTransportDecorator
60
61
from bzrlib.transport.readonly import ReadonlyTransportDecorator
61
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
62
from bzrlib.repofmt import knitrepo, weaverepo
64
65
class TestDefaultFormat(TestCase):
148
149
new = topics.get_detail('current-formats')
149
150
rest = topics.get_detail('other-formats')
150
151
experimental, deprecated = rest.split('Deprecated formats')
151
self.assertContainsRe(new, 'formats-help')
152
self.assertContainsRe(new, 'bzr help formats')
152
153
self.assertContainsRe(new,
153
154
':knit:\n \(native\) \(default\) Format using knits\n')
154
155
self.assertContainsRe(experimental,
207
208
"""See BzrDir.open_repository."""
208
209
return SampleRepository(self)
210
def create_branch(self, name=None):
211
def create_branch(self):
211
212
"""See BzrDir.create_branch."""
213
raise NoColocatedBranchSupport(self)
214
213
return SampleBranch(self)
216
215
def create_workingtree(self):
360
359
def test_create_branch_convenience_root(self):
361
360
"""Creating a branch at the root of a fs should work."""
362
self.vfs_transport_factory = memory.MemoryServer
361
self.vfs_transport_factory = MemoryServer
363
362
# outside a repo the default convenience output is a repo+branch_tree
364
363
format = bzrdir.format_registry.make_bzrdir('knit')
365
364
branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
433
432
"""The default acquisition policy should create a standalone branch."""
434
433
my_bzrdir = self.make_bzrdir('.')
435
434
repo_policy = my_bzrdir.determine_repository_policy()
436
repo, is_new = repo_policy.acquire_repository()
435
repo = repo_policy.acquire_repository()
437
436
self.assertEqual(repo.bzrdir.root_transport.base,
438
437
my_bzrdir.root_transport.base)
439
438
self.assertFalse(repo.is_shared())
441
441
def test_determine_stacking_policy(self):
442
442
parent_bzrdir = self.make_bzrdir('.')
443
443
child_bzrdir = self.make_bzrdir('child')
467
467
self.assertEqual(child_branch.base,
468
468
new_child.open_branch().get_stacked_on_url())
470
def test_default_stacking_with_stackable_branch_unstackable_repo(self):
471
# Make stackable source branch with an unstackable repo format.
472
source_bzrdir = self.make_bzrdir('source')
473
pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
474
source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
476
# Make a directory with a default stacking policy
477
parent_bzrdir = self.make_bzrdir('parent')
478
stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
479
parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
480
# Clone source into directory
481
target = source_bzrdir.clone(self.get_url('parent/target'))
483
470
def test_sprout_obeys_stacking_policy(self):
484
471
child_branch, new_child_transport = self.prepare_default_stacking()
485
472
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
573
560
super(ChrootedTests, self).setUp()
574
if not self.vfs_transport_factory == memory.MemoryServer:
561
if not self.vfs_transport_factory == MemoryServer:
575
562
self.transport_readonly_server = http_server.HttpServer
577
564
def local_branch_path(self, branch):
756
743
def test_sprout_recursive(self):
757
tree = self.make_branch_and_tree('tree1',
758
format='dirstate-with-subtree')
744
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
759
745
sub_tree = self.make_branch_and_tree('tree1/subtree',
760
746
format='dirstate-with-subtree')
761
sub_tree.set_root_id('subtree-root')
762
747
tree.add_reference(sub_tree)
763
748
self.build_tree(['tree1/subtree/file'])
764
749
sub_tree.add('file')
765
750
tree.commit('Initial commit')
766
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
768
self.addCleanup(tree2.unlock)
751
tree.bzrdir.sprout('tree2')
769
752
self.failUnlessExists('tree2/subtree/file')
770
self.assertEqual('tree-reference', tree2.kind('subtree-root'))
772
754
def test_cloning_metadir(self):
773
755
"""Ensure that cloning metadir is suitable"""
1056
1038
def setUp(self):
1057
1039
super(NonLocalTests, self).setUp()
1058
self.vfs_transport_factory = memory.MemoryServer
1040
self.vfs_transport_factory = MemoryServer
1060
1042
def test_create_branch_convenience(self):
1061
1043
# outside a repo the default convenience output is a repo+branch_tree
1158
1140
_transport = HttpTransport_urllib
1160
1142
def _qualified_url(self, host, port):
1161
result = 'http+urllib://%s:%s' % (host, port)
1162
self.permit_url(result)
1143
return 'http+urllib://%s:%s' % (host, port)
1170
1150
"""Tests redirections for pycurl implementation"""
1172
1152
def _qualified_url(self, host, port):
1173
result = 'http+pycurl://%s:%s' % (host, port)
1174
self.permit_url(result)
1153
return 'http+pycurl://%s:%s' % (host, port)
1178
1156
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1182
1160
_transport = NoSmartTransportDecorator
1184
1162
def _qualified_url(self, host, port):
1185
result = 'nosmart+http://%s:%s' % (host, port)
1186
self.permit_url(result)
1163
return 'nosmart+http://%s:%s' % (host, port)
1190
1166
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1194
1170
_transport = ReadonlyTransportDecorator
1196
1172
def _qualified_url(self, host, port):
1197
result = 'readonly+http://%s:%s' % (host, port)
1198
self.permit_url(result)
1173
return 'readonly+http://%s:%s' % (host, port)
1202
1176
class TestDotBzrHidden(TestCaseWithTransport):
1254
1228
return _TestBzrDirFormat()
1257
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1258
"""Test Branch format for TestBzrDirSprout."""
1261
1231
class _TestBranch(bzrlib.branch.Branch):
1262
1232
"""Test Branch implementation for TestBzrDirSprout."""
1264
1234
def __init__(self, *args, **kwargs):
1265
self._format = _TestBranchFormat()
1266
1235
super(_TestBranch, self).__init__(*args, **kwargs)
1267
1236
self.calls = []
1268
1237
self._parent = None
1316
1285
parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1317
1286
branch_tree = parent.bzrdir.sprout('branch').open_branch()
1318
1287
self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1321
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1323
def test_pre_open_called(self):
1325
bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1326
transport = self.get_transport('foo')
1327
url = transport.base
1328
self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1329
self.assertEqual([transport.base], [t.base for t in calls])
1331
def test_pre_open_actual_exceptions_raised(self):
1333
def fail_once(transport):
1336
raise errors.BzrError("fail")
1337
bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1338
transport = self.get_transport('foo')
1339
url = transport.base
1340
err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1341
self.assertEqual('fail', err._preformatted_string)
1343
def test_post_repo_init(self):
1344
from bzrlib.bzrdir import RepoInitHookParams
1346
bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1348
self.make_repository('foo')
1349
self.assertLength(1, calls)
1351
self.assertIsInstance(params, RepoInitHookParams)
1352
self.assertTrue(hasattr(params, 'bzrdir'))
1353
self.assertTrue(hasattr(params, 'repository'))