/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 breezy/tests/test__annotator.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009, 2010, 2011 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 bzrlib import (
 
19
from .. import (
20
20
    annotate,
21
 
    _annotator_py,
22
21
    errors,
23
 
    knit,
24
22
    revision,
25
23
    tests,
26
24
    )
27
25
 
28
 
 
29
 
def load_tests(standard_tests, module, loader):
 
26
from ..bzr import (
 
27
    knit,
 
28
    )
 
29
 
 
30
 
 
31
def load_tests(loader, standard_tests, pattern):
30
32
    """Parameterize tests for all versions of groupcompress."""
31
33
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
32
 
        'bzrlib._annotator_py', 'bzrlib._annotator_pyx')
 
34
        'breezy._annotator_py', 'breezy._annotator_pyx')
33
35
    return suite
34
36
 
35
37
 
37
39
 
38
40
    module = None # Set by load_tests
39
41
 
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')
 
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')
46
48
 
47
49
    def make_no_graph_texts(self):
48
50
        factory = knit.make_pack_factory(False, False, 2)
49
51
        self.vf = factory(self.get_transport())
50
52
        self.ann = self.module.Annotator(self.vf)
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'])
 
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'])
53
55
 
54
56
    def make_simple_text(self):
55
57
        # TODO: all we really need is a VersionedFile instance, we'd like to
62
64
        #  A    'simple|content|'
63
65
        #  |
64
66
        #  B    'simple|new content|'
65
 
        self.vf.add_lines(self.fa_key, [], ['simple\n', 'content\n'])
 
67
        self.vf.add_lines(self.fa_key, [], [b'simple\n', b'content\n'])
66
68
        self.vf.add_lines(self.fb_key, [self.fa_key],
67
 
                          ['simple\n', 'new content\n'])
 
69
                          [b'simple\n', b'new content\n'])
68
70
 
69
71
    def make_merge_text(self):
70
72
        self.make_simple_text()
76
78
        #  |/
77
79
        #  D    'simple|from c|new content|introduced in merge|'
78
80
        self.vf.add_lines(self.fc_key, [self.fa_key],
79
 
                          ['simple\n', 'from c\n', 'content\n'])
 
81
                          [b'simple\n', b'from c\n', b'content\n'])
80
82
        self.vf.add_lines(self.fd_key, [self.fb_key, self.fc_key],
81
 
                          ['simple\n', 'from c\n', 'new content\n',
82
 
                           'introduced in merge\n'])
 
83
                          [b'simple\n', b'from c\n', b'new content\n',
 
84
                           b'introduced in merge\n'])
83
85
 
84
86
    def make_common_merge_text(self):
85
87
        """Both sides of the merge will have introduced a line."""
92
94
        #  |/
93
95
        #  D    'simple|new content|'
94
96
        self.vf.add_lines(self.fc_key, [self.fa_key],
95
 
                          ['simple\n', 'new content\n'])
 
97
                          [b'simple\n', b'new content\n'])
96
98
        self.vf.add_lines(self.fd_key, [self.fb_key, self.fc_key],
97
 
                          ['simple\n', 'new content\n'])
 
99
                          [b'simple\n', b'new content\n'])
98
100
 
99
101
    def make_many_way_common_merge_text(self):
100
102
        self.make_simple_text()
110
112
        #  |  /
111
113
        #  F-'    'simple|new content|'
112
114
        self.vf.add_lines(self.fc_key, [self.fa_key],
113
 
                          ['simple\n', 'new content\n'])
 
115
                          [b'simple\n', b'new content\n'])
114
116
        self.vf.add_lines(self.fd_key, [self.fb_key, self.fc_key],
115
 
                          ['simple\n', 'new content\n'])
 
117
                          [b'simple\n', b'new content\n'])
116
118
        self.vf.add_lines(self.fe_key, [self.fa_key],
117
 
                          ['simple\n', 'new content\n'])
 
119
                          [b'simple\n', b'new content\n'])
118
120
        self.vf.add_lines(self.ff_key, [self.fd_key, self.fe_key],
119
 
                          ['simple\n', 'new content\n'])
 
121
                          [b'simple\n', b'new content\n'])
120
122
 
121
123
    def make_merge_and_restored_text(self):
122
124
        self.make_simple_text()
129
131
        #    D  'simple|content|'
130
132
        # c reverts back to 'a' for the new content line
131
133
        self.vf.add_lines(self.fc_key, [self.fb_key],
132
 
                          ['simple\n', 'content\n'])
 
134
                          [b'simple\n', b'content\n'])
133
135
        # d merges 'a' and 'c', to find both claim last modified
134
136
        self.vf.add_lines(self.fd_key, [self.fa_key, self.fc_key],
135
 
                          ['simple\n', 'content\n'])
 
137
                          [b'simple\n', b'content\n'])
136
138
 
137
139
    def assertAnnotateEqual(self, expected_annotation, key, exp_text=None):
138
140
        annotation, lines = self.ann.annotate(key)
139
141
        self.assertEqual(expected_annotation, annotation)
140
142
        if exp_text is None:
141
 
            record = self.vf.get_record_stream([key], 'unordered', True).next()
 
143
            record = next(self.vf.get_record_stream([key], 'unordered', True))
142
144
            exp_text = record.get_bytes_as('fulltext')
143
 
        self.assertEqualDiff(exp_text, ''.join(lines))
 
145
        self.assertEqualDiff(exp_text, b''.join(lines))
144
146
 
145
147
    def test_annotate_missing(self):
146
148
        self.make_simple_text()
147
149
        self.assertRaises(errors.RevisionNotPresent,
148
 
                          self.ann.annotate, ('not', 'present'))
 
150
                          self.ann.annotate, (b'not', b'present'))
149
151
 
150
152
    def test_annotate_simple(self):
151
153
        self.make_simple_text()
176
178
 
177
179
    def test_annotate_flat_simple(self):
178
180
        self.make_simple_text()
179
 
        self.assertEqual([(self.fa_key, 'simple\n'),
180
 
                          (self.fa_key, 'content\n'),
 
181
        self.assertEqual([(self.fa_key, b'simple\n'),
 
182
                          (self.fa_key, b'content\n'),
181
183
                         ], self.ann.annotate_flat(self.fa_key))
182
 
        self.assertEqual([(self.fa_key, 'simple\n'),
183
 
                          (self.fb_key, 'new content\n'),
 
184
        self.assertEqual([(self.fa_key, b'simple\n'),
 
185
                          (self.fb_key, b'new content\n'),
184
186
                         ], self.ann.annotate_flat(self.fb_key))
185
187
 
186
188
    def test_annotate_flat_merge_and_restored_text(self):
187
189
        self.make_merge_and_restored_text()
188
190
        # fc is a simple dominator of fa
189
 
        self.assertEqual([(self.fa_key, 'simple\n'),
190
 
                          (self.fc_key, 'content\n'),
 
191
        self.assertEqual([(self.fa_key, b'simple\n'),
 
192
                          (self.fc_key, b'content\n'),
191
193
                         ], self.ann.annotate_flat(self.fd_key))
192
194
 
193
195
    def test_annotate_common_merge_text(self):
194
196
        self.make_common_merge_text()
195
197
        # there is no common point, so we just pick the lexicographical lowest
196
 
        # and 'b-id' comes before 'c-id'
197
 
        self.assertEqual([(self.fa_key, 'simple\n'),
198
 
                          (self.fb_key, 'new content\n'),
 
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'),
199
201
                         ], self.ann.annotate_flat(self.fd_key))
200
202
 
201
203
    def test_annotate_many_way_common_merge_text(self):
202
204
        self.make_many_way_common_merge_text()
203
 
        self.assertEqual([(self.fa_key, 'simple\n'),
204
 
                         (self.fb_key, 'new content\n')],
 
205
        self.assertEqual([(self.fa_key, b'simple\n'),
 
206
                         (self.fb_key, b'new content\n')],
205
207
                         self.ann.annotate_flat(self.ff_key))
206
208
 
207
209
    def test_annotate_flat_respects_break_ann_tie(self):
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
235
 
 
 
210
        seen = set()
 
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(b'new content\n', left[1])
 
216
            right = annotated_lines[1]
 
217
            self.assertEqual(2, len(right))
 
218
            self.assertEqual(b'new content\n', right[1])
 
219
            seen.update([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
        self.overrideAttr(annotate, '_break_annotation_tie', custom_tiebreaker)
 
227
        self.make_many_way_common_merge_text()
 
228
        self.assertEqual([(self.fa_key, b'simple\n'),
 
229
                         (self.fe_key, b'new content\n')],
 
230
                         self.ann.annotate_flat(self.ff_key))
 
231
        # Calls happen in set iteration order but should keys should be seen
 
232
        self.assertEqual({self.fb_key, self.fc_key, self.fe_key}, seen)
236
233
 
237
234
    def test_needed_keys_simple(self):
238
235
        self.make_simple_text()
259
256
 
260
257
    def test_needed_keys_with_special_text(self):
261
258
        self.make_many_way_common_merge_text()
262
 
        spec_key = ('f-id', revision.CURRENT_REVISION)
263
 
        spec_text = 'simple\nnew content\nlocally modified\n'
 
259
        spec_key = (b'f-id', revision.CURRENT_REVISION)
 
260
        spec_text = b'simple\nnew content\nlocally modified\n'
264
261
        self.ann.add_special_text(spec_key, [self.fd_key, self.fe_key],
265
262
                                  spec_text)
266
263
        keys, ann_keys = self.ann._get_needed_keys(spec_key)
279
276
        #  |  /
280
277
        #  F-'    'simple|new content|'
281
278
        self.ann._parent_map[self.fd_key] = (self.fb_key, self.fc_key)
282
 
        self.ann._text_cache[self.fd_key] = ['simple\n', 'new content\n']
 
279
        self.ann._text_cache[self.fd_key] = [b'simple\n', b'new content\n']
283
280
        self.ann._annotations_cache[self.fd_key] = [
284
281
            (self.fa_key,),
285
282
            (self.fb_key, self.fc_key),
286
283
            ]
287
284
        self.ann._parent_map[self.fe_key] = (self.fa_key,)
288
 
        self.ann._text_cache[self.fe_key] = ['simple\n', 'new content\n']
 
285
        self.ann._text_cache[self.fe_key] = [b'simple\n', b'new content\n']
289
286
        self.ann._annotations_cache[self.fe_key] = [
290
287
            (self.fa_key,),
291
288
            (self.fe_key,),
356
353
        #  |   E  'simple|new content|'
357
354
        #  |  /
358
355
        #  SPEC   'simple|new content|locally modified|'
359
 
        spec_key = ('f-id', revision.CURRENT_REVISION)
360
 
        spec_text = 'simple\nnew content\nlocally modified\n'
 
356
        spec_key = (b'f-id', revision.CURRENT_REVISION)
 
357
        spec_text = b'simple\nnew content\nlocally modified\n'
361
358
        self.ann.add_special_text(spec_key, [self.fd_key, self.fe_key],
362
359
                                  spec_text)
363
360
        self.assertAnnotateEqual([(self.fa_key,),