/brz/remove-bazaar

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