~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: John Arbash Meinel
  • Date: 2008-10-30 00:55:00 UTC
  • mto: (3815.2.5 prepare-1.9)
  • mto: This revision was merged to the branch mainline in revision 3811.
  • Revision ID: john@arbash-meinel.com-20081030005500-r5cej1cxflqhs3io
Switch so that we are using a simple timestamp as the first action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
 
 
2
#
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
5
5
# the Free Software Foundation; either version 2 of the License, or
19
19
For interface contract tests, see tests/bzr_dir_implementations.
20
20
"""
21
21
 
 
22
import os
22
23
import os.path
23
24
from StringIO import StringIO
24
25
import subprocess
29
30
    errors,
30
31
    help_topics,
31
32
    repository,
 
33
    osutils,
32
34
    symbol_versioning,
33
35
    urlutils,
34
36
    win32utils,
41
43
                           )
42
44
from bzrlib.tests import (
43
45
    TestCase,
 
46
    TestCaseWithMemoryTransport,
44
47
    TestCaseWithTransport,
45
48
    TestSkipped,
46
49
    test_sftp_transport
423
426
        self.assertFalse(repo.is_shared())
424
427
 
425
428
 
 
429
    def test_determine_stacking_policy(self):
 
430
        parent_bzrdir = self.make_bzrdir('.')
 
431
        child_bzrdir = self.make_bzrdir('child')
 
432
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
 
433
        repo_policy = child_bzrdir.determine_repository_policy()
 
434
        self.assertEqual('http://example.org', repo_policy._stack_on)
 
435
 
 
436
    def test_determine_stacking_policy_relative(self):
 
437
        parent_bzrdir = self.make_bzrdir('.')
 
438
        child_bzrdir = self.make_bzrdir('child')
 
439
        parent_bzrdir.get_config().set_default_stack_on('child2')
 
440
        repo_policy = child_bzrdir.determine_repository_policy()
 
441
        self.assertEqual('child2', repo_policy._stack_on)
 
442
        self.assertEqual(parent_bzrdir.root_transport.base,
 
443
                         repo_policy._stack_on_pwd)
 
444
 
 
445
    def prepare_default_stacking(self, child_format='1.6'):
 
446
        parent_bzrdir = self.make_bzrdir('.')
 
447
        child_branch = self.make_branch('child', format=child_format)
 
448
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
 
449
        new_child_transport = parent_bzrdir.transport.clone('child2')
 
450
        return child_branch, new_child_transport
 
451
 
 
452
    def test_clone_on_transport_obeys_stacking_policy(self):
 
453
        child_branch, new_child_transport = self.prepare_default_stacking()
 
454
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
 
455
        self.assertEqual(child_branch.base,
 
456
                         new_child.open_branch().get_stacked_on_url())
 
457
 
 
458
    def test_sprout_obeys_stacking_policy(self):
 
459
        child_branch, new_child_transport = self.prepare_default_stacking()
 
460
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
 
461
        self.assertEqual(child_branch.base,
 
462
                         new_child.open_branch().get_stacked_on_url())
 
463
 
 
464
    def test_clone_ignores_policy_for_unsupported_formats(self):
 
465
        child_branch, new_child_transport = self.prepare_default_stacking(
 
466
            child_format='pack-0.92')
 
467
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
 
468
        self.assertRaises(errors.UnstackableBranchFormat,
 
469
                          new_child.open_branch().get_stacked_on_url)
 
470
 
 
471
    def test_sprout_ignores_policy_for_unsupported_formats(self):
 
472
        child_branch, new_child_transport = self.prepare_default_stacking(
 
473
            child_format='pack-0.92')
 
474
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
 
475
        self.assertRaises(errors.UnstackableBranchFormat,
 
476
                          new_child.open_branch().get_stacked_on_url)
 
477
 
 
478
    def test_sprout_upgrades_format_if_stacked_specified(self):
 
479
        child_branch, new_child_transport = self.prepare_default_stacking(
 
480
            child_format='pack-0.92')
 
481
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
 
482
                                               stacked=True)
 
483
        self.assertEqual(child_branch.bzrdir.root_transport.base,
 
484
                         new_child.open_branch().get_stacked_on_url())
 
485
        repo = new_child.open_repository()
 
486
        self.assertTrue(repo._format.supports_external_lookups)
 
487
        self.assertFalse(repo.supports_rich_root())
 
488
 
 
489
    def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
 
490
        child_branch, new_child_transport = self.prepare_default_stacking(
 
491
            child_format='pack-0.92')
 
492
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
 
493
            stacked_on=child_branch.bzrdir.root_transport.base)
 
494
        self.assertEqual(child_branch.bzrdir.root_transport.base,
 
495
                         new_child.open_branch().get_stacked_on_url())
 
496
        repo = new_child.open_repository()
 
497
        self.assertTrue(repo._format.supports_external_lookups)
 
498
        self.assertFalse(repo.supports_rich_root())
 
499
 
 
500
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
 
501
        child_branch, new_child_transport = self.prepare_default_stacking(
 
502
            child_format='rich-root-pack')
 
503
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
 
504
                                               stacked=True)
 
505
        repo = new_child.open_repository()
 
506
        self.assertTrue(repo._format.supports_external_lookups)
 
507
        self.assertTrue(repo.supports_rich_root())
 
508
 
 
509
    def test_add_fallback_repo_handles_absolute_urls(self):
 
510
        stack_on = self.make_branch('stack_on', format='1.6')
 
511
        repo = self.make_repository('repo', format='1.6')
 
512
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
 
513
        policy._add_fallback(repo)
 
514
 
 
515
    def test_add_fallback_repo_handles_relative_urls(self):
 
516
        stack_on = self.make_branch('stack_on', format='1.6')
 
517
        repo = self.make_repository('repo', format='1.6')
 
518
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
 
519
        policy._add_fallback(repo)
 
520
 
 
521
    def test_configure_relative_branch_stacking_url(self):
 
522
        stack_on = self.make_branch('stack_on', format='1.6')
 
523
        stacked = self.make_branch('stack_on/stacked', format='1.6')
 
524
        policy = bzrdir.UseExistingRepository(stacked.repository,
 
525
            '.', stack_on.base)
 
526
        policy.configure_branch(stacked)
 
527
        self.assertEqual('..', stacked.get_stacked_on_url())
 
528
 
 
529
    def test_relative_branch_stacking_to_absolute(self):
 
530
        stack_on = self.make_branch('stack_on', format='1.6')
 
531
        stacked = self.make_branch('stack_on/stacked', format='1.6')
 
532
        policy = bzrdir.UseExistingRepository(stacked.repository,
 
533
            '.', self.get_readonly_url('stack_on'))
 
534
        policy.configure_branch(stacked)
 
535
        self.assertEqual(self.get_readonly_url('stack_on'),
 
536
                         stacked.get_stacked_on_url())
 
537
 
 
538
 
426
539
class ChrootedTests(TestCaseWithTransport):
427
540
    """A support class that provides readonly urls outside the local namespace.
428
541
 
436
549
        if not self.vfs_transport_factory == MemoryServer:
437
550
            self.transport_readonly_server = HttpServer
438
551
 
 
552
    def local_branch_path(self, branch):
 
553
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
 
554
 
439
555
    def test_open_containing(self):
440
556
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
441
557
                          self.get_readonly_url(''))
447
563
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
448
564
        self.assertEqual('g/p/q', relpath)
449
565
 
 
566
    def test_open_containing_tree_branch_or_repository_empty(self):
 
567
        self.assertRaises(errors.NotBranchError,
 
568
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
 
569
            self.get_readonly_url(''))
 
570
 
 
571
    def test_open_containing_tree_branch_or_repository_all(self):
 
572
        self.make_branch_and_tree('topdir')
 
573
        tree, branch, repo, relpath = \
 
574
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
 
575
                'topdir/foo')
 
576
        self.assertEqual(os.path.realpath('topdir'),
 
577
                         os.path.realpath(tree.basedir))
 
578
        self.assertEqual(os.path.realpath('topdir'),
 
579
                         self.local_branch_path(branch))
 
580
        self.assertEqual(
 
581
            osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
 
582
            repo.bzrdir.transport.local_abspath('repository'))
 
583
        self.assertEqual(relpath, 'foo')
 
584
 
 
585
    def test_open_containing_tree_branch_or_repository_no_tree(self):
 
586
        self.make_branch('branch')
 
587
        tree, branch, repo, relpath = \
 
588
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
 
589
                'branch/foo')
 
590
        self.assertEqual(tree, None)
 
591
        self.assertEqual(os.path.realpath('branch'),
 
592
                         self.local_branch_path(branch))
 
593
        self.assertEqual(
 
594
            osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
 
595
            repo.bzrdir.transport.local_abspath('repository'))
 
596
        self.assertEqual(relpath, 'foo')
 
597
 
 
598
    def test_open_containing_tree_branch_or_repository_repo(self):
 
599
        self.make_repository('repo')
 
600
        tree, branch, repo, relpath = \
 
601
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
 
602
                'repo')
 
603
        self.assertEqual(tree, None)
 
604
        self.assertEqual(branch, None)
 
605
        self.assertEqual(
 
606
            osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
 
607
            repo.bzrdir.transport.local_abspath('repository'))
 
608
        self.assertEqual(relpath, '')
 
609
 
 
610
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
 
611
        self.make_repository('shared', shared=True)
 
612
        bzrdir.BzrDir.create_branch_convenience('shared/branch',
 
613
                                                force_new_tree=False)
 
614
        tree, branch, repo, relpath = \
 
615
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
 
616
                'shared/branch')
 
617
        self.assertEqual(tree, None)
 
618
        self.assertEqual(os.path.realpath('shared/branch'),
 
619
                         self.local_branch_path(branch))
 
620
        self.assertEqual(
 
621
            osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
 
622
            repo.bzrdir.transport.local_abspath('repository'))
 
623
        self.assertEqual(relpath, '')
 
624
 
 
625
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
 
626
        self.make_branch_and_tree('foo')
 
627
        self.build_tree(['foo/bar/'])
 
628
        tree, branch, repo, relpath = \
 
629
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
 
630
                'foo/bar')
 
631
        self.assertEqual(os.path.realpath('foo'),
 
632
                         os.path.realpath(tree.basedir))
 
633
        self.assertEqual(os.path.realpath('foo'),
 
634
                         self.local_branch_path(branch))
 
635
        self.assertEqual(
 
636
            osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
 
637
            repo.bzrdir.transport.local_abspath('repository'))
 
638
        self.assertEqual(relpath, 'bar')
 
639
 
 
640
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
 
641
        self.make_repository('bar')
 
642
        self.build_tree(['bar/baz/'])
 
643
        tree, branch, repo, relpath = \
 
644
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
 
645
                'bar/baz')
 
646
        self.assertEqual(tree, None)
 
647
        self.assertEqual(branch, None)
 
648
        self.assertEqual(
 
649
            osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
 
650
            repo.bzrdir.transport.local_abspath('repository'))
 
651
        self.assertEqual(relpath, 'baz')
 
652
 
450
653
    def test_open_containing_from_transport(self):
451
654
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
452
655
                          get_transport(self.get_readonly_url('')))
461
664
        self.assertEqual('g/p/q', relpath)
462
665
 
463
666
    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))
467
 
 
468
667
        self.make_branch_and_tree('topdir')
469
668
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
470
669
            'topdir/foo')
471
670
        self.assertEqual(os.path.realpath('topdir'),
472
671
                         os.path.realpath(tree.basedir))
473
672
        self.assertEqual(os.path.realpath('topdir'),
474
 
                         local_branch_path(branch))
 
673
                         self.local_branch_path(branch))
475
674
        self.assertIs(tree.bzrdir, branch.bzrdir)
476
675
        self.assertEqual('foo', relpath)
477
676
        # opening from non-local should not return the tree
485
684
            'topdir/foo')
486
685
        self.assertIs(tree, None)
487
686
        self.assertEqual(os.path.realpath('topdir/foo'),
488
 
                         local_branch_path(branch))
 
687
                         self.local_branch_path(branch))
489
688
        self.assertEqual('', relpath)
490
689
 
491
690
    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))
495
 
 
496
691
        self.make_branch_and_tree('topdir')
497
692
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
498
693
        self.assertEqual(os.path.realpath('topdir'),
499
694
                         os.path.realpath(tree.basedir))
500
695
        self.assertEqual(os.path.realpath('topdir'),
501
 
                         local_branch_path(branch))
 
696
                         self.local_branch_path(branch))
502
697
        self.assertIs(tree.bzrdir, branch.bzrdir)
503
698
        # opening from non-local should not return the tree
504
699
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
509
704
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
510
705
        self.assertIs(tree, None)
511
706
        self.assertEqual(os.path.realpath('topdir/foo'),
512
 
                         local_branch_path(branch))
 
707
                         self.local_branch_path(branch))
513
708
 
514
709
    def test_open_from_transport(self):
515
710
        # transport pointing at bzrdir should give a bzrdir with root transport
965
1160
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
966
1161
        self.build_tree(['a'])
967
1162
        self.assertEquals(['a'], self.get_ls())
 
1163
 
 
1164
 
 
1165
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
 
1166
    """Test BzrDirFormat implementation for TestBzrDirSprout."""
 
1167
 
 
1168
    def _open(self, transport):
 
1169
        return _TestBzrDir(transport, self)
 
1170
 
 
1171
 
 
1172
class _TestBzrDir(bzrdir.BzrDirMeta1):
 
1173
    """Test BzrDir implementation for TestBzrDirSprout.
 
1174
    
 
1175
    When created a _TestBzrDir already has repository and a branch.  The branch
 
1176
    is a test double as well.
 
1177
    """
 
1178
 
 
1179
    def __init__(self, *args, **kwargs):
 
1180
        super(_TestBzrDir, self).__init__(*args, **kwargs)
 
1181
        self.test_branch = _TestBranch()
 
1182
        self.test_branch.repository = self.create_repository()
 
1183
 
 
1184
    def open_branch(self, unsupported=False):
 
1185
        return self.test_branch
 
1186
 
 
1187
    def cloning_metadir(self, require_stacking=False):
 
1188
        return _TestBzrDirFormat()
 
1189
 
 
1190
 
 
1191
class _TestBranch(bzrlib.branch.Branch):
 
1192
    """Test Branch implementation for TestBzrDirSprout."""
 
1193
 
 
1194
    def __init__(self, *args, **kwargs):
 
1195
        super(_TestBranch, self).__init__(*args, **kwargs)
 
1196
        self.calls = []
 
1197
        self._parent = None
 
1198
 
 
1199
    def sprout(self, *args, **kwargs):
 
1200
        self.calls.append('sprout')
 
1201
        return _TestBranch()
 
1202
 
 
1203
    def copy_content_into(self, destination, revision_id=None):
 
1204
        self.calls.append('copy_content_into')
 
1205
 
 
1206
    def get_parent(self):
 
1207
        return self._parent
 
1208
 
 
1209
    def set_parent(self, parent):
 
1210
        self._parent = parent
 
1211
 
 
1212
 
 
1213
class TestBzrDirSprout(TestCaseWithMemoryTransport):
 
1214
 
 
1215
    def test_sprout_uses_branch_sprout(self):
 
1216
        """BzrDir.sprout calls Branch.sprout.
 
1217
 
 
1218
        Usually, BzrDir.sprout should delegate to the branch's sprout method
 
1219
        for part of the work.  This allows the source branch to control the
 
1220
        choice of format for the new branch.
 
1221
        
 
1222
        There are exceptions, but this tests avoids them:
 
1223
          - if there's no branch in the source bzrdir,
 
1224
          - or if the stacking has been requested and the format needs to be
 
1225
            overridden to satisfy that.
 
1226
        """
 
1227
        # Make an instrumented bzrdir.
 
1228
        t = self.get_transport('source')
 
1229
        t.ensure_base()
 
1230
        source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
 
1231
        # The instrumented bzrdir has a test_branch attribute that logs calls
 
1232
        # made to the branch contained in that bzrdir.  Initially the test
 
1233
        # branch exists but no calls have been made to it.
 
1234
        self.assertEqual([], source_bzrdir.test_branch.calls)
 
1235
 
 
1236
        # Sprout the bzrdir
 
1237
        target_url = self.get_url('target')
 
1238
        result = source_bzrdir.sprout(target_url, recurse='no')
 
1239
 
 
1240
        # The bzrdir called the branch's sprout method.
 
1241
        self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
 
1242
 
 
1243
    def test_sprout_parent(self):
 
1244
        grandparent_tree = self.make_branch('grandparent')
 
1245
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
 
1246
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
 
1247
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')