/brz/remove-bazaar

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