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