~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_bzrdir/test_bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
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
21
21
from itertools import izip
22
22
import os
23
23
from stat import S_ISDIR
 
24
import sys
24
25
 
25
26
import bzrlib.branch
26
27
from bzrlib import (
27
28
    bzrdir,
28
29
    check,
29
 
    controldir,
30
30
    errors,
31
31
    gpg,
 
32
    lockdir,
32
33
    osutils,
 
34
    repository,
33
35
    revision as _mod_revision,
 
36
    transactions,
34
37
    transport,
35
38
    ui,
36
39
    urlutils,
37
40
    workingtree,
38
41
    )
39
 
from bzrlib.errors import (NoSuchRevision,
 
42
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
 
43
from bzrlib.errors import (FileExists,
 
44
                           NoSuchRevision,
 
45
                           NoSuchFile,
 
46
                           UninitializableFormat,
40
47
                           NotBranchError,
41
48
                           )
42
49
import bzrlib.revision
43
50
from bzrlib.tests import (
44
51
                          ChrootedTestCase,
 
52
                          TestCase,
 
53
                          TestCaseWithTransport,
45
54
                          TestNotApplicable,
46
55
                          TestSkipped,
47
56
                          )
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,
52
63
    )
 
64
from bzrlib.upgrade import upgrade
53
65
from bzrlib.remote import RemoteBzrDir, RemoteRepository
54
66
from bzrlib.repofmt import weaverepo
55
67
 
56
68
 
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.
193
205
        """
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,
236
248
        try:
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()
255
267
 
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.
259
271
        """
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.
265
277
            return
266
278
        self.assertRaises(errors.NoWorkingTree, dir.open_workingtree)
267
279
 
512
524
        dir = self.make_bzrdir('source')
513
525
        try:
514
526
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
515
 
                target_branch=referenced_branch)
 
527
                referenced_branch)
516
528
        except errors.IncompatibleFormat:
517
529
            # this is ok too, not all formats have to support references.
518
530
            return
615
627
        dir = self.make_bzrdir('source')
616
628
        try:
617
629
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
618
 
                target_branch=referenced_branch)
 
630
                referenced_branch)
619
631
        except errors.IncompatibleFormat:
620
632
            # this is ok too, not all formats have to support references.
621
633
            return
680
692
        dir = self.make_bzrdir('source')
681
693
        try:
682
694
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
683
 
                target_branch=referenced_branch)
 
695
                referenced_branch)
684
696
        except errors.IncompatibleFormat:
685
697
            # this is ok too, not all formats have to support references.
686
698
            return
955
967
        dir = self.make_bzrdir('source')
956
968
        try:
957
969
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
958
 
                target_branch=referenced_branch)
 
970
                referenced_branch)
959
971
        except errors.IncompatibleFormat:
960
972
            # this is ok too, not all formats have to support references.
961
973
            return
975
987
        dir = self.make_bzrdir('source')
976
988
        try:
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.
981
993
            return
1001
1013
        dir = self.make_bzrdir('source')
1002
1014
        try:
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.
1007
1019
            return
1066
1078
        dir = self.make_bzrdir('source')
1067
1079
        try:
1068
1080
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1069
 
                target_branch=referenced_branch)
 
1081
                referenced_branch)
1070
1082
        except errors.IncompatibleFormat:
1071
1083
            # this is ok too, not all formats have to support references.
1072
1084
            return
1092
1104
        dir = self.make_bzrdir('source')
1093
1105
        try:
1094
1106
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1095
 
                target_branch=referenced_branch)
 
1107
                referenced_branch)
1096
1108
        except errors.IncompatibleFormat:
1097
1109
            # this is ok too, not all formats have to support references.
1098
1110
            return
1157
1169
        self.assertEqual(tree.branch.last_revision(),
1158
1170
                         target.open_branch().last_revision())
1159
1171
 
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()
1171
 
        try:
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'))
1182
 
 
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),
1196
1185
            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))
1211
1200
 
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()))
1410
1399
 
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.
1417
1406
            return
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.
1430
1419
            return
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__))
1439
1428
 
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.
1445
 
            return
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)
1453
 
        try:
1454
 
            made_control.destroy_branch()
1455
 
        except errors.UnsupportedOperation:
1456
 
            pass # Not all bzrdirs support destroying directories
1457
 
        else:
1458
 
            self.assertEquals([], made_control.list_branches())
1459
 
 
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.
1466
1435
            return
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.
1481
1450
            return
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)
1484
1453
        try:
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.
1498
1467
            return
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.
1509
1478
            return
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)
1639
1608
 
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.
1647
1616
            return
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.
1672
1641
            return
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)
1676
1645
        try:
1677
1646
            child_repo = made_control.open_repository()
1705
1674
            # need a shared repository to test this.
1706
1675
            return
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.
1725
1694
            return
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)
1730
1699
        try:
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())
1761
1730
 
1762
1731
    def test_backup_copies_existing(self):
1763
1732
        tree = self.make_branch_and_tree('test')
1826
1795
            bd.retire_bzrdir, limit=0)
1827
1796
 
1828
1797
 
1829
 
class TestBreakLock(TestCaseWithControlDir):
 
1798
class TestBreakLock(TestCaseWithBzrDir):
1830
1799
 
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')
1871
1840
        try:
1872
1841
            bzrlib.branch.BranchReferenceFormat().initialize(
1873
 
                thisdir, target_branch=master)
 
1842
                thisdir, master)
1874
1843
        except errors.IncompatibleFormat:
1875
1844
            return
1876
1845
        unused_repo = thisdir.create_repository()
1934
1903
        self.assertRaises(errors.LockBroken, tree.unlock)
1935
1904
 
1936
1905
 
1937
 
class TestTransportConfig(TestCaseWithControlDir):
 
1906
class TestTransportConfig(TestCaseWithBzrDir):
1938
1907
 
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())
1957
1926
 
1958
1927
 
1959
 
class ChrootedControlDirTests(ChrootedTestCase):
 
1928
class ChrootedBzrDirTests(ChrootedTestCase):
1960
1929
 
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'))
1975
1944
        try:
1976
1945
            repo = made_control.open_repository()
1983
1952
        self.assertRaises(errors.NoRepositoryPresent,
1984
1953
                          made_control.find_repository)
1985
1954
 
1986
 
 
1987
 
class TestControlDirControlComponent(TestCaseWithControlDir):
1988
 
    """ControlDir implementations adequately implement ControlComponent."""
1989
 
 
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)