~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Michael Ellerman
  • Date: 2006-02-28 14:45:51 UTC
  • mto: (1558.1.18 Aaron's integration)
  • mto: This revision was merged to the branch mainline in revision 1586.
  • Revision ID: michael@ellerman.id.au-20060228144551-3d9941ecde4a0b0a
Update contrib/pwk for -p1 diffs from bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
    # Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
 
 
1
# (C) 2005 Canonical Ltd
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
For interface contract tests, see tests/bzr_dir_implementations.
20
20
"""
21
21
 
22
 
import os.path
23
22
from StringIO import StringIO
24
23
 
25
 
from bzrlib import (
26
 
    bzrdir,
27
 
    errors,
28
 
    help_topics,
29
 
    repository,
30
 
    symbol_versioning,
31
 
    urlutils,
32
 
    workingtree,
33
 
    )
34
24
import bzrlib.branch
 
25
import bzrlib.bzrdir as bzrdir
 
26
import bzrlib.errors as errors
35
27
from bzrlib.errors import (NotBranchError,
36
28
                           UnknownFormatError,
37
29
                           UnsupportedFormatError,
38
30
                           )
39
 
from bzrlib.tests import (
40
 
    TestCase,
41
 
    TestCaseWithTransport,
42
 
    test_sftp_transport
43
 
    )
44
 
from bzrlib.tests.HttpServer import HttpServer
45
 
from bzrlib.tests.HTTPTestUtil import (
46
 
    TestCaseWithTwoWebservers,
47
 
    HTTPServerRedirecting,
48
 
    )
49
 
from bzrlib.tests.test_http import TestWithTransport_pycurl
 
31
import bzrlib.repository as repository
 
32
from bzrlib.tests import TestCase, TestCaseWithTransport
 
33
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
50
34
from bzrlib.transport import get_transport
51
 
from bzrlib.transport.http._urllib import HttpTransport_urllib
 
35
from bzrlib.transport.http import HttpServer
52
36
from bzrlib.transport.memory import MemoryServer
53
 
from bzrlib.repofmt import knitrepo, weaverepo
 
37
import bzrlib.workingtree as workingtree
54
38
 
55
39
 
56
40
class TestDefaultFormat(TestCase):
58
42
    def test_get_set_default_format(self):
59
43
        old_format = bzrdir.BzrDirFormat.get_default_format()
60
44
        # default is BzrDirFormat6
61
 
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
62
 
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
63
 
                             bzrdir.BzrDirFormat.set_default_format, 
64
 
                             SampleBzrDirFormat())
 
45
        self.failUnless(isinstance(old_format, bzrdir.BzrDirFormat6))
 
46
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
65
47
        # creating a bzr dir should now create an instrumented dir.
66
48
        try:
67
 
            result = bzrdir.BzrDir.create('memory:///')
 
49
            result = bzrdir.BzrDir.create('memory:/')
68
50
            self.failUnless(isinstance(result, SampleBzrDir))
69
51
        finally:
70
 
            self.applyDeprecated(symbol_versioning.zero_fourteen,
71
 
                bzrdir.BzrDirFormat.set_default_format, old_format)
 
52
            bzrdir.BzrDirFormat.set_default_format(old_format)
72
53
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
73
54
 
74
55
 
75
 
class TestFormatRegistry(TestCase):
76
 
 
77
 
    def make_format_registry(self):
78
 
        my_format_registry = bzrdir.BzrDirFormatRegistry()
79
 
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
80
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
81
 
            ' repositories', deprecated=True)
82
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
83
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
84
 
        my_format_registry.register_metadir('knit',
85
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
86
 
            'Format using knits',
87
 
            )
88
 
        my_format_registry.set_default('knit')
89
 
        my_format_registry.register_metadir(
90
 
            'branch6',
91
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
92
 
            'Experimental successor to knit.  Use at your own risk.',
93
 
            branch_format='bzrlib.branch.BzrBranchFormat6')
94
 
        my_format_registry.register_metadir(
95
 
            'hidden format',
96
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
97
 
            'Experimental successor to knit.  Use at your own risk.',
98
 
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
99
 
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
100
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
101
 
            ' repositories', hidden=True)
102
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
103
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
104
 
            hidden=True)
105
 
        return my_format_registry
106
 
 
107
 
    def test_format_registry(self):
108
 
        my_format_registry = self.make_format_registry()
109
 
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
110
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
111
 
        my_bzrdir = my_format_registry.make_bzrdir('weave')
112
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
113
 
        my_bzrdir = my_format_registry.make_bzrdir('default')
114
 
        self.assertIsInstance(my_bzrdir.repository_format, 
115
 
            knitrepo.RepositoryFormatKnit1)
116
 
        my_bzrdir = my_format_registry.make_bzrdir('knit')
117
 
        self.assertIsInstance(my_bzrdir.repository_format, 
118
 
            knitrepo.RepositoryFormatKnit1)
119
 
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
120
 
        self.assertIsInstance(my_bzrdir.get_branch_format(),
121
 
                              bzrlib.branch.BzrBranchFormat6)
122
 
 
123
 
    def test_get_help(self):
124
 
        my_format_registry = self.make_format_registry()
125
 
        self.assertEqual('Format registered lazily',
126
 
                         my_format_registry.get_help('lazy'))
127
 
        self.assertEqual('Format using knits', 
128
 
                         my_format_registry.get_help('knit'))
129
 
        self.assertEqual('Format using knits', 
130
 
                         my_format_registry.get_help('default'))
131
 
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
132
 
                         ' checkouts or shared repositories', 
133
 
                         my_format_registry.get_help('weave'))
134
 
        
135
 
    def test_help_topic(self):
136
 
        topics = help_topics.HelpTopicRegistry()
137
 
        topics.register('formats', self.make_format_registry().help_topic, 
138
 
                        'Directory formats')
139
 
        topic = topics.get_detail('formats')
140
 
        new, deprecated = topic.split('Deprecated formats')
141
 
        self.assertContainsRe(new, 'Bazaar directory formats')
142
 
        self.assertContainsRe(new, 
143
 
            '  knit/default:\n    \(native\) Format using knits\n')
144
 
        self.assertContainsRe(deprecated, 
145
 
            '  lazy:\n    \(native\) Format registered lazily\n')
146
 
        self.assertNotContainsRe(new, 'hidden')
147
 
 
148
 
    def test_set_default_repository(self):
149
 
        default_factory = bzrdir.format_registry.get('default')
150
 
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
151
 
                       if v == default_factory and k != 'default'][0]
152
 
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
153
 
        try:
154
 
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
155
 
                          bzrdir.format_registry.get('default'))
156
 
            self.assertIs(
157
 
                repository.RepositoryFormat.get_default_format().__class__,
158
 
                knitrepo.RepositoryFormatKnit3)
159
 
        finally:
160
 
            bzrdir.format_registry.set_default_repository(old_default)
161
 
 
162
 
 
163
56
class SampleBranch(bzrlib.branch.Branch):
164
57
    """A dummy branch for guess what, dummy use."""
165
58
 
170
63
class SampleBzrDir(bzrdir.BzrDir):
171
64
    """A sample BzrDir implementation to allow testing static methods."""
172
65
 
173
 
    def create_repository(self, shared=False):
 
66
    def create_repository(self):
174
67
        """See BzrDir.create_repository."""
175
68
        return "A repository"
176
69
 
198
91
        """See BzrDirFormat.get_format_string()."""
199
92
        return "Sample .bzr dir format."
200
93
 
201
 
    def initialize(self, url, possible_transports=None):
 
94
    def initialize(self, url):
202
95
        """Create a bzr dir."""
203
 
        t = get_transport(url, possible_transports)
 
96
        t = get_transport(url)
204
97
        t.mkdir('.bzr')
205
 
        t.put_bytes('.bzr/branch-format', self.get_format_string())
 
98
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
206
99
        return SampleBzrDir(t, self)
207
100
 
208
101
    def is_supported(self):
237
130
    def test_find_format_unknown_format(self):
238
131
        t = get_transport(self.get_url())
239
132
        t.mkdir('.bzr')
240
 
        t.put_bytes('.bzr/branch-format', '')
 
133
        t.put('.bzr/branch-format', StringIO())
241
134
        self.assertRaises(UnknownFormatError,
242
135
                          bzrdir.BzrDirFormat.find_format,
243
136
                          get_transport('.'))
251
144
        bzrdir.BzrDirFormat.register_format(format)
252
145
        # which bzrdir.Open will refuse (not supported)
253
146
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open, url)
254
 
        # which bzrdir.open_containing will refuse (not supported)
255
 
        self.assertRaises(UnsupportedFormatError, bzrdir.BzrDir.open_containing, url)
256
147
        # but open_downlevel will work
257
148
        t = get_transport(url)
258
149
        self.assertEqual(format.open(t), bzrdir.BzrDir.open_unsupported(url))
263
154
 
264
155
    def test_create_repository(self):
265
156
        format = SampleBzrDirFormat()
266
 
        repo = bzrdir.BzrDir.create_repository(self.get_url(), format=format)
267
 
        self.assertEqual('A repository', repo)
268
 
 
269
 
    def test_create_repository_shared(self):
270
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
271
 
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
272
 
        self.assertTrue(repo.is_shared())
273
 
 
274
 
    def test_create_repository_nonshared(self):
275
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
276
 
        repo = bzrdir.BzrDir.create_repository('.')
277
 
        self.assertFalse(repo.is_shared())
 
157
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
158
        bzrdir.BzrDirFormat.set_default_format(format)
 
159
        try:
 
160
            repo = bzrdir.BzrDir.create_repository(self.get_url())
 
161
            self.assertEqual('A repository', repo)
 
162
        finally:
 
163
            bzrdir.BzrDirFormat.set_default_format(old_format)
278
164
 
279
165
    def test_create_repository_under_shared(self):
280
166
        # an explicit create_repository always does so.
281
167
        # we trust the format is right from the 'create_repository test'
282
 
        format = bzrdir.format_registry.make_bzrdir('knit')
283
 
        self.make_repository('.', shared=True, format=format)
284
 
        repo = bzrdir.BzrDir.create_repository(self.get_url('child'),
285
 
                                               format=format)
286
 
        self.assertTrue(isinstance(repo, repository.Repository))
287
 
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
168
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
169
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
170
        try:
 
171
            self.make_repository('.', shared=True)
 
172
            repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
 
173
            self.assertTrue(isinstance(repo, repository.Repository))
 
174
            self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
175
        finally:
 
176
            bzrdir.BzrDirFormat.set_default_format(old_format)
288
177
 
289
178
    def test_create_branch_and_repo_uses_default(self):
290
179
        format = SampleBzrDirFormat()
291
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
292
 
                                                      format=format)
293
 
        self.assertTrue(isinstance(branch, SampleBranch))
 
180
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
181
        bzrdir.BzrDirFormat.set_default_format(format)
 
182
        try:
 
183
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
 
184
            self.assertTrue(isinstance(branch, SampleBranch))
 
185
        finally:
 
186
            bzrdir.BzrDirFormat.set_default_format(old_format)
294
187
 
295
188
    def test_create_branch_and_repo_under_shared(self):
296
189
        # creating a branch and repo in a shared repo uses the
297
190
        # shared repository
298
 
        format = bzrdir.format_registry.make_bzrdir('knit')
299
 
        self.make_repository('.', shared=True, format=format)
300
 
        branch = bzrdir.BzrDir.create_branch_and_repo(
301
 
            self.get_url('child'), format=format)
302
 
        self.assertRaises(errors.NoRepositoryPresent,
303
 
                          branch.bzrdir.open_repository)
 
191
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
192
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
193
        try:
 
194
            self.make_repository('.', shared=True)
 
195
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
 
196
            self.assertRaises(errors.NoRepositoryPresent,
 
197
                              branch.bzrdir.open_repository)
 
198
        finally:
 
199
            bzrdir.BzrDirFormat.set_default_format(old_format)
304
200
 
305
201
    def test_create_branch_and_repo_under_shared_force_new(self):
306
202
        # creating a branch and repo in a shared repo can be forced to 
307
203
        # make a new repo
308
 
        format = bzrdir.format_registry.make_bzrdir('knit')
309
 
        self.make_repository('.', shared=True, format=format)
310
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
311
 
                                                      force_new_repo=True,
312
 
                                                      format=format)
313
 
        branch.bzrdir.open_repository()
 
204
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
205
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
206
        try:
 
207
            self.make_repository('.', shared=True)
 
208
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
209
                                                          force_new_repo=True)
 
210
            branch.bzrdir.open_repository()
 
211
        finally:
 
212
            bzrdir.BzrDirFormat.set_default_format(old_format)
314
213
 
315
214
    def test_create_standalone_working_tree(self):
316
215
        format = SampleBzrDirFormat()
317
 
        # note this is deliberately readonly, as this failure should 
318
 
        # occur before any writes.
319
 
        self.assertRaises(errors.NotLocalUrl,
320
 
                          bzrdir.BzrDir.create_standalone_workingtree,
321
 
                          self.get_readonly_url(), format=format)
322
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
323
 
                                                           format=format)
324
 
        self.assertEqual('A tree', tree)
 
216
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
217
        bzrdir.BzrDirFormat.set_default_format(format)
 
218
        try:
 
219
            # note this is deliberately readonly, as this failure should 
 
220
            # occur before any writes.
 
221
            self.assertRaises(errors.NotLocalUrl,
 
222
                              bzrdir.BzrDir.create_standalone_workingtree,
 
223
                              self.get_readonly_url())
 
224
            tree = bzrdir.BzrDir.create_standalone_workingtree('.')
 
225
            self.assertEqual('A tree', tree)
 
226
        finally:
 
227
            bzrdir.BzrDirFormat.set_default_format(old_format)
325
228
 
326
229
    def test_create_standalone_working_tree_under_shared_repo(self):
327
230
        # create standalone working tree always makes a repo.
328
 
        format = bzrdir.format_registry.make_bzrdir('knit')
329
 
        self.make_repository('.', shared=True, format=format)
330
 
        # note this is deliberately readonly, as this failure should 
331
 
        # occur before any writes.
332
 
        self.assertRaises(errors.NotLocalUrl,
333
 
                          bzrdir.BzrDir.create_standalone_workingtree,
334
 
                          self.get_readonly_url('child'), format=format)
335
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
336
 
            format=format)
337
 
        tree.bzrdir.open_repository()
 
231
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
232
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
233
        try:
 
234
            self.make_repository('.', shared=True)
 
235
            # note this is deliberately readonly, as this failure should 
 
236
            # occur before any writes.
 
237
            self.assertRaises(errors.NotLocalUrl,
 
238
                              bzrdir.BzrDir.create_standalone_workingtree,
 
239
                              self.get_readonly_url('child'))
 
240
            tree = bzrdir.BzrDir.create_standalone_workingtree('child')
 
241
            tree.bzrdir.open_repository()
 
242
        finally:
 
243
            bzrdir.BzrDirFormat.set_default_format(old_format)
338
244
 
339
245
    def test_create_branch_convenience(self):
340
246
        # outside a repo the default convenience output is a repo+branch_tree
341
 
        format = bzrdir.format_registry.make_bzrdir('knit')
342
 
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
343
 
        branch.bzrdir.open_workingtree()
344
 
        branch.bzrdir.open_repository()
345
 
 
346
 
    def test_create_branch_convenience_possible_transports(self):
347
 
        """Check that the optional 'possible_transports' is recognized"""
348
 
        format = bzrdir.format_registry.make_bzrdir('knit')
349
 
        t = self.get_transport()
350
 
        branch = bzrdir.BzrDir.create_branch_convenience(
351
 
            '.', format=format, possible_transports=[t])
352
 
        branch.bzrdir.open_workingtree()
353
 
        branch.bzrdir.open_repository()
354
 
 
355
 
    def test_create_branch_convenience_root(self):
356
 
        """Creating a branch at the root of a fs should work."""
357
 
        self.vfs_transport_factory = MemoryServer
358
 
        # outside a repo the default convenience output is a repo+branch_tree
359
 
        format = bzrdir.format_registry.make_bzrdir('knit')
360
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
361
 
                                                         format=format)
362
 
        self.assertRaises(errors.NoWorkingTree,
363
 
                          branch.bzrdir.open_workingtree)
364
 
        branch.bzrdir.open_repository()
 
247
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
248
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
249
        try:
 
250
            branch = bzrdir.BzrDir.create_branch_convenience('.')
 
251
            branch.bzrdir.open_workingtree()
 
252
            branch.bzrdir.open_repository()
 
253
        finally:
 
254
            bzrdir.BzrDirFormat.set_default_format(old_format)
365
255
 
366
256
    def test_create_branch_convenience_under_shared_repo(self):
367
257
        # inside a repo the default convenience output is a branch+ follow the
368
258
        # repo tree policy
369
 
        format = bzrdir.format_registry.make_bzrdir('knit')
370
 
        self.make_repository('.', shared=True, format=format)
371
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
372
 
            format=format)
373
 
        branch.bzrdir.open_workingtree()
374
 
        self.assertRaises(errors.NoRepositoryPresent,
375
 
                          branch.bzrdir.open_repository)
 
259
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
260
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
261
        try:
 
262
            self.make_repository('.', shared=True)
 
263
            branch = bzrdir.BzrDir.create_branch_convenience('child')
 
264
            branch.bzrdir.open_workingtree()
 
265
            self.assertRaises(errors.NoRepositoryPresent,
 
266
                              branch.bzrdir.open_repository)
 
267
        finally:
 
268
            bzrdir.BzrDirFormat.set_default_format(old_format)
376
269
            
377
270
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
378
271
        # inside a repo the default convenience output is a branch+ follow the
379
272
        # repo tree policy but we can override that
380
 
        format = bzrdir.format_registry.make_bzrdir('knit')
381
 
        self.make_repository('.', shared=True, format=format)
382
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
383
 
            force_new_tree=False, format=format)
384
 
        self.assertRaises(errors.NoWorkingTree,
385
 
                          branch.bzrdir.open_workingtree)
386
 
        self.assertRaises(errors.NoRepositoryPresent,
387
 
                          branch.bzrdir.open_repository)
 
273
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
274
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
275
        try:
 
276
            self.make_repository('.', shared=True)
 
277
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
278
                force_new_tree=False)
 
279
            self.assertRaises(errors.NoWorkingTree,
 
280
                              branch.bzrdir.open_workingtree)
 
281
            self.assertRaises(errors.NoRepositoryPresent,
 
282
                              branch.bzrdir.open_repository)
 
283
        finally:
 
284
            bzrdir.BzrDirFormat.set_default_format(old_format)
388
285
            
389
286
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
390
287
        # inside a repo the default convenience output is a branch+ follow the
391
288
        # repo tree policy
392
 
        format = bzrdir.format_registry.make_bzrdir('knit')
393
 
        repo = self.make_repository('.', shared=True, format=format)
394
 
        repo.set_make_working_trees(False)
395
 
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
396
 
                                                         format=format)
397
 
        self.assertRaises(errors.NoWorkingTree,
398
 
                          branch.bzrdir.open_workingtree)
399
 
        self.assertRaises(errors.NoRepositoryPresent,
400
 
                          branch.bzrdir.open_repository)
 
289
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
290
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
291
        try:
 
292
            repo = self.make_repository('.', shared=True)
 
293
            repo.set_make_working_trees(False)
 
294
            branch = bzrdir.BzrDir.create_branch_convenience('child')
 
295
            self.assertRaises(errors.NoWorkingTree,
 
296
                              branch.bzrdir.open_workingtree)
 
297
            self.assertRaises(errors.NoRepositoryPresent,
 
298
                              branch.bzrdir.open_repository)
 
299
        finally:
 
300
            bzrdir.BzrDirFormat.set_default_format(old_format)
401
301
 
402
302
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
403
303
        # inside a repo the default convenience output is a branch+ follow the
404
304
        # repo tree policy but we can override that
405
 
        format = bzrdir.format_registry.make_bzrdir('knit')
406
 
        repo = self.make_repository('.', shared=True, format=format)
407
 
        repo.set_make_working_trees(False)
408
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
409
 
            force_new_tree=True, format=format)
410
 
        branch.bzrdir.open_workingtree()
411
 
        self.assertRaises(errors.NoRepositoryPresent,
412
 
                          branch.bzrdir.open_repository)
 
305
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
306
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
307
        try:
 
308
            repo = self.make_repository('.', shared=True)
 
309
            repo.set_make_working_trees(False)
 
310
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
311
                force_new_tree=True)
 
312
            branch.bzrdir.open_workingtree()
 
313
            self.assertRaises(errors.NoRepositoryPresent,
 
314
                              branch.bzrdir.open_repository)
 
315
        finally:
 
316
            bzrdir.BzrDirFormat.set_default_format(old_format)
413
317
 
414
318
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
415
319
        # inside a repo the default convenience output is overridable to give
416
320
        # repo+branch+tree
417
 
        format = bzrdir.format_registry.make_bzrdir('knit')
418
 
        self.make_repository('.', shared=True, format=format)
419
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
420
 
            force_new_repo=True, format=format)
421
 
        branch.bzrdir.open_repository()
422
 
        branch.bzrdir.open_workingtree()
 
321
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
322
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
323
        try:
 
324
            self.make_repository('.', shared=True)
 
325
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
326
                force_new_repo=True)
 
327
            branch.bzrdir.open_repository()
 
328
            branch.bzrdir.open_workingtree()
 
329
        finally:
 
330
            bzrdir.BzrDirFormat.set_default_format(old_format)
423
331
 
424
332
 
425
333
class ChrootedTests(TestCaseWithTransport):
432
340
 
433
341
    def setUp(self):
434
342
        super(ChrootedTests, self).setUp()
435
 
        if not self.vfs_transport_factory == MemoryServer:
 
343
        if not self.transport_server == MemoryServer:
436
344
            self.transport_readonly_server = HttpServer
437
345
 
438
346
    def test_open_containing(self):
459
367
            get_transport(self.get_readonly_url('g/p/q')))
460
368
        self.assertEqual('g/p/q', relpath)
461
369
 
462
 
    def test_open_containing_tree_or_branch(self):
463
 
        def local_branch_path(branch):
464
 
             return os.path.realpath(
465
 
                urlutils.local_path_from_url(branch.base))
466
 
 
467
 
        self.make_branch_and_tree('topdir')
468
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
469
 
            'topdir/foo')
470
 
        self.assertEqual(os.path.realpath('topdir'),
471
 
                         os.path.realpath(tree.basedir))
472
 
        self.assertEqual(os.path.realpath('topdir'),
473
 
                         local_branch_path(branch))
474
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
475
 
        self.assertEqual('foo', relpath)
476
 
        # opening from non-local should not return the tree
477
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
478
 
            self.get_readonly_url('topdir/foo'))
479
 
        self.assertEqual(None, tree)
480
 
        self.assertEqual('foo', relpath)
481
 
        # without a tree:
482
 
        self.make_branch('topdir/foo')
483
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
484
 
            'topdir/foo')
485
 
        self.assertIs(tree, None)
486
 
        self.assertEqual(os.path.realpath('topdir/foo'),
487
 
                         local_branch_path(branch))
488
 
        self.assertEqual('', relpath)
489
 
 
490
 
    def test_open_from_transport(self):
491
 
        # transport pointing at bzrdir should give a bzrdir with root transport
492
 
        # set to the given transport
493
 
        control = bzrdir.BzrDir.create(self.get_url())
494
 
        transport = get_transport(self.get_url())
495
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
496
 
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
497
 
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
498
 
        
499
 
    def test_open_from_transport_no_bzrdir(self):
500
 
        transport = get_transport(self.get_url())
501
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
502
 
                          transport)
503
 
 
504
 
    def test_open_from_transport_bzrdir_in_parent(self):
505
 
        control = bzrdir.BzrDir.create(self.get_url())
506
 
        transport = get_transport(self.get_url())
507
 
        transport.mkdir('subdir')
508
 
        transport = transport.clone('subdir')
509
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
510
 
                          transport)
511
 
 
512
 
    def test_sprout_recursive(self):
513
 
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
514
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
515
 
            format='dirstate-with-subtree')
516
 
        tree.add_reference(sub_tree)
517
 
        self.build_tree(['tree1/subtree/file'])
518
 
        sub_tree.add('file')
519
 
        tree.commit('Initial commit')
520
 
        tree.bzrdir.sprout('tree2')
521
 
        self.failUnlessExists('tree2/subtree/file')
522
 
 
523
 
    def test_cloning_metadir(self):
524
 
        """Ensure that cloning metadir is suitable"""
525
 
        bzrdir = self.make_bzrdir('bzrdir')
526
 
        bzrdir.cloning_metadir()
527
 
        branch = self.make_branch('branch', format='knit')
528
 
        format = branch.bzrdir.cloning_metadir()
529
 
        self.assertIsInstance(format.workingtree_format,
530
 
            workingtree.WorkingTreeFormat3)
531
 
 
532
 
    def test_sprout_recursive_treeless(self):
533
 
        tree = self.make_branch_and_tree('tree1',
534
 
            format='dirstate-with-subtree')
535
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
536
 
            format='dirstate-with-subtree')
537
 
        tree.add_reference(sub_tree)
538
 
        self.build_tree(['tree1/subtree/file'])
539
 
        sub_tree.add('file')
540
 
        tree.commit('Initial commit')
541
 
        tree.bzrdir.destroy_workingtree()
542
 
        repo = self.make_repository('repo', shared=True,
543
 
            format='dirstate-with-subtree')
544
 
        repo.set_make_working_trees(False)
545
 
        tree.bzrdir.sprout('repo/tree2')
546
 
        self.failUnlessExists('repo/tree2/subtree')
547
 
        self.failIfExists('repo/tree2/subtree/file')
548
 
 
549
370
 
550
371
class TestMeta1DirFormat(TestCaseWithTransport):
551
372
    """Tests specific to the meta1 dir format."""
560
381
        repository_base = t.clone('repository').base
561
382
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
562
383
        self.assertEqual(repository_base,
563
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
384
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
564
385
        checkout_base = t.clone('checkout').base
565
386
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
566
387
        self.assertEqual(checkout_base,
567
388
                         dir.get_workingtree_transport(workingtree.WorkingTreeFormat3()).base)
568
389
 
569
 
    def test_meta1dir_uses_lockdir(self):
570
 
        """Meta1 format uses a LockDir to guard the whole directory, not a file."""
571
 
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
572
 
        t = dir.transport
573
 
        self.assertIsDirectory('branch-lock', t)
574
 
 
575
 
    def test_comparison(self):
576
 
        """Equality and inequality behave properly.
577
 
 
578
 
        Metadirs should compare equal iff they have the same repo, branch and
579
 
        tree formats.
580
 
        """
581
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
582
 
        self.assertEqual(mydir, mydir)
583
 
        self.assertFalse(mydir != mydir)
584
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
585
 
        self.assertEqual(otherdir, mydir)
586
 
        self.assertFalse(otherdir != mydir)
587
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
588
 
        self.assertNotEqual(otherdir2, mydir)
589
 
        self.assertFalse(otherdir2 == mydir)
590
 
 
591
 
    def test_needs_conversion_different_working_tree(self):
592
 
        # meta1dirs need an conversion if any element is not the default.
593
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
594
 
        # test with 
595
 
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
596
 
        bzrdir.BzrDirFormat._set_default_format(new_default)
597
 
        try:
598
 
            tree = self.make_branch_and_tree('tree', format='knit')
599
 
            self.assertTrue(tree.bzrdir.needs_format_conversion())
600
 
        finally:
601
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
602
 
 
603
 
 
 
390
        
604
391
class TestFormat5(TestCaseWithTransport):
605
392
    """Tests specific to the version 5 bzrdir format."""
606
393
 
628
415
        # format 5 dirs need a conversion if they are not the default.
629
416
        # and they start of not the default.
630
417
        old_format = bzrdir.BzrDirFormat.get_default_format()
631
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
418
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
632
419
        try:
633
420
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
634
421
            self.assertFalse(dir.needs_format_conversion())
635
422
        finally:
636
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
423
            bzrdir.BzrDirFormat.set_default_format(old_format)
637
424
        self.assertTrue(dir.needs_format_conversion())
638
425
 
639
426
 
663
450
    def test_needs_conversion(self):
664
451
        # format 6 dirs need an conversion if they are not the default.
665
452
        old_format = bzrdir.BzrDirFormat.get_default_format()
666
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
453
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
667
454
        try:
668
455
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
669
456
            self.assertTrue(dir.needs_format_conversion())
670
457
        finally:
671
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
672
 
 
673
 
 
674
 
class NotBzrDir(bzrlib.bzrdir.BzrDir):
675
 
    """A non .bzr based control directory."""
676
 
 
677
 
    def __init__(self, transport, format):
678
 
        self._format = format
679
 
        self.root_transport = transport
680
 
        self.transport = transport.clone('.not')
681
 
 
682
 
 
683
 
class NotBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
684
 
    """A test class representing any non-.bzr based disk format."""
685
 
 
686
 
    def initialize_on_transport(self, transport):
687
 
        """Initialize a new .not dir in the base directory of a Transport."""
688
 
        transport.mkdir('.not')
689
 
        return self.open(transport)
690
 
 
691
 
    def open(self, transport):
692
 
        """Open this directory."""
693
 
        return NotBzrDir(transport, self)
694
 
 
695
 
    @classmethod
696
 
    def _known_formats(self):
697
 
        return set([NotBzrDirFormat()])
698
 
 
699
 
    @classmethod
700
 
    def probe_transport(self, transport):
701
 
        """Our format is present if the transport ends in '.not/'."""
702
 
        if transport.has('.not'):
703
 
            return NotBzrDirFormat()
704
 
 
705
 
 
706
 
class TestNotBzrDir(TestCaseWithTransport):
707
 
    """Tests for using the bzrdir api with a non .bzr based disk format.
708
 
    
709
 
    If/when one of these is in the core, we can let the implementation tests
710
 
    verify this works.
711
 
    """
712
 
 
713
 
    def test_create_and_find_format(self):
714
 
        # create a .notbzr dir 
715
 
        format = NotBzrDirFormat()
716
 
        dir = format.initialize(self.get_url())
717
 
        self.assertIsInstance(dir, NotBzrDir)
718
 
        # now probe for it.
719
 
        bzrlib.bzrdir.BzrDirFormat.register_control_format(format)
720
 
        try:
721
 
            found = bzrlib.bzrdir.BzrDirFormat.find_format(
722
 
                get_transport(self.get_url()))
723
 
            self.assertIsInstance(found, NotBzrDirFormat)
724
 
        finally:
725
 
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(format)
726
 
 
727
 
    def test_included_in_known_formats(self):
728
 
        bzrlib.bzrdir.BzrDirFormat.register_control_format(NotBzrDirFormat)
729
 
        try:
730
 
            formats = bzrlib.bzrdir.BzrDirFormat.known_formats()
731
 
            for format in formats:
732
 
                if isinstance(format, NotBzrDirFormat):
733
 
                    return
734
 
            self.fail("No NotBzrDirFormat in %s" % formats)
735
 
        finally:
736
 
            bzrlib.bzrdir.BzrDirFormat.unregister_control_format(NotBzrDirFormat)
 
458
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
459
        self.assertFalse(dir.needs_format_conversion())
737
460
 
738
461
 
739
462
class NonLocalTests(TestCaseWithTransport):
741
464
 
742
465
    def setUp(self):
743
466
        super(NonLocalTests, self).setUp()
744
 
        self.vfs_transport_factory = MemoryServer
 
467
        self.transport_server = MemoryServer
745
468
    
746
469
    def test_create_branch_convenience(self):
747
470
        # outside a repo the default convenience output is a repo+branch_tree
748
 
        format = bzrdir.format_registry.make_bzrdir('knit')
749
 
        branch = bzrdir.BzrDir.create_branch_convenience(
750
 
            self.get_url('foo'), format=format)
751
 
        self.assertRaises(errors.NoWorkingTree,
752
 
                          branch.bzrdir.open_workingtree)
753
 
        branch.bzrdir.open_repository()
 
471
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
472
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
473
        try:
 
474
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
 
475
            self.assertRaises(errors.NoWorkingTree,
 
476
                              branch.bzrdir.open_workingtree)
 
477
            branch.bzrdir.open_repository()
 
478
        finally:
 
479
            bzrdir.BzrDirFormat.set_default_format(old_format)
754
480
 
755
481
    def test_create_branch_convenience_force_tree_not_local_fails(self):
756
482
        # outside a repo the default convenience output is a repo+branch_tree
757
 
        format = bzrdir.format_registry.make_bzrdir('knit')
758
 
        self.assertRaises(errors.NotLocalUrl,
759
 
            bzrdir.BzrDir.create_branch_convenience,
760
 
            self.get_url('foo'),
761
 
            force_new_tree=True,
762
 
            format=format)
763
 
        t = get_transport(self.get_url('.'))
764
 
        self.assertFalse(t.has('foo'))
765
 
 
766
 
    def test_clone(self):
767
 
        # clone into a nonlocal path works
768
 
        format = bzrdir.format_registry.make_bzrdir('knit')
769
 
        branch = bzrdir.BzrDir.create_branch_convenience('local',
770
 
                                                         format=format)
771
 
        branch.bzrdir.open_workingtree()
772
 
        result = branch.bzrdir.clone(self.get_url('remote'))
773
 
        self.assertRaises(errors.NoWorkingTree,
774
 
                          result.open_workingtree)
775
 
        result.open_branch()
776
 
        result.open_repository()
777
 
 
778
 
    def test_checkout_metadir(self):
779
 
        # checkout_metadir has reasonable working tree format even when no
780
 
        # working tree is present
781
 
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
782
 
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
783
 
        checkout_format = my_bzrdir.checkout_metadir()
784
 
        self.assertIsInstance(checkout_format.workingtree_format,
785
 
                              workingtree.WorkingTreeFormat3)
786
 
 
787
 
 
788
 
class TestHTTPRedirectionLoop(object):
789
 
    """Test redirection loop between two http servers.
790
 
 
791
 
    This MUST be used by daughter classes that also inherit from
792
 
    TestCaseWithTwoWebservers.
793
 
 
794
 
    We can't inherit directly from TestCaseWithTwoWebservers or the
795
 
    test framework will try to create an instance which cannot
796
 
    run, its implementation being incomplete. 
797
 
    """
798
 
 
799
 
    # Should be defined by daughter classes to ensure redirection
800
 
    # still use the same transport implementation (not currently
801
 
    # enforced as it's a bit tricky to get right (see the FIXME
802
 
    # in BzrDir.open_from_transport for the unique use case so
803
 
    # far)
804
 
    _qualifier = None
805
 
 
806
 
    def create_transport_readonly_server(self):
807
 
        return HTTPServerRedirecting()
808
 
 
809
 
    def create_transport_secondary_server(self):
810
 
        return HTTPServerRedirecting()
811
 
 
812
 
    def setUp(self):
813
 
        # Both servers redirect to each server creating a loop
814
 
        super(TestHTTPRedirectionLoop, self).setUp()
815
 
        # The redirections will point to the new server
816
 
        self.new_server = self.get_readonly_server()
817
 
        # The requests to the old server will be redirected
818
 
        self.old_server = self.get_secondary_server()
819
 
        # Configure the redirections
820
 
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
821
 
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
822
 
 
823
 
    def _qualified_url(self, host, port):
824
 
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
825
 
 
826
 
    def test_loop(self):
827
 
        # Starting from either server should loop
828
 
        old_url = self._qualified_url(self.old_server.host, 
829
 
                                      self.old_server.port)
830
 
        oldt = self._transport(old_url)
831
 
        self.assertRaises(errors.NotBranchError,
832
 
                          bzrdir.BzrDir.open_from_transport, oldt)
833
 
        new_url = self._qualified_url(self.new_server.host, 
834
 
                                      self.new_server.port)
835
 
        newt = self._transport(new_url)
836
 
        self.assertRaises(errors.NotBranchError,
837
 
                          bzrdir.BzrDir.open_from_transport, newt)
838
 
 
839
 
 
840
 
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
841
 
                                  TestCaseWithTwoWebservers):
842
 
    """Tests redirections for urllib implementation"""
843
 
 
844
 
    _qualifier = 'urllib'
845
 
    _transport = HttpTransport_urllib
846
 
 
847
 
 
848
 
 
849
 
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
850
 
                                  TestHTTPRedirectionLoop,
851
 
                                  TestCaseWithTwoWebservers):
852
 
    """Tests redirections for pycurl implementation"""
853
 
 
854
 
    _qualifier = 'pycurl'
 
483
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
484
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
485
        try:
 
486
            self.assertRaises(errors.NotLocalUrl,
 
487
                bzrdir.BzrDir.create_branch_convenience,
 
488
                self.get_url('foo'),
 
489
                force_new_tree=True)
 
490
            t = get_transport(self.get_url('.'))
 
491
            self.assertFalse(t.has('foo'))
 
492
        finally:
 
493
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
494