~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2008-03-11 14:29:08 UTC
  • mto: This revision was merged to the branch mainline in revision 3264.
  • Revision ID: aaron@aaronbentley.com-20080311142908-yyrvcpn2mldt0fnn
Update documentation to reflect conflict-handling difference

Show diffs side-by-side

added added

removed removed

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