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