/brz/remove-bazaar

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