~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010 Canonical Ltd
 
1
# Copyright (C) 2010, 2011, 2012, 2016 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,
24
26
    errors,
25
27
    repository,
26
28
    revision as _mod_revision,
27
29
    transport,
28
30
    workingtree,
29
31
    )
 
32
from bzrlib.remote import RemoteBzrDirFormat
30
33
from bzrlib.tests import (
 
34
    TestNotApplicable,
31
35
    TestSkipped,
32
36
    )
33
37
from bzrlib.tests.per_bzrdir import TestCaseWithBzrDir
121
125
                    self.assertTrue(S_ISDIR(target.stat(path).st_mode))
122
126
                    directories.append(path)
123
127
                else:
124
 
                    self.assertEqualDiff(source.get(path).read(),
125
 
                                         target.get(path).read(),
 
128
                    self.assertEqualDiff(source.get_bytes(path),
 
129
                                         target.get_bytes(path),
126
130
                                         "text for file %r differs:\n" % path)
127
131
 
128
132
    def assertRepositoryHasSameItems(self, left_repo, right_repo):
159
163
                for file_id, revision_id in text_index.iterkeys():
160
164
                    desired_files.append(
161
165
                        (file_id, revision_id, (file_id, revision_id)))
162
 
                left_texts = list(left_repo.iter_files_bytes(desired_files))
163
 
                right_texts = list(right_repo.iter_files_bytes(desired_files))
 
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)]
164
172
                left_texts.sort()
165
173
                right_texts.sort()
166
174
                self.assertEqual(left_texts, right_texts)
328
336
                                     './.bzr/repository',
329
337
                                     ])
330
338
        self.assertRepositoryHasSameItems(tree.branch.repository,
331
 
            target.open_repository())
 
339
            target.open_branch().repository)
332
340
        target.open_workingtree().revert()
333
341
 
334
342
    def test_revert_inventory(self):
348
356
                                     './.bzr/repository',
349
357
                                     ])
350
358
        self.assertRepositoryHasSameItems(tree.branch.repository,
351
 
            target.open_repository())
 
359
            target.open_branch().repository)
352
360
 
353
361
        target.open_workingtree().revert()
354
362
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
360
368
                                     './.bzr/repository',
361
369
                                     ])
362
370
        self.assertRepositoryHasSameItems(tree.branch.repository,
363
 
            target.open_repository())
 
371
            target.open_branch().repository)
364
372
 
365
373
    def test_clone_bzrdir_tree_branch_reference(self):
366
374
        # a tree with a branch reference (aka a checkout)
368
376
        referenced_branch = self.make_branch('referencced')
369
377
        dir = self.make_bzrdir('source')
370
378
        try:
371
 
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
372
 
                target_branch=referenced_branch)
 
379
            dir.set_branch_reference(referenced_branch)
373
380
        except errors.IncompatibleFormat:
374
381
            # this is ok too, not all formats have to support references.
375
382
            return
412
419
        referenced_branch = self.make_branch('referencced')
413
420
        dir = self.make_bzrdir('source')
414
421
        try:
415
 
            reference = bzrlib.branch.BranchReferenceFormat().initialize(dir,
416
 
                target_branch=referenced_branch)
 
422
            dir.set_branch_reference(referenced_branch)
417
423
        except errors.IncompatibleFormat:
418
424
            # this is ok too, not all formats have to support references.
419
425
            return
500
506
        self.assertNotEqual(dir.transport.base, target.transport.base)
501
507
        self.assertDirectoriesEqual(dir.root_transport, target.root_transport,
502
508
                                    [
503
 
                                     './.bzr/branch/branch.conf',
504
 
                                     './.bzr/branch/parent',
 
509
                                     './.bzr/branch',
505
510
                                     './.bzr/checkout/dirstate',
506
511
                                     './.bzr/checkout/stat-cache',
507
512
                                     './.bzr/checkout/inventory',
597
602
        self.assertTrue(isinstance(found_transport, transport.Transport))
598
603
        # and the dir which has been initialized for us must exist.
599
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()))