/brz/remove-bazaar

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