/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
20
from breezy import trace
21
from breezy.rename_map import RenameMap
22
from breezy.tests import TestCaseWithTransport
3193.8.4 by Aaron Bentley
Get rename detection working for files.
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
6855.4.1 by Jelmer Vernooij
Yet more bees.
32
    a_lines = b'a\nb\nc\n'.splitlines(True)
33
    b_lines = b'b\nc\nd\n'.splitlines(True)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
34
35
    def test_add_edge_hashes(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
36
        rn = RenameMap(None)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
37
        rn.add_edge_hashes(self.a_lines, 'a')
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
38
        self.assertEqual({'a'}, rn.edge_hashes[myhash(('a\n', 'b\n'))])
39
        self.assertEqual({'a'}, rn.edge_hashes[myhash(('b\n', 'c\n'))])
3193.8.8 by Aaron Bentley
Get tests passing.
40
        self.assertIs(None, rn.edge_hashes.get(myhash(('c\n', 'd\n'))))
3193.8.4 by Aaron Bentley
Get rename detection working for files.
41
42
    def test_add_file_edge_hashes(self):
43
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
44
        self.build_tree_contents([('tree/a', b''.join(self.a_lines))])
45
        tree.add('a', b'a')
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
46
        rn = RenameMap(tree)
7045.4.13 by Jelmer Vernooij
Some more test fixes.
47
        rn.add_file_edge_hashes(tree, [b'a'])
7058.4.15 by Jelmer Vernooij
Fix rename map.
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'))])
3193.8.8 by Aaron Bentley
Get tests passing.
50
        self.assertIs(None, rn.edge_hashes.get(myhash(('c\n', 'd\n'))))
3193.8.4 by Aaron Bentley
Get rename detection working for files.
51
52
    def test_hitcounts(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
53
        rn = RenameMap(None)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
54
        rn.add_edge_hashes(self.a_lines, 'a')
55
        rn.add_edge_hashes(self.b_lines, 'b')
3193.8.10 by Aaron Bentley
Update to weight hits and use 10M of keyspace
56
        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.
57
        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
58
        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.
59
60
    def test_file_match(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
61
        tree = self.make_branch_and_tree('tree')
62
        rn = RenameMap(tree)
3193.8.4 by Aaron Bentley
Get rename detection working for files.
63
        rn.add_edge_hashes(self.a_lines, 'aid')
64
        rn.add_edge_hashes(self.b_lines, 'bid')
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
65
        self.build_tree_contents([('tree/a', b''.join(self.a_lines))])
66
        self.build_tree_contents([('tree/b', b''.join(self.b_lines))])
3193.8.4 by Aaron Bentley
Get rename detection working for files.
67
        self.assertEqual({'a': 'aid', 'b': 'bid'},
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
68
                         rn.file_match(['a', 'b']))
3193.8.5 by Aaron Bentley
Improve rename detection
69
70
    def test_file_match_no_dups(self):
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
71
        tree = self.make_branch_and_tree('tree')
72
        rn = RenameMap(tree)
3193.8.5 by Aaron Bentley
Improve rename detection
73
        rn.add_edge_hashes(self.a_lines, 'aid')
6855.4.1 by Jelmer Vernooij
Yet more bees.
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))])
3193.8.5 by Aaron Bentley
Improve rename detection
77
        self.assertEqual({'a': 'aid'},
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
78
                         rn.file_match(['a', 'b', 'c']))
3193.8.16 by Aaron Bentley
Get a dict of required parents.
79
80
    def test_match_directories(self):
81
        tree = self.make_branch_and_tree('tree')
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
82
        rn = RenameMap(tree)
3193.8.16 by Aaron Bentley
Get a dict of required parents.
83
        required_parents = rn.get_required_parents({
84
            'path1': 'a',
85
            'path2/tr': 'b',
3193.8.17 by Aaron Bentley
Get directory rename handling working.
86
            'path3/path4/path5': 'c',
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
87
        })
3193.8.17 by Aaron Bentley
Get directory rename handling working.
88
        self.assertEqual(
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
89
            {'path2': {'b'}, 'path3/path4': {'c'}, 'path3': set()},
3193.8.17 by Aaron Bentley
Get directory rename handling working.
90
            required_parents)
91
92
    def test_find_directory_renames(self):
93
        tree = self.make_branch_and_tree('tree')
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
94
        rn = RenameMap(tree)
3193.8.17 by Aaron Bentley
Get directory rename handling working.
95
        matches = {
96
            'path1': 'a',
97
            'path3/path4/path5': 'c',
98
        }
99
        required_parents = {
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
100
            'path2': {'b'},
101
            'path3/path4': {'c'},
3193.8.17 by Aaron Bentley
Get directory rename handling working.
102
            'path3': set([])}
103
        missing_parents = {
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
104
            'path2-id': {'b'},
105
            'path4-id': {'c'},
106
            'path3-id': {'path4-id'}}
3193.8.24 by Aaron Bentley
Use tree member instead of passing it in
107
        matches = rn.match_parents(required_parents, missing_parents)
3193.8.17 by Aaron Bentley
Get directory rename handling working.
108
        self.assertEqual({'path3/path4': 'path4-id', 'path2': 'path2-id'},
109
                         matches)
3193.8.18 by Aaron Bentley
Move all rename-guessing into RenameMap
110
111
    def test_guess_renames(self):
112
        tree = self.make_branch_and_tree('tree')
113
        tree.lock_write()
114
        self.addCleanup(tree.unlock)
115
        self.build_tree(['tree/file'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
116
        tree.add('file', b'file-id')
3193.8.18 by Aaron Bentley
Move all rename-guessing into RenameMap
117
        tree.commit('Added file')
118
        os.rename('tree/file', 'tree/file2')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
119
        RenameMap.guess_renames(tree.basis_tree(), tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
120
        self.assertEqual('file2', tree.id2path(b'file-id'))
3193.8.18 by Aaron Bentley
Move all rename-guessing into RenameMap
121
122
    def test_guess_renames_handles_directories(self):
123
        tree = self.make_branch_and_tree('tree')
124
        tree.lock_write()
125
        self.addCleanup(tree.unlock)
126
        self.build_tree(['tree/dir/', 'tree/dir/file'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
127
        tree.add(['dir', 'dir/file'], [b'dir-id', b'file-id'])
3193.8.18 by Aaron Bentley
Move all rename-guessing into RenameMap
128
        tree.commit('Added file')
129
        os.rename('tree/dir', 'tree/dir2')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
130
        RenameMap.guess_renames(tree.basis_tree(), tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
131
        self.assertEqual('dir2/file', tree.id2path(b'file-id'))
132
        self.assertEqual('dir2', tree.id2path(b'dir-id'))
3193.8.21 by Aaron Bentley
Add support for guessing grandparents with nonzero files.
133
134
    def test_guess_renames_handles_grandparent_directories(self):
135
        tree = self.make_branch_and_tree('tree')
136
        tree.lock_write()
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'],
6855.4.1 by Jelmer Vernooij
Yet more bees.
142
                 [b'topdir-id', b'middledir-id', b'file-id'])
3193.8.21 by Aaron Bentley
Add support for guessing grandparents with nonzero files.
143
        tree.commit('Added files.')
144
        os.rename('tree/topdir', 'tree/topdir2')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
145
        RenameMap.guess_renames(tree.basis_tree(), tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
146
        self.assertEqual('topdir2', tree.id2path(b'topdir-id'))
3193.8.28 by Aaron Bentley
Add test for guessing renames.
147
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')
151
        tree.lock_write()
4327.1.6 by Vincent Ladeuil
Fix 1 more lock-related test failure.
152
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
153
        self.build_tree_contents([('tree/foo/', b''),
154
                                  ('tree/foo/bar', b'bar'),
155
                                  ('tree/foo/empty', b'')])
3193.8.28 by Aaron Bentley
Add test for guessing renames.
156
        tree.add(['foo', 'foo/bar', 'foo/empty'],
6855.4.1 by Jelmer Vernooij
Yet more bees.
157
                 [b'foo-id', b'bar-id', b'empty-id'])
3193.8.28 by Aaron Bentley
Add test for guessing renames.
158
        tree.commit('rev1')
159
        os.rename('tree/foo', 'tree/baz')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
160
        RenameMap.guess_renames(tree.basis_tree(), tree)
6855.4.1 by Jelmer Vernooij
Yet more bees.
161
        self.assertEqual('baz/empty', tree.id2path(b'empty-id'))
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
162
163
    def test_guess_renames_dry_run(self):
164
        tree = self.make_branch_and_tree('tree')
165
        tree.lock_write()
166
        self.addCleanup(tree.unlock)
167
        self.build_tree(['tree/file'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
168
        tree.add('file', b'file-id')
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
169
        tree.commit('Added file')
170
        os.rename('tree/file', 'tree/file2')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
171
        RenameMap.guess_renames(tree.basis_tree(), tree, dry_run=True)
6855.4.1 by Jelmer Vernooij
Yet more bees.
172
        self.assertEqual('file', tree.id2path(b'file-id'))
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
173
174
    @staticmethod
175
    def captureNotes(cmd, *args, **kwargs):
176
        notes = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
177
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
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'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
194
        tree.add('file', b'file-id')
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
195
        tree.commit('Added file')
196
        os.rename('tree/file', 'tree/file2')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
197
        notes = self.captureNotes(
7143.15.2 by Jelmer Vernooij
Run autopep8.
198
            RenameMap.guess_renames, tree.basis_tree(), tree,
199
            dry_run=True)[0]
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
200
        self.assertEqual('file => file2', ''.join(notes))
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
201
        notes = self.captureNotes(RenameMap.guess_renames, tree.basis_tree(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
202
                                  tree, dry_run=False)[0]
3193.8.34 by Aaron Bentley
Add tests of guess_renames output.
203
        self.assertEqual('file => file2', ''.join(notes))
6547.1.1 by mnn
Fixed issue with RenameMap - also it supports renaming into new unversioned directory
204
205
    def test_guess_rename_handles_new_directories(self):
206
        """When a file was moved into a new directory."""
6547.1.2 by mnn
Simplified test_guess_rename_handles_new_directories
207
        tree = self.make_branch_and_tree('.')
6547.1.1 by mnn
Fixed issue with RenameMap - also it supports renaming into new unversioned directory
208
        tree.lock_write()
7143.15.2 by Jelmer Vernooij
Run autopep8.
209
        # self.addCleanup(tree.unlock)
6547.1.2 by mnn
Simplified test_guess_rename_handles_new_directories
210
        self.build_tree(['file'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
211
        tree.add('file', b'file-id')
6547.1.1 by mnn
Fixed issue with RenameMap - also it supports renaming into new unversioned directory
212
        tree.commit('Added file')
6547.1.2 by mnn
Simplified test_guess_rename_handles_new_directories
213
        os.mkdir('folder')
214
        os.rename('file', 'folder/file2')
6883.5.1 by Jelmer Vernooij
Add from_tree argument to RenameMap.guess_renames.
215
        notes = self.captureNotes(
7143.15.2 by Jelmer Vernooij
Run autopep8.
216
            RenameMap.guess_renames, tree.basis_tree(), tree)[0]
6547.1.1 by mnn
Fixed issue with RenameMap - also it supports renaming into new unversioned directory
217
        self.assertEqual('file => folder/file2', ''.join(notes))
218
219
        tree.unlock()