/brz/remove-bazaar

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