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