~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the BzrDir facility and any format specific tests.
18
18
 
59
59
from bzrlib.transport.memory import MemoryServer
60
60
from bzrlib.transport.nosmart import NoSmartTransportDecorator
61
61
from bzrlib.transport.readonly import ReadonlyTransportDecorator
62
 
from bzrlib.repofmt import knitrepo, weaverepo
 
62
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
63
63
 
64
64
 
65
65
class TestDefaultFormat(TestCase):
85
85
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
86
86
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
87
87
            ' repositories', deprecated=True)
88
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
 
88
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
89
89
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
90
90
        my_format_registry.register_metadir('knit',
91
91
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
118
118
        my_bzrdir = my_format_registry.make_bzrdir('weave')
119
119
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
120
120
        my_bzrdir = my_format_registry.make_bzrdir('default')
121
 
        self.assertIsInstance(my_bzrdir.repository_format, 
 
121
        self.assertIsInstance(my_bzrdir.repository_format,
122
122
            knitrepo.RepositoryFormatKnit1)
123
123
        my_bzrdir = my_format_registry.make_bzrdir('knit')
124
 
        self.assertIsInstance(my_bzrdir.repository_format, 
 
124
        self.assertIsInstance(my_bzrdir.repository_format,
125
125
            knitrepo.RepositoryFormatKnit1)
126
126
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
127
127
        self.assertIsInstance(my_bzrdir.get_branch_format(),
131
131
        my_format_registry = self.make_format_registry()
132
132
        self.assertEqual('Format registered lazily',
133
133
                         my_format_registry.get_help('lazy'))
134
 
        self.assertEqual('Format using knits', 
 
134
        self.assertEqual('Format using knits',
135
135
                         my_format_registry.get_help('knit'))
136
 
        self.assertEqual('Format using knits', 
 
136
        self.assertEqual('Format using knits',
137
137
                         my_format_registry.get_help('default'))
138
138
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
139
 
                         ' checkouts or shared repositories', 
 
139
                         ' checkouts or shared repositories',
140
140
                         my_format_registry.get_help('weave'))
141
 
        
 
141
 
142
142
    def test_help_topic(self):
143
143
        topics = help_topics.HelpTopicRegistry()
144
144
        registry = self.make_format_registry()
145
 
        topics.register('current-formats', registry.help_topic, 
 
145
        topics.register('current-formats', registry.help_topic,
146
146
                        'Current formats')
147
 
        topics.register('other-formats', registry.help_topic, 
 
147
        topics.register('other-formats', registry.help_topic,
148
148
                        'Other formats')
149
149
        new = topics.get_detail('current-formats')
150
150
        rest = topics.get_detail('other-formats')
151
151
        experimental, deprecated = rest.split('Deprecated formats')
152
152
        self.assertContainsRe(new, 'bzr help formats')
153
 
        self.assertContainsRe(new, 
 
153
        self.assertContainsRe(new,
154
154
                ':knit:\n    \(native\) \(default\) Format using knits\n')
155
 
        self.assertContainsRe(experimental, 
 
155
        self.assertContainsRe(experimental,
156
156
                ':branch6:\n    \(native\) Experimental successor to knit')
157
 
        self.assertContainsRe(deprecated, 
 
157
        self.assertContainsRe(deprecated,
158
158
                ':lazy:\n    \(native\) Format registered lazily\n')
159
159
        self.assertNotContainsRe(new, 'hidden')
160
160
 
220
220
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
221
221
    """A sample format
222
222
 
223
 
    this format is initializable, unsupported to aid in testing the 
 
223
    this format is initializable, unsupported to aid in testing the
224
224
    open and open_downlevel routines.
225
225
    """
226
226
 
247
247
    def test_find_format(self):
248
248
        # is the right format object found for a branch?
249
249
        # create a branch with a few known format objects.
250
 
        # this is not quite the same as 
 
250
        # this is not quite the same as
251
251
        t = get_transport(self.get_url())
252
252
        self.build_tree(["foo/", "bar/"], transport=t)
253
253
        def check_format(format, url):
257
257
            self.failUnless(isinstance(found_format, format.__class__))
258
258
        check_format(bzrdir.BzrDirFormat5(), "foo")
259
259
        check_format(bzrdir.BzrDirFormat6(), "bar")
260
 
        
 
260
 
261
261
    def test_find_format_nothing_there(self):
262
262
        self.assertRaises(NotBranchError,
263
263
                          bzrdir.BzrDirFormat.find_format,
307
307
                          branch.bzrdir.open_repository)
308
308
 
309
309
    def test_create_branch_and_repo_under_shared_force_new(self):
310
 
        # creating a branch and repo in a shared repo can be forced to 
 
310
        # creating a branch and repo in a shared repo can be forced to
311
311
        # make a new repo
312
312
        format = bzrdir.format_registry.make_bzrdir('knit')
313
313
        self.make_repository('.', shared=True, format=format)
318
318
 
319
319
    def test_create_standalone_working_tree(self):
320
320
        format = SampleBzrDirFormat()
321
 
        # note this is deliberately readonly, as this failure should 
 
321
        # note this is deliberately readonly, as this failure should
322
322
        # occur before any writes.
323
323
        self.assertRaises(errors.NotLocalUrl,
324
324
                          bzrdir.BzrDir.create_standalone_workingtree,
325
325
                          self.get_readonly_url(), format=format)
326
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
 
326
        tree = bzrdir.BzrDir.create_standalone_workingtree('.',
327
327
                                                           format=format)
328
328
        self.assertEqual('A tree', tree)
329
329
 
331
331
        # create standalone working tree always makes a repo.
332
332
        format = bzrdir.format_registry.make_bzrdir('knit')
333
333
        self.make_repository('.', shared=True, format=format)
334
 
        # note this is deliberately readonly, as this failure should 
 
334
        # note this is deliberately readonly, as this failure should
335
335
        # occur before any writes.
336
336
        self.assertRaises(errors.NotLocalUrl,
337
337
                          bzrdir.BzrDir.create_standalone_workingtree,
338
338
                          self.get_readonly_url('child'), format=format)
339
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
 
339
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
340
340
            format=format)
341
341
        tree.bzrdir.open_repository()
342
342
 
361
361
        self.vfs_transport_factory = MemoryServer
362
362
        # outside a repo the default convenience output is a repo+branch_tree
363
363
        format = bzrdir.format_registry.make_bzrdir('knit')
364
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
 
364
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
365
365
                                                         format=format)
366
366
        self.assertRaises(errors.NoWorkingTree,
367
367
                          branch.bzrdir.open_workingtree)
377
377
        branch.bzrdir.open_workingtree()
378
378
        self.assertRaises(errors.NoRepositoryPresent,
379
379
                          branch.bzrdir.open_repository)
380
 
            
 
380
 
381
381
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
382
382
        # inside a repo the default convenience output is a branch+ follow the
383
383
        # repo tree policy but we can override that
389
389
                          branch.bzrdir.open_workingtree)
390
390
        self.assertRaises(errors.NoRepositoryPresent,
391
391
                          branch.bzrdir.open_repository)
392
 
            
 
392
 
393
393
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
394
394
        # inside a repo the default convenience output is a branch+ follow the
395
395
        # repo tree policy
396
396
        format = bzrdir.format_registry.make_bzrdir('knit')
397
397
        repo = self.make_repository('.', shared=True, format=format)
398
398
        repo.set_make_working_trees(False)
399
 
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
 
399
        branch = bzrdir.BzrDir.create_branch_convenience('child',
400
400
                                                         format=format)
401
401
        self.assertRaises(errors.NoWorkingTree,
402
402
                          branch.bzrdir.open_workingtree)
432
432
        """The default acquisition policy should create a standalone branch."""
433
433
        my_bzrdir = self.make_bzrdir('.')
434
434
        repo_policy = my_bzrdir.determine_repository_policy()
435
 
        repo = repo_policy.acquire_repository()
 
435
        repo, is_new = repo_policy.acquire_repository()
436
436
        self.assertEqual(repo.bzrdir.root_transport.base,
437
437
                         my_bzrdir.root_transport.base)
438
438
        self.assertFalse(repo.is_shared())
439
439
 
440
 
 
441
440
    def test_determine_stacking_policy(self):
442
441
        parent_bzrdir = self.make_bzrdir('.')
443
442
        child_bzrdir = self.make_bzrdir('child')
467
466
        self.assertEqual(child_branch.base,
468
467
                         new_child.open_branch().get_stacked_on_url())
469
468
 
 
469
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
 
470
        # Make stackable source branch with an unstackable repo format.
 
471
        source_bzrdir = self.make_bzrdir('source')
 
472
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
473
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(source_bzrdir)
 
474
        # Make a directory with a default stacking policy
 
475
        parent_bzrdir = self.make_bzrdir('parent')
 
476
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
 
477
        parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
 
478
        # Clone source into directory
 
479
        target = source_bzrdir.clone(self.get_url('parent/target'))
 
480
 
470
481
    def test_sprout_obeys_stacking_policy(self):
471
482
        child_branch, new_child_transport = self.prepare_default_stacking()
472
483
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
726
737
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
727
738
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
728
739
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
729
 
        
 
740
 
730
741
    def test_open_from_transport_no_bzrdir(self):
731
742
        transport = get_transport(self.get_url())
732
743
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
741
752
                          transport)
742
753
 
743
754
    def test_sprout_recursive(self):
744
 
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
 
755
        tree = self.make_branch_and_tree('tree1',
 
756
                                         format='dirstate-with-subtree')
745
757
        sub_tree = self.make_branch_and_tree('tree1/subtree',
746
758
            format='dirstate-with-subtree')
 
759
        sub_tree.set_root_id('subtree-root')
747
760
        tree.add_reference(sub_tree)
748
761
        self.build_tree(['tree1/subtree/file'])
749
762
        sub_tree.add('file')
750
763
        tree.commit('Initial commit')
751
 
        tree.bzrdir.sprout('tree2')
 
764
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
 
765
        tree2.lock_read()
 
766
        self.addCleanup(tree2.unlock)
752
767
        self.failUnlessExists('tree2/subtree/file')
 
768
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
753
769
 
754
770
    def test_cloning_metadir(self):
755
771
        """Ensure that cloning metadir is suitable"""
907
923
    """Tests specific to the version 5 bzrdir format."""
908
924
 
909
925
    def test_same_lockfiles_between_tree_repo_branch(self):
910
 
        # this checks that only a single lockfiles instance is created 
 
926
        # this checks that only a single lockfiles instance is created
911
927
        # for format 5 objects
912
928
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
913
929
        def check_dir_components_use_same_lock(dir):
920
936
        # and if we open it normally.
921
937
        dir = bzrdir.BzrDir.open(self.get_url())
922
938
        check_dir_components_use_same_lock(dir)
923
 
    
 
939
 
924
940
    def test_can_convert(self):
925
941
        # format 5 dirs are convertable
926
942
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
927
943
        self.assertTrue(dir.can_convert_format())
928
 
    
 
944
 
929
945
    def test_needs_conversion(self):
930
946
        # format 5 dirs need a conversion if they are not the default,
931
947
        # and they aren't
941
957
    """Tests specific to the version 6 bzrdir format."""
942
958
 
943
959
    def test_same_lockfiles_between_tree_repo_branch(self):
944
 
        # this checks that only a single lockfiles instance is created 
 
960
        # this checks that only a single lockfiles instance is created
945
961
        # for format 6 objects
946
962
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
947
963
        def check_dir_components_use_same_lock(dir):
954
970
        # and if we open it normally.
955
971
        dir = bzrdir.BzrDir.open(self.get_url())
956
972
        check_dir_components_use_same_lock(dir)
957
 
    
 
973
 
958
974
    def test_can_convert(self):
959
975
        # format 6 dirs are convertable
960
976
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
961
977
        self.assertTrue(dir.can_convert_format())
962
 
    
 
978
 
963
979
    def test_needs_conversion(self):
964
980
        # format 6 dirs need an conversion if they are not the default.
965
981
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1001
1017
 
1002
1018
class TestNotBzrDir(TestCaseWithTransport):
1003
1019
    """Tests for using the bzrdir api with a non .bzr based disk format.
1004
 
    
 
1020
 
1005
1021
    If/when one of these is in the core, we can let the implementation tests
1006
1022
    verify this works.
1007
1023
    """
1008
1024
 
1009
1025
    def test_create_and_find_format(self):
1010
 
        # create a .notbzr dir 
 
1026
        # create a .notbzr dir
1011
1027
        format = NotBzrDirFormat()
1012
1028
        dir = format.initialize(self.get_url())
1013
1029
        self.assertIsInstance(dir, NotBzrDir)
1038
1054
    def setUp(self):
1039
1055
        super(NonLocalTests, self).setUp()
1040
1056
        self.vfs_transport_factory = MemoryServer
1041
 
    
 
1057
 
1042
1058
    def test_create_branch_convenience(self):
1043
1059
        # outside a repo the default convenience output is a repo+branch_tree
1044
1060
        format = bzrdir.format_registry.make_bzrdir('knit')
1089
1105
 
1090
1106
    We can't inherit directly from TestCaseWithTwoWebservers or the
1091
1107
    test framework will try to create an instance which cannot
1092
 
    run, its implementation being incomplete. 
 
1108
    run, its implementation being incomplete.
1093
1109
    """
1094
1110
 
1095
1111
    def create_transport_readonly_server(self):
1211
1227
 
1212
1228
class _TestBzrDir(bzrdir.BzrDirMeta1):
1213
1229
    """Test BzrDir implementation for TestBzrDirSprout.
1214
 
    
 
1230
 
1215
1231
    When created a _TestBzrDir already has repository and a branch.  The branch
1216
1232
    is a test double as well.
1217
1233
    """
1228
1244
        return _TestBzrDirFormat()
1229
1245
 
1230
1246
 
 
1247
class _TestBranchFormat(bzrlib.branch.BranchFormat):
 
1248
    """Test Branch format for TestBzrDirSprout."""
 
1249
 
 
1250
 
1231
1251
class _TestBranch(bzrlib.branch.Branch):
1232
1252
    """Test Branch implementation for TestBzrDirSprout."""
1233
1253
 
1234
1254
    def __init__(self, *args, **kwargs):
 
1255
        self._format = _TestBranchFormat()
1235
1256
        super(_TestBranch, self).__init__(*args, **kwargs)
1236
1257
        self.calls = []
1237
1258
        self._parent = None
1258
1279
        Usually, BzrDir.sprout should delegate to the branch's sprout method
1259
1280
        for part of the work.  This allows the source branch to control the
1260
1281
        choice of format for the new branch.
1261
 
        
 
1282
 
1262
1283
        There are exceptions, but this tests avoids them:
1263
1284
          - if there's no branch in the source bzrdir,
1264
1285
          - or if the stacking has been requested and the format needs to be
1285
1306
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1286
1307
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1287
1308
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
 
1309
 
 
1310
 
 
1311
class TestBzrDirHooks(TestCaseWithMemoryTransport):
 
1312
 
 
1313
    def test_pre_open_called(self):
 
1314
        calls = []
 
1315
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
 
1316
        transport = self.get_transport('foo')
 
1317
        url = transport.base
 
1318
        self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
 
1319
        self.assertEqual([transport.base], [t.base for t in calls])
 
1320
 
 
1321
    def test_pre_open_actual_exceptions_raised(self):
 
1322
        count = [0]
 
1323
        def fail_once(transport):
 
1324
            count[0] += 1
 
1325
            if count[0] == 1:
 
1326
                raise errors.BzrError("fail")
 
1327
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
 
1328
        transport = self.get_transport('foo')
 
1329
        url = transport.base
 
1330
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
 
1331
        self.assertEqual('fail', err._preformatted_string)