1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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
24
from bzrlib.plugins.fastimport import (
29
from bzrlib.plugins.fastimport.processors import (
34
class TestRename(tests.TestCaseWithTransport):
36
def get_handler(self):
37
branch = self.make_branch('.')
38
handler = generic_processor.GenericProcessor(branch.bzrdir)
39
return (handler, branch)
41
def get_command_iter(self, old_path, new_path):
43
author = ['', 'bugs@a.com', time.time(), time.timezone]
44
committer = ['', 'elmer@a.com', time.time(), time.timezone]
46
yield commands.FileModifyCommand(old_path, 'file', False,
48
yield commands.CommitCommand('head', '1', author,
49
committer, "commit 1", [], files_one)
51
yield commands.FileRenameCommand(old_path, new_path)
52
yield commands.CommitCommand('head', '2', author,
53
committer, "commit 2", [":1"], files_two)
56
# FIXME: [] as a default is bad, as it is mutable, but I want
57
# to use None to mean "don't check this".
58
def check_changes(self, changes, expected_added=[],
59
expected_removed=[], expected_modified=[],
61
"""Check the changes in a TreeDelta
63
This method checks that the TreeDelta contains the expected
64
modifications between the two trees that were used to generate
65
it. The required changes are passed in as a list, where
66
each entry contains the needed information about the change.
68
If you do not wish to assert anything about a particular
69
category then pass None instead.
71
changes: The TreeDelta to check.
72
expected_added: a list of (filename,) tuples that must have
73
been added in the delta.
74
expected_removed: a list of (filename,) tuples that must have
75
been removed in the delta.
76
expected_modified: a list of (filename,) tuples that must have
77
been modified in the delta.
78
expected_renamed: a list of (old_path, new_path) tuples that
79
must have been renamed in the delta.
81
renamed = changes.renamed
83
removed = changes.removed
84
modified = changes.modified
85
if expected_renamed is not None:
86
self.assertEquals(len(renamed), len(expected_renamed),
87
"%s is renamed" % str(renamed))
88
renamed_files = [(item[0], item[1]) for item in renamed]
89
for expected_renamed_entry in expected_renamed:
90
self.assertTrue(expected_renamed_entry in renamed_files,
91
"%s is not renamed, %s are" % (str(expected_renamed_entry),
93
if expected_added is not None:
94
self.assertEquals(len(added), len(expected_added),
95
"%s is added" % str(added))
96
added_files = [(item[0],) for item in added]
97
for expected_added_entry in expected_added:
98
self.assertTrue(expected_added_entry in added_files,
99
"%s is not added, %s are" % (str(expected_added_entry),
101
if expected_removed is not None:
102
self.assertEquals(len(removed), len(expected_removed),
103
"%s is removed" % str(removed))
104
removed_files = [(item[0],) for item in removed]
105
for expected_removed_entry in expected_removed:
106
self.assertTrue(expected_removed_entry in removed_files,
107
"%s is not removed, %s are" % (str(expected_removed_entry),
109
if expected_modified is not None:
110
self.assertEquals(len(modified), len(expected_modified),
111
"%s is modified" % str(modified))
112
modified_files = [(item[0],) for item in modified]
113
for expected_modified_entry in expected_modified:
114
self.assertTrue(expected_modified_entry in modified_files,
115
"%s is not modified, %s are" % (str(expected_modified_entry),
118
def test_rename_in_root(self):
119
(handler, branch) = self.get_handler()
122
command_list = self.get_command_iter(old_path, new_path)
123
handler.process(command_list)
124
repo = branch.repository
125
revtree1 = repo.revision_tree(branch.revision_history()[0])
126
revtree2 = repo.revision_tree(branch.revision_history()[1])
127
changes = revtree2.changes_from(revtree1)
128
self.check_changes(changes, expected_renamed=[(old_path, new_path)])
130
def test_rename_in_subdir(self):
131
(handler, branch) = self.get_handler()
134
command_list = self.get_command_iter(old_path, new_path)
135
handler.process(command_list)
136
repo = branch.repository
137
revtree1 = repo.revision_tree(branch.revision_history()[0])
138
revtree2 = repo.revision_tree(branch.revision_history()[1])
139
changes = revtree2.changes_from(revtree1)
140
self.check_changes(changes, expected_renamed=[(old_path, new_path)])
142
def test_move_to_new_dir(self):
143
(handler, branch) = self.get_handler()
146
command_list = self.get_command_iter(old_path, new_path)
147
handler.process(command_list)
148
repo = branch.repository
149
revtree1 = repo.revision_tree(branch.revision_history()[0])
150
revtree2 = repo.revision_tree(branch.revision_history()[1])
151
changes = revtree2.changes_from(revtree1)
152
self.check_changes(changes, expected_renamed=[(old_path, new_path)],
153
expected_added=[('b',)])