/brz/remove-bazaar

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