/brz/remove-bazaar

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