~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_bzrdir/test_bzrdir.py

  • Committer: Vincent Ladeuil
  • Date: 2010-09-28 08:57:31 UTC
  • mto: (5490.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5492.
  • Revision ID: v.ladeuil+lp@free.fr-20100928085731-8h0duqj5wf4acsgy
Add -m to search for a regexp in news entries instead of the bug number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010, 2011, 2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
 
22
22
import bzrlib.branch
23
23
from bzrlib import (
24
 
    bzrdir,
25
 
    controldir,
26
24
    errors,
27
 
    repository,
28
25
    revision as _mod_revision,
29
26
    transport,
30
 
    workingtree,
31
27
    )
32
 
from bzrlib.remote import RemoteBzrDirFormat
33
28
from bzrlib.tests import (
34
 
    TestNotApplicable,
35
29
    TestSkipped,
36
30
    )
37
31
from bzrlib.tests.per_bzrdir import TestCaseWithBzrDir
40
34
    )
41
35
 
42
36
 
43
 
class AnonymousTestBranchFormat(bzrlib.branch.BranchFormat):
44
 
    """An anonymous branch format (does not have a format string)"""
45
 
 
46
 
    def get_format_string(self):
47
 
        raise NotImplementedError(self.get_format_string)
48
 
 
49
 
 
50
 
class IdentifiableTestBranchFormat(bzrlib.branch.BranchFormat):
51
 
    """An identifable branch format (has a format string)"""
52
 
 
53
 
    def get_format_string(self):
54
 
        return "I have an identity"
55
 
 
56
 
 
57
 
class AnonymousTestRepositoryFormat(repository.RepositoryFormat):
58
 
    """An anonymous branch format (does not have a format string)"""
59
 
 
60
 
    def get_format_string(self):
61
 
        raise NotImplementedError(self.get_format_string)
62
 
 
63
 
 
64
 
class IdentifiableTestRepositoryFormat(repository.RepositoryFormat):
65
 
    """An identifable branch format (has a format string)"""
66
 
 
67
 
    def get_format_string(self):
68
 
        return "I have an identity"
69
 
 
70
 
 
71
 
class AnonymousTestWorkingTreeFormat(workingtree.WorkingTreeFormat):
72
 
    """An anonymous branch format (does not have a format string)"""
73
 
 
74
 
    def get_format_string(self):
75
 
        raise NotImplementedError(self.get_format_string)
76
 
 
77
 
 
78
 
class IdentifiableTestWorkingTreeFormat(workingtree.WorkingTreeFormat):
79
 
    """An identifable branch format (has a format string)"""
80
 
 
81
 
    def get_format_string(self):
82
 
        return "I have an identity"
83
 
 
84
 
 
85
37
class TestBzrDir(TestCaseWithBzrDir):
86
38
 
87
39
    # Many of these tests test for disk equality rather than checking
125
77
                    self.assertTrue(S_ISDIR(target.stat(path).st_mode))
126
78
                    directories.append(path)
127
79
                else:
128
 
                    self.assertEqualDiff(source.get_bytes(path),
129
 
                                         target.get_bytes(path),
 
80
                    self.assertEqualDiff(source.get(path).read(),
 
81
                                         target.get(path).read(),
130
82
                                         "text for file %r differs:\n" % path)
131
83
 
132
84
    def assertRepositoryHasSameItems(self, left_repo, right_repo):
163
115
                for file_id, revision_id in text_index.iterkeys():
164
116
                    desired_files.append(
165
117
                        (file_id, revision_id, (file_id, revision_id)))
166
 
                left_texts = [(identifier, "".join(bytes_iterator)) for
167
 
                        (identifier, bytes_iterator) in
168
 
                        left_repo.iter_files_bytes(desired_files)]
169
 
                right_texts = [(identifier, "".join(bytes_iterator)) for
170
 
                        (identifier, bytes_iterator) in
171
 
                        right_repo.iter_files_bytes(desired_files)]
 
118
                left_texts = list(left_repo.iter_files_bytes(desired_files))
 
119
                right_texts = list(right_repo.iter_files_bytes(desired_files))
172
120
                left_texts.sort()
173
121
                right_texts.sort()
174
122
                self.assertEqual(left_texts, right_texts)
221
169
        TestSkipped.  Returns the newly created working tree.
222
170
        """
223
171
        try:
224
 
            # This passes in many named options to make sure they're
225
 
            # understood by subclasses: see
226
 
            # <https://bugs.launchpad.net/bzr/+bug/524627>.
227
 
            return a_bzrdir.create_workingtree(
228
 
                revision_id=None,
229
 
                from_branch=None,
230
 
                accelerator_tree=None,
231
 
                hardlink=False)
 
172
            return a_bzrdir.create_workingtree()
232
173
        except errors.NotLocalUrl:
233
174
            raise TestSkipped("cannot make working tree with transport %r"
234
175
                              % a_bzrdir.transport)
336
277
                                     './.bzr/repository',
337
278
                                     ])
338
279
        self.assertRepositoryHasSameItems(tree.branch.repository,
339
 
            target.open_branch().repository)
 
280
            target.open_repository())
340
281
        target.open_workingtree().revert()
341
282
 
342
283
    def test_revert_inventory(self):
356
297
                                     './.bzr/repository',
357
298
                                     ])
358
299
        self.assertRepositoryHasSameItems(tree.branch.repository,
359
 
            target.open_branch().repository)
 
300
            target.open_repository())
360
301
 
361
302
        target.open_workingtree().revert()
362
303
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
368
309
                                     './.bzr/repository',
369
310
                                     ])
370
311
        self.assertRepositoryHasSameItems(tree.branch.repository,
371
 
            target.open_branch().repository)
 
312
            target.open_repository())
372
313
 
373
314
    def test_clone_bzrdir_tree_branch_reference(self):
374
315
        # a tree with a branch reference (aka a checkout)
376
317
        referenced_branch = self.make_branch('referencced')
377
318
        dir = self.make_bzrdir('source')
378
319
        try:
379
 
            dir.set_branch_reference(referenced_branch)
 
320
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
 
321
                target_branch=referenced_branch)
380
322
        except errors.IncompatibleFormat:
381
323
            # this is ok too, not all formats have to support references.
382
324
            return
419
361
        referenced_branch = self.make_branch('referencced')
420
362
        dir = self.make_bzrdir('source')
421
363
        try:
422
 
            dir.set_branch_reference(referenced_branch)
 
364
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
 
365
                target_branch=referenced_branch)
423
366
        except errors.IncompatibleFormat:
424
367
            # this is ok too, not all formats have to support references.
425
368
            return
506
449
        self.assertNotEqual(dir.transport.base, target.transport.base)
507
450
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
508
451
                                    [
509
 
                                     './.bzr/branch',
 
452
                                     './.bzr/branch/branch.conf',
 
453
                                     './.bzr/branch/parent',
510
454
                                     './.bzr/checkout/dirstate',
511
455
                                     './.bzr/checkout/stat-cache',
512
456
                                     './.bzr/checkout/inventory',
525
469
        # must not overwrite existing directories
526
470
        self.build_tree(['.bzr.retired.0/', '.bzr.retired.0/junk',],
527
471
            transport=transport)
528
 
        self.assertTrue(transport.has('.bzr'))
 
472
        self.failUnless(transport.has('.bzr'))
529
473
        bd.retire_bzrdir()
530
 
        self.assertFalse(transport.has('.bzr'))
531
 
        self.assertTrue(transport.has('.bzr.retired.1'))
 
474
        self.failIf(transport.has('.bzr'))
 
475
        self.failUnless(transport.has('.bzr.retired.1'))
532
476
 
533
477
    def test_retire_bzrdir_limited(self):
534
478
        bd = self.make_bzrdir('.')
536
480
        # must not overwrite existing directories
537
481
        self.build_tree(['.bzr.retired.0/', '.bzr.retired.0/junk',],
538
482
            transport=transport)
539
 
        self.assertTrue(transport.has('.bzr'))
 
483
        self.failUnless(transport.has('.bzr'))
540
484
        self.assertRaises((errors.FileExists, errors.DirectoryNotEmpty),
541
485
            bd.retire_bzrdir, limit=0)
542
 
 
543
 
    def test_get_branch_transport(self):
544
 
        dir = self.make_bzrdir('.')
545
 
        # without a format, get_branch_transport gives use a transport
546
 
        # which -may- point to an existing dir.
547
 
        self.assertTrue(isinstance(dir.get_branch_transport(None),
548
 
                                   transport.Transport))
549
 
        # with a given format, either the bzr dir supports identifiable
550
 
        # branches, or it supports anonymous branch formats, but not both.
551
 
        anonymous_format = AnonymousTestBranchFormat()
552
 
        identifiable_format = IdentifiableTestBranchFormat()
553
 
        try:
554
 
            found_transport = dir.get_branch_transport(anonymous_format)
555
 
            self.assertRaises(errors.IncompatibleFormat,
556
 
                              dir.get_branch_transport,
557
 
                              identifiable_format)
558
 
        except errors.IncompatibleFormat:
559
 
            found_transport = dir.get_branch_transport(identifiable_format)
560
 
        self.assertTrue(isinstance(found_transport, transport.Transport))
561
 
        # and the dir which has been initialized for us must exist.
562
 
        found_transport.list_dir('.')
563
 
 
564
 
    def test_get_repository_transport(self):
565
 
        dir = self.make_bzrdir('.')
566
 
        # without a format, get_repository_transport gives use a transport
567
 
        # which -may- point to an existing dir.
568
 
        self.assertTrue(isinstance(dir.get_repository_transport(None),
569
 
                                   transport.Transport))
570
 
        # with a given format, either the bzr dir supports identifiable
571
 
        # repositories, or it supports anonymous repository formats, but not both.
572
 
        anonymous_format = AnonymousTestRepositoryFormat()
573
 
        identifiable_format = IdentifiableTestRepositoryFormat()
574
 
        try:
575
 
            found_transport = dir.get_repository_transport(anonymous_format)
576
 
            self.assertRaises(errors.IncompatibleFormat,
577
 
                              dir.get_repository_transport,
578
 
                              identifiable_format)
579
 
        except errors.IncompatibleFormat:
580
 
            found_transport = dir.get_repository_transport(identifiable_format)
581
 
        self.assertTrue(isinstance(found_transport, transport.Transport))
582
 
        # and the dir which has been initialized for us must exist.
583
 
        found_transport.list_dir('.')
584
 
 
585
 
    def test_get_workingtree_transport(self):
586
 
        dir = self.make_bzrdir('.')
587
 
        # without a format, get_workingtree_transport gives use a transport
588
 
        # which -may- point to an existing dir.
589
 
        self.assertTrue(isinstance(dir.get_workingtree_transport(None),
590
 
                                   transport.Transport))
591
 
        # with a given format, either the bzr dir supports identifiable
592
 
        # trees, or it supports anonymous tree formats, but not both.
593
 
        anonymous_format = AnonymousTestWorkingTreeFormat()
594
 
        identifiable_format = IdentifiableTestWorkingTreeFormat()
595
 
        try:
596
 
            found_transport = dir.get_workingtree_transport(anonymous_format)
597
 
            self.assertRaises(errors.IncompatibleFormat,
598
 
                              dir.get_workingtree_transport,
599
 
                              identifiable_format)
600
 
        except errors.IncompatibleFormat:
601
 
            found_transport = dir.get_workingtree_transport(identifiable_format)
602
 
        self.assertTrue(isinstance(found_transport, transport.Transport))
603
 
        # and the dir which has been initialized for us must exist.
604
 
        found_transport.list_dir('.')
605
 
 
606
 
    def assertInitializeEx(self, t, need_meta=False, **kwargs):
607
 
        """Execute initialize_on_transport_ex and check it succeeded correctly.
608
 
 
609
 
        This involves checking that the disk objects were created, open with
610
 
        the same format returned, and had the expected disk format.
611
 
 
612
 
        :param t: The transport to initialize on.
613
 
        :param **kwargs: Additional arguments to pass to
614
 
            initialize_on_transport_ex.
615
 
        :return: the resulting repo, control dir tuple.
616
 
        """
617
 
        if not self.bzrdir_format.is_initializable():
618
 
            raise TestNotApplicable("control dir format is not "
619
 
                "initializable")
620
 
        repo, control, require_stacking, repo_policy = \
621
 
            self.bzrdir_format.initialize_on_transport_ex(t, **kwargs)
622
 
        if repo is not None:
623
 
            # Repositories are open write-locked
624
 
            self.assertTrue(repo.is_write_locked())
625
 
            self.addCleanup(repo.unlock)
626
 
        self.assertIsInstance(control, bzrdir.BzrDir)
627
 
        opened = bzrdir.BzrDir.open(t.base)
628
 
        expected_format = self.bzrdir_format
629
 
        if need_meta and expected_format.fixed_components:
630
 
            # Pre-metadir formats change when we are making something that
631
 
            # needs a metaformat, because clone is used for push.
632
 
            expected_format = bzrdir.BzrDirMetaFormat1()
633
 
        if not isinstance(expected_format, RemoteBzrDirFormat):
634
 
            self.assertEqual(control._format.network_name(),
635
 
                expected_format.network_name())
636
 
            self.assertEqual(control._format.network_name(),
637
 
                opened._format.network_name())
638
 
        self.assertEqual(control.__class__, opened.__class__)
639
 
        return repo, control
640
 
 
641
 
    def test_format_initialize_on_transport_ex_default_stack_on(self):
642
 
        # When initialize_on_transport_ex uses a stacked-on branch because of
643
 
        # a stacking policy on the target, the location of the fallback
644
 
        # repository is the same as the external location of the stacked-on
645
 
        # branch.
646
 
        balloon = self.make_bzrdir('balloon')
647
 
        if isinstance(balloon._format, bzrdir.BzrDirMetaFormat1):
648
 
            stack_on = self.make_branch('stack-on', format='1.9')
649
 
        else:
650
 
            stack_on = self.make_branch('stack-on')
651
 
        if not stack_on.repository._format.supports_nesting_repositories:
652
 
            raise TestNotApplicable("requires nesting repositories")
653
 
        config = self.make_bzrdir('.').get_config()
654
 
        try:
655
 
            config.set_default_stack_on('stack-on')
656
 
        except errors.BzrError:
657
 
            raise TestNotApplicable('Only relevant for stackable formats.')
658
 
        # Initialize a bzrdir subject to the policy.
659
 
        t = self.get_transport('stacked')
660
 
        repo_fmt = controldir.format_registry.make_bzrdir('1.9')
661
 
        repo_name = repo_fmt.repository_format.network_name()
662
 
        repo, control = self.assertInitializeEx(
663
 
            t, need_meta=True, repo_format_name=repo_name, stacked_on=None)
664
 
        # self.addCleanup(repo.unlock)
665
 
        # There's one fallback repo, with a public location.
666
 
        self.assertLength(1, repo._fallback_repositories)
667
 
        fallback_repo = repo._fallback_repositories[0]
668
 
        self.assertEqual(
669
 
            stack_on.base, fallback_repo.bzrdir.root_transport.base)
670
 
        # The bzrdir creates a branch in stacking-capable format.
671
 
        new_branch = control.create_branch()
672
 
        self.assertTrue(new_branch._format.supports_stacking())
673
 
 
674
 
    def test_no_leftover_dirs(self):
675
 
        # bug 886196: development-colo uses a branch-lock directory
676
 
        # in the user directory rather than the control directory.
677
 
        if not self.bzrdir_format.colocated_branches:
678
 
            raise TestNotApplicable(
679
 
                "format does not support colocated branches")
680
 
        branch = self.make_branch('.', format='development-colo')
681
 
        branch.bzrdir.create_branch(name="another-colocated-branch")
682
 
        self.assertEqual(
683
 
            branch.bzrdir.user_transport.list_dir("."),
684
 
            [".bzr"])
685
 
 
686
 
    def test_get_branches(self):
687
 
        repo = self.make_repository('branch-1')
688
 
        if not repo.bzrdir._format.colocated_branches:
689
 
            raise TestNotApplicable('Format does not support colocation')
690
 
        target_branch = repo.bzrdir.create_branch(name='foo')
691
 
        repo.bzrdir.set_branch_reference(target_branch)
692
 
        self.assertEqual(set(["", 'foo']),
693
 
                         set(repo.bzrdir.get_branches().keys()))