/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2696.3.1 by Martin Pool
(broken) start switching format to dirstate-tags
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 (
2100.3.35 by Aaron Bentley
equality operations on bzrdir
26
    bzrdir,
27
    errors,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
28
    help_topics,
2100.3.35 by Aaron Bentley
equality operations on bzrdir
29
    repository,
2204.4.12 by Aaron Bentley
Deprecate bzrdir.BzrDirFormat.set_default_format
30
    symbol_versioning,
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
31
    urlutils,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
32
    workingtree,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
33
    )
1508.1.25 by Robert Collins
Update per review comments.
34
import bzrlib.branch
1534.4.39 by Robert Collins
Basic BzrDir support.
35
from bzrlib.errors import (NotBranchError,
36
                           UnknownFormatError,
37
                           UnsupportedFormatError,
38
                           )
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
39
from bzrlib.symbol_versioning import (
40
    zero_ninetyone,
41
    )
2164.2.16 by Vincent Ladeuil
Add tests.
42
from bzrlib.tests import (
43
    TestCase,
44
    TestCaseWithTransport,
45
    test_sftp_transport
46
    )
2004.1.25 by v.ladeuil+lp at free
Shuffle http related test code. Hopefully it ends up at the right place :)
47
from bzrlib.tests.HttpServer import HttpServer
2164.2.16 by Vincent Ladeuil
Add tests.
48
from bzrlib.tests.HTTPTestUtil import (
49
    TestCaseWithTwoWebservers,
50
    HTTPServerRedirecting,
51
    )
52
from bzrlib.tests.test_http import TestWithTransport_pycurl
1534.4.39 by Robert Collins
Basic BzrDir support.
53
from bzrlib.transport import get_transport
2164.2.16 by Vincent Ladeuil
Add tests.
54
from bzrlib.transport.http._urllib import HttpTransport_urllib
1534.4.39 by Robert Collins
Basic BzrDir support.
55
from bzrlib.transport.memory import MemoryServer
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
56
from bzrlib.repofmt import knitrepo, weaverepo
1534.4.39 by Robert Collins
Basic BzrDir support.
57
58
59
class TestDefaultFormat(TestCase):
60
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
61
    def test_get_set_default_format(self):
1534.4.39 by Robert Collins
Basic BzrDir support.
62
        old_format = bzrdir.BzrDirFormat.get_default_format()
63
        # default is BzrDirFormat6
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
64
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
2204.4.12 by Aaron Bentley
Deprecate bzrdir.BzrDirFormat.set_default_format
65
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
66
                             bzrdir.BzrDirFormat.set_default_format, 
67
                             SampleBzrDirFormat())
1534.4.39 by Robert Collins
Basic BzrDir support.
68
        # creating a bzr dir should now create an instrumented dir.
69
        try:
1685.1.42 by John Arbash Meinel
A couple more fixes to make sure memory:/// works correctly.
70
            result = bzrdir.BzrDir.create('memory:///')
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
71
            self.failUnless(isinstance(result, SampleBzrDir))
1534.4.39 by Robert Collins
Basic BzrDir support.
72
        finally:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
73
            self.applyDeprecated(symbol_versioning.zero_fourteen,
74
                bzrdir.BzrDirFormat.set_default_format, old_format)
1534.4.39 by Robert Collins
Basic BzrDir support.
75
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
76
77
2204.4.1 by Aaron Bentley
Add 'formats' help topic
78
class TestFormatRegistry(TestCase):
79
80
    def make_format_registry(self):
81
        my_format_registry = bzrdir.BzrDirFormatRegistry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
82
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
83
            '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
84
            ' repositories', deprecated=True)
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
85
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
86
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
87
        my_format_registry.register_metadir('knit',
88
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
89
            'Format using knits',
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
90
            )
2204.4.1 by Aaron Bentley
Add 'formats' help topic
91
        my_format_registry.set_default('knit')
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
92
        my_format_registry.register_metadir(
2230.3.53 by Aaron Bentley
Merge bzr.dev
93
            'branch6',
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
94
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2230.3.53 by Aaron Bentley
Merge bzr.dev
95
            'Experimental successor to knit.  Use at your own risk.',
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
96
            branch_format='bzrlib.branch.BzrBranchFormat6')
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
97
        my_format_registry.register_metadir(
98
            'hidden format',
99
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
100
            'Experimental successor to knit.  Use at your own risk.',
101
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
102
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
103
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
104
            ' repositories', hidden=True)
105
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
106
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
107
            hidden=True)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
108
        return my_format_registry
109
110
    def test_format_registry(self):
111
        my_format_registry = self.make_format_registry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
112
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
113
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
114
        my_bzrdir = my_format_registry.make_bzrdir('weave')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
115
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
116
        my_bzrdir = my_format_registry.make_bzrdir('default')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
117
        self.assertIsInstance(my_bzrdir.repository_format, 
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
118
            knitrepo.RepositoryFormatKnit1)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
119
        my_bzrdir = my_format_registry.make_bzrdir('knit')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
120
        self.assertIsInstance(my_bzrdir.repository_format, 
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
121
            knitrepo.RepositoryFormatKnit1)
2230.3.1 by Aaron Bentley
Get branch6 creation working
122
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
2230.3.55 by Aaron Bentley
Updates from review
123
        self.assertIsInstance(my_bzrdir.get_branch_format(),
2230.3.1 by Aaron Bentley
Get branch6 creation working
124
                              bzrlib.branch.BzrBranchFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
125
126
    def test_get_help(self):
127
        my_format_registry = self.make_format_registry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
128
        self.assertEqual('Format registered lazily',
129
                         my_format_registry.get_help('lazy'))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
130
        self.assertEqual('Format using knits', 
2204.4.1 by Aaron Bentley
Add 'formats' help topic
131
                         my_format_registry.get_help('knit'))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
132
        self.assertEqual('Format using knits', 
2204.4.1 by Aaron Bentley
Add 'formats' help topic
133
                         my_format_registry.get_help('default'))
134
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
135
                         ' checkouts or shared repositories', 
136
                         my_format_registry.get_help('weave'))
137
        
138
    def test_help_topic(self):
139
        topics = help_topics.HelpTopicRegistry()
140
        topics.register('formats', self.make_format_registry().help_topic, 
141
                        'Directory formats')
142
        topic = topics.get_detail('formats')
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
143
        new, deprecated = topic.split('Deprecated formats')
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
144
        self.assertContainsRe(new, 'These formats can be used')
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
145
        self.assertContainsRe(new, 
2666.1.8 by Ian Clatworthy
Fix storage formats help test
146
                ':knit:\n    \(native\) \(default\) Format using knits\n')
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
147
        self.assertContainsRe(deprecated, 
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
148
                ':lazy:\n    \(native\) Format registered lazily\n')
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
149
        self.assertNotContainsRe(new, 'hidden')
2204.4.1 by Aaron Bentley
Add 'formats' help topic
150
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
151
    def test_set_default_repository(self):
152
        default_factory = bzrdir.format_registry.get('default')
153
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
154
                       if v == default_factory and k != 'default'][0]
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
155
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
156
        try:
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
157
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
158
                          bzrdir.format_registry.get('default'))
159
            self.assertIs(
160
                repository.RepositoryFormat.get_default_format().__class__,
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
161
                knitrepo.RepositoryFormatKnit3)
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
162
        finally:
163
            bzrdir.format_registry.set_default_repository(old_default)
164
2220.2.25 by Martin Pool
doc
165
1508.1.25 by Robert Collins
Update per review comments.
166
class SampleBranch(bzrlib.branch.Branch):
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
167
    """A dummy branch for guess what, dummy use."""
168
169
    def __init__(self, dir):
170
        self.bzrdir = dir
171
172
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
173
class SampleBzrDir(bzrdir.BzrDir):
174
    """A sample BzrDir implementation to allow testing static methods."""
175
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
176
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
177
        """See BzrDir.create_repository."""
178
        return "A repository"
179
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.
180
    def open_repository(self):
181
        """See BzrDir.open_repository."""
182
        return "A repository"
183
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
184
    def create_branch(self):
185
        """See BzrDir.create_branch."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
186
        return SampleBranch(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
187
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
188
    def create_workingtree(self):
189
        """See BzrDir.create_workingtree."""
190
        return "A tree"
191
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
192
1534.4.39 by Robert Collins
Basic BzrDir support.
193
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
194
    """A sample format
195
196
    this format is initializable, unsupported to aid in testing the 
197
    open and open_downlevel routines.
198
    """
199
200
    def get_format_string(self):
201
        """See BzrDirFormat.get_format_string()."""
202
        return "Sample .bzr dir format."
203
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
204
    def initialize_on_transport(self, t):
1534.4.39 by Robert Collins
Basic BzrDir support.
205
        """Create a bzr dir."""
206
        t.mkdir('.bzr')
1955.3.9 by John Arbash Meinel
Find more occurrances of put() and replace with put_file or put_bytes
207
        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.
208
        return SampleBzrDir(t, self)
1534.4.39 by Robert Collins
Basic BzrDir support.
209
210
    def is_supported(self):
211
        return False
212
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
213
    def open(self, transport, _found=None):
1534.4.39 by Robert Collins
Basic BzrDir support.
214
        return "opened branch."
215
216
217
class TestBzrDirFormat(TestCaseWithTransport):
218
    """Tests for the BzrDirFormat facility."""
219
220
    def test_find_format(self):
221
        # is the right format object found for a branch?
222
        # create a branch with a few known format objects.
223
        # this is not quite the same as 
224
        t = get_transport(self.get_url())
225
        self.build_tree(["foo/", "bar/"], transport=t)
226
        def check_format(format, url):
227
            format.initialize(url)
228
            t = get_transport(url)
229
            found_format = bzrdir.BzrDirFormat.find_format(t)
230
            self.failUnless(isinstance(found_format, format.__class__))
231
        check_format(bzrdir.BzrDirFormat5(), "foo")
232
        check_format(bzrdir.BzrDirFormat6(), "bar")
233
        
234
    def test_find_format_nothing_there(self):
235
        self.assertRaises(NotBranchError,
236
                          bzrdir.BzrDirFormat.find_format,
237
                          get_transport('.'))
238
239
    def test_find_format_unknown_format(self):
240
        t = get_transport(self.get_url())
241
        t.mkdir('.bzr')
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
242
        t.put_bytes('.bzr/branch-format', '')
1534.4.39 by Robert Collins
Basic BzrDir support.
243
        self.assertRaises(UnknownFormatError,
244
                          bzrdir.BzrDirFormat.find_format,
245
                          get_transport('.'))
246
247
    def test_register_unregister_format(self):
248
        format = SampleBzrDirFormat()
249
        url = self.get_url()
250
        # make a bzrdir
251
        format.initialize(url)
252
        # register a format for it.
253
        bzrdir.BzrDirFormat.register_format(format)
254
        # which bzrdir.Open will refuse (not supported)
255
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
256
        # which bzrdir.open_containing will refuse (not supported)
257
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
1534.4.39 by Robert Collins
Basic BzrDir support.
258
        # but open_downlevel will work
259
        t = get_transport(url)
260
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
261
        # unregister the format
262
        bzrdir.BzrDirFormat.unregister_format(format)
263
        # now open_downlevel should fail too.
264
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
265
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
266
    def test_create_repository_deprecated(self):
267
        # new interface is to make the bzrdir, then a repository within that.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
268
        format = SampleBzrDirFormat()
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
269
        repo = self.applyDeprecated(zero_ninetyone,
270
                bzrdir.BzrDir.create_repository,
271
                self.get_url(), format=format)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
272
        self.assertEqual('A repository', repo)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
273
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
274
    def test_create_repository_shared(self):
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
275
        # new interface is to make the bzrdir, then a repository within that.
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
276
        old_format = bzrdir.BzrDirFormat.get_default_format()
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
277
        repo = self.applyDeprecated(zero_ninetyone,
278
                bzrdir.BzrDir.create_repository,
279
                '.', shared=True)
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
280
        self.assertTrue(repo.is_shared())
281
1841.2.2 by Jelmer Vernooij
Add more tests for create_repository().
282
    def test_create_repository_nonshared(self):
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
283
        # new interface is to make the bzrdir, then a repository within that.
1841.2.2 by Jelmer Vernooij
Add more tests for create_repository().
284
        old_format = bzrdir.BzrDirFormat.get_default_format()
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
285
        repo = self.applyDeprecated(zero_ninetyone,
286
                bzrdir.BzrDir.create_repository,
287
                '.')
1841.2.2 by Jelmer Vernooij
Add more tests for create_repository().
288
        self.assertFalse(repo.is_shared())
289
1534.6.10 by Robert Collins
Finish use of repositories support.
290
    def test_create_repository_under_shared(self):
291
        # an explicit create_repository always does so.
292
        # we trust the format is right from the 'create_repository test'
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
293
        # new interface is to make the bzrdir, then a repository within that.
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
294
        format = bzrdir.format_registry.make_bzrdir('knit')
295
        self.make_repository('.', shared=True, format=format)
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
296
        repo = self.applyDeprecated(zero_ninetyone,
297
                bzrdir.BzrDir.create_repository,
298
                self.get_url('child'),
299
                format=format)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
300
        self.assertTrue(isinstance(repo, repository.Repository))
301
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
1534.6.10 by Robert Collins
Finish use of repositories support.
302
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.
303
    def test_create_branch_and_repo_uses_default(self):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
304
        format = SampleBzrDirFormat()
2476.3.10 by Vincent Ladeuil
Add a test for create_branch_convenience. Mark some places to test for multiple connections.
305
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
306
                                                      format=format)
307
        self.assertTrue(isinstance(branch, SampleBranch))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
308
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.
309
    def test_create_branch_and_repo_under_shared(self):
310
        # creating a branch and repo in a shared repo uses the
311
        # shared repository
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
312
        format = bzrdir.format_registry.make_bzrdir('knit')
313
        self.make_repository('.', shared=True, format=format)
314
        branch = bzrdir.BzrDir.create_branch_and_repo(
315
            self.get_url('child'), format=format)
316
        self.assertRaises(errors.NoRepositoryPresent,
317
                          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.
318
319
    def test_create_branch_and_repo_under_shared_force_new(self):
320
        # creating a branch and repo in a shared repo can be forced to 
321
        # make a new repo
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
322
        format = bzrdir.format_registry.make_bzrdir('knit')
323
        self.make_repository('.', shared=True, format=format)
324
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
325
                                                      force_new_repo=True,
326
                                                      format=format)
327
        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.
328
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
329
    def test_create_standalone_working_tree(self):
330
        format = SampleBzrDirFormat()
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
331
        # note this is deliberately readonly, as this failure should 
332
        # occur before any writes.
333
        self.assertRaises(errors.NotLocalUrl,
334
                          bzrdir.BzrDir.create_standalone_workingtree,
335
                          self.get_readonly_url(), format=format)
336
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
337
                                                           format=format)
338
        self.assertEqual('A tree', tree)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
339
1534.6.10 by Robert Collins
Finish use of repositories support.
340
    def test_create_standalone_working_tree_under_shared_repo(self):
341
        # create standalone working tree always makes a repo.
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
342
        format = bzrdir.format_registry.make_bzrdir('knit')
343
        self.make_repository('.', shared=True, format=format)
344
        # note this is deliberately readonly, as this failure should 
345
        # occur before any writes.
346
        self.assertRaises(errors.NotLocalUrl,
347
                          bzrdir.BzrDir.create_standalone_workingtree,
348
                          self.get_readonly_url('child'), format=format)
349
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
350
            format=format)
351
        tree.bzrdir.open_repository()
1534.6.10 by Robert Collins
Finish use of repositories support.
352
353
    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.
354
        # 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
355
        format = bzrdir.format_registry.make_bzrdir('knit')
356
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
357
        branch.bzrdir.open_workingtree()
358
        branch.bzrdir.open_repository()
1534.6.10 by Robert Collins
Finish use of repositories support.
359
2476.3.10 by Vincent Ladeuil
Add a test for create_branch_convenience. Mark some places to test for multiple connections.
360
    def test_create_branch_convenience_possible_transports(self):
361
        """Check that the optional 'possible_transports' is recognized"""
362
        format = bzrdir.format_registry.make_bzrdir('knit')
363
        t = self.get_transport()
364
        branch = bzrdir.BzrDir.create_branch_convenience(
365
            '.', format=format, possible_transports=[t])
366
        branch.bzrdir.open_workingtree()
367
        branch.bzrdir.open_repository()
368
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
369
    def test_create_branch_convenience_root(self):
370
        """Creating a branch at the root of a fs should work."""
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
371
        self.vfs_transport_factory = MemoryServer
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
372
        # 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
373
        format = bzrdir.format_registry.make_bzrdir('knit')
374
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
375
                                                         format=format)
376
        self.assertRaises(errors.NoWorkingTree,
377
                          branch.bzrdir.open_workingtree)
378
        branch.bzrdir.open_repository()
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
379
1534.6.10 by Robert Collins
Finish use of repositories support.
380
    def test_create_branch_convenience_under_shared_repo(self):
381
        # inside a repo the default convenience output is a branch+ follow the
382
        # repo tree policy
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
383
        format = bzrdir.format_registry.make_bzrdir('knit')
384
        self.make_repository('.', shared=True, format=format)
385
        branch = bzrdir.BzrDir.create_branch_convenience('child',
386
            format=format)
387
        branch.bzrdir.open_workingtree()
388
        self.assertRaises(errors.NoRepositoryPresent,
389
                          branch.bzrdir.open_repository)
1534.6.10 by Robert Collins
Finish use of repositories support.
390
            
391
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
392
        # inside a repo the default convenience output is a branch+ follow the
393
        # repo tree policy but we can override that
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
394
        format = bzrdir.format_registry.make_bzrdir('knit')
395
        self.make_repository('.', shared=True, format=format)
396
        branch = bzrdir.BzrDir.create_branch_convenience('child',
397
            force_new_tree=False, format=format)
398
        self.assertRaises(errors.NoWorkingTree,
399
                          branch.bzrdir.open_workingtree)
400
        self.assertRaises(errors.NoRepositoryPresent,
401
                          branch.bzrdir.open_repository)
1534.6.10 by Robert Collins
Finish use of repositories support.
402
            
403
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
404
        # inside a repo the default convenience output is a branch+ follow the
405
        # repo tree policy
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
406
        format = bzrdir.format_registry.make_bzrdir('knit')
407
        repo = self.make_repository('.', shared=True, format=format)
408
        repo.set_make_working_trees(False)
409
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
410
                                                         format=format)
411
        self.assertRaises(errors.NoWorkingTree,
412
                          branch.bzrdir.open_workingtree)
413
        self.assertRaises(errors.NoRepositoryPresent,
414
                          branch.bzrdir.open_repository)
1534.6.10 by Robert Collins
Finish use of repositories support.
415
416
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
417
        # inside a repo the default convenience output is a branch+ follow the
418
        # repo tree policy but we can override that
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
419
        format = bzrdir.format_registry.make_bzrdir('knit')
420
        repo = self.make_repository('.', shared=True, format=format)
421
        repo.set_make_working_trees(False)
422
        branch = bzrdir.BzrDir.create_branch_convenience('child',
423
            force_new_tree=True, format=format)
424
        branch.bzrdir.open_workingtree()
425
        self.assertRaises(errors.NoRepositoryPresent,
426
                          branch.bzrdir.open_repository)
1534.6.10 by Robert Collins
Finish use of repositories support.
427
428
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
429
        # inside a repo the default convenience output is overridable to give
430
        # repo+branch+tree
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
431
        format = bzrdir.format_registry.make_bzrdir('knit')
432
        self.make_repository('.', shared=True, format=format)
433
        branch = bzrdir.BzrDir.create_branch_convenience('child',
434
            force_new_repo=True, format=format)
435
        branch.bzrdir.open_repository()
436
        branch.bzrdir.open_workingtree()
1534.6.10 by Robert Collins
Finish use of repositories support.
437
1534.4.39 by Robert Collins
Basic BzrDir support.
438
439
class ChrootedTests(TestCaseWithTransport):
440
    """A support class that provides readonly urls outside the local namespace.
441
442
    This is done by checking if self.transport_server is a MemoryServer. if it
443
    is then we are chrooted already, if it is not then an HttpServer is used
444
    for readonly urls.
445
    """
446
447
    def setUp(self):
448
        super(ChrootedTests, self).setUp()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
449
        if not self.vfs_transport_factory == MemoryServer:
1534.4.39 by Robert Collins
Basic BzrDir support.
450
            self.transport_readonly_server = HttpServer
451
452
    def test_open_containing(self):
453
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
454
                          self.get_readonly_url(''))
455
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
456
                          self.get_readonly_url('g/p/q'))
457
        control = bzrdir.BzrDir.create(self.get_url())
458
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
459
        self.assertEqual('', relpath)
460
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
461
        self.assertEqual('g/p/q', relpath)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
462
1534.6.11 by Robert Collins
Review feedback.
463
    def test_open_containing_from_transport(self):
464
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
1534.6.3 by Robert Collins
find_repository sufficiently robust.
465
                          get_transport(self.get_readonly_url('')))
1534.6.11 by Robert Collins
Review feedback.
466
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
1534.6.3 by Robert Collins
find_repository sufficiently robust.
467
                          get_transport(self.get_readonly_url('g/p/q')))
468
        control = bzrdir.BzrDir.create(self.get_url())
1534.6.11 by Robert Collins
Review feedback.
469
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
1534.6.3 by Robert Collins
find_repository sufficiently robust.
470
            get_transport(self.get_readonly_url('')))
471
        self.assertEqual('', relpath)
1534.6.11 by Robert Collins
Review feedback.
472
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
1534.6.3 by Robert Collins
find_repository sufficiently robust.
473
            get_transport(self.get_readonly_url('g/p/q')))
474
        self.assertEqual('g/p/q', relpath)
475
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
476
    def test_open_containing_tree_or_branch(self):
477
        def local_branch_path(branch):
2215.3.4 by Aaron Bentley
rewrap some text
478
             return os.path.realpath(
479
                urlutils.local_path_from_url(branch.base))
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
480
481
        self.make_branch_and_tree('topdir')
482
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
483
            'topdir/foo')
2215.3.7 by Aaron Bentley
Remove (new) trailing whitespace
484
        self.assertEqual(os.path.realpath('topdir'),
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
485
                         os.path.realpath(tree.basedir))
2215.3.7 by Aaron Bentley
Remove (new) trailing whitespace
486
        self.assertEqual(os.path.realpath('topdir'),
2215.3.4 by Aaron Bentley
rewrap some text
487
                         local_branch_path(branch))
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
488
        self.assertIs(tree.bzrdir, branch.bzrdir)
489
        self.assertEqual('foo', relpath)
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
490
        # opening from non-local should not return the tree
491
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
492
            self.get_readonly_url('topdir/foo'))
493
        self.assertEqual(None, tree)
494
        self.assertEqual('foo', relpath)
495
        # without a tree:
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
496
        self.make_branch('topdir/foo')
497
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
498
            'topdir/foo')
499
        self.assertIs(tree, None)
2215.3.7 by Aaron Bentley
Remove (new) trailing whitespace
500
        self.assertEqual(os.path.realpath('topdir/foo'),
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
501
                         local_branch_path(branch))
502
        self.assertEqual('', relpath)
503
1910.11.5 by Andrew Bennetts
Add tests for BzrDir.open_from_transport.
504
    def test_open_from_transport(self):
505
        # transport pointing at bzrdir should give a bzrdir with root transport
506
        # set to the given transport
507
        control = bzrdir.BzrDir.create(self.get_url())
508
        transport = get_transport(self.get_url())
509
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
510
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
511
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
512
        
513
    def test_open_from_transport_no_bzrdir(self):
514
        transport = get_transport(self.get_url())
515
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
516
                          transport)
517
518
    def test_open_from_transport_bzrdir_in_parent(self):
519
        control = bzrdir.BzrDir.create(self.get_url())
520
        transport = get_transport(self.get_url())
521
        transport.mkdir('subdir')
522
        transport = transport.clone('subdir')
523
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
524
                          transport)
525
2100.3.28 by Aaron Bentley
Make sprout recursive
526
    def test_sprout_recursive(self):
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
527
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
2100.3.28 by Aaron Bentley
Make sprout recursive
528
        sub_tree = self.make_branch_and_tree('tree1/subtree',
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
529
            format='dirstate-with-subtree')
2100.3.28 by Aaron Bentley
Make sprout recursive
530
        tree.add_reference(sub_tree)
531
        self.build_tree(['tree1/subtree/file'])
532
        sub_tree.add('file')
533
        tree.commit('Initial commit')
534
        tree.bzrdir.sprout('tree2')
535
        self.failUnlessExists('tree2/subtree/file')
536
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
537
    def test_cloning_metadir(self):
538
        """Ensure that cloning metadir is suitable"""
2100.3.34 by Aaron Bentley
Fix BzrDir.cloning_metadir with no format
539
        bzrdir = self.make_bzrdir('bzrdir')
540
        bzrdir.cloning_metadir()
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
541
        branch = self.make_branch('branch', format='knit')
542
        format = branch.bzrdir.cloning_metadir()
543
        self.assertIsInstance(format.workingtree_format,
2255.2.174 by Martin Pool
remove AB1 WorkingTree and experimental-knit3
544
            workingtree.WorkingTreeFormat3)
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
545
546
    def test_sprout_recursive_treeless(self):
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
547
        tree = self.make_branch_and_tree('tree1',
548
            format='dirstate-with-subtree')
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
549
        sub_tree = self.make_branch_and_tree('tree1/subtree',
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
550
            format='dirstate-with-subtree')
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
551
        tree.add_reference(sub_tree)
552
        self.build_tree(['tree1/subtree/file'])
553
        sub_tree.add('file')
554
        tree.commit('Initial commit')
555
        tree.bzrdir.destroy_workingtree()
556
        repo = self.make_repository('repo', shared=True,
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
557
            format='dirstate-with-subtree')
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
558
        repo.set_make_working_trees(False)
559
        tree.bzrdir.sprout('repo/tree2')
560
        self.failUnlessExists('repo/tree2/subtree')
561
        self.failIfExists('repo/tree2/subtree/file')
562
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
563
564
class TestMeta1DirFormat(TestCaseWithTransport):
565
    """Tests specific to the meta1 dir format."""
566
567
    def test_right_base_dirs(self):
568
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
569
        t = dir.transport
570
        branch_base = t.clone('branch').base
571
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
572
        self.assertEqual(branch_base,
1508.1.25 by Robert Collins
Update per review comments.
573
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
574
        repository_base = t.clone('repository').base
575
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
576
        self.assertEqual(repository_base,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
577
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
578
        checkout_base = t.clone('checkout').base
579
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
580
        self.assertEqual(checkout_base,
581
                         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.
582
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
583
    def test_meta1dir_uses_lockdir(self):
584
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
585
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
586
        t = dir.transport
587
        self.assertIsDirectory('branch-lock', t)
588
2100.3.35 by Aaron Bentley
equality operations on bzrdir
589
    def test_comparison(self):
590
        """Equality and inequality behave properly.
591
592
        Metadirs should compare equal iff they have the same repo, branch and
593
        tree formats.
594
        """
595
        mydir = bzrdir.format_registry.make_bzrdir('knit')
596
        self.assertEqual(mydir, mydir)
597
        self.assertFalse(mydir != mydir)
598
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
599
        self.assertEqual(otherdir, mydir)
600
        self.assertFalse(otherdir != mydir)
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
601
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
2100.3.35 by Aaron Bentley
equality operations on bzrdir
602
        self.assertNotEqual(otherdir2, mydir)
603
        self.assertFalse(otherdir2 == mydir)
604
2255.12.1 by Robert Collins
Implement upgrade for working trees.
605
    def test_needs_conversion_different_working_tree(self):
606
        # meta1dirs need an conversion if any element is not the default.
607
        old_format = bzrdir.BzrDirFormat.get_default_format()
608
        # test with 
609
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
610
        bzrdir.BzrDirFormat._set_default_format(new_default)
611
        try:
612
            tree = self.make_branch_and_tree('tree', format='knit')
613
            self.assertTrue(tree.bzrdir.needs_format_conversion())
614
        finally:
615
            bzrdir.BzrDirFormat._set_default_format(old_format)
616
617
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
618
class TestFormat5(TestCaseWithTransport):
619
    """Tests specific to the version 5 bzrdir format."""
620
621
    def test_same_lockfiles_between_tree_repo_branch(self):
622
        # this checks that only a single lockfiles instance is created 
623
        # for format 5 objects
624
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
625
        def check_dir_components_use_same_lock(dir):
626
            ctrl_1 = dir.open_repository().control_files
627
            ctrl_2 = dir.open_branch().control_files
628
            ctrl_3 = dir.open_workingtree()._control_files
629
            self.assertTrue(ctrl_1 is ctrl_2)
630
            self.assertTrue(ctrl_2 is ctrl_3)
631
        check_dir_components_use_same_lock(dir)
632
        # and if we open it normally.
633
        dir = bzrdir.BzrDir.open(self.get_url())
634
        check_dir_components_use_same_lock(dir)
635
    
1534.5.16 by Robert Collins
Review feedback.
636
    def test_can_convert(self):
637
        # format 5 dirs are convertable
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
638
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
639
        self.assertTrue(dir.can_convert_format())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
640
    
1534.5.16 by Robert Collins
Review feedback.
641
    def test_needs_conversion(self):
642
        # 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.
643
        # and they start of not the default.
644
        old_format = bzrdir.BzrDirFormat.get_default_format()
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
645
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
646
        try:
647
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
648
            self.assertFalse(dir.needs_format_conversion())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
649
        finally:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
650
            bzrdir.BzrDirFormat._set_default_format(old_format)
1534.5.16 by Robert Collins
Review feedback.
651
        self.assertTrue(dir.needs_format_conversion())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
652
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
653
654
class TestFormat6(TestCaseWithTransport):
655
    """Tests specific to the version 6 bzrdir format."""
656
657
    def test_same_lockfiles_between_tree_repo_branch(self):
658
        # this checks that only a single lockfiles instance is created 
659
        # for format 6 objects
660
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
661
        def check_dir_components_use_same_lock(dir):
662
            ctrl_1 = dir.open_repository().control_files
663
            ctrl_2 = dir.open_branch().control_files
664
            ctrl_3 = dir.open_workingtree()._control_files
665
            self.assertTrue(ctrl_1 is ctrl_2)
666
            self.assertTrue(ctrl_2 is ctrl_3)
667
        check_dir_components_use_same_lock(dir)
668
        # and if we open it normally.
669
        dir = bzrdir.BzrDir.open(self.get_url())
670
        check_dir_components_use_same_lock(dir)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
671
    
1534.5.16 by Robert Collins
Review feedback.
672
    def test_can_convert(self):
673
        # format 6 dirs are convertable
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
674
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
675
        self.assertTrue(dir.can_convert_format())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
676
    
1534.5.16 by Robert Collins
Review feedback.
677
    def test_needs_conversion(self):
678
        # 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.
679
        old_format = bzrdir.BzrDirFormat.get_default_format()
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
680
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
681
        try:
682
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
683
            self.assertTrue(dir.needs_format_conversion())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
684
        finally:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
685
            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.
686
687
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
688
class NotBzrDir(bzrlib.bzrdir.BzrDir):
689
    """A non .bzr based control directory."""
690
691
    def __init__(self, transport, format):
692
        self._format = format
693
        self.root_transport = transport
694
        self.transport = transport.clone('.not')
695
696
697
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
698
    """A test class representing any non-.bzr based disk format."""
699
700
    def initialize_on_transport(self, transport):
701
        """Initialize a new .not dir in the base directory of a Transport."""
702
        transport.mkdir('.not')
703
        return self.open(transport)
704
705
    def open(self, transport):
706
        """Open this directory."""
707
        return NotBzrDir(transport, self)
708
709
    @classmethod
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
710
    def _known_formats(self):
711
        return set([NotBzrDirFormat()])
712
713
    @classmethod
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
714
    def probe_transport(self, transport):
715
        """Our format is present if the transport ends in '.not/'."""
1733.1.2 by Robert Collins
bugfix test for non .bzrdir support.
716
        if transport.has('.not'):
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
717
            return NotBzrDirFormat()
718
719
720
class TestNotBzrDir(TestCaseWithTransport):
721
    """Tests for using the bzrdir api with a non .bzr based disk format.
722
    
723
    If/when one of these is in the core, we can let the implementation tests
724
    verify this works.
725
    """
726
727
    def test_create_and_find_format(self):
728
        # create a .notbzr dir 
729
        format = NotBzrDirFormat()
730
        dir = format.initialize(self.get_url())
731
        self.assertIsInstance(dir, NotBzrDir)
732
        # now probe for it.
733
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
734
        try:
735
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
736
                get_transport(self.get_url()))
1733.1.2 by Robert Collins
bugfix test for non .bzrdir support.
737
            self.assertIsInstance(found, NotBzrDirFormat)
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
738
        finally:
739
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
740
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
741
    def test_included_in_known_formats(self):
742
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
743
        try:
744
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
745
            for format in formats:
746
                if isinstance(format, NotBzrDirFormat):
747
                    return
748
            self.fail("No NotBzrDirFormat in %s" % formats)
749
        finally:
750
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
751
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
752
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.
753
class NonLocalTests(TestCaseWithTransport):
754
    """Tests for bzrdir static behaviour on non local paths."""
755
756
    def setUp(self):
757
        super(NonLocalTests, self).setUp()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
758
        self.vfs_transport_factory = MemoryServer
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.
759
    
760
    def test_create_branch_convenience(self):
761
        # 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
762
        format = bzrdir.format_registry.make_bzrdir('knit')
763
        branch = bzrdir.BzrDir.create_branch_convenience(
764
            self.get_url('foo'), format=format)
765
        self.assertRaises(errors.NoWorkingTree,
766
                          branch.bzrdir.open_workingtree)
767
        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.
768
769
    def test_create_branch_convenience_force_tree_not_local_fails(self):
770
        # 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
771
        format = bzrdir.format_registry.make_bzrdir('knit')
772
        self.assertRaises(errors.NotLocalUrl,
773
            bzrdir.BzrDir.create_branch_convenience,
774
            self.get_url('foo'),
775
            force_new_tree=True,
776
            format=format)
777
        t = get_transport(self.get_url('.'))
778
        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.
779
1563.2.38 by Robert Collins
make push preserve tree formats.
780
    def test_clone(self):
781
        # clone into a nonlocal path works
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
782
        format = bzrdir.format_registry.make_bzrdir('knit')
783
        branch = bzrdir.BzrDir.create_branch_convenience('local',
784
                                                         format=format)
1563.2.38 by Robert Collins
make push preserve tree formats.
785
        branch.bzrdir.open_workingtree()
786
        result = branch.bzrdir.clone(self.get_url('remote'))
787
        self.assertRaises(errors.NoWorkingTree,
788
                          result.open_workingtree)
789
        result.open_branch()
790
        result.open_repository()
791
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
792
    def test_checkout_metadir(self):
793
        # checkout_metadir has reasonable working tree format even when no
794
        # working tree is present
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
795
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
796
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
797
        checkout_format = my_bzrdir.checkout_metadir()
798
        self.assertIsInstance(checkout_format.workingtree_format,
799
                              workingtree.WorkingTreeFormat3)
2100.3.22 by Aaron Bentley
merge from bzr.dev
800
2215.3.5 by Aaron Bentley
Add support for remote ls
801
2164.2.16 by Vincent Ladeuil
Add tests.
802
class TestHTTPRedirectionLoop(object):
803
    """Test redirection loop between two http servers.
804
805
    This MUST be used by daughter classes that also inherit from
806
    TestCaseWithTwoWebservers.
807
808
    We can't inherit directly from TestCaseWithTwoWebservers or the
809
    test framework will try to create an instance which cannot
810
    run, its implementation being incomplete. 
811
    """
812
813
    # Should be defined by daughter classes to ensure redirection
2164.2.17 by Vincent Ladeuil
Add comments and fix typos
814
    # still use the same transport implementation (not currently
815
    # enforced as it's a bit tricky to get right (see the FIXME
816
    # in BzrDir.open_from_transport for the unique use case so
817
    # far)
2164.2.16 by Vincent Ladeuil
Add tests.
818
    _qualifier = None
819
820
    def create_transport_readonly_server(self):
821
        return HTTPServerRedirecting()
822
823
    def create_transport_secondary_server(self):
824
        return HTTPServerRedirecting()
825
826
    def setUp(self):
827
        # Both servers redirect to each server creating a loop
828
        super(TestHTTPRedirectionLoop, self).setUp()
829
        # The redirections will point to the new server
830
        self.new_server = self.get_readonly_server()
831
        # The requests to the old server will be redirected
832
        self.old_server = self.get_secondary_server()
833
        # Configure the redirections
834
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
835
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
836
837
    def _qualified_url(self, host, port):
838
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
839
840
    def test_loop(self):
841
        # Starting from either server should loop
842
        old_url = self._qualified_url(self.old_server.host, 
843
                                      self.old_server.port)
844
        oldt = self._transport(old_url)
845
        self.assertRaises(errors.NotBranchError,
846
                          bzrdir.BzrDir.open_from_transport, oldt)
847
        new_url = self._qualified_url(self.new_server.host, 
848
                                      self.new_server.port)
849
        newt = self._transport(new_url)
850
        self.assertRaises(errors.NotBranchError,
851
                          bzrdir.BzrDir.open_from_transport, newt)
852
853
854
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
855
                                  TestCaseWithTwoWebservers):
856
    """Tests redirections for urllib implementation"""
857
858
    _qualifier = 'urllib'
859
    _transport = HttpTransport_urllib
860
861
862
863
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
864
                                  TestHTTPRedirectionLoop,
865
                                  TestCaseWithTwoWebservers):
866
    """Tests redirections for pycurl implementation"""
867
868
    _qualifier = 'pycurl'