~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/workingtree_implementations/test_add_reference.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import os
 
18
 
 
19
from bzrlib import errors, tests, workingtree, workingtree_4
 
20
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
 
21
 
 
22
TREES_NOT_SUPPORTING_REFERENCES = (workingtree.WorkingTree2,
 
23
                                   workingtree.WorkingTree3,
 
24
                                   workingtree_4.WorkingTree4)
 
25
 
 
26
 
 
27
class TestBasisInventory(TestCaseWithWorkingTree):
 
28
 
 
29
    def make_trees(self):
 
30
        tree = self.make_branch_and_tree('tree')
 
31
        tree.set_root_id('root-id')
 
32
        self.build_tree(['tree/file1'])
 
33
        tree.add('file1', 'file1-id')
 
34
        sub_tree = self.make_branch_and_tree('tree/sub-tree')
 
35
        sub_tree.set_root_id('sub-tree-root-id')
 
36
        sub_tree.commit('commit', rev_id='sub_1')
 
37
        return tree, sub_tree
 
38
 
 
39
    def make_nested_trees(self):
 
40
        tree, sub_tree = self.make_trees()
 
41
        try:
 
42
            tree.add_reference(sub_tree)
 
43
        except errors.UnsupportedOperation:
 
44
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
 
45
            raise tests.TestSkipped('Tree format does not support references')
 
46
        return tree, sub_tree
 
47
 
 
48
    def test_add_reference(self):
 
49
        self.make_nested_trees()
 
50
        tree = workingtree.WorkingTree.open('tree')
 
51
        tree.lock_write()
 
52
        try:
 
53
            self.assertEqual(tree.path2id('sub-tree'), 'sub-tree-root-id')
 
54
            self.assertEqual(tree.inventory['sub-tree-root-id'].kind,
 
55
                             'tree-reference')
 
56
            tree.commit('commit reference')
 
57
            basis = tree.basis_tree()
 
58
            basis.lock_read()
 
59
            try:
 
60
                sub_tree = tree.get_nested_tree('sub-tree-root-id')
 
61
                self.assertEqual(sub_tree.last_revision(),
 
62
                    tree.get_reference_revision('sub-tree-root-id'))
 
63
            finally:
 
64
                basis.unlock()
 
65
        finally:
 
66
            tree.unlock()
 
67
 
 
68
    def test_add_reference_same_root(self):
 
69
        tree = self.make_branch_and_tree('tree')
 
70
        self.build_tree(['tree/file1'])
 
71
        tree.add('file1', 'file1-id')
 
72
        tree.set_root_id('root-id')
 
73
        sub_tree = self.make_branch_and_tree('tree/sub-tree')
 
74
        sub_tree.set_root_id('root-id')
 
75
        try:
 
76
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
 
77
                              sub_tree)
 
78
        except errors.UnsupportedOperation:
 
79
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
 
80
            raise tests.TestSkipped('Tree format does not support references')
 
81
 
 
82
    def test_root_present(self):
 
83
        """Subtree root is present, though not the working tree root"""
 
84
        tree, sub_tree = self.make_trees()
 
85
        sub_tree.set_root_id('file1-id')
 
86
        try:
 
87
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
 
88
                              sub_tree)
 
89
        except errors.UnsupportedOperation:
 
90
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
 
91
            raise tests.TestSkipped('Tree format does not support references')
 
92
 
 
93
    def test_add_non_subtree(self):
 
94
        tree, sub_tree = self.make_trees()
 
95
        os.rename('tree/sub-tree', 'sibling')
 
96
        sibling = workingtree.WorkingTree.open('sibling')
 
97
        try:
 
98
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
 
99
                              sibling)
 
100
        except errors.UnsupportedOperation:
 
101
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
 
102
            raise tests.TestSkipped('Tree format does not support references')
 
103
 
 
104
    def test_get_nested_tree(self):
 
105
        tree, sub_tree = self.make_nested_trees()
 
106
        tree.lock_read()
 
107
        try:
 
108
            sub_tree2 = tree.get_nested_tree('sub-tree-root-id')
 
109
            self.assertEqual(sub_tree.basedir, sub_tree2.basedir)
 
110
            sub_tree2 = tree.get_nested_tree('sub-tree-root-id', 'sub-tree')
 
111
        finally:
 
112
            tree.unlock()