~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-10-24 12:49:17 UTC
  • mfrom: (2935.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20071024124917-xb75eckyxx6vkrlg
Makefile fixes - hooks.html generation & allow python to be overridden (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
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
22
23
from StringIO import StringIO
23
24
 
 
25
from bzrlib import (
 
26
    bzrdir,
 
27
    errors,
 
28
    help_topics,
 
29
    repository,
 
30
    symbol_versioning,
 
31
    urlutils,
 
32
    workingtree,
 
33
    )
24
34
import bzrlib.branch
25
 
import bzrlib.bzrdir as bzrdir
26
 
import bzrlib.errors as errors
27
35
from bzrlib.errors import (NotBranchError,
28
36
                           UnknownFormatError,
29
37
                           UnsupportedFormatError,
30
38
                           )
31
 
import bzrlib.repository as repository
32
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
39
from bzrlib.symbol_versioning import (
 
40
    zero_ninetyone,
 
41
    )
 
42
from bzrlib.tests import (
 
43
    TestCase,
 
44
    TestCaseWithTransport,
 
45
    test_sftp_transport
 
46
    )
 
47
from bzrlib.tests.HttpServer import HttpServer
 
48
from bzrlib.tests.HTTPTestUtil import (
 
49
    TestCaseWithTwoWebservers,
 
50
    HTTPServerRedirecting,
 
51
    )
 
52
from bzrlib.tests.test_http import TestWithTransport_pycurl
33
53
from bzrlib.transport import get_transport
34
 
from bzrlib.transport.http import HttpServer
 
54
from bzrlib.transport.http._urllib import HttpTransport_urllib
35
55
from bzrlib.transport.memory import MemoryServer
36
 
import bzrlib.workingtree as workingtree
 
56
from bzrlib.repofmt import knitrepo, weaverepo
37
57
 
38
58
 
39
59
class TestDefaultFormat(TestCase):
42
62
        old_format = bzrdir.BzrDirFormat.get_default_format()
43
63
        # default is BzrDirFormat6
44
64
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
45
 
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
 
65
        self.applyDeprecated(symbol_versioning.zero_fourteen, 
 
66
                             bzrdir.BzrDirFormat.set_default_format, 
 
67
                             SampleBzrDirFormat())
46
68
        # creating a bzr dir should now create an instrumented dir.
47
69
        try:
48
70
            result = bzrdir.BzrDir.create('memory:///')
49
71
            self.failUnless(isinstance(result, SampleBzrDir))
50
72
        finally:
51
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
73
            self.applyDeprecated(symbol_versioning.zero_fourteen,
 
74
                bzrdir.BzrDirFormat.set_default_format, old_format)
52
75
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
53
76
 
54
77
 
 
78
class TestFormatRegistry(TestCase):
 
79
 
 
80
    def make_format_registry(self):
 
81
        my_format_registry = bzrdir.BzrDirFormatRegistry()
 
82
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
 
83
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
84
            ' repositories', deprecated=True)
 
85
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
 
86
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
 
87
        my_format_registry.register_metadir('knit',
 
88
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
 
89
            'Format using knits',
 
90
            )
 
91
        my_format_registry.set_default('knit')
 
92
        my_format_registry.register_metadir(
 
93
            'branch6',
 
94
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
 
95
            'Experimental successor to knit.  Use at your own risk.',
 
96
            branch_format='bzrlib.branch.BzrBranchFormat6')
 
97
        my_format_registry.register_metadir(
 
98
            'hidden format',
 
99
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
 
100
            'Experimental successor to knit.  Use at your own risk.',
 
101
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
 
102
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
 
103
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
 
104
            ' repositories', hidden=True)
 
105
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
 
106
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
 
107
            hidden=True)
 
108
        return my_format_registry
 
109
 
 
110
    def test_format_registry(self):
 
111
        my_format_registry = self.make_format_registry()
 
112
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
 
113
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
114
        my_bzrdir = my_format_registry.make_bzrdir('weave')
 
115
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
 
116
        my_bzrdir = my_format_registry.make_bzrdir('default')
 
117
        self.assertIsInstance(my_bzrdir.repository_format, 
 
118
            knitrepo.RepositoryFormatKnit1)
 
119
        my_bzrdir = my_format_registry.make_bzrdir('knit')
 
120
        self.assertIsInstance(my_bzrdir.repository_format, 
 
121
            knitrepo.RepositoryFormatKnit1)
 
122
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
 
123
        self.assertIsInstance(my_bzrdir.get_branch_format(),
 
124
                              bzrlib.branch.BzrBranchFormat6)
 
125
 
 
126
    def test_get_help(self):
 
127
        my_format_registry = self.make_format_registry()
 
128
        self.assertEqual('Format registered lazily',
 
129
                         my_format_registry.get_help('lazy'))
 
130
        self.assertEqual('Format using knits', 
 
131
                         my_format_registry.get_help('knit'))
 
132
        self.assertEqual('Format using knits', 
 
133
                         my_format_registry.get_help('default'))
 
134
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
 
135
                         ' checkouts or shared repositories', 
 
136
                         my_format_registry.get_help('weave'))
 
137
        
 
138
    def test_help_topic(self):
 
139
        topics = help_topics.HelpTopicRegistry()
 
140
        topics.register('formats', self.make_format_registry().help_topic, 
 
141
                        'Directory formats')
 
142
        topic = topics.get_detail('formats')
 
143
        new, deprecated = topic.split('Deprecated formats')
 
144
        self.assertContainsRe(new, 'These formats can be used')
 
145
        self.assertContainsRe(new, 
 
146
                ':knit:\n    \(native\) \(default\) Format using knits\n')
 
147
        self.assertContainsRe(deprecated, 
 
148
                ':lazy:\n    \(native\) Format registered lazily\n')
 
149
        self.assertNotContainsRe(new, 'hidden')
 
150
 
 
151
    def test_set_default_repository(self):
 
152
        default_factory = bzrdir.format_registry.get('default')
 
153
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
 
154
                       if v == default_factory and k != 'default'][0]
 
155
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
 
156
        try:
 
157
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
 
158
                          bzrdir.format_registry.get('default'))
 
159
            self.assertIs(
 
160
                repository.RepositoryFormat.get_default_format().__class__,
 
161
                knitrepo.RepositoryFormatKnit3)
 
162
        finally:
 
163
            bzrdir.format_registry.set_default_repository(old_default)
 
164
 
 
165
 
55
166
class SampleBranch(bzrlib.branch.Branch):
56
167
    """A dummy branch for guess what, dummy use."""
57
168
 
90
201
        """See BzrDirFormat.get_format_string()."""
91
202
        return "Sample .bzr dir format."
92
203
 
93
 
    def initialize(self, url):
 
204
    def initialize_on_transport(self, t):
94
205
        """Create a bzr dir."""
95
 
        t = get_transport(url)
96
206
        t.mkdir('.bzr')
97
 
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
 
207
        t.put_bytes('.bzr/branch-format', self.get_format_string())
98
208
        return SampleBzrDir(t, self)
99
209
 
100
210
    def is_supported(self):
129
239
    def test_find_format_unknown_format(self):
130
240
        t = get_transport(self.get_url())
131
241
        t.mkdir('.bzr')
132
 
        t.put('.bzr/branch-format', StringIO())
 
242
        t.put_bytes('.bzr/branch-format', '')
133
243
        self.assertRaises(UnknownFormatError,
134
244
                          bzrdir.BzrDirFormat.find_format,
135
245
                          get_transport('.'))
153
263
        # now open_downlevel should fail too.
154
264
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
155
265
 
156
 
    def test_create_repository(self):
 
266
    def test_create_repository_deprecated(self):
 
267
        # new interface is to make the bzrdir, then a repository within that.
157
268
        format = SampleBzrDirFormat()
158
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
159
 
        bzrdir.BzrDirFormat.set_default_format(format)
160
 
        try:
161
 
            repo = bzrdir.BzrDir.create_repository(self.get_url())
162
 
            self.assertEqual('A repository', repo)
163
 
        finally:
164
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
269
        repo = self.applyDeprecated(zero_ninetyone,
 
270
                bzrdir.BzrDir.create_repository,
 
271
                self.get_url(), format=format)
 
272
        self.assertEqual('A repository', repo)
165
273
 
166
274
    def test_create_repository_shared(self):
 
275
        # new interface is to make the bzrdir, then a repository within that.
167
276
        old_format = bzrdir.BzrDirFormat.get_default_format()
168
 
        repo = bzrdir.BzrDir.create_repository('.', shared=True)
 
277
        repo = self.applyDeprecated(zero_ninetyone,
 
278
                bzrdir.BzrDir.create_repository,
 
279
                '.', shared=True)
169
280
        self.assertTrue(repo.is_shared())
170
281
 
171
282
    def test_create_repository_nonshared(self):
 
283
        # new interface is to make the bzrdir, then a repository within that.
172
284
        old_format = bzrdir.BzrDirFormat.get_default_format()
173
 
        repo = bzrdir.BzrDir.create_repository('.')
 
285
        repo = self.applyDeprecated(zero_ninetyone,
 
286
                bzrdir.BzrDir.create_repository,
 
287
                '.')
174
288
        self.assertFalse(repo.is_shared())
175
289
 
176
290
    def test_create_repository_under_shared(self):
177
291
        # an explicit create_repository always does so.
178
292
        # we trust the format is right from the 'create_repository test'
179
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
180
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
181
 
        try:
182
 
            self.make_repository('.', shared=True)
183
 
            repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
184
 
            self.assertTrue(isinstance(repo, repository.Repository))
185
 
            self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
186
 
        finally:
187
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
293
        # new interface is to make the bzrdir, then a repository within that.
 
294
        format = bzrdir.format_registry.make_bzrdir('knit')
 
295
        self.make_repository('.', shared=True, format=format)
 
296
        repo = self.applyDeprecated(zero_ninetyone,
 
297
                bzrdir.BzrDir.create_repository,
 
298
                self.get_url('child'),
 
299
                format=format)
 
300
        self.assertTrue(isinstance(repo, repository.Repository))
 
301
        self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
188
302
 
189
303
    def test_create_branch_and_repo_uses_default(self):
190
304
        format = SampleBzrDirFormat()
191
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
192
 
        bzrdir.BzrDirFormat.set_default_format(format)
193
 
        try:
194
 
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
195
 
            self.assertTrue(isinstance(branch, SampleBranch))
196
 
        finally:
197
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
305
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
 
306
                                                      format=format)
 
307
        self.assertTrue(isinstance(branch, SampleBranch))
198
308
 
199
309
    def test_create_branch_and_repo_under_shared(self):
200
310
        # creating a branch and repo in a shared repo uses the
201
311
        # shared repository
202
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
203
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
204
 
        try:
205
 
            self.make_repository('.', shared=True)
206
 
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
207
 
            self.assertRaises(errors.NoRepositoryPresent,
208
 
                              branch.bzrdir.open_repository)
209
 
        finally:
210
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
312
        format = bzrdir.format_registry.make_bzrdir('knit')
 
313
        self.make_repository('.', shared=True, format=format)
 
314
        branch = bzrdir.BzrDir.create_branch_and_repo(
 
315
            self.get_url('child'), format=format)
 
316
        self.assertRaises(errors.NoRepositoryPresent,
 
317
                          branch.bzrdir.open_repository)
211
318
 
212
319
    def test_create_branch_and_repo_under_shared_force_new(self):
213
320
        # creating a branch and repo in a shared repo can be forced to 
214
321
        # make a new repo
215
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
216
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
217
 
        try:
218
 
            self.make_repository('.', shared=True)
219
 
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
220
 
                                                          force_new_repo=True)
221
 
            branch.bzrdir.open_repository()
222
 
        finally:
223
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
322
        format = bzrdir.format_registry.make_bzrdir('knit')
 
323
        self.make_repository('.', shared=True, format=format)
 
324
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
325
                                                      force_new_repo=True,
 
326
                                                      format=format)
 
327
        branch.bzrdir.open_repository()
224
328
 
225
329
    def test_create_standalone_working_tree(self):
226
330
        format = SampleBzrDirFormat()
227
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
228
 
        bzrdir.BzrDirFormat.set_default_format(format)
229
 
        try:
230
 
            # note this is deliberately readonly, as this failure should 
231
 
            # occur before any writes.
232
 
            self.assertRaises(errors.NotLocalUrl,
233
 
                              bzrdir.BzrDir.create_standalone_workingtree,
234
 
                              self.get_readonly_url())
235
 
            tree = bzrdir.BzrDir.create_standalone_workingtree('.')
236
 
            self.assertEqual('A tree', tree)
237
 
        finally:
238
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
331
        # note this is deliberately readonly, as this failure should 
 
332
        # occur before any writes.
 
333
        self.assertRaises(errors.NotLocalUrl,
 
334
                          bzrdir.BzrDir.create_standalone_workingtree,
 
335
                          self.get_readonly_url(), format=format)
 
336
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
 
337
                                                           format=format)
 
338
        self.assertEqual('A tree', tree)
239
339
 
240
340
    def test_create_standalone_working_tree_under_shared_repo(self):
241
341
        # create standalone working tree always makes a repo.
242
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
243
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
244
 
        try:
245
 
            self.make_repository('.', shared=True)
246
 
            # note this is deliberately readonly, as this failure should 
247
 
            # occur before any writes.
248
 
            self.assertRaises(errors.NotLocalUrl,
249
 
                              bzrdir.BzrDir.create_standalone_workingtree,
250
 
                              self.get_readonly_url('child'))
251
 
            tree = bzrdir.BzrDir.create_standalone_workingtree('child')
252
 
            tree.bzrdir.open_repository()
253
 
        finally:
254
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
342
        format = bzrdir.format_registry.make_bzrdir('knit')
 
343
        self.make_repository('.', shared=True, format=format)
 
344
        # note this is deliberately readonly, as this failure should 
 
345
        # occur before any writes.
 
346
        self.assertRaises(errors.NotLocalUrl,
 
347
                          bzrdir.BzrDir.create_standalone_workingtree,
 
348
                          self.get_readonly_url('child'), format=format)
 
349
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
 
350
            format=format)
 
351
        tree.bzrdir.open_repository()
255
352
 
256
353
    def test_create_branch_convenience(self):
257
354
        # outside a repo the default convenience output is a repo+branch_tree
258
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
259
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
260
 
        try:
261
 
            branch = bzrdir.BzrDir.create_branch_convenience('.')
262
 
            branch.bzrdir.open_workingtree()
263
 
            branch.bzrdir.open_repository()
264
 
        finally:
265
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
355
        format = bzrdir.format_registry.make_bzrdir('knit')
 
356
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
 
357
        branch.bzrdir.open_workingtree()
 
358
        branch.bzrdir.open_repository()
 
359
 
 
360
    def test_create_branch_convenience_possible_transports(self):
 
361
        """Check that the optional 'possible_transports' is recognized"""
 
362
        format = bzrdir.format_registry.make_bzrdir('knit')
 
363
        t = self.get_transport()
 
364
        branch = bzrdir.BzrDir.create_branch_convenience(
 
365
            '.', format=format, possible_transports=[t])
 
366
        branch.bzrdir.open_workingtree()
 
367
        branch.bzrdir.open_repository()
266
368
 
267
369
    def test_create_branch_convenience_root(self):
268
370
        """Creating a branch at the root of a fs should work."""
269
 
        self.transport_server = MemoryServer
 
371
        self.vfs_transport_factory = MemoryServer
270
372
        # outside a repo the default convenience output is a repo+branch_tree
271
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
272
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
273
 
        try:
274
 
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
275
 
            self.assertRaises(errors.NoWorkingTree,
276
 
                              branch.bzrdir.open_workingtree)
277
 
            branch.bzrdir.open_repository()
278
 
        finally:
279
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
373
        format = bzrdir.format_registry.make_bzrdir('knit')
 
374
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
 
375
                                                         format=format)
 
376
        self.assertRaises(errors.NoWorkingTree,
 
377
                          branch.bzrdir.open_workingtree)
 
378
        branch.bzrdir.open_repository()
280
379
 
281
380
    def test_create_branch_convenience_under_shared_repo(self):
282
381
        # inside a repo the default convenience output is a branch+ follow the
283
382
        # repo tree policy
284
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
285
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
286
 
        try:
287
 
            self.make_repository('.', shared=True)
288
 
            branch = bzrdir.BzrDir.create_branch_convenience('child')
289
 
            branch.bzrdir.open_workingtree()
290
 
            self.assertRaises(errors.NoRepositoryPresent,
291
 
                              branch.bzrdir.open_repository)
292
 
        finally:
293
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
383
        format = bzrdir.format_registry.make_bzrdir('knit')
 
384
        self.make_repository('.', shared=True, format=format)
 
385
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
386
            format=format)
 
387
        branch.bzrdir.open_workingtree()
 
388
        self.assertRaises(errors.NoRepositoryPresent,
 
389
                          branch.bzrdir.open_repository)
294
390
            
295
391
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
296
392
        # inside a repo the default convenience output is a branch+ follow the
297
393
        # repo tree policy but we can override that
298
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
299
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
300
 
        try:
301
 
            self.make_repository('.', shared=True)
302
 
            branch = bzrdir.BzrDir.create_branch_convenience('child',
303
 
                force_new_tree=False)
304
 
            self.assertRaises(errors.NoWorkingTree,
305
 
                              branch.bzrdir.open_workingtree)
306
 
            self.assertRaises(errors.NoRepositoryPresent,
307
 
                              branch.bzrdir.open_repository)
308
 
        finally:
309
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
394
        format = bzrdir.format_registry.make_bzrdir('knit')
 
395
        self.make_repository('.', shared=True, format=format)
 
396
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
397
            force_new_tree=False, format=format)
 
398
        self.assertRaises(errors.NoWorkingTree,
 
399
                          branch.bzrdir.open_workingtree)
 
400
        self.assertRaises(errors.NoRepositoryPresent,
 
401
                          branch.bzrdir.open_repository)
310
402
            
311
403
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
312
404
        # inside a repo the default convenience output is a branch+ follow the
313
405
        # repo tree policy
314
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
315
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
316
 
        try:
317
 
            repo = self.make_repository('.', shared=True)
318
 
            repo.set_make_working_trees(False)
319
 
            branch = bzrdir.BzrDir.create_branch_convenience('child')
320
 
            self.assertRaises(errors.NoWorkingTree,
321
 
                              branch.bzrdir.open_workingtree)
322
 
            self.assertRaises(errors.NoRepositoryPresent,
323
 
                              branch.bzrdir.open_repository)
324
 
        finally:
325
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
406
        format = bzrdir.format_registry.make_bzrdir('knit')
 
407
        repo = self.make_repository('.', shared=True, format=format)
 
408
        repo.set_make_working_trees(False)
 
409
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
 
410
                                                         format=format)
 
411
        self.assertRaises(errors.NoWorkingTree,
 
412
                          branch.bzrdir.open_workingtree)
 
413
        self.assertRaises(errors.NoRepositoryPresent,
 
414
                          branch.bzrdir.open_repository)
326
415
 
327
416
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
328
417
        # inside a repo the default convenience output is a branch+ follow the
329
418
        # repo tree policy but we can override that
330
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
331
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
332
 
        try:
333
 
            repo = self.make_repository('.', shared=True)
334
 
            repo.set_make_working_trees(False)
335
 
            branch = bzrdir.BzrDir.create_branch_convenience('child',
336
 
                force_new_tree=True)
337
 
            branch.bzrdir.open_workingtree()
338
 
            self.assertRaises(errors.NoRepositoryPresent,
339
 
                              branch.bzrdir.open_repository)
340
 
        finally:
341
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
419
        format = bzrdir.format_registry.make_bzrdir('knit')
 
420
        repo = self.make_repository('.', shared=True, format=format)
 
421
        repo.set_make_working_trees(False)
 
422
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
423
            force_new_tree=True, format=format)
 
424
        branch.bzrdir.open_workingtree()
 
425
        self.assertRaises(errors.NoRepositoryPresent,
 
426
                          branch.bzrdir.open_repository)
342
427
 
343
428
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
344
429
        # inside a repo the default convenience output is overridable to give
345
430
        # repo+branch+tree
346
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
347
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
348
 
        try:
349
 
            self.make_repository('.', shared=True)
350
 
            branch = bzrdir.BzrDir.create_branch_convenience('child',
351
 
                force_new_repo=True)
352
 
            branch.bzrdir.open_repository()
353
 
            branch.bzrdir.open_workingtree()
354
 
        finally:
355
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
431
        format = bzrdir.format_registry.make_bzrdir('knit')
 
432
        self.make_repository('.', shared=True, format=format)
 
433
        branch = bzrdir.BzrDir.create_branch_convenience('child',
 
434
            force_new_repo=True, format=format)
 
435
        branch.bzrdir.open_repository()
 
436
        branch.bzrdir.open_workingtree()
356
437
 
357
438
 
358
439
class ChrootedTests(TestCaseWithTransport):
365
446
 
366
447
    def setUp(self):
367
448
        super(ChrootedTests, self).setUp()
368
 
        if not self.transport_server == MemoryServer:
 
449
        if not self.vfs_transport_factory == MemoryServer:
369
450
            self.transport_readonly_server = HttpServer
370
451
 
371
452
    def test_open_containing(self):
392
473
            get_transport(self.get_readonly_url('g/p/q')))
393
474
        self.assertEqual('g/p/q', relpath)
394
475
 
 
476
    def test_open_containing_tree_or_branch(self):
 
477
        def local_branch_path(branch):
 
478
             return os.path.realpath(
 
479
                urlutils.local_path_from_url(branch.base))
 
480
 
 
481
        self.make_branch_and_tree('topdir')
 
482
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
483
            'topdir/foo')
 
484
        self.assertEqual(os.path.realpath('topdir'),
 
485
                         os.path.realpath(tree.basedir))
 
486
        self.assertEqual(os.path.realpath('topdir'),
 
487
                         local_branch_path(branch))
 
488
        self.assertIs(tree.bzrdir, branch.bzrdir)
 
489
        self.assertEqual('foo', relpath)
 
490
        # opening from non-local should not return the tree
 
491
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
492
            self.get_readonly_url('topdir/foo'))
 
493
        self.assertEqual(None, tree)
 
494
        self.assertEqual('foo', relpath)
 
495
        # without a tree:
 
496
        self.make_branch('topdir/foo')
 
497
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
 
498
            'topdir/foo')
 
499
        self.assertIs(tree, None)
 
500
        self.assertEqual(os.path.realpath('topdir/foo'),
 
501
                         local_branch_path(branch))
 
502
        self.assertEqual('', relpath)
 
503
 
 
504
    def test_open_from_transport(self):
 
505
        # transport pointing at bzrdir should give a bzrdir with root transport
 
506
        # set to the given transport
 
507
        control = bzrdir.BzrDir.create(self.get_url())
 
508
        transport = get_transport(self.get_url())
 
509
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
 
510
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
 
511
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
 
512
        
 
513
    def test_open_from_transport_no_bzrdir(self):
 
514
        transport = get_transport(self.get_url())
 
515
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
516
                          transport)
 
517
 
 
518
    def test_open_from_transport_bzrdir_in_parent(self):
 
519
        control = bzrdir.BzrDir.create(self.get_url())
 
520
        transport = get_transport(self.get_url())
 
521
        transport.mkdir('subdir')
 
522
        transport = transport.clone('subdir')
 
523
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
 
524
                          transport)
 
525
 
 
526
    def test_sprout_recursive(self):
 
527
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
 
528
        sub_tree = self.make_branch_and_tree('tree1/subtree',
 
529
            format='dirstate-with-subtree')
 
530
        tree.add_reference(sub_tree)
 
531
        self.build_tree(['tree1/subtree/file'])
 
532
        sub_tree.add('file')
 
533
        tree.commit('Initial commit')
 
534
        tree.bzrdir.sprout('tree2')
 
535
        self.failUnlessExists('tree2/subtree/file')
 
536
 
 
537
    def test_cloning_metadir(self):
 
538
        """Ensure that cloning metadir is suitable"""
 
539
        bzrdir = self.make_bzrdir('bzrdir')
 
540
        bzrdir.cloning_metadir()
 
541
        branch = self.make_branch('branch', format='knit')
 
542
        format = branch.bzrdir.cloning_metadir()
 
543
        self.assertIsInstance(format.workingtree_format,
 
544
            workingtree.WorkingTreeFormat3)
 
545
 
 
546
    def test_sprout_recursive_treeless(self):
 
547
        tree = self.make_branch_and_tree('tree1',
 
548
            format='dirstate-with-subtree')
 
549
        sub_tree = self.make_branch_and_tree('tree1/subtree',
 
550
            format='dirstate-with-subtree')
 
551
        tree.add_reference(sub_tree)
 
552
        self.build_tree(['tree1/subtree/file'])
 
553
        sub_tree.add('file')
 
554
        tree.commit('Initial commit')
 
555
        tree.bzrdir.destroy_workingtree()
 
556
        repo = self.make_repository('repo', shared=True,
 
557
            format='dirstate-with-subtree')
 
558
        repo.set_make_working_trees(False)
 
559
        tree.bzrdir.sprout('repo/tree2')
 
560
        self.failUnlessExists('repo/tree2/subtree')
 
561
        self.failIfExists('repo/tree2/subtree/file')
 
562
 
395
563
 
396
564
class TestMeta1DirFormat(TestCaseWithTransport):
397
565
    """Tests specific to the meta1 dir format."""
406
574
        repository_base = t.clone('repository').base
407
575
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
408
576
        self.assertEqual(repository_base,
409
 
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
 
577
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
410
578
        checkout_base = t.clone('checkout').base
411
579
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
412
580
        self.assertEqual(checkout_base,
418
586
        t = dir.transport
419
587
        self.assertIsDirectory('branch-lock', t)
420
588
 
421
 
        
 
589
    def test_comparison(self):
 
590
        """Equality and inequality behave properly.
 
591
 
 
592
        Metadirs should compare equal iff they have the same repo, branch and
 
593
        tree formats.
 
594
        """
 
595
        mydir = bzrdir.format_registry.make_bzrdir('knit')
 
596
        self.assertEqual(mydir, mydir)
 
597
        self.assertFalse(mydir != mydir)
 
598
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
 
599
        self.assertEqual(otherdir, mydir)
 
600
        self.assertFalse(otherdir != mydir)
 
601
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
 
602
        self.assertNotEqual(otherdir2, mydir)
 
603
        self.assertFalse(otherdir2 == mydir)
 
604
 
 
605
    def test_needs_conversion_different_working_tree(self):
 
606
        # meta1dirs need an conversion if any element is not the default.
 
607
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
608
        # test with 
 
609
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
 
610
        bzrdir.BzrDirFormat._set_default_format(new_default)
 
611
        try:
 
612
            tree = self.make_branch_and_tree('tree', format='knit')
 
613
            self.assertTrue(tree.bzrdir.needs_format_conversion())
 
614
        finally:
 
615
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
616
 
 
617
 
422
618
class TestFormat5(TestCaseWithTransport):
423
619
    """Tests specific to the version 5 bzrdir format."""
424
620
 
446
642
        # format 5 dirs need a conversion if they are not the default.
447
643
        # and they start of not the default.
448
644
        old_format = bzrdir.BzrDirFormat.get_default_format()
449
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
 
645
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
450
646
        try:
451
647
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
452
648
            self.assertFalse(dir.needs_format_conversion())
453
649
        finally:
454
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
650
            bzrdir.BzrDirFormat._set_default_format(old_format)
455
651
        self.assertTrue(dir.needs_format_conversion())
456
652
 
457
653
 
481
677
    def test_needs_conversion(self):
482
678
        # format 6 dirs need an conversion if they are not the default.
483
679
        old_format = bzrdir.BzrDirFormat.get_default_format()
484
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
680
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
485
681
        try:
486
682
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
487
683
            self.assertTrue(dir.needs_format_conversion())
488
684
        finally:
489
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
685
            bzrdir.BzrDirFormat._set_default_format(old_format)
490
686
 
491
687
 
492
688
class NotBzrDir(bzrlib.bzrdir.BzrDir):
559
755
 
560
756
    def setUp(self):
561
757
        super(NonLocalTests, self).setUp()
562
 
        self.transport_server = MemoryServer
 
758
        self.vfs_transport_factory = MemoryServer
563
759
    
564
760
    def test_create_branch_convenience(self):
565
761
        # outside a repo the default convenience output is a repo+branch_tree
566
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
567
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
568
 
        try:
569
 
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
570
 
            self.assertRaises(errors.NoWorkingTree,
571
 
                              branch.bzrdir.open_workingtree)
572
 
            branch.bzrdir.open_repository()
573
 
        finally:
574
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
762
        format = bzrdir.format_registry.make_bzrdir('knit')
 
763
        branch = bzrdir.BzrDir.create_branch_convenience(
 
764
            self.get_url('foo'), format=format)
 
765
        self.assertRaises(errors.NoWorkingTree,
 
766
                          branch.bzrdir.open_workingtree)
 
767
        branch.bzrdir.open_repository()
575
768
 
576
769
    def test_create_branch_convenience_force_tree_not_local_fails(self):
577
770
        # outside a repo the default convenience output is a repo+branch_tree
578
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
579
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
580
 
        try:
581
 
            self.assertRaises(errors.NotLocalUrl,
582
 
                bzrdir.BzrDir.create_branch_convenience,
583
 
                self.get_url('foo'),
584
 
                force_new_tree=True)
585
 
            t = get_transport(self.get_url('.'))
586
 
            self.assertFalse(t.has('foo'))
587
 
        finally:
588
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
771
        format = bzrdir.format_registry.make_bzrdir('knit')
 
772
        self.assertRaises(errors.NotLocalUrl,
 
773
            bzrdir.BzrDir.create_branch_convenience,
 
774
            self.get_url('foo'),
 
775
            force_new_tree=True,
 
776
            format=format)
 
777
        t = get_transport(self.get_url('.'))
 
778
        self.assertFalse(t.has('foo'))
589
779
 
590
780
    def test_clone(self):
591
781
        # clone into a nonlocal path works
592
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
593
 
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
594
 
        try:
595
 
            branch = bzrdir.BzrDir.create_branch_convenience('local')
596
 
        finally:
597
 
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
782
        format = bzrdir.format_registry.make_bzrdir('knit')
 
783
        branch = bzrdir.BzrDir.create_branch_convenience('local',
 
784
                                                         format=format)
598
785
        branch.bzrdir.open_workingtree()
599
786
        result = branch.bzrdir.clone(self.get_url('remote'))
600
787
        self.assertRaises(errors.NoWorkingTree,
602
789
        result.open_branch()
603
790
        result.open_repository()
604
791
 
 
792
    def test_checkout_metadir(self):
 
793
        # checkout_metadir has reasonable working tree format even when no
 
794
        # working tree is present
 
795
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
 
796
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
 
797
        checkout_format = my_bzrdir.checkout_metadir()
 
798
        self.assertIsInstance(checkout_format.workingtree_format,
 
799
                              workingtree.WorkingTreeFormat3)
 
800
 
 
801
 
 
802
class TestHTTPRedirectionLoop(object):
 
803
    """Test redirection loop between two http servers.
 
804
 
 
805
    This MUST be used by daughter classes that also inherit from
 
806
    TestCaseWithTwoWebservers.
 
807
 
 
808
    We can't inherit directly from TestCaseWithTwoWebservers or the
 
809
    test framework will try to create an instance which cannot
 
810
    run, its implementation being incomplete. 
 
811
    """
 
812
 
 
813
    # Should be defined by daughter classes to ensure redirection
 
814
    # still use the same transport implementation (not currently
 
815
    # enforced as it's a bit tricky to get right (see the FIXME
 
816
    # in BzrDir.open_from_transport for the unique use case so
 
817
    # far)
 
818
    _qualifier = None
 
819
 
 
820
    def create_transport_readonly_server(self):
 
821
        return HTTPServerRedirecting()
 
822
 
 
823
    def create_transport_secondary_server(self):
 
824
        return HTTPServerRedirecting()
 
825
 
 
826
    def setUp(self):
 
827
        # Both servers redirect to each server creating a loop
 
828
        super(TestHTTPRedirectionLoop, self).setUp()
 
829
        # The redirections will point to the new server
 
830
        self.new_server = self.get_readonly_server()
 
831
        # The requests to the old server will be redirected
 
832
        self.old_server = self.get_secondary_server()
 
833
        # Configure the redirections
 
834
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
 
835
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
 
836
 
 
837
    def _qualified_url(self, host, port):
 
838
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
 
839
 
 
840
    def test_loop(self):
 
841
        # Starting from either server should loop
 
842
        old_url = self._qualified_url(self.old_server.host, 
 
843
                                      self.old_server.port)
 
844
        oldt = self._transport(old_url)
 
845
        self.assertRaises(errors.NotBranchError,
 
846
                          bzrdir.BzrDir.open_from_transport, oldt)
 
847
        new_url = self._qualified_url(self.new_server.host, 
 
848
                                      self.new_server.port)
 
849
        newt = self._transport(new_url)
 
850
        self.assertRaises(errors.NotBranchError,
 
851
                          bzrdir.BzrDir.open_from_transport, newt)
 
852
 
 
853
 
 
854
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
 
855
                                  TestCaseWithTwoWebservers):
 
856
    """Tests redirections for urllib implementation"""
 
857
 
 
858
    _qualifier = 'urllib'
 
859
    _transport = HttpTransport_urllib
 
860
 
 
861
 
 
862
 
 
863
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
 
864
                                  TestHTTPRedirectionLoop,
 
865
                                  TestCaseWithTwoWebservers):
 
866
    """Tests redirections for pycurl implementation"""
 
867
 
 
868
    _qualifier = 'pycurl'