/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
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
500
    def test_get_branch_reference_on_reference(self):
501
        """get_branch_reference should return the right url."""
502
        referenced_branch = self.make_branch('referenced')
503
        dir = self.make_bzrdir('source')
504
        try:
505
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
506
                referenced_branch)
507
        except errors.IncompatibleFormat:
508
            # this is ok too, not all formats have to support references.
509
            return
510
        self.assertEqual(referenced_branch.bzrdir.root_transport.abspath('') + '/',
511
            dir.get_branch_reference())
512
513
    def test_get_branch_reference_on_non_reference(self):
514
        """get_branch_reference should return None for non-reference branches."""
515
        branch = self.make_branch('referenced')
516
        self.assertEqual(None, branch.bzrdir.get_branch_reference())
517
518
    def test_get_branch_reference_no_branch(self):
519
        """get_branch_reference should not mask NotBranchErrors."""
520
        dir = self.make_bzrdir('source')
521
        if dir.has_branch():
522
            # this format does not support branchless bzrdirs.
523
            return
524
        self.assertRaises(errors.NotBranchError, dir.get_branch_reference)
525
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.
526
    def test_sprout_bzrdir_empty(self):
527
        dir = self.make_bzrdir('source')
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
528
        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.
529
        self.assertNotEqual(dir.transport.base, target.transport.base)
530
        # creates a new repository branch and tree
531
        target.open_repository()
532
        target.open_branch()
533
        target.open_workingtree()
1534.6.9 by Robert Collins
sprouting into shared repositories
534
535
    def test_sprout_bzrdir_empty_under_shared_repo(self):
536
        # sprouting an empty dir into a repo uses the repo
537
        dir = self.make_bzrdir('source')
538
        try:
539
            self.make_repository('target', shared=True)
540
        except errors.IncompatibleFormat:
541
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
542
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
543
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
544
        target.open_branch()
545
        target.open_workingtree()
546
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.
547
    def test_sprout_bzrdir_empty_under_shared_repo_force_new(self):
1534.6.9 by Robert Collins
sprouting into shared repositories
548
        # the force_new_repo parameter should force use of a new repo in an empty
549
        # bzrdir's sprout logic
550
        dir = self.make_bzrdir('source')
551
        try:
552
            self.make_repository('target', shared=True)
553
        except errors.IncompatibleFormat:
554
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
555
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
556
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
557
        target.open_repository()
558
        target.open_branch()
559
        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.
560
    
561
    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.
562
        tree = self.make_branch_and_tree('commit_tree')
563
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
564
        tree.add('foo')
565
        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.
566
        dir = self.make_bzrdir('source')
567
        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.
568
        repo.fetch(tree.branch.repository)
569
        self.assertTrue(repo.has_revision('1'))
1731.1.33 by Aaron Bentley
Revert no-special-root changes
570
        try:
571
            self.assertIs(dir.open_branch().last_revision(), None)
572
        except errors.NotBranchError:
573
            pass
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
574
        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.
575
        self.assertNotEqual(dir.transport.base, target.transport.base)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
576
        # testing inventory isn't reasonable for repositories
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
577
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
578
                                    ['./.bzr/repository/inventory.knit',
1731.1.33 by Aaron Bentley
Revert no-special-root changes
579
                                     './.bzr/inventory'
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
580
                                     ])
1731.1.33 by Aaron Bentley
Revert no-special-root changes
581
        try:
582
            # If we happen to have a tree, we'll guarantee everything
583
            # except for the tree root is the same.
584
            inventory_f = file(dir.transport.base+'inventory', 'rb')
585
            self.assertContainsRe(inventory_f.read(), 
586
                                  '<inventory file_id="TREE_ROOT[^"]*"'
587
                                  ' format="5">\n</inventory>\n')
588
            inventory_f.close()
589
        except IOError, e:
590
            if e.errno != errno.ENOENT:
591
                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.
592
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
593
    def test_sprout_bzrdir_with_repository_to_shared(self):
1534.6.9 by Robert Collins
sprouting into shared repositories
594
        tree = self.make_branch_and_tree('commit_tree')
595
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
596
        tree.add('foo')
597
        tree.commit('revision 1', rev_id='1')
598
        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.
599
        tree.set_parent_trees([])
1534.6.9 by Robert Collins
sprouting into shared repositories
600
        tree.commit('revision 2', rev_id='2')
601
        source = self.make_repository('source')
602
        tree.bzrdir.open_repository().copy_content_into(source)
603
        dir = source.bzrdir
604
        try:
605
            shared_repo = self.make_repository('target', shared=True)
606
        except errors.IncompatibleFormat:
607
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
608
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
609
        self.assertNotEqual(dir.transport.base, target.transport.base)
610
        self.assertTrue(shared_repo.has_revision('1'))
611
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
612
    def test_sprout_bzrdir_repository_branch_both_under_shared(self):
613
        try:
614
            shared_repo = self.make_repository('shared', shared=True)
615
        except errors.IncompatibleFormat:
616
            return
617
        tree = self.make_branch_and_tree('commit_tree')
618
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
619
        tree.add('foo')
620
        tree.commit('revision 1', rev_id='1')
621
        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.
622
        tree.set_parent_trees([])
1534.6.13 by Robert Collins
Allow push/pull and branch between branches in the same shared repository.
623
        tree.commit('revision 2', rev_id='2')
624
        tree.bzrdir.open_repository().copy_content_into(shared_repo)
625
        dir = self.make_bzrdir('shared/source')
626
        dir.create_branch()
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
627
        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.
628
        self.assertNotEqual(dir.transport.base, target.transport.base)
629
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
630
        self.assertTrue(shared_repo.has_revision('1'))
631
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
632
    def test_sprout_bzrdir_repository_branch_only_source_under_shared(self):
633
        try:
634
            shared_repo = self.make_repository('shared', shared=True)
635
        except errors.IncompatibleFormat:
636
            return
637
        tree = self.make_branch_and_tree('commit_tree')
638
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
639
        tree.add('foo')
640
        tree.commit('revision 1', rev_id='1')
641
        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.
642
        tree.set_parent_trees([])
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
643
        tree.commit('revision 2', rev_id='2')
644
        tree.bzrdir.open_repository().copy_content_into(shared_repo)
645
        shared_repo.set_make_working_trees(False)
646
        self.assertFalse(shared_repo.make_working_trees())
647
        self.assertTrue(shared_repo.has_revision('1'))
648
        dir = self.make_bzrdir('shared/source')
649
        dir.create_branch()
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
650
        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
651
        self.assertNotEqual(dir.transport.base, target.transport.base)
652
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
653
        branch = target.open_branch()
654
        self.assertTrue(branch.repository.has_revision('1'))
655
        self.assertTrue(branch.repository.make_working_trees())
656
        self.assertFalse(branch.repository.is_shared())
657
1534.6.9 by Robert Collins
sprouting into shared repositories
658
    def test_sprout_bzrdir_repository_under_shared_force_new_repo(self):
659
        tree = self.make_branch_and_tree('commit_tree')
660
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
661
        tree.add('foo')
662
        tree.commit('revision 1', rev_id='1')
663
        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.
664
        tree.set_parent_trees([])
1534.6.9 by Robert Collins
sprouting into shared repositories
665
        tree.commit('revision 2', rev_id='2')
666
        source = self.make_repository('source')
667
        tree.bzrdir.open_repository().copy_content_into(source)
668
        dir = source.bzrdir
669
        try:
670
            shared_repo = self.make_repository('target', shared=True)
671
        except errors.IncompatibleFormat:
672
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
673
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
674
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
675
        self.assertNotEqual(dir.transport.base, target.transport.base)
676
        self.assertFalse(shared_repo.has_revision('1'))
677
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.
678
    def test_sprout_bzrdir_repository_revision(self):
679
        # test for revision limiting, [smoke test, not corner case checks].
680
        # make a repository with some revisions,
681
        # and sprout it with a revision limit.
682
        # 
683
        tree = self.make_branch_and_tree('commit_tree')
684
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
685
        tree.add('foo')
686
        tree.commit('revision 1', rev_id='1')
687
        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.
688
        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.
689
        tree.commit('revision 2', rev_id='2')
690
        source = self.make_repository('source')
691
        tree.bzrdir.open_repository().copy_content_into(source)
692
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
693
        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.
694
        raise TestSkipped('revision limiting not strict yet')
695
696
    def test_sprout_bzrdir_branch_and_repo(self):
697
        tree = self.make_branch_and_tree('commit_tree')
698
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
699
        tree.add('foo')
700
        tree.commit('revision 1')
701
        source = self.make_branch('source')
702
        tree.bzrdir.open_repository().copy_content_into(source.repository)
703
        tree.bzrdir.open_branch().copy_content_into(source)
704
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
705
        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.
706
        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.
707
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
708
                                    ['./.bzr/stat-cache',
709
                                     './.bzr/checkout/stat-cache',
710
                                     './.bzr/inventory',
711
                                     './.bzr/checkout/inventory',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
712
                                     './.bzr/repository/inventory.knit',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
713
                                     ])
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.
714
1534.6.9 by Robert Collins
sprouting into shared repositories
715
    def test_sprout_bzrdir_branch_and_repo_shared(self):
716
        # sprouting a branch with a repo into a shared repo uses the shared
717
        # repo
718
        tree = self.make_branch_and_tree('commit_tree')
719
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
720
        tree.add('foo')
721
        tree.commit('revision 1', rev_id='1')
722
        source = self.make_branch('source')
723
        tree.bzrdir.open_repository().copy_content_into(source.repository)
724
        tree.bzrdir.open_branch().copy_content_into(source)
725
        dir = source.bzrdir
726
        try:
727
            shared_repo = self.make_repository('target', shared=True)
728
        except errors.IncompatibleFormat:
729
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
730
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
1534.6.9 by Robert Collins
sprouting into shared repositories
731
        self.assertTrue(shared_repo.has_revision('1'))
732
733
    def test_sprout_bzrdir_branch_and_repo_shared_force_new_repo(self):
734
        # sprouting a branch with a repo into a shared repo uses the shared
735
        # repo
736
        tree = self.make_branch_and_tree('commit_tree')
737
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
738
        tree.add('foo')
739
        tree.commit('revision 1', rev_id='1')
740
        source = self.make_branch('source')
741
        tree.bzrdir.open_repository().copy_content_into(source.repository)
742
        tree.bzrdir.open_branch().copy_content_into(source)
743
        dir = source.bzrdir
744
        try:
745
            shared_repo = self.make_repository('target', shared=True)
746
        except errors.IncompatibleFormat:
747
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
748
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
749
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
750
        self.assertNotEqual(dir.transport.base, target.transport.base)
751
        self.assertFalse(shared_repo.has_revision('1'))
752
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.
753
    def test_sprout_bzrdir_branch_reference(self):
754
        # sprouting should create a repository if needed and a sprouted branch.
755
        referenced_branch = self.make_branch('referencced')
756
        dir = self.make_bzrdir('source')
757
        try:
1508.1.25 by Robert Collins
Update per review comments.
758
            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.
759
                referenced_branch)
760
        except errors.IncompatibleFormat:
761
            # this is ok too, not all formats have to support references.
762
            return
763
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
764
        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.
765
        self.assertNotEqual(dir.transport.base, target.transport.base)
766
        # we want target to have a branch that is in-place.
767
        self.assertEqual(target, target.open_branch().bzrdir)
768
        # and as we dont support repositories being detached yet, a repo in 
769
        # place
770
        target.open_repository()
771
1534.6.9 by Robert Collins
sprouting into shared repositories
772
    def test_sprout_bzrdir_branch_reference_shared(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'))
1534.6.9 by Robert Collins
sprouting into shared repositories
789
        self.assertNotEqual(dir.transport.base, target.transport.base)
790
        # we want target to have a branch that is in-place.
791
        self.assertEqual(target, target.open_branch().bzrdir)
792
        # and we want no repository as the target is shared
793
        self.assertRaises(errors.NoRepositoryPresent, 
794
                          target.open_repository)
795
        # and we want revision '1' in the shared repo
796
        self.assertTrue(shared_repo.has_revision('1'))
797
798
    def test_sprout_bzrdir_branch_reference_shared_force_new_repo(self):
799
        # sprouting should create a repository if needed and a sprouted branch.
800
        referenced_tree = self.make_branch_and_tree('referenced')
801
        referenced_tree.commit('1', rev_id='1', allow_pointless=True)
802
        dir = self.make_bzrdir('source')
803
        try:
804
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
805
                referenced_tree.branch)
806
        except errors.IncompatibleFormat:
807
            # this is ok too, not all formats have to support references.
808
            return
809
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
810
        try:
811
            shared_repo = self.make_repository('target', shared=True)
812
        except errors.IncompatibleFormat:
813
            return
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
814
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
815
                                   force_new_repo=True)
1534.6.9 by Robert Collins
sprouting into shared repositories
816
        self.assertNotEqual(dir.transport.base, target.transport.base)
817
        # we want target to have a branch that is in-place.
818
        self.assertEqual(target, target.open_branch().bzrdir)
819
        # and we want revision '1' in the new repo
820
        self.assertTrue(target.open_repository().has_revision('1'))
821
        # but not the shared one
822
        self.assertFalse(shared_repo.has_revision('1'))
823
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.
824
    def test_sprout_bzrdir_branch_revision(self):
825
        # test for revision limiting, [smoke test, not corner case checks].
826
        # make a repository with some revisions,
827
        # and sprout it with a revision limit.
828
        # 
829
        tree = self.make_branch_and_tree('commit_tree')
830
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
831
        tree.add('foo')
832
        tree.commit('revision 1', rev_id='1')
833
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
834
        source = self.make_branch('source')
835
        tree.bzrdir.open_repository().copy_content_into(source.repository)
836
        tree.bzrdir.open_branch().copy_content_into(source)
837
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
838
        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.
839
        self.assertEqual('1', target.open_branch().last_revision())
840
        
841
    def test_sprout_bzrdir_tree_branch_repo(self):
842
        tree = self.make_branch_and_tree('sourcce')
843
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
844
        tree.add('foo')
845
        tree.commit('revision 1')
846
        dir = tree.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
847
        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.
848
        self.assertNotEqual(dir.transport.base, target.transport.base)
849
        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.
850
                                    ['./.bzr/stat-cache',
851
                                     './.bzr/checkout/stat-cache',
852
                                     './.bzr/inventory',
853
                                     './.bzr/checkout/inventory',
1666.1.16 by Robert Collins
Fix occasional test failuresin bzrdir_implementations.
854
                                     './.bzr/repository/inventory.knit',
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
855
                                     ])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
856
857
    def test_sprout_bzrdir_tree_branch_reference(self):
858
        # 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.
859
        # 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.
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)
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
870
        tree.bzrdir.root_transport.mkdir('subdir')
871
        tree.add('subdir')
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
872
        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.
873
        self.assertNotEqual(dir.transport.base, target.transport.base)
874
        # we want target to have a branch that is in-place.
875
        self.assertEqual(target, target.open_branch().bzrdir)
876
        # and as we dont support repositories being detached yet, a repo in 
877
        # place
878
        target.open_repository()
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
879
        result_tree = target.open_workingtree()
880
        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.
881
882
    def test_sprout_bzrdir_tree_branch_reference_revision(self):
883
        # 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.
884
        # 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.
885
        # and the likewise the new branch should be truncated too
886
        referenced_branch = self.make_branch('referencced')
887
        dir = self.make_bzrdir('source')
888
        try:
1508.1.25 by Robert Collins
Update per review comments.
889
            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.
890
                referenced_branch)
891
        except errors.IncompatibleFormat:
892
            # this is ok too, not all formats have to support references.
893
            return
894
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
895
        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.
896
        self.build_tree(['foo'], transport=dir.root_transport)
897
        tree.add('foo')
898
        tree.commit('revision 1', rev_id='1')
899
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
900
        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.
901
        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.
902
        self.assertNotEqual(dir.transport.base, target.transport.base)
903
        # we want target to have a branch that is in-place.
904
        self.assertEqual(target, target.open_branch().bzrdir)
905
        # and as we dont support repositories being detached yet, a repo in 
906
        # place
907
        target.open_repository()
908
        # we trust that the working tree sprouting works via the other tests.
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
909
        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.
910
        self.assertEqual('1', target.open_branch().last_revision())
911
912
    def test_sprout_bzrdir_tree_revision(self):
913
        # test for revision limiting, [smoke test, not corner case checks].
914
        # make a tree with a revision with a last-revision
915
        # and sprout it with a revision limit.
916
        # This smoke test just checks the revision-id is right. Tree specific
917
        # tests will check corner cases.
918
        tree = self.make_branch_and_tree('source')
919
        self.build_tree(['foo'], transport=tree.bzrdir.root_transport)
920
        tree.add('foo')
921
        tree.commit('revision 1', rev_id='1')
922
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
923
        dir = tree.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
924
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='1')
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
925
        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.
926
927
    def test_sprout_bzrdir_incomplete_source_with_basis(self):
928
        # ensure that basis really does grab from the basis by having incomplete source
929
        tree = self.make_branch_and_tree('commit_tree')
930
        self.build_tree(['foo'], transport=tree.bzrdir.root_transport)
931
        tree.add('foo')
932
        tree.commit('revision 1', rev_id='1')
933
        source = self.make_branch_and_tree('source')
934
        # this gives us an incomplete repository
935
        tree.bzrdir.open_repository().copy_content_into(source.branch.repository)
936
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
937
        tree.bzrdir.open_branch().copy_content_into(source.branch)
938
        tree.copy_content_into(source)
939
        self.assertFalse(source.branch.repository.has_revision('2'))
940
        dir = source.bzrdir
1910.4.14 by Andrew Bennetts
Add a sproutOrSkip test helper.
941
        target = self.sproutOrSkip(dir, self.get_url('target'),
942
                                   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.
943
        self.assertEqual('2', target.open_branch().last_revision())
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
944
        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.
945
        self.assertTrue(target.open_branch().repository.has_revision('2'))
1534.4.39 by Robert Collins
Basic BzrDir support.
946
947
    def test_format_initialize_find_open(self):
948
        # loopback test to check the current format initializes to itself.
949
        if not self.bzrdir_format.is_supported():
950
            # unsupported formats are not loopback testable
951
            # because the default open will not open them and
952
            # they may not be initializable.
953
            return
954
        # supported formats must be able to init and open
955
        t = get_transport(self.get_url())
956
        readonly_t = get_transport(self.get_readonly_url())
957
        made_control = self.bzrdir_format.initialize(t.base)
958
        self.failUnless(isinstance(made_control, bzrdir.BzrDir))
959
        self.assertEqual(self.bzrdir_format,
960
                         bzrdir.BzrDirFormat.find_format(readonly_t))
961
        direct_opened_dir = self.bzrdir_format.open(readonly_t)
962
        opened_dir = bzrdir.BzrDir.open(t.base)
963
        self.assertEqual(made_control._format,
964
                         opened_dir._format)
965
        self.assertEqual(direct_opened_dir._format,
966
                         opened_dir._format)
967
        self.failUnless(isinstance(opened_dir, bzrdir.BzrDir))
968
969
    def test_open_not_bzrdir(self):
970
        # test the formats specific behaviour for no-content or similar dirs.
971
        self.assertRaises(NotBranchError,
972
                          self.bzrdir_format.open,
973
                          get_transport(self.get_readonly_url()))
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
974
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
975
    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.
976
        # a bzrdir can construct a branch and repository for itself.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
977
        if not self.bzrdir_format.is_supported():
978
            # unsupported formats are not loopback testable
979
            # because the default open will not open them and
980
            # they may not be initializable.
981
            return
982
        t = get_transport(self.get_url())
983
        made_control = self.bzrdir_format.initialize(t.base)
984
        made_repo = made_control.create_repository()
985
        made_branch = made_control.create_branch()
1508.1.25 by Robert Collins
Update per review comments.
986
        self.failUnless(isinstance(made_branch, bzrlib.branch.Branch))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
987
        self.assertEqual(made_control, made_branch.bzrdir)
988
        
989
    def test_open_branch(self):
990
        if not self.bzrdir_format.is_supported():
991
            # unsupported formats are not loopback testable
992
            # because the default open will not open them and
993
            # they may not be initializable.
994
            return
995
        t = get_transport(self.get_url())
996
        made_control = self.bzrdir_format.initialize(t.base)
997
        made_repo = made_control.create_repository()
998
        made_branch = made_control.create_branch()
999
        opened_branch = made_control.open_branch()
1000
        self.assertEqual(made_control, opened_branch.bzrdir)
1001
        self.failUnless(isinstance(opened_branch, made_branch.__class__))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1002
        self.failUnless(isinstance(opened_branch._format, made_branch._format.__class__))
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1003
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1004
    def test_create_repository(self):
1005
        # a bzrdir can construct a repository for itself.
1006
        if not self.bzrdir_format.is_supported():
1007
            # unsupported formats are not loopback testable
1008
            # because the default open will not open them and
1009
            # they may not be initializable.
1010
            return
1011
        t = get_transport(self.get_url())
1012
        made_control = self.bzrdir_format.initialize(t.base)
1013
        made_repo = made_control.create_repository()
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
1014
        # Check that we have a repository object.
1015
        made_repo.has_revision('foo')
1016
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1017
        self.assertEqual(made_control, made_repo.bzrdir)
1841.2.2 by Jelmer Vernooij
Add more tests for create_repository().
1018
1019
    def test_create_repository_shared(self):
1020
        # a bzrdir can create a shared repository or 
1021
        # fail appropriately
1022
        if not self.bzrdir_format.is_supported():
1023
            # unsupported formats are not loopback testable
1024
            # because the default open will not open them and
1025
            # they may not be initializable.
1026
            return
1027
        t = get_transport(self.get_url())
1028
        made_control = self.bzrdir_format.initialize(t.base)
1029
        try:
1030
            made_repo = made_control.create_repository(shared=True)
1031
        except errors.IncompatibleFormat:
1032
            # Old bzrdir formats don't support shared repositories
1033
            # and should raise IncompatibleFormat
1034
            return
1035
        self.assertTrue(made_repo.is_shared())
1036
1037
    def test_create_repository_nonshared(self):
1038
        # a bzrdir can create a non-shared repository 
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
1044
        t = get_transport(self.get_url())
1045
        made_control = self.bzrdir_format.initialize(t.base)
1046
        made_repo = made_control.create_repository(shared=False)
1047
        self.assertFalse(made_repo.is_shared())
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1048
        
1049
    def test_open_repository(self):
1050
        if not self.bzrdir_format.is_supported():
1051
            # unsupported formats are not loopback testable
1052
            # because the default open will not open them and
1053
            # they may not be initializable.
1054
            return
1055
        t = get_transport(self.get_url())
1056
        made_control = self.bzrdir_format.initialize(t.base)
1057
        made_repo = made_control.create_repository()
1058
        opened_repo = made_control.open_repository()
1059
        self.assertEqual(made_control, opened_repo.bzrdir)
1060
        self.failUnless(isinstance(opened_repo, made_repo.__class__))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1061
        self.failUnless(isinstance(opened_repo._format, made_repo._format.__class__))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1062
1063
    def test_create_workingtree(self):
1064
        # a bzrdir can construct a working tree for itself.
1065
        if not self.bzrdir_format.is_supported():
1066
            # unsupported formats are not loopback testable
1067
            # because the default open will not open them and
1068
            # they may not be initializable.
1069
            return
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.
1070
        t = self.get_transport()
1910.5.9 by Andrew Bennetts
Move stuff out of a try block that doesn't need to be there.
1071
        made_control = self.bzrdir_format.initialize(t.base)
1072
        made_repo = made_control.create_repository()
1073
        made_branch = made_control.create_branch()
1910.5.11 by Andrew Bennetts
Use createWorkingTreeOrSkip helper in test_create_workingtree.
1074
        made_tree = self.createWorkingTreeOrSkip(made_control)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1075
        self.failUnless(isinstance(made_tree, workingtree.WorkingTree))
1076
        self.assertEqual(made_control, made_tree.bzrdir)
1077
        
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1078
    def test_create_workingtree_revision(self):
1079
        # 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.
1080
        t = self.get_transport()
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1081
        source = self.make_branch_and_tree('source')
1082
        source.commit('a', rev_id='a', allow_pointless=True)
1083
        source.commit('b', rev_id='b', allow_pointless=True)
1084
        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.
1085
        t_new = t.clone('new')
1086
        made_control = self.bzrdir_format.initialize_on_transport(t_new)
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1087
        source.branch.repository.clone(made_control)
1088
        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.
1089
        try:
1090
            made_tree = made_control.create_workingtree(revision_id='a')
1091
        except errors.NotLocalUrl:
1092
            raise TestSkipped("Can't make working tree on transport %r" % t)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
1093
        self.assertEqual(['a'], made_tree.get_parent_ids())
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1094
        
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1095
    def test_open_workingtree(self):
1096
        if not self.bzrdir_format.is_supported():
1097
            # unsupported formats are not loopback testable
1098
            # because the default open will not open them and
1099
            # they may not be initializable.
1100
            return
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1101
        # 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.
1102
        # format 6 bzrdirs
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1103
        t = self.get_transport()
1104
        try:
1105
            made_control = self.bzrdir_format.initialize(t.base)
1106
            made_repo = made_control.create_repository()
1107
            made_branch = made_control.create_branch()
1108
            made_tree = made_control.create_workingtree()
1109
        except errors.NotLocalUrl:
1110
            raise TestSkipped("Can't initialize %r on transport %r"
1111
                              % (self.bzrdir_format, t))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1112
        opened_tree = made_control.open_workingtree()
1113
        self.assertEqual(made_control, opened_tree.bzrdir)
1114
        self.failUnless(isinstance(opened_tree, made_tree.__class__))
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1115
        self.failUnless(isinstance(opened_tree._format, made_tree._format.__class__))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1116
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1117
    def test_get_branch_transport(self):
1118
        dir = self.make_bzrdir('.')
1119
        # without a format, get_branch_transport gives use a transport
1120
        # which -may- point to an existing dir.
1121
        self.assertTrue(isinstance(dir.get_branch_transport(None),
1122
                                   transport.Transport))
1123
        # with a given format, either the bzr dir supports identifiable
1124
        # branches, or it supports anonymous  branch formats, but not both.
1508.1.25 by Robert Collins
Update per review comments.
1125
        anonymous_format = bzrlib.branch.BzrBranchFormat4()
1126
        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.
1127
        try:
1128
            found_transport = dir.get_branch_transport(anonymous_format)
1129
            self.assertRaises(errors.IncompatibleFormat,
1130
                              dir.get_branch_transport,
1131
                              identifiable_format)
1132
        except errors.IncompatibleFormat:
1133
            found_transport = dir.get_branch_transport(identifiable_format)
1134
        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.
1135
        # and the dir which has been initialized for us must exist.
1136
        found_transport.list_dir('.')
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1137
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1138
    def test_get_repository_transport(self):
1139
        dir = self.make_bzrdir('.')
1140
        # without a format, get_repository_transport gives use a transport
1141
        # which -may- point to an existing dir.
1142
        self.assertTrue(isinstance(dir.get_repository_transport(None),
1143
                                   transport.Transport))
1144
        # with a given format, either the bzr dir supports identifiable
1145
        # repositoryes, or it supports anonymous  repository formats, but not both.
1146
        anonymous_format = repository.RepositoryFormat6()
1147
        identifiable_format = repository.RepositoryFormat7()
1148
        try:
1149
            found_transport = dir.get_repository_transport(anonymous_format)
1150
            self.assertRaises(errors.IncompatibleFormat,
1151
                              dir.get_repository_transport,
1152
                              identifiable_format)
1153
        except errors.IncompatibleFormat:
1154
            found_transport = dir.get_repository_transport(identifiable_format)
1155
        self.assertTrue(isinstance(found_transport, transport.Transport))
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
1156
        # and the dir which has been initialized for us must exist.
1157
        found_transport.list_dir('.')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1158
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1159
    def test_get_workingtree_transport(self):
1160
        dir = self.make_bzrdir('.')
1161
        # without a format, get_workingtree_transport gives use a transport
1162
        # which -may- point to an existing dir.
1163
        self.assertTrue(isinstance(dir.get_workingtree_transport(None),
1164
                                   transport.Transport))
1165
        # with a given format, either the bzr dir supports identifiable
1166
        # trees, or it supports anonymous tree formats, but not both.
1167
        anonymous_format = workingtree.WorkingTreeFormat2()
1168
        identifiable_format = workingtree.WorkingTreeFormat3()
1169
        try:
1170
            found_transport = dir.get_workingtree_transport(anonymous_format)
1171
            self.assertRaises(errors.IncompatibleFormat,
1172
                              dir.get_workingtree_transport,
1173
                              identifiable_format)
1174
        except errors.IncompatibleFormat:
1175
            found_transport = dir.get_workingtree_transport(identifiable_format)
1176
        self.assertTrue(isinstance(found_transport, transport.Transport))
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
1177
        # and the dir which has been initialized for us must exist.
1178
        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.
1179
1180
    def test_root_transport(self):
1181
        dir = self.make_bzrdir('.')
1182
        self.assertEqual(dir.root_transport.base,
1183
                         get_transport(self.get_url('.')).base)
1184
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.
1185
    def test_find_repository_no_repo_under_standalone_branch(self):
1186
        # finding a repo stops at standalone branches even if there is a
1187
        # higher repository available.
1188
        try:
1189
            repo = self.make_repository('.', shared=True)
1190
        except errors.IncompatibleFormat:
1191
            # need a shared repository to test this.
1192
            return
1193
        url = self.get_url('intermediate')
1194
        get_transport(self.get_url()).mkdir('intermediate')
1195
        get_transport(self.get_url()).mkdir('intermediate/child')
1196
        made_control = self.bzrdir_format.initialize(url)
1197
        made_control.create_repository()
1198
        innermost_control = self.bzrdir_format.initialize(
1199
            self.get_url('intermediate/child'))
1200
        try:
1201
            child_repo = innermost_control.open_repository()
1202
            # if there is a repository, then the format cannot ever hit this 
1203
            # code path.
1204
            return
1205
        except errors.NoRepositoryPresent:
1206
            pass
1207
        self.assertRaises(errors.NoRepositoryPresent,
1208
                          innermost_control.find_repository)
1209
1210
    def test_find_repository_containing_shared_repository(self):
1211
        # find repo inside a shared repo with an empty control dir
1212
        # returns the shared repo.
1213
        try:
1214
            repo = self.make_repository('.', shared=True)
1215
        except errors.IncompatibleFormat:
1216
            # need a shared repository to test this.
1217
            return
1218
        url = self.get_url('childbzrdir')
1219
        get_transport(self.get_url()).mkdir('childbzrdir')
1220
        made_control = self.bzrdir_format.initialize(url)
1221
        try:
1222
            child_repo = made_control.open_repository()
1223
            # if there is a repository, then the format cannot ever hit this 
1224
            # code path.
1225
            return
1226
        except errors.NoRepositoryPresent:
1227
            pass
1228
        found_repo = made_control.find_repository()
1229
        self.assertEqual(repo.bzrdir.root_transport.base,
1230
                         found_repo.bzrdir.root_transport.base)
1231
        
1232
    def test_find_repository_standalone_with_containing_shared_repository(self):
1233
        # find repo inside a standalone repo inside a shared repo finds the standalone repo
1234
        try:
1235
            containing_repo = self.make_repository('.', shared=True)
1236
        except errors.IncompatibleFormat:
1237
            # need a shared repository to test this.
1238
            return
1239
        child_repo = self.make_repository('childrepo')
1240
        opened_control = bzrdir.BzrDir.open(self.get_url('childrepo'))
1241
        found_repo = opened_control.find_repository()
1242
        self.assertEqual(child_repo.bzrdir.root_transport.base,
1243
                         found_repo.bzrdir.root_transport.base)
1244
1245
    def test_find_repository_shared_within_shared_repository(self):
1246
        # find repo at a shared repo inside a shared repo finds the inner repo
1247
        try:
1248
            containing_repo = self.make_repository('.', shared=True)
1249
        except errors.IncompatibleFormat:
1250
            # need a shared repository to test this.
1251
            return
1252
        url = self.get_url('childrepo')
1253
        get_transport(self.get_url()).mkdir('childrepo')
1254
        child_control = self.bzrdir_format.initialize(url)
1255
        child_repo = child_control.create_repository(shared=True)
1256
        opened_control = bzrdir.BzrDir.open(self.get_url('childrepo'))
1257
        found_repo = opened_control.find_repository()
1258
        self.assertEqual(child_repo.bzrdir.root_transport.base,
1259
                         found_repo.bzrdir.root_transport.base)
1260
        self.assertNotEqual(child_repo.bzrdir.root_transport.base,
1261
                            containing_repo.bzrdir.root_transport.base)
1262
1263
    def test_find_repository_with_nested_dirs_works(self):
1264
        # find repo inside a bzrdir inside a bzrdir inside a shared repo 
1265
        # finds the outer shared repo.
1266
        try:
1267
            repo = self.make_repository('.', shared=True)
1268
        except errors.IncompatibleFormat:
1269
            # need a shared repository to test this.
1270
            return
1271
        url = self.get_url('intermediate')
1272
        get_transport(self.get_url()).mkdir('intermediate')
1273
        get_transport(self.get_url()).mkdir('intermediate/child')
1274
        made_control = self.bzrdir_format.initialize(url)
1275
        try:
1276
            child_repo = made_control.open_repository()
1277
            # if there is a repository, then the format cannot ever hit this 
1278
            # code path.
1279
            return
1280
        except errors.NoRepositoryPresent:
1281
            pass
1282
        innermost_control = self.bzrdir_format.initialize(
1283
            self.get_url('intermediate/child'))
1284
        try:
1285
            child_repo = innermost_control.open_repository()
1286
            # if there is a repository, then the format cannot ever hit this 
1287
            # code path.
1288
            return
1289
        except errors.NoRepositoryPresent:
1290
            pass
1291
        found_repo = innermost_control.find_repository()
1292
        self.assertEqual(repo.bzrdir.root_transport.base,
1293
                         found_repo.bzrdir.root_transport.base)
1294
        
1534.5.16 by Robert Collins
Review feedback.
1295
    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.
1296
        # check that we can ask an instance if its upgradable
1297
        dir = self.make_bzrdir('.')
1534.5.16 by Robert Collins
Review feedback.
1298
        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.
1299
            # if its default updatable there must be an updater 
2204.4.14 by Aaron Bentley
lastest -> latest
1300
            # (we force the latest known format as downgrades may not be
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1301
            # available
1302
            self.assertTrue(isinstance(dir._format.get_converter(
1303
                format=dir._format), bzrdir.Converter))
1534.5.16 by Robert Collins
Review feedback.
1304
        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.
1305
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1306
    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.
1307
        """Does an available updater work?"""
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1308
        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.
1309
        # 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.
1310
        dir.create_repository()
1311
        dir.create_branch()
1910.4.13 by Andrew Bennetts
Slightly more consistent names.
1312
        self.createWorkingTreeOrSkip(dir)
1534.5.16 by Robert Collins
Review feedback.
1313
        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.
1314
            # if its default updatable there must be an updater 
2204.4.14 by Aaron Bentley
lastest -> latest
1315
            # (we force the latest known format as downgrades may not be
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1316
            # available
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1317
            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.
1318
            try:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1319
                dir._format.get_converter(format=dir._format).convert(dir, pb)
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1320
            finally:
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1321
                pb.finished()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1322
            # and it should pass 'check' now.
1323
            check(bzrdir.BzrDir.open(self.get_url('.')).open_branch(), False)
1324
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1325
    def test_format_description(self):
1326
        dir = self.make_bzrdir('.')
1327
        text = dir._format.get_format_description()
1328
        self.failUnless(len(text))
1329
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.
1330
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1331
class TestBreakLock(TestCaseWithBzrDir):
1332
1333
    def setUp(self):
1334
        super(TestBreakLock, self).setUp()
1335
        # we want a UI factory that accepts canned input for the tests:
1336
        # while SilentUIFactory still accepts stdin, we need to customise
1337
        # ours
1338
        self.old_factory = bzrlib.ui.ui_factory
1687.1.15 by Robert Collins
Review comments.
1339
        self.addCleanup(self.restoreFactory)
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1340
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1341
1687.1.15 by Robert Collins
Review comments.
1342
    def restoreFactory(self):
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1343
        bzrlib.ui.ui_factory = self.old_factory
1344
1345
    def test_break_lock_empty(self):
1346
        # break lock on an empty bzrdir should work silently.
1347
        dir = self.make_bzrdir('.')
1348
        try:
1349
            dir.break_lock()
1350
        except NotImplementedError:
1351
            pass
1352
1353
    def test_break_lock_repository(self):
1354
        # break lock with just a repo should unlock the repo.
1355
        repo = self.make_repository('.')
1356
        repo.lock_write()
1357
        # only one yes needed here: it should only be unlocking
1358
        # the repo
1359
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1360
        try:
1361
            repo.bzrdir.break_lock()
1362
        except NotImplementedError:
1363
            # this bzrdir does not implement break_lock - so we cant test it.
1364
            repo.unlock()
1365
            return
1366
        lock_repo = repo.bzrdir.open_repository()
1367
        lock_repo.lock_write()
1368
        lock_repo.unlock()
1369
        self.assertRaises(errors.LockBroken, repo.unlock)
1370
1371
    def test_break_lock_branch(self):
1372
        # break lock with just a repo should unlock the branch.
1373
        # and not directly try the repository.
1374
        # we test this by making a branch reference to a branch
1375
        # and repository in another bzrdir
1376
        # for pre-metadir formats this will fail, thats ok.
1377
        master = self.make_branch('branch')
1378
        thisdir = self.make_bzrdir('this')
1379
        try:
1380
            bzrlib.branch.BranchReferenceFormat().initialize(
1381
                thisdir, master)
1382
        except errors.IncompatibleFormat:
1383
            return
1384
        unused_repo = thisdir.create_repository()
1385
        master.lock_write()
1386
        unused_repo.lock_write()
1957.1.17 by John Arbash Meinel
Change tests that expect locking to fail to timeout sooner.
1387
        try:
1388
            # two yes's : branch and repository. If the repo in this
1389
            # dir is inappropriately accessed, 3 will be needed, and
1390
            # we'll see that because the stream will be fully consumed
1391
            bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\n")
1392
            master.bzrdir.break_lock()
1393
            # only two ys should have been read
1394
            self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1395
            # we should be able to lock a newly opened branch now
1396
            branch = master.bzrdir.open_branch()
1397
            branch.lock_write()
1398
            branch.unlock()
1399
            # we should not be able to lock the repository in thisdir as its still
1400
            # held by the explicit lock we took, and the break lock should not have
1401
            # touched it.
1402
            repo = thisdir.open_repository()
1403
            orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
1404
            try:
1405
                lockdir._DEFAULT_TIMEOUT_SECONDS = 1
1406
                self.assertRaises(errors.LockContention, repo.lock_write)
1407
            finally:
1408
                lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
1409
        finally:
1410
            unused_repo.unlock()
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
1411
        self.assertRaises(errors.LockBroken, master.unlock)
1412
1413
    def test_break_lock_tree(self):
1414
        # break lock with a tree should unlock the tree but not try the 
1415
        # branch explicitly. However this is very hard to test for as we 
1416
        # dont have a tree reference class, nor is one needed; 
1417
        # the worst case if this code unlocks twice is an extra question
1418
        # being asked.
1419
        tree = self.make_branch_and_tree('.')
1420
        tree.lock_write()
1421
        # three yes's : tree, branch and repository.
1422
        bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\ny\n")
1423
        try:
1424
            tree.bzrdir.break_lock()
1425
        except NotImplementedError:
1426
            # bzrdir does not support break_lock
1427
            tree.unlock()
1428
            return
1429
        self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
1430
        lock_tree = tree.bzrdir.open_workingtree()
1431
        lock_tree.lock_write()
1432
        lock_tree.unlock()
1433
        self.assertRaises(errors.LockBroken, tree.unlock)
1434
1435
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.
1436
class ChrootedBzrDirTests(ChrootedTestCase):
1437
1438
    def test_find_repository_no_repository(self):
1439
        # loopback test to check the current format fails to find a 
1440
        # share repository correctly.
1441
        if not self.bzrdir_format.is_supported():
1442
            # unsupported formats are not loopback testable
1443
            # because the default open will not open them and
1444
            # they may not be initializable.
1445
            return
1446
        # supported formats must be able to init and open
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1447
        # - do the vfs initialisation over the basic vfs transport
1448
        # XXX: TODO this should become a 'bzrdirlocation' api call.
1449
        url = self.get_vfs_only_url('subdir')
1450
        get_transport(self.get_vfs_only_url()).mkdir('subdir')
1451
        made_control = self.bzrdir_format.initialize(self.get_url('subdir'))
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.
1452
        try:
1453
            repo = made_control.open_repository()
1454
            # if there is a repository, then the format cannot ever hit this 
1455
            # code path.
1456
            return
1457
        except errors.NoRepositoryPresent:
1458
            pass
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1459
        made_control = bzrdir.BzrDir.open(self.get_readonly_url('subdir'))
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.
1460
        self.assertRaises(errors.NoRepositoryPresent,
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1461
                          made_control.find_repository)
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
1462