/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_smart_add.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-19 23:18:42 UTC
  • mto: (7490.3.4 work)
  • mto: This revision was merged to the branch mainline in revision 7495.
  • Revision ID: jelmer@jelmer.uk-20200219231842-agwjh2db66cpajqg
Consistent return values.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from .. import (
 
18
    add,
 
19
    cache_utf8,
 
20
    errors,
 
21
    tests,
 
22
    )
 
23
from ..bzr import (
 
24
    inventory,
 
25
    )
 
26
from ..sixish import (
 
27
    StringIO,
 
28
    )
 
29
 
 
30
 
 
31
class AddCustomIDAction(add.AddAction):
 
32
 
 
33
    def __call__(self, inv, parent_ie, path, kind):
 
34
        # The first part just logs if appropriate
 
35
        # Now generate a custom id
 
36
        file_id = cache_utf8.encode(kind + '-' + path.replace('/', '%'))
 
37
        if self.should_print:
 
38
            self._to_file.write('added %s with id %s\n'
 
39
                                % (path, file_id.decode('utf-8')))
 
40
        return file_id
 
41
 
 
42
 
 
43
class TestAddFrom(tests.TestCaseWithTransport):
 
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',
 
52
                         ])
 
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):
 
59
        to_file = StringIO()
 
60
        base_tree.lock_read()
 
61
        try:
 
62
            new_tree.lock_write()
 
63
            try:
 
64
                action = add.AddFromBaseAction(base_tree, base_path,
 
65
                                               to_file=to_file,
 
66
                                               should_print=should_print)
 
67
                new_tree.smart_add(file_list, action=action)
 
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',
 
81
                 ]
 
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'])
 
96
        new_tree.set_root_id(self.base_tree.path2id(''))
 
97
        self.add_helper(self.base_tree, 'dir', new_tree, ['new'])
 
98
 
 
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
 
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)
 
118
        self.base_tree.lock_read()
 
119
        self.addCleanup(self.base_tree.unlock)
 
120
        self.assertRaises(errors.NoSuchId, self.base_tree.id2path, c_id)
 
121
 
 
122
        d_id = new_tree.path2id('subdir/d')
 
123
        self.assertNotEqual(None, d_id)
 
124
        self.assertRaises(errors.NoSuchId, self.base_tree.id2path, d_id)
 
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)
 
143
        self.base_tree.lock_read()
 
144
        self.addCleanup(self.base_tree.unlock)
 
145
        self.assertRaises(errors.NoSuchId, self.base_tree.id2path, a_id)
 
146
 
 
147
 
 
148
class TestAddActions(tests.TestCase):
 
149
 
 
150
    def test_quiet(self):
 
151
        self.run_action("")
 
152
 
 
153
    def test__print(self):
 
154
        self.run_action("adding path\n")
 
155
 
 
156
    def run_action(self, output):
 
157
        inv = inventory.Inventory()
 
158
        stdout = StringIO()
 
159
        action = add.AddAction(to_file=stdout, should_print=bool(output))
 
160
 
 
161
        self.apply_redirected(None, stdout, None, action, inv, None,
 
162
                              'path', 'file')
 
163
        self.assertEqual(stdout.getvalue(), output)