/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')
2100.3.8 by Aaron Bentley
Add add_reference
51
        self.assertEqual(tree.path2id('sub-tree'), 'sub-tree-root-id')
52
        self.assertEqual(tree.inventory['sub-tree-root-id'].kind, 
53
                         'tree-reference')
2100.3.19 by Aaron Bentley
Ensure commit preserves reference revision
54
        tree.commit('commit reference')
55
        entry = tree.basis_tree().inventory['sub-tree-root-id']
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
56
        sub_tree = tree.get_nested_tree(entry)
2100.3.19 by Aaron Bentley
Ensure commit preserves reference revision
57
        self.assertEqual(sub_tree.last_revision(), entry.reference_revision)
58
        
2100.3.8 by Aaron Bentley
Add add_reference
59
60
    def test_add_reference_same_root(self):
61
        tree = self.make_branch_and_tree('tree')
62
        self.build_tree(['tree/file1'])
63
        tree.add('file1', 'file1-id')
64
        tree.set_root_id('root-id')
65
        sub_tree = self.make_branch_and_tree('tree/sub-tree')
66
        sub_tree.set_root_id('root-id')
67
        try:
68
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
69
                              sub_tree)
70
        except errors.UnsupportedOperation:
2255.6.3 by Aaron Bentley
tweak tests
71
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.8 by Aaron Bentley
Add add_reference
72
            raise tests.TestSkipped('Tree format does not support references')
73
74
    def test_root_present(self):
75
        """Subtree root is present, though not the working tree root"""
76
        tree, sub_tree = self.make_trees()
77
        sub_tree.set_root_id('file1-id')
78
        try:
79
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
80
                              sub_tree)
81
        except errors.UnsupportedOperation:
2255.6.3 by Aaron Bentley
tweak tests
82
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.8 by Aaron Bentley
Add add_reference
83
            raise tests.TestSkipped('Tree format does not support references')
84
85
    def test_add_non_subtree(self):
86
        tree, sub_tree = self.make_trees()
87
        os.rename('tree/sub-tree', 'sibling')
88
        sibling = workingtree.WorkingTree.open('sibling')
89
        try:
90
            self.assertRaises(errors.BadReferenceTarget, tree.add_reference, 
91
                              sibling)
92
        except errors.UnsupportedOperation:
2255.6.3 by Aaron Bentley
tweak tests
93
            assert tree.__class__ in TREES_NOT_SUPPORTING_REFERENCES
2100.3.8 by Aaron Bentley
Add add_reference
94
            raise tests.TestSkipped('Tree format does not support references')
95
2100.3.27 by Aaron Bentley
Enable nested commits
96
    def test_get_nested_tree(self):
97
        tree, sub_tree = self.make_nested_trees()
98
        sub_tree2 = tree.get_nested_tree(tree.inventory['sub-tree-root-id'])
99
        self.assertEqual(sub_tree.basedir, sub_tree2.basedir)
100
        sub_tree2 = tree.get_nested_tree(tree.inventory['sub-tree-root-id'],
101
                                         'sub-tree')
102
103
    def test_iter_nested_trees(self):
104
        tree, sub_tree = self.make_nested_trees()
105
        iterator = tree.iter_nested_trees()
106
        sub_tree2 = iterator.next()
107
        self.assertEqual(sub_tree.basedir, sub_tree2.basedir)
108
        self.assertRaises(StopIteration, iterator.next)