/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Martin Pool
  • Date: 2007-02-06 08:16:13 UTC
  • mto: This revision was merged to the branch mainline in revision 2283.
  • Revision ID: mbp@sourcefrog.net-20070206081613-dxop566k13bll6j0
Move KnitFormat2 into repofmt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
2
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Tests for the BzrDir facility and any format specific tests.
 
18
 
 
19
For interface contract tests, see tests/bzr_dir_implementations.
 
20
"""
 
21
 
 
22
import os.path
 
23
from StringIO import StringIO
 
24
 
 
25
from bzrlib import (
 
26
    help_topics,
 
27
    symbol_versioning,
 
28
    urlutils,
 
29
    workingtree,
 
30
    )
 
31
import bzrlib.branch
 
32
import bzrlib.bzrdir as bzrdir
 
33
import bzrlib.errors as errors
 
34
from bzrlib.errors import (NotBranchError,
 
35
                           UnknownFormatError,
 
36
                           UnsupportedFormatError,
 
37
                           )
 
38
import bzrlib.repository as repository
 
39
from bzrlib.tests import TestCase, TestCaseWithTransport, test_sftp_transport
 
40
from bzrlib.tests.HttpServer import HttpServer
 
41
from bzrlib.transport import get_transport
 
42
from bzrlib.transport.memory import MemoryServer
 
43
from bzrlib.repofmt import knitrepo, weaverepo
 
44
 
 
45
 
 
46
class TestDefaultFormat(TestCase):
 
47
 
 
48
    def test_get_set_default_format(self):
 
49
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
50
        # default is BzrDirFormat6
 
51
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
 
52
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
 
53
                             bzrdir.BzrDirFormat.set_default_format, 
 
54
                             SampleBzrDirFormat())
 
55
        # creating a bzr dir should now create an instrumented dir.
 
56
        try:
 
57
            result = bzrdir.BzrDir.create('memory:///')
 
58
            self.failUnless(isinstance(result, SampleBzrDir))
 
59
        finally:
 
60
            self.applyDeprecated(symbol_versioning.zero_fourteen,
 
61
                bzrdir.BzrDirFormat.set_default_format, old_format)
 
62
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
 
63
 
 
64
 
 
65
class TestFormatRegistry(TestCase):
 
66
 
 
67
    def make_format_registry(self):
 
68
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
69
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
70
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
71
            ' repositories', deprecated=True)
 
72
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
 
73
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
74
        my_format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
 
75
            'Format using knits')
 
76
        my_format_registry.set_default('knit')
 
77
        my_format_registry.register_metadir('experimental-knit2', 
 
78
                                            'RepositoryFormatKnit2',
 
79
            'Experimental successor to knit.  Use at your own risk.')
 
80
        return my_format_registry
 
81
 
 
82
    def test_format_registry(self):
 
83
        my_format_registry = self.make_format_registry()
 
84
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
 
85
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
86
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
87
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
88
        my_bzrdir = my_format_registry.make_bzrdir('default')
 
89
        self.assertIsInstance(my_bzrdir.repository_format, 
 
90
            repository.RepositoryFormatKnit1)
 
91
        my_bzrdir = my_format_registry.make_bzrdir('knit')
 
92
        self.assertIsInstance(my_bzrdir.repository_format, 
 
93
            repository.RepositoryFormatKnit1)
 
94
 
 
95
    def test_get_help(self):
 
96
        my_format_registry = self.make_format_registry()
 
97
        self.assertEqual('Format registered lazily',
 
98
                         my_format_registry.get_help('lazy'))
 
99
        self.assertEqual('Format using knits', 
 
100
                         my_format_registry.get_help('knit'))
 
101
        self.assertEqual('Format using knits', 
 
102
                         my_format_registry.get_help('default'))
 
103
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
104
                         ' checkouts or shared repositories', 
 
105
                         my_format_registry.get_help('weave'))
 
106
        
 
107
    def test_help_topic(self):
 
108
        topics = help_topics.HelpTopicRegistry()
 
109
        topics.register('formats', self.make_format_registry().help_topic, 
 
110
                        'Directory formats')
 
111
        topic = topics.get_detail('formats')
 
112
        new, deprecated = topic.split('Deprecated formats')
 
113
        self.assertContainsRe(new, 'Bazaar directory formats')
 
114
        self.assertContainsRe(new, 
 
115
            '  knit/default:\n    \(native\) Format using knits\n')
 
116
        self.assertContainsRe(deprecated, 
 
117
            '  lazy:\n    \(native\) Format registered lazily\n')
 
118
 
 
119
    def test_set_default_repository(self):
 
120
        default_factory = bzrdir.format_registry.get('default')
 
121
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
 
122
                       if v == default_factory and k != 'default'][0]
 
123
        bzrdir.format_registry.set_default_repository('experimental-knit2')
 
124
        try:
 
125
            self.assertIs(bzrdir.format_registry.get('experimental-knit2'),
 
126
                          bzrdir.format_registry.get('default'))
 
127
            self.assertIs(
 
128
                repository.RepositoryFormat.get_default_format().__class__,
 
129
                knitrepo.RepositoryFormatKnit2)
 
130
        finally:
 
131
            bzrdir.format_registry.set_default_repository(old_default)
 
132
 
 
133
class SampleBranch(bzrlib.branch.Branch):
 
134
    """A dummy branch for guess what, dummy use."""
 
135
 
 
136
    def __init__(self, dir):
 
137
        self.bzrdir = dir
 
138
 
 
139
 
 
140
class SampleBzrDir(bzrdir.BzrDir):
 
141
    """A sample BzrDir implementation to allow testing static methods."""
 
142
 
 
143
    def create_repository(self, shared=False):
 
144
        """See BzrDir.create_repository."""
 
145
        return "A repository"
 
146
 
 
147
    def open_repository(self):
 
148
        """See BzrDir.open_repository."""
 
149
        return "A repository"
 
150
 
 
151
    def create_branch(self):
 
152
        """See BzrDir.create_branch."""
 
153
        return SampleBranch(self)
 
154
 
 
155
    def create_workingtree(self):
 
156
        """See BzrDir.create_workingtree."""
 
157
        return "A tree"
 
158
 
 
159
 
 
160
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
 
161
    """A sample format
 
162
 
 
163
    this format is initializable, unsupported to aid in testing the 
 
164
    open and open_downlevel routines.
 
165
    """
 
166
 
 
167
    def get_format_string(self):
 
168
        """See BzrDirFormat.get_format_string()."""
 
169
        return "Sample .bzr dir format."
 
170
 
 
171
    def initialize(self, url):
 
172
        """Create a bzr dir."""
 
173
        t = get_transport(url)
 
174
        t.mkdir('.bzr')
 
175
        t.put_bytes('.bzr/branch-format', self.get_format_string())
 
176
        return SampleBzrDir(t, self)
 
177
 
 
178
    def is_supported(self):
 
179
        return False
 
180
 
 
181
    def open(self, transport, _found=None):
 
182
        return "opened branch."
 
183
 
 
184
 
 
185
class TestBzrDirFormat(TestCaseWithTransport):
 
186
    """Tests for the BzrDirFormat facility."""
 
187
 
 
188
    def test_find_format(self):
 
189
        # is the right format object found for a branch?
 
190
        # create a branch with a few known format objects.
 
191
        # this is not quite the same as 
 
192
        t = get_transport(self.get_url())
 
193
        self.build_tree(["foo/", "bar/"], transport=t)
 
194
        def check_format(format, url):
 
195
            format.initialize(url)
 
196
            t = get_transport(url)
 
197
            found_format = bzrdir.BzrDirFormat.find_format(t)
 
198
            self.failUnless(isinstance(found_format, format.__class__))
 
199
        check_format(bzrdir.BzrDirFormat5(), "foo")
 
200
        check_format(bzrdir.BzrDirFormat6(), "bar")
 
201
        
 
202
    def test_find_format_nothing_there(self):
 
203
        self.assertRaises(NotBranchError,
 
204
                          bzrdir.BzrDirFormat.find_format,
 
205
                          get_transport('.'))
 
206
 
 
207
    def test_find_format_unknown_format(self):
 
208
        t = get_transport(self.get_url())
 
209
        t.mkdir('.bzr')
 
210
        t.put_bytes('.bzr/branch-format', '')
 
211
        self.assertRaises(UnknownFormatError,
 
212
                          bzrdir.BzrDirFormat.find_format,
 
213
                          get_transport('.'))
 
214
 
 
215
    def test_register_unregister_format(self):
 
216
        format = SampleBzrDirFormat()
 
217
        url = self.get_url()
 
218
        # make a bzrdir
 
219
        format.initialize(url)
 
220
        # register a format for it.
 
221
        bzrdir.BzrDirFormat.register_format(format)
 
222
        # which bzrdir.Open will refuse (not supported)
 
223
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
 
224
        # which bzrdir.open_containing will refuse (not supported)
 
225
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
 
226
        # but open_downlevel will work
 
227
        t = get_transport(url)
 
228
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
 
229
        # unregister the format
 
230
        bzrdir.BzrDirFormat.unregister_format(format)
 
231
        # now open_downlevel should fail too.
 
232
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
 
233
 
 
234
    def test_create_repository(self):
 
235
        format = SampleBzrDirFormat()
 
236
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
 
237
        self.assertEqual('A repository', repo)
 
238
 
 
239
    def test_create_repository_shared(self):
 
240
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
241
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
 
242
        self.assertTrue(repo.is_shared())
 
243
 
 
244
    def test_create_repository_nonshared(self):
 
245
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
246
        repo = bzrdir.BzrDir.create_repository('.')
 
247
        self.assertFalse(repo.is_shared())
 
248
 
 
249
    def test_create_repository_under_shared(self):
 
250
        # an explicit create_repository always does so.
 
251
        # we trust the format is right from the 'create_repository test'
 
252
        format = bzrdir.format_registry.make_bzrdir('knit')
 
253
        self.make_repository('.', shared=True, format=format)
 
254
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
 
255
                                               format=format)
 
256
        self.assertTrue(isinstance(repo, repository.Repository))
 
257
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
258
 
 
259
    def test_create_branch_and_repo_uses_default(self):
 
260
        format = SampleBzrDirFormat()
 
261
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(), 
 
262
                                                      format=format)
 
263
        self.assertTrue(isinstance(branch, SampleBranch))
 
264
 
 
265
    def test_create_branch_and_repo_under_shared(self):
 
266
        # creating a branch and repo in a shared repo uses the
 
267
        # shared repository
 
268
        format = bzrdir.format_registry.make_bzrdir('knit')
 
269
        self.make_repository('.', shared=True, format=format)
 
270
        branch = bzrdir.BzrDir.create_branch_and_repo(
 
271
            self.get_url('child'), format=format)
 
272
        self.assertRaises(errors.NoRepositoryPresent,
 
273
                          branch.bzrdir.open_repository)
 
274
 
 
275
    def test_create_branch_and_repo_under_shared_force_new(self):
 
276
        # creating a branch and repo in a shared repo can be forced to 
 
277
        # make a new repo
 
278
        format = bzrdir.format_registry.make_bzrdir('knit')
 
279
        self.make_repository('.', shared=True, format=format)
 
280
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
281
                                                      force_new_repo=True,
 
282
                                                      format=format)
 
283
        branch.bzrdir.open_repository()
 
284
 
 
285
    def test_create_standalone_working_tree(self):
 
286
        format = SampleBzrDirFormat()
 
287
        # note this is deliberately readonly, as this failure should 
 
288
        # occur before any writes.
 
289
        self.assertRaises(errors.NotLocalUrl,
 
290
                          bzrdir.BzrDir.create_standalone_workingtree,
 
291
                          self.get_readonly_url(), format=format)
 
292
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
 
293
                                                           format=format)
 
294
        self.assertEqual('A tree', tree)
 
295
 
 
296
    def test_create_standalone_working_tree_under_shared_repo(self):
 
297
        # create standalone working tree always makes a repo.
 
298
        format = bzrdir.format_registry.make_bzrdir('knit')
 
299
        self.make_repository('.', shared=True, format=format)
 
300
        # note this is deliberately readonly, as this failure should 
 
301
        # occur before any writes.
 
302
        self.assertRaises(errors.NotLocalUrl,
 
303
                          bzrdir.BzrDir.create_standalone_workingtree,
 
304
                          self.get_readonly_url('child'), format=format)
 
305
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
 
306
            format=format)
 
307
        tree.bzrdir.open_repository()
 
308
 
 
309
    def test_create_branch_convenience(self):
 
310
        # outside a repo the default convenience output is a repo+branch_tree
 
311
        format = bzrdir.format_registry.make_bzrdir('knit')
 
312
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
 
313
        branch.bzrdir.open_workingtree()
 
314
        branch.bzrdir.open_repository()
 
315
 
 
316
    def test_create_branch_convenience_root(self):
 
317
        """Creating a branch at the root of a fs should work."""
 
318
        self.transport_server = MemoryServer
 
319
        # outside a repo the default convenience output is a repo+branch_tree
 
320
        format = bzrdir.format_registry.make_bzrdir('knit')
 
321
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
 
322
                                                         format=format)
 
323
        self.assertRaises(errors.NoWorkingTree,
 
324
                          branch.bzrdir.open_workingtree)
 
325
        branch.bzrdir.open_repository()
 
326
 
 
327
    def test_create_branch_convenience_under_shared_repo(self):
 
328
        # inside a repo the default convenience output is a branch+ follow the
 
329
        # repo tree policy
 
330
        format = bzrdir.format_registry.make_bzrdir('knit')
 
331
        self.make_repository('.', shared=True, format=format)
 
332
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
333
            format=format)
 
334
        branch.bzrdir.open_workingtree()
 
335
        self.assertRaises(errors.NoRepositoryPresent,
 
336
                          branch.bzrdir.open_repository)
 
337
            
 
338
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
 
339
        # inside a repo the default convenience output is a branch+ follow the
 
340
        # repo tree policy but we can override that
 
341
        format = bzrdir.format_registry.make_bzrdir('knit')
 
342
        self.make_repository('.', shared=True, format=format)
 
343
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
344
            force_new_tree=False, format=format)
 
345
        self.assertRaises(errors.NoWorkingTree,
 
346
                          branch.bzrdir.open_workingtree)
 
347
        self.assertRaises(errors.NoRepositoryPresent,
 
348
                          branch.bzrdir.open_repository)
 
349
            
 
350
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
 
351
        # inside a repo the default convenience output is a branch+ follow the
 
352
        # repo tree policy
 
353
        format = bzrdir.format_registry.make_bzrdir('knit')
 
354
        repo = self.make_repository('.', shared=True, format=format)
 
355
        repo.set_make_working_trees(False)
 
356
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
 
357
                                                         format=format)
 
358
        self.assertRaises(errors.NoWorkingTree,
 
359
                          branch.bzrdir.open_workingtree)
 
360
        self.assertRaises(errors.NoRepositoryPresent,
 
361
                          branch.bzrdir.open_repository)
 
362
 
 
363
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
 
364
        # inside a repo the default convenience output is a branch+ follow the
 
365
        # repo tree policy but we can override that
 
366
        format = bzrdir.format_registry.make_bzrdir('knit')
 
367
        repo = self.make_repository('.', shared=True, format=format)
 
368
        repo.set_make_working_trees(False)
 
369
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
370
            force_new_tree=True, format=format)
 
371
        branch.bzrdir.open_workingtree()
 
372
        self.assertRaises(errors.NoRepositoryPresent,
 
373
                          branch.bzrdir.open_repository)
 
374
 
 
375
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
 
376
        # inside a repo the default convenience output is overridable to give
 
377
        # repo+branch+tree
 
378
        format = bzrdir.format_registry.make_bzrdir('knit')
 
379
        self.make_repository('.', shared=True, format=format)
 
380
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
381
            force_new_repo=True, format=format)
 
382
        branch.bzrdir.open_repository()
 
383
        branch.bzrdir.open_workingtree()
 
384
 
 
385
 
 
386
class ChrootedTests(TestCaseWithTransport):
 
387
    """A support class that provides readonly urls outside the local namespace.
 
388
 
 
389
    This is done by checking if self.transport_server is a MemoryServer. if it
 
390
    is then we are chrooted already, if it is not then an HttpServer is used
 
391
    for readonly urls.
 
392
    """
 
393
 
 
394
    def setUp(self):
 
395
        super(ChrootedTests, self).setUp()
 
396
        if not self.transport_server == MemoryServer:
 
397
            self.transport_readonly_server = HttpServer
 
398
 
 
399
    def test_open_containing(self):
 
400
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
401
                          self.get_readonly_url(''))
 
402
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
 
403
                          self.get_readonly_url('g/p/q'))
 
404
        control = bzrdir.BzrDir.create(self.get_url())
 
405
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
 
406
        self.assertEqual('', relpath)
 
407
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
 
408
        self.assertEqual('g/p/q', relpath)
 
409
 
 
410
    def test_open_containing_from_transport(self):
 
411
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
412
                          get_transport(self.get_readonly_url('')))
 
413
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
 
414
                          get_transport(self.get_readonly_url('g/p/q')))
 
415
        control = bzrdir.BzrDir.create(self.get_url())
 
416
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
 
417
            get_transport(self.get_readonly_url('')))
 
418
        self.assertEqual('', relpath)
 
419
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
 
420
            get_transport(self.get_readonly_url('g/p/q')))
 
421
        self.assertEqual('g/p/q', relpath)
 
422
 
 
423
    def test_open_containing_tree_or_branch(self):
 
424
        def local_branch_path(branch):
 
425
             return os.path.realpath(
 
426
                urlutils.local_path_from_url(branch.base))
 
427
 
 
428
        self.make_branch_and_tree('topdir')
 
429
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
430
            'topdir/foo')
 
431
        self.assertEqual(os.path.realpath('topdir'),
 
432
                         os.path.realpath(tree.basedir))
 
433
        self.assertEqual(os.path.realpath('topdir'),
 
434
                         local_branch_path(branch))
 
435
        self.assertIs(tree.bzrdir, branch.bzrdir)
 
436
        self.assertEqual('foo', relpath)
 
437
        self.make_branch('topdir/foo')
 
438
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
439
            'topdir/foo')
 
440
        self.assertIs(tree, None)
 
441
        self.assertEqual(os.path.realpath('topdir/foo'),
 
442
                         local_branch_path(branch))
 
443
        self.assertEqual('', relpath)
 
444
 
 
445
    def test_open_from_transport(self):
 
446
        # transport pointing at bzrdir should give a bzrdir with root transport
 
447
        # set to the given transport
 
448
        control = bzrdir.BzrDir.create(self.get_url())
 
449
        transport = get_transport(self.get_url())
 
450
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
451
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
 
452
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
 
453
        
 
454
    def test_open_from_transport_no_bzrdir(self):
 
455
        transport = get_transport(self.get_url())
 
456
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
457
                          transport)
 
458
 
 
459
    def test_open_from_transport_bzrdir_in_parent(self):
 
460
        control = bzrdir.BzrDir.create(self.get_url())
 
461
        transport = get_transport(self.get_url())
 
462
        transport.mkdir('subdir')
 
463
        transport = transport.clone('subdir')
 
464
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
465
                          transport)
 
466
 
 
467
 
 
468
class TestMeta1DirFormat(TestCaseWithTransport):
 
469
    """Tests specific to the meta1 dir format."""
 
470
 
 
471
    def test_right_base_dirs(self):
 
472
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
473
        t = dir.transport
 
474
        branch_base = t.clone('branch').base
 
475
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
 
476
        self.assertEqual(branch_base,
 
477
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
 
478
        repository_base = t.clone('repository').base
 
479
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
 
480
        self.assertEqual(repository_base,
 
481
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
482
        checkout_base = t.clone('checkout').base
 
483
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
 
484
        self.assertEqual(checkout_base,
 
485
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
 
486
 
 
487
    def test_meta1dir_uses_lockdir(self):
 
488
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
 
489
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
490
        t = dir.transport
 
491
        self.assertIsDirectory('branch-lock', t)
 
492
 
 
493
        
 
494
class TestFormat5(TestCaseWithTransport):
 
495
    """Tests specific to the version 5 bzrdir format."""
 
496
 
 
497
    def test_same_lockfiles_between_tree_repo_branch(self):
 
498
        # this checks that only a single lockfiles instance is created 
 
499
        # for format 5 objects
 
500
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
501
        def check_dir_components_use_same_lock(dir):
 
502
            ctrl_1 = dir.open_repository().control_files
 
503
            ctrl_2 = dir.open_branch().control_files
 
504
            ctrl_3 = dir.open_workingtree()._control_files
 
505
            self.assertTrue(ctrl_1 is ctrl_2)
 
506
            self.assertTrue(ctrl_2 is ctrl_3)
 
507
        check_dir_components_use_same_lock(dir)
 
508
        # and if we open it normally.
 
509
        dir = bzrdir.BzrDir.open(self.get_url())
 
510
        check_dir_components_use_same_lock(dir)
 
511
    
 
512
    def test_can_convert(self):
 
513
        # format 5 dirs are convertable
 
514
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
515
        self.assertTrue(dir.can_convert_format())
 
516
    
 
517
    def test_needs_conversion(self):
 
518
        # format 5 dirs need a conversion if they are not the default.
 
519
        # and they start of not the default.
 
520
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
521
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
522
        try:
 
523
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
 
524
            self.assertFalse(dir.needs_format_conversion())
 
525
        finally:
 
526
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
527
        self.assertTrue(dir.needs_format_conversion())
 
528
 
 
529
 
 
530
class TestFormat6(TestCaseWithTransport):
 
531
    """Tests specific to the version 6 bzrdir format."""
 
532
 
 
533
    def test_same_lockfiles_between_tree_repo_branch(self):
 
534
        # this checks that only a single lockfiles instance is created 
 
535
        # for format 6 objects
 
536
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
537
        def check_dir_components_use_same_lock(dir):
 
538
            ctrl_1 = dir.open_repository().control_files
 
539
            ctrl_2 = dir.open_branch().control_files
 
540
            ctrl_3 = dir.open_workingtree()._control_files
 
541
            self.assertTrue(ctrl_1 is ctrl_2)
 
542
            self.assertTrue(ctrl_2 is ctrl_3)
 
543
        check_dir_components_use_same_lock(dir)
 
544
        # and if we open it normally.
 
545
        dir = bzrdir.BzrDir.open(self.get_url())
 
546
        check_dir_components_use_same_lock(dir)
 
547
    
 
548
    def test_can_convert(self):
 
549
        # format 6 dirs are convertable
 
550
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
551
        self.assertTrue(dir.can_convert_format())
 
552
    
 
553
    def test_needs_conversion(self):
 
554
        # format 6 dirs need an conversion if they are not the default.
 
555
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
556
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
557
        try:
 
558
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
 
559
            self.assertTrue(dir.needs_format_conversion())
 
560
        finally:
 
561
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
562
 
 
563
 
 
564
class NotBzrDir(bzrlib.bzrdir.BzrDir):
 
565
    """A non .bzr based control directory."""
 
566
 
 
567
    def __init__(self, transport, format):
 
568
        self._format = format
 
569
        self.root_transport = transport
 
570
        self.transport = transport.clone('.not')
 
571
 
 
572
 
 
573
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
 
574
    """A test class representing any non-.bzr based disk format."""
 
575
 
 
576
    def initialize_on_transport(self, transport):
 
577
        """Initialize a new .not dir in the base directory of a Transport."""
 
578
        transport.mkdir('.not')
 
579
        return self.open(transport)
 
580
 
 
581
    def open(self, transport):
 
582
        """Open this directory."""
 
583
        return NotBzrDir(transport, self)
 
584
 
 
585
    @classmethod
 
586
    def _known_formats(self):
 
587
        return set([NotBzrDirFormat()])
 
588
 
 
589
    @classmethod
 
590
    def probe_transport(self, transport):
 
591
        """Our format is present if the transport ends in '.not/'."""
 
592
        if transport.has('.not'):
 
593
            return NotBzrDirFormat()
 
594
 
 
595
 
 
596
class TestNotBzrDir(TestCaseWithTransport):
 
597
    """Tests for using the bzrdir api with a non .bzr based disk format.
 
598
    
 
599
    If/when one of these is in the core, we can let the implementation tests
 
600
    verify this works.
 
601
    """
 
602
 
 
603
    def test_create_and_find_format(self):
 
604
        # create a .notbzr dir 
 
605
        format = NotBzrDirFormat()
 
606
        dir = format.initialize(self.get_url())
 
607
        self.assertIsInstance(dir, NotBzrDir)
 
608
        # now probe for it.
 
609
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
 
610
        try:
 
611
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
 
612
                get_transport(self.get_url()))
 
613
            self.assertIsInstance(found, NotBzrDirFormat)
 
614
        finally:
 
615
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
 
616
 
 
617
    def test_included_in_known_formats(self):
 
618
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
 
619
        try:
 
620
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
 
621
            for format in formats:
 
622
                if isinstance(format, NotBzrDirFormat):
 
623
                    return
 
624
            self.fail("No NotBzrDirFormat in %s" % formats)
 
625
        finally:
 
626
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
627
 
 
628
 
 
629
class NonLocalTests(TestCaseWithTransport):
 
630
    """Tests for bzrdir static behaviour on non local paths."""
 
631
 
 
632
    def setUp(self):
 
633
        super(NonLocalTests, self).setUp()
 
634
        self.transport_server = MemoryServer
 
635
    
 
636
    def test_create_branch_convenience(self):
 
637
        # outside a repo the default convenience output is a repo+branch_tree
 
638
        format = bzrdir.format_registry.make_bzrdir('knit')
 
639
        branch = bzrdir.BzrDir.create_branch_convenience(
 
640
            self.get_url('foo'), format=format)
 
641
        self.assertRaises(errors.NoWorkingTree,
 
642
                          branch.bzrdir.open_workingtree)
 
643
        branch.bzrdir.open_repository()
 
644
 
 
645
    def test_create_branch_convenience_force_tree_not_local_fails(self):
 
646
        # outside a repo the default convenience output is a repo+branch_tree
 
647
        format = bzrdir.format_registry.make_bzrdir('knit')
 
648
        self.assertRaises(errors.NotLocalUrl,
 
649
            bzrdir.BzrDir.create_branch_convenience,
 
650
            self.get_url('foo'),
 
651
            force_new_tree=True,
 
652
            format=format)
 
653
        t = get_transport(self.get_url('.'))
 
654
        self.assertFalse(t.has('foo'))
 
655
 
 
656
    def test_clone(self):
 
657
        # clone into a nonlocal path works
 
658
        format = bzrdir.format_registry.make_bzrdir('knit')
 
659
        branch = bzrdir.BzrDir.create_branch_convenience('local',
 
660
                                                         format=format)
 
661
        branch.bzrdir.open_workingtree()
 
662
        result = branch.bzrdir.clone(self.get_url('remote'))
 
663
        self.assertRaises(errors.NoWorkingTree,
 
664
                          result.open_workingtree)
 
665
        result.open_branch()
 
666
        result.open_repository()
 
667
 
 
668
 
 
669
class TestRemoteSFTP(test_sftp_transport.TestCaseWithSFTPServer):
 
670
 
 
671
    def test_open_containing_tree_or_branch(self):
 
672
        tree = self.make_branch_and_tree('tree')
 
673
        bzrdir.BzrDir.open_containing_tree_or_branch(self.get_url('tree'))