/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__annotator.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for Annotators."""
18
18
 
19
 
from .. import (
 
19
from bzrlib import (
20
20
    annotate,
 
21
    _annotator_py,
21
22
    errors,
 
23
    knit,
22
24
    revision,
23
25
    tests,
24
26
    )
25
27
 
26
 
from ..bzr import (
27
 
    knit,
28
 
    )
29
 
 
30
 
 
31
 
def load_tests(loader, standard_tests, pattern):
 
28
 
 
29
def load_tests(standard_tests, module, loader):
32
30
    """Parameterize tests for all versions of groupcompress."""
33
31
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
34
 
                                                 'breezy._annotator_py', 'breezy._annotator_pyx')
 
32
        'bzrlib._annotator_py', 'bzrlib._annotator_pyx')
35
33
    return suite
36
34
 
37
35
 
38
36
class TestAnnotator(tests.TestCaseWithMemoryTransport):
39
37
 
40
 
    module = None  # Set by load_tests
 
38
    module = None # Set by load_tests
41
39
 
42
 
    fa_key = (b'f-id', b'a-id')
43
 
    fb_key = (b'f-id', b'b-id')
44
 
    fc_key = (b'f-id', b'c-id')
45
 
    fd_key = (b'f-id', b'd-id')
46
 
    fe_key = (b'f-id', b'e-id')
47
 
    ff_key = (b'f-id', b'f-id')
 
40
    fa_key = ('f-id', 'a-id')
 
41
    fb_key = ('f-id', 'b-id')
 
42
    fc_key = ('f-id', 'c-id')
 
43
    fd_key = ('f-id', 'd-id')
 
44
    fe_key = ('f-id', 'e-id')
 
45
    ff_key = ('f-id', 'f-id')
48
46
 
49
47
    def make_no_graph_texts(self):
50
48
        factory = knit.make_pack_factory(False, False, 2)
51
49
        self.vf = factory(self.get_transport())
52
50
        self.ann = self.module.Annotator(self.vf)
53
 
        self.vf.add_lines(self.fa_key, (), [b'simple\n', b'content\n'])
54
 
        self.vf.add_lines(self.fb_key, (), [b'simple\n', b'new content\n'])
 
51
        self.vf.add_lines(self.fa_key, (), ['simple\n', 'content\n'])
 
52
        self.vf.add_lines(self.fb_key, (), ['simple\n', 'new content\n'])
55
53
 
56
54
    def make_simple_text(self):
57
55
        # TODO: all we really need is a VersionedFile instance, we'd like to
64
62
        #  A    'simple|content|'
65
63
        #  |
66
64
        #  B    'simple|new content|'
67
 
        self.vf.add_lines(self.fa_key, [], [b'simple\n', b'content\n'])
 
65
        self.vf.add_lines(self.fa_key, [], ['simple\n', 'content\n'])
68
66
        self.vf.add_lines(self.fb_key, [self.fa_key],
69
 
                          [b'simple\n', b'new content\n'])
 
67
                          ['simple\n', 'new content\n'])
70
68
 
71
69
    def make_merge_text(self):
72
70
        self.make_simple_text()
78
76
        #  |/
79
77
        #  D    'simple|from c|new content|introduced in merge|'
80
78
        self.vf.add_lines(self.fc_key, [self.fa_key],
81
 
                          [b'simple\n', b'from c\n', b'content\n'])
 
79
                          ['simple\n', 'from c\n', 'content\n'])
82
80
        self.vf.add_lines(self.fd_key, [self.fb_key, self.fc_key],
83
 
                          [b'simple\n', b'from c\n', b'new content\n',
84
 
                           b'introduced in merge\n'])
 
81
                          ['simple\n', 'from c\n', 'new content\n',
 
82
                           'introduced in merge\n'])
85
83
 
86
84
    def make_common_merge_text(self):
87
85
        """Both sides of the merge will have introduced a line."""
94
92
        #  |/
95
93
        #  D    'simple|new content|'
96
94
        self.vf.add_lines(self.fc_key, [self.fa_key],
97
 
                          [b'simple\n', b'new content\n'])
 
95
                          ['simple\n', 'new content\n'])
98
96
        self.vf.add_lines(self.fd_key, [self.fb_key, self.fc_key],
99
 
                          [b'simple\n', b'new content\n'])
 
97
                          ['simple\n', 'new content\n'])
100
98
 
101
99
    def make_many_way_common_merge_text(self):
102
100
        self.make_simple_text()
112
110
        #  |  /
113
111
        #  F-'    'simple|new content|'
114
112
        self.vf.add_lines(self.fc_key, [self.fa_key],
115
 
                          [b'simple\n', b'new content\n'])
 
113
                          ['simple\n', 'new content\n'])
116
114
        self.vf.add_lines(self.fd_key, [self.fb_key, self.fc_key],
117
 
                          [b'simple\n', b'new content\n'])
 
115
                          ['simple\n', 'new content\n'])
118
116
        self.vf.add_lines(self.fe_key, [self.fa_key],
119
 
                          [b'simple\n', b'new content\n'])
 
117
                          ['simple\n', 'new content\n'])
120
118
        self.vf.add_lines(self.ff_key, [self.fd_key, self.fe_key],
121
 
                          [b'simple\n', b'new content\n'])
 
119
                          ['simple\n', 'new content\n'])
122
120
 
123
121
    def make_merge_and_restored_text(self):
124
122
        self.make_simple_text()
131
129
        #    D  'simple|content|'
132
130
        # c reverts back to 'a' for the new content line
133
131
        self.vf.add_lines(self.fc_key, [self.fb_key],
134
 
                          [b'simple\n', b'content\n'])
 
132
                          ['simple\n', 'content\n'])
135
133
        # d merges 'a' and 'c', to find both claim last modified
136
134
        self.vf.add_lines(self.fd_key, [self.fa_key, self.fc_key],
137
 
                          [b'simple\n', b'content\n'])
 
135
                          ['simple\n', 'content\n'])
138
136
 
139
137
    def assertAnnotateEqual(self, expected_annotation, key, exp_text=None):
140
138
        annotation, lines = self.ann.annotate(key)
141
139
        self.assertEqual(expected_annotation, annotation)
142
140
        if exp_text is None:
143
 
            record = next(self.vf.get_record_stream([key], 'unordered', True))
 
141
            record = self.vf.get_record_stream([key], 'unordered', True).next()
144
142
            exp_text = record.get_bytes_as('fulltext')
145
 
        self.assertEqualDiff(exp_text, b''.join(lines))
 
143
        self.assertEqualDiff(exp_text, ''.join(lines))
146
144
 
147
145
    def test_annotate_missing(self):
148
146
        self.make_simple_text()
149
147
        self.assertRaises(errors.RevisionNotPresent,
150
 
                          self.ann.annotate, (b'not', b'present'))
 
148
                          self.ann.annotate, ('not', 'present'))
151
149
 
152
150
    def test_annotate_simple(self):
153
151
        self.make_simple_text()
154
 
        self.assertAnnotateEqual([(self.fa_key,)] * 2, self.fa_key)
 
152
        self.assertAnnotateEqual([(self.fa_key,)]*2, self.fa_key)
155
153
        self.assertAnnotateEqual([(self.fa_key,), (self.fb_key,)], self.fb_key)
156
154
 
157
155
    def test_annotate_merge_text(self):
178
176
 
179
177
    def test_annotate_flat_simple(self):
180
178
        self.make_simple_text()
181
 
        self.assertEqual([(self.fa_key, b'simple\n'),
182
 
                          (self.fa_key, b'content\n'),
183
 
                          ], self.ann.annotate_flat(self.fa_key))
184
 
        self.assertEqual([(self.fa_key, b'simple\n'),
185
 
                          (self.fb_key, b'new content\n'),
186
 
                          ], self.ann.annotate_flat(self.fb_key))
 
179
        self.assertEqual([(self.fa_key, 'simple\n'),
 
180
                          (self.fa_key, 'content\n'),
 
181
                         ], self.ann.annotate_flat(self.fa_key))
 
182
        self.assertEqual([(self.fa_key, 'simple\n'),
 
183
                          (self.fb_key, 'new content\n'),
 
184
                         ], self.ann.annotate_flat(self.fb_key))
187
185
 
188
186
    def test_annotate_flat_merge_and_restored_text(self):
189
187
        self.make_merge_and_restored_text()
190
188
        # fc is a simple dominator of fa
191
 
        self.assertEqual([(self.fa_key, b'simple\n'),
192
 
                          (self.fc_key, b'content\n'),
193
 
                          ], self.ann.annotate_flat(self.fd_key))
 
189
        self.assertEqual([(self.fa_key, 'simple\n'),
 
190
                          (self.fc_key, 'content\n'),
 
191
                         ], self.ann.annotate_flat(self.fd_key))
194
192
 
195
193
    def test_annotate_common_merge_text(self):
196
194
        self.make_common_merge_text()
197
195
        # there is no common point, so we just pick the lexicographical lowest
198
 
        # and b'b-id' comes before b'c-id'
199
 
        self.assertEqual([(self.fa_key, b'simple\n'),
200
 
                          (self.fb_key, b'new content\n'),
201
 
                          ], self.ann.annotate_flat(self.fd_key))
 
196
        # and 'b-id' comes before 'c-id'
 
197
        self.assertEqual([(self.fa_key, 'simple\n'),
 
198
                          (self.fb_key, 'new content\n'),
 
199
                         ], self.ann.annotate_flat(self.fd_key))
202
200
 
203
201
    def test_annotate_many_way_common_merge_text(self):
204
202
        self.make_many_way_common_merge_text()
205
 
        self.assertEqual([(self.fa_key, b'simple\n'),
206
 
                          (self.fb_key, b'new content\n')],
 
203
        self.assertEqual([(self.fa_key, 'simple\n'),
 
204
                         (self.fb_key, 'new content\n')],
207
205
                         self.ann.annotate_flat(self.ff_key))
208
206
 
209
207
    def test_annotate_flat_respects_break_ann_tie(self):
210
 
        seen = set()
 
208
        tiebreaker = annotate._break_annotation_tie
 
209
        try:
 
210
            calls = []
 
211
            def custom_tiebreaker(annotated_lines):
 
212
                self.assertEqual(2, len(annotated_lines))
 
213
                left = annotated_lines[0]
 
214
                self.assertEqual(2, len(left))
 
215
                self.assertEqual('new content\n', left[1])
 
216
                right = annotated_lines[1]
 
217
                self.assertEqual(2, len(right))
 
218
                self.assertEqual('new content\n', right[1])
 
219
                calls.append((left[0], right[0]))
 
220
                # Our custom tiebreaker takes the *largest* value, rather than
 
221
                # the *smallest* value
 
222
                if left[0] < right[0]:
 
223
                    return right
 
224
                else:
 
225
                    return left
 
226
            annotate._break_annotation_tie = custom_tiebreaker
 
227
            self.make_many_way_common_merge_text()
 
228
            self.assertEqual([(self.fa_key, 'simple\n'),
 
229
                             (self.fe_key, 'new content\n')],
 
230
                             self.ann.annotate_flat(self.ff_key))
 
231
            self.assertEqual([(self.fe_key, self.fc_key),
 
232
                              (self.fe_key, self.fb_key)], calls)
 
233
        finally:
 
234
            annotate._break_annotation_tie = tiebreaker
211
235
 
212
 
        def custom_tiebreaker(annotated_lines):
213
 
            self.assertEqual(2, len(annotated_lines))
214
 
            left = annotated_lines[0]
215
 
            self.assertEqual(2, len(left))
216
 
            self.assertEqual(b'new content\n', left[1])
217
 
            right = annotated_lines[1]
218
 
            self.assertEqual(2, len(right))
219
 
            self.assertEqual(b'new content\n', right[1])
220
 
            seen.update([left[0], right[0]])
221
 
            # Our custom tiebreaker takes the *largest* value, rather than
222
 
            # the *smallest* value
223
 
            if left[0] < right[0]:
224
 
                return right
225
 
            else:
226
 
                return left
227
 
        self.overrideAttr(annotate, '_break_annotation_tie', custom_tiebreaker)
228
 
        self.make_many_way_common_merge_text()
229
 
        self.assertEqual([(self.fa_key, b'simple\n'),
230
 
                          (self.fe_key, b'new content\n')],
231
 
                         self.ann.annotate_flat(self.ff_key))
232
 
        # Calls happen in set iteration order but should keys should be seen
233
 
        self.assertEqual({self.fb_key, self.fc_key, self.fe_key}, seen)
234
236
 
235
237
    def test_needed_keys_simple(self):
236
238
        self.make_simple_text()
245
247
        keys, ann_keys = self.ann._get_needed_keys(self.ff_key)
246
248
        self.assertEqual([self.fa_key, self.fb_key, self.fc_key,
247
249
                          self.fd_key, self.fe_key, self.ff_key,
248
 
                          ], sorted(keys))
 
250
                         ], sorted(keys))
249
251
        self.assertEqual({self.fa_key: 3,
250
252
                          self.fb_key: 1,
251
253
                          self.fc_key: 1,
252
254
                          self.fd_key: 1,
253
255
                          self.fe_key: 1,
254
256
                          self.ff_key: 1,
255
 
                          }, self.ann._num_needed_children)
 
257
                         }, self.ann._num_needed_children)
256
258
        self.assertEqual(set(), ann_keys)
257
259
 
258
260
    def test_needed_keys_with_special_text(self):
259
261
        self.make_many_way_common_merge_text()
260
 
        spec_key = (b'f-id', revision.CURRENT_REVISION)
261
 
        spec_text = b'simple\nnew content\nlocally modified\n'
 
262
        spec_key = ('f-id', revision.CURRENT_REVISION)
 
263
        spec_text = 'simple\nnew content\nlocally modified\n'
262
264
        self.ann.add_special_text(spec_key, [self.fd_key, self.fe_key],
263
265
                                  spec_text)
264
266
        keys, ann_keys = self.ann._get_needed_keys(spec_key)
265
267
        self.assertEqual([self.fa_key, self.fb_key, self.fc_key,
266
268
                          self.fd_key, self.fe_key,
267
 
                          ], sorted(keys))
 
269
                         ], sorted(keys))
268
270
        self.assertEqual([spec_key], sorted(ann_keys))
269
271
 
270
272
    def test_needed_keys_with_parent_texts(self):
277
279
        #  |  /
278
280
        #  F-'    'simple|new content|'
279
281
        self.ann._parent_map[self.fd_key] = (self.fb_key, self.fc_key)
280
 
        self.ann._text_cache[self.fd_key] = [b'simple\n', b'new content\n']
 
282
        self.ann._text_cache[self.fd_key] = ['simple\n', 'new content\n']
281
283
        self.ann._annotations_cache[self.fd_key] = [
282
284
            (self.fa_key,),
283
285
            (self.fb_key, self.fc_key),
284
286
            ]
285
287
        self.ann._parent_map[self.fe_key] = (self.fa_key,)
286
 
        self.ann._text_cache[self.fe_key] = [b'simple\n', b'new content\n']
 
288
        self.ann._text_cache[self.fe_key] = ['simple\n', 'new content\n']
287
289
        self.ann._annotations_cache[self.fe_key] = [
288
290
            (self.fa_key,),
289
291
            (self.fe_key,),
293
295
        self.assertEqual({self.fd_key: 1,
294
296
                          self.fe_key: 1,
295
297
                          self.ff_key: 1,
296
 
                          }, self.ann._num_needed_children)
 
298
                         }, self.ann._num_needed_children)
297
299
        self.assertEqual([], sorted(ann_keys))
298
300
 
299
301
    def test_record_annotation_removes_texts(self):
307
309
                          self.fd_key: 1,
308
310
                          self.fe_key: 1,
309
311
                          self.ff_key: 1,
310
 
                          }, self.ann._num_needed_children)
 
312
                         }, self.ann._num_needed_children)
311
313
        self.assertEqual([self.fa_key, self.fb_key, self.fc_key,
312
314
                          self.fd_key, self.fe_key, self.ff_key,
313
 
                          ], sorted(self.ann._text_cache.keys()))
 
315
                         ], sorted(self.ann._text_cache.keys()))
314
316
        self.ann._record_annotation(self.fa_key, [], [])
315
317
        self.ann._record_annotation(self.fb_key, [self.fa_key], [])
316
318
        self.assertEqual({self.fa_key: 2,
319
321
                          self.fd_key: 1,
320
322
                          self.fe_key: 1,
321
323
                          self.ff_key: 1,
322
 
                          }, self.ann._num_needed_children)
 
324
                         }, self.ann._num_needed_children)
323
325
        self.assertTrue(self.fa_key in self.ann._text_cache)
324
326
        self.assertTrue(self.fa_key in self.ann._annotations_cache)
325
327
        self.ann._record_annotation(self.fc_key, [self.fa_key], [])
326
 
        self.ann._record_annotation(
327
 
            self.fd_key, [self.fb_key, self.fc_key], [])
 
328
        self.ann._record_annotation(self.fd_key, [self.fb_key, self.fc_key], [])
328
329
        self.assertEqual({self.fa_key: 1,
329
330
                          self.fb_key: 0,
330
331
                          self.fc_key: 0,
331
332
                          self.fd_key: 1,
332
333
                          self.fe_key: 1,
333
334
                          self.ff_key: 1,
334
 
                          }, self.ann._num_needed_children)
 
335
                         }, self.ann._num_needed_children)
335
336
        self.assertTrue(self.fa_key in self.ann._text_cache)
336
337
        self.assertTrue(self.fa_key in self.ann._annotations_cache)
337
338
        self.assertFalse(self.fb_key in self.ann._text_cache)
355
356
        #  |   E  'simple|new content|'
356
357
        #  |  /
357
358
        #  SPEC   'simple|new content|locally modified|'
358
 
        spec_key = (b'f-id', revision.CURRENT_REVISION)
359
 
        spec_text = b'simple\nnew content\nlocally modified\n'
 
359
        spec_key = ('f-id', revision.CURRENT_REVISION)
 
360
        spec_text = 'simple\nnew content\nlocally modified\n'
360
361
        self.ann.add_special_text(spec_key, [self.fd_key, self.fe_key],
361
362
                                  spec_text)
362
363
        self.assertAnnotateEqual([(self.fa_key,),
363
364
                                  (self.fb_key, self.fc_key, self.fe_key),
364
365
                                  (spec_key,),
365
 
                                  ], spec_key,
 
366
                                 ], spec_key,
366
367
                                 exp_text=spec_text)
367
368
 
368
369
    def test_no_graph(self):
369
370
        self.make_no_graph_texts()
370
371
        self.assertAnnotateEqual([(self.fa_key,),
371
372
                                  (self.fa_key,),
372
 
                                  ], self.fa_key)
 
373
                                 ], self.fa_key)
373
374
        self.assertAnnotateEqual([(self.fb_key,),
374
375
                                  (self.fb_key,),
375
 
                                  ], self.fb_key)
 
376
                                 ], self.fb_key)