/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: Aaron Bentley
  • Date: 2007-02-21 06:06:06 UTC
  • mto: (2255.6.1 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: aaron.bentley@utoronto.ca-20070221060606-unjaailciijp12ab
rename working tree format 4 to AB1 everywhere

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