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