/brz/remove-bazaar

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