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