/brz/remove-bazaar

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