1
# Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from breezy import trace
21
from breezy.rename_map import RenameMap
22
from breezy.tests import TestCaseWithTransport
26
"""This the hash used by RenameMap."""
27
return hash(val) % (1024 * 1024 * 10)
30
class TestRenameMap(TestCaseWithTransport):
32
a_lines = b'a\nb\nc\n'.splitlines(True)
33
b_lines = b'b\nc\nd\n'.splitlines(True)
35
def test_add_edge_hashes(self):
37
rn.add_edge_hashes(self.a_lines, 'a')
38
self.assertEqual({'a'}, rn.edge_hashes[myhash(('a\n', 'b\n'))])
39
self.assertEqual({'a'}, rn.edge_hashes[myhash(('b\n', 'c\n'))])
40
self.assertIs(None, rn.edge_hashes.get(myhash(('c\n', 'd\n'))))
42
def test_add_file_edge_hashes(self):
43
tree = self.make_branch_and_tree('tree')
44
self.build_tree_contents([('tree/a', b''.join(self.a_lines))])
47
rn.add_file_edge_hashes(tree, [b'a'])
48
self.assertEqual({b'a'}, rn.edge_hashes[myhash(('a\n', 'b\n'))])
49
self.assertEqual({b'a'}, rn.edge_hashes[myhash(('b\n', 'c\n'))])
50
self.assertIs(None, rn.edge_hashes.get(myhash(('c\n', 'd\n'))))
52
def test_hitcounts(self):
54
rn.add_edge_hashes(self.a_lines, 'a')
55
rn.add_edge_hashes(self.b_lines, 'b')
56
self.assertEqual({'a': 2.5, 'b': 0.5}, rn.hitcounts(self.a_lines))
57
self.assertEqual({'a': 1}, rn.hitcounts(self.a_lines[:-1]))
58
self.assertEqual({'b': 2.5, 'a': 0.5}, rn.hitcounts(self.b_lines))
60
def test_file_match(self):
61
tree = self.make_branch_and_tree('tree')
63
rn.add_edge_hashes(self.a_lines, 'aid')
64
rn.add_edge_hashes(self.b_lines, 'bid')
65
self.build_tree_contents([('tree/a', b''.join(self.a_lines))])
66
self.build_tree_contents([('tree/b', b''.join(self.b_lines))])
67
self.assertEqual({'a': 'aid', 'b': 'bid'},
68
rn.file_match(['a', 'b']))
70
def test_file_match_no_dups(self):
71
tree = self.make_branch_and_tree('tree')
73
rn.add_edge_hashes(self.a_lines, 'aid')
74
self.build_tree_contents([('tree/a', b''.join(self.a_lines))])
75
self.build_tree_contents([('tree/b', b''.join(self.b_lines))])
76
self.build_tree_contents([('tree/c', b''.join(self.b_lines))])
77
self.assertEqual({'a': 'aid'},
78
rn.file_match(['a', 'b', 'c']))
80
def test_match_directories(self):
81
tree = self.make_branch_and_tree('tree')
83
required_parents = rn.get_required_parents({
86
'path3/path4/path5': 'c',
89
{'path2': {'b'}, 'path3/path4': {'c'}, 'path3': set()},
92
def test_find_directory_renames(self):
93
tree = self.make_branch_and_tree('tree')
97
'path3/path4/path5': 'c',
101
'path3/path4': {'c'},
106
'path3-id': {'path4-id'}}
107
matches = rn.match_parents(required_parents, missing_parents)
108
self.assertEqual({'path3/path4': 'path4-id', 'path2': 'path2-id'},
111
def test_guess_renames(self):
112
tree = self.make_branch_and_tree('tree')
114
self.addCleanup(tree.unlock)
115
self.build_tree(['tree/file'])
116
tree.add('file', b'file-id')
117
tree.commit('Added file')
118
os.rename('tree/file', 'tree/file2')
119
RenameMap.guess_renames(tree.basis_tree(), tree)
120
self.assertEqual('file2', tree.id2path(b'file-id'))
122
def test_guess_renames_handles_directories(self):
123
tree = self.make_branch_and_tree('tree')
125
self.addCleanup(tree.unlock)
126
self.build_tree(['tree/dir/', 'tree/dir/file'])
127
tree.add(['dir', 'dir/file'], [b'dir-id', b'file-id'])
128
tree.commit('Added file')
129
os.rename('tree/dir', 'tree/dir2')
130
RenameMap.guess_renames(tree.basis_tree(), tree)
131
self.assertEqual('dir2/file', tree.id2path(b'file-id'))
132
self.assertEqual('dir2', tree.id2path(b'dir-id'))
134
def test_guess_renames_handles_grandparent_directories(self):
135
tree = self.make_branch_and_tree('tree')
137
self.addCleanup(tree.unlock)
138
self.build_tree(['tree/topdir/',
139
'tree/topdir/middledir/',
140
'tree/topdir/middledir/file'])
141
tree.add(['topdir', 'topdir/middledir', 'topdir/middledir/file'],
142
[b'topdir-id', b'middledir-id', b'file-id'])
143
tree.commit('Added files.')
144
os.rename('tree/topdir', 'tree/topdir2')
145
RenameMap.guess_renames(tree.basis_tree(), tree)
146
self.assertEqual('topdir2', tree.id2path(b'topdir-id'))
148
def test_guess_renames_preserves_children(self):
149
"""When a directory has been moved, its children are preserved."""
150
tree = self.make_branch_and_tree('tree')
152
self.addCleanup(tree.unlock)
153
self.build_tree_contents([('tree/foo/', b''),
154
('tree/foo/bar', b'bar'),
155
('tree/foo/empty', b'')])
156
tree.add(['foo', 'foo/bar', 'foo/empty'],
157
[b'foo-id', b'bar-id', b'empty-id'])
159
os.rename('tree/foo', 'tree/baz')
160
RenameMap.guess_renames(tree.basis_tree(), tree)
161
self.assertEqual('baz/empty', tree.id2path(b'empty-id'))
163
def test_guess_renames_dry_run(self):
164
tree = self.make_branch_and_tree('tree')
166
self.addCleanup(tree.unlock)
167
self.build_tree(['tree/file'])
168
tree.add('file', b'file-id')
169
tree.commit('Added file')
170
os.rename('tree/file', 'tree/file2')
171
RenameMap.guess_renames(tree.basis_tree(), tree, dry_run=True)
172
self.assertEqual('file', tree.id2path(b'file-id'))
175
def captureNotes(cmd, *args, **kwargs):
178
def my_note(fmt, *args):
179
notes.append(fmt % args)
180
old_note = trace.note
183
result = cmd(*args, **kwargs)
185
trace.note = old_note
188
def test_guess_renames_output(self):
189
"""guess_renames emits output whether dry_run is True or False."""
190
tree = self.make_branch_and_tree('tree')
192
self.addCleanup(tree.unlock)
193
self.build_tree(['tree/file'])
194
tree.add('file', b'file-id')
195
tree.commit('Added file')
196
os.rename('tree/file', 'tree/file2')
197
notes = self.captureNotes(
198
RenameMap.guess_renames, tree.basis_tree(), tree,
200
self.assertEqual('file => file2', ''.join(notes))
201
notes = self.captureNotes(RenameMap.guess_renames, tree.basis_tree(),
202
tree, dry_run=False)[0]
203
self.assertEqual('file => file2', ''.join(notes))
205
def test_guess_rename_handles_new_directories(self):
206
"""When a file was moved into a new directory."""
207
tree = self.make_branch_and_tree('.')
209
# self.addCleanup(tree.unlock)
210
self.build_tree(['file'])
211
tree.add('file', b'file-id')
212
tree.commit('Added file')
214
os.rename('file', 'folder/file2')
215
notes = self.captureNotes(
216
RenameMap.guess_renames, tree.basis_tree(), tree)[0]
217
self.assertEqual('file => folder/file2', ''.join(notes))