447
451
branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
448
452
self.assertEqual('g/p/q', relpath)
454
def test_open_containing_tree_branch_or_repository_empty(self):
455
self.assertRaises(errors.NotBranchError,
456
bzrdir.BzrDir.open_containing_tree_branch_or_repository,
457
self.get_readonly_url(''))
459
def test_open_containing_tree_branch_or_repository_all(self):
460
self.make_branch_and_tree('topdir')
461
tree, branch, repo, relpath = \
462
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
464
self.assertEqual(os.path.realpath('topdir'),
465
os.path.realpath(tree.basedir))
466
self.assertEqual(os.path.realpath('topdir'),
467
self.local_branch_path(branch))
469
os.path.realpath(os.path.join('topdir', '.bzr', 'repository')),
470
repo.bzrdir.transport.local_abspath('repository'))
471
self.assertEqual(relpath, 'foo')
473
def test_open_containing_tree_branch_or_repository_no_tree(self):
474
self.make_branch('branch')
475
tree, branch, repo, relpath = \
476
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
478
self.assertEqual(tree, None)
479
self.assertEqual(os.path.realpath('branch'),
480
self.local_branch_path(branch))
482
os.path.realpath(os.path.join('branch', '.bzr', 'repository')),
483
repo.bzrdir.transport.local_abspath('repository'))
484
self.assertEqual(relpath, 'foo')
486
def test_open_containing_tree_branch_or_repository_repo(self):
487
self.make_repository('repo')
488
tree, branch, repo, relpath = \
489
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
491
self.assertEqual(tree, None)
492
self.assertEqual(branch, None)
494
os.path.realpath(os.path.join('repo', '.bzr', 'repository')),
495
repo.bzrdir.transport.local_abspath('repository'))
496
self.assertEqual(relpath, '')
498
def test_open_containing_tree_branch_or_repository_shared_repo(self):
499
self.make_repository('shared', shared=True)
500
bzrdir.BzrDir.create_branch_convenience('shared/branch',
501
force_new_tree=False)
502
tree, branch, repo, relpath = \
503
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
505
self.assertEqual(tree, None)
506
self.assertEqual(os.path.realpath('shared/branch'),
507
self.local_branch_path(branch))
509
os.path.realpath(os.path.join('shared', '.bzr', 'repository')),
510
repo.bzrdir.transport.local_abspath('repository'))
511
self.assertEqual(relpath, '')
513
def test_open_containing_tree_branch_or_repository_branch_subdir(self):
514
self.make_branch_and_tree('foo')
515
self.build_tree(['foo/bar/'])
516
tree, branch, repo, relpath = \
517
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
519
self.assertEqual(os.path.realpath('foo'),
520
os.path.realpath(tree.basedir))
521
self.assertEqual(os.path.realpath('foo'),
522
self.local_branch_path(branch))
524
os.path.realpath(os.path.join('foo', '.bzr', 'repository')),
525
repo.bzrdir.transport.local_abspath('repository'))
526
self.assertEqual(relpath, 'bar')
528
def test_open_containing_tree_branch_or_repository_repo_subdir(self):
529
self.make_repository('bar')
530
self.build_tree(['bar/baz/'])
531
tree, branch, repo, relpath = \
532
bzrdir.BzrDir.open_containing_tree_branch_or_repository(
534
self.assertEqual(tree, None)
535
self.assertEqual(branch, None)
537
os.path.realpath(os.path.join('bar', '.bzr', 'repository')),
538
repo.bzrdir.transport.local_abspath('repository'))
539
self.assertEqual(relpath, 'baz')
450
541
def test_open_containing_from_transport(self):
451
542
self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
452
543
get_transport(self.get_readonly_url('')))
461
552
self.assertEqual('g/p/q', relpath)
463
554
def test_open_containing_tree_or_branch(self):
464
def local_branch_path(branch):
465
return os.path.realpath(
466
urlutils.local_path_from_url(branch.base))
468
555
self.make_branch_and_tree('topdir')
469
556
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
471
558
self.assertEqual(os.path.realpath('topdir'),
472
559
os.path.realpath(tree.basedir))
473
560
self.assertEqual(os.path.realpath('topdir'),
474
local_branch_path(branch))
561
self.local_branch_path(branch))
475
562
self.assertIs(tree.bzrdir, branch.bzrdir)
476
563
self.assertEqual('foo', relpath)
477
564
# opening from non-local should not return the tree
486
573
self.assertIs(tree, None)
487
574
self.assertEqual(os.path.realpath('topdir/foo'),
488
local_branch_path(branch))
575
self.local_branch_path(branch))
489
576
self.assertEqual('', relpath)
491
578
def test_open_tree_or_branch(self):
492
def local_branch_path(branch):
493
return os.path.realpath(
494
urlutils.local_path_from_url(branch.base))
496
579
self.make_branch_and_tree('topdir')
497
580
tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
498
581
self.assertEqual(os.path.realpath('topdir'),
499
582
os.path.realpath(tree.basedir))
500
583
self.assertEqual(os.path.realpath('topdir'),
501
local_branch_path(branch))
584
self.local_branch_path(branch))
502
585
self.assertIs(tree.bzrdir, branch.bzrdir)
503
586
# opening from non-local should not return the tree
504
587
tree, branch = bzrdir.BzrDir.open_tree_or_branch(