1140
1197
opened_dir._format)
1141
1198
self.failUnless(isinstance(opened_dir, bzrdir.BzrDir))
1200
def test_format_initialize_on_transport_ex(self):
1201
t = self.get_transport('dir')
1202
self.assertInitializeEx(t)
1204
def test_format_initialize_on_transport_ex_use_existing_dir_True(self):
1205
t = self.get_transport('dir')
1207
self.assertInitializeEx(t, use_existing_dir=True)
1209
def test_format_initialize_on_transport_ex_use_existing_dir_False(self):
1210
if not self.bzrdir_format.is_supported():
1211
# Not initializable - not a failure either.
1213
t = self.get_transport('dir')
1215
self.assertRaises(errors.FileExists,
1216
self.bzrdir_format.initialize_on_transport_ex, t,
1217
use_existing_dir=False)
1219
def test_format_initialize_on_transport_ex_create_prefix_True(self):
1220
t = self.get_transport('missing/dir')
1221
self.assertInitializeEx(t, create_prefix=True)
1223
def test_format_initialize_on_transport_ex_create_prefix_False(self):
1224
if not self.bzrdir_format.is_supported():
1225
# Not initializable - not a failure either.
1227
t = self.get_transport('missing/dir')
1228
self.assertRaises(errors.NoSuchFile, self.assertInitializeEx, t,
1229
create_prefix=False)
1231
def test_format_initialize_on_transport_ex_force_new_repo_True(self):
1232
t = self.get_transport('repo')
1233
repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
1234
repo_name = repo_fmt.repository_format.network_name()
1235
repo = repo_fmt.initialize_on_transport_ex(t,
1236
repo_format_name=repo_name, shared_repo=True)[0]
1237
made_repo, control = self.assertInitializeEx(t.clone('branch'),
1238
force_new_repo=True, repo_format_name=repo_name)
1240
# uninitialisable format
1242
self.assertNotEqual(repo.bzrdir.root_transport.base,
1243
made_repo.bzrdir.root_transport.base)
1245
def test_format_initialize_on_transport_ex_force_new_repo_False(self):
1246
t = self.get_transport('repo')
1247
repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
1248
repo_name = repo_fmt.repository_format.network_name()
1249
repo = repo_fmt.initialize_on_transport_ex(t,
1250
repo_format_name=repo_name, shared_repo=True)[0]
1251
made_repo, control = self.assertInitializeEx(t.clone('branch'),
1252
force_new_repo=False, repo_format_name=repo_name)
1254
# uninitialisable format
1256
if not isinstance(control._format, (bzrdir.BzrDirFormat5,
1257
bzrdir.BzrDirFormat6,)):
1258
self.assertEqual(repo.bzrdir.root_transport.base,
1259
made_repo.bzrdir.root_transport.base)
1261
def test_format_initialize_on_transport_ex_stacked_on(self):
1262
# trunk is a stackable format. Note that its in the same server area
1263
# which is what launchpad does, but not sufficient to exercise the
1265
trunk = self.make_branch('trunk', format='1.9')
1266
t = self.get_transport('stacked')
1267
old_fmt = bzrdir.format_registry.make_bzrdir('pack-0.92')
1268
repo_name = old_fmt.repository_format.network_name()
1269
# Should end up with a 1.9 format (stackable)
1270
repo, control = self.assertInitializeEx(t, need_meta=True,
1271
repo_format_name=repo_name, stacked_on='../trunk', stack_on_pwd=t.base)
1273
# uninitialisable format
1275
self.assertLength(1, repo._fallback_repositories)
1277
def test_format_initialize_on_transport_ex_default_stack_on(self):
1278
# When initialize_on_transport_ex uses a stacked-on branch because of
1279
# a stacking policy on the target, the location of the fallback
1280
# repository is the same as the external location of the stacked-on
1282
balloon = self.make_bzrdir('balloon')
1283
if isinstance(balloon._format, bzrdir.BzrDirMetaFormat1):
1284
stack_on = self.make_branch('stack-on', format='1.9')
1286
stack_on = self.make_branch('stack-on')
1287
config = self.make_bzrdir('.').get_config()
1289
config.set_default_stack_on('stack-on')
1290
except errors.BzrError:
1291
raise TestNotApplicable('Only relevant for stackable formats.')
1292
# Initialize a bzrdir subject to the policy.
1293
t = self.get_transport('stacked')
1294
repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
1295
repo_name = repo_fmt.repository_format.network_name()
1296
repo, control = self.assertInitializeEx(
1297
t, need_meta=True, repo_format_name=repo_name, stacked_on=None)
1298
# self.addCleanup(repo.unlock)
1300
# uninitialisable format
1302
# There's one fallback repo, with a public location.
1303
self.assertLength(1, repo._fallback_repositories)
1304
fallback_repo = repo._fallback_repositories[0]
1306
stack_on.base, fallback_repo.bzrdir.root_transport.base)
1307
# The bzrdir creates a branch in stacking-capable format.
1308
new_branch = control.create_branch()
1309
self.assertTrue(new_branch._format.supports_stacking())
1311
def test_format_initialize_on_transport_ex_repo_fmt_name_None(self):
1312
t = self.get_transport('dir')
1313
repo, control = self.assertInitializeEx(t)
1314
self.assertEqual(None, repo)
1316
def test_format_initialize_on_transport_ex_repo_fmt_name_followed(self):
1317
t = self.get_transport('dir')
1318
# 1.6 is likely to never be default
1319
fmt = bzrdir.format_registry.make_bzrdir('1.6')
1320
repo_name = fmt.repository_format.network_name()
1321
repo, control = self.assertInitializeEx(t, repo_format_name=repo_name)
1323
# uninitialisable format
1325
if isinstance(self.bzrdir_format, (bzrdir.BzrDirFormat5,
1326
bzrdir.BzrDirFormat6)):
1327
# must stay with the all-in-one-format.
1328
repo_name = self.bzrdir_format.network_name()
1329
self.assertEqual(repo_name, repo._format.network_name())
1331
def assertInitializeEx(self, t, need_meta=False, **kwargs):
1332
"""Execute initialize_on_transport_ex and check it succeeded correctly.
1334
This involves checking that the disk objects were created, open with
1335
the same format returned, and had the expected disk format.
1337
:param t: The transport to initialize on.
1338
:param **kwargs: Additional arguments to pass to
1339
initialize_on_transport_ex.
1340
:return: the resulting repo, control dir tuple.
1342
if not self.bzrdir_format.is_supported():
1343
# Not initializable - not a failure either.
1345
repo, control, require_stacking, repo_policy = \
1346
self.bzrdir_format.initialize_on_transport_ex(t, **kwargs)
1347
if repo is not None:
1348
# Repositories are open write-locked
1349
self.assertTrue(repo.is_write_locked())
1350
self.addCleanup(repo.unlock)
1351
self.assertIsInstance(control, bzrdir.BzrDir)
1352
opened = bzrdir.BzrDir.open(t.base)
1353
expected_format = self.bzrdir_format
1354
if isinstance(expected_format, bzrdir.RemoteBzrDirFormat):
1355
# Current RemoteBzrDirFormat's do not reliably get network_name
1356
# set, so we skip a number of tests for RemoteBzrDirFormat's.
1357
self.assertIsInstance(control, RemoteBzrDir)
1359
if need_meta and isinstance(expected_format, (bzrdir.BzrDirFormat5,
1360
bzrdir.BzrDirFormat6)):
1361
# Pre-metadir formats change when we are making something that
1362
# needs a metaformat, because clone is used for push.
1363
expected_format = bzrdir.BzrDirMetaFormat1()
1364
self.assertEqual(control._format.network_name(),
1365
expected_format.network_name())
1366
self.assertEqual(control._format.network_name(),
1367
opened._format.network_name())
1368
self.assertEqual(control.__class__, opened.__class__)
1369
return repo, control
1371
def test_format_network_name(self):
1372
# All control formats must have a network name.
1373
dir = self.make_bzrdir('.')
1374
format = dir._format
1375
# We want to test that the network_name matches the actual format on
1376
# disk. For local control dirsthat means that using network_name as a
1377
# key in the registry gives back the same format. For remote obects
1378
# we check that the network_name of the RemoteBzrDirFormat we have
1379
# locally matches the actual format present on disk.
1380
if isinstance(format, bzrdir.RemoteBzrDirFormat):
1382
real_dir = dir._real_bzrdir
1383
network_name = format.network_name()
1384
self.assertEqual(real_dir._format.network_name(), network_name)
1386
registry = bzrdir.network_format_registry
1387
network_name = format.network_name()
1388
looked_up_format = registry.get(network_name)
1389
self.assertEqual(format.__class__, looked_up_format.__class__)
1390
# The network name must be a byte string.
1391
self.assertIsInstance(network_name, str)
1143
1393
def test_open_not_bzrdir(self):
1144
1394
# test the formats specific behaviour for no-content or similar dirs.
1145
1395
self.assertRaises(NotBranchError,
1464
1734
found_repo = innermost_control.find_repository()
1465
1735
self.assertEqual(repo.bzrdir.root_transport.base,
1466
1736
found_repo.bzrdir.root_transport.base)
1468
1738
def test_can_and_needs_format_conversion(self):
1469
1739
# check that we can ask an instance if its upgradable
1470
1740
dir = self.make_bzrdir('.')
1471
1741
if dir.can_convert_format():
1472
# if its default updatable there must be an updater
1742
# if its default updatable there must be an updater
1473
1743
# (we force the latest known format as downgrades may not be
1475
1745
self.assertTrue(isinstance(dir._format.get_converter(
1476
1746
format=dir._format), bzrdir.Converter))
1477
dir.needs_format_conversion(None)
1747
dir.needs_format_conversion(
1748
bzrdir.BzrDirFormat.get_default_format())
1750
def test_backup_copies_existing(self):
1751
tree = self.make_branch_and_tree('test')
1752
self.build_tree(['test/a'])
1753
tree.add(['a'], ['a-id'])
1754
tree.commit('some data to be copied.')
1755
old_url, new_url = tree.bzrdir.backup_bzrdir()
1756
old_path = urlutils.local_path_from_url(old_url)
1757
new_path = urlutils.local_path_from_url(new_url)
1758
self.failUnlessExists(old_path)
1759
self.failUnlessExists(new_path)
1760
for (((dir_relpath1, _), entries1),
1761
((dir_relpath2, _), entries2)) in izip(
1762
osutils.walkdirs(old_path),
1763
osutils.walkdirs(new_path)):
1764
self.assertEquals(dir_relpath1, dir_relpath2)
1765
for f1, f2 in zip(entries1, entries2):
1766
self.assertEquals(f1[0], f2[0])
1767
self.assertEquals(f1[2], f2[2])
1769
osutils.compare_files(open(f1[4]), open(f2[4]))
1479
1771
def test_upgrade_new_instance(self):
1480
1772
"""Does an available updater work?"""