~bzr-pqm/bzr/bzr.dev

3193.8.13 by Aaron Bentley
Update texts
1
# Copyright (C) 2009 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
3193.8.32 by Aaron Bentley
Update GPL preamble
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3193.8.13 by Aaron Bentley
Update texts
16
17
3193.8.18 by Aaron Bentley
Move all rename-guessing into RenameMap
18
import os
19
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
20
from bzrlib import trace
3193.8.4 by Aaron Bentley
Get rename detection working for files.
21
from bzrlib.rename_map import RenameMap
22
from bzrlib.tests import TestCaseWithTransport
23
3193.8.8 by Aaron Bentley
Get tests passing.
24
25
def myhash(val):
26
    """This the hash used by RenameMap."""
3193.8.10 by Aaron Bentley
Update to weight hits and use 10M of keyspace
27
    return hash(val) % (1024 * 1024 * 10)
3193.8.8 by Aaron Bentley
Get tests passing.
28
29
3193.8.4 by Aaron Bentley
Get rename detection working for files.
30
class TestRenameMap(TestCaseWithTransport):
31
32
    a_lines = 'a\nb\nc\n'.splitlines(True)
33
    b_lines = 'b\nc\nd\n'.splitlines(True)
34
35
36
    def test_add_edge_hashes(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
37
        rn = RenameMap(None)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
38
        rn.add_edge_hashes(self.a_lines, 'a')
3193.8.8 by Aaron Bentley
Get tests passing.
39
        self.assertEqual(set(['a']), rn.edge_hashes[myhash(('a\n', 'b\n'))])
40
        self.assertEqual(set(['a']), rn.edge_hashes[myhash(('b\n', 'c\n'))])
41
        self.assertIs(None, rn.edge_hashes.get(myhash(('c\n', 'd\n'))))
3193.8.4 by Aaron Bentley
Get rename detection working for files.
42
43
    def test_add_file_edge_hashes(self):
44
        tree = self.make_branch_and_tree('tree')
45
        self.build_tree_contents([('tree/a', ''.join(self.a_lines))])
46
        tree.add('a', 'a')
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
47
        rn = RenameMap(tree)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
48
        rn.add_file_edge_hashes(tree, ['a'])
3193.8.8 by Aaron Bentley
Get tests passing.
49
        self.assertEqual(set(['a']), rn.edge_hashes[myhash(('a\n', 'b\n'))])
50
        self.assertEqual(set(['a']), rn.edge_hashes[myhash(('b\n', 'c\n'))])
51
        self.assertIs(None, rn.edge_hashes.get(myhash(('c\n', 'd\n'))))
3193.8.4 by Aaron Bentley
Get rename detection working for files.
52
53
    def test_hitcounts(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
54
        rn = RenameMap(None)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
55
        rn.add_edge_hashes(self.a_lines, 'a')
56
        rn.add_edge_hashes(self.b_lines, 'b')
3193.8.10 by Aaron Bentley
Update to weight hits and use 10M of keyspace
57
        self.assertEqual({'a': 2.5, 'b': 0.5}, rn.hitcounts(self.a_lines))
3193.8.4 by Aaron Bentley
Get rename detection working for files.
58
        self.assertEqual({'a': 1}, rn.hitcounts(self.a_lines[:-1]))
3193.8.10 by Aaron Bentley
Update to weight hits and use 10M of keyspace
59
        self.assertEqual({'b': 2.5, 'a': 0.5}, rn.hitcounts(self.b_lines))
3193.8.4 by Aaron Bentley
Get rename detection working for files.
60
61
    def test_file_match(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
62
        tree = self.make_branch_and_tree('tree')
63
        rn = RenameMap(tree)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
64
        rn.add_edge_hashes(self.a_lines, 'aid')
65
        rn.add_edge_hashes(self.b_lines, 'bid')
66
        self.build_tree_contents([('tree/a', ''.join(self.a_lines))])
67
        self.build_tree_contents([('tree/b', ''.join(self.b_lines))])
68
        self.assertEqual({'a': 'aid', 'b': 'bid'},
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
69
                         rn.file_match(['a', 'b']))
3193.8.5 by Aaron Bentley
Improve rename detection
70
71
    def test_file_match_no_dups(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
72
        tree = self.make_branch_and_tree('tree')
73
        rn = RenameMap(tree)
3193.8.5 by Aaron Bentley
Improve rename detection
74
        rn.add_edge_hashes(self.a_lines, 'aid')
75
        self.build_tree_contents([('tree/a', ''.join(self.a_lines))])
76
        self.build_tree_contents([('tree/b', ''.join(self.b_lines))])
77
        self.build_tree_contents([('tree/c', ''.join(self.b_lines))])
78
        self.assertEqual({'a': 'aid'},
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
79
                         rn.file_match(['a', 'b', 'c']))
3193.8.16 by Aaron Bentley
Get a dict of required parents.
80
81
    def test_match_directories(self):
82
        tree = self.make_branch_and_tree('tree')
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
83
        rn = RenameMap(tree)
3193.8.16 by Aaron Bentley
Get a dict of required parents.
84
        required_parents = rn.get_required_parents({
85
            'path1': 'a',
86
            'path2/tr': 'b',
3193.8.17 by Aaron Bentley
Get directory rename handling working.
87
            'path3/path4/path5': 'c',
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
88
        })
3193.8.17 by Aaron Bentley
Get directory rename handling working.
89
        self.assertEqual(
90
            {'path2': set(['b']), 'path3/path4': set(['c']), 'path3': set()},
91
            required_parents)
92
93
    def test_find_directory_renames(self):
94
        tree = self.make_branch_and_tree('tree')
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
95
        rn = RenameMap(tree)
3193.8.17 by Aaron Bentley
Get directory rename handling working.
96
        matches = {
97
            'path1': 'a',
98
            'path3/path4/path5': 'c',
99
        }
100
        required_parents = {
101
            'path2': set(['b']),
102
            'path3/path4': set(['c']),
103
            'path3': set([])}
104
        missing_parents = {
105
            'path2-id': set(['b']),
106
            'path4-id': set(['c']),
107
            'path3-id': set(['path4-id'])}
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
108
        matches = rn.match_parents(required_parents, missing_parents)
3193.8.17 by Aaron Bentley
Get directory rename handling working.
109
        self.assertEqual({'path3/path4': 'path4-id', 'path2': 'path2-id'},
110
                         matches)
3193.8.18 by Aaron Bentley
Move all rename-guessing into RenameMap
111
112
    def test_guess_renames(self):
113
        tree = self.make_branch_and_tree('tree')
114
        tree.lock_write()
115
        self.addCleanup(tree.unlock)
116
        self.build_tree(['tree/file'])
117
        tree.add('file', 'file-id')
118
        tree.commit('Added file')
119
        os.rename('tree/file', 'tree/file2')
120
        RenameMap.guess_renames(tree)
121
        self.assertEqual('file2', tree.id2path('file-id'))
122
123
    def test_guess_renames_handles_directories(self):
124
        tree = self.make_branch_and_tree('tree')
125
        tree.lock_write()
126
        self.addCleanup(tree.unlock)
127
        self.build_tree(['tree/dir/', 'tree/dir/file'])
128
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
129
        tree.commit('Added file')
130
        os.rename('tree/dir', 'tree/dir2')
131
        RenameMap.guess_renames(tree)
132
        self.assertEqual('dir2/file', tree.id2path('file-id'))
3193.8.20 by Aaron Bentley
Cleanup and enhance tests.
133
        self.assertEqual('dir2', tree.id2path('dir-id'))
3193.8.21 by Aaron Bentley
Add support for guessing grandparents with nonzero files.
134
135
    def test_guess_renames_handles_grandparent_directories(self):
136
        tree = self.make_branch_and_tree('tree')
137
        tree.lock_write()
138
        self.addCleanup(tree.unlock)
139
        self.build_tree(['tree/topdir/',
140
                         'tree/topdir/middledir/',
141
                         'tree/topdir/middledir/file'])
142
        tree.add(['topdir', 'topdir/middledir', 'topdir/middledir/file'],
143
                 ['topdir-id', 'middledir-id', 'file-id'])
144
        tree.commit('Added files.')
145
        os.rename('tree/topdir', 'tree/topdir2')
146
        RenameMap.guess_renames(tree)
147
        self.assertEqual('topdir2', tree.id2path('topdir-id'))
3193.8.28 by Aaron Bentley
Add test for guessing renames.
148
149
    def test_guess_renames_preserves_children(self):
150
        """When a directory has been moved, its children are preserved."""
151
        tree = self.make_branch_and_tree('tree')
152
        tree.lock_write()
4327.1.6 by Vincent Ladeuil
Fix 1 more lock-related test failure.
153
        self.addCleanup(tree.unlock)
3193.8.28 by Aaron Bentley
Add test for guessing renames.
154
        self.build_tree_contents([('tree/foo/', ''),
155
                                  ('tree/foo/bar', 'bar'),
156
                                  ('tree/foo/empty', '')])
157
        tree.add(['foo', 'foo/bar', 'foo/empty'],
158
                 ['foo-id', 'bar-id', 'empty-id'])
159
        tree.commit('rev1')
160
        os.rename('tree/foo', 'tree/baz')
161
        RenameMap.guess_renames(tree)
162
        self.assertEqual('baz/empty', tree.id2path('empty-id'))
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
163
164
    def test_guess_renames_dry_run(self):
165
        tree = self.make_branch_and_tree('tree')
166
        tree.lock_write()
167
        self.addCleanup(tree.unlock)
168
        self.build_tree(['tree/file'])
169
        tree.add('file', 'file-id')
170
        tree.commit('Added file')
171
        os.rename('tree/file', 'tree/file2')
172
        RenameMap.guess_renames(tree, dry_run=True)
173
        self.assertEqual('file', tree.id2path('file-id'))
174
175
    @staticmethod
176
    def captureNotes(cmd, *args, **kwargs):
177
        notes = []
178
        def my_note(fmt, *args):
179
            notes.append(fmt % args)
180
        old_note = trace.note
181
        trace.note = my_note
182
        try:
183
            result = cmd(*args, **kwargs)
184
        finally:
185
            trace.note = old_note
186
        return notes, result
187
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')
191
        tree.lock_write()
192
        self.addCleanup(tree.unlock)
193
        self.build_tree(['tree/file'])
194
        tree.add('file', 'file-id')
195
        tree.commit('Added file')
196
        os.rename('tree/file', 'tree/file2')
197
        notes = self.captureNotes(RenameMap.guess_renames, tree,
198
                                  dry_run=True)[0]
199
        self.assertEqual('file => file2', ''.join(notes))
200
        notes = self.captureNotes(RenameMap.guess_renames, tree,
201
                                  dry_run=False)[0]
202
        self.assertEqual('file => file2', ''.join(notes))