/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.65.4 by James Westby
Make the rename handling more robust.
1
# Copyright (C) 2008 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 time
18
19
from bzrlib import (
20
    branch,
21
    tests,
22
    )
23
24
from bzrlib.plugins.fastimport import (
25
    commands,
26
    errors,
27
    )
28
29
from bzrlib.plugins.fastimport.processors import (
30
    generic_processor,
31
    )
32
33
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
34
class TestCaseForGenericProcessor(tests.TestCaseWithTransport):
0.65.4 by James Westby
Make the rename handling more robust.
35
36
    def get_handler(self):
37
        branch = self.make_branch('.')
38
        handler = generic_processor.GenericProcessor(branch.bzrdir)
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
39
        return handler, branch
0.65.4 by James Westby
Make the rename handling more robust.
40
41
    # FIXME: [] as a default is bad, as it is mutable, but I want
42
    # to use None to mean "don't check this".
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
43
    def assertChanges(self, branch, revno, expected_added=[],
44
            expected_removed=[], expected_modified=[],
45
            expected_renamed=[]):
46
        """Check the changes introduced in a revision of a branch.
47
48
        This method checks that a revision introduces expected changes.
49
        The required changes are passed in as a list, where
50
        each entry contains the needed information about the change.
51
52
        If you do not wish to assert anything about a particular
53
        category then pass None instead.
54
55
        branch: The branch.
56
        revno: revision number of revision to check.
57
        expected_added: a list of (filename,) tuples that must have
58
            been added in the delta.
59
        expected_removed: a list of (filename,) tuples that must have
60
            been removed in the delta.
61
        expected_modified: a list of (filename,) tuples that must have
62
            been modified in the delta.
63
        expected_renamed: a list of (old_path, new_path) tuples that
64
            must have been renamed in the delta.
65
        :return: revtree1, revtree2
66
        """
67
        repo = branch.repository
68
        revtree1 = repo.revision_tree(branch.revision_history()[revno - 1])
69
        revtree2 = repo.revision_tree(branch.revision_history()[revno])
70
        changes = revtree2.changes_from(revtree1)
71
        self.check_changes(changes, expected_added, expected_removed,
72
            expected_modified, expected_renamed)
73
        return revtree1, revtree2
74
0.65.4 by James Westby
Make the rename handling more robust.
75
    def check_changes(self, changes, expected_added=[],
76
            expected_removed=[], expected_modified=[],
77
            expected_renamed=[]):
78
        """Check the changes in a TreeDelta
79
80
        This method checks that the TreeDelta contains the expected
81
        modifications between the two trees that were used to generate
82
        it. The required changes are passed in as a list, where
83
        each entry contains the needed information about the change.
84
85
        If you do not wish to assert anything about a particular
86
        category then pass None instead.
87
88
        changes: The TreeDelta to check.
89
        expected_added: a list of (filename,) tuples that must have
90
            been added in the delta.
91
        expected_removed: a list of (filename,) tuples that must have
92
            been removed in the delta.
93
        expected_modified: a list of (filename,) tuples that must have
94
            been modified in the delta.
95
        expected_renamed: a list of (old_path, new_path) tuples that
96
            must have been renamed in the delta.
97
        """
98
        renamed = changes.renamed
99
        added = changes.added
100
        removed = changes.removed
101
        modified = changes.modified
102
        if expected_renamed is not None:
103
            self.assertEquals(len(renamed), len(expected_renamed),
0.74.1 by John Arbash Meinel
Change the rename code to create a new text entry.
104
                "%s is renamed, expected %s" % (renamed, expected_renamed))
0.65.4 by James Westby
Make the rename handling more robust.
105
            renamed_files = [(item[0], item[1]) for item in renamed]
106
            for expected_renamed_entry in expected_renamed:
107
                self.assertTrue(expected_renamed_entry in renamed_files,
108
                    "%s is not renamed, %s are" % (str(expected_renamed_entry),
109
                        renamed_files))
110
        if expected_added is not None:
111
            self.assertEquals(len(added), len(expected_added),
112
                "%s is added" % str(added))
113
            added_files = [(item[0],) for item in added]
114
            for expected_added_entry in expected_added:
115
                self.assertTrue(expected_added_entry in added_files,
116
                    "%s is not added, %s are" % (str(expected_added_entry),
117
                        added_files))
118
        if expected_removed is not None:
119
            self.assertEquals(len(removed), len(expected_removed),
120
                "%s is removed" % str(removed))
121
            removed_files = [(item[0],) for item in removed]
122
            for expected_removed_entry in expected_removed:
123
                self.assertTrue(expected_removed_entry in removed_files,
124
                    "%s is not removed, %s are" % (str(expected_removed_entry),
125
                        removed_files))
126
        if expected_modified is not None:
127
            self.assertEquals(len(modified), len(expected_modified),
128
                "%s is modified" % str(modified))
129
            modified_files = [(item[0],) for item in modified]
130
            for expected_modified_entry in expected_modified:
131
                self.assertTrue(expected_modified_entry in modified_files,
132
                    "%s is not modified, %s are" % (str(expected_modified_entry),
133
                        modified_files))
134
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
135
136
class TestRename(TestCaseForGenericProcessor):
137
138
    def get_command_iter(self, old_path, new_path):
139
        def command_list():
140
            author = ['', 'bugs@a.com', time.time(), time.timezone]
141
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
142
            def files_one():
143
                yield commands.FileModifyCommand(old_path, 'file', False,
144
                        None, "aaa")
145
            yield commands.CommitCommand('head', '1', author,
146
                committer, "commit 1", None, [], files_one)
147
            def files_two():
148
                yield commands.FileRenameCommand(old_path, new_path)
149
            yield commands.CommitCommand('head', '2', author,
150
                committer, "commit 2", ":1", [], files_two)
151
        return command_list
152
0.65.4 by James Westby
Make the rename handling more robust.
153
    def test_rename_in_root(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
154
        handler, branch = self.get_handler()
0.65.4 by James Westby
Make the rename handling more robust.
155
        old_path = 'a'
156
        new_path = 'b'
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
157
        handler.process(self.get_command_iter(old_path, new_path))
158
        revtree1, revtree2 = self.assertChanges(branch, 1,
159
            expected_renamed=[(old_path, new_path)])
0.74.1 by John Arbash Meinel
Change the rename code to create a new text entry.
160
        self.assertEqual(revtree1.get_revision_id(),
161
                         revtree1.inventory.root.children['a'].revision)
162
        self.assertEqual(revtree2.get_revision_id(),
163
                         revtree2.inventory.root.children['b'].revision)
0.65.4 by James Westby
Make the rename handling more robust.
164
165
    def test_rename_in_subdir(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
166
        handler, branch = self.get_handler()
0.65.4 by James Westby
Make the rename handling more robust.
167
        old_path = 'a/a'
168
        new_path = 'a/b'
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
169
        handler.process(self.get_command_iter(old_path, new_path))
170
        self.assertChanges(branch, 1, expected_renamed=[(old_path, new_path)])
0.65.4 by James Westby
Make the rename handling more robust.
171
172
    def test_move_to_new_dir(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
173
        handler, branch = self.get_handler()
0.65.4 by James Westby
Make the rename handling more robust.
174
        old_path = 'a/a'
175
        new_path = 'b/a'
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
176
        handler.process(self.get_command_iter(old_path, new_path))
177
        self.assertChanges(branch, 1, expected_renamed=[(old_path, new_path)],
0.65.4 by James Westby
Make the rename handling more robust.
178
            expected_added=[('b',)])
0.64.74 by Ian Clatworthy
fix symlink importing
179
180
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
181
class TestFileKinds(TestCaseForGenericProcessor):
0.64.74 by Ian Clatworthy
fix symlink importing
182
183
    def get_command_iter(self, path, kind, content):
184
        def command_list():
185
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
186
            def files_one():
187
                yield commands.FileModifyCommand(path, kind, False,
188
                        None, content)
189
            yield commands.CommitCommand('head', '1', None,
190
                committer, "commit 1", None, [], files_one)
191
        return command_list
192
193
    def test_import_plainfile(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
194
        handler, branch = self.get_handler()
195
        handler.process(self.get_command_iter('foo', 'file', 'aaa'))
0.64.74 by Ian Clatworthy
fix symlink importing
196
197
    def test_import_symlink(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
198
        handler, branch = self.get_handler()
199
        handler.process(self.get_command_iter('foo', 'symlink', 'bar'))