/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2006-2010 Canonical Ltd
3650.3.10 by Aaron Bentley
Ensure that sprout chooses a rich-root format as needed
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1534.4.39 by Robert Collins
Basic BzrDir support.
16
17
"""Tests for the BzrDir facility and any format specific tests.
18
4523.1.4 by Martin Pool
Rename remaining *_implementations tests
19
For interface contract tests, see tests/per_bzr_dir.
1534.4.39 by Robert Collins
Basic BzrDir support.
20
"""
21
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
22
import os
3023.1.3 by Alexander Belchenko
John's review
23
import subprocess
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
24
import sys
1534.4.39 by Robert Collins
Basic BzrDir support.
25
2204.4.1 by Aaron Bentley
Add 'formats' help topic
26
from bzrlib import (
2100.3.35 by Aaron Bentley
equality operations on bzrdir
27
    bzrdir,
28
    errors,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
29
    help_topics,
2100.3.35 by Aaron Bentley
equality operations on bzrdir
30
    repository,
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
31
    osutils,
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
32
    remote,
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,
5051.3.3 by Jelmer Vernooij
Add tests for colo branches.
39
                           NoColocatedBranchSupport,
1534.4.39 by Robert Collins
Basic BzrDir support.
40
                           UnknownFormatError,
41
                           UnsupportedFormatError,
42
                           )
2164.2.16 by Vincent Ladeuil
Add tests.
43
from bzrlib.tests import (
44
    TestCase,
3583.1.2 by Andrew Bennetts
Add test for fix.
45
    TestCaseWithMemoryTransport,
2164.2.16 by Vincent Ladeuil
Add tests.
46
    TestCaseWithTransport,
3023.1.2 by Alexander Belchenko
Martin's review.
47
    TestSkipped,
2164.2.16 by Vincent Ladeuil
Add tests.
48
    )
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
49
from bzrlib.tests import(
50
    http_server,
51
    http_utils,
2164.2.16 by Vincent Ladeuil
Add tests.
52
    )
53
from bzrlib.tests.test_http import TestWithTransport_pycurl
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
54
from bzrlib.transport import (
55
    get_transport,
56
    memory,
5215.3.2 by Marius Kruger
* Move TestCaseWithMemoryTransport.make_smart_server => TestCaseWithTransport
57
    pathfilter,
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
58
    )
2164.2.16 by Vincent Ladeuil
Add tests.
59
from bzrlib.transport.http._urllib import HttpTransport_urllib
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
60
from bzrlib.transport.nosmart import NoSmartTransportDecorator
61
from bzrlib.transport.readonly import ReadonlyTransportDecorator
4126.1.1 by Andrew Bennetts
Fix bug when pushing stackable branch in unstackable repo to default-stacking target.
62
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
1534.4.39 by Robert Collins
Basic BzrDir support.
63
64
65
class TestDefaultFormat(TestCase):
66
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
67
    def test_get_set_default_format(self):
1534.4.39 by Robert Collins
Basic BzrDir support.
68
        old_format = bzrdir.BzrDirFormat.get_default_format()
69
        # default is BzrDirFormat6
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
70
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
3400.3.6 by Martin Pool
Remove code deprecated prior to 1.1 and its tests
71
        bzrdir.BzrDirFormat._set_default_format(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:
3400.3.6 by Martin Pool
Remove code deprecated prior to 1.1 and its tests
77
            bzrdir.BzrDirFormat._set_default_format(old_format)
1534.4.39 by Robert Collins
Basic BzrDir support.
78
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
79
80
2204.4.1 by Aaron Bentley
Add 'formats' help topic
81
class TestFormatRegistry(TestCase):
82
83
    def make_format_registry(self):
84
        my_format_registry = bzrdir.BzrDirFormatRegistry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
85
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
86
            '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
87
            ' repositories', deprecated=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
88
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir',
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
89
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
90
        my_format_registry.register_metadir('knit',
91
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
92
            'Format using knits',
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
93
            )
2204.4.1 by Aaron Bentley
Add 'formats' help topic
94
        my_format_registry.set_default('knit')
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
95
        my_format_registry.register_metadir(
2230.3.53 by Aaron Bentley
Merge bzr.dev
96
            'branch6',
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
97
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2230.3.53 by Aaron Bentley
Merge bzr.dev
98
            'Experimental successor to knit.  Use at your own risk.',
2939.2.3 by Ian Clatworthy
add tests for experimental formats including help content checking
99
            branch_format='bzrlib.branch.BzrBranchFormat6',
100
            experimental=True)
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
101
        my_format_registry.register_metadir(
102
            'hidden format',
103
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
104
            'Experimental successor to knit.  Use at your own risk.',
105
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
106
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
107
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
108
            ' repositories', hidden=True)
109
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
110
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
111
            hidden=True)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
112
        return my_format_registry
113
114
    def test_format_registry(self):
115
        my_format_registry = self.make_format_registry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
116
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
117
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
118
        my_bzrdir = my_format_registry.make_bzrdir('weave')
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
119
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
120
        my_bzrdir = my_format_registry.make_bzrdir('default')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
121
        self.assertIsInstance(my_bzrdir.repository_format,
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
122
            knitrepo.RepositoryFormatKnit1)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
123
        my_bzrdir = my_format_registry.make_bzrdir('knit')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
124
        self.assertIsInstance(my_bzrdir.repository_format,
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
125
            knitrepo.RepositoryFormatKnit1)
2230.3.1 by Aaron Bentley
Get branch6 creation working
126
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
2230.3.55 by Aaron Bentley
Updates from review
127
        self.assertIsInstance(my_bzrdir.get_branch_format(),
2230.3.1 by Aaron Bentley
Get branch6 creation working
128
                              bzrlib.branch.BzrBranchFormat6)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
129
130
    def test_get_help(self):
131
        my_format_registry = self.make_format_registry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
132
        self.assertEqual('Format registered lazily',
133
                         my_format_registry.get_help('lazy'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
134
        self.assertEqual('Format using knits',
2204.4.1 by Aaron Bentley
Add 'formats' help topic
135
                         my_format_registry.get_help('knit'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
136
        self.assertEqual('Format using knits',
2204.4.1 by Aaron Bentley
Add 'formats' help topic
137
                         my_format_registry.get_help('default'))
138
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
139
                         ' checkouts or shared repositories',
2204.4.1 by Aaron Bentley
Add 'formats' help topic
140
                         my_format_registry.get_help('weave'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
141
2204.4.1 by Aaron Bentley
Add 'formats' help topic
142
    def test_help_topic(self):
143
        topics = help_topics.HelpTopicRegistry()
3892.1.3 by Ian Clatworthy
tweak test suite to support the split up formats topic
144
        registry = self.make_format_registry()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
145
        topics.register('current-formats', registry.help_topic,
3892.1.3 by Ian Clatworthy
tweak test suite to support the split up formats topic
146
                        'Current formats')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
147
        topics.register('other-formats', registry.help_topic,
3892.1.3 by Ian Clatworthy
tweak test suite to support the split up formats topic
148
                        'Other formats')
149
        new = topics.get_detail('current-formats')
150
        rest = topics.get_detail('other-formats')
2939.2.3 by Ian Clatworthy
add tests for experimental formats including help content checking
151
        experimental, deprecated = rest.split('Deprecated formats')
4927.2.10 by Ian Clatworthy
fix test failures
152
        self.assertContainsRe(new, 'formats-help')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
153
        self.assertContainsRe(new,
2666.1.8 by Ian Clatworthy
Fix storage formats help test
154
                ':knit:\n    \(native\) \(default\) Format using knits\n')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
155
        self.assertContainsRe(experimental,
2939.2.3 by Ian Clatworthy
add tests for experimental formats including help content checking
156
                ':branch6:\n    \(native\) Experimental successor to knit')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
157
        self.assertContainsRe(deprecated,
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
158
                ':lazy:\n    \(native\) Format registered lazily\n')
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
159
        self.assertNotContainsRe(new, 'hidden')
2204.4.1 by Aaron Bentley
Add 'formats' help topic
160
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
161
    def test_set_default_repository(self):
162
        default_factory = bzrdir.format_registry.get('default')
163
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
164
                       if v == default_factory and k != 'default'][0]
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
165
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
166
        try:
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
167
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
168
                          bzrdir.format_registry.get('default'))
169
            self.assertIs(
170
                repository.RepositoryFormat.get_default_format().__class__,
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
171
                knitrepo.RepositoryFormatKnit3)
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
172
        finally:
173
            bzrdir.format_registry.set_default_repository(old_default)
174
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
175
    def test_aliases(self):
176
        a_registry = bzrdir.BzrDirFormatRegistry()
177
        a_registry.register('weave', bzrdir.BzrDirFormat6,
178
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
179
            ' repositories', deprecated=True)
180
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
181
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
182
            ' repositories', deprecated=True, alias=True)
183
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
3928.3.4 by John Arbash Meinel
SampleBzrDir now needs to return a real repo from open_repository
184
2220.2.25 by Martin Pool
doc
185
1508.1.25 by Robert Collins
Update per review comments.
186
class SampleBranch(bzrlib.branch.Branch):
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
187
    """A dummy branch for guess what, dummy use."""
188
189
    def __init__(self, dir):
190
        self.bzrdir = dir
191
192
3928.3.4 by John Arbash Meinel
SampleBzrDir now needs to return a real repo from open_repository
193
class SampleRepository(bzrlib.repository.Repository):
194
    """A dummy repo."""
195
196
    def __init__(self, dir):
197
        self.bzrdir = dir
198
199
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
200
class SampleBzrDir(bzrdir.BzrDir):
201
    """A sample BzrDir implementation to allow testing static methods."""
202
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
203
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
204
        """See BzrDir.create_repository."""
205
        return "A repository"
206
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.
207
    def open_repository(self):
208
        """See BzrDir.open_repository."""
3928.3.4 by John Arbash Meinel
SampleBzrDir now needs to return a real repo from open_repository
209
        return SampleRepository(self)
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.
210
5051.3.3 by Jelmer Vernooij
Add tests for colo branches.
211
    def create_branch(self, name=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
212
        """See BzrDir.create_branch."""
5051.3.3 by Jelmer Vernooij
Add tests for colo branches.
213
        if name is not None:
214
            raise NoColocatedBranchSupport(self)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
215
        return SampleBranch(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
216
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
217
    def create_workingtree(self):
218
        """See BzrDir.create_workingtree."""
219
        return "A tree"
220
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
221
1534.4.39 by Robert Collins
Basic BzrDir support.
222
class SampleBzrDirFormat(bzrdir.BzrDirFormat):
223
    """A sample format
224
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
225
    this format is initializable, unsupported to aid in testing the
1534.4.39 by Robert Collins
Basic BzrDir support.
226
    open and open_downlevel routines.
227
    """
228
229
    def get_format_string(self):
230
        """See BzrDirFormat.get_format_string()."""
231
        return "Sample .bzr dir format."
232
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
233
    def initialize_on_transport(self, t):
1534.4.39 by Robert Collins
Basic BzrDir support.
234
        """Create a bzr dir."""
235
        t.mkdir('.bzr')
1955.3.9 by John Arbash Meinel
Find more occurrances of put() and replace with put_file or put_bytes
236
        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.
237
        return SampleBzrDir(t, self)
1534.4.39 by Robert Collins
Basic BzrDir support.
238
239
    def is_supported(self):
240
        return False
241
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
242
    def open(self, transport, _found=None):
1534.4.39 by Robert Collins
Basic BzrDir support.
243
        return "opened branch."
244
245
246
class TestBzrDirFormat(TestCaseWithTransport):
247
    """Tests for the BzrDirFormat facility."""
248
249
    def test_find_format(self):
250
        # is the right format object found for a branch?
251
        # create a branch with a few known format objects.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
252
        # this is not quite the same as
1534.4.39 by Robert Collins
Basic BzrDir support.
253
        t = get_transport(self.get_url())
254
        self.build_tree(["foo/", "bar/"], transport=t)
255
        def check_format(format, url):
256
            format.initialize(url)
257
            t = get_transport(url)
258
            found_format = bzrdir.BzrDirFormat.find_format(t)
259
            self.failUnless(isinstance(found_format, format.__class__))
260
        check_format(bzrdir.BzrDirFormat5(), "foo")
261
        check_format(bzrdir.BzrDirFormat6(), "bar")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
262
1534.4.39 by Robert Collins
Basic BzrDir support.
263
    def test_find_format_nothing_there(self):
264
        self.assertRaises(NotBranchError,
265
                          bzrdir.BzrDirFormat.find_format,
266
                          get_transport('.'))
267
268
    def test_find_format_unknown_format(self):
269
        t = get_transport(self.get_url())
270
        t.mkdir('.bzr')
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
271
        t.put_bytes('.bzr/branch-format', '')
1534.4.39 by Robert Collins
Basic BzrDir support.
272
        self.assertRaises(UnknownFormatError,
273
                          bzrdir.BzrDirFormat.find_format,
274
                          get_transport('.'))
275
276
    def test_register_unregister_format(self):
277
        format = SampleBzrDirFormat()
278
        url = self.get_url()
279
        # make a bzrdir
280
        format.initialize(url)
281
        # register a format for it.
282
        bzrdir.BzrDirFormat.register_format(format)
283
        # which bzrdir.Open will refuse (not supported)
284
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
285
        # which bzrdir.open_containing will refuse (not supported)
286
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
1534.4.39 by Robert Collins
Basic BzrDir support.
287
        # but open_downlevel will work
288
        t = get_transport(url)
289
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
290
        # unregister the format
291
        bzrdir.BzrDirFormat.unregister_format(format)
292
        # now open_downlevel should fail too.
293
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
294
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.
295
    def test_create_branch_and_repo_uses_default(self):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
296
        format = SampleBzrDirFormat()
2476.3.10 by Vincent Ladeuil
Add a test for create_branch_convenience. Mark some places to test for multiple connections.
297
        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
298
                                                      format=format)
299
        self.assertTrue(isinstance(branch, SampleBranch))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
300
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.
301
    def test_create_branch_and_repo_under_shared(self):
302
        # creating a branch and repo in a shared repo uses the
303
        # shared repository
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
304
        format = bzrdir.format_registry.make_bzrdir('knit')
305
        self.make_repository('.', shared=True, format=format)
306
        branch = bzrdir.BzrDir.create_branch_and_repo(
307
            self.get_url('child'), format=format)
308
        self.assertRaises(errors.NoRepositoryPresent,
309
                          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.
310
311
    def test_create_branch_and_repo_under_shared_force_new(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
312
        # creating a branch and repo in a shared repo can be forced to
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.
313
        # make a new repo
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
314
        format = bzrdir.format_registry.make_bzrdir('knit')
315
        self.make_repository('.', shared=True, format=format)
316
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
317
                                                      force_new_repo=True,
318
                                                      format=format)
319
        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.
320
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
321
    def test_create_standalone_working_tree(self):
322
        format = SampleBzrDirFormat()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
323
        # note this is deliberately readonly, as this failure should
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
324
        # occur before any writes.
325
        self.assertRaises(errors.NotLocalUrl,
326
                          bzrdir.BzrDir.create_standalone_workingtree,
327
                          self.get_readonly_url(), format=format)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
328
        tree = bzrdir.BzrDir.create_standalone_workingtree('.',
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
329
                                                           format=format)
330
        self.assertEqual('A tree', tree)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
331
1534.6.10 by Robert Collins
Finish use of repositories support.
332
    def test_create_standalone_working_tree_under_shared_repo(self):
333
        # create standalone working tree always makes a repo.
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
334
        format = bzrdir.format_registry.make_bzrdir('knit')
335
        self.make_repository('.', shared=True, format=format)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
336
        # note this is deliberately readonly, as this failure should
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
337
        # occur before any writes.
338
        self.assertRaises(errors.NotLocalUrl,
339
                          bzrdir.BzrDir.create_standalone_workingtree,
340
                          self.get_readonly_url('child'), format=format)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
341
        tree = bzrdir.BzrDir.create_standalone_workingtree('child',
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
342
            format=format)
343
        tree.bzrdir.open_repository()
1534.6.10 by Robert Collins
Finish use of repositories support.
344
345
    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.
346
        # 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
347
        format = bzrdir.format_registry.make_bzrdir('knit')
348
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
349
        branch.bzrdir.open_workingtree()
350
        branch.bzrdir.open_repository()
1534.6.10 by Robert Collins
Finish use of repositories support.
351
2476.3.10 by Vincent Ladeuil
Add a test for create_branch_convenience. Mark some places to test for multiple connections.
352
    def test_create_branch_convenience_possible_transports(self):
353
        """Check that the optional 'possible_transports' is recognized"""
354
        format = bzrdir.format_registry.make_bzrdir('knit')
355
        t = self.get_transport()
356
        branch = bzrdir.BzrDir.create_branch_convenience(
357
            '.', format=format, possible_transports=[t])
358
        branch.bzrdir.open_workingtree()
359
        branch.bzrdir.open_repository()
360
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
361
    def test_create_branch_convenience_root(self):
362
        """Creating a branch at the root of a fs should work."""
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
363
        self.vfs_transport_factory = memory.MemoryServer
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
364
        # 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
365
        format = bzrdir.format_registry.make_bzrdir('knit')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
366
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(),
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
367
                                                         format=format)
368
        self.assertRaises(errors.NoWorkingTree,
369
                          branch.bzrdir.open_workingtree)
370
        branch.bzrdir.open_repository()
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
371
1534.6.10 by Robert Collins
Finish use of repositories support.
372
    def test_create_branch_convenience_under_shared_repo(self):
373
        # inside a repo the default convenience output is a branch+ follow the
374
        # repo tree policy
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
375
        format = bzrdir.format_registry.make_bzrdir('knit')
376
        self.make_repository('.', shared=True, format=format)
377
        branch = bzrdir.BzrDir.create_branch_convenience('child',
378
            format=format)
379
        branch.bzrdir.open_workingtree()
380
        self.assertRaises(errors.NoRepositoryPresent,
381
                          branch.bzrdir.open_repository)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
382
1534.6.10 by Robert Collins
Finish use of repositories support.
383
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
384
        # inside a repo the default convenience output is a branch+ follow the
385
        # repo tree policy but we can override that
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
386
        format = bzrdir.format_registry.make_bzrdir('knit')
387
        self.make_repository('.', shared=True, format=format)
388
        branch = bzrdir.BzrDir.create_branch_convenience('child',
389
            force_new_tree=False, format=format)
390
        self.assertRaises(errors.NoWorkingTree,
391
                          branch.bzrdir.open_workingtree)
392
        self.assertRaises(errors.NoRepositoryPresent,
393
                          branch.bzrdir.open_repository)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
394
1534.6.10 by Robert Collins
Finish use of repositories support.
395
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
396
        # inside a repo the default convenience output is a branch+ follow the
397
        # repo tree policy
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
398
        format = bzrdir.format_registry.make_bzrdir('knit')
399
        repo = self.make_repository('.', shared=True, format=format)
400
        repo.set_make_working_trees(False)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
401
        branch = bzrdir.BzrDir.create_branch_convenience('child',
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
402
                                                         format=format)
403
        self.assertRaises(errors.NoWorkingTree,
404
                          branch.bzrdir.open_workingtree)
405
        self.assertRaises(errors.NoRepositoryPresent,
406
                          branch.bzrdir.open_repository)
1534.6.10 by Robert Collins
Finish use of repositories support.
407
408
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
409
        # inside a repo the default convenience output is a branch+ follow the
410
        # repo tree policy but we can override that
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
411
        format = bzrdir.format_registry.make_bzrdir('knit')
412
        repo = self.make_repository('.', shared=True, format=format)
413
        repo.set_make_working_trees(False)
414
        branch = bzrdir.BzrDir.create_branch_convenience('child',
415
            force_new_tree=True, format=format)
416
        branch.bzrdir.open_workingtree()
417
        self.assertRaises(errors.NoRepositoryPresent,
418
                          branch.bzrdir.open_repository)
1534.6.10 by Robert Collins
Finish use of repositories support.
419
420
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
421
        # inside a repo the default convenience output is overridable to give
422
        # repo+branch+tree
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
423
        format = bzrdir.format_registry.make_bzrdir('knit')
424
        self.make_repository('.', shared=True, format=format)
425
        branch = bzrdir.BzrDir.create_branch_convenience('child',
426
            force_new_repo=True, format=format)
427
        branch.bzrdir.open_repository()
428
        branch.bzrdir.open_workingtree()
1534.6.10 by Robert Collins
Finish use of repositories support.
429
3242.2.14 by Aaron Bentley
Update from review comments
430
431
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
432
3242.2.10 by Aaron Bentley
Rename RepositoryPolicy.apply to acquire_repository
433
    def test_acquire_repository_standalone(self):
3242.2.14 by Aaron Bentley
Update from review comments
434
        """The default acquisition policy should create a standalone branch."""
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
435
        my_bzrdir = self.make_bzrdir('.')
436
        repo_policy = my_bzrdir.determine_repository_policy()
4070.9.2 by Andrew Bennetts
Rough prototype of allowing a SearchResult to be passed to fetch, and using that to improve network conversations.
437
        repo, is_new = repo_policy.acquire_repository()
3242.2.1 by Aaron Bentley
Abstract policy decisions into determine_repository_policy
438
        self.assertEqual(repo.bzrdir.root_transport.base,
439
                         my_bzrdir.root_transport.base)
3242.2.14 by Aaron Bentley
Update from review comments
440
        self.assertFalse(repo.is_shared())
441
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
442
    def test_determine_stacking_policy(self):
443
        parent_bzrdir = self.make_bzrdir('.')
444
        child_bzrdir = self.make_bzrdir('child')
3242.3.11 by Aaron Bentley
Clean up BzrDirConfig usage
445
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
446
        repo_policy = child_bzrdir.determine_repository_policy()
447
        self.assertEqual('http://example.org', repo_policy._stack_on)
448
3242.3.27 by Aaron Bentley
Interpret default stacking paths relative to config bzrdir
449
    def test_determine_stacking_policy_relative(self):
450
        parent_bzrdir = self.make_bzrdir('.')
451
        child_bzrdir = self.make_bzrdir('child')
452
        parent_bzrdir.get_config().set_default_stack_on('child2')
453
        repo_policy = child_bzrdir.determine_repository_policy()
3242.3.32 by Aaron Bentley
Defer handling relative stacking URLs as late as possible.
454
        self.assertEqual('child2', repo_policy._stack_on)
455
        self.assertEqual(parent_bzrdir.root_transport.base,
456
                         repo_policy._stack_on_pwd)
3242.3.27 by Aaron Bentley
Interpret default stacking paths relative to config bzrdir
457
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
458
    def prepare_default_stacking(self, child_format='1.6'):
3242.3.5 by Aaron Bentley
Implement stacking for clone_on_transport
459
        parent_bzrdir = self.make_bzrdir('.')
3650.3.1 by Aaron Bentley
Ensure stacking policy does not cause format upgrades
460
        child_branch = self.make_branch('child', format=child_format)
3242.5.1 by Jonathan Lange
Allow stacked-on branch locations to be stored as relative URLs.
461
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
3242.3.5 by Aaron Bentley
Implement stacking for clone_on_transport
462
        new_child_transport = parent_bzrdir.transport.clone('child2')
3242.3.28 by Aaron Bentley
Use repository acquisition policy for sprouting
463
        return child_branch, new_child_transport
464
465
    def test_clone_on_transport_obeys_stacking_policy(self):
466
        child_branch, new_child_transport = self.prepare_default_stacking()
3242.3.5 by Aaron Bentley
Implement stacking for clone_on_transport
467
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
3242.5.1 by Jonathan Lange
Allow stacked-on branch locations to be stored as relative URLs.
468
        self.assertEqual(child_branch.base,
3537.3.5 by Martin Pool
merge trunk including stacking policy
469
                         new_child.open_branch().get_stacked_on_url())
3242.3.5 by Aaron Bentley
Implement stacking for clone_on_transport
470
4126.1.1 by Andrew Bennetts
Fix bug when pushing stackable branch in unstackable repo to default-stacking target.
471
    def test_default_stacking_with_stackable_branch_unstackable_repo(self):
472
        # Make stackable source branch with an unstackable repo format.
473
        source_bzrdir = self.make_bzrdir('source')
474
        pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
5051.3.10 by Jelmer Vernooij
Pass colocated branch name around in more places.
475
        source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
476
            source_bzrdir)
4126.1.1 by Andrew Bennetts
Fix bug when pushing stackable branch in unstackable repo to default-stacking target.
477
        # Make a directory with a default stacking policy
478
        parent_bzrdir = self.make_bzrdir('parent')
479
        stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
480
        parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
481
        # Clone source into directory
482
        target = source_bzrdir.clone(self.get_url('parent/target'))
483
3242.3.28 by Aaron Bentley
Use repository acquisition policy for sprouting
484
    def test_sprout_obeys_stacking_policy(self):
485
        child_branch, new_child_transport = self.prepare_default_stacking()
486
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
487
        self.assertEqual(child_branch.base,
3537.3.5 by Martin Pool
merge trunk including stacking policy
488
                         new_child.open_branch().get_stacked_on_url())
3242.3.28 by Aaron Bentley
Use repository acquisition policy for sprouting
489
3650.3.1 by Aaron Bentley
Ensure stacking policy does not cause format upgrades
490
    def test_clone_ignores_policy_for_unsupported_formats(self):
491
        child_branch, new_child_transport = self.prepare_default_stacking(
492
            child_format='pack-0.92')
493
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
494
        self.assertRaises(errors.UnstackableBranchFormat,
495
                          new_child.open_branch().get_stacked_on_url)
496
497
    def test_sprout_ignores_policy_for_unsupported_formats(self):
498
        child_branch, new_child_transport = self.prepare_default_stacking(
499
            child_format='pack-0.92')
500
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
501
        self.assertRaises(errors.UnstackableBranchFormat,
502
                          new_child.open_branch().get_stacked_on_url)
503
504
    def test_sprout_upgrades_format_if_stacked_specified(self):
505
        child_branch, new_child_transport = self.prepare_default_stacking(
506
            child_format='pack-0.92')
507
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
508
                                               stacked=True)
509
        self.assertEqual(child_branch.bzrdir.root_transport.base,
510
                         new_child.open_branch().get_stacked_on_url())
3650.3.10 by Aaron Bentley
Ensure that sprout chooses a rich-root format as needed
511
        repo = new_child.open_repository()
512
        self.assertTrue(repo._format.supports_external_lookups)
513
        self.assertFalse(repo.supports_rich_root())
514
3650.5.1 by Aaron Bentley
Fix push to use clone all the time.
515
    def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
516
        child_branch, new_child_transport = self.prepare_default_stacking(
517
            child_format='pack-0.92')
518
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
519
            stacked_on=child_branch.bzrdir.root_transport.base)
520
        self.assertEqual(child_branch.bzrdir.root_transport.base,
521
                         new_child.open_branch().get_stacked_on_url())
522
        repo = new_child.open_repository()
523
        self.assertTrue(repo._format.supports_external_lookups)
524
        self.assertFalse(repo.supports_rich_root())
525
3650.3.10 by Aaron Bentley
Ensure that sprout chooses a rich-root format as needed
526
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
527
        child_branch, new_child_transport = self.prepare_default_stacking(
528
            child_format='rich-root-pack')
3665.2.3 by John Arbash Meinel
Fix a test that was expected to fail.
529
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
530
                                               stacked=True)
3650.3.10 by Aaron Bentley
Ensure that sprout chooses a rich-root format as needed
531
        repo = new_child.open_repository()
532
        self.assertTrue(repo._format.supports_external_lookups)
533
        self.assertTrue(repo.supports_rich_root())
3650.3.1 by Aaron Bentley
Ensure stacking policy does not cause format upgrades
534
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
535
    def test_add_fallback_repo_handles_absolute_urls(self):
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
536
        stack_on = self.make_branch('stack_on', format='1.6')
537
        repo = self.make_repository('repo', format='1.6')
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
538
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
539
        policy._add_fallback(repo)
540
541
    def test_add_fallback_repo_handles_relative_urls(self):
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
542
        stack_on = self.make_branch('stack_on', format='1.6')
543
        repo = self.make_repository('repo', format='1.6')
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
544
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
545
        policy._add_fallback(repo)
546
547
    def test_configure_relative_branch_stacking_url(self):
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
548
        stack_on = self.make_branch('stack_on', format='1.6')
549
        stacked = self.make_branch('stack_on/stacked', format='1.6')
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
550
        policy = bzrdir.UseExistingRepository(stacked.repository,
551
            '.', stack_on.base)
552
        policy.configure_branch(stacked)
3537.3.5 by Martin Pool
merge trunk including stacking policy
553
        self.assertEqual('..', stacked.get_stacked_on_url())
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
554
555
    def test_relative_branch_stacking_to_absolute(self):
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
556
        stack_on = self.make_branch('stack_on', format='1.6')
557
        stacked = self.make_branch('stack_on/stacked', format='1.6')
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
558
        policy = bzrdir.UseExistingRepository(stacked.repository,
559
            '.', self.get_readonly_url('stack_on'))
560
        policy.configure_branch(stacked)
561
        self.assertEqual(self.get_readonly_url('stack_on'),
3537.3.5 by Martin Pool
merge trunk including stacking policy
562
                         stacked.get_stacked_on_url())
3242.3.33 by Aaron Bentley
Handle relative URL stacking cleanly
563
3242.3.4 by Aaron Bentley
Initial determination of stacking policy
564
1534.4.39 by Robert Collins
Basic BzrDir support.
565
class ChrootedTests(TestCaseWithTransport):
566
    """A support class that provides readonly urls outside the local namespace.
567
568
    This is done by checking if self.transport_server is a MemoryServer. if it
569
    is then we are chrooted already, if it is not then an HttpServer is used
570
    for readonly urls.
571
    """
572
573
    def setUp(self):
574
        super(ChrootedTests, self).setUp()
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
575
        if not self.vfs_transport_factory == memory.MemoryServer:
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
576
            self.transport_readonly_server = http_server.HttpServer
1534.4.39 by Robert Collins
Basic BzrDir support.
577
3015.3.45 by Daniel Watkins
Extract common method.
578
    def local_branch_path(self, branch):
579
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
580
1534.4.39 by Robert Collins
Basic BzrDir support.
581
    def test_open_containing(self):
582
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
583
                          self.get_readonly_url(''))
584
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
585
                          self.get_readonly_url('g/p/q'))
586
        control = bzrdir.BzrDir.create(self.get_url())
587
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url(''))
588
        self.assertEqual('', relpath)
589
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
590
        self.assertEqual('g/p/q', relpath)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
591
3015.3.46 by Daniel Watkins
Made tests more granular.
592
    def test_open_containing_tree_branch_or_repository_empty(self):
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
593
        self.assertRaises(errors.NotBranchError,
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
594
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
595
            self.get_readonly_url(''))
596
3015.3.46 by Daniel Watkins
Made tests more granular.
597
    def test_open_containing_tree_branch_or_repository_all(self):
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
598
        self.make_branch_and_tree('topdir')
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
599
        tree, branch, repo, relpath = \
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
600
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
601
                'topdir/foo')
602
        self.assertEqual(os.path.realpath('topdir'),
603
                         os.path.realpath(tree.basedir))
604
        self.assertEqual(os.path.realpath('topdir'),
3015.3.45 by Daniel Watkins
Extract common method.
605
                         self.local_branch_path(branch))
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
606
        self.assertEqual(
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
607
            osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
608
            repo.bzrdir.transport.local_abspath('repository'))
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
609
        self.assertEqual(relpath, 'foo')
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
610
3015.3.46 by Daniel Watkins
Made tests more granular.
611
    def test_open_containing_tree_branch_or_repository_no_tree(self):
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
612
        self.make_branch('branch')
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
613
        tree, branch, repo, relpath = \
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
614
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
615
                'branch/foo')
616
        self.assertEqual(tree, None)
617
        self.assertEqual(os.path.realpath('branch'),
3015.3.45 by Daniel Watkins
Extract common method.
618
                         self.local_branch_path(branch))
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
619
        self.assertEqual(
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
620
            osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
621
            repo.bzrdir.transport.local_abspath('repository'))
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
622
        self.assertEqual(relpath, 'foo')
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
623
3015.3.46 by Daniel Watkins
Made tests more granular.
624
    def test_open_containing_tree_branch_or_repository_repo(self):
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
625
        self.make_repository('repo')
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
626
        tree, branch, repo, relpath = \
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
627
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
628
                'repo')
629
        self.assertEqual(tree, None)
630
        self.assertEqual(branch, None)
631
        self.assertEqual(
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
632
            osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
633
            repo.bzrdir.transport.local_abspath('repository'))
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
634
        self.assertEqual(relpath, '')
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
635
3015.3.46 by Daniel Watkins
Made tests more granular.
636
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
637
        self.make_repository('shared', shared=True)
638
        bzrdir.BzrDir.create_branch_convenience('shared/branch',
639
                                                force_new_tree=False)
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
640
        tree, branch, repo, relpath = \
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
641
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
642
                'shared/branch')
643
        self.assertEqual(tree, None)
644
        self.assertEqual(os.path.realpath('shared/branch'),
3015.3.45 by Daniel Watkins
Extract common method.
645
                         self.local_branch_path(branch))
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
646
        self.assertEqual(
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
647
            osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
648
            repo.bzrdir.transport.local_abspath('repository'))
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
649
        self.assertEqual(relpath, '')
3015.3.38 by Daniel Watkins
Added bzrlib.tests.test_bzrdir.test_open_containing_tree_branch_or_repository.
650
3015.3.48 by Daniel Watkins
Further granulated tests.
651
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
652
        self.make_branch_and_tree('foo')
3015.3.52 by Daniel Watkins
Replaced use of os functions with use of test suite functions.
653
        self.build_tree(['foo/bar/'])
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
654
        tree, branch, repo, relpath = \
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
655
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
656
                'foo/bar')
657
        self.assertEqual(os.path.realpath('foo'),
658
                         os.path.realpath(tree.basedir))
659
        self.assertEqual(os.path.realpath('foo'),
3015.3.45 by Daniel Watkins
Extract common method.
660
                         self.local_branch_path(branch))
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
661
        self.assertEqual(
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
662
            osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
663
            repo.bzrdir.transport.local_abspath('repository'))
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
664
        self.assertEqual(relpath, 'bar')
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
665
3015.3.48 by Daniel Watkins
Further granulated tests.
666
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
667
        self.make_repository('bar')
3015.3.52 by Daniel Watkins
Replaced use of os functions with use of test suite functions.
668
        self.build_tree(['bar/baz/'])
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
669
        tree, branch, repo, relpath = \
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
670
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
671
                'bar/baz')
672
        self.assertEqual(tree, None)
673
        self.assertEqual(branch, None)
674
        self.assertEqual(
3616.2.12 by Mark Hammond
use osutils.realpath instead of os.path.realpath so we get fwd slashes.
675
            osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
676
            repo.bzrdir.transport.local_abspath('repository'))
3015.3.57 by Daniel Watkins
Made changes to BzrDir.open_containing_tree_branch_or_repository suggested on list.
677
        self.assertEqual(relpath, 'baz')
3015.3.42 by Daniel Watkins
Added test to ensure that BzrDir.open_containing_tree_branch_or_repository will open containing versioned directories of unversioned subdirectories.
678
1534.6.11 by Robert Collins
Review feedback.
679
    def test_open_containing_from_transport(self):
680
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
1534.6.3 by Robert Collins
find_repository sufficiently robust.
681
                          get_transport(self.get_readonly_url('')))
1534.6.11 by Robert Collins
Review feedback.
682
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
1534.6.3 by Robert Collins
find_repository sufficiently robust.
683
                          get_transport(self.get_readonly_url('g/p/q')))
684
        control = bzrdir.BzrDir.create(self.get_url())
1534.6.11 by Robert Collins
Review feedback.
685
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
1534.6.3 by Robert Collins
find_repository sufficiently robust.
686
            get_transport(self.get_readonly_url('')))
687
        self.assertEqual('', relpath)
1534.6.11 by Robert Collins
Review feedback.
688
        branch, relpath = bzrdir.BzrDir.open_containing_from_transport(
1534.6.3 by Robert Collins
find_repository sufficiently robust.
689
            get_transport(self.get_readonly_url('g/p/q')))
690
        self.assertEqual('g/p/q', relpath)
691
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
692
    def test_open_containing_tree_or_branch(self):
693
        self.make_branch_and_tree('topdir')
694
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
695
            'topdir/foo')
2215.3.7 by Aaron Bentley
Remove (new) trailing whitespace
696
        self.assertEqual(os.path.realpath('topdir'),
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
697
                         os.path.realpath(tree.basedir))
2215.3.7 by Aaron Bentley
Remove (new) trailing whitespace
698
        self.assertEqual(os.path.realpath('topdir'),
3015.3.45 by Daniel Watkins
Extract common method.
699
                         self.local_branch_path(branch))
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
700
        self.assertIs(tree.bzrdir, branch.bzrdir)
701
        self.assertEqual('foo', relpath)
2381.1.1 by Robert Collins
Split out hpss test fixes which dont depend on new or altered API's.
702
        # opening from non-local should not return the tree
703
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
704
            self.get_readonly_url('topdir/foo'))
705
        self.assertEqual(None, tree)
706
        self.assertEqual('foo', relpath)
707
        # without a tree:
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
708
        self.make_branch('topdir/foo')
709
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
710
            'topdir/foo')
711
        self.assertIs(tree, None)
2215.3.7 by Aaron Bentley
Remove (new) trailing whitespace
712
        self.assertEqual(os.path.realpath('topdir/foo'),
3015.3.45 by Daniel Watkins
Extract common method.
713
                         self.local_branch_path(branch))
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
714
        self.assertEqual('', relpath)
715
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
716
    def test_open_tree_or_branch(self):
717
        self.make_branch_and_tree('topdir')
718
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
719
        self.assertEqual(os.path.realpath('topdir'),
720
                         os.path.realpath(tree.basedir))
721
        self.assertEqual(os.path.realpath('topdir'),
3015.3.45 by Daniel Watkins
Extract common method.
722
                         self.local_branch_path(branch))
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
723
        self.assertIs(tree.bzrdir, branch.bzrdir)
724
        # opening from non-local should not return the tree
3123.5.15 by Aaron Bentley
Fix open_tree_or_branch tests
725
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
726
            self.get_readonly_url('topdir'))
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
727
        self.assertEqual(None, tree)
728
        # without a tree:
729
        self.make_branch('topdir/foo')
3123.5.15 by Aaron Bentley
Fix open_tree_or_branch tests
730
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
731
        self.assertIs(tree, None)
732
        self.assertEqual(os.path.realpath('topdir/foo'),
3015.3.45 by Daniel Watkins
Extract common method.
733
                         self.local_branch_path(branch))
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
734
1910.11.5 by Andrew Bennetts
Add tests for BzrDir.open_from_transport.
735
    def test_open_from_transport(self):
736
        # transport pointing at bzrdir should give a bzrdir with root transport
737
        # set to the given transport
738
        control = bzrdir.BzrDir.create(self.get_url())
739
        transport = get_transport(self.get_url())
740
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
741
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
742
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
743
1910.11.5 by Andrew Bennetts
Add tests for BzrDir.open_from_transport.
744
    def test_open_from_transport_no_bzrdir(self):
745
        transport = get_transport(self.get_url())
746
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
747
                          transport)
748
749
    def test_open_from_transport_bzrdir_in_parent(self):
750
        control = bzrdir.BzrDir.create(self.get_url())
751
        transport = get_transport(self.get_url())
752
        transport.mkdir('subdir')
753
        transport = transport.clone('subdir')
754
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
755
                          transport)
756
2100.3.28 by Aaron Bentley
Make sprout recursive
757
    def test_sprout_recursive(self):
4100.2.4 by Aaron Bentley
More support for not autodetecting tree refs
758
        tree = self.make_branch_and_tree('tree1',
759
                                         format='dirstate-with-subtree')
2100.3.28 by Aaron Bentley
Make sprout recursive
760
        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.
761
            format='dirstate-with-subtree')
4100.2.4 by Aaron Bentley
More support for not autodetecting tree refs
762
        sub_tree.set_root_id('subtree-root')
2100.3.28 by Aaron Bentley
Make sprout recursive
763
        tree.add_reference(sub_tree)
764
        self.build_tree(['tree1/subtree/file'])
765
        sub_tree.add('file')
766
        tree.commit('Initial commit')
4100.2.4 by Aaron Bentley
More support for not autodetecting tree refs
767
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
768
        tree2.lock_read()
769
        self.addCleanup(tree2.unlock)
2100.3.28 by Aaron Bentley
Make sprout recursive
770
        self.failUnlessExists('tree2/subtree/file')
4100.2.4 by Aaron Bentley
More support for not autodetecting tree refs
771
        self.assertEqual('tree-reference', tree2.kind('subtree-root'))
2100.3.28 by Aaron Bentley
Make sprout recursive
772
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
773
    def test_cloning_metadir(self):
774
        """Ensure that cloning metadir is suitable"""
2100.3.34 by Aaron Bentley
Fix BzrDir.cloning_metadir with no format
775
        bzrdir = self.make_bzrdir('bzrdir')
776
        bzrdir.cloning_metadir()
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
777
        branch = self.make_branch('branch', format='knit')
778
        format = branch.bzrdir.cloning_metadir()
779
        self.assertIsInstance(format.workingtree_format,
2255.2.174 by Martin Pool
remove AB1 WorkingTree and experimental-knit3
780
            workingtree.WorkingTreeFormat3)
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
781
782
    def test_sprout_recursive_treeless(self):
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
783
        tree = self.make_branch_and_tree('tree1',
784
            format='dirstate-with-subtree')
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
785
        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.
786
            format='dirstate-with-subtree')
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
787
        tree.add_reference(sub_tree)
788
        self.build_tree(['tree1/subtree/file'])
789
        sub_tree.add('file')
790
        tree.commit('Initial commit')
791
        tree.bzrdir.destroy_workingtree()
792
        repo = self.make_repository('repo', shared=True,
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
793
            format='dirstate-with-subtree')
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
794
        repo.set_make_working_trees(False)
795
        tree.bzrdir.sprout('repo/tree2')
796
        self.failUnlessExists('repo/tree2/subtree')
797
        self.failIfExists('repo/tree2/subtree/file')
798
3140.1.1 by Aaron Bentley
Implement find_bzrdir functionality
799
    def make_foo_bar_baz(self):
3140.1.3 by Aaron Bentley
Add support for finding branches to BzrDir
800
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
3140.1.1 by Aaron Bentley
Implement find_bzrdir functionality
801
        bar = self.make_branch('foo/bar').bzrdir
802
        baz = self.make_branch('baz').bzrdir
803
        return foo, bar, baz
804
805
    def test_find_bzrdirs(self):
806
        foo, bar, baz = self.make_foo_bar_baz()
807
        transport = get_transport(self.get_url())
808
        self.assertEqualBzrdirs([baz, foo, bar],
809
                                bzrdir.BzrDir.find_bzrdirs(transport))
810
5215.3.4 by Marius Kruger
extract make_fake_permission_denied_transport and standardise the assert urls a little
811
    def make_fake_permission_denied_transport(self, transport, paths):
5215.3.9 by Marius Kruger
* Tried to improve code docs and NEWS as per review
812
        """Create a transport that raises PermissionDenied for some paths."""
5215.3.2 by Marius Kruger
* Move TestCaseWithMemoryTransport.make_smart_server => TestCaseWithTransport
813
        def filter(path):
5215.3.4 by Marius Kruger
extract make_fake_permission_denied_transport and standardise the assert urls a little
814
            if path in paths:
5215.3.2 by Marius Kruger
* Move TestCaseWithMemoryTransport.make_smart_server => TestCaseWithTransport
815
                raise errors.PermissionDenied(path)
816
            return path
817
        path_filter_server = pathfilter.PathFilteringServer(transport, filter)
818
        path_filter_server.start_server()
5215.3.9 by Marius Kruger
* Tried to improve code docs and NEWS as per review
819
        self.addCleanup(path_filter_server.stop_server)
5215.3.2 by Marius Kruger
* Move TestCaseWithMemoryTransport.make_smart_server => TestCaseWithTransport
820
        path_filter_transport = pathfilter.PathFilteringTransport(
821
            path_filter_server, '.')
5215.3.4 by Marius Kruger
extract make_fake_permission_denied_transport and standardise the assert urls a little
822
        return (path_filter_server, path_filter_transport)
823
5215.3.10 by Robert Collins
Merge trunk, adjusting NEWS and fixing up the permission denied test to be clearer and more focused.
824
    def assertBranchUrlsEndWith(self, expect_url_suffix, actual_bzrdirs):
825
        """Check that each branch url ends with the given suffix."""
826
        for actual_bzrdir in actual_bzrdirs:
5215.3.5 by Marius Kruger
factor out _assert_branch_urls
827
            self.assertEndsWith(actual_bzrdir.user_url, expect_url_suffix)
5215.3.4 by Marius Kruger
extract make_fake_permission_denied_transport and standardise the assert urls a little
828
829
    def test_find_bzrdirs_permission_denied(self):
830
        foo, bar, baz = self.make_foo_bar_baz()
831
        transport = get_transport(self.get_url())
5215.3.10 by Robert Collins
Merge trunk, adjusting NEWS and fixing up the permission denied test to be clearer and more focused.
832
        path_filter_server, path_filter_transport = \
833
            self.make_fake_permission_denied_transport(transport, ['foo'])
5215.3.5 by Marius Kruger
factor out _assert_branch_urls
834
        # local transport
5215.3.10 by Robert Collins
Merge trunk, adjusting NEWS and fixing up the permission denied test to be clearer and more focused.
835
        self.assertBranchUrlsEndWith('/baz/',
5215.3.9 by Marius Kruger
* Tried to improve code docs and NEWS as per review
836
            bzrdir.BzrDir.find_bzrdirs(path_filter_transport))
5215.3.2 by Marius Kruger
* Move TestCaseWithMemoryTransport.make_smart_server => TestCaseWithTransport
837
        # smart server
838
        smart_transport = self.make_smart_server('.',
839
            backing_server=path_filter_server)
5215.3.10 by Robert Collins
Merge trunk, adjusting NEWS and fixing up the permission denied test to be clearer and more focused.
840
        self.assertBranchUrlsEndWith('/baz/',
5215.3.9 by Marius Kruger
* Tried to improve code docs and NEWS as per review
841
            bzrdir.BzrDir.find_bzrdirs(smart_transport))
5215.3.1 by Marius Kruger
don't raise an exception when finding or brobing for a bzrdir and permission is denied
842
3140.1.1 by Aaron Bentley
Implement find_bzrdir functionality
843
    def test_find_bzrdirs_list_current(self):
844
        def list_current(transport):
845
            return [s for s in transport.list_dir('') if s != 'baz']
846
847
        foo, bar, baz = self.make_foo_bar_baz()
848
        transport = get_transport(self.get_url())
849
        self.assertEqualBzrdirs([foo, bar],
850
                                bzrdir.BzrDir.find_bzrdirs(transport,
851
                                    list_current=list_current))
852
853
    def test_find_bzrdirs_evaluate(self):
854
        def evaluate(bzrdir):
855
            try:
856
                repo = bzrdir.open_repository()
857
            except NoRepositoryPresent:
858
                return True, bzrdir.root_transport.base
859
            else:
860
                return False, bzrdir.root_transport.base
861
862
        foo, bar, baz = self.make_foo_bar_baz()
863
        transport = get_transport(self.get_url())
864
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
865
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
866
                                                         evaluate=evaluate)))
867
868
    def assertEqualBzrdirs(self, first, second):
869
        first = list(first)
870
        second = list(second)
871
        self.assertEqual(len(first), len(second))
872
        for x, y in zip(first, second):
873
            self.assertEqual(x.root_transport.base, y.root_transport.base)
874
3140.1.3 by Aaron Bentley
Add support for finding branches to BzrDir
875
    def test_find_branches(self):
876
        root = self.make_repository('', shared=True)
877
        foo, bar, baz = self.make_foo_bar_baz()
878
        qux = self.make_bzrdir('foo/qux')
879
        transport = get_transport(self.get_url())
880
        branches = bzrdir.BzrDir.find_branches(transport)
881
        self.assertEqual(baz.root_transport.base, branches[0].base)
882
        self.assertEqual(foo.root_transport.base, branches[1].base)
883
        self.assertEqual(bar.root_transport.base, branches[2].base)
884
885
        # ensure this works without a top-level repo
886
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
887
        self.assertEqual(foo.root_transport.base, branches[0].base)
888
        self.assertEqual(bar.root_transport.base, branches[1].base)
889
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
890
891
class TestMeta1DirFormat(TestCaseWithTransport):
892
    """Tests specific to the meta1 dir format."""
893
894
    def test_right_base_dirs(self):
895
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
896
        t = dir.transport
897
        branch_base = t.clone('branch').base
898
        self.assertEqual(branch_base, dir.get_branch_transport(None).base)
899
        self.assertEqual(branch_base,
1508.1.25 by Robert Collins
Update per review comments.
900
                         dir.get_branch_transport(bzrlib.branch.BzrBranchFormat5()).base)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
901
        repository_base = t.clone('repository').base
902
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
903
        self.assertEqual(repository_base,
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
904
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
905
        checkout_base = t.clone('checkout').base
906
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
907
        self.assertEqual(checkout_base,
908
                         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.
909
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
910
    def test_meta1dir_uses_lockdir(self):
911
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
912
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
913
        t = dir.transport
914
        self.assertIsDirectory('branch-lock', t)
915
2100.3.35 by Aaron Bentley
equality operations on bzrdir
916
    def test_comparison(self):
917
        """Equality and inequality behave properly.
918
919
        Metadirs should compare equal iff they have the same repo, branch and
920
        tree formats.
921
        """
922
        mydir = bzrdir.format_registry.make_bzrdir('knit')
923
        self.assertEqual(mydir, mydir)
924
        self.assertFalse(mydir != mydir)
925
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
926
        self.assertEqual(otherdir, mydir)
927
        self.assertFalse(otherdir != mydir)
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
928
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
2100.3.35 by Aaron Bentley
equality operations on bzrdir
929
        self.assertNotEqual(otherdir2, mydir)
930
        self.assertFalse(otherdir2 == mydir)
931
2255.12.1 by Robert Collins
Implement upgrade for working trees.
932
    def test_needs_conversion_different_working_tree(self):
933
        # meta1dirs need an conversion if any element is not the default.
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
934
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
935
        tree = self.make_branch_and_tree('tree', format='knit')
936
        self.assertTrue(tree.bzrdir.needs_format_conversion(
937
            new_format))
2255.12.1 by Robert Collins
Implement upgrade for working trees.
938
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
939
    def test_initialize_on_format_uses_smart_transport(self):
940
        self.setup_smart_server_with_call_log()
941
        new_format = bzrdir.format_registry.make_bzrdir('dirstate')
942
        transport = self.get_transport('target')
943
        transport.ensure_base()
944
        self.reset_smart_call_log()
945
        instance = new_format.initialize_on_transport(transport)
946
        self.assertIsInstance(instance, remote.RemoteBzrDir)
947
        rpc_count = len(self.hpss_calls)
948
        # This figure represent the amount of work to perform this use case. It
949
        # is entirely ok to reduce this number if a test fails due to rpc_count
950
        # being too low. If rpc_count increases, more network roundtrips have
951
        # become necessary for this use case. Please do not adjust this number
952
        # upwards without agreement from bzr's network support maintainers.
4017.2.2 by Robert Collins
Perform creation of BzrDirMetaFormat1 control directories using an RPC where possible. (Robert Collins)
953
        self.assertEqual(2, rpc_count)
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
954
2255.12.1 by Robert Collins
Implement upgrade for working trees.
955
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
956
class TestFormat5(TestCaseWithTransport):
957
    """Tests specific to the version 5 bzrdir format."""
958
959
    def test_same_lockfiles_between_tree_repo_branch(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
960
        # this checks that only a single lockfiles instance is created
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
961
        # for format 5 objects
962
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
963
        def check_dir_components_use_same_lock(dir):
964
            ctrl_1 = dir.open_repository().control_files
965
            ctrl_2 = dir.open_branch().control_files
966
            ctrl_3 = dir.open_workingtree()._control_files
967
            self.assertTrue(ctrl_1 is ctrl_2)
968
            self.assertTrue(ctrl_2 is ctrl_3)
969
        check_dir_components_use_same_lock(dir)
970
        # and if we open it normally.
971
        dir = bzrdir.BzrDir.open(self.get_url())
972
        check_dir_components_use_same_lock(dir)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
973
1534.5.16 by Robert Collins
Review feedback.
974
    def test_can_convert(self):
975
        # format 5 dirs are convertable
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
976
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
977
        self.assertTrue(dir.can_convert_format())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
978
1534.5.16 by Robert Collins
Review feedback.
979
    def test_needs_conversion(self):
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
980
        # format 5 dirs need a conversion if they are not the default,
981
        # and they aren't
982
        dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
983
        # don't need to convert it to itself
984
        self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
985
        # do need to convert it to the current default
986
        self.assertTrue(dir.needs_format_conversion(
987
            bzrdir.BzrDirFormat.get_default_format()))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
988
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
989
990
class TestFormat6(TestCaseWithTransport):
991
    """Tests specific to the version 6 bzrdir format."""
992
993
    def test_same_lockfiles_between_tree_repo_branch(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
994
        # this checks that only a single lockfiles instance is created
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
995
        # for format 6 objects
996
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
997
        def check_dir_components_use_same_lock(dir):
998
            ctrl_1 = dir.open_repository().control_files
999
            ctrl_2 = dir.open_branch().control_files
1000
            ctrl_3 = dir.open_workingtree()._control_files
1001
            self.assertTrue(ctrl_1 is ctrl_2)
1002
            self.assertTrue(ctrl_2 is ctrl_3)
1003
        check_dir_components_use_same_lock(dir)
1004
        # and if we open it normally.
1005
        dir = bzrdir.BzrDir.open(self.get_url())
1006
        check_dir_components_use_same_lock(dir)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1007
1534.5.16 by Robert Collins
Review feedback.
1008
    def test_can_convert(self):
1009
        # format 6 dirs are convertable
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1010
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1534.5.16 by Robert Collins
Review feedback.
1011
        self.assertTrue(dir.can_convert_format())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1012
1534.5.16 by Robert Collins
Review feedback.
1013
    def test_needs_conversion(self):
1014
        # format 6 dirs need an conversion if they are not the default.
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
1015
        dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
1016
        self.assertTrue(dir.needs_format_conversion(
1017
            bzrdir.BzrDirFormat.get_default_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.
1018
1019
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1020
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1021
    """A non .bzr based control directory."""
1022
1023
    def __init__(self, transport, format):
1024
        self._format = format
1025
        self.root_transport = transport
1026
        self.transport = transport.clone('.not')
1027
1028
1029
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
1030
    """A test class representing any non-.bzr based disk format."""
1031
1032
    def initialize_on_transport(self, transport):
1033
        """Initialize a new .not dir in the base directory of a Transport."""
1034
        transport.mkdir('.not')
1035
        return self.open(transport)
1036
1037
    def open(self, transport):
1038
        """Open this directory."""
1039
        return NotBzrDir(transport, self)
1040
1041
    @classmethod
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1042
    def _known_formats(self):
1043
        return set([NotBzrDirFormat()])
1044
1045
    @classmethod
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1046
    def probe_transport(self, transport):
1047
        """Our format is present if the transport ends in '.not/'."""
1733.1.2 by Robert Collins
bugfix test for non .bzrdir support.
1048
        if transport.has('.not'):
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1049
            return NotBzrDirFormat()
1050
1051
1052
class TestNotBzrDir(TestCaseWithTransport):
1053
    """Tests for using the bzrdir api with a non .bzr based disk format.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1054
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1055
    If/when one of these is in the core, we can let the implementation tests
1056
    verify this works.
1057
    """
1058
1059
    def test_create_and_find_format(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1060
        # create a .notbzr dir
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1061
        format = NotBzrDirFormat()
1062
        dir = format.initialize(self.get_url())
1063
        self.assertIsInstance(dir, NotBzrDir)
1064
        # now probe for it.
1065
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
1066
        try:
1067
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
1068
                get_transport(self.get_url()))
1733.1.2 by Robert Collins
bugfix test for non .bzrdir support.
1069
            self.assertIsInstance(found, NotBzrDirFormat)
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1070
        finally:
1071
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
1072
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1073
    def test_included_in_known_formats(self):
1074
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
1075
        try:
1076
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
1077
            for format in formats:
1078
                if isinstance(format, NotBzrDirFormat):
1079
                    return
1080
            self.fail("No NotBzrDirFormat in %s" % formats)
1081
        finally:
1082
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
1083
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1084
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.
1085
class NonLocalTests(TestCaseWithTransport):
1086
    """Tests for bzrdir static behaviour on non local paths."""
1087
1088
    def setUp(self):
1089
        super(NonLocalTests, self).setUp()
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
1090
        self.vfs_transport_factory = memory.MemoryServer
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1091
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.
1092
    def test_create_branch_convenience(self):
1093
        # 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
1094
        format = bzrdir.format_registry.make_bzrdir('knit')
1095
        branch = bzrdir.BzrDir.create_branch_convenience(
1096
            self.get_url('foo'), format=format)
1097
        self.assertRaises(errors.NoWorkingTree,
1098
                          branch.bzrdir.open_workingtree)
1099
        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.
1100
1101
    def test_create_branch_convenience_force_tree_not_local_fails(self):
1102
        # 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
1103
        format = bzrdir.format_registry.make_bzrdir('knit')
1104
        self.assertRaises(errors.NotLocalUrl,
1105
            bzrdir.BzrDir.create_branch_convenience,
1106
            self.get_url('foo'),
1107
            force_new_tree=True,
1108
            format=format)
1109
        t = get_transport(self.get_url('.'))
1110
        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.
1111
1563.2.38 by Robert Collins
make push preserve tree formats.
1112
    def test_clone(self):
1113
        # clone into a nonlocal path works
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1114
        format = bzrdir.format_registry.make_bzrdir('knit')
1115
        branch = bzrdir.BzrDir.create_branch_convenience('local',
1116
                                                         format=format)
1563.2.38 by Robert Collins
make push preserve tree formats.
1117
        branch.bzrdir.open_workingtree()
1118
        result = branch.bzrdir.clone(self.get_url('remote'))
1119
        self.assertRaises(errors.NoWorkingTree,
1120
                          result.open_workingtree)
1121
        result.open_branch()
1122
        result.open_repository()
1123
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1124
    def test_checkout_metadir(self):
1125
        # checkout_metadir has reasonable working tree format even when no
1126
        # working tree is present
2255.2.194 by Robert Collins
[BROKEN] Many updates to stop using experimental formats in tests.
1127
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
1128
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1129
        checkout_format = my_bzrdir.checkout_metadir()
1130
        self.assertIsInstance(checkout_format.workingtree_format,
1131
                              workingtree.WorkingTreeFormat3)
2100.3.22 by Aaron Bentley
merge from bzr.dev
1132
2215.3.5 by Aaron Bentley
Add support for remote ls
1133
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1134
class TestHTTPRedirections(object):
1135
    """Test redirection between two http servers.
2164.2.16 by Vincent Ladeuil
Add tests.
1136
1137
    This MUST be used by daughter classes that also inherit from
1138
    TestCaseWithTwoWebservers.
1139
1140
    We can't inherit directly from TestCaseWithTwoWebservers or the
1141
    test framework will try to create an instance which cannot
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1142
    run, its implementation being incomplete.
2164.2.16 by Vincent Ladeuil
Add tests.
1143
    """
1144
1145
    def create_transport_readonly_server(self):
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1146
        return http_utils.HTTPServerRedirecting()
2164.2.16 by Vincent Ladeuil
Add tests.
1147
1148
    def create_transport_secondary_server(self):
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1149
        return http_utils.HTTPServerRedirecting()
2164.2.16 by Vincent Ladeuil
Add tests.
1150
1151
    def setUp(self):
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1152
        super(TestHTTPRedirections, self).setUp()
2164.2.16 by Vincent Ladeuil
Add tests.
1153
        # The redirections will point to the new server
1154
        self.new_server = self.get_readonly_server()
1155
        # The requests to the old server will be redirected
1156
        self.old_server = self.get_secondary_server()
1157
        # Configure the redirections
1158
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1159
1160
    def test_loop(self):
1161
        # Both servers redirect to each other creating a loop
2164.2.16 by Vincent Ladeuil
Add tests.
1162
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1163
        # Starting from either server should loop
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1164
        old_url = self._qualified_url(self.old_server.host,
2164.2.16 by Vincent Ladeuil
Add tests.
1165
                                      self.old_server.port)
1166
        oldt = self._transport(old_url)
1167
        self.assertRaises(errors.NotBranchError,
1168
                          bzrdir.BzrDir.open_from_transport, oldt)
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1169
        new_url = self._qualified_url(self.new_server.host,
2164.2.16 by Vincent Ladeuil
Add tests.
1170
                                      self.new_server.port)
1171
        newt = self._transport(new_url)
1172
        self.assertRaises(errors.NotBranchError,
1173
                          bzrdir.BzrDir.open_from_transport, newt)
1174
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1175
    def test_qualifier_preserved(self):
1176
        wt = self.make_branch_and_tree('branch')
1177
        old_url = self._qualified_url(self.old_server.host,
1178
                                      self.old_server.port)
1179
        start = self._transport(old_url).clone('branch')
1180
        bdir = bzrdir.BzrDir.open_from_transport(start)
1181
        # Redirection should preserve the qualifier, hence the transport class
1182
        # itself.
1183
        self.assertIsInstance(bdir.root_transport, type(start))
1184
1185
1186
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1187
                                  http_utils.TestCaseWithTwoWebservers):
2164.2.16 by Vincent Ladeuil
Add tests.
1188
    """Tests redirections for urllib implementation"""
1189
1190
    _transport = HttpTransport_urllib
1191
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1192
    def _qualified_url(self, host, port):
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
1193
        result = 'http+urllib://%s:%s' % (host, port)
1194
        self.permit_url(result)
1195
        return result
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1196
2164.2.16 by Vincent Ladeuil
Add tests.
1197
1198
1199
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1200
                                  TestHTTPRedirections,
1201
                                  http_utils.TestCaseWithTwoWebservers):
2164.2.16 by Vincent Ladeuil
Add tests.
1202
    """Tests redirections for pycurl implementation"""
1203
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1204
    def _qualified_url(self, host, port):
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
1205
        result = 'http+pycurl://%s:%s' % (host, port)
1206
        self.permit_url(result)
1207
        return result
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1208
1209
1210
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1211
                                  http_utils.TestCaseWithTwoWebservers):
1212
    """Tests redirections for the nosmart decorator"""
1213
1214
    _transport = NoSmartTransportDecorator
1215
1216
    def _qualified_url(self, host, port):
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
1217
        result = 'nosmart+http://%s:%s' % (host, port)
1218
        self.permit_url(result)
1219
        return result
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1220
1221
1222
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1223
                                    http_utils.TestCaseWithTwoWebservers):
1224
    """Tests redirections for readonly decoratror"""
1225
1226
    _transport = ReadonlyTransportDecorator
1227
1228
    def _qualified_url(self, host, port):
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
1229
        result = 'readonly+http://%s:%s' % (host, port)
1230
        self.permit_url(result)
1231
        return result
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1232
1233
1234
class TestDotBzrHidden(TestCaseWithTransport):
1235
3023.1.3 by Alexander Belchenko
John's review
1236
    ls = ['ls']
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1237
    if sys.platform == 'win32':
3023.1.3 by Alexander Belchenko
John's review
1238
        ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1239
1240
    def get_ls(self):
3023.1.3 by Alexander Belchenko
John's review
1241
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1242
            stderr=subprocess.PIPE)
1243
        out, err = f.communicate()
1244
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1245
                         % (self.ls, err))
1246
        return out.splitlines()
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1247
1248
    def test_dot_bzr_hidden(self):
3023.1.2 by Alexander Belchenko
Martin's review.
1249
        if sys.platform == 'win32' and not win32utils.has_win32file:
1250
            raise TestSkipped('unable to make file hidden without pywin32 library')
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1251
        b = bzrdir.BzrDir.create('.')
3044.1.1 by Martin Pool
Fix up calls to TestCase.build_tree passing a string rather than a list
1252
        self.build_tree(['a'])
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1253
        self.assertEquals(['a'], self.get_ls())
1254
1255
    def test_dot_bzr_hidden_with_url(self):
3023.1.2 by Alexander Belchenko
Martin's review.
1256
        if sys.platform == 'win32' and not win32utils.has_win32file:
1257
            raise TestSkipped('unable to make file hidden without pywin32 library')
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1258
        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
1259
        self.build_tree(['a'])
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1260
        self.assertEquals(['a'], self.get_ls())
3583.1.2 by Andrew Bennetts
Add test for fix.
1261
1262
1263
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1264
    """Test BzrDirFormat implementation for TestBzrDirSprout."""
1265
1266
    def _open(self, transport):
1267
        return _TestBzrDir(transport, self)
1268
1269
1270
class _TestBzrDir(bzrdir.BzrDirMeta1):
1271
    """Test BzrDir implementation for TestBzrDirSprout.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1272
3583.1.2 by Andrew Bennetts
Add test for fix.
1273
    When created a _TestBzrDir already has repository and a branch.  The branch
1274
    is a test double as well.
1275
    """
1276
1277
    def __init__(self, *args, **kwargs):
1278
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1279
        self.test_branch = _TestBranch()
1280
        self.test_branch.repository = self.create_repository()
1281
1282
    def open_branch(self, unsupported=False):
1283
        return self.test_branch
1284
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
1285
    def cloning_metadir(self, require_stacking=False):
3583.1.2 by Andrew Bennetts
Add test for fix.
1286
        return _TestBzrDirFormat()
1287
1288
4086.1.3 by Andrew Bennetts
Fix bzrlib.tests.test_bzrdir.
1289
class _TestBranchFormat(bzrlib.branch.BranchFormat):
1290
    """Test Branch format for TestBzrDirSprout."""
1291
1292
3583.1.2 by Andrew Bennetts
Add test for fix.
1293
class _TestBranch(bzrlib.branch.Branch):
1294
    """Test Branch implementation for TestBzrDirSprout."""
1295
1296
    def __init__(self, *args, **kwargs):
4086.1.3 by Andrew Bennetts
Fix bzrlib.tests.test_bzrdir.
1297
        self._format = _TestBranchFormat()
3583.1.2 by Andrew Bennetts
Add test for fix.
1298
        super(_TestBranch, self).__init__(*args, **kwargs)
1299
        self.calls = []
3650.3.7 by Aaron Bentley
Fix test
1300
        self._parent = None
1301
3583.1.2 by Andrew Bennetts
Add test for fix.
1302
    def sprout(self, *args, **kwargs):
1303
        self.calls.append('sprout')
3650.3.7 by Aaron Bentley
Fix test
1304
        return _TestBranch()
3583.1.2 by Andrew Bennetts
Add test for fix.
1305
3650.3.4 by Aaron Bentley
Update test to permit calling copy_content_into
1306
    def copy_content_into(self, destination, revision_id=None):
1307
        self.calls.append('copy_content_into')
1308
3650.3.7 by Aaron Bentley
Fix test
1309
    def get_parent(self):
1310
        return self._parent
1311
1312
    def set_parent(self, parent):
1313
        self._parent = parent
1314
3583.1.2 by Andrew Bennetts
Add test for fix.
1315
1316
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1317
1318
    def test_sprout_uses_branch_sprout(self):
1319
        """BzrDir.sprout calls Branch.sprout.
1320
1321
        Usually, BzrDir.sprout should delegate to the branch's sprout method
1322
        for part of the work.  This allows the source branch to control the
1323
        choice of format for the new branch.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1324
3583.1.2 by Andrew Bennetts
Add test for fix.
1325
        There are exceptions, but this tests avoids them:
1326
          - if there's no branch in the source bzrdir,
1327
          - or if the stacking has been requested and the format needs to be
1328
            overridden to satisfy that.
1329
        """
1330
        # Make an instrumented bzrdir.
1331
        t = self.get_transport('source')
1332
        t.ensure_base()
1333
        source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1334
        # The instrumented bzrdir has a test_branch attribute that logs calls
1335
        # made to the branch contained in that bzrdir.  Initially the test
1336
        # branch exists but no calls have been made to it.
1337
        self.assertEqual([], source_bzrdir.test_branch.calls)
1338
1339
        # Sprout the bzrdir
1340
        target_url = self.get_url('target')
1341
        result = source_bzrdir.sprout(target_url, recurse='no')
1342
1343
        # The bzrdir called the branch's sprout method.
3650.3.4 by Aaron Bentley
Update test to permit calling copy_content_into
1344
        self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
3650.3.5 by Aaron Bentley
Fix parent location when copying content
1345
1346
    def test_sprout_parent(self):
1347
        grandparent_tree = self.make_branch('grandparent')
1348
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1349
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1350
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
4160.1.1 by Robert Collins
Add a BzrDir.pre_open hook for use by the smart server gaol.
1351
1352
1353
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1354
1355
    def test_pre_open_called(self):
1356
        calls = []
1357
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1358
        transport = self.get_transport('foo')
1359
        url = transport.base
1360
        self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1361
        self.assertEqual([transport.base], [t.base for t in calls])
1362
1363
    def test_pre_open_actual_exceptions_raised(self):
1364
        count = [0]
1365
        def fail_once(transport):
1366
            count[0] += 1
1367
            if count[0] == 1:
1368
                raise errors.BzrError("fail")
1369
        bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1370
        transport = self.get_transport('foo')
1371
        url = transport.base
1372
        err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1373
        self.assertEqual('fail', err._preformatted_string)
5107.3.1 by Marco Pantaleoni
Added the new hooks 'post_branch', 'post_switch' and 'post_repo_init',
1374
1375
    def test_post_repo_init(self):
1376
        from bzrlib.bzrdir import RepoInitHookParams
1377
        calls = []
5107.3.4 by Marco Pantaleoni
Applied suggestions from merge reviewer (John A Meinel):
1378
        bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1379
            calls.append, None)
5107.3.1 by Marco Pantaleoni
Added the new hooks 'post_branch', 'post_switch' and 'post_repo_init',
1380
        self.make_repository('foo')
1381
        self.assertLength(1, calls)
1382
        params = calls[0]
1383
        self.assertIsInstance(params, RepoInitHookParams)
1384
        self.assertTrue(hasattr(params, 'bzrdir'))
1385
        self.assertTrue(hasattr(params, 'repository'))