~bzr-pqm/bzr/bzr.dev

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