/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2012, 2016 Canonical Ltd
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
from breezy import (
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
18
    errors,
7122.4.1 by Jelmer Vernooij
Some refactoring; check for required functionality rather than specific implementation (InventoryTree).
19
    tests,
20
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
21
from breezy.tests.per_tree import TestCaseWithTree
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
22
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
23
7122.4.1 by Jelmer Vernooij
Some refactoring; check for required functionality rather than specific implementation (InventoryTree).
24
class Path2IdTests(TestCaseWithTree):
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
25
26
    def setUp(self):
7122.4.1 by Jelmer Vernooij
Some refactoring; check for required functionality rather than specific implementation (InventoryTree).
27
        super(Path2IdTests, self).setUp()
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
28
        work_a = self.make_branch_and_tree('wta')
6745.1.1 by Jelmer Vernooij
Avoid explicitly setting file ids or guard it by checking
29
        if not work_a.supports_setting_file_ids():
6821.2.2 by Jelmer Vernooij
More foreign fixes.
30
            self.skipTest("working tree does not support setting file ids")
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
31
        self.build_tree(['wta/bla', 'wta/dir/', 'wta/dir/file'])
7143.15.2 by Jelmer Vernooij
Run autopep8.
32
        work_a.add(['bla', 'dir', 'dir/file'],
33
                   [b'bla-id', b'dir-id', b'file-id'])
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
34
        work_a.commit('add files')
35
        self.tree_a = self.workingtree_to_test_tree(work_a)
36
37
    def test_path2id(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
38
        self.assertEqual(b'bla-id', self.tree_a.path2id('bla'))
39
        self.assertEqual(b'dir-id', self.tree_a.path2id('dir'))
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
40
        self.assertIs(None, self.tree_a.path2id('idontexist'))
41
42
    def test_path2id_list(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
43
        self.assertEqual(b'bla-id', self.tree_a.path2id(['bla']))
44
        self.assertEqual(b'dir-id', self.tree_a.path2id(['dir']))
45
        self.assertEqual(b'file-id', self.tree_a.path2id(['dir', 'file']))
7358.14.1 by Jelmer Vernooij
Remove Tree.get_root_id() in favour of Tree.path2id('').
46
        self.assertEqual(self.tree_a.path2id(''),
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
47
                         self.tree_a.path2id([]))
6478.2.1 by Jelmer Vernooij
Accept path element list as argument to Tree.path2id.
48
        self.assertIs(None, self.tree_a.path2id(['idontexist']))
49
        self.assertIs(None, self.tree_a.path2id(['dir', 'idontexist']))
50
51
    def test_id2path(self):
52
        self.addCleanup(self.tree_a.lock_read().unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
53
        self.assertEqual('bla', self.tree_a.id2path(b'bla-id'))
54
        self.assertEqual('dir', self.tree_a.id2path(b'dir-id'))
55
        self.assertEqual('dir/file', self.tree_a.id2path(b'file-id'))
56
        self.assertRaises(errors.NoSuchId, self.tree_a.id2path, b'nonexistant')
7122.4.1 by Jelmer Vernooij
Some refactoring; check for required functionality rather than specific implementation (InventoryTree).
57
7404.4.1 by Jelmer Vernooij
Add support for tree references in id2path.
58
    def skip_if_no_reference(self, tree):
59
        if not getattr(tree, 'supports_tree_reference', lambda: False)():
60
            raise tests.TestNotApplicable('Tree references not supported')
61
62
    def create_nested(self):
63
        work_tree = self.make_branch_and_tree('wt')
64
        with work_tree.lock_write():
65
            self.skip_if_no_reference(work_tree)
66
            subtree = self.make_branch_and_tree('wt/subtree')
67
            self.build_tree(['wt/subtree/a'])
68
            subtree.add(['a'])
69
            subtree.commit('foo')
70
            work_tree.add_reference(subtree)
71
        tree = self._convert_tree(work_tree)
72
        self.skip_if_no_reference(tree)
73
        return tree, subtree
74
75
    def test_path2id_nested_tree(self):
76
        tree, subtree = self.create_nested()
77
        self.assertIsNot(None, tree.path2id('subtree'))
78
        self.assertIsNot(None, tree.path2id('subtree/a'))
79
        self.assertEqual('subtree', tree.id2path(tree.path2id('subtree')))
80
        self.assertEqual('subtree/a', tree.id2path(tree.path2id('subtree/a')))
7450.1.1 by Jelmer Vernooij
Support recurse argument to id2path.
81
        self.assertIsNot('subtree/a', tree.id2path(tree.path2id('subtree/a'), recurse='down'))
82
        self.assertRaises(errors.NoSuchId, tree.id2path, tree.path2id('subtree/a'), recurse='none')
7404.4.1 by Jelmer Vernooij
Add support for tree references in id2path.
83
7143.15.2 by Jelmer Vernooij
Run autopep8.
84
7122.4.1 by Jelmer Vernooij
Some refactoring; check for required functionality rather than specific implementation (InventoryTree).
85
class Path2IdsTests(TestCaseWithTree):
86
87
    def test_paths2ids_recursive(self):
88
        work_tree = self.make_branch_and_tree('tree')
89
        self.build_tree(['tree/dir/', 'tree/dir/file'])
90
        work_tree.add(['dir', 'dir/file'])
91
        if not work_tree.supports_setting_file_ids():
92
            raise tests.TestNotApplicable(
93
                "test not applicable on non-inventory tests")
94
        tree = self._convert_tree(work_tree)
95
        tree.lock_read()
96
        self.addCleanup(tree.unlock)
97
        self.assertEqual({tree.path2id('dir'), tree.path2id('dir/file')},
98
                         tree.paths2ids(['dir']))
99
100
    def test_paths2ids_forget_old(self):
101
        work_tree = self.make_branch_and_tree('tree')
102
        self.build_tree(['tree/file'])
103
        work_tree.add('file')
104
        work_tree.commit('commit old state')
105
        work_tree.remove('file')
106
        if not work_tree.supports_setting_file_ids():
107
            raise tests.TestNotApplicable(
108
                "test not applicable on non-inventory tests")
109
        tree = self._convert_tree(work_tree)
110
        tree.lock_read()
111
        self.addCleanup(tree.unlock)
112
        self.assertEqual(set([]), tree.paths2ids(['file'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
113
                                                 require_versioned=False))