/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.5 by John Arbash Meinel
Merge bzr.dev 5021 to resolve NEWS
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010 Canonical Ltd
1911.3.1 by John Arbash Meinel
Updated smart_add so that the AddAction can return a custom id.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1911.3.1 by John Arbash Meinel
Updated smart_add so that the AddAction can return a custom id.
16
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
17
from .. import (
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
18
    add,
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
19
    cache_utf8,
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
20
    errors,
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
21
    tests,
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
22
    )
6670.4.1 by Jelmer Vernooij
Update imports.
23
from ..bzr import (
24
    inventory,
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from ..sixish import (
6963.1.1 by Jelmer Vernooij
Fix a bunch of tests on python3.
27
    StringIO,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
28
    )
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
29
30
31
class AddCustomIDAction(add.AddAction):
1911.3.1 by John Arbash Meinel
Updated smart_add so that the AddAction can return a custom id.
32
33
    def __call__(self, inv, parent_ie, path, kind):
34
        # The first part just logs if appropriate
35
        # Now generate a custom id
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
36
        file_id = cache_utf8.encode(kind + '-' + path.replace('/', '%'))
1911.3.1 by John Arbash Meinel
Updated smart_add so that the AddAction can return a custom id.
37
        if self.should_print:
3985.2.5 by Daniel Watkins
Reverted some irrelevant changes.
38
            self._to_file.write('added %s with id %s\n'
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
39
                                % (path, file_id.decode('utf-8')))
1911.3.1 by John Arbash Meinel
Updated smart_add so that the AddAction can return a custom id.
40
        return file_id
41
42
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
43
class TestAddFrom(tests.TestCaseWithTransport):
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
44
    """Tests for AddFromBaseAction"""
45
46
    def make_base_tree(self):
47
        self.base_tree = self.make_branch_and_tree('base')
48
        self.build_tree(['base/a', 'base/b',
49
                         'base/dir/', 'base/dir/a',
50
                         'base/dir/subdir/',
51
                         'base/dir/subdir/b',
7143.15.2 by Jelmer Vernooij
Run autopep8.
52
                         ])
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
53
        self.base_tree.add(['a', 'b', 'dir', 'dir/a',
54
                            'dir/subdir', 'dir/subdir/b'])
55
        self.base_tree.commit('creating initial tree.')
56
57
    def add_helper(self, base_tree, base_path, new_tree, file_list,
58
                   should_print=False):
6963.1.1 by Jelmer Vernooij
Fix a bunch of tests on python3.
59
        to_file = StringIO()
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
60
        base_tree.lock_read()
61
        try:
62
            new_tree.lock_write()
63
            try:
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
64
                action = add.AddFromBaseAction(base_tree, base_path,
65
                                               to_file=to_file,
66
                                               should_print=should_print)
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
67
                new_tree.smart_add(file_list, action=action)
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
68
            finally:
69
                new_tree.unlock()
70
        finally:
71
            base_tree.unlock()
72
        return to_file.getvalue()
73
74
    def test_copy_all(self):
75
        self.make_base_tree()
76
        new_tree = self.make_branch_and_tree('new')
77
        files = ['a', 'b',
78
                 'dir/', 'dir/a',
79
                 'dir/subdir/',
80
                 'dir/subdir/b',
7143.15.2 by Jelmer Vernooij
Run autopep8.
81
                 ]
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
82
        self.build_tree(['new/' + fn for fn in files])
83
        self.add_helper(self.base_tree, '', new_tree, ['new'])
84
85
        for fn in files:
86
            base_file_id = self.base_tree.path2id(fn)
87
            new_file_id = new_tree.path2id(fn)
88
            self.assertEqual(base_file_id, new_file_id)
89
90
    def test_copy_from_dir(self):
91
        self.make_base_tree()
92
        new_tree = self.make_branch_and_tree('new')
93
94
        self.build_tree(['new/a', 'new/b', 'new/c',
95
                         'new/subdir/', 'new/subdir/b', 'new/subdir/d'])
7358.14.1 by Jelmer Vernooij
Remove Tree.get_root_id() in favour of Tree.path2id('').
96
        new_tree.set_root_id(self.base_tree.path2id(''))
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
97
        self.add_helper(self.base_tree, 'dir', new_tree, ['new'])
98
1731.1.46 by Aaron Bentley
merge from bzr.dev
99
        # We know 'a' and 'b' exist in the root, and they are being added
100
        # in a new 'root'. Since ROOT ids have been set as the same, we will
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
101
        # use those ids
102
        self.assertEqual(self.base_tree.path2id('a'),
103
                         new_tree.path2id('a'))
104
        self.assertEqual(self.base_tree.path2id('b'),
105
                         new_tree.path2id('b'))
106
107
        # Because we specified 'dir/' as the base path, and we have
108
        # nothing named 'subdir' in the base tree, we should grab the
109
        # ids from there
110
        self.assertEqual(self.base_tree.path2id('dir/subdir'),
111
                         new_tree.path2id('subdir'))
112
        self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
113
                         new_tree.path2id('subdir/b'))
114
115
        # These should get newly generated ids
116
        c_id = new_tree.path2id('c')
117
        self.assertNotEqual(None, c_id)
2255.7.57 by Robert Collins
Fix test_smart_add tests for dirstate - mainly inventory outside locks issues.
118
        self.base_tree.lock_read()
119
        self.addCleanup(self.base_tree.unlock)
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
120
        self.assertRaises(errors.NoSuchId, self.base_tree.id2path, c_id)
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
121
122
        d_id = new_tree.path2id('subdir/d')
123
        self.assertNotEqual(None, d_id)
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
124
        self.assertRaises(errors.NoSuchId, self.base_tree.id2path, d_id)
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
125
126
    def test_copy_existing_dir(self):
127
        self.make_base_tree()
128
        new_tree = self.make_branch_and_tree('new')
129
        self.build_tree(['new/subby/', 'new/subby/a', 'new/subby/b'])
130
131
        subdir_file_id = self.base_tree.path2id('dir/subdir')
132
        new_tree.add(['subby'], [subdir_file_id])
133
        self.add_helper(self.base_tree, '', new_tree, ['new'])
134
        # Because 'subby' already points to subdir, we should add
135
        # 'b' with the same id
136
        self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
137
                         new_tree.path2id('subby/b'))
138
139
        # 'subby/a' should be added with a new id because there is no
140
        # matching path or child of 'subby'.
141
        a_id = new_tree.path2id('subby/a')
142
        self.assertNotEqual(None, a_id)
2255.7.57 by Robert Collins
Fix test_smart_add tests for dirstate - mainly inventory outside locks issues.
143
        self.base_tree.lock_read()
144
        self.addCleanup(self.base_tree.unlock)
7397.4.7 by Jelmer Vernooij
Remove Tree.has_id.
145
        self.assertRaises(errors.NoSuchId, self.base_tree.id2path, a_id)
1911.3.2 by John Arbash Meinel
Adding the AddFromBaseAction, which tries to reuse file ids from another tree
146
147
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
148
class TestAddActions(tests.TestCase):
1185.53.2 by Michael Ellerman
Add tests for bzr add --dry-run
149
1757.2.6 by Robert Collins
Steps towards a nicer smart add - unwind the conditional add logic - having parents not in the inventory was overly complicating the rest of the code.
150
    def test_quiet(self):
151
        self.run_action("")
152
153
    def test__print(self):
3985.2.1 by Daniel Watkins
Updated tests for new behaviour.
154
        self.run_action("adding path\n")
1757.2.6 by Robert Collins
Steps towards a nicer smart add - unwind the conditional add logic - having parents not in the inventory was overly complicating the rest of the code.
155
156
    def run_action(self, output):
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
157
        inv = inventory.Inventory()
6963.1.1 by Jelmer Vernooij
Fix a bunch of tests on python3.
158
        stdout = StringIO()
5013.2.1 by Vincent Ladeuil
Fix imports in test_smart_add.py.
159
        action = add.AddAction(to_file=stdout, should_print=bool(output))
1185.53.2 by Michael Ellerman
Add tests for bzr add --dry-run
160
2568.2.5 by Robert Collins
* ``bzrlib.add.FastPath`` is now private and moved to
161
        self.apply_redirected(None, stdout, None, action, inv, None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
162
                              'path', 'file')
1185.53.2 by Michael Ellerman
Add tests for bzr add --dry-run
163
        self.assertEqual(stdout.getvalue(), output)