~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-08-24 23:20:14 UTC
  • mfrom: (5365.5.29 2.3-btree-chk-leaf)
  • Revision ID: pqm@pqm.ubuntu.com-20100824232014-nu9owzel2zym2jk2
(jam) Use a custom C type for CHK index entries, saves memory

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2013, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
26
26
from bzrlib import (
27
27
    branch,
28
28
    bzrdir,
29
 
    config,
30
29
    controldir,
31
30
    errors,
32
31
    help_topics,
33
 
    lock,
34
32
    repository,
35
 
    revision as _mod_revision,
36
33
    osutils,
37
34
    remote,
38
 
    transport as _mod_transport,
39
35
    urlutils,
40
36
    win32utils,
41
 
    workingtree_3,
42
 
    workingtree_4,
 
37
    workingtree,
43
38
    )
44
39
import bzrlib.branch
45
 
from bzrlib.branchfmt.fullhistory import BzrBranchFormat5
46
 
from bzrlib.errors import (
47
 
    NotBranchError,
48
 
    NoColocatedBranchSupport,
49
 
    UnknownFormatError,
50
 
    UnsupportedFormatError,
51
 
    )
 
40
from bzrlib.errors import (NotBranchError,
 
41
                           NoColocatedBranchSupport,
 
42
                           UnknownFormatError,
 
43
                           UnsupportedFormatError,
 
44
                           )
52
45
from bzrlib.tests import (
53
46
    TestCase,
54
47
    TestCaseWithMemoryTransport,
61
54
    )
62
55
from bzrlib.tests.test_http import TestWithTransport_pycurl
63
56
from bzrlib.transport import (
 
57
    get_transport,
64
58
    memory,
65
59
    pathfilter,
66
60
    )
67
61
from bzrlib.transport.http._urllib import HttpTransport_urllib
68
62
from bzrlib.transport.nosmart import NoSmartTransportDecorator
69
63
from bzrlib.transport.readonly import ReadonlyTransportDecorator
70
 
from bzrlib.repofmt import knitrepo, knitpack_repo
 
64
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
71
65
 
72
66
 
73
67
class TestDefaultFormat(TestCase):
74
68
 
75
69
    def test_get_set_default_format(self):
76
70
        old_format = bzrdir.BzrDirFormat.get_default_format()
77
 
        # default is BzrDirMetaFormat1
78
 
        self.assertIsInstance(old_format, bzrdir.BzrDirMetaFormat1)
 
71
        # default is BzrDirFormat6
 
72
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
79
73
        controldir.ControlDirFormat._set_default_format(SampleBzrDirFormat())
80
74
        # creating a bzr dir should now create an instrumented dir.
81
75
        try:
82
76
            result = bzrdir.BzrDir.create('memory:///')
83
 
            self.assertIsInstance(result, SampleBzrDir)
 
77
            self.failUnless(isinstance(result, SampleBzrDir))
84
78
        finally:
85
79
            controldir.ControlDirFormat._set_default_format(old_format)
86
80
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
87
81
 
88
82
 
89
 
class DeprecatedBzrDirFormat(bzrdir.BzrDirFormat):
90
 
    """A deprecated bzr dir format."""
91
 
 
92
 
 
93
83
class TestFormatRegistry(TestCase):
94
84
 
95
85
    def make_format_registry(self):
96
86
        my_format_registry = controldir.ControlDirFormatRegistry()
97
 
        my_format_registry.register('deprecated', DeprecatedBzrDirFormat,
98
 
            'Some format.  Slower and unawesome and deprecated.',
99
 
            deprecated=True)
100
 
        my_format_registry.register_lazy('lazy', 'bzrlib.tests.test_bzrdir',
101
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
102
 
            deprecated=True)
 
87
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
88
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
89
            ' repositories', deprecated=True)
 
90
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
 
91
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
103
92
        bzrdir.register_metadir(my_format_registry, 'knit',
104
93
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
105
94
            'Format using knits',
116
105
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
117
106
            'Experimental successor to knit.  Use at your own risk.',
118
107
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
119
 
        my_format_registry.register('hiddendeprecated', DeprecatedBzrDirFormat,
120
 
            'Old format.  Slower and does not support things. ', hidden=True)
121
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.tests.test_bzrdir',
122
 
            'DeprecatedBzrDirFormat', 'Format registered lazily',
123
 
            deprecated=True, hidden=True)
 
108
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
109
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
110
            ' repositories', hidden=True)
 
111
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
112
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
113
            hidden=True)
124
114
        return my_format_registry
125
115
 
126
116
    def test_format_registry(self):
127
117
        my_format_registry = self.make_format_registry()
128
118
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
129
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
130
 
        my_bzrdir = my_format_registry.make_bzrdir('deprecated')
131
 
        self.assertIsInstance(my_bzrdir, DeprecatedBzrDirFormat)
 
119
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
120
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
121
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
132
122
        my_bzrdir = my_format_registry.make_bzrdir('default')
133
123
        self.assertIsInstance(my_bzrdir.repository_format,
134
124
            knitrepo.RepositoryFormatKnit1)
147
137
                         my_format_registry.get_help('knit'))
148
138
        self.assertEqual('Format using knits',
149
139
                         my_format_registry.get_help('default'))
150
 
        self.assertEqual('Some format.  Slower and unawesome and deprecated.',
151
 
                         my_format_registry.get_help('deprecated'))
 
140
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
141
                         ' checkouts or shared repositories',
 
142
                         my_format_registry.get_help('weave'))
152
143
 
153
144
    def test_help_topic(self):
154
145
        topics = help_topics.HelpTopicRegistry()
170
161
        self.assertNotContainsRe(new, 'hidden')
171
162
 
172
163
    def test_set_default_repository(self):
173
 
        default_factory = controldir.format_registry.get('default')
174
 
        old_default = [k for k, v in controldir.format_registry.iteritems()
 
164
        default_factory = bzrdir.format_registry.get('default')
 
165
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
175
166
                       if v == default_factory and k != 'default'][0]
176
 
        controldir.format_registry.set_default_repository('dirstate-with-subtree')
 
167
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
177
168
        try:
178
 
            self.assertIs(controldir.format_registry.get('dirstate-with-subtree'),
179
 
                          controldir.format_registry.get('default'))
 
169
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
 
170
                          bzrdir.format_registry.get('default'))
180
171
            self.assertIs(
181
 
                repository.format_registry.get_default().__class__,
 
172
                repository.RepositoryFormat.get_default_format().__class__,
182
173
                knitrepo.RepositoryFormatKnit3)
183
174
        finally:
184
 
            controldir.format_registry.set_default_repository(old_default)
 
175
            bzrdir.format_registry.set_default_repository(old_default)
185
176
 
186
177
    def test_aliases(self):
187
178
        a_registry = controldir.ControlDirFormatRegistry()
188
 
        a_registry.register('deprecated', DeprecatedBzrDirFormat,
189
 
            'Old format.  Slower and does not support stuff',
190
 
            deprecated=True)
191
 
        a_registry.register('deprecatedalias', DeprecatedBzrDirFormat,
192
 
            'Old format.  Slower and does not support stuff',
193
 
            deprecated=True, alias=True)
194
 
        self.assertEqual(frozenset(['deprecatedalias']), a_registry.aliases())
 
179
        a_registry.register('weave', bzrdir.BzrDirFormat6,
 
180
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
181
            ' repositories', deprecated=True)
 
182
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
 
183
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
184
            ' repositories', deprecated=True, alias=True)
 
185
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
195
186
 
196
187
 
197
188
class SampleBranch(bzrlib.branch.Branch):
212
203
    """A sample BzrDir implementation to allow testing static methods."""
213
204
 
214
205
    def create_repository(self, shared=False):
215
 
        """See ControlDir.create_repository."""
 
206
        """See BzrDir.create_repository."""
216
207
        return "A repository"
217
208
 
218
209
    def open_repository(self):
219
 
        """See ControlDir.open_repository."""
 
210
        """See BzrDir.open_repository."""
220
211
        return SampleRepository(self)
221
212
 
222
213
    def create_branch(self, name=None):
223
 
        """See ControlDir.create_branch."""
 
214
        """See BzrDir.create_branch."""
224
215
        if name is not None:
225
216
            raise NoColocatedBranchSupport(self)
226
217
        return SampleBranch(self)
227
218
 
228
219
    def create_workingtree(self):
229
 
        """See ControlDir.create_workingtree."""
 
220
        """See BzrDir.create_workingtree."""
230
221
        return "A tree"
231
222
 
232
223
 
253
244
    def open(self, transport, _found=None):
254
245
        return "opened branch."
255
246
 
256
 
    @classmethod
257
 
    def from_string(cls, format_string):
258
 
        return cls()
259
 
 
260
 
 
261
 
class BzrDirFormatTest1(bzrdir.BzrDirMetaFormat1):
262
 
 
263
 
    @staticmethod
264
 
    def get_format_string():
265
 
        return "Test format 1"
266
 
 
267
 
 
268
 
class BzrDirFormatTest2(bzrdir.BzrDirMetaFormat1):
269
 
 
270
 
    @staticmethod
271
 
    def get_format_string():
272
 
        return "Test format 2"
273
 
 
274
247
 
275
248
class TestBzrDirFormat(TestCaseWithTransport):
276
249
    """Tests for the BzrDirFormat facility."""
278
251
    def test_find_format(self):
279
252
        # is the right format object found for a branch?
280
253
        # create a branch with a few known format objects.
281
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest1.get_format_string(),
282
 
            BzrDirFormatTest1())
283
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
284
 
            BzrDirFormatTest1.get_format_string())
285
 
        bzrdir.BzrProber.formats.register(BzrDirFormatTest2.get_format_string(),
286
 
            BzrDirFormatTest2())
287
 
        self.addCleanup(bzrdir.BzrProber.formats.remove,
288
 
            BzrDirFormatTest2.get_format_string())
289
 
        t = self.get_transport()
 
254
        # this is not quite the same as
 
255
        t = get_transport(self.get_url())
290
256
        self.build_tree(["foo/", "bar/"], transport=t)
291
257
        def check_format(format, url):
292
258
            format.initialize(url)
293
 
            t = _mod_transport.get_transport_from_path(url)
 
259
            t = get_transport(url)
294
260
            found_format = bzrdir.BzrDirFormat.find_format(t)
295
 
            self.assertIsInstance(found_format, format.__class__)
296
 
        check_format(BzrDirFormatTest1(), "foo")
297
 
        check_format(BzrDirFormatTest2(), "bar")
 
261
            self.failUnless(isinstance(found_format, format.__class__))
 
262
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
263
        check_format(bzrdir.BzrDirFormat6(), "bar")
298
264
 
299
265
    def test_find_format_nothing_there(self):
300
266
        self.assertRaises(NotBranchError,
301
267
                          bzrdir.BzrDirFormat.find_format,
302
 
                          _mod_transport.get_transport_from_path('.'))
 
268
                          get_transport('.'))
303
269
 
304
270
    def test_find_format_unknown_format(self):
305
 
        t = self.get_transport()
 
271
        t = get_transport(self.get_url())
306
272
        t.mkdir('.bzr')
307
273
        t.put_bytes('.bzr/branch-format', '')
308
274
        self.assertRaises(UnknownFormatError,
309
275
                          bzrdir.BzrDirFormat.find_format,
310
 
                          _mod_transport.get_transport_from_path('.'))
 
276
                          get_transport('.'))
311
277
 
312
278
    def test_register_unregister_format(self):
313
279
        format = SampleBzrDirFormat()
315
281
        # make a bzrdir
316
282
        format.initialize(url)
317
283
        # register a format for it.
318
 
        bzrdir.BzrProber.formats.register(format.get_format_string(), format)
 
284
        bzrdir.BzrDirFormat.register_format(format)
319
285
        # which bzrdir.Open will refuse (not supported)
320
286
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
321
287
        # which bzrdir.open_containing will refuse (not supported)
322
288
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
323
289
        # but open_downlevel will work
324
 
        t = _mod_transport.get_transport_from_url(url)
 
290
        t = get_transport(url)
325
291
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
326
292
        # unregister the format
327
 
        bzrdir.BzrProber.formats.remove(format.get_format_string())
 
293
        bzrdir.BzrDirFormat.unregister_format(format)
328
294
        # now open_downlevel should fail too.
329
295
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
330
296
 
337
303
    def test_create_branch_and_repo_under_shared(self):
338
304
        # creating a branch and repo in a shared repo uses the
339
305
        # shared repository
340
 
        format = controldir.format_registry.make_bzrdir('knit')
 
306
        format = bzrdir.format_registry.make_bzrdir('knit')
341
307
        self.make_repository('.', shared=True, format=format)
342
308
        branch = bzrdir.BzrDir.create_branch_and_repo(
343
309
            self.get_url('child'), format=format)
347
313
    def test_create_branch_and_repo_under_shared_force_new(self):
348
314
        # creating a branch and repo in a shared repo can be forced to
349
315
        # make a new repo
350
 
        format = controldir.format_registry.make_bzrdir('knit')
 
316
        format = bzrdir.format_registry.make_bzrdir('knit')
351
317
        self.make_repository('.', shared=True, format=format)
352
318
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
353
319
                                                      force_new_repo=True,
367
333
 
368
334
    def test_create_standalone_working_tree_under_shared_repo(self):
369
335
        # create standalone working tree always makes a repo.
370
 
        format = controldir.format_registry.make_bzrdir('knit')
 
336
        format = bzrdir.format_registry.make_bzrdir('knit')
371
337
        self.make_repository('.', shared=True, format=format)
372
338
        # note this is deliberately readonly, as this failure should
373
339
        # occur before any writes.
380
346
 
381
347
    def test_create_branch_convenience(self):
382
348
        # outside a repo the default convenience output is a repo+branch_tree
383
 
        format = controldir.format_registry.make_bzrdir('knit')
 
349
        format = bzrdir.format_registry.make_bzrdir('knit')
384
350
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
385
351
        branch.bzrdir.open_workingtree()
386
352
        branch.bzrdir.open_repository()
387
353
 
388
354
    def test_create_branch_convenience_possible_transports(self):
389
355
        """Check that the optional 'possible_transports' is recognized"""
390
 
        format = controldir.format_registry.make_bzrdir('knit')
 
356
        format = bzrdir.format_registry.make_bzrdir('knit')
391
357
        t = self.get_transport()
392
358
        branch = bzrdir.BzrDir.create_branch_convenience(
393
359
            '.', format=format, possible_transports=[t])
398
364
        """Creating a branch at the root of a fs should work."""
399
365
        self.vfs_transport_factory = memory.MemoryServer
400
366
        # outside a repo the default convenience output is a repo+branch_tree
401
 
        format = controldir.format_registry.make_bzrdir('knit')
 
367
        format = bzrdir.format_registry.make_bzrdir('knit')
402
368
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
403
369
                                                         format=format)
404
370
        self.assertRaises(errors.NoWorkingTree,
408
374
    def test_create_branch_convenience_under_shared_repo(self):
409
375
        # inside a repo the default convenience output is a branch+ follow the
410
376
        # repo tree policy
411
 
        format = controldir.format_registry.make_bzrdir('knit')
 
377
        format = bzrdir.format_registry.make_bzrdir('knit')
412
378
        self.make_repository('.', shared=True, format=format)
413
379
        branch = bzrdir.BzrDir.create_branch_convenience('child',
414
380
            format=format)
419
385
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
420
386
        # inside a repo the default convenience output is a branch+ follow the
421
387
        # repo tree policy but we can override that
422
 
        format = controldir.format_registry.make_bzrdir('knit')
 
388
        format = bzrdir.format_registry.make_bzrdir('knit')
423
389
        self.make_repository('.', shared=True, format=format)
424
390
        branch = bzrdir.BzrDir.create_branch_convenience('child',
425
391
            force_new_tree=False, format=format)
431
397
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
432
398
        # inside a repo the default convenience output is a branch+ follow the
433
399
        # repo tree policy
434
 
        format = controldir.format_registry.make_bzrdir('knit')
 
400
        format = bzrdir.format_registry.make_bzrdir('knit')
435
401
        repo = self.make_repository('.', shared=True, format=format)
436
402
        repo.set_make_working_trees(False)
437
403
        branch = bzrdir.BzrDir.create_branch_convenience('child',
444
410
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
445
411
        # inside a repo the default convenience output is a branch+ follow the
446
412
        # repo tree policy but we can override that
447
 
        format = controldir.format_registry.make_bzrdir('knit')
 
413
        format = bzrdir.format_registry.make_bzrdir('knit')
448
414
        repo = self.make_repository('.', shared=True, format=format)
449
415
        repo.set_make_working_trees(False)
450
416
        branch = bzrdir.BzrDir.create_branch_convenience('child',
456
422
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
457
423
        # inside a repo the default convenience output is overridable to give
458
424
        # repo+branch+tree
459
 
        format = controldir.format_registry.make_bzrdir('knit')
 
425
        format = bzrdir.format_registry.make_bzrdir('knit')
460
426
        self.make_repository('.', shared=True, format=format)
461
427
        branch = bzrdir.BzrDir.create_branch_convenience('child',
462
428
            force_new_repo=True, format=format)
507
473
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
508
474
        # Make stackable source branch with an unstackable repo format.
509
475
        source_bzrdir = self.make_bzrdir('source')
510
 
        knitpack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
 
476
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
511
477
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
512
478
            source_bzrdir)
513
479
        # Make a directory with a default stacking policy
517
483
        # Clone source into directory
518
484
        target = source_bzrdir.clone(self.get_url('parent/target'))
519
485
 
520
 
    def test_format_initialize_on_transport_ex_stacked_on(self):
521
 
        # trunk is a stackable format.  Note that its in the same server area
522
 
        # which is what launchpad does, but not sufficient to exercise the
523
 
        # general case.
524
 
        trunk = self.make_branch('trunk', format='1.9')
525
 
        t = self.get_transport('stacked')
526
 
        old_fmt = controldir.format_registry.make_bzrdir('pack-0.92')
527
 
        repo_name = old_fmt.repository_format.network_name()
528
 
        # Should end up with a 1.9 format (stackable)
529
 
        repo, control, require_stacking, repo_policy = \
530
 
            old_fmt.initialize_on_transport_ex(t,
531
 
                    repo_format_name=repo_name, stacked_on='../trunk',
532
 
                    stack_on_pwd=t.base)
533
 
        if repo is not None:
534
 
            # Repositories are open write-locked
535
 
            self.assertTrue(repo.is_write_locked())
536
 
            self.addCleanup(repo.unlock)
537
 
        else:
538
 
            repo = control.open_repository()
539
 
        self.assertIsInstance(control, bzrdir.BzrDir)
540
 
        opened = bzrdir.BzrDir.open(t.base)
541
 
        if not isinstance(old_fmt, remote.RemoteBzrDirFormat):
542
 
            self.assertEqual(control._format.network_name(),
543
 
                old_fmt.network_name())
544
 
            self.assertEqual(control._format.network_name(),
545
 
                opened._format.network_name())
546
 
        self.assertEqual(control.__class__, opened.__class__)
547
 
        self.assertLength(1, repo._fallback_repositories)
548
 
 
549
486
    def test_sprout_obeys_stacking_policy(self):
550
487
        child_branch, new_child_transport = self.prepare_default_stacking()
551
488
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
742
679
        self.assertEqual(relpath, 'baz')
743
680
 
744
681
    def test_open_containing_from_transport(self):
745
 
        self.assertRaises(NotBranchError,
746
 
            bzrdir.BzrDir.open_containing_from_transport,
747
 
            _mod_transport.get_transport_from_url(self.get_readonly_url('')))
748
 
        self.assertRaises(NotBranchError,
749
 
            bzrdir.BzrDir.open_containing_from_transport,
750
 
            _mod_transport.get_transport_from_url(
751
 
                self.get_readonly_url('g/p/q')))
 
682
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
683
                          get_transport(self.get_readonly_url('')))
 
684
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
685
                          get_transport(self.get_readonly_url('g/p/q')))
752
686
        control = bzrdir.BzrDir.create(self.get_url())
753
687
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
754
 
            _mod_transport.get_transport_from_url(
755
 
                self.get_readonly_url('')))
 
688
            get_transport(self.get_readonly_url('')))
756
689
        self.assertEqual('', relpath)
757
690
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
758
 
            _mod_transport.get_transport_from_url(
759
 
                self.get_readonly_url('g/p/q')))
 
691
            get_transport(self.get_readonly_url('g/p/q')))
760
692
        self.assertEqual('g/p/q', relpath)
761
693
 
762
694
    def test_open_containing_tree_or_branch(self):
806
738
        # transport pointing at bzrdir should give a bzrdir with root transport
807
739
        # set to the given transport
808
740
        control = bzrdir.BzrDir.create(self.get_url())
809
 
        t = self.get_transport()
810
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(t)
811
 
        self.assertEqual(t.base, opened_bzrdir.root_transport.base)
 
741
        transport = get_transport(self.get_url())
 
742
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
743
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
812
744
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
813
745
 
814
746
    def test_open_from_transport_no_bzrdir(self):
815
 
        t = self.get_transport()
816
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
747
        transport = get_transport(self.get_url())
 
748
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
749
                          transport)
817
750
 
818
751
    def test_open_from_transport_bzrdir_in_parent(self):
819
752
        control = bzrdir.BzrDir.create(self.get_url())
820
 
        t = self.get_transport()
821
 
        t.mkdir('subdir')
822
 
        t = t.clone('subdir')
823
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport, t)
 
753
        transport = get_transport(self.get_url())
 
754
        transport.mkdir('subdir')
 
755
        transport = transport.clone('subdir')
 
756
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
757
                          transport)
824
758
 
825
759
    def test_sprout_recursive(self):
826
760
        tree = self.make_branch_and_tree('tree1',
827
 
                                         format='development-subtree')
 
761
                                         format='dirstate-with-subtree')
828
762
        sub_tree = self.make_branch_and_tree('tree1/subtree',
829
 
            format='development-subtree')
 
763
            format='dirstate-with-subtree')
830
764
        sub_tree.set_root_id('subtree-root')
831
765
        tree.add_reference(sub_tree)
832
766
        self.build_tree(['tree1/subtree/file'])
835
769
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
836
770
        tree2.lock_read()
837
771
        self.addCleanup(tree2.unlock)
838
 
        self.assertPathExists('tree2/subtree/file')
 
772
        self.failUnlessExists('tree2/subtree/file')
839
773
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
840
774
 
841
775
    def test_cloning_metadir(self):
845
779
        branch = self.make_branch('branch', format='knit')
846
780
        format = branch.bzrdir.cloning_metadir()
847
781
        self.assertIsInstance(format.workingtree_format,
848
 
            workingtree_4.WorkingTreeFormat6)
 
782
            workingtree.WorkingTreeFormat3)
849
783
 
850
784
    def test_sprout_recursive_treeless(self):
851
785
        tree = self.make_branch_and_tree('tree1',
852
 
            format='development-subtree')
 
786
            format='dirstate-with-subtree')
853
787
        sub_tree = self.make_branch_and_tree('tree1/subtree',
854
 
            format='development-subtree')
 
788
            format='dirstate-with-subtree')
855
789
        tree.add_reference(sub_tree)
856
790
        self.build_tree(['tree1/subtree/file'])
857
791
        sub_tree.add('file')
858
792
        tree.commit('Initial commit')
859
 
        # The following line force the orhaning to reveal bug #634470
860
 
        tree.branch.get_config_stack().set(
861
 
            'bzr.transform.orphan_policy', 'move')
862
793
        tree.bzrdir.destroy_workingtree()
863
 
        # FIXME: subtree/.bzr is left here which allows the test to pass (or
864
 
        # fail :-( ) -- vila 20100909
865
794
        repo = self.make_repository('repo', shared=True,
866
 
            format='development-subtree')
 
795
            format='dirstate-with-subtree')
867
796
        repo.set_make_working_trees(False)
868
 
        # FIXME: we just deleted the workingtree and now we want to use it ????
869
 
        # At a minimum, we should use tree.branch below (but this fails too
870
 
        # currently) or stop calling this test 'treeless'. Specifically, I've
871
 
        # turn the line below into an assertRaises when 'subtree/.bzr' is
872
 
        # orphaned and sprout tries to access the branch there (which is left
873
 
        # by bzrdir.BzrDirMeta1.destroy_workingtree when it ignores the
874
 
        # [DeletingParent('Not deleting', u'subtree', None)] conflict). See bug
875
 
        # #634470.  -- vila 20100909
876
 
        self.assertRaises(errors.NotBranchError,
877
 
                          tree.bzrdir.sprout, 'repo/tree2')
878
 
#        self.assertPathExists('repo/tree2/subtree')
879
 
#        self.assertPathDoesNotExist('repo/tree2/subtree/file')
 
797
        tree.bzrdir.sprout('repo/tree2')
 
798
        self.failUnlessExists('repo/tree2/subtree')
 
799
        self.failIfExists('repo/tree2/subtree/file')
880
800
 
881
801
    def make_foo_bar_baz(self):
882
802
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
886
806
 
887
807
    def test_find_bzrdirs(self):
888
808
        foo, bar, baz = self.make_foo_bar_baz()
889
 
        t = self.get_transport()
890
 
        self.assertEqualBzrdirs([baz, foo, bar], bzrdir.BzrDir.find_bzrdirs(t))
 
809
        transport = get_transport(self.get_url())
 
810
        self.assertEqualBzrdirs([baz, foo, bar],
 
811
                                bzrdir.BzrDir.find_bzrdirs(transport))
891
812
 
892
813
    def make_fake_permission_denied_transport(self, transport, paths):
893
814
        """Create a transport that raises PermissionDenied for some paths."""
909
830
 
910
831
    def test_find_bzrdirs_permission_denied(self):
911
832
        foo, bar, baz = self.make_foo_bar_baz()
912
 
        t = self.get_transport()
 
833
        transport = get_transport(self.get_url())
913
834
        path_filter_server, path_filter_transport = \
914
 
            self.make_fake_permission_denied_transport(t, ['foo'])
 
835
            self.make_fake_permission_denied_transport(transport, ['foo'])
915
836
        # local transport
916
837
        self.assertBranchUrlsEndWith('/baz/',
917
838
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
926
847
            return [s for s in transport.list_dir('') if s != 'baz']
927
848
 
928
849
        foo, bar, baz = self.make_foo_bar_baz()
929
 
        t = self.get_transport()
930
 
        self.assertEqualBzrdirs(
931
 
            [foo, bar],
932
 
            bzrdir.BzrDir.find_bzrdirs(t, list_current=list_current))
 
850
        transport = get_transport(self.get_url())
 
851
        self.assertEqualBzrdirs([foo, bar],
 
852
                                bzrdir.BzrDir.find_bzrdirs(transport,
 
853
                                    list_current=list_current))
933
854
 
934
855
    def test_find_bzrdirs_evaluate(self):
935
856
        def evaluate(bzrdir):
936
857
            try:
937
858
                repo = bzrdir.open_repository()
938
 
            except errors.NoRepositoryPresent:
 
859
            except NoRepositoryPresent:
939
860
                return True, bzrdir.root_transport.base
940
861
            else:
941
862
                return False, bzrdir.root_transport.base
942
863
 
943
864
        foo, bar, baz = self.make_foo_bar_baz()
944
 
        t = self.get_transport()
 
865
        transport = get_transport(self.get_url())
945
866
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
946
 
                         list(bzrdir.BzrDir.find_bzrdirs(t, evaluate=evaluate)))
 
867
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
 
868
                                                         evaluate=evaluate)))
947
869
 
948
870
    def assertEqualBzrdirs(self, first, second):
949
871
        first = list(first)
956
878
        root = self.make_repository('', shared=True)
957
879
        foo, bar, baz = self.make_foo_bar_baz()
958
880
        qux = self.make_bzrdir('foo/qux')
959
 
        t = self.get_transport()
960
 
        branches = bzrdir.BzrDir.find_branches(t)
 
881
        transport = get_transport(self.get_url())
 
882
        branches = bzrdir.BzrDir.find_branches(transport)
961
883
        self.assertEqual(baz.root_transport.base, branches[0].base)
962
884
        self.assertEqual(foo.root_transport.base, branches[1].base)
963
885
        self.assertEqual(bar.root_transport.base, branches[2].base)
964
886
 
965
887
        # ensure this works without a top-level repo
966
 
        branches = bzrdir.BzrDir.find_branches(t.clone('foo'))
 
888
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
967
889
        self.assertEqual(foo.root_transport.base, branches[0].base)
968
890
        self.assertEqual(bar.root_transport.base, branches[1].base)
969
891
 
971
893
class TestMissingRepoBranchesSkipped(TestCaseWithMemoryTransport):
972
894
 
973
895
    def test_find_bzrdirs_missing_repo(self):
974
 
        t = self.get_transport()
 
896
        transport = get_transport(self.get_url())
975
897
        arepo = self.make_repository('arepo', shared=True)
976
898
        abranch_url = arepo.user_url + '/abranch'
977
899
        abranch = bzrdir.BzrDir.create(abranch_url).create_branch()
978
 
        t.delete_tree('arepo/.bzr')
 
900
        transport.delete_tree('arepo/.bzr')
979
901
        self.assertRaises(errors.NoRepositoryPresent,
980
902
            branch.Branch.open, abranch_url)
981
903
        self.make_branch('baz')
982
 
        for actual_bzrdir in bzrdir.BzrDir.find_branches(t):
 
904
        for actual_bzrdir in bzrdir.BzrDir.find_branches(transport):
983
905
            self.assertEndsWith(actual_bzrdir.user_url, '/baz/')
984
906
 
985
907
 
992
914
        branch_base = t.clone('branch').base
993
915
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
994
916
        self.assertEqual(branch_base,
995
 
                         dir.get_branch_transport(BzrBranchFormat5()).base)
 
917
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
996
918
        repository_base = t.clone('repository').base
997
919
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
998
 
        repository_format = repository.format_registry.get_default()
999
920
        self.assertEqual(repository_base,
1000
 
                         dir.get_repository_transport(repository_format).base)
 
921
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
1001
922
        checkout_base = t.clone('checkout').base
1002
923
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
1003
924
        self.assertEqual(checkout_base,
1004
 
                         dir.get_workingtree_transport(workingtree_3.WorkingTreeFormat3()).base)
 
925
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
1005
926
 
1006
927
    def test_meta1dir_uses_lockdir(self):
1007
928
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
1015
936
        Metadirs should compare equal iff they have the same repo, branch and
1016
937
        tree formats.
1017
938
        """
1018
 
        mydir = controldir.format_registry.make_bzrdir('knit')
 
939
        mydir = bzrdir.format_registry.make_bzrdir('knit')
1019
940
        self.assertEqual(mydir, mydir)
1020
941
        self.assertFalse(mydir != mydir)
1021
 
        otherdir = controldir.format_registry.make_bzrdir('knit')
 
942
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
1022
943
        self.assertEqual(otherdir, mydir)
1023
944
        self.assertFalse(otherdir != mydir)
1024
 
        otherdir2 = controldir.format_registry.make_bzrdir('development-subtree')
 
945
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
1025
946
        self.assertNotEqual(otherdir2, mydir)
1026
947
        self.assertFalse(otherdir2 == mydir)
1027
948
 
1028
 
    def test_with_features(self):
1029
 
        tree = self.make_branch_and_tree('tree', format='2a')
1030
 
        tree.bzrdir.update_feature_flags({"bar": "required"})
1031
 
        self.assertRaises(errors.MissingFeature, bzrdir.BzrDir.open, 'tree')
1032
 
        bzrdir.BzrDirMetaFormat1.register_feature('bar')
1033
 
        self.addCleanup(bzrdir.BzrDirMetaFormat1.unregister_feature, 'bar')
1034
 
        dir = bzrdir.BzrDir.open('tree')
1035
 
        self.assertEqual("required", dir._format.features.get("bar"))
1036
 
        tree.bzrdir.update_feature_flags({"bar": None, "nonexistant": None})
1037
 
        dir = bzrdir.BzrDir.open('tree')
1038
 
        self.assertEqual({}, dir._format.features)
1039
 
 
1040
949
    def test_needs_conversion_different_working_tree(self):
1041
950
        # meta1dirs need an conversion if any element is not the default.
1042
 
        new_format = controldir.format_registry.make_bzrdir('dirstate')
 
951
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
1043
952
        tree = self.make_branch_and_tree('tree', format='knit')
1044
953
        self.assertTrue(tree.bzrdir.needs_format_conversion(
1045
954
            new_format))
1046
955
 
1047
956
    def test_initialize_on_format_uses_smart_transport(self):
1048
957
        self.setup_smart_server_with_call_log()
1049
 
        new_format = controldir.format_registry.make_bzrdir('dirstate')
 
958
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
1050
959
        transport = self.get_transport('target')
1051
960
        transport.ensure_base()
1052
961
        self.reset_smart_call_log()
1061
970
        self.assertEqual(2, rpc_count)
1062
971
 
1063
972
 
 
973
class TestFormat5(TestCaseWithTransport):
 
974
    """Tests specific to the version 5 bzrdir format."""
 
975
 
 
976
    def test_same_lockfiles_between_tree_repo_branch(self):
 
977
        # this checks that only a single lockfiles instance is created
 
978
        # for format 5 objects
 
979
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
980
        def check_dir_components_use_same_lock(dir):
 
981
            ctrl_1 = dir.open_repository().control_files
 
982
            ctrl_2 = dir.open_branch().control_files
 
983
            ctrl_3 = dir.open_workingtree()._control_files
 
984
            self.assertTrue(ctrl_1 is ctrl_2)
 
985
            self.assertTrue(ctrl_2 is ctrl_3)
 
986
        check_dir_components_use_same_lock(dir)
 
987
        # and if we open it normally.
 
988
        dir = bzrdir.BzrDir.open(self.get_url())
 
989
        check_dir_components_use_same_lock(dir)
 
990
 
 
991
    def test_can_convert(self):
 
992
        # format 5 dirs are convertable
 
993
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
994
        self.assertTrue(dir.can_convert_format())
 
995
 
 
996
    def test_needs_conversion(self):
 
997
        # format 5 dirs need a conversion if they are not the default,
 
998
        # and they aren't
 
999
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
1000
        # don't need to convert it to itself
 
1001
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
 
1002
        # do need to convert it to the current default
 
1003
        self.assertTrue(dir.needs_format_conversion(
 
1004
            bzrdir.BzrDirFormat.get_default_format()))
 
1005
 
 
1006
 
 
1007
class TestFormat6(TestCaseWithTransport):
 
1008
    """Tests specific to the version 6 bzrdir format."""
 
1009
 
 
1010
    def test_same_lockfiles_between_tree_repo_branch(self):
 
1011
        # this checks that only a single lockfiles instance is created
 
1012
        # for format 6 objects
 
1013
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1014
        def check_dir_components_use_same_lock(dir):
 
1015
            ctrl_1 = dir.open_repository().control_files
 
1016
            ctrl_2 = dir.open_branch().control_files
 
1017
            ctrl_3 = dir.open_workingtree()._control_files
 
1018
            self.assertTrue(ctrl_1 is ctrl_2)
 
1019
            self.assertTrue(ctrl_2 is ctrl_3)
 
1020
        check_dir_components_use_same_lock(dir)
 
1021
        # and if we open it normally.
 
1022
        dir = bzrdir.BzrDir.open(self.get_url())
 
1023
        check_dir_components_use_same_lock(dir)
 
1024
 
 
1025
    def test_can_convert(self):
 
1026
        # format 6 dirs are convertable
 
1027
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1028
        self.assertTrue(dir.can_convert_format())
 
1029
 
 
1030
    def test_needs_conversion(self):
 
1031
        # format 6 dirs need an conversion if they are not the default.
 
1032
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
1033
        self.assertTrue(dir.needs_format_conversion(
 
1034
            bzrdir.BzrDirFormat.get_default_format()))
 
1035
 
 
1036
 
 
1037
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
1038
    """A non .bzr based control directory."""
 
1039
 
 
1040
    def __init__(self, transport, format):
 
1041
        self._format = format
 
1042
        self.root_transport = transport
 
1043
        self.transport = transport.clone('.not')
 
1044
 
 
1045
 
 
1046
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
1047
    """A test class representing any non-.bzr based disk format."""
 
1048
 
 
1049
    def initialize_on_transport(self, transport):
 
1050
        """Initialize a new .not dir in the base directory of a Transport."""
 
1051
        transport.mkdir('.not')
 
1052
        return self.open(transport)
 
1053
 
 
1054
    def open(self, transport):
 
1055
        """Open this directory."""
 
1056
        return NotBzrDir(transport, self)
 
1057
 
 
1058
    @classmethod
 
1059
    def _known_formats(self):
 
1060
        return set([NotBzrDirFormat()])
 
1061
 
 
1062
 
 
1063
class NotBzrDirProber(controldir.Prober):
 
1064
 
 
1065
    def probe_transport(self, transport):
 
1066
        """Our format is present if the transport ends in '.not/'."""
 
1067
        if transport.has('.not'):
 
1068
            return NotBzrDirFormat()
 
1069
 
 
1070
 
 
1071
class TestNotBzrDir(TestCaseWithTransport):
 
1072
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
1073
 
 
1074
    If/when one of these is in the core, we can let the implementation tests
 
1075
    verify this works.
 
1076
    """
 
1077
 
 
1078
    def test_create_and_find_format(self):
 
1079
        # create a .notbzr dir
 
1080
        format = NotBzrDirFormat()
 
1081
        dir = format.initialize(self.get_url())
 
1082
        self.assertIsInstance(dir, NotBzrDir)
 
1083
        # now probe for it.
 
1084
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
 
1085
        try:
 
1086
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
1087
                get_transport(self.get_url()))
 
1088
            self.assertIsInstance(found, NotBzrDirFormat)
 
1089
        finally:
 
1090
            controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
 
1091
 
 
1092
    def test_included_in_known_formats(self):
 
1093
        not_format = NotBzrDirFormat()
 
1094
        bzrlib.controldir.ControlDirFormat.register_format(not_format)
 
1095
        try:
 
1096
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
1097
            for format in formats:
 
1098
                if isinstance(format, NotBzrDirFormat):
 
1099
                    return
 
1100
            self.fail("No NotBzrDirFormat in %s" % formats)
 
1101
        finally:
 
1102
            bzrlib.controldir.ControlDirFormat.unregister_format(not_format)
 
1103
 
 
1104
 
1064
1105
class NonLocalTests(TestCaseWithTransport):
1065
1106
    """Tests for bzrdir static behaviour on non local paths."""
1066
1107
 
1070
1111
 
1071
1112
    def test_create_branch_convenience(self):
1072
1113
        # outside a repo the default convenience output is a repo+branch_tree
1073
 
        format = controldir.format_registry.make_bzrdir('knit')
 
1114
        format = bzrdir.format_registry.make_bzrdir('knit')
1074
1115
        branch = bzrdir.BzrDir.create_branch_convenience(
1075
1116
            self.get_url('foo'), format=format)
1076
1117
        self.assertRaises(errors.NoWorkingTree,
1079
1120
 
1080
1121
    def test_create_branch_convenience_force_tree_not_local_fails(self):
1081
1122
        # outside a repo the default convenience output is a repo+branch_tree
1082
 
        format = controldir.format_registry.make_bzrdir('knit')
 
1123
        format = bzrdir.format_registry.make_bzrdir('knit')
1083
1124
        self.assertRaises(errors.NotLocalUrl,
1084
1125
            bzrdir.BzrDir.create_branch_convenience,
1085
1126
            self.get_url('foo'),
1086
1127
            force_new_tree=True,
1087
1128
            format=format)
1088
 
        t = self.get_transport()
 
1129
        t = get_transport(self.get_url('.'))
1089
1130
        self.assertFalse(t.has('foo'))
1090
1131
 
1091
1132
    def test_clone(self):
1092
1133
        # clone into a nonlocal path works
1093
 
        format = controldir.format_registry.make_bzrdir('knit')
 
1134
        format = bzrdir.format_registry.make_bzrdir('knit')
1094
1135
        branch = bzrdir.BzrDir.create_branch_convenience('local',
1095
1136
                                                         format=format)
1096
1137
        branch.bzrdir.open_workingtree()
1107
1148
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1108
1149
        checkout_format = my_bzrdir.checkout_metadir()
1109
1150
        self.assertIsInstance(checkout_format.workingtree_format,
1110
 
                              workingtree_4.WorkingTreeFormat4)
 
1151
                              workingtree.WorkingTreeFormat3)
1111
1152
 
1112
1153
 
1113
1154
class TestHTTPRedirections(object):
1231
1272
            raise TestSkipped('unable to make file hidden without pywin32 library')
1232
1273
        b = bzrdir.BzrDir.create('.')
1233
1274
        self.build_tree(['a'])
1234
 
        self.assertEqual(['a'], self.get_ls())
 
1275
        self.assertEquals(['a'], self.get_ls())
1235
1276
 
1236
1277
    def test_dot_bzr_hidden_with_url(self):
1237
1278
        if sys.platform == 'win32' and not win32utils.has_win32file:
1238
1279
            raise TestSkipped('unable to make file hidden without pywin32 library')
1239
1280
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1240
1281
        self.build_tree(['a'])
1241
 
        self.assertEqual(['a'], self.get_ls())
 
1282
        self.assertEquals(['a'], self.get_ls())
1242
1283
 
1243
1284
 
1244
1285
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1257
1298
 
1258
1299
    def __init__(self, *args, **kwargs):
1259
1300
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1260
 
        self.test_branch = _TestBranch(self.transport)
 
1301
        self.test_branch = _TestBranch()
1261
1302
        self.test_branch.repository = self.create_repository()
1262
1303
 
1263
 
    def open_branch(self, unsupported=False, possible_transports=None):
 
1304
    def open_branch(self, unsupported=False):
1264
1305
        return self.test_branch
1265
1306
 
1266
1307
    def cloning_metadir(self, require_stacking=False):
1274
1315
class _TestBranch(bzrlib.branch.Branch):
1275
1316
    """Test Branch implementation for TestBzrDirSprout."""
1276
1317
 
1277
 
    def __init__(self, transport, *args, **kwargs):
 
1318
    def __init__(self, *args, **kwargs):
1278
1319
        self._format = _TestBranchFormat()
1279
 
        self._transport = transport
1280
 
        self.base = transport.base
1281
1320
        super(_TestBranch, self).__init__(*args, **kwargs)
1282
1321
        self.calls = []
1283
1322
        self._parent = None
1284
1323
 
1285
1324
    def sprout(self, *args, **kwargs):
1286
1325
        self.calls.append('sprout')
1287
 
        return _TestBranch(self._transport)
 
1326
        return _TestBranch()
1288
1327
 
1289
1328
    def copy_content_into(self, destination, revision_id=None):
1290
1329
        self.calls.append('copy_content_into')
1291
1330
 
1292
 
    def last_revision(self):
1293
 
        return _mod_revision.NULL_REVISION
1294
 
 
1295
1331
    def get_parent(self):
1296
1332
        return self._parent
1297
1333
 
1298
 
    def _get_config(self):
1299
 
        return config.TransportConfig(self._transport, 'branch.conf')
1300
 
 
1301
 
    def _get_config_store(self):
1302
 
        return config.BranchStore(self)
1303
 
 
1304
1334
    def set_parent(self, parent):
1305
1335
        self._parent = parent
1306
1336
 
1307
 
    def lock_read(self):
1308
 
        return lock.LogicalLockResult(self.unlock)
1309
 
 
1310
 
    def unlock(self):
1311
 
        return
1312
 
 
1313
1337
 
1314
1338
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1315
1339
 
1371
1395
        self.assertEqual('fail', err._preformatted_string)
1372
1396
 
1373
1397
    def test_post_repo_init(self):
1374
 
        from bzrlib.controldir import RepoInitHookParams
 
1398
        from bzrlib.bzrdir import RepoInitHookParams
1375
1399
        calls = []
1376
1400
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1377
1401
            calls.append, None)
1393
1417
 
1394
1418
 
1395
1419
class TestGenerateBackupName(TestCaseWithMemoryTransport):
1396
 
    # FIXME: This may need to be unified with test_osutils.TestBackupNames or
1397
 
    # moved to per_bzrdir or per_transport for better coverage ?
1398
 
    # -- vila 20100909
1399
1420
 
1400
1421
    def setUp(self):
1401
1422
        super(TestGenerateBackupName, self).setUp()
1402
 
        self._transport = self.get_transport()
 
1423
        self._transport = get_transport(self.get_url())
1403
1424
        bzrdir.BzrDir.create(self.get_url(),
1404
1425
            possible_transports=[self._transport])
1405
1426
        self._bzrdir = bzrdir.BzrDir.open_from_transport(self._transport)
1406
1427
 
1407
1428
    def test_new(self):
1408
 
        self.assertEqual("a.~1~", self._bzrdir._available_backup_name("a"))
 
1429
        self.assertEqual("a.~1~", self._bzrdir.generate_backup_name("a"))
1409
1430
 
1410
1431
    def test_exiting(self):
1411
1432
        self._transport.put_bytes("a.~1~", "some content")
1412
 
        self.assertEqual("a.~2~", self._bzrdir._available_backup_name("a"))
1413
 
 
1414
 
 
1415
 
class TestMeta1DirColoFormat(TestCaseWithTransport):
1416
 
    """Tests specific to the meta1 dir with colocated branches format."""
1417
 
 
1418
 
    def test_supports_colo(self):
1419
 
        format = bzrdir.BzrDirMetaFormat1Colo()
1420
 
        self.assertTrue(format.colocated_branches)
1421
 
 
1422
 
    def test_upgrade_from_2a(self):
1423
 
        tree = self.make_branch_and_tree('.', format='2a')
1424
 
        format = bzrdir.BzrDirMetaFormat1Colo()
1425
 
        self.assertTrue(tree.bzrdir.needs_format_conversion(format))
1426
 
        converter = tree.bzrdir._format.get_converter(format)
1427
 
        result = converter.convert(tree.bzrdir, None)
1428
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1Colo)
1429
 
        self.assertFalse(result.needs_format_conversion(format))
1430
 
 
1431
 
    def test_downgrade_to_2a(self):
1432
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1433
 
        format = bzrdir.BzrDirMetaFormat1()
1434
 
        self.assertTrue(tree.bzrdir.needs_format_conversion(format))
1435
 
        converter = tree.bzrdir._format.get_converter(format)
1436
 
        result = converter.convert(tree.bzrdir, None)
1437
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1438
 
        self.assertFalse(result.needs_format_conversion(format))
1439
 
 
1440
 
    def test_downgrade_to_2a_too_many_branches(self):
1441
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1442
 
        tree.bzrdir.create_branch(name="another-colocated-branch")
1443
 
        converter = tree.bzrdir._format.get_converter(
1444
 
            bzrdir.BzrDirMetaFormat1())
1445
 
        result = converter.convert(tree.bzrdir, bzrdir.BzrDirMetaFormat1())
1446
 
        self.assertIsInstance(result._format, bzrdir.BzrDirMetaFormat1)
1447
 
 
1448
 
    def test_nested(self):
1449
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1450
 
        tree.bzrdir.create_branch(name='foo')
1451
 
        tree.bzrdir.create_branch(name='fool/bla')
1452
 
        self.assertRaises(
1453
 
            errors.ParentBranchExists, tree.bzrdir.create_branch,
1454
 
            name='foo/bar')
1455
 
 
1456
 
    def test_parent(self):
1457
 
        tree = self.make_branch_and_tree('.', format='development-colo')
1458
 
        tree.bzrdir.create_branch(name='fool/bla')
1459
 
        tree.bzrdir.create_branch(name='foo/bar')
1460
 
        self.assertRaises(
1461
 
            errors.AlreadyBranchError, tree.bzrdir.create_branch,
1462
 
            name='foo')
1463
 
 
1464
 
 
1465
 
class SampleBzrFormat(bzrdir.BzrFormat):
1466
 
 
1467
 
    @classmethod
1468
 
    def get_format_string(cls):
1469
 
        return "First line\n"
1470
 
 
1471
 
 
1472
 
class TestBzrFormat(TestCase):
1473
 
    """Tests for BzrFormat."""
1474
 
 
1475
 
    def test_as_string(self):
1476
 
        format = SampleBzrFormat()
1477
 
        format.features = {"foo": "required"}
1478
 
        self.assertEqual(format.as_string(),
1479
 
            "First line\n"
1480
 
            "required foo\n")
1481
 
        format.features["another"] = "optional"
1482
 
        self.assertEqual(format.as_string(),
1483
 
            "First line\n"
1484
 
            "required foo\n"
1485
 
            "optional another\n")
1486
 
 
1487
 
    def test_network_name(self):
1488
 
        # The network string should include the feature info
1489
 
        format = SampleBzrFormat()
1490
 
        format.features = {"foo": "required"}
1491
 
        self.assertEqual(
1492
 
            "First line\nrequired foo\n",
1493
 
            format.network_name())
1494
 
 
1495
 
    def test_from_string_no_features(self):
1496
 
        # No features
1497
 
        format = SampleBzrFormat.from_string(
1498
 
            "First line\n")
1499
 
        self.assertEqual({}, format.features)
1500
 
 
1501
 
    def test_from_string_with_feature(self):
1502
 
        # Proper feature
1503
 
        format = SampleBzrFormat.from_string(
1504
 
            "First line\nrequired foo\n")
1505
 
        self.assertEqual("required", format.features.get("foo"))
1506
 
 
1507
 
    def test_from_string_format_string_mismatch(self):
1508
 
        # The first line has to match the format string
1509
 
        self.assertRaises(AssertionError, SampleBzrFormat.from_string,
1510
 
            "Second line\nrequired foo\n")
1511
 
 
1512
 
    def test_from_string_missing_space(self):
1513
 
        # At least one space is required in the feature lines
1514
 
        self.assertRaises(errors.ParseFormatError, SampleBzrFormat.from_string,
1515
 
            "First line\nfoo\n")
1516
 
 
1517
 
    def test_from_string_with_spaces(self):
1518
 
        # Feature with spaces (in case we add stuff like this in the future)
1519
 
        format = SampleBzrFormat.from_string(
1520
 
            "First line\nrequired foo with spaces\n")
1521
 
        self.assertEqual("required", format.features.get("foo with spaces"))
1522
 
 
1523
 
    def test_eq(self):
1524
 
        format1 = SampleBzrFormat()
1525
 
        format1.features = {"nested-trees": "optional"}
1526
 
        format2 = SampleBzrFormat()
1527
 
        format2.features = {"nested-trees": "optional"}
1528
 
        self.assertEqual(format1, format1)
1529
 
        self.assertEqual(format1, format2)
1530
 
        format3 = SampleBzrFormat()
1531
 
        self.assertNotEqual(format1, format3)
1532
 
 
1533
 
    def test_check_support_status_optional(self):
1534
 
        # Optional, so silently ignore
1535
 
        format = SampleBzrFormat()
1536
 
        format.features = {"nested-trees": "optional"}
1537
 
        format.check_support_status(True)
1538
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1539
 
        SampleBzrFormat.register_feature("nested-trees")
1540
 
        format.check_support_status(True)
1541
 
 
1542
 
    def test_check_support_status_required(self):
1543
 
        # Optional, so trigger an exception
1544
 
        format = SampleBzrFormat()
1545
 
        format.features = {"nested-trees": "required"}
1546
 
        self.assertRaises(errors.MissingFeature, format.check_support_status,
1547
 
            True)
1548
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1549
 
        SampleBzrFormat.register_feature("nested-trees")
1550
 
        format.check_support_status(True)
1551
 
 
1552
 
    def test_check_support_status_unknown(self):
1553
 
        # treat unknown necessity as required
1554
 
        format = SampleBzrFormat()
1555
 
        format.features = {"nested-trees": "unknown"}
1556
 
        self.assertRaises(errors.MissingFeature, format.check_support_status,
1557
 
            True)
1558
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1559
 
        SampleBzrFormat.register_feature("nested-trees")
1560
 
        format.check_support_status(True)
1561
 
 
1562
 
    def test_feature_already_registered(self):
1563
 
        # a feature can only be registered once
1564
 
        self.addCleanup(SampleBzrFormat.unregister_feature, "nested-trees")
1565
 
        SampleBzrFormat.register_feature("nested-trees")
1566
 
        self.assertRaises(errors.FeatureAlreadyRegistered,
1567
 
            SampleBzrFormat.register_feature, "nested-trees")
1568
 
 
1569
 
    def test_feature_with_space(self):
1570
 
        # spaces are not allowed in feature names
1571
 
        self.assertRaises(ValueError, SampleBzrFormat.register_feature,
1572
 
            "nested trees")
 
1433
        self.assertEqual("a.~2~", self._bzrdir.generate_backup_name("a"))