1
# Copyright (C) 2006-2009, 2011 Canonical Ltd
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.
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.
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
17
"""Whitebox tests for annotate functionality."""
20
from cStringIO import StringIO
29
return [tuple(l.split(' ', 1)) for l in text.splitlines(True)]
32
parent_1 = annotation("""\
41
parent_2 = annotation("""\
51
expected_2_1 = annotation("""\
60
# a: in both, same value, kept
62
# c: in both, same value, kept
63
# d: in both, same value, kept
64
# e: 1 and 2 disagree, so it goes to blahblah
65
# f: in 2, but not in new, so ignored
66
# g: not in 1 or 2, so it goes to blahblah
67
# h: only in parent 2, so 2 gets it
68
expected_1_2_2 = annotation("""\
87
expected_1 = annotation("""\
107
# For the 'duplicate' series, both sides introduce the same change, which then
108
# gets merged around. The last-modified should properly reflect this.
109
# We always change the fourth line so that the file is properly tracked as
110
# being modified in each revision. In reality, this probably would happen over
111
# many revisions, and it would be a different line that changes.
114
# A B # line should be annotated as new for A and B
116
# C D # line should 'converge' and say A
118
# E # D should supersede A and stay as D (not become E because C references
120
duplicate_base = annotation("""\
127
duplicate_A = annotation("""\
134
duplicate_B = annotation("""\
141
duplicate_C = annotation("""\
148
duplicate_D = annotation("""\
155
duplicate_E = annotation("""\
163
class TestAnnotate(tests.TestCaseWithTransport):
165
def create_merged_trees(self):
166
"""create 2 trees with merges between them.
176
builder = self.make_branch_builder('branch')
177
builder.start_series()
178
self.addCleanup(builder.finish_series)
179
builder.build_snapshot('rev-1', None, [
180
('add', ('', 'root-id', 'directory', None)),
181
('add', ('a', 'a-id', 'file', 'first\n')),
182
], timestamp=1166046000.00, timezone=0, committer="joe@foo.com")
183
builder.build_snapshot('rev-2', ['rev-1'], [
184
('modify', ('a-id', 'first\nsecond\n')),
185
], timestamp=1166046001.00, timezone=0, committer="joe@foo.com")
186
builder.build_snapshot('rev-1_1_1', ['rev-1'], [
187
('modify', ('a-id', 'first\nthird\n')),
188
], timestamp=1166046002.00, timezone=0, committer="barry@foo.com")
189
builder.build_snapshot('rev-3', ['rev-2', 'rev-1_1_1'], [
190
('modify', ('a-id', 'first\nsecond\nthird\n')),
191
], timestamp=1166046003.00, timezone=0, committer="sal@foo.com")
194
def create_deeply_merged_trees(self):
195
"""Create some trees with a more complex merge history.
203
rev-3 rev-1_1_2 rev-1_2_1 ------+
207
rev-4 rev-1_2_2 rev-1_3_1
209
+-----------------+ |
213
+--------------------------------+
217
builder = self.create_merged_trees()
218
builder.build_snapshot('rev-1_1_2', ['rev-1_1_1'], [])
219
builder.build_snapshot('rev-4', ['rev-3', 'rev-1_1_2'], [])
220
builder.build_snapshot('rev-1_2_1', ['rev-1_1_1'], [
221
('modify', ('a-id', 'first\nthird\nfourth\n')),
222
], timestamp=1166046003.00, timezone=0, committer="jerry@foo.com")
223
builder.build_snapshot('rev-1_2_2', ['rev-1_2_1'], [],
224
timestamp=1166046004.00, timezone=0, committer="jerry@foo.com")
225
builder.build_snapshot('rev-5', ['rev-4', 'rev-1_2_2'], [
226
('modify', ('a-id', 'first\nsecond\nthird\nfourth\n')),
227
], timestamp=1166046004.00, timezone=0, committer="jerry@foo.com")
228
builder.build_snapshot('rev-1_3_1', ['rev-1_2_1'], [
229
('modify', ('a-id', 'first\nthird\nfourth\nfifth\nsixth\n')),
230
], timestamp=1166046005.00, timezone=0, committer="george@foo.com")
231
builder.build_snapshot('rev-6', ['rev-5', 'rev-1_3_1'], [
233
'first\nsecond\nthird\nfourth\nfifth\nsixth\n')),
237
def create_duplicate_lines_tree(self):
238
builder = self.make_branch_builder('branch')
239
builder.start_series()
240
self.addCleanup(builder.finish_series)
241
base_text = ''.join(l for r, l in duplicate_base)
242
a_text = ''.join(l for r, l in duplicate_A)
243
b_text = ''.join(l for r, l in duplicate_B)
244
c_text = ''.join(l for r, l in duplicate_C)
245
d_text = ''.join(l for r, l in duplicate_D)
246
e_text = ''.join(l for r, l in duplicate_E)
247
builder.build_snapshot('rev-base', None, [
248
('add', ('', 'root-id', 'directory', None)),
249
('add', ('file', 'file-id', 'file', base_text)),
251
builder.build_snapshot('rev-A', ['rev-base'], [
252
('modify', ('file-id', a_text))])
253
builder.build_snapshot('rev-B', ['rev-base'], [
254
('modify', ('file-id', b_text))])
255
builder.build_snapshot('rev-C', ['rev-A'], [
256
('modify', ('file-id', c_text))])
257
builder.build_snapshot('rev-D', ['rev-B', 'rev-A'], [
258
('modify', ('file-id', d_text))])
259
builder.build_snapshot('rev-E', ['rev-C', 'rev-D'], [
260
('modify', ('file-id', e_text))])
263
def assertRepoAnnotate(self, expected, repo, file_id, revision_id):
264
"""Assert that the revision is properly annotated."""
265
actual = list(repo.revision_tree(revision_id).annotate_iter(file_id))
266
if actual != expected:
267
# Create an easier to understand diff when the lines don't actually
269
self.assertEqualDiff(''.join('\t'.join(l) for l in expected),
270
''.join('\t'.join(l) for l in actual))
272
def test_annotate_duplicate_lines(self):
273
# XXX: Should this be a per_repository test?
274
builder = self.create_duplicate_lines_tree()
275
repo = builder.get_branch().repository
277
self.addCleanup(repo.unlock)
278
self.assertRepoAnnotate(duplicate_base, repo, 'file-id', 'rev-base')
279
self.assertRepoAnnotate(duplicate_A, repo, 'file-id', 'rev-A')
280
self.assertRepoAnnotate(duplicate_B, repo, 'file-id', 'rev-B')
281
self.assertRepoAnnotate(duplicate_C, repo, 'file-id', 'rev-C')
282
self.assertRepoAnnotate(duplicate_D, repo, 'file-id', 'rev-D')
283
self.assertRepoAnnotate(duplicate_E, repo, 'file-id', 'rev-E')
285
def test_annotate_shows_dotted_revnos(self):
286
builder = self.create_merged_trees()
289
annotate.annotate_file(builder.get_branch(), 'rev-3', 'a-id',
291
self.assertEqualDiff('1 joe@foo | first\n'
292
'2 joe@foo | second\n'
293
'1.1.1 barry@f | third\n',
296
def test_annotate_limits_dotted_revnos(self):
297
"""Annotate should limit dotted revnos to a depth of 12"""
298
builder = self.create_deeply_merged_trees()
301
annotate.annotate_file(builder.get_branch(), 'rev-6', 'a-id',
302
to_file=sio, verbose=False, full=False)
303
self.assertEqualDiff('1 joe@foo | first\n'
304
'2 joe@foo | second\n'
305
'1.1.1 barry@f | third\n'
306
'1.2.1 jerry@f | fourth\n'
307
'1.3.1 george@ | fifth\n'
312
annotate.annotate_file(builder.get_branch(), 'rev-6', 'a-id',
313
to_file=sio, verbose=False, full=True)
314
self.assertEqualDiff('1 joe@foo | first\n'
315
'2 joe@foo | second\n'
316
'1.1.1 barry@f | third\n'
317
'1.2.1 jerry@f | fourth\n'
318
'1.3.1 george@ | fifth\n'
319
'1.3.1 george@ | sixth\n',
322
# verbose=True shows everything, the full revno, user id, and date
324
annotate.annotate_file(builder.get_branch(), 'rev-6', 'a-id',
325
to_file=sio, verbose=True, full=False)
326
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
327
'2 joe@foo.com 20061213 | second\n'
328
'1.1.1 barry@foo.com 20061213 | third\n'
329
'1.2.1 jerry@foo.com 20061213 | fourth\n'
330
'1.3.1 george@foo.com 20061213 | fifth\n'
335
annotate.annotate_file(builder.get_branch(), 'rev-6', 'a-id',
336
to_file=sio, verbose=True, full=True)
337
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
338
'2 joe@foo.com 20061213 | second\n'
339
'1.1.1 barry@foo.com 20061213 | third\n'
340
'1.2.1 jerry@foo.com 20061213 | fourth\n'
341
'1.3.1 george@foo.com 20061213 | fifth\n'
342
'1.3.1 george@foo.com 20061213 | sixth\n',
345
def test_annotate_uses_branch_context(self):
346
"""Dotted revnos should use the Branch context.
348
When annotating a non-mainline revision, the annotation should still
349
use dotted revnos from the mainline.
351
builder = self.create_deeply_merged_trees()
354
annotate.annotate_file(builder.get_branch(), 'rev-1_3_1', 'a-id',
355
to_file=sio, verbose=False, full=False)
356
self.assertEqualDiff('1 joe@foo | first\n'
357
'1.1.1 barry@f | third\n'
358
'1.2.1 jerry@f | fourth\n'
359
'1.3.1 george@ | fifth\n'
363
def test_annotate_show_ids(self):
364
builder = self.create_deeply_merged_trees()
367
annotate.annotate_file(builder.get_branch(), 'rev-6', 'a-id',
368
to_file=sio, show_ids=True, full=False)
370
# It looks better with real revision ids :)
371
self.assertEqualDiff(' rev-1 | first\n'
373
'rev-1_1_1 | third\n'
374
'rev-1_2_1 | fourth\n'
375
'rev-1_3_1 | fifth\n'
380
annotate.annotate_file(builder.get_branch(), 'rev-6', 'a-id',
381
to_file=sio, show_ids=True, full=True)
383
self.assertEqualDiff(' rev-1 | first\n'
385
'rev-1_1_1 | third\n'
386
'rev-1_2_1 | fourth\n'
387
'rev-1_3_1 | fifth\n'
388
'rev-1_3_1 | sixth\n',
391
def test_annotate_unicode_author(self):
392
tree1 = self.make_branch_and_tree('tree1')
394
self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
395
tree1.add(['a'], ['a-id'])
396
tree1.commit('a', rev_id='rev-1',
397
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
398
timestamp=1166046000.00, timezone=0)
400
self.build_tree_contents([('tree1/b', 'bye')])
401
tree1.add(['b'], ['b-id'])
402
tree1.commit('b', rev_id='rev-2',
403
committer=u'p\xe9rez',
404
timestamp=1166046000.00, timezone=0)
407
self.addCleanup(tree1.unlock)
408
# this passes if no exception is raised
410
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
413
to_file = codecs.getwriter('ascii')(sio)
414
to_file.encoding = 'ascii' # codecs does not set it
415
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
416
self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue())
418
# test now with to_file.encoding = None
419
to_file = tests.StringIOWrapper()
420
to_file.encoding = None
421
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
422
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
424
# and when it does not exist
426
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
427
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
429
def test_annotate_author_or_committer(self):
430
tree1 = self.make_branch_and_tree('tree1')
432
self.build_tree_contents([('tree1/a', 'hello')])
433
tree1.add(['a'], ['a-id'])
434
tree1.commit('a', rev_id='rev-1',
435
committer='Committer <committer@example.com>',
436
timestamp=1166046000.00, timezone=0)
438
self.build_tree_contents([('tree1/b', 'bye')])
439
tree1.add(['b'], ['b-id'])
440
tree1.commit('b', rev_id='rev-2',
441
committer='Committer <committer@example.com>',
442
authors=['Author <author@example.com>'],
443
timestamp=1166046000.00, timezone=0)
446
self.addCleanup(tree1.unlock)
448
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
449
self.assertEqual('1 committ | hello\n', to_file.getvalue())
452
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
453
self.assertEqual('2 author@ | bye\n', to_file.getvalue())
456
class TestReannotate(tests.TestCase):
458
def annotateEqual(self, expected, parents, newlines, revision_id,
460
annotate_list = list(annotate.reannotate(parents, newlines,
461
revision_id, blocks))
462
self.assertEqual(len(expected), len(annotate_list))
463
for e, a in zip(expected, annotate_list):
464
self.assertEqual(e, a)
466
def test_reannotate(self):
467
self.annotateEqual(parent_1, [parent_1], new_1, 'blahblah')
468
self.annotateEqual(expected_2_1, [parent_2], new_1, 'blahblah')
469
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
472
def test_reannotate_no_parents(self):
473
self.annotateEqual(expected_1, [], new_1, 'blahblah')
475
def test_reannotate_left_matching_blocks(self):
476
"""Ensure that left_matching_blocks has an impact.
478
In this case, the annotation is ambiguous, so the hint isn't actually
481
parent = [('rev1', 'a\n')]
482
new_text = ['a\n', 'a\n']
483
blocks = [(0, 0, 1), (1, 2, 0)]
484
self.annotateEqual([('rev1', 'a\n'), ('rev2', 'a\n')], [parent],
485
new_text, 'rev2', blocks)
486
blocks = [(0, 1, 1), (1, 2, 0)]
487
self.annotateEqual([('rev2', 'a\n'), ('rev1', 'a\n')], [parent],
488
new_text, 'rev2', blocks)