/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
0.80.1 by Ian Clatworthy
basic units tests for filemodify
68
        revtree1 = repo.revision_tree(branch.get_rev_id(revno - 1))
69
        revtree2 = repo.revision_tree(branch.get_rev_id(revno))
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
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.80.1 by Ian Clatworthy
basic units tests for filemodify
135
    def assertContent(self, branch, tree, path, content):
136
        file_id = tree.inventory.path2id(path)
137
        branch.lock_read()
138
        self.addCleanup(branch.unlock)
139
        self.assertEqual(tree.get_file_text(file_id), content)
140
141
    def assertSymlinkTarget(self, branch, tree, path, target):
142
        file_id = tree.inventory.path2id(path)
143
        branch.lock_read()
144
        self.addCleanup(branch.unlock)
145
        self.assertEqual(tree.get_symlink_target(file_id), target)
146
147
    def assertRevisionRoot(self, revtree, path):
148
        self.assertEqual(revtree.get_revision_id(),
149
                         revtree.inventory.root.children[path].revision)
150
151
152
class TestModify(TestCaseForGenericProcessor):
153
154
    def file_command_iter(self, path, kind='file'):
155
        def command_list():
156
            author = ['', 'bugs@a.com', time.time(), time.timezone]
157
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
158
            def files_one():
159
                yield commands.FileModifyCommand(path, kind, False,
160
                        None, "aaa")
161
            yield commands.CommitCommand('head', '1', author,
162
                committer, "commit 1", None, [], files_one)
163
            def files_two():
164
                yield commands.FileModifyCommand(path, kind, False,
165
                        None, "bbb")
166
            yield commands.CommitCommand('head', '2', author,
167
                committer, "commit 2", ":1", [], files_two)
168
        return command_list
169
170
    def test_modify_file_in_root(self):
171
        handler, branch = self.get_handler()
172
        path = 'a'
173
        handler.process(self.file_command_iter(path))
174
        revtree0, revtree1 = self.assertChanges(branch, 1,
175
            expected_added=[(path,)])
176
        revtree1, revtree2 = self.assertChanges(branch, 2,
177
            expected_modified=[(path,)])
178
        self.assertContent(branch, revtree1, path, "aaa")
179
        self.assertContent(branch, revtree2, path, "bbb")
180
        self.assertRevisionRoot(revtree1, path)
181
        self.assertRevisionRoot(revtree2, path)
182
183
    def test_modify_file_in_subdir(self):
184
        handler, branch = self.get_handler()
185
        path = 'a/a'
186
        handler.process(self.file_command_iter(path))
187
        revtree0, revtree1 = self.assertChanges(branch, 1,
188
            expected_added=[('a',), (path,)])
189
        revtree1, revtree2 = self.assertChanges(branch, 2,
190
            expected_modified=[(path,)])
191
        self.assertContent(branch, revtree1, path, "aaa")
192
        self.assertContent(branch, revtree2, path, "bbb")
193
194
    def test_modify_symlink_in_root(self):
195
        handler, branch = self.get_handler()
196
        path = 'a'
197
        handler.process(self.file_command_iter(path, kind='symlink'))
198
        revtree1, revtree2 = self.assertChanges(branch, 2,
199
            expected_modified=[(path,)])
200
        self.assertSymlinkTarget(branch, revtree1, path, "aaa")
201
        self.assertSymlinkTarget(branch, revtree2, path, "bbb")
202
        self.assertRevisionRoot(revtree1, path)
203
        self.assertRevisionRoot(revtree2, path)
204
205
    def test_modify_symlink_in_subdir(self):
206
        handler, branch = self.get_handler()
207
        path = 'a/a'
208
        handler.process(self.file_command_iter(path, kind='symlink'))
209
        revtree0, revtree1 = self.assertChanges(branch, 1,
210
            expected_added=[('a',), (path,)])
211
        revtree1, revtree2 = self.assertChanges(branch, 2,
212
            expected_modified=[(path,)])
213
        self.assertSymlinkTarget(branch, revtree1, path, "aaa")
214
        self.assertSymlinkTarget(branch, revtree2, path, "bbb")
215
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
216
217
class TestRename(TestCaseForGenericProcessor):
218
219
    def get_command_iter(self, old_path, new_path):
220
        def command_list():
221
            author = ['', 'bugs@a.com', time.time(), time.timezone]
222
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
223
            def files_one():
224
                yield commands.FileModifyCommand(old_path, 'file', False,
225
                        None, "aaa")
226
            yield commands.CommitCommand('head', '1', author,
227
                committer, "commit 1", None, [], files_one)
228
            def files_two():
229
                yield commands.FileRenameCommand(old_path, new_path)
230
            yield commands.CommitCommand('head', '2', author,
231
                committer, "commit 2", ":1", [], files_two)
232
        return command_list
233
0.65.4 by James Westby
Make the rename handling more robust.
234
    def test_rename_in_root(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
235
        handler, branch = self.get_handler()
0.65.4 by James Westby
Make the rename handling more robust.
236
        old_path = 'a'
237
        new_path = 'b'
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
238
        handler.process(self.get_command_iter(old_path, new_path))
0.80.1 by Ian Clatworthy
basic units tests for filemodify
239
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
240
            expected_renamed=[(old_path, new_path)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
241
        self.assertRevisionRoot(revtree1, old_path)
242
        self.assertRevisionRoot(revtree2, new_path)
0.65.4 by James Westby
Make the rename handling more robust.
243
244
    def test_rename_in_subdir(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
245
        handler, branch = self.get_handler()
0.65.4 by James Westby
Make the rename handling more robust.
246
        old_path = 'a/a'
247
        new_path = 'a/b'
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
248
        handler.process(self.get_command_iter(old_path, new_path))
0.80.1 by Ian Clatworthy
basic units tests for filemodify
249
        self.assertChanges(branch, 2, expected_renamed=[(old_path, new_path)])
0.65.4 by James Westby
Make the rename handling more robust.
250
251
    def test_move_to_new_dir(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
252
        handler, branch = self.get_handler()
0.65.4 by James Westby
Make the rename handling more robust.
253
        old_path = 'a/a'
254
        new_path = 'b/a'
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
255
        handler.process(self.get_command_iter(old_path, new_path))
0.80.1 by Ian Clatworthy
basic units tests for filemodify
256
        self.assertChanges(branch, 2, expected_renamed=[(old_path, new_path)],
0.65.4 by James Westby
Make the rename handling more robust.
257
            expected_added=[('b',)])
0.64.74 by Ian Clatworthy
fix symlink importing
258
259
0.76.2 by Ian Clatworthy
code & tests for file copying
260
class TestCopy(TestCaseForGenericProcessor):
261
0.80.1 by Ian Clatworthy
basic units tests for filemodify
262
    def file_command_iter(self, src_path, dest_path, kind='file'):
0.76.2 by Ian Clatworthy
code & tests for file copying
263
        def command_list():
264
            author = ['', 'bugs@a.com', time.time(), time.timezone]
265
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
266
            def files_one():
0.80.1 by Ian Clatworthy
basic units tests for filemodify
267
                yield commands.FileModifyCommand(src_path, kind, False,
0.76.2 by Ian Clatworthy
code & tests for file copying
268
                        None, "aaa")
269
            yield commands.CommitCommand('head', '1', author,
270
                committer, "commit 1", None, [], files_one)
271
            def files_two():
272
                yield commands.FileCopyCommand(src_path, dest_path)
273
            yield commands.CommitCommand('head', '2', author,
274
                committer, "commit 2", ":1", [], files_two)
275
        return command_list
276
277
    def test_copy_file_in_root(self):
278
        handler, branch = self.get_handler()
279
        src_path = 'a'
280
        dest_path = 'b'
281
        handler.process(self.file_command_iter(src_path, dest_path))
0.80.1 by Ian Clatworthy
basic units tests for filemodify
282
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.2 by Ian Clatworthy
code & tests for file copying
283
            expected_added=[(dest_path,)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
284
        self.assertContent(branch, revtree1, src_path, "aaa")
285
        self.assertContent(branch, revtree2, src_path, "aaa")
0.76.2 by Ian Clatworthy
code & tests for file copying
286
        self.assertContent(branch, revtree2, dest_path, "aaa")
0.80.1 by Ian Clatworthy
basic units tests for filemodify
287
        self.assertRevisionRoot(revtree1, src_path)
288
        self.assertRevisionRoot(revtree2, dest_path)
0.76.2 by Ian Clatworthy
code & tests for file copying
289
290
    def test_copy_file_in_subdir(self):
291
        handler, branch = self.get_handler()
292
        src_path = 'a/a'
293
        dest_path = 'a/b'
294
        handler.process(self.file_command_iter(src_path, dest_path))
0.80.1 by Ian Clatworthy
basic units tests for filemodify
295
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.2 by Ian Clatworthy
code & tests for file copying
296
            expected_added=[(dest_path,)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
297
        self.assertContent(branch, revtree1, src_path, "aaa")
298
        self.assertContent(branch, revtree2, src_path, "aaa")
0.76.2 by Ian Clatworthy
code & tests for file copying
299
        self.assertContent(branch, revtree2, dest_path, "aaa")
300
301
    def test_copy_file_to_new_dir(self):
302
        handler, branch = self.get_handler()
303
        src_path = 'a/a'
304
        dest_path = 'b/a'
305
        handler.process(self.file_command_iter(src_path, dest_path))
0.80.1 by Ian Clatworthy
basic units tests for filemodify
306
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.2 by Ian Clatworthy
code & tests for file copying
307
            expected_added=[('b',), (dest_path,)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
308
        self.assertContent(branch, revtree1, src_path, "aaa")
309
        self.assertContent(branch, revtree2, src_path, "aaa")
0.76.2 by Ian Clatworthy
code & tests for file copying
310
        self.assertContent(branch, revtree2, dest_path, "aaa")
311
0.76.3 by Ian Clatworthy
symlink copying tests
312
    def test_copy_symlink_in_root(self):
313
        handler, branch = self.get_handler()
314
        src_path = 'a'
315
        dest_path = 'b'
0.80.1 by Ian Clatworthy
basic units tests for filemodify
316
        handler.process(self.file_command_iter(src_path, dest_path, 'symlink'))
317
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.3 by Ian Clatworthy
symlink copying tests
318
            expected_added=[(dest_path,)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
319
        self.assertSymlinkTarget(branch, revtree1, src_path, "aaa")
320
        self.assertSymlinkTarget(branch, revtree2, src_path, "aaa")
0.76.3 by Ian Clatworthy
symlink copying tests
321
        self.assertSymlinkTarget(branch, revtree2, dest_path, "aaa")
0.80.1 by Ian Clatworthy
basic units tests for filemodify
322
        self.assertRevisionRoot(revtree1, src_path)
323
        self.assertRevisionRoot(revtree2, dest_path)
0.76.3 by Ian Clatworthy
symlink copying tests
324
325
    def test_copy_symlink_in_subdir(self):
326
        handler, branch = self.get_handler()
327
        src_path = 'a/a'
328
        dest_path = 'a/b'
0.80.1 by Ian Clatworthy
basic units tests for filemodify
329
        handler.process(self.file_command_iter(src_path, dest_path, 'symlink'))
330
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.3 by Ian Clatworthy
symlink copying tests
331
            expected_added=[(dest_path,)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
332
        self.assertSymlinkTarget(branch, revtree1, src_path, "aaa")
333
        self.assertSymlinkTarget(branch, revtree2, src_path, "aaa")
0.76.3 by Ian Clatworthy
symlink copying tests
334
        self.assertSymlinkTarget(branch, revtree2, dest_path, "aaa")
335
336
    def test_copy_symlink_to_new_dir(self):
337
        handler, branch = self.get_handler()
338
        src_path = 'a/a'
339
        dest_path = 'b/a'
0.80.1 by Ian Clatworthy
basic units tests for filemodify
340
        handler.process(self.file_command_iter(src_path, dest_path, 'symlink'))
341
        revtree1, revtree2 = self.assertChanges(branch, 2,
0.76.3 by Ian Clatworthy
symlink copying tests
342
            expected_added=[('b',), (dest_path,)])
0.80.1 by Ian Clatworthy
basic units tests for filemodify
343
        self.assertSymlinkTarget(branch, revtree1, src_path, "aaa")
344
        self.assertSymlinkTarget(branch, revtree2, src_path, "aaa")
0.76.3 by Ian Clatworthy
symlink copying tests
345
        self.assertSymlinkTarget(branch, revtree2, dest_path, "aaa")
346
0.76.2 by Ian Clatworthy
code & tests for file copying
347
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
348
class TestFileKinds(TestCaseForGenericProcessor):
0.64.74 by Ian Clatworthy
fix symlink importing
349
350
    def get_command_iter(self, path, kind, content):
351
        def command_list():
352
            committer = ['', 'elmer@a.com', time.time(), time.timezone]
353
            def files_one():
354
                yield commands.FileModifyCommand(path, kind, False,
355
                        None, content)
356
            yield commands.CommitCommand('head', '1', None,
357
                committer, "commit 1", None, [], files_one)
358
        return command_list
359
360
    def test_import_plainfile(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
361
        handler, branch = self.get_handler()
362
        handler.process(self.get_command_iter('foo', 'file', 'aaa'))
0.64.74 by Ian Clatworthy
fix symlink importing
363
364
    def test_import_symlink(self):
0.76.1 by Ian Clatworthy
clean-up tests for GenericProcessor
365
        handler, branch = self.get_handler()
366
        handler.process(self.get_command_iter('foo', 'symlink', 'bar'))