/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2010, 2011, 2012, 2016 Canonical Ltd
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
2
#
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.
7
#
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.
12
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for bzrdir implementations - tests a bzrdir format."""
18
19
import errno
20
from stat import S_ISDIR
21
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
22
import breezy.branch
23
from breezy import (
6472.2.1 by Jelmer Vernooij
Use bzrdir.controldir for generic access to control directories.
24
    controldir,
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
25
    errors,
5699.4.7 by Jelmer Vernooij
Fix imports.
26
    repository,
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
27
    revision as _mod_revision,
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
28
    transport,
5699.4.7 by Jelmer Vernooij
Fix imports.
29
    workingtree,
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
30
    )
6670.4.3 by Jelmer Vernooij
Fix more imports.
31
from breezy.bzr import (
32
    bzrdir,
33
    )
6670.4.14 by Jelmer Vernooij
Move remote to breezy.bzr.
34
from breezy.bzr.remote import RemoteBzrDirFormat
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
35
from breezy.tests import (
6164.2.1 by Jelmer Vernooij
Skip tests if the repository doesn't support ghosts.
36
    TestNotApplicable,
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
37
    TestSkipped,
38
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
39
from breezy.tests.per_bzrdir import TestCaseWithBzrDir
40
from breezy.transport.local import (
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
41
    LocalTransport,
42
    )
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
43
44
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
45
class AnonymousTestBranchFormat(breezy.branch.BranchFormat):
5699.4.7 by Jelmer Vernooij
Fix imports.
46
    """An anonymous branch format (does not have a format string)"""
47
48
    def get_format_string(self):
49
        raise NotImplementedError(self.get_format_string)
50
51
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
52
class IdentifiableTestBranchFormat(breezy.branch.BranchFormat):
5699.4.7 by Jelmer Vernooij
Fix imports.
53
    """An identifable branch format (has a format string)"""
54
55
    def get_format_string(self):
56
        return "I have an identity"
57
58
59
class AnonymousTestRepositoryFormat(repository.RepositoryFormat):
60
    """An anonymous branch format (does not have a format string)"""
61
62
    def get_format_string(self):
63
        raise NotImplementedError(self.get_format_string)
64
65
66
class IdentifiableTestRepositoryFormat(repository.RepositoryFormat):
67
    """An identifable branch format (has a format string)"""
68
69
    def get_format_string(self):
70
        return "I have an identity"
71
72
73
class AnonymousTestWorkingTreeFormat(workingtree.WorkingTreeFormat):
74
    """An anonymous branch format (does not have a format string)"""
75
76
    def get_format_string(self):
77
        raise NotImplementedError(self.get_format_string)
78
79
80
class IdentifiableTestWorkingTreeFormat(workingtree.WorkingTreeFormat):
81
    """An identifable branch format (has a format string)"""
82
83
    def get_format_string(self):
84
        return "I have an identity"
85
86
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
87
class TestBzrDir(TestCaseWithBzrDir):
88
89
    # Many of these tests test for disk equality rather than checking
90
    # for semantic equivalence. This works well for some tests but
91
    # is not good at handling changes in representation or the addition
92
    # or removal of control data. It would be nice to for instance:
93
    # sprout a new branch, check that the nickname has been reset by hand
94
    # and then set the nickname to match the source branch, at which point
95
    # a semantic equivalence should pass
96
97
    def assertDirectoriesEqual(self, source, target, ignore_list=[]):
98
        """Assert that the content of source and target are identical.
99
100
        paths in ignore list will be completely ignored.
101
102
        We ignore paths that represent data which is allowed to change during
103
        a clone or sprout: for instance, inventory.knit contains gzip fragements
104
        which have timestamps in them, and as we have read the inventory from
105
        the source knit, the already-read data is recompressed rather than
106
        reading it again, which leads to changed timestamps. This is ok though,
107
        because the inventory.kndx file is not ignored, and the integrity of
108
        knit joins is tested by test_knit and test_versionedfile.
109
110
        :seealso: Additionally, assertRepositoryHasSameItems provides value
111
            rather than representation checking of repositories for
112
            equivalence.
113
        """
114
        files = []
115
        directories = ['.']
116
        while directories:
117
            dir = directories.pop()
118
            for path in set(source.list_dir(dir) + target.list_dir(dir)):
119
                path = dir + '/' + path
120
                if path in ignore_list:
121
                    continue
122
                try:
123
                    stat = source.stat(path)
124
                except errors.NoSuchFile:
125
                    self.fail('%s not in source' % path)
126
                if S_ISDIR(stat.st_mode):
127
                    self.assertTrue(S_ISDIR(target.stat(path).st_mode))
128
                    directories.append(path)
129
                else:
6437.20.5 by Wouter van Heyst
express intent more pithily with transport.get_bytes
130
                    self.assertEqualDiff(source.get_bytes(path),
131
                                         target.get_bytes(path),
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
132
                                         "text for file %r differs:\n" % path)
133
134
    def assertRepositoryHasSameItems(self, left_repo, right_repo):
135
        """require left_repo and right_repo to contain the same data."""
136
        # XXX: TODO: Doesn't work yet, because we need to be able to compare
137
        # local repositories to remote ones...  but this is an as-yet unsolved
138
        # aspect of format management and the Remote protocols...
139
        # self.assertEqual(left_repo._format.__class__,
140
        #     right_repo._format.__class__)
141
        left_repo.lock_read()
142
        try:
143
            right_repo.lock_read()
144
            try:
145
                # revs
146
                all_revs = left_repo.all_revision_ids()
147
                self.assertEqual(left_repo.all_revision_ids(),
148
                    right_repo.all_revision_ids())
149
                for rev_id in left_repo.all_revision_ids():
150
                    self.assertEqual(left_repo.get_revision(rev_id),
151
                        right_repo.get_revision(rev_id))
152
                # Assert the revision trees (and thus the inventories) are equal
153
                sort_key = lambda rev_tree: rev_tree.get_revision_id()
154
                rev_trees_a = sorted(
155
                    left_repo.revision_trees(all_revs), key=sort_key)
156
                rev_trees_b = sorted(
157
                    right_repo.revision_trees(all_revs), key=sort_key)
158
                for tree_a, tree_b in zip(rev_trees_a, rev_trees_b):
159
                    self.assertEqual([], list(tree_a.iter_changes(tree_b)))
160
                # texts
161
                text_index = left_repo._generate_text_key_index()
162
                self.assertEqual(text_index,
163
                    right_repo._generate_text_key_index())
164
                desired_files = []
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
165
                for file_id, revision_id in text_index:
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
166
                    desired_files.append(
167
                        (file_id, revision_id, (file_id, revision_id)))
6280.10.21 by Jelmer Vernooij
Consume iter_files_bytes contents directly.
168
                left_texts = [(identifier, "".join(bytes_iterator)) for
169
                        (identifier, bytes_iterator) in
170
                        left_repo.iter_files_bytes(desired_files)]
171
                right_texts = [(identifier, "".join(bytes_iterator)) for
172
                        (identifier, bytes_iterator) in
173
                        right_repo.iter_files_bytes(desired_files)]
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
174
                left_texts.sort()
175
                right_texts.sort()
176
                self.assertEqual(left_texts, right_texts)
177
                # signatures
178
                for rev_id in all_revs:
179
                    try:
180
                        left_text = left_repo.get_signature_text(rev_id)
181
                    except errors.NoSuchRevision:
182
                        continue
183
                    right_text = right_repo.get_signature_text(rev_id)
184
                    self.assertEqual(left_text, right_text)
185
            finally:
186
                right_repo.unlock()
187
        finally:
188
            left_repo.unlock()
189
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
190
    def sproutOrSkip(self, from_bzrdir, to_url, revision_id=None,
191
                     force_new_repo=False, accelerator_tree=None,
192
                     create_tree_if_local=True):
193
        """Sprout from_bzrdir into to_url, or raise TestSkipped.
194
195
        A simple wrapper for from_bzrdir.sprout that translates NotLocalUrl into
196
        TestSkipped.  Returns the newly sprouted bzrdir.
197
        """
198
        to_transport = transport.get_transport(to_url)
199
        if not isinstance(to_transport, LocalTransport):
200
            raise TestSkipped('Cannot sprout to remote bzrdirs.')
201
        target = from_bzrdir.sprout(to_url, revision_id=revision_id,
202
                                    force_new_repo=force_new_repo,
203
                                    possible_transports=[to_transport],
204
                                    accelerator_tree=accelerator_tree,
205
                                    create_tree_if_local=create_tree_if_local)
206
        return target
207
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
208
    def skipIfNoWorkingTree(self, a_controldir):
209
        """Raises TestSkipped if a_controldir doesn't have a working tree.
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
210
211
        If the bzrdir does have a workingtree, this is a no-op.
212
        """
213
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
214
            a_controldir.open_workingtree()
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
215
        except (errors.NotLocalUrl, errors.NoWorkingTree):
216
            raise TestSkipped("bzrdir on transport %r has no working tree"
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
217
                              % a_controldir.transport)
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
218
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
219
    def createWorkingTreeOrSkip(self, a_controldir):
220
        """Create a working tree on a_controldir, or raise TestSkipped.
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
221
222
        A simple wrapper for create_workingtree that translates NotLocalUrl into
223
        TestSkipped.  Returns the newly created working tree.
224
        """
225
        try:
5042.1.3 by Martin Pool
Update a test for BzrDir.create_workingtree so that it covers bug 524627
226
            # This passes in many named options to make sure they're
227
            # understood by subclasses: see
228
            # <https://bugs.launchpad.net/bzr/+bug/524627>.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
229
            return a_controldir.create_workingtree(
5042.1.3 by Martin Pool
Update a test for BzrDir.create_workingtree so that it covers bug 524627
230
                revision_id=None,
231
                from_branch=None,
232
                accelerator_tree=None,
233
                hardlink=False)
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
234
        except errors.NotLocalUrl:
235
            raise TestSkipped("cannot make working tree with transport %r"
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
236
                              % a_controldir.transport)
5363.2.30 by Jelmer Vernooij
actually run per_bzrdir tests.
237
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
238
    def test_clone_bzrdir_repository_under_shared_force_new_repo(self):
239
        tree = self.make_branch_and_tree('commit_tree')
240
        self.build_tree(['commit_tree/foo'])
241
        tree.add('foo')
242
        tree.commit('revision 1', rev_id='1')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
243
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
244
        repo = dir.create_repository()
245
        repo.fetch(tree.branch.repository)
246
        self.assertTrue(repo.has_revision('1'))
247
        try:
248
            self.make_repository('target', shared=True)
249
        except errors.IncompatibleFormat:
250
            return
251
        target = dir.clone(self.get_url('target/child'), force_new_repo=True)
252
        self.assertNotEqual(dir.transport.base, target.transport.base)
253
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
254
                                    ['./.bzr/repository',
255
                                     ])
256
        self.assertRepositoryHasSameItems(tree.branch.repository, repo)
257
258
    def test_clone_bzrdir_branch_and_repo(self):
259
        tree = self.make_branch_and_tree('commit_tree')
260
        self.build_tree(['commit_tree/foo'])
261
        tree.add('foo')
262
        tree.commit('revision 1')
263
        source = self.make_branch('source')
264
        tree.branch.repository.copy_content_into(source.repository)
265
        tree.branch.copy_content_into(source)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
266
        dir = source.controldir
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
267
        target = dir.clone(self.get_url('target'))
268
        self.assertNotEqual(dir.transport.base, target.transport.base)
269
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
270
                                    [
271
                                     './.bzr/basis-inventory-cache',
272
                                     './.bzr/checkout/stat-cache',
273
                                     './.bzr/merge-hashes',
274
                                     './.bzr/repository',
275
                                     './.bzr/stat-cache',
276
                                    ])
277
        self.assertRepositoryHasSameItems(
278
            tree.branch.repository, target.open_repository())
279
280
    def test_clone_on_transport(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
281
        a_dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
282
        target_transport = a_dir.root_transport.clone('..').clone('target')
283
        target = a_dir.clone_on_transport(target_transport)
284
        self.assertNotEqual(a_dir.transport.base, target.transport.base)
285
        self.assertDirectoriesEqual(a_dir.root_transport, target.root_transport,
286
                                    ['./.bzr/merge-hashes'])
287
288
    def test_clone_bzrdir_empty(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
289
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
290
        target = dir.clone(self.get_url('target'))
291
        self.assertNotEqual(dir.transport.base, target.transport.base)
292
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
293
                                    ['./.bzr/merge-hashes'])
294
295
    def test_clone_bzrdir_empty_force_new_ignored(self):
296
        # the force_new_repo parameter should have no effect on an empty
297
        # bzrdir's clone logic
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
298
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
299
        target = dir.clone(self.get_url('target'), force_new_repo=True)
300
        self.assertNotEqual(dir.transport.base, target.transport.base)
301
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
302
                                    ['./.bzr/merge-hashes'])
303
304
    def test_clone_bzrdir_repository(self):
305
        tree = self.make_branch_and_tree('commit_tree')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
306
        self.build_tree(['foo'], transport=tree.controldir.transport.clone('..'))
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
307
        tree.add('foo')
308
        tree.commit('revision 1', rev_id='1')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
309
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
310
        repo = dir.create_repository()
311
        repo.fetch(tree.branch.repository)
312
        self.assertTrue(repo.has_revision('1'))
313
        target = dir.clone(self.get_url('target'))
314
        self.assertNotEqual(dir.transport.base, target.transport.base)
315
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
316
                                    [
317
                                     './.bzr/merge-hashes',
318
                                     './.bzr/repository',
319
                                     ])
320
        self.assertRepositoryHasSameItems(tree.branch.repository,
321
            target.open_repository())
322
323
    def test_clone_bzrdir_tree_branch_repo(self):
324
        tree = self.make_branch_and_tree('source')
325
        self.build_tree(['source/foo'])
326
        tree.add('foo')
327
        tree.commit('revision 1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
328
        dir = tree.controldir
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
329
        target = dir.clone(self.get_url('target'))
330
        self.skipIfNoWorkingTree(target)
331
        self.assertNotEqual(dir.transport.base, target.transport.base)
332
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
333
                                    ['./.bzr/stat-cache',
334
                                     './.bzr/checkout/dirstate',
335
                                     './.bzr/checkout/stat-cache',
336
                                     './.bzr/checkout/merge-hashes',
337
                                     './.bzr/merge-hashes',
338
                                     './.bzr/repository',
339
                                     ])
340
        self.assertRepositoryHasSameItems(tree.branch.repository,
6182.1.9 by Jelmer Vernooij
More test fixes.
341
            target.open_branch().repository)
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
342
        target.open_workingtree().revert()
343
344
    def test_revert_inventory(self):
345
        tree = self.make_branch_and_tree('source')
346
        self.build_tree(['source/foo'])
347
        tree.add('foo')
348
        tree.commit('revision 1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
349
        dir = tree.controldir
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
350
        target = dir.clone(self.get_url('target'))
351
        self.skipIfNoWorkingTree(target)
352
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
353
                                    ['./.bzr/stat-cache',
354
                                     './.bzr/checkout/dirstate',
355
                                     './.bzr/checkout/stat-cache',
356
                                     './.bzr/checkout/merge-hashes',
357
                                     './.bzr/merge-hashes',
358
                                     './.bzr/repository',
359
                                     ])
360
        self.assertRepositoryHasSameItems(tree.branch.repository,
6182.1.9 by Jelmer Vernooij
More test fixes.
361
            target.open_branch().repository)
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
362
363
        target.open_workingtree().revert()
364
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
365
                                    ['./.bzr/stat-cache',
366
                                     './.bzr/checkout/dirstate',
367
                                     './.bzr/checkout/stat-cache',
368
                                     './.bzr/checkout/merge-hashes',
369
                                     './.bzr/merge-hashes',
370
                                     './.bzr/repository',
371
                                     ])
372
        self.assertRepositoryHasSameItems(tree.branch.repository,
6182.1.9 by Jelmer Vernooij
More test fixes.
373
            target.open_branch().repository)
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
374
375
    def test_clone_bzrdir_tree_branch_reference(self):
376
        # a tree with a branch reference (aka a checkout)
377
        # should stay a checkout on clone.
378
        referenced_branch = self.make_branch('referencced')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
379
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
380
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
381
            dir.set_branch_reference(referenced_branch)
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
382
        except errors.IncompatibleFormat:
383
            # this is ok too, not all formats have to support references.
384
            return
385
        self.createWorkingTreeOrSkip(dir)
386
        target = dir.clone(self.get_url('target'))
387
        self.skipIfNoWorkingTree(target)
388
        self.assertNotEqual(dir.transport.base, target.transport.base)
389
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
390
                                    ['./.bzr/stat-cache',
391
                                     './.bzr/checkout/stat-cache',
392
                                     './.bzr/checkout/merge-hashes',
393
                                     './.bzr/merge-hashes',
394
                                     './.bzr/repository/inventory.knit',
395
                                     ])
396
397
    def test_clone_bzrdir_branch_and_repo_into_shared_repo_force_new_repo(self):
398
        # by default cloning into a shared repo uses the shared repo.
399
        tree = self.make_branch_and_tree('commit_tree')
400
        self.build_tree(['commit_tree/foo'])
401
        tree.add('foo')
402
        tree.commit('revision 1')
403
        source = self.make_branch('source')
404
        tree.branch.repository.copy_content_into(source.repository)
405
        tree.branch.copy_content_into(source)
406
        try:
407
            self.make_repository('target', shared=True)
408
        except errors.IncompatibleFormat:
409
            return
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
410
        dir = source.controldir
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
411
        target = dir.clone(self.get_url('target/child'), force_new_repo=True)
412
        self.assertNotEqual(dir.transport.base, target.transport.base)
413
        repo = target.open_repository()
414
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
415
                                    ['./.bzr/repository',
416
                                     ])
417
        self.assertRepositoryHasSameItems(tree.branch.repository, repo)
418
419
    def test_clone_bzrdir_branch_reference(self):
420
        # cloning should preserve the reference status of the branch in a bzrdir
421
        referenced_branch = self.make_branch('referencced')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
422
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
423
        try:
6437.7.3 by Jelmer Vernooij
Use ControlDir.set_branch_reference.
424
            dir.set_branch_reference(referenced_branch)
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
425
        except errors.IncompatibleFormat:
426
            # this is ok too, not all formats have to support references.
427
            return
428
        target = dir.clone(self.get_url('target'))
429
        self.assertNotEqual(dir.transport.base, target.transport.base)
430
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport)
431
432
    def test_sprout_bzrdir_repository(self):
433
        tree = self.make_branch_and_tree('commit_tree')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
434
        self.build_tree(['foo'], transport=tree.controldir.transport.clone('..'))
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
435
        tree.add('foo')
436
        tree.commit('revision 1', rev_id='1')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
437
        dir = self.make_controldir('source')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
438
        repo = dir.create_repository()
439
        repo.fetch(tree.branch.repository)
440
        self.assertTrue(repo.has_revision('1'))
441
        try:
442
            self.assertTrue(
443
                _mod_revision.is_null(_mod_revision.ensure_null(
444
                dir.open_branch().last_revision())))
445
        except errors.NotBranchError:
446
            pass
447
        target = dir.sprout(self.get_url('target'))
448
        self.assertNotEqual(dir.transport.base, target.transport.base)
449
        # testing inventory isn't reasonable for repositories
450
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
451
                                    [
452
                                     './.bzr/branch',
453
                                     './.bzr/checkout',
454
                                     './.bzr/inventory',
455
                                     './.bzr/parent',
456
                                     './.bzr/repository/inventory.knit',
457
                                     ])
458
        try:
459
            local_inventory = dir.transport.local_abspath('inventory')
460
        except errors.NotLocalUrl:
461
            return
462
        try:
463
            # If we happen to have a tree, we'll guarantee everything
464
            # except for the tree root is the same.
465
            inventory_f = file(local_inventory, 'rb')
466
            self.addCleanup(inventory_f.close)
467
            self.assertContainsRe(inventory_f.read(),
468
                                  '<inventory format="5">\n</inventory>\n')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
469
        except IOError as e:
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
470
            if e.errno != errno.ENOENT:
471
                raise
472
473
    def test_sprout_bzrdir_branch_and_repo(self):
474
        tree = self.make_branch_and_tree('commit_tree')
475
        self.build_tree(['commit_tree/foo'])
476
        tree.add('foo')
477
        tree.commit('revision 1')
478
        source = self.make_branch('source')
479
        tree.branch.repository.copy_content_into(source.repository)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
480
        tree.controldir.open_branch().copy_content_into(source)
481
        dir = source.controldir
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
482
        target = dir.sprout(self.get_url('target'))
483
        self.assertNotEqual(dir.transport.base, target.transport.base)
484
        target_repo = target.open_repository()
485
        self.assertRepositoryHasSameItems(source.repository, target_repo)
486
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
487
                                    [
488
                                     './.bzr/basis-inventory-cache',
489
                                     './.bzr/branch/branch.conf',
490
                                     './.bzr/branch/parent',
491
                                     './.bzr/checkout',
492
                                     './.bzr/checkout/inventory',
493
                                     './.bzr/checkout/stat-cache',
494
                                     './.bzr/inventory',
495
                                     './.bzr/parent',
496
                                     './.bzr/repository',
497
                                     './.bzr/stat-cache',
498
                                     './foo',
499
                                     ])
500
501
    def test_sprout_bzrdir_tree_branch_repo(self):
502
        tree = self.make_branch_and_tree('source')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
503
        self.build_tree(['foo'], transport=tree.controldir.transport.clone('..'))
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
504
        tree.add('foo')
505
        tree.commit('revision 1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
506
        dir = tree.controldir
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
507
        target = self.sproutOrSkip(dir, self.get_url('target'))
508
        self.assertNotEqual(dir.transport.base, target.transport.base)
509
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
510
                                    [
6182.1.10 by Jelmer Vernooij
Fix test.
511
                                     './.bzr/branch',
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
512
                                     './.bzr/checkout/dirstate',
513
                                     './.bzr/checkout/stat-cache',
514
                                     './.bzr/checkout/inventory',
515
                                     './.bzr/inventory',
516
                                     './.bzr/parent',
517
                                     './.bzr/repository',
518
                                     './.bzr/stat-cache',
519
                                     ])
520
        self.assertRepositoryHasSameItems(
521
            tree.branch.repository, target.open_repository())
522
523
524
    def test_retire_bzrdir(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
525
        bd = self.make_controldir('.')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
526
        transport = bd.root_transport
527
        # must not overwrite existing directories
528
        self.build_tree(['.bzr.retired.0/', '.bzr.retired.0/junk',],
529
            transport=transport)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
530
        self.assertTrue(transport.has('.bzr'))
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
531
        bd.retire_bzrdir()
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
532
        self.assertFalse(transport.has('.bzr'))
533
        self.assertTrue(transport.has('.bzr.retired.1'))
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
534
535
    def test_retire_bzrdir_limited(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
536
        bd = self.make_controldir('.')
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
537
        transport = bd.root_transport
538
        # must not overwrite existing directories
539
        self.build_tree(['.bzr.retired.0/', '.bzr.retired.0/junk',],
540
            transport=transport)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
541
        self.assertTrue(transport.has('.bzr'))
5363.2.29 by Jelmer Vernooij
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.
542
        self.assertRaises((errors.FileExists, errors.DirectoryNotEmpty),
543
            bd.retire_bzrdir, limit=0)
5699.4.1 by Jelmer Vernooij
Move get_branch_transport, .get_workingtree_transport and .get_repository_transport
544
545
    def test_get_branch_transport(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
546
        dir = self.make_controldir('.')
5699.4.1 by Jelmer Vernooij
Move get_branch_transport, .get_workingtree_transport and .get_repository_transport
547
        # without a format, get_branch_transport gives use a transport
548
        # which -may- point to an existing dir.
549
        self.assertTrue(isinstance(dir.get_branch_transport(None),
550
                                   transport.Transport))
551
        # with a given format, either the bzr dir supports identifiable
552
        # branches, or it supports anonymous branch formats, but not both.
553
        anonymous_format = AnonymousTestBranchFormat()
554
        identifiable_format = IdentifiableTestBranchFormat()
555
        try:
556
            found_transport = dir.get_branch_transport(anonymous_format)
557
            self.assertRaises(errors.IncompatibleFormat,
558
                              dir.get_branch_transport,
559
                              identifiable_format)
560
        except errors.IncompatibleFormat:
561
            found_transport = dir.get_branch_transport(identifiable_format)
562
        self.assertTrue(isinstance(found_transport, transport.Transport))
563
        # and the dir which has been initialized for us must exist.
564
        found_transport.list_dir('.')
565
566
    def test_get_repository_transport(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
567
        dir = self.make_controldir('.')
5699.4.1 by Jelmer Vernooij
Move get_branch_transport, .get_workingtree_transport and .get_repository_transport
568
        # without a format, get_repository_transport gives use a transport
569
        # which -may- point to an existing dir.
570
        self.assertTrue(isinstance(dir.get_repository_transport(None),
571
                                   transport.Transport))
572
        # with a given format, either the bzr dir supports identifiable
573
        # repositories, or it supports anonymous repository formats, but not both.
574
        anonymous_format = AnonymousTestRepositoryFormat()
575
        identifiable_format = IdentifiableTestRepositoryFormat()
576
        try:
577
            found_transport = dir.get_repository_transport(anonymous_format)
578
            self.assertRaises(errors.IncompatibleFormat,
579
                              dir.get_repository_transport,
580
                              identifiable_format)
581
        except errors.IncompatibleFormat:
582
            found_transport = dir.get_repository_transport(identifiable_format)
583
        self.assertTrue(isinstance(found_transport, transport.Transport))
584
        # and the dir which has been initialized for us must exist.
585
        found_transport.list_dir('.')
586
587
    def test_get_workingtree_transport(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
588
        dir = self.make_controldir('.')
5699.4.1 by Jelmer Vernooij
Move get_branch_transport, .get_workingtree_transport and .get_repository_transport
589
        # without a format, get_workingtree_transport gives use a transport
590
        # which -may- point to an existing dir.
591
        self.assertTrue(isinstance(dir.get_workingtree_transport(None),
592
                                   transport.Transport))
593
        # with a given format, either the bzr dir supports identifiable
594
        # trees, or it supports anonymous tree formats, but not both.
595
        anonymous_format = AnonymousTestWorkingTreeFormat()
596
        identifiable_format = IdentifiableTestWorkingTreeFormat()
597
        try:
598
            found_transport = dir.get_workingtree_transport(anonymous_format)
599
            self.assertRaises(errors.IncompatibleFormat,
600
                              dir.get_workingtree_transport,
601
                              identifiable_format)
602
        except errors.IncompatibleFormat:
603
            found_transport = dir.get_workingtree_transport(identifiable_format)
604
        self.assertTrue(isinstance(found_transport, transport.Transport))
605
        # and the dir which has been initialized for us must exist.
606
        found_transport.list_dir('.')
6164.2.5 by Jelmer Vernooij
Move some stacking tests specific to bzr formats to per_bzrdir.
607
608
    def assertInitializeEx(self, t, need_meta=False, **kwargs):
609
        """Execute initialize_on_transport_ex and check it succeeded correctly.
610
611
        This involves checking that the disk objects were created, open with
612
        the same format returned, and had the expected disk format.
613
614
        :param t: The transport to initialize on.
615
        :param **kwargs: Additional arguments to pass to
616
            initialize_on_transport_ex.
617
        :return: the resulting repo, control dir tuple.
618
        """
619
        if not self.bzrdir_format.is_initializable():
620
            raise TestNotApplicable("control dir format is not "
621
                "initializable")
622
        repo, control, require_stacking, repo_policy = \
623
            self.bzrdir_format.initialize_on_transport_ex(t, **kwargs)
624
        if repo is not None:
625
            # Repositories are open write-locked
626
            self.assertTrue(repo.is_write_locked())
627
            self.addCleanup(repo.unlock)
628
        self.assertIsInstance(control, bzrdir.BzrDir)
629
        opened = bzrdir.BzrDir.open(t.base)
630
        expected_format = self.bzrdir_format
631
        if need_meta and expected_format.fixed_components:
632
            # Pre-metadir formats change when we are making something that
633
            # needs a metaformat, because clone is used for push.
634
            expected_format = bzrdir.BzrDirMetaFormat1()
635
        if not isinstance(expected_format, RemoteBzrDirFormat):
636
            self.assertEqual(control._format.network_name(),
637
                expected_format.network_name())
638
            self.assertEqual(control._format.network_name(),
639
                opened._format.network_name())
640
        self.assertEqual(control.__class__, opened.__class__)
641
        return repo, control
642
643
    def test_format_initialize_on_transport_ex_default_stack_on(self):
644
        # When initialize_on_transport_ex uses a stacked-on branch because of
645
        # a stacking policy on the target, the location of the fallback
646
        # repository is the same as the external location of the stacked-on
647
        # branch.
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
648
        balloon = self.make_controldir('balloon')
6164.2.5 by Jelmer Vernooij
Move some stacking tests specific to bzr formats to per_bzrdir.
649
        if isinstance(balloon._format, bzrdir.BzrDirMetaFormat1):
650
            stack_on = self.make_branch('stack-on', format='1.9')
651
        else:
652
            stack_on = self.make_branch('stack-on')
6164.2.10 by Jelmer Vernooij
Merge more-foreign-tests-fixes-7
653
        if not stack_on.repository._format.supports_nesting_repositories:
654
            raise TestNotApplicable("requires nesting repositories")
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
655
        config = self.make_controldir('.').get_config()
6164.2.5 by Jelmer Vernooij
Move some stacking tests specific to bzr formats to per_bzrdir.
656
        try:
657
            config.set_default_stack_on('stack-on')
658
        except errors.BzrError:
659
            raise TestNotApplicable('Only relevant for stackable formats.')
660
        # Initialize a bzrdir subject to the policy.
661
        t = self.get_transport('stacked')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
662
        repo_fmt = controldir.format_registry.make_controldir('1.9')
6164.2.5 by Jelmer Vernooij
Move some stacking tests specific to bzr formats to per_bzrdir.
663
        repo_name = repo_fmt.repository_format.network_name()
664
        repo, control = self.assertInitializeEx(
665
            t, need_meta=True, repo_format_name=repo_name, stacked_on=None)
666
        # self.addCleanup(repo.unlock)
667
        # There's one fallback repo, with a public location.
668
        self.assertLength(1, repo._fallback_repositories)
669
        fallback_repo = repo._fallback_repositories[0]
670
        self.assertEqual(
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
671
            stack_on.base, fallback_repo.controldir.root_transport.base)
6164.2.5 by Jelmer Vernooij
Move some stacking tests specific to bzr formats to per_bzrdir.
672
        # The bzrdir creates a branch in stacking-capable format.
673
        new_branch = control.create_branch()
674
        self.assertTrue(new_branch._format.supports_stacking())
675
6239.1.2 by Jelmer Vernooij
Move test to per_bzrdir.
676
    def test_no_leftover_dirs(self):
677
        # bug 886196: development-colo uses a branch-lock directory
678
        # in the user directory rather than the control directory.
679
        if not self.bzrdir_format.colocated_branches:
680
            raise TestNotApplicable(
681
                "format does not support colocated branches")
682
        branch = self.make_branch('.', format='development-colo')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
683
        branch.controldir.create_branch(name="another-colocated-branch")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
684
        self.assertEqual(
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
685
            branch.controldir.user_transport.list_dir("."),
6239.1.2 by Jelmer Vernooij
Move test to per_bzrdir.
686
            [".bzr"])
6282.5.8 by Neil Martinsen-Burrell
refine tests
687
688
    def test_get_branches(self):
689
        repo = self.make_repository('branch-1')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
690
        if not repo.controldir._format.colocated_branches:
6282.5.11 by Jelmer Vernooij
Review tweaks.
691
            raise TestNotApplicable('Format does not support colocation')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
692
        target_branch = repo.controldir.create_branch(name='foo')
693
        repo.controldir.set_branch_reference(target_branch)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
694
        self.assertEqual({"", 'foo'},
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
695
                         set(repo.controldir.get_branches().keys()))