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