/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_annotate.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2009, 2011 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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Whitebox tests for annotate functionality."""
 
18
 
 
19
import codecs
 
20
from io import BytesIO
 
21
 
 
22
from .. import (
 
23
    annotate,
 
24
    tests,
 
25
    )
 
26
from ..sixish import (
 
27
    StringIO,
 
28
    )
 
29
from .ui_testing import StringIOWithEncoding
 
30
 
 
31
 
 
32
def annotation(text):
 
33
    return [tuple(l.split(b' ', 1)) for l in text.splitlines(True)]
 
34
 
 
35
 
 
36
parent_1 = annotation(b"""\
 
37
rev1 a
 
38
rev2 b
 
39
rev3 c
 
40
rev4 d
 
41
rev5 e
 
42
""")
 
43
 
 
44
 
 
45
parent_2 = annotation(b"""\
 
46
rev1 a
 
47
rev3 c
 
48
rev4 d
 
49
rev6 f
 
50
rev7 e
 
51
rev8 h
 
52
""")
 
53
 
 
54
 
 
55
expected_2_1 = annotation(b"""\
 
56
rev1 a
 
57
blahblah b
 
58
rev3 c
 
59
rev4 d
 
60
rev7 e
 
61
""")
 
62
 
 
63
 
 
64
# a: in both, same value, kept
 
65
# b: in 1, kept
 
66
# c: in both, same value, kept
 
67
# d: in both, same value, kept
 
68
# e: 1 and 2 disagree, so it goes to blahblah
 
69
# f: in 2, but not in new, so ignored
 
70
# g: not in 1 or 2, so it goes to blahblah
 
71
# h: only in parent 2, so 2 gets it
 
72
expected_1_2_2 = annotation(b"""\
 
73
rev1 a
 
74
rev2 b
 
75
rev3 c
 
76
rev4 d
 
77
blahblah e
 
78
blahblah g
 
79
rev8 h
 
80
""")
 
81
 
 
82
 
 
83
new_1 = b"""\
 
84
a
 
85
b
 
86
c
 
87
d
 
88
e
 
89
""".splitlines(True)
 
90
 
 
91
expected_1 = annotation(b"""\
 
92
blahblah a
 
93
blahblah b
 
94
blahblah c
 
95
blahblah d
 
96
blahblah e
 
97
""")
 
98
 
 
99
 
 
100
new_2 = b"""\
 
101
a
 
102
b
 
103
c
 
104
d
 
105
e
 
106
g
 
107
h
 
108
""".splitlines(True)
 
109
 
 
110
 
 
111
# For the 'duplicate' series, both sides introduce the same change, which then
 
112
# gets merged around. The last-modified should properly reflect this.
 
113
# We always change the fourth line so that the file is properly tracked as
 
114
# being modified in each revision. In reality, this probably would happen over
 
115
# many revisions, and it would be a different line that changes.
 
116
# BASE
 
117
#  |\
 
118
#  A B  # line should be annotated as new for A and B
 
119
#  |\|
 
120
#  C D  # line should 'converge' and say A
 
121
#  |/
 
122
#  E    # D should supersede A and stay as D (not become E because C references
 
123
#         A)
 
124
duplicate_base = annotation(b"""\
 
125
rev-base first
 
126
rev-base second
 
127
rev-base third
 
128
rev-base fourth-base
 
129
""")
 
130
 
 
131
duplicate_A = annotation(b"""\
 
132
rev-base first
 
133
rev-A alt-second
 
134
rev-base third
 
135
rev-A fourth-A
 
136
""")
 
137
 
 
138
duplicate_B = annotation(b"""\
 
139
rev-base first
 
140
rev-B alt-second
 
141
rev-base third
 
142
rev-B fourth-B
 
143
""")
 
144
 
 
145
duplicate_C = annotation(b"""\
 
146
rev-base first
 
147
rev-A alt-second
 
148
rev-base third
 
149
rev-C fourth-C
 
150
""")
 
151
 
 
152
duplicate_D = annotation(b"""\
 
153
rev-base first
 
154
rev-A alt-second
 
155
rev-base third
 
156
rev-D fourth-D
 
157
""")
 
158
 
 
159
duplicate_E = annotation(b"""\
 
160
rev-base first
 
161
rev-A alt-second
 
162
rev-base third
 
163
rev-E fourth-E
 
164
""")
 
165
 
 
166
 
 
167
class TestAnnotate(tests.TestCaseWithTransport):
 
168
 
 
169
    def create_merged_trees(self):
 
170
        """create 2 trees with merges between them.
 
171
 
 
172
        rev-1 --+
 
173
         |      |
 
174
        rev-2  rev-1_1_1
 
175
         |      |
 
176
         +------+
 
177
         |
 
178
        rev-3
 
179
        """
 
180
        builder = self.make_branch_builder('branch')
 
181
        builder.start_series()
 
182
        self.addCleanup(builder.finish_series)
 
183
        builder.build_snapshot(None, [
 
184
            ('add', ('', b'root-id', 'directory', None)),
 
185
            ('add', ('a', b'a-id', 'file', b'first\n')),
 
186
            ], timestamp=1166046000.00, timezone=0, committer="joe@foo.com",
 
187
            revision_id=b'rev-1')
 
188
        builder.build_snapshot([b'rev-1'], [
 
189
            ('modify', ('a', b'first\nsecond\n')),
 
190
            ], timestamp=1166046001.00, timezone=0, committer="joe@foo.com",
 
191
            revision_id=b'rev-2')
 
192
        builder.build_snapshot([b'rev-1'], [
 
193
            ('modify', ('a', b'first\nthird\n')),
 
194
            ], timestamp=1166046002.00, timezone=0, committer="barry@foo.com",
 
195
            revision_id=b'rev-1_1_1')
 
196
        builder.build_snapshot([b'rev-2', b'rev-1_1_1'], [
 
197
            ('modify', ('a', b'first\nsecond\nthird\n')),
 
198
            ], timestamp=1166046003.00, timezone=0, committer="sal@foo.com",
 
199
            revision_id=b'rev-3')
 
200
        return builder
 
201
 
 
202
    def create_deeply_merged_trees(self):
 
203
        """Create some trees with a more complex merge history.
 
204
 
 
205
        rev-1 --+
 
206
         |      |
 
207
        rev-2  rev-1_1_1 --+
 
208
         |      |          |
 
209
         +------+          |
 
210
         |      |          |
 
211
        rev-3  rev-1_1_2  rev-1_2_1 ------+
 
212
         |      |          |              |
 
213
         +------+          |              |
 
214
         |                 |              |
 
215
        rev-4             rev-1_2_2  rev-1_3_1
 
216
         |                 |              |
 
217
         +-----------------+              |
 
218
         |                                |
 
219
        rev-5                             |
 
220
         |                                |
 
221
         +--------------------------------+
 
222
         |
 
223
        rev-6
 
224
        """
 
225
        builder = self.create_merged_trees()
 
226
        builder.build_snapshot([b'rev-1_1_1'], [], revision_id=b'rev-1_1_2')
 
227
        builder.build_snapshot([b'rev-3', b'rev-1_1_2'],
 
228
                               [], revision_id=b'rev-4')
 
229
        builder.build_snapshot([b'rev-1_1_1'], [
 
230
            ('modify', ('a', b'first\nthird\nfourth\n')),
 
231
            ], timestamp=1166046003.00, timezone=0, committer="jerry@foo.com",
 
232
            revision_id=b'rev-1_2_1')
 
233
        builder.build_snapshot([b'rev-1_2_1'], [],
 
234
                               timestamp=1166046004.00, timezone=0, committer="jerry@foo.com",
 
235
                               revision_id=b'rev-1_2_2')
 
236
        builder.build_snapshot([b'rev-4', b'rev-1_2_2'], [
 
237
            ('modify', ('a', b'first\nsecond\nthird\nfourth\n')),
 
238
            ], timestamp=1166046004.00, timezone=0, committer="jerry@foo.com",
 
239
            revision_id=b'rev-5')
 
240
        builder.build_snapshot([b'rev-1_2_1'], [
 
241
            ('modify', ('a', b'first\nthird\nfourth\nfifth\nsixth\n')),
 
242
            ], timestamp=1166046005.00, timezone=0, committer="george@foo.com",
 
243
            revision_id=b'rev-1_3_1')
 
244
        builder.build_snapshot([b'rev-5', b'rev-1_3_1'], [
 
245
            ('modify', ('a',
 
246
                        b'first\nsecond\nthird\nfourth\nfifth\nsixth\n')),
 
247
            ], revision_id=b'rev-6')
 
248
        return builder
 
249
 
 
250
    def create_duplicate_lines_tree(self):
 
251
        builder = self.make_branch_builder('branch')
 
252
        builder.start_series()
 
253
        self.addCleanup(builder.finish_series)
 
254
        base_text = b''.join(l for r, l in duplicate_base)
 
255
        a_text = b''.join(l for r, l in duplicate_A)
 
256
        b_text = b''.join(l for r, l in duplicate_B)
 
257
        c_text = b''.join(l for r, l in duplicate_C)
 
258
        d_text = b''.join(l for r, l in duplicate_D)
 
259
        e_text = b''.join(l for r, l in duplicate_E)
 
260
        builder.build_snapshot(None, [
 
261
            ('add', ('', b'root-id', 'directory', None)),
 
262
            ('add', ('file', b'file-id', 'file', base_text)),
 
263
            ], revision_id=b'rev-base')
 
264
        builder.build_snapshot([b'rev-base'], [
 
265
            ('modify', ('file', a_text))],
 
266
            revision_id=b'rev-A')
 
267
        builder.build_snapshot([b'rev-base'], [
 
268
            ('modify', ('file', b_text))],
 
269
            revision_id=b'rev-B')
 
270
        builder.build_snapshot([b'rev-A'], [
 
271
            ('modify', ('file', c_text))],
 
272
            revision_id=b'rev-C')
 
273
        builder.build_snapshot([b'rev-B', b'rev-A'], [
 
274
            ('modify', ('file', d_text))],
 
275
            revision_id=b'rev-D')
 
276
        builder.build_snapshot([b'rev-C', b'rev-D'], [
 
277
            ('modify', ('file', e_text))],
 
278
            revision_id=b'rev-E')
 
279
        return builder
 
280
 
 
281
    def assertAnnotateEqualDiff(self, actual, expected):
 
282
        if actual != expected:
 
283
            # Create an easier to understand diff when the lines don't actually
 
284
            # match
 
285
            self.assertEqualDiff(''.join('\t'.join(l) for l in expected),
 
286
                                 ''.join('\t'.join(l) for l in actual))
 
287
 
 
288
    def assertBranchAnnotate(self, expected, branch, path, revision_id,
 
289
                             verbose=False, full=False, show_ids=False):
 
290
        tree = branch.repository.revision_tree(revision_id)
 
291
        to_file = StringIO()
 
292
        annotate.annotate_file_tree(tree, path, to_file,
 
293
                                    verbose=verbose, full=full, show_ids=show_ids, branch=branch)
 
294
        self.assertAnnotateEqualDiff(to_file.getvalue(), expected)
 
295
 
 
296
    def assertRepoAnnotate(self, expected, repo, path, revision_id):
 
297
        """Assert that the revision is properly annotated."""
 
298
        actual = list(repo.revision_tree(revision_id).annotate_iter(path))
 
299
        self.assertAnnotateEqualDiff(actual, expected)
 
300
 
 
301
    def test_annotate_duplicate_lines(self):
 
302
        # XXX: Should this be a per_repository test?
 
303
        builder = self.create_duplicate_lines_tree()
 
304
        repo = builder.get_branch().repository
 
305
        repo.lock_read()
 
306
        self.addCleanup(repo.unlock)
 
307
        self.assertRepoAnnotate(duplicate_base, repo, 'file', b'rev-base')
 
308
        self.assertRepoAnnotate(duplicate_A, repo, 'file', b'rev-A')
 
309
        self.assertRepoAnnotate(duplicate_B, repo, 'file', b'rev-B')
 
310
        self.assertRepoAnnotate(duplicate_C, repo, 'file', b'rev-C')
 
311
        self.assertRepoAnnotate(duplicate_D, repo, 'file', b'rev-D')
 
312
        self.assertRepoAnnotate(duplicate_E, repo, 'file', b'rev-E')
 
313
 
 
314
    def test_annotate_shows_dotted_revnos(self):
 
315
        builder = self.create_merged_trees()
 
316
 
 
317
        self.assertBranchAnnotate('1     joe@foo | first\n'
 
318
                                  '2     joe@foo | second\n'
 
319
                                  '1.1.1 barry@f | third\n',
 
320
                                  builder.get_branch(), 'a', b'rev-3')
 
321
 
 
322
    def test_annotate_limits_dotted_revnos(self):
 
323
        """Annotate should limit dotted revnos to a depth of 12"""
 
324
        builder = self.create_deeply_merged_trees()
 
325
 
 
326
        self.assertBranchAnnotate('1     joe@foo | first\n'
 
327
                                  '2     joe@foo | second\n'
 
328
                                  '1.1.1 barry@f | third\n'
 
329
                                  '1.2.1 jerry@f | fourth\n'
 
330
                                  '1.3.1 george@ | fifth\n'
 
331
                                  '              | sixth\n',
 
332
                                  builder.get_branch(), 'a', b'rev-6',
 
333
                                  verbose=False, full=False)
 
334
 
 
335
        self.assertBranchAnnotate('1     joe@foo | first\n'
 
336
                                  '2     joe@foo | second\n'
 
337
                                  '1.1.1 barry@f | third\n'
 
338
                                  '1.2.1 jerry@f | fourth\n'
 
339
                                  '1.3.1 george@ | fifth\n'
 
340
                                  '1.3.1 george@ | sixth\n',
 
341
                                  builder.get_branch(), 'a', b'rev-6',
 
342
                                  verbose=False, full=True)
 
343
 
 
344
        # verbose=True shows everything, the full revno, user id, and date
 
345
        self.assertBranchAnnotate('1     joe@foo.com    20061213 | first\n'
 
346
                                  '2     joe@foo.com    20061213 | second\n'
 
347
                                  '1.1.1 barry@foo.com  20061213 | third\n'
 
348
                                  '1.2.1 jerry@foo.com  20061213 | fourth\n'
 
349
                                  '1.3.1 george@foo.com 20061213 | fifth\n'
 
350
                                  '                              | sixth\n',
 
351
                                  builder.get_branch(), 'a', b'rev-6',
 
352
                                  verbose=True, full=False)
 
353
 
 
354
        self.assertBranchAnnotate('1     joe@foo.com    20061213 | first\n'
 
355
                                  '2     joe@foo.com    20061213 | second\n'
 
356
                                  '1.1.1 barry@foo.com  20061213 | third\n'
 
357
                                  '1.2.1 jerry@foo.com  20061213 | fourth\n'
 
358
                                  '1.3.1 george@foo.com 20061213 | fifth\n'
 
359
                                  '1.3.1 george@foo.com 20061213 | sixth\n',
 
360
                                  builder.get_branch(), 'a', b'rev-6',
 
361
                                  verbose=True, full=True)
 
362
 
 
363
    def test_annotate_uses_branch_context(self):
 
364
        """Dotted revnos should use the Branch context.
 
365
 
 
366
        When annotating a non-mainline revision, the annotation should still
 
367
        use dotted revnos from the mainline.
 
368
        """
 
369
        builder = self.create_deeply_merged_trees()
 
370
 
 
371
        self.assertBranchAnnotate('1     joe@foo | first\n'
 
372
                                  '1.1.1 barry@f | third\n'
 
373
                                  '1.2.1 jerry@f | fourth\n'
 
374
                                  '1.3.1 george@ | fifth\n'
 
375
                                  '              | sixth\n',
 
376
                                  builder.get_branch(), 'a', b'rev-1_3_1',
 
377
                                  verbose=False, full=False)
 
378
 
 
379
    def test_annotate_show_ids(self):
 
380
        builder = self.create_deeply_merged_trees()
 
381
 
 
382
        # It looks better with real revision ids :)
 
383
        self.assertBranchAnnotate('    rev-1 | first\n'
 
384
                                  '    rev-2 | second\n'
 
385
                                  'rev-1_1_1 | third\n'
 
386
                                  'rev-1_2_1 | fourth\n'
 
387
                                  'rev-1_3_1 | fifth\n'
 
388
                                  '          | sixth\n',
 
389
                                  builder.get_branch(), 'a', b'rev-6',
 
390
                                  show_ids=True, full=False)
 
391
 
 
392
        self.assertBranchAnnotate('    rev-1 | first\n'
 
393
                                  '    rev-2 | second\n'
 
394
                                  'rev-1_1_1 | third\n'
 
395
                                  'rev-1_2_1 | fourth\n'
 
396
                                  'rev-1_3_1 | fifth\n'
 
397
                                  'rev-1_3_1 | sixth\n',
 
398
                                  builder.get_branch(), 'a', b'rev-6',
 
399
                                  show_ids=True, full=True)
 
400
 
 
401
    def test_annotate_unicode_author(self):
 
402
        tree1 = self.make_branch_and_tree('tree1')
 
403
 
 
404
        self.build_tree_contents([('tree1/a', b'adi\xc3\xb3s')])
 
405
        tree1.add(['a'], [b'a-id'])
 
406
        tree1.commit('a', rev_id=b'rev-1',
 
407
                     committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
 
408
                     timestamp=1166046000.00, timezone=0)
 
409
 
 
410
        self.build_tree_contents([('tree1/b', b'bye')])
 
411
        tree1.add(['b'], [b'b-id'])
 
412
        tree1.commit('b', rev_id=b'rev-2',
 
413
                     committer=u'p\xe9rez',
 
414
                     timestamp=1166046000.00, timezone=0)
 
415
 
 
416
        tree1.lock_read()
 
417
        self.addCleanup(tree1.unlock)
 
418
 
 
419
        revtree_1 = tree1.branch.repository.revision_tree(b'rev-1')
 
420
        revtree_2 = tree1.branch.repository.revision_tree(b'rev-2')
 
421
 
 
422
        # this passes if no exception is raised
 
423
        to_file = StringIO()
 
424
        annotate.annotate_file_tree(revtree_1, 'a',
 
425
                                    to_file=to_file, branch=tree1.branch)
 
426
 
 
427
        sio = BytesIO()
 
428
        to_file = codecs.getwriter('ascii')(sio, 'replace')
 
429
        annotate.annotate_file_tree(revtree_2, 'b',
 
430
                                    to_file=to_file, branch=tree1.branch)
 
431
        self.assertEqualDiff(b'2   p?rez   | bye\n', sio.getvalue())
 
432
 
 
433
        # test now with unicode file-like
 
434
        to_file = StringIOWithEncoding()
 
435
        annotate.annotate_file_tree(revtree_2, 'b',
 
436
                                    to_file=to_file, branch=tree1.branch)
 
437
        self.assertContainsRe(u'2   p\xe9rez   | bye\n', to_file.getvalue())
 
438
 
 
439
    def test_annotate_author_or_committer(self):
 
440
        tree1 = self.make_branch_and_tree('tree1')
 
441
 
 
442
        self.build_tree_contents([('tree1/a', b'hello')])
 
443
        tree1.add(['a'], [b'a-id'])
 
444
        tree1.commit('a', rev_id=b'rev-1',
 
445
                     committer='Committer <committer@example.com>',
 
446
                     timestamp=1166046000.00, timezone=0)
 
447
 
 
448
        self.build_tree_contents([('tree1/b', b'bye')])
 
449
        tree1.add(['b'], [b'b-id'])
 
450
        tree1.commit('b', rev_id=b'rev-2',
 
451
                     committer='Committer <committer@example.com>',
 
452
                     authors=['Author <author@example.com>'],
 
453
                     timestamp=1166046000.00, timezone=0)
 
454
 
 
455
        tree1.lock_read()
 
456
        self.addCleanup(tree1.unlock)
 
457
 
 
458
        self.assertBranchAnnotate('1   committ | hello\n', tree1.branch,
 
459
                                  'a', b'rev-1')
 
460
 
 
461
        self.assertBranchAnnotate('2   author@ | bye\n', tree1.branch,
 
462
                                  'b', b'rev-2')
 
463
 
 
464
 
 
465
class TestReannotate(tests.TestCase):
 
466
 
 
467
    def annotateEqual(self, expected, parents, newlines, revision_id,
 
468
                      blocks=None):
 
469
        annotate_list = list(annotate.reannotate(parents, newlines,
 
470
                                                 revision_id, blocks))
 
471
        self.assertEqual(len(expected), len(annotate_list))
 
472
        for e, a in zip(expected, annotate_list):
 
473
            self.assertEqual(e, a)
 
474
 
 
475
    def test_reannotate(self):
 
476
        self.annotateEqual(parent_1, [parent_1], new_1, b'blahblah')
 
477
        self.annotateEqual(expected_2_1, [parent_2], new_1, b'blahblah')
 
478
        self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
 
479
                           b'blahblah')
 
480
 
 
481
    def test_reannotate_no_parents(self):
 
482
        self.annotateEqual(expected_1, [], new_1, b'blahblah')
 
483
 
 
484
    def test_reannotate_left_matching_blocks(self):
 
485
        """Ensure that left_matching_blocks has an impact.
 
486
 
 
487
        In this case, the annotation is ambiguous, so the hint isn't actually
 
488
        lying.
 
489
        """
 
490
        parent = [(b'rev1', b'a\n')]
 
491
        new_text = [b'a\n', b'a\n']
 
492
        blocks = [(0, 0, 1), (1, 2, 0)]
 
493
        self.annotateEqual([(b'rev1', b'a\n'), (b'rev2', b'a\n')], [parent],
 
494
                           new_text, b'rev2', blocks)
 
495
        blocks = [(0, 1, 1), (1, 2, 0)]
 
496
        self.annotateEqual([(b'rev2', b'a\n'), (b'rev1', b'a\n')], [parent],
 
497
                           new_text, b'rev2', blocks)