/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2100.3.8 by Aaron Bentley
Add add_reference
1
# Copyright (C) 2006 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
2255.6.3 by Aaron Bentley
tweak tests
19
from bzrlib import errors, tests, workingtree, workingtree_4
2100.3.8 by Aaron Bentley
Add add_reference
20
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
21
2255.6.3 by Aaron Bentley
tweak tests
22
TREES_NOT_SUPPORTING_REFERENCES = (workingtree.WorkingTree2,
23
                                   workingtree.WorkingTree3,
24
                                   workingtree_4.WorkingTree4)
25
26
2100.3.8 by Aaron Bentley
Add add_reference
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')
2100.3.19 by Aaron Bentley
Ensure commit preserves reference revision
36
        sub_tree.commit('commit', rev_id='sub_1')
2100.3.8 by Aaron Bentley
Add add_reference
37
        return tree, sub_tree
38
2100.3.27 by Aaron Bentley
Enable nested commits
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:
2255.6.3 by Aaron Bentley
tweak tests
44
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.27 by Aaron Bentley
Enable nested commits
45
            raise tests.TestSkipped('Tree format does not support references')
46
        return tree, sub_tree
47
2100.3.8 by Aaron Bentley
Add add_reference
48
    def test_add_reference(self):
2100.3.27 by Aaron Bentley
Enable nested commits
49
        self.make_nested_trees()
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
50
        tree = workingtree.WorkingTree.open('tree')
2255.6.9 by Aaron Bentley
Fix test locking for dirstate
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
                entry = basis.inventory['sub-tree-root-id']
61
                sub_tree = tree.get_nested_tree(entry)
62
                self.assertEqual(sub_tree.last_revision(),
63
                                 entry.reference_revision)
64
            finally:
65
                basis.unlock()
66
        finally:
67
            tree.unlock()
2100.3.19 by Aaron Bentley
Ensure commit preserves reference revision
68
        
2100.3.8 by Aaron Bentley
Add add_reference
69
70
    def test_add_reference_same_root(self):
71
        tree = self.make_branch_and_tree('tree')
72
        self.build_tree(['tree/file1'])
73
        tree.add('file1', 'file1-id')
74
        tree.set_root_id('root-id')
75
        sub_tree = self.make_branch_and_tree('tree/sub-tree')
76
        sub_tree.set_root_id('root-id')
77
        try:
78
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
79
                              sub_tree)
80
        except errors.UnsupportedOperation:
2255.6.3 by Aaron Bentley
tweak tests
81
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.8 by Aaron Bentley
Add add_reference
82
            raise tests.TestSkipped('Tree format does not support references')
83
84
    def test_root_present(self):
85
        """Subtree root is present, though not the working tree root"""
86
        tree, sub_tree = self.make_trees()
87
        sub_tree.set_root_id('file1-id')
88
        try:
89
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
90
                              sub_tree)
91
        except errors.UnsupportedOperation:
2255.6.3 by Aaron Bentley
tweak tests
92
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.8 by Aaron Bentley
Add add_reference
93
            raise tests.TestSkipped('Tree format does not support references')
94
95
    def test_add_non_subtree(self):
96
        tree, sub_tree = self.make_trees()
97
        os.rename('tree/sub-tree', 'sibling')
98
        sibling = workingtree.WorkingTree.open('sibling')
99
        try:
100
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
101
                              sibling)
102
        except errors.UnsupportedOperation:
2255.6.3 by Aaron Bentley
tweak tests
103
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.8 by Aaron Bentley
Add add_reference
104
            raise tests.TestSkipped('Tree format does not support references')
105
2100.3.27 by Aaron Bentley
Enable nested commits
106
    def test_get_nested_tree(self):
107
        tree, sub_tree = self.make_nested_trees()
2255.6.9 by Aaron Bentley
Fix test locking for dirstate
108
        tree.lock_read()
109
        try:
110
            sub_tree2 = tree.get_nested_tree(
111
                tree.inventory['sub-tree-root-id'])
112
            self.assertEqual(sub_tree.basedir, sub_tree2.basedir)
113
            sub_tree2 = tree.get_nested_tree(
114
                tree.inventory['sub-tree-root-id'], 'sub-tree')
115
        finally:
116
            tree.unlock()
2100.3.27 by Aaron Bentley
Enable nested commits
117
118
    def test_iter_nested_trees(self):
119
        tree, sub_tree = self.make_nested_trees()
120
        iterator = tree.iter_nested_trees()
121
        sub_tree2 = iterator.next()
122
        self.assertEqual(sub_tree.basedir, sub_tree2.basedir)
123
        self.assertRaises(StopIteration, iterator.next)