/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Martin von Gagern
  • Date: 2010-04-20 08:47:38 UTC
  • mfrom: (5167 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5195.
  • Revision ID: martin.vgagern@gmx.net-20100420084738-ygymnqmdllzrhpfn
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for bzrdir implementations - tests a bzrdir format."""
18
18
 
21
21
from itertools import izip
22
22
import os
23
23
from stat import S_ISDIR
24
 
import sys
25
24
 
26
25
import bzrlib.branch
27
26
from bzrlib import (
28
27
    bzrdir,
 
28
    check,
29
29
    errors,
 
30
    gpg,
30
31
    lockdir,
 
32
    osutils,
31
33
    repository,
32
34
    revision as _mod_revision,
33
35
    transactions,
34
36
    transport,
35
37
    ui,
 
38
    urlutils,
36
39
    workingtree,
37
40
    )
38
41
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
39
 
from bzrlib.check import check_branch
40
42
from bzrlib.errors import (FileExists,
41
43
                           NoSuchRevision,
42
44
                           NoSuchFile,
51
53
                          TestNotApplicable,
52
54
                          TestSkipped,
53
55
                          )
54
 
from bzrlib.tests.bzrdir_implementations import TestCaseWithBzrDir
 
56
from bzrlib.tests.per_bzrdir import TestCaseWithBzrDir
55
57
from bzrlib.trace import mutter
56
58
from bzrlib.transport import get_transport
57
59
from bzrlib.transport.local import LocalTransport
 
60
from bzrlib.ui import (
 
61
    CannedInputUIFactory,
 
62
    )
58
63
from bzrlib.upgrade import upgrade
59
 
from bzrlib.remote import RemoteBzrDir
 
64
from bzrlib.remote import RemoteBzrDir, RemoteRepository
60
65
from bzrlib.repofmt import weaverepo
61
66
 
62
67
 
73
78
        """Assert that the content of source and target are identical.
74
79
 
75
80
        paths in ignore list will be completely ignored.
76
 
        
 
81
 
77
82
        We ignore paths that represent data which is allowed to change during
78
83
        a clone or sprout: for instance, inventory.knit contains gzip fragements
79
 
        which have timestamps in them, and as we have read the inventory from 
 
84
        which have timestamps in them, and as we have read the inventory from
80
85
        the source knit, the already-read data is recompressed rather than
81
86
        reading it again, which leads to changed timestamps. This is ok though,
82
87
        because the inventory.kndx file is not ignored, and the integrity of
124
129
                for rev_id in left_repo.all_revision_ids():
125
130
                    self.assertEqual(left_repo.get_revision(rev_id),
126
131
                        right_repo.get_revision(rev_id))
127
 
                # inventories
128
 
                left_inv_weave = left_repo.inventories
129
 
                right_inv_weave = right_repo.inventories
130
 
                self.assertEqual(set(left_inv_weave.keys()),
131
 
                    set(right_inv_weave.keys()))
132
 
                # XXX: currently this does not handle indirectly referenced
133
 
                # inventories (e.g. where the inventory is a delta basis for
134
 
                # one that is fully present but that the revid for that
135
 
                # inventory is not yet present.)
136
 
                self.assertEqual(set(left_inv_weave.keys()),
137
 
                    set(left_repo.revisions.keys()))
138
 
                left_trees = left_repo.revision_trees(all_revs)
139
 
                right_trees = right_repo.revision_trees(all_revs)
140
 
                for left_tree, right_tree in izip(left_trees, right_trees):
141
 
                    self.assertEqual(left_tree.inventory, right_tree.inventory)
 
132
                # Assert the revision trees (and thus the inventories) are equal
 
133
                sort_key = lambda rev_tree: rev_tree.get_revision_id()
 
134
                rev_trees_a = sorted(
 
135
                    left_repo.revision_trees(all_revs), key=sort_key)
 
136
                rev_trees_b = sorted(
 
137
                    right_repo.revision_trees(all_revs), key=sort_key)
 
138
                for tree_a, tree_b in zip(rev_trees_a, rev_trees_b):
 
139
                    self.assertEqual([], list(tree_a.iter_changes(tree_b)))
142
140
                # texts
143
141
                text_index = left_repo._generate_text_key_index()
144
142
                self.assertEqual(text_index,
167
165
 
168
166
    def skipIfNoWorkingTree(self, a_bzrdir):
169
167
        """Raises TestSkipped if a_bzrdir doesn't have a working tree.
170
 
        
 
168
 
171
169
        If the bzrdir does have a workingtree, this is a no-op.
172
170
        """
173
171
        try:
176
174
            raise TestSkipped("bzrdir on transport %r has no working tree"
177
175
                              % a_bzrdir.transport)
178
176
 
 
177
    def openWorkingTreeIfLocal(self, a_bzrdir):
 
178
        """If a_bzrdir is on a local transport, call open_workingtree() on it.
 
179
        """
 
180
        if not isinstance(a_bzrdir.root_transport, LocalTransport):
 
181
            # it's not local, but that's ok
 
182
            return
 
183
        a_bzrdir.open_workingtree()
 
184
 
179
185
    def createWorkingTreeOrSkip(self, a_bzrdir):
180
186
        """Create a working tree on a_bzrdir, or raise TestSkipped.
181
 
        
 
187
 
182
188
        A simple wrapper for create_workingtree that translates NotLocalUrl into
183
189
        TestSkipped.  Returns the newly created working tree.
184
190
        """
189
195
                              % a_bzrdir.transport)
190
196
 
191
197
    def sproutOrSkip(self, from_bzrdir, to_url, revision_id=None,
192
 
                     force_new_repo=False, accelerator_tree=None):
 
198
                     force_new_repo=False, accelerator_tree=None,
 
199
                     create_tree_if_local=True):
193
200
        """Sprout from_bzrdir into to_url, or raise TestSkipped.
194
 
        
 
201
 
195
202
        A simple wrapper for from_bzrdir.sprout that translates NotLocalUrl into
196
203
        TestSkipped.  Returns the newly sprouted bzrdir.
197
204
        """
201
208
        target = from_bzrdir.sprout(to_url, revision_id=revision_id,
202
209
                                    force_new_repo=force_new_repo,
203
210
                                    possible_transports=[to_transport],
204
 
                                    accelerator_tree=accelerator_tree)
 
211
                                    accelerator_tree=accelerator_tree,
 
212
                                    create_tree_if_local=create_tree_if_local)
205
213
        return target
206
214
 
207
215
    def test_create_null_workingtree(self):
239
247
        try:
240
248
            bzrdir.destroy_branch()
241
249
        except (errors.UnsupportedOperation, errors.TransportNotPossible):
242
 
            raise TestNotApplicable('Format does not support destroying tree')
 
250
            raise TestNotApplicable('Format does not support destroying branch')
243
251
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
244
252
        bzrdir.create_branch()
245
253
        bzrdir.open_branch()
282
290
        self.assertNotEqual(dir.transport.base, target.transport.base)
283
291
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
284
292
                                    ['./.bzr/merge-hashes'])
285
 
    
 
293
 
286
294
    def test_clone_bzrdir_empty_force_new_ignored(self):
287
295
        # the force_new_repo parameter should have no effect on an empty
288
296
        # bzrdir's clone logic
291
299
        self.assertNotEqual(dir.transport.base, target.transport.base)
292
300
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
293
301
                                    ['./.bzr/merge-hashes'])
294
 
    
 
302
 
295
303
    def test_clone_bzrdir_repository(self):
296
304
        tree = self.make_branch_and_tree('commit_tree')
297
305
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
385
393
        self.assertTrue(branch.repository.has_revision('1'))
386
394
        self.assertFalse(branch.repository.make_working_trees())
387
395
        self.assertTrue(branch.repository.is_shared())
388
 
        
 
396
 
389
397
    def test_clone_bzrdir_repository_under_shared_force_new_repo(self):
390
398
        tree = self.make_branch_and_tree('commit_tree')
391
399
        self.build_tree(['commit_tree/foo'])
410
418
        # test for revision limiting, [smoke test, not corner case checks].
411
419
        # make a repository with some revisions,
412
420
        # and clone it with a revision limit.
413
 
        # 
 
421
        #
414
422
        tree = self.make_branch_and_tree('commit_tree')
415
423
        self.build_tree(['commit_tree/foo'])
416
424
        tree.add('foo')
424
432
        target = dir.clone(self.get_url('target'), revision_id='2')
425
433
        raise TestSkipped('revision limiting not strict yet')
426
434
 
 
435
    def test_clone_bzrdir_branch_and_repo_fixed_user_id(self):
 
436
        # Bug #430868 is about an email containing '.sig'
 
437
        os.environ['BZR_EMAIL'] = 'murphy@host.sighup.org'
 
438
        tree = self.make_branch_and_tree('commit_tree')
 
439
        self.build_tree(['commit_tree/foo'])
 
440
        tree.add('foo')
 
441
        rev1 = tree.commit('revision 1')
 
442
        tree_repo = tree.branch.repository
 
443
        tree_repo.lock_write()
 
444
        tree_repo.start_write_group()
 
445
        tree_repo.sign_revision(rev1, gpg.LoopbackGPGStrategy(None))
 
446
        tree_repo.commit_write_group()
 
447
        tree_repo.unlock()
 
448
        target = self.make_branch('target')
 
449
        tree.branch.repository.copy_content_into(target.repository)
 
450
        tree.branch.copy_content_into(target)
 
451
        self.assertTrue(target.repository.has_revision(rev1))
 
452
        self.assertEqual(
 
453
            tree_repo.get_signature_text(rev1),
 
454
            target.repository.get_signature_text(rev1))
 
455
 
427
456
    def test_clone_bzrdir_branch_and_repo(self):
428
457
        tree = self.make_branch_and_tree('commit_tree')
429
458
        self.build_tree(['commit_tree/foo'])
494
523
        dir = self.make_bzrdir('source')
495
524
        try:
496
525
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
497
 
                referenced_branch)
 
526
                target_branch=referenced_branch)
498
527
        except errors.IncompatibleFormat:
499
528
            # this is ok too, not all formats have to support references.
500
529
            return
506
535
        # test for revision limiting, [smoke test, not corner case checks].
507
536
        # make a branch with some revisions,
508
537
        # and clone it with a revision limit.
509
 
        # 
 
538
        #
510
539
        tree = self.make_branch_and_tree('commit_tree')
511
540
        self.build_tree(['commit_tree/foo'])
512
541
        tree.add('foo')
518
547
        dir = source.bzrdir
519
548
        target = dir.clone(self.get_url('target'), revision_id='1')
520
549
        self.assertEqual('1', target.open_branch().last_revision())
521
 
        
 
550
 
522
551
    def test_clone_bzrdir_tree_branch_repo(self):
523
552
        tree = self.make_branch_and_tree('source')
524
553
        self.build_tree(['source/foo'])
549
578
        # Ensure no format data is cached
550
579
        a_dir = bzrlib.branch.Branch.open_from_transport(
551
580
            self.get_transport('source')).bzrdir
552
 
        target_transport = a_dir.root_transport.clone('..').clone('target')
 
581
        target_transport = self.get_transport('target')
553
582
        target_bzrdir = a_dir.clone_on_transport(target_transport)
554
583
        target_repo = target_bzrdir.open_repository()
555
584
        source_branch = bzrlib.branch.Branch.open(
556
585
            self.get_vfs_only_url('source'))
 
586
        if isinstance(target_repo, RemoteRepository):
 
587
            target_repo._ensure_real()
 
588
            target_repo = target_repo._real_repository
557
589
        self.assertEqual(target_repo._format, source_branch.repository._format)
558
590
 
559
591
    def test_revert_inventory(self):
588
620
            target.open_repository())
589
621
 
590
622
    def test_clone_bzrdir_tree_branch_reference(self):
591
 
        # a tree with a branch reference (aka a checkout) 
 
623
        # a tree with a branch reference (aka a checkout)
592
624
        # should stay a checkout on clone.
593
625
        referenced_branch = self.make_branch('referencced')
594
626
        dir = self.make_bzrdir('source')
595
627
        try:
596
628
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
597
 
                referenced_branch)
 
629
                target_branch=referenced_branch)
598
630
        except errors.IncompatibleFormat:
599
631
            # this is ok too, not all formats have to support references.
600
632
            return
648
680
 
649
681
    def test_clone_respects_stacked(self):
650
682
        branch = self.make_branch('parent')
651
 
        child_transport = branch.bzrdir.root_transport.clone('../child')
 
683
        child_transport = self.get_transport('child')
652
684
        child = branch.bzrdir.clone_on_transport(child_transport,
653
685
                                                 stacked_on=branch.base)
654
686
        self.assertEqual(child.open_branch().get_stacked_on_url(), branch.base)
659
691
        dir = self.make_bzrdir('source')
660
692
        try:
661
693
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
662
 
                referenced_branch)
 
694
                target_branch=referenced_branch)
663
695
        except errors.IncompatibleFormat:
664
696
            # this is ok too, not all formats have to support references.
665
697
            return
681
713
 
682
714
    def test_sprout_bzrdir_empty(self):
683
715
        dir = self.make_bzrdir('source')
684
 
        target = self.sproutOrSkip(dir, self.get_url('target'))
 
716
        target = dir.sprout(self.get_url('target'))
685
717
        self.assertNotEqual(dir.transport.base, target.transport.base)
686
718
        # creates a new repository branch and tree
687
719
        target.open_repository()
688
720
        target.open_branch()
689
 
        target.open_workingtree()
 
721
        self.openWorkingTreeIfLocal(target)
690
722
 
691
723
    def test_sprout_bzrdir_empty_under_shared_repo(self):
692
724
        # sprouting an empty dir into a repo uses the repo
695
727
            self.make_repository('target', shared=True)
696
728
        except errors.IncompatibleFormat:
697
729
            return
698
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
 
730
        target = dir.sprout(self.get_url('target/child'))
699
731
        self.assertRaises(errors.NoRepositoryPresent, target.open_repository)
700
732
        target.open_branch()
701
733
        try:
713
745
            self.make_repository('target', shared=True)
714
746
        except errors.IncompatibleFormat:
715
747
            return
716
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
717
 
                                   force_new_repo=True)
 
748
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
718
749
        target.open_repository()
719
750
        target.open_branch()
720
 
        target.open_workingtree()
721
 
    
 
751
        self.openWorkingTreeIfLocal(target)
 
752
 
722
753
    def test_sprout_bzrdir_repository(self):
723
754
        tree = self.make_branch_and_tree('commit_tree')
724
755
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
734
765
                dir.open_branch().last_revision())))
735
766
        except errors.NotBranchError:
736
767
            pass
737
 
        target = self.sproutOrSkip(dir, self.get_url('target'))
 
768
        target = dir.sprout(self.get_url('target'))
738
769
        self.assertNotEqual(dir.transport.base, target.transport.base)
739
770
        # testing inventory isn't reasonable for repositories
740
771
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
746
777
                                     './.bzr/repository/inventory.knit',
747
778
                                     ])
748
779
        try:
 
780
            local_inventory = dir.transport.local_abspath('inventory')
 
781
        except errors.NotLocalUrl:
 
782
            return
 
783
        try:
749
784
            # If we happen to have a tree, we'll guarantee everything
750
785
            # except for the tree root is the same.
751
 
            inventory_f = file(dir.transport.base+'inventory', 'rb')
752
 
            self.assertContainsRe(inventory_f.read(), 
753
 
                                  '<inventory file_id="TREE_ROOT[^"]*"'
754
 
                                  ' format="5">\n</inventory>\n')
755
 
            inventory_f.close()
 
786
            inventory_f = file(local_inventory, 'rb')
 
787
            self.addCleanup(inventory_f.close)
 
788
            self.assertContainsRe(inventory_f.read(),
 
789
                                  '<inventory format="5">\n</inventory>\n')
756
790
        except IOError, e:
757
791
            if e.errno != errno.ENOENT:
758
792
                raise
772
806
            shared_repo = self.make_repository('target', shared=True)
773
807
        except errors.IncompatibleFormat:
774
808
            return
775
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
 
809
        target = dir.sprout(self.get_url('target/child'))
776
810
        self.assertNotEqual(dir.transport.base, target.transport.base)
777
811
        self.assertTrue(shared_repo.has_revision('1'))
778
812
 
791
825
        tree.branch.repository.copy_content_into(shared_repo)
792
826
        dir = self.make_bzrdir('shared/source')
793
827
        dir.create_branch()
794
 
        target = self.sproutOrSkip(dir, self.get_url('shared/target'))
 
828
        target = dir.sprout(self.get_url('shared/target'))
795
829
        self.assertNotEqual(dir.transport.base, target.transport.base)
796
830
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
797
831
        self.assertTrue(shared_repo.has_revision('1'))
815
849
        self.assertTrue(shared_repo.has_revision('1'))
816
850
        dir = self.make_bzrdir('shared/source')
817
851
        dir.create_branch()
818
 
        target = self.sproutOrSkip(dir, self.get_url('target'))
 
852
        target = dir.sprout(self.get_url('target'))
819
853
        self.assertNotEqual(dir.transport.base, target.transport.base)
820
854
        self.assertNotEqual(dir.transport.base, shared_repo.bzrdir.transport.base)
821
855
        branch = target.open_branch()
839
873
            shared_repo = self.make_repository('target', shared=True)
840
874
        except errors.IncompatibleFormat:
841
875
            return
842
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
843
 
                                   force_new_repo=True)
 
876
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
844
877
        self.assertNotEqual(dir.transport.base, target.transport.base)
845
878
        self.assertFalse(shared_repo.has_revision('1'))
846
879
 
848
881
        # test for revision limiting, [smoke test, not corner case checks].
849
882
        # make a repository with some revisions,
850
883
        # and sprout it with a revision limit.
851
 
        # 
 
884
        #
852
885
        tree = self.make_branch_and_tree('commit_tree')
853
886
        self.build_tree(['commit_tree/foo'])
854
887
        tree.add('foo')
871
904
        tree.branch.repository.copy_content_into(source.repository)
872
905
        tree.bzrdir.open_branch().copy_content_into(source)
873
906
        dir = source.bzrdir
874
 
        target = self.sproutOrSkip(dir, self.get_url('target'))
 
907
        target = dir.sprout(self.get_url('target'))
875
908
        self.assertNotEqual(dir.transport.base, target.transport.base)
 
909
        target_repo = target.open_repository()
 
910
        self.assertRepositoryHasSameItems(source.repository, target_repo)
876
911
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
877
912
                                    [
878
913
                                     './.bzr/basis-inventory-cache',
883
918
                                     './.bzr/checkout/stat-cache',
884
919
                                     './.bzr/inventory',
885
920
                                     './.bzr/parent',
886
 
                                     './.bzr/repository/inventory.knit',
 
921
                                     './.bzr/repository',
887
922
                                     './.bzr/stat-cache',
888
923
                                     './foo',
889
924
                                     ])
903
938
            shared_repo = self.make_repository('target', shared=True)
904
939
        except errors.IncompatibleFormat:
905
940
            return
906
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
 
941
        target = dir.sprout(self.get_url('target/child'))
907
942
        self.assertTrue(shared_repo.has_revision('1'))
908
943
 
909
944
    def test_sprout_bzrdir_branch_and_repo_shared_force_new_repo(self):
921
956
            shared_repo = self.make_repository('target', shared=True)
922
957
        except errors.IncompatibleFormat:
923
958
            return
924
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
925
 
                                   force_new_repo=True)
 
959
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
926
960
        self.assertNotEqual(dir.transport.base, target.transport.base)
927
961
        self.assertFalse(shared_repo.has_revision('1'))
928
962
 
929
963
    def test_sprout_bzrdir_branch_reference(self):
930
964
        # sprouting should create a repository if needed and a sprouted branch.
931
 
        referenced_branch = self.make_branch('referencced')
 
965
        referenced_branch = self.make_branch('referenced')
932
966
        dir = self.make_bzrdir('source')
933
967
        try:
934
968
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
935
 
                referenced_branch)
 
969
                target_branch=referenced_branch)
936
970
        except errors.IncompatibleFormat:
937
971
            # this is ok too, not all formats have to support references.
938
972
            return
939
973
        self.assertRaises(errors.NoRepositoryPresent, dir.open_repository)
940
 
        target = self.sproutOrSkip(dir, self.get_url('target'))
 
974
        target = dir.sprout(self.get_url('target'))
941
975
        self.assertNotEqual(dir.transport.base, target.transport.base)
942
976
        # we want target to have a branch that is in-place.
943
977
        self.assertEqual(target, target.open_branch().bzrdir)
944
 
        # and as we dont support repositories being detached yet, a repo in 
 
978
        # and as we dont support repositories being detached yet, a repo in
945
979
        # place
946
980
        target.open_repository()
947
981
 
952
986
        dir = self.make_bzrdir('source')
953
987
        try:
954
988
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
955
 
                referenced_tree.branch)
 
989
                target_branch=referenced_tree.branch)
956
990
        except errors.IncompatibleFormat:
957
991
            # this is ok too, not all formats have to support references.
958
992
            return
961
995
            shared_repo = self.make_repository('target', shared=True)
962
996
        except errors.IncompatibleFormat:
963
997
            return
964
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'))
 
998
        target = dir.sprout(self.get_url('target/child'))
965
999
        self.assertNotEqual(dir.transport.base, target.transport.base)
966
1000
        # we want target to have a branch that is in-place.
967
1001
        self.assertEqual(target, target.open_branch().bzrdir)
968
1002
        # and we want no repository as the target is shared
969
 
        self.assertRaises(errors.NoRepositoryPresent, 
 
1003
        self.assertRaises(errors.NoRepositoryPresent,
970
1004
                          target.open_repository)
971
1005
        # and we want revision '1' in the shared repo
972
1006
        self.assertTrue(shared_repo.has_revision('1'))
978
1012
        dir = self.make_bzrdir('source')
979
1013
        try:
980
1014
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
981
 
                referenced_tree.branch)
 
1015
                target_branch=referenced_tree.branch)
982
1016
        except errors.IncompatibleFormat:
983
1017
            # this is ok too, not all formats have to support references.
984
1018
            return
987
1021
            shared_repo = self.make_repository('target', shared=True)
988
1022
        except errors.IncompatibleFormat:
989
1023
            return
990
 
        target = self.sproutOrSkip(dir, self.get_url('target/child'),
991
 
                                   force_new_repo=True)
 
1024
        target = dir.sprout(self.get_url('target/child'), force_new_repo=True)
992
1025
        self.assertNotEqual(dir.transport.base, target.transport.base)
993
1026
        # we want target to have a branch that is in-place.
994
1027
        self.assertEqual(target, target.open_branch().bzrdir)
1001
1034
        # test for revision limiting, [smoke test, not corner case checks].
1002
1035
        # make a repository with some revisions,
1003
1036
        # and sprout it with a revision limit.
1004
 
        # 
 
1037
        #
1005
1038
        tree = self.make_branch_and_tree('commit_tree')
1006
1039
        self.build_tree(['commit_tree/foo'])
1007
1040
        tree.add('foo')
1011
1044
        tree.branch.repository.copy_content_into(source.repository)
1012
1045
        tree.bzrdir.open_branch().copy_content_into(source)
1013
1046
        dir = source.bzrdir
1014
 
        target = self.sproutOrSkip(dir, self.get_url('target'), revision_id='1')
 
1047
        target = dir.sprout(self.get_url('target'), revision_id='1')
1015
1048
        self.assertEqual('1', target.open_branch().last_revision())
1016
 
        
 
1049
 
1017
1050
    def test_sprout_bzrdir_tree_branch_repo(self):
1018
1051
        tree = self.make_branch_and_tree('source')
1019
1052
        self.build_tree(['foo'], transport=tree.bzrdir.transport.clone('..'))
1044
1077
        dir = self.make_bzrdir('source')
1045
1078
        try:
1046
1079
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1047
 
                referenced_branch)
 
1080
                target_branch=referenced_branch)
1048
1081
        except errors.IncompatibleFormat:
1049
1082
            # this is ok too, not all formats have to support references.
1050
1083
            return
1052
1085
        tree = self.createWorkingTreeOrSkip(dir)
1053
1086
        self.build_tree(['source/subdir/'])
1054
1087
        tree.add('subdir')
1055
 
        target = self.sproutOrSkip(dir, self.get_url('target'))
 
1088
        target = dir.sprout(self.get_url('target'))
1056
1089
        self.assertNotEqual(dir.transport.base, target.transport.base)
1057
1090
        # we want target to have a branch that is in-place.
1058
1091
        self.assertEqual(target, target.open_branch().bzrdir)
1059
 
        # and as we dont support repositories being detached yet, a repo in 
 
1092
        # and as we dont support repositories being detached yet, a repo in
1060
1093
        # place
1061
1094
        target.open_repository()
1062
1095
        result_tree = target.open_workingtree()
1070
1103
        dir = self.make_bzrdir('source')
1071
1104
        try:
1072
1105
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
1073
 
                referenced_branch)
 
1106
                target_branch=referenced_branch)
1074
1107
        except errors.IncompatibleFormat:
1075
1108
            # this is ok too, not all formats have to support references.
1076
1109
            return
1085
1118
        self.assertNotEqual(dir.transport.base, target.transport.base)
1086
1119
        # we want target to have a branch that is in-place.
1087
1120
        self.assertEqual(target, target.open_branch().bzrdir)
1088
 
        # and as we dont support repositories being detached yet, a repo in 
 
1121
        # and as we dont support repositories being detached yet, a repo in
1089
1122
        # place
1090
1123
        target.open_repository()
1091
1124
        # we trust that the working tree sprouting works via the other tests.
1118
1151
                                   accelerator_tree=tree)
1119
1152
        self.assertEqual(['2'], target.open_workingtree().get_parent_ids())
1120
1153
 
 
1154
    def test_sprout_branch_no_tree(self):
 
1155
        tree = self.make_branch_and_tree('source')
 
1156
        self.build_tree(['source/foo'])
 
1157
        tree.add('foo')
 
1158
        tree.commit('revision 1', rev_id='1')
 
1159
        tree.commit('revision 2', rev_id='2', allow_pointless=True)
 
1160
        dir = tree.bzrdir
 
1161
        if isinstance(dir, (bzrdir.BzrDirPreSplitOut,)):
 
1162
            self.assertRaises(errors.MustHaveWorkingTree, dir.sprout,
 
1163
                              self.get_url('target'),
 
1164
                              create_tree_if_local=False)
 
1165
            return
 
1166
        target = dir.sprout(self.get_url('target'), create_tree_if_local=False)
 
1167
        self.failIfExists('target/foo')
 
1168
        self.assertEqual(tree.branch.last_revision(),
 
1169
                         target.open_branch().last_revision())
 
1170
 
1121
1171
    def test_format_initialize_find_open(self):
1122
1172
        # loopback test to check the current format initializes to itself.
1123
1173
        if not self.bzrdir_format.is_supported():
1125
1175
            # because the default open will not open them and
1126
1176
            # they may not be initializable.
1127
1177
            return
 
1178
        # for remote formats, there must be no prior assumption about the
 
1179
        # network name to use - it's possible that this may somehow have got
 
1180
        # in through an unisolated test though - see
 
1181
        # <https://bugs.edge.launchpad.net/bzr/+bug/504102>
 
1182
        self.assertEquals(getattr(self.bzrdir_format,
 
1183
            '_network_name', None),
 
1184
            None)
1128
1185
        # supported formats must be able to init and open
1129
1186
        t = get_transport(self.get_url())
1130
1187
        readonly_t = get_transport(self.get_readonly_url())
1140
1197
                         opened_dir._format)
1141
1198
        self.failUnless(isinstance(opened_dir, bzrdir.BzrDir))
1142
1199
 
 
1200
    def test_format_initialize_on_transport_ex(self):
 
1201
        t = self.get_transport('dir')
 
1202
        self.assertInitializeEx(t)
 
1203
 
 
1204
    def test_format_initialize_on_transport_ex_use_existing_dir_True(self):
 
1205
        t = self.get_transport('dir')
 
1206
        t.ensure_base()
 
1207
        self.assertInitializeEx(t, use_existing_dir=True)
 
1208
 
 
1209
    def test_format_initialize_on_transport_ex_use_existing_dir_False(self):
 
1210
        if not self.bzrdir_format.is_supported():
 
1211
            # Not initializable - not a failure either.
 
1212
            return
 
1213
        t = self.get_transport('dir')
 
1214
        t.ensure_base()
 
1215
        self.assertRaises(errors.FileExists,
 
1216
            self.bzrdir_format.initialize_on_transport_ex, t,
 
1217
            use_existing_dir=False)
 
1218
 
 
1219
    def test_format_initialize_on_transport_ex_create_prefix_True(self):
 
1220
        t = self.get_transport('missing/dir')
 
1221
        self.assertInitializeEx(t, create_prefix=True)
 
1222
 
 
1223
    def test_format_initialize_on_transport_ex_create_prefix_False(self):
 
1224
        if not self.bzrdir_format.is_supported():
 
1225
            # Not initializable - not a failure either.
 
1226
            return
 
1227
        t = self.get_transport('missing/dir')
 
1228
        self.assertRaises(errors.NoSuchFile, self.assertInitializeEx, t,
 
1229
            create_prefix=False)
 
1230
 
 
1231
    def test_format_initialize_on_transport_ex_force_new_repo_True(self):
 
1232
        t = self.get_transport('repo')
 
1233
        repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
 
1234
        repo_name = repo_fmt.repository_format.network_name()
 
1235
        repo = repo_fmt.initialize_on_transport_ex(t,
 
1236
            repo_format_name=repo_name, shared_repo=True)[0]
 
1237
        made_repo, control = self.assertInitializeEx(t.clone('branch'),
 
1238
            force_new_repo=True, repo_format_name=repo_name)
 
1239
        if control is None:
 
1240
            # uninitialisable format
 
1241
            return
 
1242
        self.assertNotEqual(repo.bzrdir.root_transport.base,
 
1243
            made_repo.bzrdir.root_transport.base)
 
1244
 
 
1245
    def test_format_initialize_on_transport_ex_force_new_repo_False(self):
 
1246
        t = self.get_transport('repo')
 
1247
        repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
 
1248
        repo_name = repo_fmt.repository_format.network_name()
 
1249
        repo = repo_fmt.initialize_on_transport_ex(t,
 
1250
            repo_format_name=repo_name, shared_repo=True)[0]
 
1251
        made_repo, control = self.assertInitializeEx(t.clone('branch'),
 
1252
            force_new_repo=False, repo_format_name=repo_name)
 
1253
        if control is None:
 
1254
            # uninitialisable format
 
1255
            return
 
1256
        if not isinstance(control._format, (bzrdir.BzrDirFormat5,
 
1257
            bzrdir.BzrDirFormat6,)):
 
1258
            self.assertEqual(repo.bzrdir.root_transport.base,
 
1259
                made_repo.bzrdir.root_transport.base)
 
1260
 
 
1261
    def test_format_initialize_on_transport_ex_stacked_on(self):
 
1262
        # trunk is a stackable format.  Note that its in the same server area
 
1263
        # which is what launchpad does, but not sufficient to exercise the
 
1264
        # general case.
 
1265
        trunk = self.make_branch('trunk', format='1.9')
 
1266
        t = self.get_transport('stacked')
 
1267
        old_fmt = bzrdir.format_registry.make_bzrdir('pack-0.92')
 
1268
        repo_name = old_fmt.repository_format.network_name()
 
1269
        # Should end up with a 1.9 format (stackable)
 
1270
        repo, control = self.assertInitializeEx(t, need_meta=True,
 
1271
            repo_format_name=repo_name, stacked_on='../trunk', stack_on_pwd=t.base)
 
1272
        if control is None:
 
1273
            # uninitialisable format
 
1274
            return
 
1275
        self.assertLength(1, repo._fallback_repositories)
 
1276
 
 
1277
    def test_format_initialize_on_transport_ex_default_stack_on(self):
 
1278
        # When initialize_on_transport_ex uses a stacked-on branch because of
 
1279
        # a stacking policy on the target, the location of the fallback
 
1280
        # repository is the same as the external location of the stacked-on
 
1281
        # branch.
 
1282
        balloon = self.make_bzrdir('balloon')
 
1283
        if isinstance(balloon._format, bzrdir.BzrDirMetaFormat1):
 
1284
            stack_on = self.make_branch('stack-on', format='1.9')
 
1285
        else:
 
1286
            stack_on = self.make_branch('stack-on')
 
1287
        config = self.make_bzrdir('.').get_config()
 
1288
        try:
 
1289
            config.set_default_stack_on('stack-on')
 
1290
        except errors.BzrError:
 
1291
            raise TestNotApplicable('Only relevant for stackable formats.')
 
1292
        # Initialize a bzrdir subject to the policy.
 
1293
        t = self.get_transport('stacked')
 
1294
        repo_fmt = bzrdir.format_registry.make_bzrdir('1.9')
 
1295
        repo_name = repo_fmt.repository_format.network_name()
 
1296
        repo, control = self.assertInitializeEx(
 
1297
            t, need_meta=True, repo_format_name=repo_name, stacked_on=None)
 
1298
        # self.addCleanup(repo.unlock)
 
1299
        if control is None:
 
1300
            # uninitialisable format
 
1301
            return
 
1302
        # There's one fallback repo, with a public location.
 
1303
        self.assertLength(1, repo._fallback_repositories)
 
1304
        fallback_repo = repo._fallback_repositories[0]
 
1305
        self.assertEqual(
 
1306
            stack_on.base, fallback_repo.bzrdir.root_transport.base)
 
1307
        # The bzrdir creates a branch in stacking-capable format.
 
1308
        new_branch = control.create_branch()
 
1309
        self.assertTrue(new_branch._format.supports_stacking())
 
1310
 
 
1311
    def test_format_initialize_on_transport_ex_repo_fmt_name_None(self):
 
1312
        t = self.get_transport('dir')
 
1313
        repo, control = self.assertInitializeEx(t)
 
1314
        self.assertEqual(None, repo)
 
1315
 
 
1316
    def test_format_initialize_on_transport_ex_repo_fmt_name_followed(self):
 
1317
        t = self.get_transport('dir')
 
1318
        # 1.6 is likely to never be default
 
1319
        fmt = bzrdir.format_registry.make_bzrdir('1.6')
 
1320
        repo_name = fmt.repository_format.network_name()
 
1321
        repo, control = self.assertInitializeEx(t, repo_format_name=repo_name)
 
1322
        if control is None:
 
1323
            # uninitialisable format
 
1324
            return
 
1325
        if isinstance(self.bzrdir_format, (bzrdir.BzrDirFormat5,
 
1326
            bzrdir.BzrDirFormat6)):
 
1327
            # must stay with the all-in-one-format.
 
1328
            repo_name = self.bzrdir_format.network_name()
 
1329
        self.assertEqual(repo_name, repo._format.network_name())
 
1330
 
 
1331
    def assertInitializeEx(self, t, need_meta=False, **kwargs):
 
1332
        """Execute initialize_on_transport_ex and check it succeeded correctly.
 
1333
 
 
1334
        This involves checking that the disk objects were created, open with
 
1335
        the same format returned, and had the expected disk format.
 
1336
 
 
1337
        :param t: The transport to initialize on.
 
1338
        :param **kwargs: Additional arguments to pass to
 
1339
            initialize_on_transport_ex.
 
1340
        :return: the resulting repo, control dir tuple.
 
1341
        """
 
1342
        if not self.bzrdir_format.is_supported():
 
1343
            # Not initializable - not a failure either.
 
1344
            return None, None
 
1345
        repo, control, require_stacking, repo_policy = \
 
1346
            self.bzrdir_format.initialize_on_transport_ex(t, **kwargs)
 
1347
        if repo is not None:
 
1348
            # Repositories are open write-locked
 
1349
            self.assertTrue(repo.is_write_locked())
 
1350
            self.addCleanup(repo.unlock)
 
1351
        self.assertIsInstance(control, bzrdir.BzrDir)
 
1352
        opened = bzrdir.BzrDir.open(t.base)
 
1353
        expected_format = self.bzrdir_format
 
1354
        if isinstance(expected_format, bzrdir.RemoteBzrDirFormat):
 
1355
            # Current RemoteBzrDirFormat's do not reliably get network_name
 
1356
            # set, so we skip a number of tests for RemoteBzrDirFormat's.
 
1357
            self.assertIsInstance(control, RemoteBzrDir)
 
1358
        else:
 
1359
            if need_meta and isinstance(expected_format, (bzrdir.BzrDirFormat5,
 
1360
                bzrdir.BzrDirFormat6)):
 
1361
                # Pre-metadir formats change when we are making something that
 
1362
                # needs a metaformat, because clone is used for push.
 
1363
                expected_format = bzrdir.BzrDirMetaFormat1()
 
1364
            self.assertEqual(control._format.network_name(),
 
1365
                expected_format.network_name())
 
1366
            self.assertEqual(control._format.network_name(),
 
1367
                opened._format.network_name())
 
1368
        self.assertEqual(control.__class__, opened.__class__)
 
1369
        return repo, control
 
1370
 
 
1371
    def test_format_network_name(self):
 
1372
        # All control formats must have a network name.
 
1373
        dir = self.make_bzrdir('.')
 
1374
        format = dir._format
 
1375
        # We want to test that the network_name matches the actual format on
 
1376
        # disk. For local control dirsthat means that using network_name as a
 
1377
        # key in the registry gives back the same format. For remote obects
 
1378
        # we check that the network_name of the RemoteBzrDirFormat we have
 
1379
        # locally matches the actual format present on disk.
 
1380
        if isinstance(format, bzrdir.RemoteBzrDirFormat):
 
1381
            dir._ensure_real()
 
1382
            real_dir = dir._real_bzrdir
 
1383
            network_name = format.network_name()
 
1384
            self.assertEqual(real_dir._format.network_name(), network_name)
 
1385
        else:
 
1386
            registry = bzrdir.network_format_registry
 
1387
            network_name = format.network_name()
 
1388
            looked_up_format = registry.get(network_name)
 
1389
            self.assertEqual(format.__class__, looked_up_format.__class__)
 
1390
        # The network name must be a byte string.
 
1391
        self.assertIsInstance(network_name, str)
 
1392
 
1143
1393
    def test_open_not_bzrdir(self):
1144
1394
        # test the formats specific behaviour for no-content or similar dirs.
1145
1395
        self.assertRaises(NotBranchError,
1159
1409
        made_branch = made_control.create_branch()
1160
1410
        self.failUnless(isinstance(made_branch, bzrlib.branch.Branch))
1161
1411
        self.assertEqual(made_control, made_branch.bzrdir)
1162
 
        
 
1412
 
1163
1413
    def test_open_branch(self):
1164
1414
        if not self.bzrdir_format.is_supported():
1165
1415
            # unsupported formats are not loopback testable
1175
1425
        self.failUnless(isinstance(opened_branch, made_branch.__class__))
1176
1426
        self.failUnless(isinstance(opened_branch._format, made_branch._format.__class__))
1177
1427
 
 
1428
    def test_list_branches(self):
 
1429
        if not self.bzrdir_format.is_supported():
 
1430
            # unsupported formats are not loopback testable
 
1431
            # because the default open will not open them and
 
1432
            # they may not be initializable.
 
1433
            return
 
1434
        t = get_transport(self.get_url())
 
1435
        made_control = self.bzrdir_format.initialize(t.base)
 
1436
        made_repo = made_control.create_repository()
 
1437
        made_branch = made_control.create_branch()
 
1438
        branches = made_control.list_branches()
 
1439
        self.assertEquals(1, len(branches))
 
1440
        self.assertEquals(made_branch.base, branches[0].base)
 
1441
        try:
 
1442
            made_control.destroy_branch()
 
1443
        except errors.UnsupportedOperation:
 
1444
            pass # Not all bzrdirs support destroying directories
 
1445
        else:
 
1446
            self.assertEquals([], made_control.list_branches())
 
1447
 
1178
1448
    def test_create_repository(self):
1179
1449
        # a bzrdir can construct a repository for itself.
1180
1450
        if not self.bzrdir_format.is_supported():
1190
1460
        self.assertEqual(made_control, made_repo.bzrdir)
1191
1461
 
1192
1462
    def test_create_repository_shared(self):
1193
 
        # a bzrdir can create a shared repository or 
 
1463
        # a bzrdir can create a shared repository or
1194
1464
        # fail appropriately
1195
1465
        if not self.bzrdir_format.is_supported():
1196
1466
            # unsupported formats are not loopback testable
1208
1478
        self.assertTrue(made_repo.is_shared())
1209
1479
 
1210
1480
    def test_create_repository_nonshared(self):
1211
 
        # a bzrdir can create a non-shared repository 
 
1481
        # a bzrdir can create a non-shared repository
1212
1482
        if not self.bzrdir_format.is_supported():
1213
1483
            # unsupported formats are not loopback testable
1214
1484
            # because the default open will not open them and
1218
1488
        made_control = self.bzrdir_format.initialize(t.base)
1219
1489
        made_repo = made_control.create_repository(shared=False)
1220
1490
        self.assertFalse(made_repo.is_shared())
1221
 
        
 
1491
 
1222
1492
    def test_open_repository(self):
1223
1493
        if not self.bzrdir_format.is_supported():
1224
1494
            # unsupported formats are not loopback testable
1247
1517
        made_tree = self.createWorkingTreeOrSkip(made_control)
1248
1518
        self.failUnless(isinstance(made_tree, workingtree.WorkingTree))
1249
1519
        self.assertEqual(made_control, made_tree.bzrdir)
1250
 
        
 
1520
 
1251
1521
    def test_create_workingtree_revision(self):
1252
1522
        # a bzrdir can construct a working tree for itself @ a specific revision.
1253
1523
        t = self.get_transport()
1264
1534
        except errors.NotLocalUrl:
1265
1535
            raise TestSkipped("Can't make working tree on transport %r" % t)
1266
1536
        self.assertEqual(['a'], made_tree.get_parent_ids())
1267
 
        
 
1537
 
1268
1538
    def test_open_workingtree(self):
1269
1539
        if not self.bzrdir_format.is_supported():
1270
1540
            # unsupported formats are not loopback testable
1372
1642
            self.get_url('intermediate/child'))
1373
1643
        try:
1374
1644
            child_repo = innermost_control.open_repository()
1375
 
            # if there is a repository, then the format cannot ever hit this 
 
1645
            # if there is a repository, then the format cannot ever hit this
1376
1646
            # code path.
1377
1647
            return
1378
1648
        except errors.NoRepositoryPresent:
1393
1663
        made_control = self.bzrdir_format.initialize(url)
1394
1664
        try:
1395
1665
            child_repo = made_control.open_repository()
1396
 
            # if there is a repository, then the format cannot ever hit this 
 
1666
            # if there is a repository, then the format cannot ever hit this
1397
1667
            # code path.
1398
1668
            return
1399
1669
        except errors.NoRepositoryPresent:
1401
1671
        found_repo = made_control.find_repository()
1402
1672
        self.assertEqual(repo.bzrdir.root_transport.base,
1403
1673
                         found_repo.bzrdir.root_transport.base)
1404
 
        
 
1674
 
1405
1675
    def test_find_repository_standalone_with_containing_shared_repository(self):
1406
1676
        # find repo inside a standalone repo inside a shared repo finds the standalone repo
1407
1677
        try:
1434
1704
                            containing_repo.bzrdir.root_transport.base)
1435
1705
 
1436
1706
    def test_find_repository_with_nested_dirs_works(self):
1437
 
        # find repo inside a bzrdir inside a bzrdir inside a shared repo 
 
1707
        # find repo inside a bzrdir inside a bzrdir inside a shared repo
1438
1708
        # finds the outer shared repo.
1439
1709
        try:
1440
1710
            repo = self.make_repository('.', shared=True)
1447
1717
        made_control = self.bzrdir_format.initialize(url)
1448
1718
        try:
1449
1719
            child_repo = made_control.open_repository()
1450
 
            # if there is a repository, then the format cannot ever hit this 
 
1720
            # if there is a repository, then the format cannot ever hit this
1451
1721
            # code path.
1452
1722
            return
1453
1723
        except errors.NoRepositoryPresent:
1456
1726
            self.get_url('intermediate/child'))
1457
1727
        try:
1458
1728
            child_repo = innermost_control.open_repository()
1459
 
            # if there is a repository, then the format cannot ever hit this 
 
1729
            # if there is a repository, then the format cannot ever hit this
1460
1730
            # code path.
1461
1731
            return
1462
1732
        except errors.NoRepositoryPresent:
1464
1734
        found_repo = innermost_control.find_repository()
1465
1735
        self.assertEqual(repo.bzrdir.root_transport.base,
1466
1736
                         found_repo.bzrdir.root_transport.base)
1467
 
        
 
1737
 
1468
1738
    def test_can_and_needs_format_conversion(self):
1469
1739
        # check that we can ask an instance if its upgradable
1470
1740
        dir = self.make_bzrdir('.')
1471
1741
        if dir.can_convert_format():
1472
 
            # if its default updatable there must be an updater 
 
1742
            # if its default updatable there must be an updater
1473
1743
            # (we force the latest known format as downgrades may not be
1474
1744
            # available
1475
1745
            self.assertTrue(isinstance(dir._format.get_converter(
1476
1746
                format=dir._format), bzrdir.Converter))
1477
 
        dir.needs_format_conversion(None)
 
1747
        dir.needs_format_conversion(
 
1748
            bzrdir.BzrDirFormat.get_default_format())
 
1749
 
 
1750
    def test_backup_copies_existing(self):
 
1751
        tree = self.make_branch_and_tree('test')
 
1752
        self.build_tree(['test/a'])
 
1753
        tree.add(['a'], ['a-id'])
 
1754
        tree.commit('some data to be copied.')
 
1755
        old_url, new_url = tree.bzrdir.backup_bzrdir()
 
1756
        old_path = urlutils.local_path_from_url(old_url)
 
1757
        new_path = urlutils.local_path_from_url(new_url)
 
1758
        self.failUnlessExists(old_path)
 
1759
        self.failUnlessExists(new_path)
 
1760
        for (((dir_relpath1, _), entries1),
 
1761
             ((dir_relpath2, _), entries2)) in izip(
 
1762
                osutils.walkdirs(old_path),
 
1763
                osutils.walkdirs(new_path)):
 
1764
            self.assertEquals(dir_relpath1, dir_relpath2)
 
1765
            for f1, f2 in zip(entries1, entries2):
 
1766
                self.assertEquals(f1[0], f2[0])
 
1767
                self.assertEquals(f1[2], f2[2])
 
1768
                if f1[2] == "file":
 
1769
                    osutils.compare_files(open(f1[4]), open(f2[4]))
1478
1770
 
1479
1771
    def test_upgrade_new_instance(self):
1480
1772
        """Does an available updater work?"""
1484
1776
        dir.create_branch()
1485
1777
        self.createWorkingTreeOrSkip(dir)
1486
1778
        if dir.can_convert_format():
1487
 
            # if its default updatable there must be an updater 
 
1779
            # if its default updatable there must be an updater
1488
1780
            # (we force the latest known format as downgrades may not be
1489
1781
            # available
1490
1782
            pb = ui.ui_factory.nested_progress_bar()
1493
1785
            finally:
1494
1786
                pb.finished()
1495
1787
            # and it should pass 'check' now.
1496
 
            check_branch(bzrdir.BzrDir.open(self.get_url('.')).open_branch(),
1497
 
                         False)
 
1788
            check.check_dwim(self.get_url('.'), False, True, True)
1498
1789
 
1499
1790
    def test_format_description(self):
1500
1791
        dir = self.make_bzrdir('.')
1520
1811
            transport=transport)
1521
1812
        self.failUnless(transport.has('.bzr'))
1522
1813
        self.assertRaises((errors.FileExists, errors.DirectoryNotEmpty),
1523
 
            bd.retire_bzrdir, limit=0) 
 
1814
            bd.retire_bzrdir, limit=0)
1524
1815
 
1525
1816
 
1526
1817
class TestBreakLock(TestCaseWithBzrDir):
1527
1818
 
1528
 
    def setUp(self):
1529
 
        super(TestBreakLock, self).setUp()
1530
 
        # we want a UI factory that accepts canned input for the tests:
1531
 
        # while SilentUIFactory still accepts stdin, we need to customise
1532
 
        # ours
1533
 
        self.old_factory = bzrlib.ui.ui_factory
1534
 
        self.addCleanup(self.restoreFactory)
1535
 
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1536
 
 
1537
 
    def restoreFactory(self):
1538
 
        bzrlib.ui.ui_factory = self.old_factory
1539
 
 
1540
1819
    def test_break_lock_empty(self):
1541
1820
        # break lock on an empty bzrdir should work silently.
1542
1821
        dir = self.make_bzrdir('.')
1558
1837
            return
1559
1838
        # only one yes needed here: it should only be unlocking
1560
1839
        # the repo
1561
 
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
 
1840
        bzrlib.ui.ui_factory = CannedInputUIFactory([True])
1562
1841
        try:
1563
1842
            repo.bzrdir.break_lock()
1564
1843
        except NotImplementedError:
1579
1858
        thisdir = self.make_bzrdir('this')
1580
1859
        try:
1581
1860
            bzrlib.branch.BranchReferenceFormat().initialize(
1582
 
                thisdir, master)
 
1861
                thisdir, target_branch=master)
1583
1862
        except errors.IncompatibleFormat:
1584
1863
            return
1585
1864
        unused_repo = thisdir.create_repository()
1589
1868
            # two yes's : branch and repository. If the repo in this
1590
1869
            # dir is inappropriately accessed, 3 will be needed, and
1591
1870
            # we'll see that because the stream will be fully consumed
1592
 
            bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\n")
 
1871
            bzrlib.ui.ui_factory = CannedInputUIFactory([True, True, True])
1593
1872
            # determine if the repository will have been locked;
1594
1873
            this_repo_locked = \
1595
1874
                thisdir.open_repository().get_physical_lock_status()
1596
1875
            master.bzrdir.break_lock()
1597
1876
            if this_repo_locked:
1598
1877
                # only two ys should have been read
1599
 
                self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
 
1878
                self.assertEqual([True],
 
1879
                    bzrlib.ui.ui_factory.responses)
1600
1880
            else:
1601
1881
                # only one y should have been read
1602
 
                self.assertEqual("y\ny\n", bzrlib.ui.ui_factory.stdin.read())
 
1882
                self.assertEqual([True, True],
 
1883
                    bzrlib.ui.ui_factory.responses)
1603
1884
            # we should be able to lock a newly opened branch now
1604
1885
            branch = master.bzrdir.open_branch()
1605
1886
            branch.lock_write()
1615
1896
        self.assertRaises(errors.LockBroken, master.unlock)
1616
1897
 
1617
1898
    def test_break_lock_tree(self):
1618
 
        # break lock with a tree should unlock the tree but not try the 
1619
 
        # branch explicitly. However this is very hard to test for as we 
1620
 
        # dont have a tree reference class, nor is one needed; 
 
1899
        # break lock with a tree should unlock the tree but not try the
 
1900
        # branch explicitly. However this is very hard to test for as we
 
1901
        # dont have a tree reference class, nor is one needed;
1621
1902
        # the worst case if this code unlocks twice is an extra question
1622
1903
        # being asked.
1623
1904
        tree = self.make_branch_and_tree('.')
1624
1905
        tree.lock_write()
1625
1906
        # three yes's : tree, branch and repository.
1626
 
        bzrlib.ui.ui_factory.stdin = StringIO("y\ny\ny\ny\n")
 
1907
        bzrlib.ui.ui_factory = CannedInputUIFactory([True, True, True])
1627
1908
        try:
1628
1909
            tree.bzrdir.break_lock()
1629
1910
        except (NotImplementedError, errors.LockActive):
1633
1914
            # object.
1634
1915
            tree.unlock()
1635
1916
            return
1636
 
        self.assertEqual("y\n", bzrlib.ui.ui_factory.stdin.read())
 
1917
        self.assertEqual([True],
 
1918
                bzrlib.ui.ui_factory.responses)
1637
1919
        lock_tree = tree.bzrdir.open_workingtree()
1638
1920
        lock_tree.lock_write()
1639
1921
        lock_tree.unlock()
1645
1927
    def test_get_config(self):
1646
1928
        my_dir = self.make_bzrdir('.')
1647
1929
        config = my_dir.get_config()
1648
 
        if config is None:
1649
 
            self.assertFalse(
1650
 
                isinstance(my_dir, (bzrdir.BzrDirMeta1, RemoteBzrDir)),
1651
 
                "%r should support configs" % my_dir)
1652
 
            raise TestNotApplicable(
1653
 
                'This BzrDir format does not support configs.')
1654
 
        config.set_default_stack_on('http://example.com')
 
1930
        try:
 
1931
            config.set_default_stack_on('http://example.com')
 
1932
        except errors.BzrError, e:
 
1933
            if 'Cannot set config' in str(e):
 
1934
                self.assertFalse(
 
1935
                    isinstance(my_dir, (bzrdir.BzrDirMeta1, RemoteBzrDir)),
 
1936
                    "%r should support configs" % my_dir)
 
1937
                raise TestNotApplicable(
 
1938
                    'This BzrDir format does not support configs.')
 
1939
            else:
 
1940
                raise
1655
1941
        self.assertEqual('http://example.com', config.get_default_stack_on())
1656
1942
        my_dir2 = bzrdir.BzrDir.open(self.get_url('.'))
1657
1943
        config2 = my_dir2.get_config()
1661
1947
class ChrootedBzrDirTests(ChrootedTestCase):
1662
1948
 
1663
1949
    def test_find_repository_no_repository(self):
1664
 
        # loopback test to check the current format fails to find a 
 
1950
        # loopback test to check the current format fails to find a
1665
1951
        # share repository correctly.
1666
1952
        if not self.bzrdir_format.is_supported():
1667
1953
            # unsupported formats are not loopback testable
1676
1962
        made_control = self.bzrdir_format.initialize(self.get_url('subdir'))
1677
1963
        try:
1678
1964
            repo = made_control.open_repository()
1679
 
            # if there is a repository, then the format cannot ever hit this 
 
1965
            # if there is a repository, then the format cannot ever hit this
1680
1966
            # code path.
1681
1967
            return
1682
1968
        except errors.NoRepositoryPresent: