/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-01-02 20:15:07 UTC
  • mto: (1551.9.35 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2237.
  • Revision ID: abentley@panoramicfeedback.com-20070102201507-zej9hrf5cxxjkypj
Get ls working on branches

Show diffs side-by-side

added added

removed removed

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