/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.4.39 by Robert Collins
Basic BzrDir support.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.4.39 by Robert Collins
Basic BzrDir support.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1534.4.39 by Robert Collins
Basic BzrDir support.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for bzrdir implementations - tests a bzrdir format."""
18
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
19
from cStringIO import StringIO
1731.1.33 by Aaron Bentley
Revert no-special-root changes
20
import errno
1534.4.39 by Robert Collins
Basic BzrDir support.
21
import os
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
22
from stat import S_ISDIR
1534.4.39 by Robert Collins
Basic BzrDir support.
23
import sys
24
1508.1.25 by Robert Collins
Update per review comments.
25
import bzrlib.branch
1957.1.17 by John Arbash Meinel
Change tests that expect locking to fail to timeout sooner.
26
from bzrlib import (
27
    bzrdir,
28
    errors,
29
    lockdir,
30
    repository,
31
    transactions,
32
    transport,
33
    ui,
34
    workingtree,
35
    )
1534.4.39 by Robert Collins
Basic BzrDir support.
36
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
37
from bzrlib.check import check
1534.4.39 by Robert Collins
Basic BzrDir support.
38
from bzrlib.errors import (FileExists,
39
                           NoSuchRevision,
40
                           NoSuchFile,
41
                           UninitializableFormat,
42
                           NotBranchError,
43
                           )
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
44
import bzrlib.revision
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
45
from bzrlib.tests import (
46
                          ChrootedTestCase,
47
                          TestCase,
48
                          TestCaseWithTransport,
49
                          TestSkipped,
50
                          )
1534.4.39 by Robert Collins
Basic BzrDir support.
51
from bzrlib.trace import mutter
52
from bzrlib.transport import get_transport
53
from bzrlib.upgrade import upgrade
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
54
from bzrlib.repofmt import weaverepo
1534.4.39 by Robert Collins
Basic BzrDir support.
55
56
57
class TestCaseWithBzrDir(TestCaseWithTransport):
58
59
    def setUp(self):
60
        super(TestCaseWithBzrDir, self).setUp()
61
        self.bzrdir = None
62
63
    def get_bzrdir(self):
64
        if self.bzrdir is None:
65
            self.bzrdir = self.make_bzrdir(None)
66
        return self.bzrdir
67
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
68
    def make_bzrdir(self, relpath, format=None):
69
        return super(TestCaseWithBzrDir, self).make_bzrdir(
70
            relpath, format=self.bzrdir_format)
71
1534.4.39 by Robert Collins
Basic BzrDir support.
72
73
74
class TestBzrDir(TestCaseWithBzrDir):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
75
    # Many of these tests test for disk equality rather than checking
76
    # for semantic equivalence. This works well for some tests but
77
    # is not good at handling changes in representation or the addition
78
    # or removal of control data. It would be nice to for instance:
79
    # sprout a new branch, check that the nickname has been reset by hand
80
    # and then set the nickname to match the source branch, at which point
81
    # a semantic equivalence should pass
82
83
    def assertDirectoriesEqual(self, source, target, ignore_list=[]):
84
        """Assert that the content of source and target are identical.
85
86
        paths in ignore list will be completely ignored.
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
87
        
88
        We ignore paths that represent data which is allowed to change during
89
        a clone or sprout: for instance, inventory.knit contains gzip fragements
90
        which have timestamps in them, and as we have read the inventory from 
91
        the source knit, the already-read data is recompressed rather than
92
        reading it again, which leads to changed timestamps. This is ok though,
93
        because the inventory.kndx file is not ignored, and the integrity of
94
        knit joins is tested by test_knit and test_versionedfile.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
95
        """
96
        files = []
97
        directories = ['.']
98
        while directories:
99
            dir = directories.pop()
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
100
            for path in set(source.list_dir(dir) + target.list_dir(dir)):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
101
                path = dir + '/' + path
102
                if path in ignore_list:
103
                    continue
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
104
                try:
105
                    stat = source.stat(path)
2230.3.30 by Aaron Bentley
Fix whitespace issues
106
                except errors.NoSuchFile:
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
107
                    self.fail('%s not in source' % path)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
108
                if S_ISDIR(stat.st_mode):
109
                    self.assertTrue(S_ISDIR(target.stat(path).st_mode))
110
                    directories.append(path)
111
                else:
112
                    self.assertEqualDiff(source.get(path).read(),
113
                                         target.get(path).read(),
114
                                         "text for file %r differs:\n" % path)
115
1910.4.12 by Andrew Bennetts
Use camelCase for test helpers to be more consistent unittest naming.
116
    def skipIfNoWorkingTree(self, a_bzrdir):
1910.4.11 by Andrew Bennetts
Add 'create_workingtree_or_skip' and 'skip_if_no_workingtree' helper methods to reduce duplicated code.
117
        """Raises TestSkipped if a_bzrdir doesn't have a working tree.
118
        
119
        If the bzrdir does have a workingtree, this is a no-op.
120
        """
121
        try:
122
            a_bzrdir.open_workingtree()
123
        except (errors.NotLocalUrl, errors.NoWorkingTree):
124
            raise TestSkipped("bzrdir on transport %r has no working tree"
125
                              % a_bzrdir.transport)
126
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
127
    def createWorkingTreeOrSkip(self, a_bzrdir):
1910.4.11 by Andrew Bennetts
Add 'create_workingtree_or_skip' and 'skip_if_no_workingtree' helper methods to reduce duplicated code.
128
        """Create a working tree on a_bzrdir, or raise TestSkipped.
129
        
130
        A simple wrapper for create_workingtree that translates NotLocalUrl into
131
        TestSkipped.  Returns the newly created working tree.
132
        """
133
        try:
134
            return a_bzrdir.create_workingtree()
135
        except errors.NotLocalUrl:
136
            raise TestSkipped("cannot make working tree with transport %r"
137
                              % a_bzrdir.transport)
138
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
139
    def sproutOrSkip(self, from_bzrdir, to_url, revision_id=None, basis=None,
140
                     force_new_repo=False):
141
        """Sprout from_bzrdir into to_url, or raise TestSkipped.
142
        
143
        A simple wrapper for from_bzrdir.sprout that translates NotLocalUrl into
144
        TestSkipped.  Returns the newly sprouted bzrdir.
145
        """
146
        try:
147
            target = from_bzrdir.sprout(to_url, revision_id=revision_id,
148
                                        basis=basis,
149
                                        force_new_repo=force_new_repo)
150
        except errors.NotLocalUrl:
151
            raise TestSkipped('Cannot sprout to remote bzrdirs.')
152
        return target
153
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
154
    def test_create_null_workingtree(self):
155
        dir = self.make_bzrdir('dir1')
156
        dir.create_repository()
157
        dir.create_branch()
1752.2.87 by Andrew Bennetts
Make tests pass.
158
        try:
159
            wt = dir.create_workingtree(revision_id=bzrlib.revision.NULL_REVISION)
160
        except errors.NotLocalUrl:
161
            raise TestSkipped("cannot make working tree with transport %r"
162
                              % dir.transport)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
163
        self.assertEqual([], wt.get_parent_ids())
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
164
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
165
    def test_destroy_workingtree(self):
166
        tree = self.make_branch_and_tree('tree')
167
        self.build_tree(['tree/file'])
168
        tree.add('file')
169
        tree.commit('first commit')
170
        bzrdir = tree.bzrdir
171
        try:
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
172
            bzrdir.destroy_workingtree()
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
173
        except errors.UnsupportedOperation:
174
            raise TestSkipped('Format does not support destroying tree')
175
        self.failIfExists('tree/file')
176
        self.assertRaises(errors.NoWorkingTree, bzrdir.open_workingtree)
177
        bzrdir.create_workingtree()
178
        self.failUnlessExists('tree/file')
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
179
        bzrdir.destroy_workingtree_metadata()
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
180
        self.failUnlessExists('tree/file')
181
        self.assertRaises(errors.NoWorkingTree, bzrdir.open_workingtree)
182
            
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
183
    def test_clone_bzrdir_empty(self):
184
        dir = self.make_bzrdir('source')
185
        target = dir.clone(self.get_url('target'))
186
        self.assertNotEqual(dir.transport.base, target.transport.base)
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
187
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
188
                                    ['./.bzr/merge-hashes'])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
189
    
1534.6.8 by Robert Collins
Test the use of clone on empty bzrdir with force_new_repo.
190
    def test_clone_bzrdir_empty_force_new_ignored(self):
1534.6.9 by Robert Collins
sprouting into shared repositories
191
        # the force_new_repo parameter should have no effect on an empty
1534.6.8 by Robert Collins
Test the use of clone on empty bzrdir with force_new_repo.
192
        # bzrdir's clone logic
193
        dir = self.make_bzrdir('source')
194
        target = dir.clone(self.get_url('target'), force_new_repo=True)
195
        self.assertNotEqual(dir.transport.base, target.transport.base)
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
196
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
197
                                    ['./.bzr/merge-hashes'])
1534.6.8 by Robert Collins
Test the use of clone on empty bzrdir with force_new_repo.
198
    
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
199
    def test_clone_bzrdir_repository(self):
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
200
        tree = self.make_branch_and_tree('commit_tree')
201
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
202
        tree.add('foo')
203
        tree.commit('revision 1', rev_id='1')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
204
        dir = self.make_bzrdir('source')
205
        repo = dir.create_repository()
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
206
        repo.fetch(tree.branch.repository)
207
        self.assertTrue(repo.has_revision('1'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
208
        target = dir.clone(self.get_url('target'))
209
        self.assertNotEqual(dir.transport.base, target.transport.base)
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
210
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
211
                                    [
212
                                     './.bzr/merge-hashes',
213
                                     './.bzr/repository/inventory.knit',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
214
                                     ])
215
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
216
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
217
    def test_clone_bzrdir_repository_under_shared(self):
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
218
        tree = self.make_branch_and_tree('commit_tree')
219
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
220
        tree.add('foo')
221
        tree.commit('revision 1', rev_id='1')
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
222
        dir = self.make_bzrdir('source')
223
        repo = dir.create_repository()
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
224
        repo.fetch(tree.branch.repository)
225
        self.assertTrue(repo.has_revision('1'))
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
226
        try:
227
            self.make_repository('target', shared=True)
228
        except errors.IncompatibleFormat:
229
            return
230
        target = dir.clone(self.get_url('target/child'))
231
        self.assertNotEqual(dir.transport.base, target.transport.base)
232
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
233
234
    def test_clone_bzrdir_repository_branch_both_under_shared(self):
235
        try:
236
            shared_repo = self.make_repository('shared', shared=True)
237
        except errors.IncompatibleFormat:
238
            return
239
        tree = self.make_branch_and_tree('commit_tree')
240
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
241
        tree.add('foo')
242
        tree.commit('revision 1', rev_id='1')
243
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
244
        tree.set_parent_trees([])
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
245
        tree.commit('revision 2', rev_id='2')
246
        tree.bzrdir.open_repository().copy_content_into(shared_repo)
247
        dir = self.make_bzrdir('shared/source')
248
        dir.create_branch()
249
        target = dir.clone(self.get_url('shared/target'))
250
        self.assertNotEqual(dir.transport.base, target.transport.base)
251
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
252
        self.assertTrue(shared_repo.has_revision('1'))
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
253
254
    def test_clone_bzrdir_repository_branch_only_source_under_shared(self):
255
        try:
256
            shared_repo = self.make_repository('shared', shared=True)
257
        except errors.IncompatibleFormat:
258
            return
259
        tree = self.make_branch_and_tree('commit_tree')
260
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
261
        tree.add('foo')
262
        tree.commit('revision 1', rev_id='1')
263
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
264
        tree.set_parent_trees([])
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
265
        tree.commit('revision 2', rev_id='2')
266
        tree.bzrdir.open_repository().copy_content_into(shared_repo)
267
        shared_repo.set_make_working_trees(False)
268
        self.assertFalse(shared_repo.make_working_trees())
269
        self.assertTrue(shared_repo.has_revision('1'))
270
        dir = self.make_bzrdir('shared/source')
271
        dir.create_branch()
272
        target = dir.clone(self.get_url('target'))
273
        self.assertNotEqual(dir.transport.base, target.transport.base)
274
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
275
        branch = target.open_branch()
276
        self.assertTrue(branch.repository.has_revision('1'))
277
        self.assertFalse(branch.repository.make_working_trees())
278
        self.assertTrue(branch.repository.is_shared())
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
279
        
1534.6.9 by Robert Collins
sprouting into shared repositories
280
    def test_clone_bzrdir_repository_under_shared_force_new_repo(self):
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
281
        tree = self.make_branch_and_tree('commit_tree')
282
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
283
        tree.add('foo')
284
        tree.commit('revision 1', rev_id='1')
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
285
        dir = self.make_bzrdir('source')
286
        repo = dir.create_repository()
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
287
        repo.fetch(tree.branch.repository)
288
        self.assertTrue(repo.has_revision('1'))
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
289
        try:
290
            self.make_repository('target', shared=True)
291
        except errors.IncompatibleFormat:
292
            return
293
        target = dir.clone(self.get_url('target/child'), force_new_repo=True)
294
        self.assertNotEqual(dir.transport.base, target.transport.base)
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
295
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
296
                                    ['./.bzr/repository/inventory.knit',
297
                                     ])
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
298
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
299
    def test_clone_bzrdir_repository_revision(self):
300
        # test for revision limiting, [smoke test, not corner case checks].
301
        # make a repository with some revisions,
302
        # and clone it with a revision limit.
303
        # 
304
        tree = self.make_branch_and_tree('commit_tree')
305
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
306
        tree.add('foo')
307
        tree.commit('revision 1', rev_id='1')
308
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
309
        tree.set_parent_trees([])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
310
        tree.commit('revision 2', rev_id='2')
311
        source = self.make_repository('source')
312
        tree.bzrdir.open_repository().copy_content_into(source)
313
        dir = source.bzrdir
314
        target = dir.clone(self.get_url('target'), revision_id='2')
315
        raise TestSkipped('revision limiting not strict yet')
316
317
    def test_clone_bzrdir_branch_and_repo(self):
318
        tree = self.make_branch_and_tree('commit_tree')
319
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
320
        tree.add('foo')
321
        tree.commit('revision 1')
322
        source = self.make_branch('source')
323
        tree.bzrdir.open_repository().copy_content_into(source.repository)
324
        tree.bzrdir.open_branch().copy_content_into(source)
325
        dir = source.bzrdir
326
        target = dir.clone(self.get_url('target'))
327
        self.assertNotEqual(dir.transport.base, target.transport.base)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
328
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
329
                                    [
330
                                     './.bzr/basis-inventory-cache',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
331
                                     './.bzr/checkout/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
332
                                     './.bzr/merge-hashes',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
333
                                     './.bzr/repository/inventory.knit',
2230.3.30 by Aaron Bentley
Fix whitespace issues
334
                                     './.bzr/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
335
                                    ])
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
336
337
    def test_clone_bzrdir_branch_and_repo_into_shared_repo(self):
338
        # by default cloning into a shared repo uses the shared repo.
339
        tree = self.make_branch_and_tree('commit_tree')
340
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
341
        tree.add('foo')
342
        tree.commit('revision 1')
343
        source = self.make_branch('source')
344
        tree.bzrdir.open_repository().copy_content_into(source.repository)
345
        tree.bzrdir.open_branch().copy_content_into(source)
346
        try:
347
            self.make_repository('target', shared=True)
348
        except errors.IncompatibleFormat:
349
            return
350
        dir = source.bzrdir
351
        target = dir.clone(self.get_url('target/child'))
352
        self.assertNotEqual(dir.transport.base, target.transport.base)
353
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
354
        self.assertEqual(source.revision_history(),
355
                         target.open_branch().revision_history())
356
357
    def test_clone_bzrdir_branch_and_repo_into_shared_repo_force_new_repo(self):
358
        # by default cloning into a shared repo uses the shared repo.
359
        tree = self.make_branch_and_tree('commit_tree')
360
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
361
        tree.add('foo')
362
        tree.commit('revision 1')
363
        source = self.make_branch('source')
364
        tree.bzrdir.open_repository().copy_content_into(source.repository)
365
        tree.bzrdir.open_branch().copy_content_into(source)
366
        try:
367
            self.make_repository('target', shared=True)
368
        except errors.IncompatibleFormat:
369
            return
370
        dir = source.bzrdir
371
        target = dir.clone(self.get_url('target/child'), force_new_repo=True)
372
        self.assertNotEqual(dir.transport.base, target.transport.base)
373
        target.open_repository()
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
374
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
375
                                    ['./.bzr/repository/inventory.knit',
376
                                     ])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
377
378
    def test_clone_bzrdir_branch_reference(self):
379
        # cloning should preserve the reference status of the branch in a bzrdir
380
        referenced_branch = self.make_branch('referencced')
381
        dir = self.make_bzrdir('source')
382
        try:
1508.1.25 by Robert Collins
Update per review comments.
383
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
384
                referenced_branch)
385
        except errors.IncompatibleFormat:
386
            # this is ok too, not all formats have to support references.
387
            return
388
        target = dir.clone(self.get_url('target'))
389
        self.assertNotEqual(dir.transport.base, target.transport.base)
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
390
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
391
                                    ['./.bzr/repository/inventory.knit',
392
                                     ])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
393
394
    def test_clone_bzrdir_branch_revision(self):
395
        # test for revision limiting, [smoke test, not corner case checks].
396
        # make a branch with some revisions,
397
        # and clone it with a revision limit.
398
        # 
399
        tree = self.make_branch_and_tree('commit_tree')
400
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
401
        tree.add('foo')
402
        tree.commit('revision 1', rev_id='1')
403
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
404
        source = self.make_branch('source')
405
        tree.bzrdir.open_repository().copy_content_into(source.repository)
406
        tree.bzrdir.open_branch().copy_content_into(source)
407
        dir = source.bzrdir
408
        target = dir.clone(self.get_url('target'), revision_id='1')
409
        self.assertEqual('1', target.open_branch().last_revision())
410
        
411
    def test_clone_bzrdir_tree_branch_repo(self):
412
        tree = self.make_branch_and_tree('sourcce')
413
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
414
        tree.add('foo')
415
        tree.commit('revision 1')
416
        dir = tree.bzrdir
417
        target = dir.clone(self.get_url('target'))
1910.4.12 by Andrew Bennetts
Use camelCase for test helpers to be more consistent unittest naming.
418
        self.skipIfNoWorkingTree(target)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
419
        self.assertNotEqual(dir.transport.base, target.transport.base)
420
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
421
                                    ['./.bzr/stat-cache',
2255.10.5 by John Arbash Meinel
Fix a small bug when we have a symlink that does not need to be re-read.
422
                                     './.bzr/checkout/dirstate',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
423
                                     './.bzr/checkout/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
424
                                     './.bzr/checkout/merge-hashes',
425
                                     './.bzr/merge-hashes',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
426
                                     './.bzr/repository/inventory.knit',
427
                                     ])
428
1534.7.175 by Aaron Bentley
Ensured revert writes a normal inventory
429
        target.open_workingtree().revert([])
430
431
    def test_revert_inventory(self):
432
        tree = self.make_branch_and_tree('sourcce')
433
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
434
        tree.add('foo')
435
        tree.commit('revision 1')
436
        dir = tree.bzrdir
437
        target = dir.clone(self.get_url('target'))
1910.4.12 by Andrew Bennetts
Use camelCase for test helpers to be more consistent unittest naming.
438
        self.skipIfNoWorkingTree(target)
1534.7.175 by Aaron Bentley
Ensured revert writes a normal inventory
439
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
440
                                    ['./.bzr/stat-cache',
2255.10.8 by John Arbash Meinel
Fix another tests that was assuming dirstate was identical
441
                                     './.bzr/checkout/dirstate',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
442
                                     './.bzr/checkout/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
443
                                     './.bzr/checkout/merge-hashes',
444
                                     './.bzr/merge-hashes',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
445
                                     './.bzr/repository/inventory.knit',
446
                                     ])
447
1534.7.175 by Aaron Bentley
Ensured revert writes a normal inventory
448
        target.open_workingtree().revert([])
449
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
450
                                    ['./.bzr/stat-cache',
2255.10.8 by John Arbash Meinel
Fix another tests that was assuming dirstate was identical
451
                                     './.bzr/checkout/dirstate',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
452
                                     './.bzr/checkout/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
453
                                     './.bzr/checkout/merge-hashes',
454
                                     './.bzr/merge-hashes',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
455
                                     './.bzr/repository/inventory.knit',
456
                                     ])
457
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
458
    def test_clone_bzrdir_tree_branch_reference(self):
459
        # a tree with a branch reference (aka a checkout) 
460
        # should stay a checkout on clone.
461
        referenced_branch = self.make_branch('referencced')
462
        dir = self.make_bzrdir('source')
463
        try:
1508.1.25 by Robert Collins
Update per review comments.
464
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
465
                referenced_branch)
466
        except errors.IncompatibleFormat:
467
            # this is ok too, not all formats have to support references.
468
            return
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
469
        self.createWorkingTreeOrSkip(dir)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
470
        target = dir.clone(self.get_url('target'))
1910.4.12 by Andrew Bennetts
Use camelCase for test helpers to be more consistent unittest naming.
471
        self.skipIfNoWorkingTree(target)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
472
        self.assertNotEqual(dir.transport.base, target.transport.base)
473
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
474
                                    ['./.bzr/stat-cache',
475
                                     './.bzr/checkout/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
476
                                     './.bzr/checkout/merge-hashes',
477
                                     './.bzr/merge-hashes',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
478
                                     './.bzr/repository/inventory.knit',
479
                                     ])
480
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
481
    def test_clone_bzrdir_tree_revision(self):
482
        # test for revision limiting, [smoke test, not corner case checks].
483
        # make a tree with a revision with a last-revision
484
        # and clone it with a revision limit.
485
        # This smoke test just checks the revision-id is right. Tree specific
486
        # tests will check corner cases.
487
        tree = self.make_branch_and_tree('source')
488
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
489
        tree.add('foo')
490
        tree.commit('revision 1', rev_id='1')
491
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
492
        dir = tree.bzrdir
493
        target = dir.clone(self.get_url('target'), revision_id='1')
1910.4.12 by Andrew Bennetts
Use camelCase for test helpers to be more consistent unittest naming.
494
        self.skipIfNoWorkingTree(target)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
495
        self.assertEqual(['1'], target.open_workingtree().get_parent_ids())
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
496
497
    def test_clone_bzrdir_incomplete_source_with_basis(self):
498
        # ensure that basis really does grab from the basis by having incomplete source
499
        tree = self.make_branch_and_tree('commit_tree')
500
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
501
        tree.add('foo')
502
        tree.commit('revision 1', rev_id='1')
503
        source = self.make_branch_and_tree('source')
504
        # this gives us an incomplete repository
505
        tree.bzrdir.open_repository().copy_content_into(source.branch.repository)
506
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
507
        tree.bzrdir.open_branch().copy_content_into(source.branch)
508
        tree.copy_content_into(source)
509
        self.assertFalse(source.branch.repository.has_revision('2'))
510
        dir = source.bzrdir
511
        target = dir.clone(self.get_url('target'), basis=tree.bzrdir)
512
        self.assertEqual('2', target.open_branch().last_revision())
1910.4.10 by Andrew Bennetts
Skip various test_sprout* tests when sprouting to non-local bzrdirs that can't have working trees; plus fix a test method naming clash and the bug it revealed in bzrdir.sprout.
513
        try:
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
514
            self.assertEqual(['2'], target.open_workingtree().get_parent_ids())
1910.4.10 by Andrew Bennetts
Skip various test_sprout* tests when sprouting to non-local bzrdirs that can't have working trees; plus fix a test method naming clash and the bug it revealed in bzrdir.sprout.
515
        except errors.NoWorkingTree:
516
            # It should have a working tree if it's able to have one, so if
517
            # we're here make sure it really can't have one.
518
            self.assertRaises(errors.NotLocalUrl, target.create_workingtree)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
519
        self.assertTrue(target.open_branch().repository.has_revision('2'))
520
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
521
    def test_get_branch_reference_on_reference(self):
522
        """get_branch_reference should return the right url."""
523
        referenced_branch = self.make_branch('referenced')
524
        dir = self.make_bzrdir('source')
525
        try:
526
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
527
                referenced_branch)
528
        except errors.IncompatibleFormat:
529
            # this is ok too, not all formats have to support references.
530
            return
531
        self.assertEqual(referenced_branch.bzrdir.root_transport.abspath('') + '/',
532
            dir.get_branch_reference())
533
534
    def test_get_branch_reference_on_non_reference(self):
535
        """get_branch_reference should return None for non-reference branches."""
536
        branch = self.make_branch('referenced')
537
        self.assertEqual(None, branch.bzrdir.get_branch_reference())
538
539
    def test_get_branch_reference_no_branch(self):
540
        """get_branch_reference should not mask NotBranchErrors."""
541
        dir = self.make_bzrdir('source')
542
        if dir.has_branch():
543
            # this format does not support branchless bzrdirs.
544
            return
545
        self.assertRaises(errors.NotBranchError, dir.get_branch_reference)
546
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
547
    def test_sprout_bzrdir_empty(self):
548
        dir = self.make_bzrdir('source')
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
549
        target = self.sproutOrSkip(dir, self.get_url('target'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
550
        self.assertNotEqual(dir.transport.base, target.transport.base)
551
        # creates a new repository branch and tree
552
        target.open_repository()
553
        target.open_branch()
554
        target.open_workingtree()
1534.6.9 by Robert Collins
sprouting into shared repositories
555
556
    def test_sprout_bzrdir_empty_under_shared_repo(self):
557
        # sprouting an empty dir into a repo uses the repo
558
        dir = self.make_bzrdir('source')
559
        try:
560
            self.make_repository('target', shared=True)
561
        except errors.IncompatibleFormat:
562
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
563
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
564
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
565
        target.open_branch()
566
        target.open_workingtree()
567
1910.4.10 by Andrew Bennetts
Skip various test_sprout* tests when sprouting to non-local bzrdirs that can't have working trees; plus fix a test method naming clash and the bug it revealed in bzrdir.sprout.
568
    def test_sprout_bzrdir_empty_under_shared_repo_force_new(self):
1534.6.9 by Robert Collins
sprouting into shared repositories
569
        # the force_new_repo parameter should force use of a new repo in an empty
570
        # bzrdir's sprout logic
571
        dir = self.make_bzrdir('source')
572
        try:
573
            self.make_repository('target', shared=True)
574
        except errors.IncompatibleFormat:
575
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
576
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
577
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
578
        target.open_repository()
579
        target.open_branch()
580
        target.open_workingtree()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
581
    
582
    def test_sprout_bzrdir_repository(self):
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
583
        tree = self.make_branch_and_tree('commit_tree')
584
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
585
        tree.add('foo')
586
        tree.commit('revision 1', rev_id='1')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
587
        dir = self.make_bzrdir('source')
588
        repo = dir.create_repository()
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
589
        repo.fetch(tree.branch.repository)
590
        self.assertTrue(repo.has_revision('1'))
1731.1.33 by Aaron Bentley
Revert no-special-root changes
591
        try:
592
            self.assertIs(dir.open_branch().last_revision(), None)
593
        except errors.NotBranchError:
594
            pass
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
595
        target = self.sproutOrSkip(dir, self.get_url('target'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
596
        self.assertNotEqual(dir.transport.base, target.transport.base)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
597
        # testing inventory isn't reasonable for repositories
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
598
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
599
                                    [
600
                                     './.bzr/branch',
601
                                     './.bzr/checkout',
602
                                     './.bzr/inventory',
603
                                     './.bzr/parent',
604
                                     './.bzr/repository/inventory.knit',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
605
                                     ])
1731.1.33 by Aaron Bentley
Revert no-special-root changes
606
        try:
607
            # If we happen to have a tree, we'll guarantee everything
608
            # except for the tree root is the same.
609
            inventory_f = file(dir.transport.base+'inventory', 'rb')
610
            self.assertContainsRe(inventory_f.read(), 
611
                                  '<inventory file_id="TREE_ROOT[^"]*"'
612
                                  ' format="5">\n</inventory>\n')
613
            inventory_f.close()
614
        except IOError, e:
615
            if e.errno != errno.ENOENT:
616
                raise
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
617
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
618
    def test_sprout_bzrdir_with_repository_to_shared(self):
1534.6.9 by Robert Collins
sprouting into shared repositories
619
        tree = self.make_branch_and_tree('commit_tree')
620
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
621
        tree.add('foo')
622
        tree.commit('revision 1', rev_id='1')
623
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
624
        tree.set_parent_trees([])
1534.6.9 by Robert Collins
sprouting into shared repositories
625
        tree.commit('revision 2', rev_id='2')
626
        source = self.make_repository('source')
627
        tree.bzrdir.open_repository().copy_content_into(source)
628
        dir = source.bzrdir
629
        try:
630
            shared_repo = self.make_repository('target', shared=True)
631
        except errors.IncompatibleFormat:
632
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
633
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
634
        self.assertNotEqual(dir.transport.base, target.transport.base)
635
        self.assertTrue(shared_repo.has_revision('1'))
636
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
637
    def test_sprout_bzrdir_repository_branch_both_under_shared(self):
638
        try:
639
            shared_repo = self.make_repository('shared', shared=True)
640
        except errors.IncompatibleFormat:
641
            return
642
        tree = self.make_branch_and_tree('commit_tree')
643
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
644
        tree.add('foo')
645
        tree.commit('revision 1', rev_id='1')
646
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
647
        tree.set_parent_trees([])
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
648
        tree.commit('revision 2', rev_id='2')
649
        tree.bzrdir.open_repository().copy_content_into(shared_repo)
650
        dir = self.make_bzrdir('shared/source')
651
        dir.create_branch()
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
652
        target = self.sproutOrSkip(dir, self.get_url('shared/target'))
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
653
        self.assertNotEqual(dir.transport.base, target.transport.base)
654
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
655
        self.assertTrue(shared_repo.has_revision('1'))
656
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
657
    def test_sprout_bzrdir_repository_branch_only_source_under_shared(self):
658
        try:
659
            shared_repo = self.make_repository('shared', shared=True)
660
        except errors.IncompatibleFormat:
661
            return
662
        tree = self.make_branch_and_tree('commit_tree')
663
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
664
        tree.add('foo')
665
        tree.commit('revision 1', rev_id='1')
666
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
667
        tree.set_parent_trees([])
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
668
        tree.commit('revision 2', rev_id='2')
669
        tree.bzrdir.open_repository().copy_content_into(shared_repo)
670
        shared_repo.set_make_working_trees(False)
671
        self.assertFalse(shared_repo.make_working_trees())
672
        self.assertTrue(shared_repo.has_revision('1'))
673
        dir = self.make_bzrdir('shared/source')
674
        dir.create_branch()
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
675
        target = self.sproutOrSkip(dir, self.get_url('target'))
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
676
        self.assertNotEqual(dir.transport.base, target.transport.base)
677
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
678
        branch = target.open_branch()
679
        self.assertTrue(branch.repository.has_revision('1'))
680
        self.assertTrue(branch.repository.make_working_trees())
681
        self.assertFalse(branch.repository.is_shared())
682
1534.6.9 by Robert Collins
sprouting into shared repositories
683
    def test_sprout_bzrdir_repository_under_shared_force_new_repo(self):
684
        tree = self.make_branch_and_tree('commit_tree')
685
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
686
        tree.add('foo')
687
        tree.commit('revision 1', rev_id='1')
688
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
689
        tree.set_parent_trees([])
1534.6.9 by Robert Collins
sprouting into shared repositories
690
        tree.commit('revision 2', rev_id='2')
691
        source = self.make_repository('source')
692
        tree.bzrdir.open_repository().copy_content_into(source)
693
        dir = source.bzrdir
694
        try:
695
            shared_repo = self.make_repository('target', shared=True)
696
        except errors.IncompatibleFormat:
697
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
698
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
699
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
700
        self.assertNotEqual(dir.transport.base, target.transport.base)
701
        self.assertFalse(shared_repo.has_revision('1'))
702
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
703
    def test_sprout_bzrdir_repository_revision(self):
704
        # test for revision limiting, [smoke test, not corner case checks].
705
        # make a repository with some revisions,
706
        # and sprout it with a revision limit.
707
        # 
708
        tree = self.make_branch_and_tree('commit_tree')
709
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
710
        tree.add('foo')
711
        tree.commit('revision 1', rev_id='1')
712
        tree.bzrdir.open_branch().set_revision_history([])
1908.6.1 by Robert Collins
Change all callers of set_last_revision to use set_parent_trees.
713
        tree.set_parent_trees([])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
714
        tree.commit('revision 2', rev_id='2')
715
        source = self.make_repository('source')
716
        tree.bzrdir.open_repository().copy_content_into(source)
717
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
718
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='2')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
719
        raise TestSkipped('revision limiting not strict yet')
720
721
    def test_sprout_bzrdir_branch_and_repo(self):
722
        tree = self.make_branch_and_tree('commit_tree')
723
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
724
        tree.add('foo')
725
        tree.commit('revision 1')
726
        source = self.make_branch('source')
727
        tree.bzrdir.open_repository().copy_content_into(source.repository)
728
        tree.bzrdir.open_branch().copy_content_into(source)
729
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
730
        target = self.sproutOrSkip(dir, self.get_url('target'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
731
        self.assertNotEqual(dir.transport.base, target.transport.base)
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
732
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
733
                                    [
734
                                     './.bzr/basis-inventory-cache',
735
                                     './.bzr/branch/branch.conf',
736
                                     './.bzr/branch/parent',
737
                                     './.bzr/checkout',
738
                                     './.bzr/checkout/inventory',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
739
                                     './.bzr/checkout/stat-cache',
740
                                     './.bzr/inventory',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
741
                                     './.bzr/parent',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
742
                                     './.bzr/repository/inventory.knit',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
743
                                     './.bzr/stat-cache',
744
                                     './foo',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
745
                                     ])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
746
1534.6.9 by Robert Collins
sprouting into shared repositories
747
    def test_sprout_bzrdir_branch_and_repo_shared(self):
748
        # sprouting a branch with a repo into a shared repo uses the shared
749
        # repo
750
        tree = self.make_branch_and_tree('commit_tree')
751
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
752
        tree.add('foo')
753
        tree.commit('revision 1', rev_id='1')
754
        source = self.make_branch('source')
755
        tree.bzrdir.open_repository().copy_content_into(source.repository)
756
        tree.bzrdir.open_branch().copy_content_into(source)
757
        dir = source.bzrdir
758
        try:
759
            shared_repo = self.make_repository('target', shared=True)
760
        except errors.IncompatibleFormat:
761
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
762
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
763
        self.assertTrue(shared_repo.has_revision('1'))
764
765
    def test_sprout_bzrdir_branch_and_repo_shared_force_new_repo(self):
766
        # sprouting a branch with a repo into a shared repo uses the shared
767
        # repo
768
        tree = self.make_branch_and_tree('commit_tree')
769
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
770
        tree.add('foo')
771
        tree.commit('revision 1', rev_id='1')
772
        source = self.make_branch('source')
773
        tree.bzrdir.open_repository().copy_content_into(source.repository)
774
        tree.bzrdir.open_branch().copy_content_into(source)
775
        dir = source.bzrdir
776
        try:
777
            shared_repo = self.make_repository('target', shared=True)
778
        except errors.IncompatibleFormat:
779
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
780
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
781
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
782
        self.assertNotEqual(dir.transport.base, target.transport.base)
783
        self.assertFalse(shared_repo.has_revision('1'))
784
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
785
    def test_sprout_bzrdir_branch_reference(self):
786
        # sprouting should create a repository if needed and a sprouted branch.
787
        referenced_branch = self.make_branch('referencced')
788
        dir = self.make_bzrdir('source')
789
        try:
1508.1.25 by Robert Collins
Update per review comments.
790
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
791
                referenced_branch)
792
        except errors.IncompatibleFormat:
793
            # this is ok too, not all formats have to support references.
794
            return
795
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
796
        target = self.sproutOrSkip(dir, self.get_url('target'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
797
        self.assertNotEqual(dir.transport.base, target.transport.base)
798
        # we want target to have a branch that is in-place.
799
        self.assertEqual(target, target.open_branch().bzrdir)
800
        # and as we dont support repositories being detached yet, a repo in 
801
        # place
802
        target.open_repository()
803
1534.6.9 by Robert Collins
sprouting into shared repositories
804
    def test_sprout_bzrdir_branch_reference_shared(self):
805
        # sprouting should create a repository if needed and a sprouted branch.
806
        referenced_tree = self.make_branch_and_tree('referenced')
807
        referenced_tree.commit('1', rev_id='1', allow_pointless=True)
808
        dir = self.make_bzrdir('source')
809
        try:
810
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
811
                referenced_tree.branch)
812
        except errors.IncompatibleFormat:
813
            # this is ok too, not all formats have to support references.
814
            return
815
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
816
        try:
817
            shared_repo = self.make_repository('target', shared=True)
818
        except errors.IncompatibleFormat:
819
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
820
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
821
        self.assertNotEqual(dir.transport.base, target.transport.base)
822
        # we want target to have a branch that is in-place.
823
        self.assertEqual(target, target.open_branch().bzrdir)
824
        # and we want no repository as the target is shared
825
        self.assertRaises(errors.NoRepositoryPresent, 
826
                          target.open_repository)
827
        # and we want revision '1' in the shared repo
828
        self.assertTrue(shared_repo.has_revision('1'))
829
830
    def test_sprout_bzrdir_branch_reference_shared_force_new_repo(self):
831
        # sprouting should create a repository if needed and a sprouted branch.
832
        referenced_tree = self.make_branch_and_tree('referenced')
833
        referenced_tree.commit('1', rev_id='1', allow_pointless=True)
834
        dir = self.make_bzrdir('source')
835
        try:
836
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
837
                referenced_tree.branch)
838
        except errors.IncompatibleFormat:
839
            # this is ok too, not all formats have to support references.
840
            return
841
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
842
        try:
843
            shared_repo = self.make_repository('target', shared=True)
844
        except errors.IncompatibleFormat:
845
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
846
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
847
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
848
        self.assertNotEqual(dir.transport.base, target.transport.base)
849
        # we want target to have a branch that is in-place.
850
        self.assertEqual(target, target.open_branch().bzrdir)
851
        # and we want revision '1' in the new repo
852
        self.assertTrue(target.open_repository().has_revision('1'))
853
        # but not the shared one
854
        self.assertFalse(shared_repo.has_revision('1'))
855
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
856
    def test_sprout_bzrdir_branch_revision(self):
857
        # test for revision limiting, [smoke test, not corner case checks].
858
        # make a repository with some revisions,
859
        # and sprout it with a revision limit.
860
        # 
861
        tree = self.make_branch_and_tree('commit_tree')
862
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
863
        tree.add('foo')
864
        tree.commit('revision 1', rev_id='1')
865
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
866
        source = self.make_branch('source')
867
        tree.bzrdir.open_repository().copy_content_into(source.repository)
868
        tree.bzrdir.open_branch().copy_content_into(source)
869
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
870
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='1')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
871
        self.assertEqual('1', target.open_branch().last_revision())
872
        
873
    def test_sprout_bzrdir_tree_branch_repo(self):
874
        tree = self.make_branch_and_tree('sourcce')
875
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
876
        tree.add('foo')
877
        tree.commit('revision 1')
878
        dir = tree.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
879
        target = self.sproutOrSkip(dir, self.get_url('target'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
880
        self.assertNotEqual(dir.transport.base, target.transport.base)
881
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
882
                                    [
883
                                     './.bzr/branch/branch.conf',
884
                                     './.bzr/branch/parent',
2255.10.9 by John Arbash Meinel
one more test that needs to ignore dirstate
885
                                     './.bzr/checkout/dirstate',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
886
                                     './.bzr/checkout/stat-cache',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
887
                                     './.bzr/checkout/inventory',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
888
                                     './.bzr/inventory',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
889
                                     './.bzr/parent',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
890
                                     './.bzr/repository/inventory.knit',
2230.3.15 by Aaron Bentley
assertDirectoriesEqual detects missing source, tests handle Branch6
891
                                     './.bzr/stat-cache',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
892
                                     ])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
893
894
    def test_sprout_bzrdir_tree_branch_reference(self):
895
        # sprouting should create a repository if needed and a sprouted branch.
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
896
        # the tree state should not be copied.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
897
        referenced_branch = self.make_branch('referencced')
898
        dir = self.make_bzrdir('source')
899
        try:
1508.1.25 by Robert Collins
Update per review comments.
900
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
901
                referenced_branch)
902
        except errors.IncompatibleFormat:
903
            # this is ok too, not all formats have to support references.
904
            return
905
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
906
        tree = self.createWorkingTreeOrSkip(dir)
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
907
        tree.bzrdir.root_transport.mkdir('subdir')
908
        tree.add('subdir')
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
909
        target = self.sproutOrSkip(dir, self.get_url('target'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
910
        self.assertNotEqual(dir.transport.base, target.transport.base)
911
        # we want target to have a branch that is in-place.
912
        self.assertEqual(target, target.open_branch().bzrdir)
913
        # and as we dont support repositories being detached yet, a repo in 
914
        # place
915
        target.open_repository()
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
916
        result_tree = target.open_workingtree()
917
        self.assertFalse(result_tree.has_filename('subdir'))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
918
919
    def test_sprout_bzrdir_tree_branch_reference_revision(self):
920
        # sprouting should create a repository if needed and a sprouted branch.
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
921
        # the tree state should not be copied but the revision changed,
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
922
        # and the likewise the new branch should be truncated too
923
        referenced_branch = self.make_branch('referencced')
924
        dir = self.make_bzrdir('source')
925
        try:
1508.1.25 by Robert Collins
Update per review comments.
926
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
927
                referenced_branch)
928
        except errors.IncompatibleFormat:
929
            # this is ok too, not all formats have to support references.
930
            return
931
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
932
        tree = self.createWorkingTreeOrSkip(dir)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
933
        self.build_tree(['foo'], transport=dir.root_transport)
934
        tree.add('foo')
935
        tree.commit('revision 1', rev_id='1')
936
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
937
        target = dir.sprout(self.get_url('target'), revision_id='1')
1910.4.12 by Andrew Bennetts
Use camelCase for test helpers to be more consistent unittest naming.
938
        self.skipIfNoWorkingTree(target)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
939
        self.assertNotEqual(dir.transport.base, target.transport.base)
940
        # we want target to have a branch that is in-place.
941
        self.assertEqual(target, target.open_branch().bzrdir)
942
        # and as we dont support repositories being detached yet, a repo in 
943
        # place
944
        target.open_repository()
945
        # we trust that the working tree sprouting works via the other tests.
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
946
        self.assertEqual(['1'], target.open_workingtree().get_parent_ids())
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
947
        self.assertEqual('1', target.open_branch().last_revision())
948
949
    def test_sprout_bzrdir_tree_revision(self):
950
        # test for revision limiting, [smoke test, not corner case checks].
951
        # make a tree with a revision with a last-revision
952
        # and sprout it with a revision limit.
953
        # This smoke test just checks the revision-id is right. Tree specific
954
        # tests will check corner cases.
955
        tree = self.make_branch_and_tree('source')
956
        self.build_tree(['foo'], transport=tree.bzrdir.root_transport)
957
        tree.add('foo')
958
        tree.commit('revision 1', rev_id='1')
959
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
960
        dir = tree.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
961
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='1')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
962
        self.assertEqual(['1'], target.open_workingtree().get_parent_ids())
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
963
964
    def test_sprout_bzrdir_incomplete_source_with_basis(self):
965
        # ensure that basis really does grab from the basis by having incomplete source
966
        tree = self.make_branch_and_tree('commit_tree')
967
        self.build_tree(['foo'], transport=tree.bzrdir.root_transport)
968
        tree.add('foo')
969
        tree.commit('revision 1', rev_id='1')
970
        source = self.make_branch_and_tree('source')
971
        # this gives us an incomplete repository
972
        tree.bzrdir.open_repository().copy_content_into(source.branch.repository)
973
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
974
        tree.bzrdir.open_branch().copy_content_into(source.branch)
975
        tree.copy_content_into(source)
976
        self.assertFalse(source.branch.repository.has_revision('2'))
977
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
978
        target = self.sproutOrSkip(dir, self.get_url('target'),
979
                                   basis=tree.bzrdir)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
980
        self.assertEqual('2', target.open_branch().last_revision())
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
981
        self.assertEqual(['2'], target.open_workingtree().get_parent_ids())
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
982
        self.assertTrue(target.open_branch().repository.has_revision('2'))
1534.4.39 by Robert Collins
Basic BzrDir support.
983
984
    def test_format_initialize_find_open(self):
985
        # loopback test to check the current format initializes to itself.
986
        if not self.bzrdir_format.is_supported():
987
            # unsupported formats are not loopback testable
988
            # because the default open will not open them and
989
            # they may not be initializable.
990
            return
991
        # supported formats must be able to init and open
992
        t = get_transport(self.get_url())
993
        readonly_t = get_transport(self.get_readonly_url())
994
        made_control = self.bzrdir_format.initialize(t.base)
995
        self.failUnless(isinstance(made_control, bzrdir.BzrDir))
996
        self.assertEqual(self.bzrdir_format,
997
                         bzrdir.BzrDirFormat.find_format(readonly_t))
998
        direct_opened_dir = self.bzrdir_format.open(readonly_t)
999
        opened_dir = bzrdir.BzrDir.open(t.base)
1000
        self.assertEqual(made_control._format,
1001
                         opened_dir._format)
1002
        self.assertEqual(direct_opened_dir._format,
1003
                         opened_dir._format)
1004
        self.failUnless(isinstance(opened_dir, bzrdir.BzrDir))
1005
1006
    def test_open_not_bzrdir(self):
1007
        # test the formats specific behaviour for no-content or similar dirs.
1008
        self.assertRaises(NotBranchError,
1009
                          self.bzrdir_format.open,
1010
                          get_transport(self.get_readonly_url()))
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1011
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1012
    def test_create_branch(self):
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1013
        # a bzrdir can construct a branch and repository for itself.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1014
        if not self.bzrdir_format.is_supported():
1015
            # unsupported formats are not loopback testable
1016
            # because the default open will not open them and
1017
            # they may not be initializable.
1018
            return
1019
        t = get_transport(self.get_url())
1020
        made_control = self.bzrdir_format.initialize(t.base)
1021
        made_repo = made_control.create_repository()
1022
        made_branch = made_control.create_branch()
1508.1.25 by Robert Collins
Update per review comments.
1023
        self.failUnless(isinstance(made_branch, bzrlib.branch.Branch))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1024
        self.assertEqual(made_control, made_branch.bzrdir)
1025
        
1026
    def test_open_branch(self):
1027
        if not self.bzrdir_format.is_supported():
1028
            # unsupported formats are not loopback testable
1029
            # because the default open will not open them and
1030
            # they may not be initializable.
1031
            return
1032
        t = get_transport(self.get_url())
1033
        made_control = self.bzrdir_format.initialize(t.base)
1034
        made_repo = made_control.create_repository()
1035
        made_branch = made_control.create_branch()
1036
        opened_branch = made_control.open_branch()
1037
        self.assertEqual(made_control, opened_branch.bzrdir)
1038
        self.failUnless(isinstance(opened_branch, made_branch.__class__))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1039
        self.failUnless(isinstance(opened_branch._format, made_branch._format.__class__))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1040
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1041
    def test_create_repository(self):
1042
        # a bzrdir can construct a repository for itself.
1043
        if not self.bzrdir_format.is_supported():
1044
            # unsupported formats are not loopback testable
1045
            # because the default open will not open them and
1046
            # they may not be initializable.
1047
            return
1048
        t = get_transport(self.get_url())
1049
        made_control = self.bzrdir_format.initialize(t.base)
1050
        made_repo = made_control.create_repository()
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
1051
        # Check that we have a repository object.
1052
        made_repo.has_revision('foo')
1053
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1054
        self.assertEqual(made_control, made_repo.bzrdir)
1841.2.2 by Jelmer Vernooij
Add more tests for create_repository().
1055
1056
    def test_create_repository_shared(self):
1057
        # a bzrdir can create a shared repository or 
1058
        # fail appropriately
1059
        if not self.bzrdir_format.is_supported():
1060
            # unsupported formats are not loopback testable
1061
            # because the default open will not open them and
1062
            # they may not be initializable.
1063
            return
1064
        t = get_transport(self.get_url())
1065
        made_control = self.bzrdir_format.initialize(t.base)
1066
        try:
1067
            made_repo = made_control.create_repository(shared=True)
1068
        except errors.IncompatibleFormat:
1069
            # Old bzrdir formats don't support shared repositories
1070
            # and should raise IncompatibleFormat
1071
            return
1072
        self.assertTrue(made_repo.is_shared())
1073
1074
    def test_create_repository_nonshared(self):
1075
        # a bzrdir can create a non-shared repository 
1076
        if not self.bzrdir_format.is_supported():
1077
            # unsupported formats are not loopback testable
1078
            # because the default open will not open them and
1079
            # they may not be initializable.
1080
            return
1081
        t = get_transport(self.get_url())
1082
        made_control = self.bzrdir_format.initialize(t.base)
1083
        made_repo = made_control.create_repository(shared=False)
1084
        self.assertFalse(made_repo.is_shared())
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1085
        
1086
    def test_open_repository(self):
1087
        if not self.bzrdir_format.is_supported():
1088
            # unsupported formats are not loopback testable
1089
            # because the default open will not open them and
1090
            # they may not be initializable.
1091
            return
1092
        t = get_transport(self.get_url())
1093
        made_control = self.bzrdir_format.initialize(t.base)
1094
        made_repo = made_control.create_repository()
1095
        opened_repo = made_control.open_repository()
1096
        self.assertEqual(made_control, opened_repo.bzrdir)
1097
        self.failUnless(isinstance(opened_repo, made_repo.__class__))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1098
        self.failUnless(isinstance(opened_repo._format, made_repo._format.__class__))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1099
1100
    def test_create_workingtree(self):
1101
        # a bzrdir can construct a working tree for itself.
1102
        if not self.bzrdir_format.is_supported():
1103
            # unsupported formats are not loopback testable
1104
            # because the default open will not open them and
1105
            # they may not be initializable.
1106
            return
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1107
        t = self.get_transport()
1910.5.9 by Andrew Bennetts
Move stuff out of a try block that doesn't need to be there.
1108
        made_control = self.bzrdir_format.initialize(t.base)
1109
        made_repo = made_control.create_repository()
1110
        made_branch = made_control.create_branch()
1910.5.11 by Andrew Bennetts
Use createWorkingTreeOrSkip helper in test_create_workingtree.
1111
        made_tree = self.createWorkingTreeOrSkip(made_control)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1112
        self.failUnless(isinstance(made_tree, workingtree.WorkingTree))
1113
        self.assertEqual(made_control, made_tree.bzrdir)
1114
        
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1115
    def test_create_workingtree_revision(self):
1116
        # a bzrdir can construct a working tree for itself @ a specific revision.
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1117
        t = self.get_transport()
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1118
        source = self.make_branch_and_tree('source')
1119
        source.commit('a', rev_id='a', allow_pointless=True)
1120
        source.commit('b', rev_id='b', allow_pointless=True)
1121
        self.build_tree(['new/'])
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1122
        t_new = t.clone('new')
1123
        made_control = self.bzrdir_format.initialize_on_transport(t_new)
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1124
        source.branch.repository.clone(made_control)
1125
        source.branch.clone(made_control)
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1126
        try:
1127
            made_tree = made_control.create_workingtree(revision_id='a')
1128
        except errors.NotLocalUrl:
1129
            raise TestSkipped("Can't make working tree on transport %r" % t)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
1130
        self.assertEqual(['a'], made_tree.get_parent_ids())
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1131
        
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1132
    def test_open_workingtree(self):
1133
        if not self.bzrdir_format.is_supported():
1134
            # unsupported formats are not loopback testable
1135
            # because the default open will not open them and
1136
            # they may not be initializable.
1137
            return
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1138
        # this has to be tested with local access as we still support creating
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1139
        # format 6 bzrdirs
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1140
        t = self.get_transport()
1141
        try:
1142
            made_control = self.bzrdir_format.initialize(t.base)
1143
            made_repo = made_control.create_repository()
1144
            made_branch = made_control.create_branch()
1145
            made_tree = made_control.create_workingtree()
1146
        except errors.NotLocalUrl:
1147
            raise TestSkipped("Can't initialize %r on transport %r"
1148
                              % (self.bzrdir_format, t))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1149
        opened_tree = made_control.open_workingtree()
1150
        self.assertEqual(made_control, opened_tree.bzrdir)
1151
        self.failUnless(isinstance(opened_tree, made_tree.__class__))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1152
        self.failUnless(isinstance(opened_tree._format, made_tree._format.__class__))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1153
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1154
    def test_get_branch_transport(self):
1155
        dir = self.make_bzrdir('.')
1156
        # without a format, get_branch_transport gives use a transport
1157
        # which -may- point to an existing dir.
1158
        self.assertTrue(isinstance(dir.get_branch_transport(None),
1159
                                   transport.Transport))
1160
        # with a given format, either the bzr dir supports identifiable
1161
        # branches, or it supports anonymous  branch formats, but not both.
1508.1.25 by Robert Collins
Update per review comments.
1162
        anonymous_format = bzrlib.branch.BzrBranchFormat4()
1163
        identifiable_format = bzrlib.branch.BzrBranchFormat5()
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1164
        try:
1165
            found_transport = dir.get_branch_transport(anonymous_format)
1166
            self.assertRaises(errors.IncompatibleFormat,
1167
                              dir.get_branch_transport,
1168
                              identifiable_format)
1169
        except errors.IncompatibleFormat:
1170
            found_transport = dir.get_branch_transport(identifiable_format)
1171
        self.assertTrue(isinstance(found_transport, transport.Transport))
1910.4.1 by Andrew Bennetts
Clearer comment and more precise test for directory existence in test_get_branch_transport.
1172
        # and the dir which has been initialized for us must exist.
1173
        found_transport.list_dir('.')
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1174
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1175
    def test_get_repository_transport(self):
1176
        dir = self.make_bzrdir('.')
1177
        # without a format, get_repository_transport gives use a transport
1178
        # which -may- point to an existing dir.
1179
        self.assertTrue(isinstance(dir.get_repository_transport(None),
1180
                                   transport.Transport))
1181
        # with a given format, either the bzr dir supports identifiable
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1182
        # repositories, or it supports anonymous  repository formats, but not both.
1183
        anonymous_format = weaverepo.RepositoryFormat6()
1184
        identifiable_format = weaverepo.RepositoryFormat7()
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1185
        try:
1186
            found_transport = dir.get_repository_transport(anonymous_format)
1187
            self.assertRaises(errors.IncompatibleFormat,
1188
                              dir.get_repository_transport,
1189
                              identifiable_format)
1190
        except errors.IncompatibleFormat:
1191
            found_transport = dir.get_repository_transport(identifiable_format)
1192
        self.assertTrue(isinstance(found_transport, transport.Transport))
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
1193
        # and the dir which has been initialized for us must exist.
1194
        found_transport.list_dir('.')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1195
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1196
    def test_get_workingtree_transport(self):
1197
        dir = self.make_bzrdir('.')
1198
        # without a format, get_workingtree_transport gives use a transport
1199
        # which -may- point to an existing dir.
1200
        self.assertTrue(isinstance(dir.get_workingtree_transport(None),
1201
                                   transport.Transport))
1202
        # with a given format, either the bzr dir supports identifiable
1203
        # trees, or it supports anonymous tree formats, but not both.
1204
        anonymous_format = workingtree.WorkingTreeFormat2()
1205
        identifiable_format = workingtree.WorkingTreeFormat3()
1206
        try:
1207
            found_transport = dir.get_workingtree_transport(anonymous_format)
1208
            self.assertRaises(errors.IncompatibleFormat,
1209
                              dir.get_workingtree_transport,
1210
                              identifiable_format)
1211
        except errors.IncompatibleFormat:
1212
            found_transport = dir.get_workingtree_transport(identifiable_format)
1213
        self.assertTrue(isinstance(found_transport, transport.Transport))
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
1214
        # and the dir which has been initialized for us must exist.
1215
        found_transport.list_dir('.')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1216
1217
    def test_root_transport(self):
1218
        dir = self.make_bzrdir('.')
1219
        self.assertEqual(dir.root_transport.base,
1220
                         get_transport(self.get_url('.')).base)
1221
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1222
    def test_find_repository_no_repo_under_standalone_branch(self):
1223
        # finding a repo stops at standalone branches even if there is a
1224
        # higher repository available.
1225
        try:
1226
            repo = self.make_repository('.', shared=True)
1227
        except errors.IncompatibleFormat:
1228
            # need a shared repository to test this.
1229
            return
1230
        url = self.get_url('intermediate')
1231
        get_transport(self.get_url()).mkdir('intermediate')
1232
        get_transport(self.get_url()).mkdir('intermediate/child')
1233
        made_control = self.bzrdir_format.initialize(url)
1234
        made_control.create_repository()
1235
        innermost_control = self.bzrdir_format.initialize(
1236
            self.get_url('intermediate/child'))
1237
        try:
1238
            child_repo = innermost_control.open_repository()
1239
            # if there is a repository, then the format cannot ever hit this 
1240
            # code path.
1241
            return
1242
        except errors.NoRepositoryPresent:
1243
            pass
1244
        self.assertRaises(errors.NoRepositoryPresent,
1245
                          innermost_control.find_repository)
1246
1247
    def test_find_repository_containing_shared_repository(self):
1248
        # find repo inside a shared repo with an empty control dir
1249
        # returns the shared repo.
1250
        try:
1251
            repo = self.make_repository('.', shared=True)
1252
        except errors.IncompatibleFormat:
1253
            # need a shared repository to test this.
1254
            return
1255
        url = self.get_url('childbzrdir')
1256
        get_transport(self.get_url()).mkdir('childbzrdir')
1257
        made_control = self.bzrdir_format.initialize(url)
1258
        try:
1259
            child_repo = made_control.open_repository()
1260
            # if there is a repository, then the format cannot ever hit this 
1261
            # code path.
1262
            return
1263
        except errors.NoRepositoryPresent:
1264
            pass
1265
        found_repo = made_control.find_repository()
1266
        self.assertEqual(repo.bzrdir.root_transport.base,
1267
                         found_repo.bzrdir.root_transport.base)
1268
        
1269
    def test_find_repository_standalone_with_containing_shared_repository(self):
1270
        # find repo inside a standalone repo inside a shared repo finds the standalone repo
1271
        try:
1272
            containing_repo = self.make_repository('.', shared=True)
1273
        except errors.IncompatibleFormat:
1274
            # need a shared repository to test this.
1275
            return
1276
        child_repo = self.make_repository('childrepo')
1277
        opened_control = bzrdir.BzrDir.open(self.get_url('childrepo'))
1278
        found_repo = opened_control.find_repository()
1279
        self.assertEqual(child_repo.bzrdir.root_transport.base,
1280
                         found_repo.bzrdir.root_transport.base)
1281
1282
    def test_find_repository_shared_within_shared_repository(self):
1283
        # find repo at a shared repo inside a shared repo finds the inner repo
1284
        try:
1285
            containing_repo = self.make_repository('.', shared=True)
1286
        except errors.IncompatibleFormat:
1287
            # need a shared repository to test this.
1288
            return
1289
        url = self.get_url('childrepo')
1290
        get_transport(self.get_url()).mkdir('childrepo')
1291
        child_control = self.bzrdir_format.initialize(url)
1292
        child_repo = child_control.create_repository(shared=True)
1293
        opened_control = bzrdir.BzrDir.open(self.get_url('childrepo'))
1294
        found_repo = opened_control.find_repository()
1295
        self.assertEqual(child_repo.bzrdir.root_transport.base,
1296
                         found_repo.bzrdir.root_transport.base)
1297
        self.assertNotEqual(child_repo.bzrdir.root_transport.base,
1298
                            containing_repo.bzrdir.root_transport.base)
1299
1300
    def test_find_repository_with_nested_dirs_works(self):
1301
        # find repo inside a bzrdir inside a bzrdir inside a shared repo 
1302
        # finds the outer shared repo.
1303
        try:
1304
            repo = self.make_repository('.', shared=True)
1305
        except errors.IncompatibleFormat:
1306
            # need a shared repository to test this.
1307
            return
1308
        url = self.get_url('intermediate')
1309
        get_transport(self.get_url()).mkdir('intermediate')
1310
        get_transport(self.get_url()).mkdir('intermediate/child')
1311
        made_control = self.bzrdir_format.initialize(url)
1312
        try:
1313
            child_repo = made_control.open_repository()
1314
            # if there is a repository, then the format cannot ever hit this 
1315
            # code path.
1316
            return
1317
        except errors.NoRepositoryPresent:
1318
            pass
1319
        innermost_control = self.bzrdir_format.initialize(
1320
            self.get_url('intermediate/child'))
1321
        try:
1322
            child_repo = innermost_control.open_repository()
1323
            # if there is a repository, then the format cannot ever hit this 
1324
            # code path.
1325
            return
1326
        except errors.NoRepositoryPresent:
1327
            pass
1328
        found_repo = innermost_control.find_repository()
1329
        self.assertEqual(repo.bzrdir.root_transport.base,
1330
                         found_repo.bzrdir.root_transport.base)
1331
        
1534.5.16 by Robert Collins
Review feedback.
1332
    def test_can_and_needs_format_conversion(self):
1534.5.8 by Robert Collins
Ensure that bzrdir implementations offer the can_update_format and needs_format_update api.
1333
        # check that we can ask an instance if its upgradable
1334
        dir = self.make_bzrdir('.')
1534.5.16 by Robert Collins
Review feedback.
1335
        if dir.can_convert_format():
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1336
            # if its default updatable there must be an updater 
2204.4.14 by Aaron Bentley
lastest -> latest
1337
            # (we force the latest known format as downgrades may not be
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1338
            # available
1339
            self.assertTrue(isinstance(dir._format.get_converter(
1340
                format=dir._format), bzrdir.Converter))
1534.5.16 by Robert Collins
Review feedback.
1341
        dir.needs_format_conversion(None)
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1342
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1343
    def test_upgrade_new_instance(self):
1910.4.9 by Andrew Bennetts
Skip test_upgrade_new_instance if we don't have a local transport, and cosmetic tweaks.
1344
        """Does an available updater work?"""
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1345
        dir = self.make_bzrdir('.')
1910.4.9 by Andrew Bennetts
Skip test_upgrade_new_instance if we don't have a local transport, and cosmetic tweaks.
1346
        # for now, upgrade is not ready for partial bzrdirs.
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1347
        dir.create_repository()
1348
        dir.create_branch()
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
1349
        self.createWorkingTreeOrSkip(dir)
1534.5.16 by Robert Collins
Review feedback.
1350
        if dir.can_convert_format():
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1351
            # if its default updatable there must be an updater 
2204.4.14 by Aaron Bentley
lastest -> latest
1352
            # (we force the latest known format as downgrades may not be
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1353
            # available
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1354
            pb = ui.ui_factory.nested_progress_bar()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1355
            try:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1356
                dir._format.get_converter(format=dir._format).convert(dir, pb)
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1357
            finally:
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1358
                pb.finished()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1359
            # and it should pass 'check' now.
1360
            check(bzrdir.BzrDir.open(self.get_url('.')).open_branch(), False)
1361
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1362
    def test_format_description(self):
1363
        dir = self.make_bzrdir('.')
1364
        text = dir._format.get_format_description()
1365
        self.failUnless(len(text))
1366
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
1367
    def test_retire_bzrdir(self):
1368
        bd = self.make_bzrdir('.')
1369
        # must not overwrite existing directories
1370
        self.build_tree(['.bzr.retired.0/', '.bzr.retired.0/junk',])
1371
        self.failUnlessExists('.bzr')
1372
        bd.retire_bzrdir()
1373
        self.failIfExists('.bzr')
1374
        self.failUnlessExists('.bzr.retired.1')
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1375
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1376
class TestBreakLock(TestCaseWithBzrDir):
1377
1378
    def setUp(self):
1379
        super(TestBreakLock, self).setUp()
1380
        # we want a UI factory that accepts canned input for the tests:
1381
        # while SilentUIFactory still accepts stdin, we need to customise
1382
        # ours
1383
        self.old_factory = bzrlib.ui.ui_factory
1687.1.15 by Robert Collins
Review comments.
1384
        self.addCleanup(self.restoreFactory)
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1385
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1386
1687.1.15 by Robert Collins
Review comments.
1387
    def restoreFactory(self):
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1388
        bzrlib.ui.ui_factory = self.old_factory
1389
1390
    def test_break_lock_empty(self):
1391
        # break lock on an empty bzrdir should work silently.
1392
        dir = self.make_bzrdir('.')
1393
        try:
1394
            dir.break_lock()
1395
        except NotImplementedError:
1396
            pass
1397
1398
    def test_break_lock_repository(self):
1399
        # break lock with just a repo should unlock the repo.
1400
        repo = self.make_repository('.')
1401
        repo.lock_write()
1402
        # only one yes needed here: it should only be unlocking
1403
        # the repo
1404
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1405
        try:
1406
            repo.bzrdir.break_lock()
1407
        except NotImplementedError:
1408
            # this bzrdir does not implement break_lock - so we cant test it.
1409
            repo.unlock()
1410
            return
1411
        lock_repo = repo.bzrdir.open_repository()
1412
        lock_repo.lock_write()
1413
        lock_repo.unlock()
1414
        self.assertRaises(errors.LockBroken, repo.unlock)
1415
1416
    def test_break_lock_branch(self):
1417
        # break lock with just a repo should unlock the branch.
1418
        # and not directly try the repository.
1419
        # we test this by making a branch reference to a branch
1420
        # and repository in another bzrdir
1421
        # for pre-metadir formats this will fail, thats ok.
1422
        master = self.make_branch('branch')
1423
        thisdir = self.make_bzrdir('this')
1424
        try:
1425
            bzrlib.branch.BranchReferenceFormat().initialize(
1426
                thisdir, master)
1427
        except errors.IncompatibleFormat:
1428
            return
1429
        unused_repo = thisdir.create_repository()
1430
        master.lock_write()
1431
        unused_repo.lock_write()
1957.1.17 by John Arbash Meinel
Change tests that expect locking to fail to timeout sooner.
1432
        try:
1433
            # two yes's : branch and repository. If the repo in this
1434
            # dir is inappropriately accessed, 3 will be needed, and
1435
            # we'll see that because the stream will be fully consumed
1436
            bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\n")
1437
            master.bzrdir.break_lock()
1438
            # only two ys should have been read
1439
            self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1440
            # we should be able to lock a newly opened branch now
1441
            branch = master.bzrdir.open_branch()
1442
            branch.lock_write()
1443
            branch.unlock()
1444
            # we should not be able to lock the repository in thisdir as its still
1445
            # held by the explicit lock we took, and the break lock should not have
1446
            # touched it.
1447
            repo = thisdir.open_repository()
1448
            orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
1449
            try:
1450
                lockdir._DEFAULT_TIMEOUT_SECONDS = 1
1451
                self.assertRaises(errors.LockContention, repo.lock_write)
1452
            finally:
1453
                lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
1454
        finally:
1455
            unused_repo.unlock()
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1456
        self.assertRaises(errors.LockBroken, master.unlock)
1457
1458
    def test_break_lock_tree(self):
1459
        # break lock with a tree should unlock the tree but not try the 
1460
        # branch explicitly. However this is very hard to test for as we 
1461
        # dont have a tree reference class, nor is one needed; 
1462
        # the worst case if this code unlocks twice is an extra question
1463
        # being asked.
1464
        tree = self.make_branch_and_tree('.')
1465
        tree.lock_write()
1466
        # three yes's : tree, branch and repository.
1467
        bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\ny\n")
1468
        try:
1469
            tree.bzrdir.break_lock()
2255.2.145 by Robert Collins
Support unbreakable locks for trees.
1470
        except (NotImplementedError, errors.LockActive):
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1471
            # bzrdir does not support break_lock
2255.2.145 by Robert Collins
Support unbreakable locks for trees.
1472
            # or one of the locked objects (currently only tree does this)
1473
            # raised a LockActive because we do still have a live locked
1474
            # object.
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1475
            tree.unlock()
1476
            return
1477
        self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1478
        lock_tree = tree.bzrdir.open_workingtree()
1479
        lock_tree.lock_write()
1480
        lock_tree.unlock()
1481
        self.assertRaises(errors.LockBroken, tree.unlock)
1482
1483
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1484
class ChrootedBzrDirTests(ChrootedTestCase):
1485
1486
    def test_find_repository_no_repository(self):
1487
        # loopback test to check the current format fails to find a 
1488
        # share repository correctly.
1489
        if not self.bzrdir_format.is_supported():
1490
            # unsupported formats are not loopback testable
1491
            # because the default open will not open them and
1492
            # they may not be initializable.
1493
            return
1494
        # supported formats must be able to init and open
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1495
        # - do the vfs initialisation over the basic vfs transport
1496
        # XXX: TODO this should become a 'bzrdirlocation' api call.
1497
        url = self.get_vfs_only_url('subdir')
1498
        get_transport(self.get_vfs_only_url()).mkdir('subdir')
1499
        made_control = self.bzrdir_format.initialize(self.get_url('subdir'))
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1500
        try:
1501
            repo = made_control.open_repository()
1502
            # if there is a repository, then the format cannot ever hit this 
1503
            # code path.
1504
            return
1505
        except errors.NoRepositoryPresent:
1506
            pass
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1507
        made_control = bzrdir.BzrDir.open(self.get_readonly_url('subdir'))
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1508
        self.assertRaises(errors.NoRepositoryPresent,
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1509
                          made_control.find_repository)
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1510