~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Martin Pool
  • Date: 2005-09-30 05:56:05 UTC
  • mto: (1185.14.2)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050930055605-a2c534529b392a7d
- fix upgrade for transport changes

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