1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# Copyright (C) 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import time
from bzrlib import (
branch,
tests,
)
from bzrlib.plugins.fastimport import (
commands,
errors,
)
from bzrlib.plugins.fastimport.processors import (
generic_processor,
)
class TestCaseForGenericProcessor(tests.TestCaseWithTransport):
def get_handler(self):
branch = self.make_branch('.')
handler = generic_processor.GenericProcessor(branch.bzrdir)
return handler, branch
# FIXME: [] as a default is bad, as it is mutable, but I want
# to use None to mean "don't check this".
def assertChanges(self, branch, revno, expected_added=[],
expected_removed=[], expected_modified=[],
expected_renamed=[]):
"""Check the changes introduced in a revision of a branch.
This method checks that a revision introduces expected changes.
The required changes are passed in as a list, where
each entry contains the needed information about the change.
If you do not wish to assert anything about a particular
category then pass None instead.
branch: The branch.
revno: revision number of revision to check.
expected_added: a list of (filename,) tuples that must have
been added in the delta.
expected_removed: a list of (filename,) tuples that must have
been removed in the delta.
expected_modified: a list of (filename,) tuples that must have
been modified in the delta.
expected_renamed: a list of (old_path, new_path) tuples that
must have been renamed in the delta.
:return: revtree1, revtree2
"""
repo = branch.repository
revtree1 = repo.revision_tree(branch.get_rev_id(revno - 1))
revtree2 = repo.revision_tree(branch.get_rev_id(revno))
changes = revtree2.changes_from(revtree1)
self.check_changes(changes, expected_added, expected_removed,
expected_modified, expected_renamed)
return revtree1, revtree2
def check_changes(self, changes, expected_added=[],
expected_removed=[], expected_modified=[],
expected_renamed=[]):
"""Check the changes in a TreeDelta
This method checks that the TreeDelta contains the expected
modifications between the two trees that were used to generate
it. The required changes are passed in as a list, where
each entry contains the needed information about the change.
If you do not wish to assert anything about a particular
category then pass None instead.
changes: The TreeDelta to check.
expected_added: a list of (filename,) tuples that must have
been added in the delta.
expected_removed: a list of (filename,) tuples that must have
been removed in the delta.
expected_modified: a list of (filename,) tuples that must have
been modified in the delta.
expected_renamed: a list of (old_path, new_path) tuples that
must have been renamed in the delta.
"""
renamed = changes.renamed
added = changes.added
removed = changes.removed
modified = changes.modified
if expected_renamed is not None:
self.assertEquals(len(renamed), len(expected_renamed),
"%s is renamed, expected %s" % (renamed, expected_renamed))
renamed_files = [(item[0], item[1]) for item in renamed]
for expected_renamed_entry in expected_renamed:
self.assertTrue(expected_renamed_entry in renamed_files,
"%s is not renamed, %s are" % (str(expected_renamed_entry),
renamed_files))
if expected_added is not None:
self.assertEquals(len(added), len(expected_added),
"%s is added" % str(added))
added_files = [(item[0],) for item in added]
for expected_added_entry in expected_added:
self.assertTrue(expected_added_entry in added_files,
"%s is not added, %s are" % (str(expected_added_entry),
added_files))
if expected_removed is not None:
self.assertEquals(len(removed), len(expected_removed),
"%s is removed" % str(removed))
removed_files = [(item[0],) for item in removed]
for expected_removed_entry in expected_removed:
self.assertTrue(expected_removed_entry in removed_files,
"%s is not removed, %s are" % (str(expected_removed_entry),
removed_files))
if expected_modified is not None:
self.assertEquals(len(modified), len(expected_modified),
"%s is modified" % str(modified))
modified_files = [(item[0],) for item in modified]
for expected_modified_entry in expected_modified:
self.assertTrue(expected_modified_entry in modified_files,
"%s is not modified, %s are" % (str(expected_modified_entry),
modified_files))
def assertContent(self, branch, tree, path, content):
file_id = tree.inventory.path2id(path)
branch.lock_read()
self.addCleanup(branch.unlock)
self.assertEqual(tree.get_file_text(file_id), content)
def assertSymlinkTarget(self, branch, tree, path, target):
file_id = tree.inventory.path2id(path)
branch.lock_read()
self.addCleanup(branch.unlock)
self.assertEqual(tree.get_symlink_target(file_id), target)
def assertRevisionRoot(self, revtree, path):
self.assertEqual(revtree.get_revision_id(),
revtree.inventory.root.children[path].revision)
class TestModify(TestCaseForGenericProcessor):
def file_command_iter(self, path, kind='file'):
def command_list():
author = ['', 'bugs@a.com', time.time(), time.timezone]
committer = ['', 'elmer@a.com', time.time(), time.timezone]
def files_one():
yield commands.FileModifyCommand(path, kind, False,
None, "aaa")
yield commands.CommitCommand('head', '1', author,
committer, "commit 1", None, [], files_one)
def files_two():
yield commands.FileModifyCommand(path, kind, False,
None, "bbb")
yield commands.CommitCommand('head', '2', author,
committer, "commit 2", ":1", [], files_two)
return command_list
def test_modify_file_in_root(self):
handler, branch = self.get_handler()
path = 'a'
handler.process(self.file_command_iter(path))
revtree0, revtree1 = self.assertChanges(branch, 1,
expected_added=[(path,)])
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_modified=[(path,)])
self.assertContent(branch, revtree1, path, "aaa")
self.assertContent(branch, revtree2, path, "bbb")
self.assertRevisionRoot(revtree1, path)
self.assertRevisionRoot(revtree2, path)
def test_modify_file_in_subdir(self):
handler, branch = self.get_handler()
path = 'a/a'
handler.process(self.file_command_iter(path))
revtree0, revtree1 = self.assertChanges(branch, 1,
expected_added=[('a',), (path,)])
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_modified=[(path,)])
self.assertContent(branch, revtree1, path, "aaa")
self.assertContent(branch, revtree2, path, "bbb")
def test_modify_symlink_in_root(self):
handler, branch = self.get_handler()
path = 'a'
handler.process(self.file_command_iter(path, kind='symlink'))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_modified=[(path,)])
self.assertSymlinkTarget(branch, revtree1, path, "aaa")
self.assertSymlinkTarget(branch, revtree2, path, "bbb")
self.assertRevisionRoot(revtree1, path)
self.assertRevisionRoot(revtree2, path)
def test_modify_symlink_in_subdir(self):
handler, branch = self.get_handler()
path = 'a/a'
handler.process(self.file_command_iter(path, kind='symlink'))
revtree0, revtree1 = self.assertChanges(branch, 1,
expected_added=[('a',), (path,)])
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_modified=[(path,)])
self.assertSymlinkTarget(branch, revtree1, path, "aaa")
self.assertSymlinkTarget(branch, revtree2, path, "bbb")
class TestDelete(TestCaseForGenericProcessor):
def file_command_iter(self, path, kind='file'):
def command_list():
author = ['', 'bugs@a.com', time.time(), time.timezone]
committer = ['', 'elmer@a.com', time.time(), time.timezone]
def files_one():
yield commands.FileModifyCommand(path, kind, False,
None, "aaa")
yield commands.CommitCommand('head', '1', author,
committer, "commit 1", None, [], files_one)
def files_two():
yield commands.FileDeleteCommand(path)
yield commands.CommitCommand('head', '2', author,
committer, "commit 2", ":1", [], files_two)
return command_list
def test_delete_file_in_root(self):
handler, branch = self.get_handler()
path = 'a'
handler.process(self.file_command_iter(path))
revtree0, revtree1 = self.assertChanges(branch, 1,
expected_added=[(path,)])
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_removed=[(path,)])
self.assertContent(branch, revtree1, path, "aaa")
self.assertRevisionRoot(revtree1, path)
def test_delete_file_in_subdir(self):
handler, branch = self.get_handler()
path = 'a/a'
handler.process(self.file_command_iter(path))
revtree0, revtree1 = self.assertChanges(branch, 1,
expected_added=[('a',), (path,)])
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_removed=[(path,)])
self.assertContent(branch, revtree1, path, "aaa")
def test_delete_symlink_in_root(self):
handler, branch = self.get_handler()
path = 'a'
handler.process(self.file_command_iter(path, kind='symlink'))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_removed=[(path,)])
self.assertSymlinkTarget(branch, revtree1, path, "aaa")
self.assertRevisionRoot(revtree1, path)
def test_delete_symlink_in_subdir(self):
handler, branch = self.get_handler()
path = 'a/a'
handler.process(self.file_command_iter(path, kind='symlink'))
revtree0, revtree1 = self.assertChanges(branch, 1,
expected_added=[('a',), (path,)])
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_removed=[(path,)])
self.assertSymlinkTarget(branch, revtree1, path, "aaa")
class TestRename(TestCaseForGenericProcessor):
def get_command_iter(self, old_path, new_path):
def command_list():
author = ['', 'bugs@a.com', time.time(), time.timezone]
committer = ['', 'elmer@a.com', time.time(), time.timezone]
def files_one():
yield commands.FileModifyCommand(old_path, 'file', False,
None, "aaa")
yield commands.CommitCommand('head', '1', author,
committer, "commit 1", None, [], files_one)
def files_two():
yield commands.FileRenameCommand(old_path, new_path)
yield commands.CommitCommand('head', '2', author,
committer, "commit 2", ":1", [], files_two)
return command_list
def test_rename_in_root(self):
handler, branch = self.get_handler()
old_path = 'a'
new_path = 'b'
handler.process(self.get_command_iter(old_path, new_path))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_renamed=[(old_path, new_path)])
self.assertRevisionRoot(revtree1, old_path)
self.assertRevisionRoot(revtree2, new_path)
def test_rename_in_subdir(self):
handler, branch = self.get_handler()
old_path = 'a/a'
new_path = 'a/b'
handler.process(self.get_command_iter(old_path, new_path))
self.assertChanges(branch, 2, expected_renamed=[(old_path, new_path)])
def test_move_to_new_dir(self):
handler, branch = self.get_handler()
old_path = 'a/a'
new_path = 'b/a'
handler.process(self.get_command_iter(old_path, new_path))
self.assertChanges(branch, 2, expected_renamed=[(old_path, new_path)],
expected_added=[('b',)])
class TestCopy(TestCaseForGenericProcessor):
def file_command_iter(self, src_path, dest_path, kind='file'):
def command_list():
author = ['', 'bugs@a.com', time.time(), time.timezone]
committer = ['', 'elmer@a.com', time.time(), time.timezone]
def files_one():
yield commands.FileModifyCommand(src_path, kind, False,
None, "aaa")
yield commands.CommitCommand('head', '1', author,
committer, "commit 1", None, [], files_one)
def files_two():
yield commands.FileCopyCommand(src_path, dest_path)
yield commands.CommitCommand('head', '2', author,
committer, "commit 2", ":1", [], files_two)
return command_list
def test_copy_file_in_root(self):
handler, branch = self.get_handler()
src_path = 'a'
dest_path = 'b'
handler.process(self.file_command_iter(src_path, dest_path))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_added=[(dest_path,)])
self.assertContent(branch, revtree1, src_path, "aaa")
self.assertContent(branch, revtree2, src_path, "aaa")
self.assertContent(branch, revtree2, dest_path, "aaa")
self.assertRevisionRoot(revtree1, src_path)
self.assertRevisionRoot(revtree2, dest_path)
def test_copy_file_in_subdir(self):
handler, branch = self.get_handler()
src_path = 'a/a'
dest_path = 'a/b'
handler.process(self.file_command_iter(src_path, dest_path))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_added=[(dest_path,)])
self.assertContent(branch, revtree1, src_path, "aaa")
self.assertContent(branch, revtree2, src_path, "aaa")
self.assertContent(branch, revtree2, dest_path, "aaa")
def test_copy_file_to_new_dir(self):
handler, branch = self.get_handler()
src_path = 'a/a'
dest_path = 'b/a'
handler.process(self.file_command_iter(src_path, dest_path))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_added=[('b',), (dest_path,)])
self.assertContent(branch, revtree1, src_path, "aaa")
self.assertContent(branch, revtree2, src_path, "aaa")
self.assertContent(branch, revtree2, dest_path, "aaa")
def test_copy_symlink_in_root(self):
handler, branch = self.get_handler()
src_path = 'a'
dest_path = 'b'
handler.process(self.file_command_iter(src_path, dest_path, 'symlink'))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_added=[(dest_path,)])
self.assertSymlinkTarget(branch, revtree1, src_path, "aaa")
self.assertSymlinkTarget(branch, revtree2, src_path, "aaa")
self.assertSymlinkTarget(branch, revtree2, dest_path, "aaa")
self.assertRevisionRoot(revtree1, src_path)
self.assertRevisionRoot(revtree2, dest_path)
def test_copy_symlink_in_subdir(self):
handler, branch = self.get_handler()
src_path = 'a/a'
dest_path = 'a/b'
handler.process(self.file_command_iter(src_path, dest_path, 'symlink'))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_added=[(dest_path,)])
self.assertSymlinkTarget(branch, revtree1, src_path, "aaa")
self.assertSymlinkTarget(branch, revtree2, src_path, "aaa")
self.assertSymlinkTarget(branch, revtree2, dest_path, "aaa")
def test_copy_symlink_to_new_dir(self):
handler, branch = self.get_handler()
src_path = 'a/a'
dest_path = 'b/a'
handler.process(self.file_command_iter(src_path, dest_path, 'symlink'))
revtree1, revtree2 = self.assertChanges(branch, 2,
expected_added=[('b',), (dest_path,)])
self.assertSymlinkTarget(branch, revtree1, src_path, "aaa")
self.assertSymlinkTarget(branch, revtree2, src_path, "aaa")
self.assertSymlinkTarget(branch, revtree2, dest_path, "aaa")
class TestFileKinds(TestCaseForGenericProcessor):
def get_command_iter(self, path, kind, content):
def command_list():
committer = ['', 'elmer@a.com', time.time(), time.timezone]
def files_one():
yield commands.FileModifyCommand(path, kind, False,
None, content)
yield commands.CommitCommand('head', '1', None,
committer, "commit 1", None, [], files_one)
return command_list
def test_import_plainfile(self):
handler, branch = self.get_handler()
handler.process(self.get_command_iter('foo', 'file', 'aaa'))
def test_import_symlink(self):
handler, branch = self.get_handler()
handler.process(self.get_command_iter('foo', 'symlink', 'bar'))
|