/brz/remove-bazaar

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