~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2006-06-14 19:45:57 UTC
  • mto: This revision was merged to the branch mainline in revision 1777.
  • Revision ID: abentley@panoramicfeedback.com-20060614194557-6b499aa1cf03f7e6
Use create_signature for signing policy, deprecate check_signatures for this

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005, 2006 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
23
 
import os.path
24
22
from StringIO import StringIO
25
 
import subprocess
26
 
import sys
27
23
 
28
 
from bzrlib import (
29
 
    bzrdir,
30
 
    errors,
31
 
    help_topics,
32
 
    repository,
33
 
    osutils,
34
 
    symbol_versioning,
35
 
    urlutils,
36
 
    win32utils,
37
 
    workingtree,
38
 
    )
39
24
import bzrlib.branch
 
25
import bzrlib.bzrdir as bzrdir
 
26
import bzrlib.errors as errors
40
27
from bzrlib.errors import (NotBranchError,
41
28
                           UnknownFormatError,
42
29
                           UnsupportedFormatError,
43
30
                           )
44
 
from bzrlib.tests import (
45
 
    TestCase,
46
 
    TestCaseWithMemoryTransport,
47
 
    TestCaseWithTransport,
48
 
    TestSkipped,
49
 
    test_sftp_transport
50
 
    )
51
 
from bzrlib.tests.http_server import HttpServer
52
 
from bzrlib.tests.http_utils import (
53
 
    TestCaseWithTwoWebservers,
54
 
    HTTPServerRedirecting,
55
 
    )
56
 
from bzrlib.tests.test_http import TestWithTransport_pycurl
 
31
import bzrlib.repository as repository
 
32
from bzrlib.tests import TestCase, TestCaseWithTransport
57
33
from bzrlib.transport import get_transport
58
 
from bzrlib.transport.http._urllib import HttpTransport_urllib
 
34
from bzrlib.transport.http import HttpServer
59
35
from bzrlib.transport.memory import MemoryServer
60
 
from bzrlib.repofmt import knitrepo, weaverepo
 
36
import bzrlib.workingtree as workingtree
61
37
 
62
38
 
63
39
class TestDefaultFormat(TestCase):
66
42
        old_format = bzrdir.BzrDirFormat.get_default_format()
67
43
        # default is BzrDirFormat6
68
44
        self.failUnless(isinstance(old_format, bzrdir.BzrDirMetaFormat1))
69
 
        bzrdir.BzrDirFormat._set_default_format(SampleBzrDirFormat())
 
45
        bzrdir.BzrDirFormat.set_default_format(SampleBzrDirFormat())
70
46
        # creating a bzr dir should now create an instrumented dir.
71
47
        try:
72
48
            result = bzrdir.BzrDir.create('memory:///')
73
49
            self.failUnless(isinstance(result, SampleBzrDir))
74
50
        finally:
75
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
51
            bzrdir.BzrDirFormat.set_default_format(old_format)
76
52
        self.assertEqual(old_format, bzrdir.BzrDirFormat.get_default_format())
77
53
 
78
54
 
79
 
class TestFormatRegistry(TestCase):
80
 
 
81
 
    def make_format_registry(self):
82
 
        my_format_registry = bzrdir.BzrDirFormatRegistry()
83
 
        my_format_registry.register('weave', bzrdir.BzrDirFormat6,
84
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
85
 
            ' repositories', deprecated=True)
86
 
        my_format_registry.register_lazy('lazy', 'bzrlib.bzrdir', 
87
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True)
88
 
        my_format_registry.register_metadir('knit',
89
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
90
 
            'Format using knits',
91
 
            )
92
 
        my_format_registry.set_default('knit')
93
 
        my_format_registry.register_metadir(
94
 
            'branch6',
95
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
96
 
            'Experimental successor to knit.  Use at your own risk.',
97
 
            branch_format='bzrlib.branch.BzrBranchFormat6',
98
 
            experimental=True)
99
 
        my_format_registry.register_metadir(
100
 
            'hidden format',
101
 
            'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
102
 
            'Experimental successor to knit.  Use at your own risk.',
103
 
            branch_format='bzrlib.branch.BzrBranchFormat6', hidden=True)
104
 
        my_format_registry.register('hiddenweave', bzrdir.BzrDirFormat6,
105
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
106
 
            ' repositories', hidden=True)
107
 
        my_format_registry.register_lazy('hiddenlazy', 'bzrlib.bzrdir',
108
 
            'BzrDirFormat6', 'Format registered lazily', deprecated=True,
109
 
            hidden=True)
110
 
        return my_format_registry
111
 
 
112
 
    def test_format_registry(self):
113
 
        my_format_registry = self.make_format_registry()
114
 
        my_bzrdir = my_format_registry.make_bzrdir('lazy')
115
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
116
 
        my_bzrdir = my_format_registry.make_bzrdir('weave')
117
 
        self.assertIsInstance(my_bzrdir, bzrdir.BzrDirFormat6)
118
 
        my_bzrdir = my_format_registry.make_bzrdir('default')
119
 
        self.assertIsInstance(my_bzrdir.repository_format, 
120
 
            knitrepo.RepositoryFormatKnit1)
121
 
        my_bzrdir = my_format_registry.make_bzrdir('knit')
122
 
        self.assertIsInstance(my_bzrdir.repository_format, 
123
 
            knitrepo.RepositoryFormatKnit1)
124
 
        my_bzrdir = my_format_registry.make_bzrdir('branch6')
125
 
        self.assertIsInstance(my_bzrdir.get_branch_format(),
126
 
                              bzrlib.branch.BzrBranchFormat6)
127
 
 
128
 
    def test_get_help(self):
129
 
        my_format_registry = self.make_format_registry()
130
 
        self.assertEqual('Format registered lazily',
131
 
                         my_format_registry.get_help('lazy'))
132
 
        self.assertEqual('Format using knits', 
133
 
                         my_format_registry.get_help('knit'))
134
 
        self.assertEqual('Format using knits', 
135
 
                         my_format_registry.get_help('default'))
136
 
        self.assertEqual('Pre-0.8 format.  Slower and does not support'
137
 
                         ' checkouts or shared repositories', 
138
 
                         my_format_registry.get_help('weave'))
139
 
        
140
 
    def test_help_topic(self):
141
 
        topics = help_topics.HelpTopicRegistry()
142
 
        topics.register('formats', self.make_format_registry().help_topic, 
143
 
                        'Directory formats')
144
 
        topic = topics.get_detail('formats')
145
 
        new, rest = topic.split('Experimental formats')
146
 
        experimental, deprecated = rest.split('Deprecated formats')
147
 
        self.assertContainsRe(new, 'These formats can be used')
148
 
        self.assertContainsRe(new, 
149
 
                ':knit:\n    \(native\) \(default\) Format using knits\n')
150
 
        self.assertContainsRe(experimental, 
151
 
                ':branch6:\n    \(native\) Experimental successor to knit')
152
 
        self.assertContainsRe(deprecated, 
153
 
                ':lazy:\n    \(native\) Format registered lazily\n')
154
 
        self.assertNotContainsRe(new, 'hidden')
155
 
 
156
 
    def test_set_default_repository(self):
157
 
        default_factory = bzrdir.format_registry.get('default')
158
 
        old_default = [k for k, v in bzrdir.format_registry.iteritems()
159
 
                       if v == default_factory and k != 'default'][0]
160
 
        bzrdir.format_registry.set_default_repository('dirstate-with-subtree')
161
 
        try:
162
 
            self.assertIs(bzrdir.format_registry.get('dirstate-with-subtree'),
163
 
                          bzrdir.format_registry.get('default'))
164
 
            self.assertIs(
165
 
                repository.RepositoryFormat.get_default_format().__class__,
166
 
                knitrepo.RepositoryFormatKnit3)
167
 
        finally:
168
 
            bzrdir.format_registry.set_default_repository(old_default)
169
 
 
170
 
    def test_aliases(self):
171
 
        a_registry = bzrdir.BzrDirFormatRegistry()
172
 
        a_registry.register('weave', bzrdir.BzrDirFormat6,
173
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
174
 
            ' repositories', deprecated=True)
175
 
        a_registry.register('weavealias', bzrdir.BzrDirFormat6,
176
 
            'Pre-0.8 format.  Slower and does not support checkouts or shared'
177
 
            ' repositories', deprecated=True, alias=True)
178
 
        self.assertEqual(frozenset(['weavealias']), a_registry.aliases())
179
 
    
180
 
 
181
55
class SampleBranch(bzrlib.branch.Branch):
182
56
    """A dummy branch for guess what, dummy use."""
183
57
 
188
62
class SampleBzrDir(bzrdir.BzrDir):
189
63
    """A sample BzrDir implementation to allow testing static methods."""
190
64
 
191
 
    def create_repository(self, shared=False):
 
65
    def create_repository(self):
192
66
        """See BzrDir.create_repository."""
193
67
        return "A repository"
194
68
 
216
90
        """See BzrDirFormat.get_format_string()."""
217
91
        return "Sample .bzr dir format."
218
92
 
219
 
    def initialize_on_transport(self, t):
 
93
    def initialize(self, url):
220
94
        """Create a bzr dir."""
 
95
        t = get_transport(url)
221
96
        t.mkdir('.bzr')
222
 
        t.put_bytes('.bzr/branch-format', self.get_format_string())
 
97
        t.put('.bzr/branch-format', StringIO(self.get_format_string()))
223
98
        return SampleBzrDir(t, self)
224
99
 
225
100
    def is_supported(self):
254
129
    def test_find_format_unknown_format(self):
255
130
        t = get_transport(self.get_url())
256
131
        t.mkdir('.bzr')
257
 
        t.put_bytes('.bzr/branch-format', '')
 
132
        t.put('.bzr/branch-format', StringIO())
258
133
        self.assertRaises(UnknownFormatError,
259
134
                          bzrdir.BzrDirFormat.find_format,
260
135
                          get_transport('.'))
278
153
        # now open_downlevel should fail too.
279
154
        self.assertRaises(UnknownFormatError, bzrdir.BzrDir.open_unsupported, url)
280
155
 
 
156
    def test_create_repository(self):
 
157
        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)
 
165
 
 
166
    def test_create_repository_under_shared(self):
 
167
        # an explicit create_repository always does so.
 
168
        # we trust the format is right from the 'create_repository test'
 
169
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
170
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
171
        try:
 
172
            self.make_repository('.', shared=True)
 
173
            repo = bzrdir.BzrDir.create_repository(self.get_url('child'))
 
174
            self.assertTrue(isinstance(repo, repository.Repository))
 
175
            self.assertTrue(repo.bzrdir.root_transport.base.endswith('child/'))
 
176
        finally:
 
177
            bzrdir.BzrDirFormat.set_default_format(old_format)
 
178
 
281
179
    def test_create_branch_and_repo_uses_default(self):
282
180
        format = SampleBzrDirFormat()
283
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url(),
284
 
                                                      format=format)
285
 
        self.assertTrue(isinstance(branch, SampleBranch))
 
181
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
182
        bzrdir.BzrDirFormat.set_default_format(format)
 
183
        try:
 
184
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
 
185
            self.assertTrue(isinstance(branch, SampleBranch))
 
186
        finally:
 
187
            bzrdir.BzrDirFormat.set_default_format(old_format)
286
188
 
287
189
    def test_create_branch_and_repo_under_shared(self):
288
190
        # creating a branch and repo in a shared repo uses the
289
191
        # shared repository
290
 
        format = bzrdir.format_registry.make_bzrdir('knit')
291
 
        self.make_repository('.', shared=True, format=format)
292
 
        branch = bzrdir.BzrDir.create_branch_and_repo(
293
 
            self.get_url('child'), format=format)
294
 
        self.assertRaises(errors.NoRepositoryPresent,
295
 
                          branch.bzrdir.open_repository)
 
192
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
193
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
194
        try:
 
195
            self.make_repository('.', shared=True)
 
196
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'))
 
197
            self.assertRaises(errors.NoRepositoryPresent,
 
198
                              branch.bzrdir.open_repository)
 
199
        finally:
 
200
            bzrdir.BzrDirFormat.set_default_format(old_format)
296
201
 
297
202
    def test_create_branch_and_repo_under_shared_force_new(self):
298
203
        # creating a branch and repo in a shared repo can be forced to 
299
204
        # make a new repo
300
 
        format = bzrdir.format_registry.make_bzrdir('knit')
301
 
        self.make_repository('.', shared=True, format=format)
302
 
        branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
303
 
                                                      force_new_repo=True,
304
 
                                                      format=format)
305
 
        branch.bzrdir.open_repository()
 
205
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
206
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
207
        try:
 
208
            self.make_repository('.', shared=True)
 
209
            branch = bzrdir.BzrDir.create_branch_and_repo(self.get_url('child'),
 
210
                                                          force_new_repo=True)
 
211
            branch.bzrdir.open_repository()
 
212
        finally:
 
213
            bzrdir.BzrDirFormat.set_default_format(old_format)
306
214
 
307
215
    def test_create_standalone_working_tree(self):
308
216
        format = SampleBzrDirFormat()
309
 
        # note this is deliberately readonly, as this failure should 
310
 
        # occur before any writes.
311
 
        self.assertRaises(errors.NotLocalUrl,
312
 
                          bzrdir.BzrDir.create_standalone_workingtree,
313
 
                          self.get_readonly_url(), format=format)
314
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('.', 
315
 
                                                           format=format)
316
 
        self.assertEqual('A tree', tree)
 
217
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
218
        bzrdir.BzrDirFormat.set_default_format(format)
 
219
        try:
 
220
            # note this is deliberately readonly, as this failure should 
 
221
            # occur before any writes.
 
222
            self.assertRaises(errors.NotLocalUrl,
 
223
                              bzrdir.BzrDir.create_standalone_workingtree,
 
224
                              self.get_readonly_url())
 
225
            tree = bzrdir.BzrDir.create_standalone_workingtree('.')
 
226
            self.assertEqual('A tree', tree)
 
227
        finally:
 
228
            bzrdir.BzrDirFormat.set_default_format(old_format)
317
229
 
318
230
    def test_create_standalone_working_tree_under_shared_repo(self):
319
231
        # create standalone working tree always makes a repo.
320
 
        format = bzrdir.format_registry.make_bzrdir('knit')
321
 
        self.make_repository('.', shared=True, format=format)
322
 
        # note this is deliberately readonly, as this failure should 
323
 
        # occur before any writes.
324
 
        self.assertRaises(errors.NotLocalUrl,
325
 
                          bzrdir.BzrDir.create_standalone_workingtree,
326
 
                          self.get_readonly_url('child'), format=format)
327
 
        tree = bzrdir.BzrDir.create_standalone_workingtree('child', 
328
 
            format=format)
329
 
        tree.bzrdir.open_repository()
 
232
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
233
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
234
        try:
 
235
            self.make_repository('.', shared=True)
 
236
            # note this is deliberately readonly, as this failure should 
 
237
            # occur before any writes.
 
238
            self.assertRaises(errors.NotLocalUrl,
 
239
                              bzrdir.BzrDir.create_standalone_workingtree,
 
240
                              self.get_readonly_url('child'))
 
241
            tree = bzrdir.BzrDir.create_standalone_workingtree('child')
 
242
            tree.bzrdir.open_repository()
 
243
        finally:
 
244
            bzrdir.BzrDirFormat.set_default_format(old_format)
330
245
 
331
246
    def test_create_branch_convenience(self):
332
247
        # outside a repo the default convenience output is a repo+branch_tree
333
 
        format = bzrdir.format_registry.make_bzrdir('knit')
334
 
        branch = bzrdir.BzrDir.create_branch_convenience('.', format=format)
335
 
        branch.bzrdir.open_workingtree()
336
 
        branch.bzrdir.open_repository()
337
 
 
338
 
    def test_create_branch_convenience_possible_transports(self):
339
 
        """Check that the optional 'possible_transports' is recognized"""
340
 
        format = bzrdir.format_registry.make_bzrdir('knit')
341
 
        t = self.get_transport()
342
 
        branch = bzrdir.BzrDir.create_branch_convenience(
343
 
            '.', format=format, possible_transports=[t])
344
 
        branch.bzrdir.open_workingtree()
345
 
        branch.bzrdir.open_repository()
 
248
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
249
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
250
        try:
 
251
            branch = bzrdir.BzrDir.create_branch_convenience('.')
 
252
            branch.bzrdir.open_workingtree()
 
253
            branch.bzrdir.open_repository()
 
254
        finally:
 
255
            bzrdir.BzrDirFormat.set_default_format(old_format)
346
256
 
347
257
    def test_create_branch_convenience_root(self):
348
258
        """Creating a branch at the root of a fs should work."""
349
 
        self.vfs_transport_factory = MemoryServer
 
259
        self.transport_server = MemoryServer
350
260
        # outside a repo the default convenience output is a repo+branch_tree
351
 
        format = bzrdir.format_registry.make_bzrdir('knit')
352
 
        branch = bzrdir.BzrDir.create_branch_convenience(self.get_url(), 
353
 
                                                         format=format)
354
 
        self.assertRaises(errors.NoWorkingTree,
355
 
                          branch.bzrdir.open_workingtree)
356
 
        branch.bzrdir.open_repository()
 
261
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
262
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
263
        try:
 
264
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url())
 
265
            self.assertRaises(errors.NoWorkingTree,
 
266
                              branch.bzrdir.open_workingtree)
 
267
            branch.bzrdir.open_repository()
 
268
        finally:
 
269
            bzrdir.BzrDirFormat.set_default_format(old_format)
357
270
 
358
271
    def test_create_branch_convenience_under_shared_repo(self):
359
272
        # inside a repo the default convenience output is a branch+ follow the
360
273
        # repo tree policy
361
 
        format = bzrdir.format_registry.make_bzrdir('knit')
362
 
        self.make_repository('.', shared=True, format=format)
363
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
364
 
            format=format)
365
 
        branch.bzrdir.open_workingtree()
366
 
        self.assertRaises(errors.NoRepositoryPresent,
367
 
                          branch.bzrdir.open_repository)
 
274
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
275
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
276
        try:
 
277
            self.make_repository('.', shared=True)
 
278
            branch = bzrdir.BzrDir.create_branch_convenience('child')
 
279
            branch.bzrdir.open_workingtree()
 
280
            self.assertRaises(errors.NoRepositoryPresent,
 
281
                              branch.bzrdir.open_repository)
 
282
        finally:
 
283
            bzrdir.BzrDirFormat.set_default_format(old_format)
368
284
            
369
285
    def test_create_branch_convenience_under_shared_repo_force_no_tree(self):
370
286
        # inside a repo the default convenience output is a branch+ follow the
371
287
        # repo tree policy but we can override that
372
 
        format = bzrdir.format_registry.make_bzrdir('knit')
373
 
        self.make_repository('.', shared=True, format=format)
374
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
375
 
            force_new_tree=False, format=format)
376
 
        self.assertRaises(errors.NoWorkingTree,
377
 
                          branch.bzrdir.open_workingtree)
378
 
        self.assertRaises(errors.NoRepositoryPresent,
379
 
                          branch.bzrdir.open_repository)
 
288
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
289
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
290
        try:
 
291
            self.make_repository('.', shared=True)
 
292
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
293
                force_new_tree=False)
 
294
            self.assertRaises(errors.NoWorkingTree,
 
295
                              branch.bzrdir.open_workingtree)
 
296
            self.assertRaises(errors.NoRepositoryPresent,
 
297
                              branch.bzrdir.open_repository)
 
298
        finally:
 
299
            bzrdir.BzrDirFormat.set_default_format(old_format)
380
300
            
381
301
    def test_create_branch_convenience_under_shared_repo_no_tree_policy(self):
382
302
        # inside a repo the default convenience output is a branch+ follow the
383
303
        # repo tree policy
384
 
        format = bzrdir.format_registry.make_bzrdir('knit')
385
 
        repo = self.make_repository('.', shared=True, format=format)
386
 
        repo.set_make_working_trees(False)
387
 
        branch = bzrdir.BzrDir.create_branch_convenience('child', 
388
 
                                                         format=format)
389
 
        self.assertRaises(errors.NoWorkingTree,
390
 
                          branch.bzrdir.open_workingtree)
391
 
        self.assertRaises(errors.NoRepositoryPresent,
392
 
                          branch.bzrdir.open_repository)
 
304
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
305
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
306
        try:
 
307
            repo = self.make_repository('.', shared=True)
 
308
            repo.set_make_working_trees(False)
 
309
            branch = bzrdir.BzrDir.create_branch_convenience('child')
 
310
            self.assertRaises(errors.NoWorkingTree,
 
311
                              branch.bzrdir.open_workingtree)
 
312
            self.assertRaises(errors.NoRepositoryPresent,
 
313
                              branch.bzrdir.open_repository)
 
314
        finally:
 
315
            bzrdir.BzrDirFormat.set_default_format(old_format)
393
316
 
394
317
    def test_create_branch_convenience_under_shared_repo_no_tree_policy_force_tree(self):
395
318
        # inside a repo the default convenience output is a branch+ follow the
396
319
        # repo tree policy but we can override that
397
 
        format = bzrdir.format_registry.make_bzrdir('knit')
398
 
        repo = self.make_repository('.', shared=True, format=format)
399
 
        repo.set_make_working_trees(False)
400
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
401
 
            force_new_tree=True, format=format)
402
 
        branch.bzrdir.open_workingtree()
403
 
        self.assertRaises(errors.NoRepositoryPresent,
404
 
                          branch.bzrdir.open_repository)
 
320
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
321
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
322
        try:
 
323
            repo = self.make_repository('.', shared=True)
 
324
            repo.set_make_working_trees(False)
 
325
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
326
                force_new_tree=True)
 
327
            branch.bzrdir.open_workingtree()
 
328
            self.assertRaises(errors.NoRepositoryPresent,
 
329
                              branch.bzrdir.open_repository)
 
330
        finally:
 
331
            bzrdir.BzrDirFormat.set_default_format(old_format)
405
332
 
406
333
    def test_create_branch_convenience_under_shared_repo_force_new_repo(self):
407
334
        # inside a repo the default convenience output is overridable to give
408
335
        # repo+branch+tree
409
 
        format = bzrdir.format_registry.make_bzrdir('knit')
410
 
        self.make_repository('.', shared=True, format=format)
411
 
        branch = bzrdir.BzrDir.create_branch_convenience('child',
412
 
            force_new_repo=True, format=format)
413
 
        branch.bzrdir.open_repository()
414
 
        branch.bzrdir.open_workingtree()
415
 
 
416
 
 
417
 
class TestRepositoryAcquisitionPolicy(TestCaseWithTransport):
418
 
 
419
 
    def test_acquire_repository_standalone(self):
420
 
        """The default acquisition policy should create a standalone branch."""
421
 
        my_bzrdir = self.make_bzrdir('.')
422
 
        repo_policy = my_bzrdir.determine_repository_policy()
423
 
        repo = repo_policy.acquire_repository()
424
 
        self.assertEqual(repo.bzrdir.root_transport.base,
425
 
                         my_bzrdir.root_transport.base)
426
 
        self.assertFalse(repo.is_shared())
427
 
 
428
 
 
429
 
    def test_determine_stacking_policy(self):
430
 
        parent_bzrdir = self.make_bzrdir('.')
431
 
        child_bzrdir = self.make_bzrdir('child')
432
 
        parent_bzrdir.get_config().set_default_stack_on('http://example.org')
433
 
        repo_policy = child_bzrdir.determine_repository_policy()
434
 
        self.assertEqual('http://example.org', repo_policy._stack_on)
435
 
 
436
 
    def test_determine_stacking_policy_relative(self):
437
 
        parent_bzrdir = self.make_bzrdir('.')
438
 
        child_bzrdir = self.make_bzrdir('child')
439
 
        parent_bzrdir.get_config().set_default_stack_on('child2')
440
 
        repo_policy = child_bzrdir.determine_repository_policy()
441
 
        self.assertEqual('child2', repo_policy._stack_on)
442
 
        self.assertEqual(parent_bzrdir.root_transport.base,
443
 
                         repo_policy._stack_on_pwd)
444
 
 
445
 
    def prepare_default_stacking(self, child_format='1.6'):
446
 
        parent_bzrdir = self.make_bzrdir('.')
447
 
        child_branch = self.make_branch('child', format=child_format)
448
 
        parent_bzrdir.get_config().set_default_stack_on(child_branch.base)
449
 
        new_child_transport = parent_bzrdir.transport.clone('child2')
450
 
        return child_branch, new_child_transport
451
 
 
452
 
    def test_clone_on_transport_obeys_stacking_policy(self):
453
 
        child_branch, new_child_transport = self.prepare_default_stacking()
454
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
455
 
        self.assertEqual(child_branch.base,
456
 
                         new_child.open_branch().get_stacked_on_url())
457
 
 
458
 
    def test_sprout_obeys_stacking_policy(self):
459
 
        child_branch, new_child_transport = self.prepare_default_stacking()
460
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
461
 
        self.assertEqual(child_branch.base,
462
 
                         new_child.open_branch().get_stacked_on_url())
463
 
 
464
 
    def test_clone_ignores_policy_for_unsupported_formats(self):
465
 
        child_branch, new_child_transport = self.prepare_default_stacking(
466
 
            child_format='pack-0.92')
467
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport)
468
 
        self.assertRaises(errors.UnstackableBranchFormat,
469
 
                          new_child.open_branch().get_stacked_on_url)
470
 
 
471
 
    def test_sprout_ignores_policy_for_unsupported_formats(self):
472
 
        child_branch, new_child_transport = self.prepare_default_stacking(
473
 
            child_format='pack-0.92')
474
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base)
475
 
        self.assertRaises(errors.UnstackableBranchFormat,
476
 
                          new_child.open_branch().get_stacked_on_url)
477
 
 
478
 
    def test_sprout_upgrades_format_if_stacked_specified(self):
479
 
        child_branch, new_child_transport = self.prepare_default_stacking(
480
 
            child_format='pack-0.92')
481
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
482
 
                                               stacked=True)
483
 
        self.assertEqual(child_branch.bzrdir.root_transport.base,
484
 
                         new_child.open_branch().get_stacked_on_url())
485
 
        repo = new_child.open_repository()
486
 
        self.assertTrue(repo._format.supports_external_lookups)
487
 
        self.assertFalse(repo.supports_rich_root())
488
 
 
489
 
    def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
490
 
        child_branch, new_child_transport = self.prepare_default_stacking(
491
 
            child_format='pack-0.92')
492
 
        new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
493
 
            stacked_on=child_branch.bzrdir.root_transport.base)
494
 
        self.assertEqual(child_branch.bzrdir.root_transport.base,
495
 
                         new_child.open_branch().get_stacked_on_url())
496
 
        repo = new_child.open_repository()
497
 
        self.assertTrue(repo._format.supports_external_lookups)
498
 
        self.assertFalse(repo.supports_rich_root())
499
 
 
500
 
    def test_sprout_upgrades_to_rich_root_format_if_needed(self):
501
 
        child_branch, new_child_transport = self.prepare_default_stacking(
502
 
            child_format='rich-root-pack')
503
 
        new_child = child_branch.bzrdir.sprout(new_child_transport.base,
504
 
                                               stacked=True)
505
 
        repo = new_child.open_repository()
506
 
        self.assertTrue(repo._format.supports_external_lookups)
507
 
        self.assertTrue(repo.supports_rich_root())
508
 
 
509
 
    def test_add_fallback_repo_handles_absolute_urls(self):
510
 
        stack_on = self.make_branch('stack_on', format='1.6')
511
 
        repo = self.make_repository('repo', format='1.6')
512
 
        policy = bzrdir.UseExistingRepository(repo, stack_on.base)
513
 
        policy._add_fallback(repo)
514
 
 
515
 
    def test_add_fallback_repo_handles_relative_urls(self):
516
 
        stack_on = self.make_branch('stack_on', format='1.6')
517
 
        repo = self.make_repository('repo', format='1.6')
518
 
        policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
519
 
        policy._add_fallback(repo)
520
 
 
521
 
    def test_configure_relative_branch_stacking_url(self):
522
 
        stack_on = self.make_branch('stack_on', format='1.6')
523
 
        stacked = self.make_branch('stack_on/stacked', format='1.6')
524
 
        policy = bzrdir.UseExistingRepository(stacked.repository,
525
 
            '.', stack_on.base)
526
 
        policy.configure_branch(stacked)
527
 
        self.assertEqual('..', stacked.get_stacked_on_url())
528
 
 
529
 
    def test_relative_branch_stacking_to_absolute(self):
530
 
        stack_on = self.make_branch('stack_on', format='1.6')
531
 
        stacked = self.make_branch('stack_on/stacked', format='1.6')
532
 
        policy = bzrdir.UseExistingRepository(stacked.repository,
533
 
            '.', self.get_readonly_url('stack_on'))
534
 
        policy.configure_branch(stacked)
535
 
        self.assertEqual(self.get_readonly_url('stack_on'),
536
 
                         stacked.get_stacked_on_url())
 
336
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
337
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
338
        try:
 
339
            self.make_repository('.', shared=True)
 
340
            branch = bzrdir.BzrDir.create_branch_convenience('child',
 
341
                force_new_repo=True)
 
342
            branch.bzrdir.open_repository()
 
343
            branch.bzrdir.open_workingtree()
 
344
        finally:
 
345
            bzrdir.BzrDirFormat.set_default_format(old_format)
537
346
 
538
347
 
539
348
class ChrootedTests(TestCaseWithTransport):
546
355
 
547
356
    def setUp(self):
548
357
        super(ChrootedTests, self).setUp()
549
 
        if not self.vfs_transport_factory == MemoryServer:
 
358
        if not self.transport_server == MemoryServer:
550
359
            self.transport_readonly_server = HttpServer
551
360
 
552
 
    def local_branch_path(self, branch):
553
 
         return os.path.realpath(urlutils.local_path_from_url(branch.base))
554
 
 
555
361
    def test_open_containing(self):
556
362
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing,
557
363
                          self.get_readonly_url(''))
563
369
        branch, relpath = bzrdir.BzrDir.open_containing(self.get_readonly_url('g/p/q'))
564
370
        self.assertEqual('g/p/q', relpath)
565
371
 
566
 
    def test_open_containing_tree_branch_or_repository_empty(self):
567
 
        self.assertRaises(errors.NotBranchError,
568
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository,
569
 
            self.get_readonly_url(''))
570
 
 
571
 
    def test_open_containing_tree_branch_or_repository_all(self):
572
 
        self.make_branch_and_tree('topdir')
573
 
        tree, branch, repo, relpath = \
574
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
575
 
                'topdir/foo')
576
 
        self.assertEqual(os.path.realpath('topdir'),
577
 
                         os.path.realpath(tree.basedir))
578
 
        self.assertEqual(os.path.realpath('topdir'),
579
 
                         self.local_branch_path(branch))
580
 
        self.assertEqual(
581
 
            osutils.realpath(os.path.join('topdir', '.bzr', 'repository')),
582
 
            repo.bzrdir.transport.local_abspath('repository'))
583
 
        self.assertEqual(relpath, 'foo')
584
 
 
585
 
    def test_open_containing_tree_branch_or_repository_no_tree(self):
586
 
        self.make_branch('branch')
587
 
        tree, branch, repo, relpath = \
588
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
589
 
                'branch/foo')
590
 
        self.assertEqual(tree, None)
591
 
        self.assertEqual(os.path.realpath('branch'),
592
 
                         self.local_branch_path(branch))
593
 
        self.assertEqual(
594
 
            osutils.realpath(os.path.join('branch', '.bzr', 'repository')),
595
 
            repo.bzrdir.transport.local_abspath('repository'))
596
 
        self.assertEqual(relpath, 'foo')
597
 
 
598
 
    def test_open_containing_tree_branch_or_repository_repo(self):
599
 
        self.make_repository('repo')
600
 
        tree, branch, repo, relpath = \
601
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
602
 
                'repo')
603
 
        self.assertEqual(tree, None)
604
 
        self.assertEqual(branch, None)
605
 
        self.assertEqual(
606
 
            osutils.realpath(os.path.join('repo', '.bzr', 'repository')),
607
 
            repo.bzrdir.transport.local_abspath('repository'))
608
 
        self.assertEqual(relpath, '')
609
 
 
610
 
    def test_open_containing_tree_branch_or_repository_shared_repo(self):
611
 
        self.make_repository('shared', shared=True)
612
 
        bzrdir.BzrDir.create_branch_convenience('shared/branch',
613
 
                                                force_new_tree=False)
614
 
        tree, branch, repo, relpath = \
615
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
616
 
                'shared/branch')
617
 
        self.assertEqual(tree, None)
618
 
        self.assertEqual(os.path.realpath('shared/branch'),
619
 
                         self.local_branch_path(branch))
620
 
        self.assertEqual(
621
 
            osutils.realpath(os.path.join('shared', '.bzr', 'repository')),
622
 
            repo.bzrdir.transport.local_abspath('repository'))
623
 
        self.assertEqual(relpath, '')
624
 
 
625
 
    def test_open_containing_tree_branch_or_repository_branch_subdir(self):
626
 
        self.make_branch_and_tree('foo')
627
 
        self.build_tree(['foo/bar/'])
628
 
        tree, branch, repo, relpath = \
629
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
630
 
                'foo/bar')
631
 
        self.assertEqual(os.path.realpath('foo'),
632
 
                         os.path.realpath(tree.basedir))
633
 
        self.assertEqual(os.path.realpath('foo'),
634
 
                         self.local_branch_path(branch))
635
 
        self.assertEqual(
636
 
            osutils.realpath(os.path.join('foo', '.bzr', 'repository')),
637
 
            repo.bzrdir.transport.local_abspath('repository'))
638
 
        self.assertEqual(relpath, 'bar')
639
 
 
640
 
    def test_open_containing_tree_branch_or_repository_repo_subdir(self):
641
 
        self.make_repository('bar')
642
 
        self.build_tree(['bar/baz/'])
643
 
        tree, branch, repo, relpath = \
644
 
            bzrdir.BzrDir.open_containing_tree_branch_or_repository(
645
 
                'bar/baz')
646
 
        self.assertEqual(tree, None)
647
 
        self.assertEqual(branch, None)
648
 
        self.assertEqual(
649
 
            osutils.realpath(os.path.join('bar', '.bzr', 'repository')),
650
 
            repo.bzrdir.transport.local_abspath('repository'))
651
 
        self.assertEqual(relpath, 'baz')
652
 
 
653
372
    def test_open_containing_from_transport(self):
654
373
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_containing_from_transport,
655
374
                          get_transport(self.get_readonly_url('')))
663
382
            get_transport(self.get_readonly_url('g/p/q')))
664
383
        self.assertEqual('g/p/q', relpath)
665
384
 
666
 
    def test_open_containing_tree_or_branch(self):
667
 
        self.make_branch_and_tree('topdir')
668
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
669
 
            'topdir/foo')
670
 
        self.assertEqual(os.path.realpath('topdir'),
671
 
                         os.path.realpath(tree.basedir))
672
 
        self.assertEqual(os.path.realpath('topdir'),
673
 
                         self.local_branch_path(branch))
674
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
675
 
        self.assertEqual('foo', relpath)
676
 
        # opening from non-local should not return the tree
677
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
678
 
            self.get_readonly_url('topdir/foo'))
679
 
        self.assertEqual(None, tree)
680
 
        self.assertEqual('foo', relpath)
681
 
        # without a tree:
682
 
        self.make_branch('topdir/foo')
683
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
684
 
            'topdir/foo')
685
 
        self.assertIs(tree, None)
686
 
        self.assertEqual(os.path.realpath('topdir/foo'),
687
 
                         self.local_branch_path(branch))
688
 
        self.assertEqual('', relpath)
689
 
 
690
 
    def test_open_tree_or_branch(self):
691
 
        self.make_branch_and_tree('topdir')
692
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir')
693
 
        self.assertEqual(os.path.realpath('topdir'),
694
 
                         os.path.realpath(tree.basedir))
695
 
        self.assertEqual(os.path.realpath('topdir'),
696
 
                         self.local_branch_path(branch))
697
 
        self.assertIs(tree.bzrdir, branch.bzrdir)
698
 
        # opening from non-local should not return the tree
699
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch(
700
 
            self.get_readonly_url('topdir'))
701
 
        self.assertEqual(None, tree)
702
 
        # without a tree:
703
 
        self.make_branch('topdir/foo')
704
 
        tree, branch = bzrdir.BzrDir.open_tree_or_branch('topdir/foo')
705
 
        self.assertIs(tree, None)
706
 
        self.assertEqual(os.path.realpath('topdir/foo'),
707
 
                         self.local_branch_path(branch))
708
 
 
709
 
    def test_open_from_transport(self):
710
 
        # transport pointing at bzrdir should give a bzrdir with root transport
711
 
        # set to the given transport
712
 
        control = bzrdir.BzrDir.create(self.get_url())
713
 
        transport = get_transport(self.get_url())
714
 
        opened_bzrdir = bzrdir.BzrDir.open_from_transport(transport)
715
 
        self.assertEqual(transport.base, opened_bzrdir.root_transport.base)
716
 
        self.assertIsInstance(opened_bzrdir, bzrdir.BzrDir)
717
 
        
718
 
    def test_open_from_transport_no_bzrdir(self):
719
 
        transport = get_transport(self.get_url())
720
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
721
 
                          transport)
722
 
 
723
 
    def test_open_from_transport_bzrdir_in_parent(self):
724
 
        control = bzrdir.BzrDir.create(self.get_url())
725
 
        transport = get_transport(self.get_url())
726
 
        transport.mkdir('subdir')
727
 
        transport = transport.clone('subdir')
728
 
        self.assertRaises(NotBranchError, bzrdir.BzrDir.open_from_transport,
729
 
                          transport)
730
 
 
731
 
    def test_sprout_recursive(self):
732
 
        tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
733
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
734
 
            format='dirstate-with-subtree')
735
 
        tree.add_reference(sub_tree)
736
 
        self.build_tree(['tree1/subtree/file'])
737
 
        sub_tree.add('file')
738
 
        tree.commit('Initial commit')
739
 
        tree.bzrdir.sprout('tree2')
740
 
        self.failUnlessExists('tree2/subtree/file')
741
 
 
742
 
    def test_cloning_metadir(self):
743
 
        """Ensure that cloning metadir is suitable"""
744
 
        bzrdir = self.make_bzrdir('bzrdir')
745
 
        bzrdir.cloning_metadir()
746
 
        branch = self.make_branch('branch', format='knit')
747
 
        format = branch.bzrdir.cloning_metadir()
748
 
        self.assertIsInstance(format.workingtree_format,
749
 
            workingtree.WorkingTreeFormat3)
750
 
 
751
 
    def test_sprout_recursive_treeless(self):
752
 
        tree = self.make_branch_and_tree('tree1',
753
 
            format='dirstate-with-subtree')
754
 
        sub_tree = self.make_branch_and_tree('tree1/subtree',
755
 
            format='dirstate-with-subtree')
756
 
        tree.add_reference(sub_tree)
757
 
        self.build_tree(['tree1/subtree/file'])
758
 
        sub_tree.add('file')
759
 
        tree.commit('Initial commit')
760
 
        tree.bzrdir.destroy_workingtree()
761
 
        repo = self.make_repository('repo', shared=True,
762
 
            format='dirstate-with-subtree')
763
 
        repo.set_make_working_trees(False)
764
 
        tree.bzrdir.sprout('repo/tree2')
765
 
        self.failUnlessExists('repo/tree2/subtree')
766
 
        self.failIfExists('repo/tree2/subtree/file')
767
 
 
768
 
    def make_foo_bar_baz(self):
769
 
        foo = bzrdir.BzrDir.create_branch_convenience('foo').bzrdir
770
 
        bar = self.make_branch('foo/bar').bzrdir
771
 
        baz = self.make_branch('baz').bzrdir
772
 
        return foo, bar, baz
773
 
 
774
 
    def test_find_bzrdirs(self):
775
 
        foo, bar, baz = self.make_foo_bar_baz()
776
 
        transport = get_transport(self.get_url())
777
 
        self.assertEqualBzrdirs([baz, foo, bar],
778
 
                                bzrdir.BzrDir.find_bzrdirs(transport))
779
 
 
780
 
    def test_find_bzrdirs_list_current(self):
781
 
        def list_current(transport):
782
 
            return [s for s in transport.list_dir('') if s != 'baz']
783
 
 
784
 
        foo, bar, baz = self.make_foo_bar_baz()
785
 
        transport = get_transport(self.get_url())
786
 
        self.assertEqualBzrdirs([foo, bar],
787
 
                                bzrdir.BzrDir.find_bzrdirs(transport,
788
 
                                    list_current=list_current))
789
 
 
790
 
 
791
 
    def test_find_bzrdirs_evaluate(self):
792
 
        def evaluate(bzrdir):
793
 
            try:
794
 
                repo = bzrdir.open_repository()
795
 
            except NoRepositoryPresent:
796
 
                return True, bzrdir.root_transport.base
797
 
            else:
798
 
                return False, bzrdir.root_transport.base
799
 
 
800
 
        foo, bar, baz = self.make_foo_bar_baz()
801
 
        transport = get_transport(self.get_url())
802
 
        self.assertEqual([baz.root_transport.base, foo.root_transport.base],
803
 
                         list(bzrdir.BzrDir.find_bzrdirs(transport,
804
 
                                                         evaluate=evaluate)))
805
 
 
806
 
    def assertEqualBzrdirs(self, first, second):
807
 
        first = list(first)
808
 
        second = list(second)
809
 
        self.assertEqual(len(first), len(second))
810
 
        for x, y in zip(first, second):
811
 
            self.assertEqual(x.root_transport.base, y.root_transport.base)
812
 
 
813
 
    def test_find_branches(self):
814
 
        root = self.make_repository('', shared=True)
815
 
        foo, bar, baz = self.make_foo_bar_baz()
816
 
        qux = self.make_bzrdir('foo/qux')
817
 
        transport = get_transport(self.get_url())
818
 
        branches = bzrdir.BzrDir.find_branches(transport)
819
 
        self.assertEqual(baz.root_transport.base, branches[0].base)
820
 
        self.assertEqual(foo.root_transport.base, branches[1].base)
821
 
        self.assertEqual(bar.root_transport.base, branches[2].base)
822
 
 
823
 
        # ensure this works without a top-level repo
824
 
        branches = bzrdir.BzrDir.find_branches(transport.clone('foo'))
825
 
        self.assertEqual(foo.root_transport.base, branches[0].base)
826
 
        self.assertEqual(bar.root_transport.base, branches[1].base)
827
 
 
828
385
 
829
386
class TestMeta1DirFormat(TestCaseWithTransport):
830
387
    """Tests specific to the meta1 dir format."""
839
396
        repository_base = t.clone('repository').base
840
397
        self.assertEqual(repository_base, dir.get_repository_transport(None).base)
841
398
        self.assertEqual(repository_base,
842
 
                         dir.get_repository_transport(weaverepo.RepositoryFormat7()).base)
 
399
                         dir.get_repository_transport(repository.RepositoryFormat7()).base)
843
400
        checkout_base = t.clone('checkout').base
844
401
        self.assertEqual(checkout_base, dir.get_workingtree_transport(None).base)
845
402
        self.assertEqual(checkout_base,
851
408
        t = dir.transport
852
409
        self.assertIsDirectory('branch-lock', t)
853
410
 
854
 
    def test_comparison(self):
855
 
        """Equality and inequality behave properly.
856
 
 
857
 
        Metadirs should compare equal iff they have the same repo, branch and
858
 
        tree formats.
859
 
        """
860
 
        mydir = bzrdir.format_registry.make_bzrdir('knit')
861
 
        self.assertEqual(mydir, mydir)
862
 
        self.assertFalse(mydir != mydir)
863
 
        otherdir = bzrdir.format_registry.make_bzrdir('knit')
864
 
        self.assertEqual(otherdir, mydir)
865
 
        self.assertFalse(otherdir != mydir)
866
 
        otherdir2 = bzrdir.format_registry.make_bzrdir('dirstate-with-subtree')
867
 
        self.assertNotEqual(otherdir2, mydir)
868
 
        self.assertFalse(otherdir2 == mydir)
869
 
 
870
 
    def test_needs_conversion_different_working_tree(self):
871
 
        # meta1dirs need an conversion if any element is not the default.
872
 
        old_format = bzrdir.BzrDirFormat.get_default_format()
873
 
        # test with 
874
 
        new_default = bzrdir.format_registry.make_bzrdir('dirstate')
875
 
        bzrdir.BzrDirFormat._set_default_format(new_default)
876
 
        try:
877
 
            tree = self.make_branch_and_tree('tree', format='knit')
878
 
            self.assertTrue(tree.bzrdir.needs_format_conversion())
879
 
        finally:
880
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
881
 
 
882
 
 
 
411
        
883
412
class TestFormat5(TestCaseWithTransport):
884
413
    """Tests specific to the version 5 bzrdir format."""
885
414
 
907
436
        # format 5 dirs need a conversion if they are not the default.
908
437
        # and they start of not the default.
909
438
        old_format = bzrdir.BzrDirFormat.get_default_format()
910
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
439
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirFormat5())
911
440
        try:
912
441
            dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
913
442
            self.assertFalse(dir.needs_format_conversion())
914
443
        finally:
915
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
444
            bzrdir.BzrDirFormat.set_default_format(old_format)
916
445
        self.assertTrue(dir.needs_format_conversion())
917
446
 
918
447
 
942
471
    def test_needs_conversion(self):
943
472
        # format 6 dirs need an conversion if they are not the default.
944
473
        old_format = bzrdir.BzrDirFormat.get_default_format()
945
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
 
474
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
946
475
        try:
947
476
            dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
948
477
            self.assertTrue(dir.needs_format_conversion())
949
478
        finally:
950
 
            bzrdir.BzrDirFormat._set_default_format(old_format)
 
479
            bzrdir.BzrDirFormat.set_default_format(old_format)
951
480
 
952
481
 
953
482
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1020
549
 
1021
550
    def setUp(self):
1022
551
        super(NonLocalTests, self).setUp()
1023
 
        self.vfs_transport_factory = MemoryServer
 
552
        self.transport_server = MemoryServer
1024
553
    
1025
554
    def test_create_branch_convenience(self):
1026
555
        # outside a repo the default convenience output is a repo+branch_tree
1027
 
        format = bzrdir.format_registry.make_bzrdir('knit')
1028
 
        branch = bzrdir.BzrDir.create_branch_convenience(
1029
 
            self.get_url('foo'), format=format)
1030
 
        self.assertRaises(errors.NoWorkingTree,
1031
 
                          branch.bzrdir.open_workingtree)
1032
 
        branch.bzrdir.open_repository()
 
556
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
557
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
558
        try:
 
559
            branch = bzrdir.BzrDir.create_branch_convenience(self.get_url('foo'))
 
560
            self.assertRaises(errors.NoWorkingTree,
 
561
                              branch.bzrdir.open_workingtree)
 
562
            branch.bzrdir.open_repository()
 
563
        finally:
 
564
            bzrdir.BzrDirFormat.set_default_format(old_format)
1033
565
 
1034
566
    def test_create_branch_convenience_force_tree_not_local_fails(self):
1035
567
        # outside a repo the default convenience output is a repo+branch_tree
1036
 
        format = bzrdir.format_registry.make_bzrdir('knit')
1037
 
        self.assertRaises(errors.NotLocalUrl,
1038
 
            bzrdir.BzrDir.create_branch_convenience,
1039
 
            self.get_url('foo'),
1040
 
            force_new_tree=True,
1041
 
            format=format)
1042
 
        t = get_transport(self.get_url('.'))
1043
 
        self.assertFalse(t.has('foo'))
 
568
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
569
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
570
        try:
 
571
            self.assertRaises(errors.NotLocalUrl,
 
572
                bzrdir.BzrDir.create_branch_convenience,
 
573
                self.get_url('foo'),
 
574
                force_new_tree=True)
 
575
            t = get_transport(self.get_url('.'))
 
576
            self.assertFalse(t.has('foo'))
 
577
        finally:
 
578
            bzrdir.BzrDirFormat.set_default_format(old_format)
1044
579
 
1045
580
    def test_clone(self):
1046
581
        # clone into a nonlocal path works
1047
 
        format = bzrdir.format_registry.make_bzrdir('knit')
1048
 
        branch = bzrdir.BzrDir.create_branch_convenience('local',
1049
 
                                                         format=format)
 
582
        old_format = bzrdir.BzrDirFormat.get_default_format()
 
583
        bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
584
        try:
 
585
            branch = bzrdir.BzrDir.create_branch_convenience('local')
 
586
        finally:
 
587
            bzrdir.BzrDirFormat.set_default_format(old_format)
1050
588
        branch.bzrdir.open_workingtree()
1051
589
        result = branch.bzrdir.clone(self.get_url('remote'))
1052
590
        self.assertRaises(errors.NoWorkingTree,
1054
592
        result.open_branch()
1055
593
        result.open_repository()
1056
594
 
1057
 
    def test_checkout_metadir(self):
1058
 
        # checkout_metadir has reasonable working tree format even when no
1059
 
        # working tree is present
1060
 
        self.make_branch('branch-knit2', format='dirstate-with-subtree')
1061
 
        my_bzrdir = bzrdir.BzrDir.open(self.get_url('branch-knit2'))
1062
 
        checkout_format = my_bzrdir.checkout_metadir()
1063
 
        self.assertIsInstance(checkout_format.workingtree_format,
1064
 
                              workingtree.WorkingTreeFormat3)
1065
 
 
1066
 
 
1067
 
class TestHTTPRedirectionLoop(object):
1068
 
    """Test redirection loop between two http servers.
1069
 
 
1070
 
    This MUST be used by daughter classes that also inherit from
1071
 
    TestCaseWithTwoWebservers.
1072
 
 
1073
 
    We can't inherit directly from TestCaseWithTwoWebservers or the
1074
 
    test framework will try to create an instance which cannot
1075
 
    run, its implementation being incomplete. 
1076
 
    """
1077
 
 
1078
 
    # Should be defined by daughter classes to ensure redirection
1079
 
    # still use the same transport implementation (not currently
1080
 
    # enforced as it's a bit tricky to get right (see the FIXME
1081
 
    # in BzrDir.open_from_transport for the unique use case so
1082
 
    # far)
1083
 
    _qualifier = None
1084
 
 
1085
 
    def create_transport_readonly_server(self):
1086
 
        return HTTPServerRedirecting()
1087
 
 
1088
 
    def create_transport_secondary_server(self):
1089
 
        return HTTPServerRedirecting()
1090
 
 
1091
 
    def setUp(self):
1092
 
        # Both servers redirect to each server creating a loop
1093
 
        super(TestHTTPRedirectionLoop, self).setUp()
1094
 
        # The redirections will point to the new server
1095
 
        self.new_server = self.get_readonly_server()
1096
 
        # The requests to the old server will be redirected
1097
 
        self.old_server = self.get_secondary_server()
1098
 
        # Configure the redirections
1099
 
        self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1100
 
        self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1101
 
 
1102
 
    def _qualified_url(self, host, port):
1103
 
        return 'http+%s://%s:%s' % (self._qualifier, host, port)
1104
 
 
1105
 
    def test_loop(self):
1106
 
        # Starting from either server should loop
1107
 
        old_url = self._qualified_url(self.old_server.host, 
1108
 
                                      self.old_server.port)
1109
 
        oldt = self._transport(old_url)
1110
 
        self.assertRaises(errors.NotBranchError,
1111
 
                          bzrdir.BzrDir.open_from_transport, oldt)
1112
 
        new_url = self._qualified_url(self.new_server.host, 
1113
 
                                      self.new_server.port)
1114
 
        newt = self._transport(new_url)
1115
 
        self.assertRaises(errors.NotBranchError,
1116
 
                          bzrdir.BzrDir.open_from_transport, newt)
1117
 
 
1118
 
 
1119
 
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
1120
 
                                  TestCaseWithTwoWebservers):
1121
 
    """Tests redirections for urllib implementation"""
1122
 
 
1123
 
    _qualifier = 'urllib'
1124
 
    _transport = HttpTransport_urllib
1125
 
 
1126
 
 
1127
 
 
1128
 
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1129
 
                                  TestHTTPRedirectionLoop,
1130
 
                                  TestCaseWithTwoWebservers):
1131
 
    """Tests redirections for pycurl implementation"""
1132
 
 
1133
 
    _qualifier = 'pycurl'
1134
 
 
1135
 
 
1136
 
class TestDotBzrHidden(TestCaseWithTransport):
1137
 
 
1138
 
    ls = ['ls']
1139
 
    if sys.platform == 'win32':
1140
 
        ls = [os.environ['COMSPEC'], '/C', 'dir', '/B']
1141
 
 
1142
 
    def get_ls(self):
1143
 
        f = subprocess.Popen(self.ls, stdout=subprocess.PIPE,
1144
 
            stderr=subprocess.PIPE)
1145
 
        out, err = f.communicate()
1146
 
        self.assertEqual(0, f.returncode, 'Calling %s failed: %s'
1147
 
                         % (self.ls, err))
1148
 
        return out.splitlines()
1149
 
 
1150
 
    def test_dot_bzr_hidden(self):
1151
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1152
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1153
 
        b = bzrdir.BzrDir.create('.')
1154
 
        self.build_tree(['a'])
1155
 
        self.assertEquals(['a'], self.get_ls())
1156
 
 
1157
 
    def test_dot_bzr_hidden_with_url(self):
1158
 
        if sys.platform == 'win32' and not win32utils.has_win32file:
1159
 
            raise TestSkipped('unable to make file hidden without pywin32 library')
1160
 
        b = bzrdir.BzrDir.create(urlutils.local_path_to_url('.'))
1161
 
        self.build_tree(['a'])
1162
 
        self.assertEquals(['a'], self.get_ls())
1163
 
 
1164
 
 
1165
 
class _TestBzrDirFormat(bzrdir.BzrDirMetaFormat1):
1166
 
    """Test BzrDirFormat implementation for TestBzrDirSprout."""
1167
 
 
1168
 
    def _open(self, transport):
1169
 
        return _TestBzrDir(transport, self)
1170
 
 
1171
 
 
1172
 
class _TestBzrDir(bzrdir.BzrDirMeta1):
1173
 
    """Test BzrDir implementation for TestBzrDirSprout.
1174
 
    
1175
 
    When created a _TestBzrDir already has repository and a branch.  The branch
1176
 
    is a test double as well.
1177
 
    """
1178
 
 
1179
 
    def __init__(self, *args, **kwargs):
1180
 
        super(_TestBzrDir, self).__init__(*args, **kwargs)
1181
 
        self.test_branch = _TestBranch()
1182
 
        self.test_branch.repository = self.create_repository()
1183
 
 
1184
 
    def open_branch(self, unsupported=False):
1185
 
        return self.test_branch
1186
 
 
1187
 
    def cloning_metadir(self, require_stacking=False):
1188
 
        return _TestBzrDirFormat()
1189
 
 
1190
 
 
1191
 
class _TestBranch(bzrlib.branch.Branch):
1192
 
    """Test Branch implementation for TestBzrDirSprout."""
1193
 
 
1194
 
    def __init__(self, *args, **kwargs):
1195
 
        super(_TestBranch, self).__init__(*args, **kwargs)
1196
 
        self.calls = []
1197
 
        self._parent = None
1198
 
 
1199
 
    def sprout(self, *args, **kwargs):
1200
 
        self.calls.append('sprout')
1201
 
        return _TestBranch()
1202
 
 
1203
 
    def copy_content_into(self, destination, revision_id=None):
1204
 
        self.calls.append('copy_content_into')
1205
 
 
1206
 
    def get_parent(self):
1207
 
        return self._parent
1208
 
 
1209
 
    def set_parent(self, parent):
1210
 
        self._parent = parent
1211
 
 
1212
 
 
1213
 
class TestBzrDirSprout(TestCaseWithMemoryTransport):
1214
 
 
1215
 
    def test_sprout_uses_branch_sprout(self):
1216
 
        """BzrDir.sprout calls Branch.sprout.
1217
 
 
1218
 
        Usually, BzrDir.sprout should delegate to the branch's sprout method
1219
 
        for part of the work.  This allows the source branch to control the
1220
 
        choice of format for the new branch.
1221
 
        
1222
 
        There are exceptions, but this tests avoids them:
1223
 
          - if there's no branch in the source bzrdir,
1224
 
          - or if the stacking has been requested and the format needs to be
1225
 
            overridden to satisfy that.
1226
 
        """
1227
 
        # Make an instrumented bzrdir.
1228
 
        t = self.get_transport('source')
1229
 
        t.ensure_base()
1230
 
        source_bzrdir = _TestBzrDirFormat().initialize_on_transport(t)
1231
 
        # The instrumented bzrdir has a test_branch attribute that logs calls
1232
 
        # made to the branch contained in that bzrdir.  Initially the test
1233
 
        # branch exists but no calls have been made to it.
1234
 
        self.assertEqual([], source_bzrdir.test_branch.calls)
1235
 
 
1236
 
        # Sprout the bzrdir
1237
 
        target_url = self.get_url('target')
1238
 
        result = source_bzrdir.sprout(target_url, recurse='no')
1239
 
 
1240
 
        # The bzrdir called the branch's sprout method.
1241
 
        self.assertSubset(['sprout'], source_bzrdir.test_branch.calls)
1242
 
 
1243
 
    def test_sprout_parent(self):
1244
 
        grandparent_tree = self.make_branch('grandparent')
1245
 
        parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1246
 
        branch_tree = parent.bzrdir.sprout('branch').open_branch()
1247
 
        self.assertContainsRe(branch_tree.get_parent(), '/parent/$')