1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Whitebox tests for annotate functionality."""
20
from cStringIO import StringIO
32
return [tuple(l.split(' ', 1)) for l in text.splitlines(True)]
35
parent_1 = annotation("""\
44
parent_2 = annotation("""\
54
expected_2_1 = annotation("""\
63
# a: in both, same value, kept
65
# c: in both, same value, kept
66
# d: in both, same value, kept
67
# e: 1 and 2 disagree, so it goes to blahblah
68
# f: in 2, but not in new, so ignored
69
# g: not in 1 or 2, so it goes to blahblah
70
# h: only in parent 2, so 2 gets it
71
expected_1_2_2 = annotation("""\
90
expected_1 = annotation("""\
110
class TestAnnotate(tests.TestCaseWithTransport):
112
def create_merged_trees(self):
113
"""create 2 trees with merges between them.
124
tree1 = self.make_branch_and_tree('tree1')
125
self.build_tree_contents([('tree1/a', 'first\n')])
126
tree1.add(['a'], ['a-id'])
127
tree1.commit('a', rev_id='rev-1',
128
committer="joe@foo.com",
129
timestamp=1166046000.00, timezone=0)
131
tree2 = tree1.bzrdir.clone('tree2').open_workingtree()
133
self.build_tree_contents([('tree1/a', 'first\nsecond\n')])
134
tree1.commit('b', rev_id='rev-2',
135
committer='joe@foo.com',
136
timestamp=1166046001.00, timezone=0)
138
self.build_tree_contents([('tree2/a', 'first\nthird\n')])
139
tree2.commit('c', rev_id='rev-1_1_1',
140
committer="barry@foo.com",
141
timestamp=1166046002.00, timezone=0)
143
num_conflicts = tree1.merge_from_branch(tree2.branch)
144
self.assertEqual(1, num_conflicts)
146
self.build_tree_contents([('tree1/a',
147
'first\nsecond\nthird\n')])
148
tree1.set_conflicts(conflicts.ConflictList())
149
tree1.commit('merge 2', rev_id='rev-3',
150
committer='sal@foo.com',
151
timestamp=1166046003.00, timezone=0)
153
self.addCleanup(tree1.unlock)
156
def create_deeply_merged_trees(self):
157
"""Create some trees with a more complex merge history.
165
rev-3 rev-1_1_2 rev-1_1_1_1_1 --+
169
rev-4 rev-1_1_1_1_2 rev-1_1_1_1_1_1_1
171
+-----------------+ |
175
+--------------------------------+
179
tree1, tree2 = self.create_merged_trees()
182
tree3 = tree2.bzrdir.clone('tree3').open_workingtree()
184
tree2.commit('noop', rev_id='rev-1_1_2')
185
self.assertEqual(0, tree1.merge_from_branch(tree2.branch))
186
tree1.commit('noop merge', rev_id='rev-4')
188
self.build_tree_contents([('tree3/a', 'first\nthird\nfourth\n')])
189
tree3.commit('four', rev_id='rev-1_1_1_1_1',
190
committer='jerry@foo.com',
191
timestamp=1166046003.00, timezone=0)
193
tree4 = tree3.bzrdir.clone('tree4').open_workingtree()
195
tree3.commit('noop', rev_id='rev-1_1_1_1_2',
196
committer='jerry@foo.com',
197
timestamp=1166046004.00, timezone=0)
198
self.assertEqual(0, tree1.merge_from_branch(tree3.branch))
199
tree1.commit('merge four', rev_id='rev-5')
201
self.build_tree_contents([('tree4/a',
202
'first\nthird\nfourth\nfifth\nsixth\n')])
203
tree4.commit('five and six', rev_id='rev-1_1_1_1_1_1_1',
204
committer='george@foo.com',
205
timestamp=1166046005.00, timezone=0)
206
self.assertEqual(0, tree1.merge_from_branch(tree4.branch))
207
tree1.commit('merge five and six', rev_id='rev-6')
211
def test_annotate_shows_dotted_revnos(self):
212
tree1, tree2 = self.create_merged_trees()
215
annotate.annotate_file(tree1.branch, 'rev-3', 'a-id',
217
self.assertEqualDiff('1 joe@foo | first\n'
218
'2 joe@foo | second\n'
219
'1.1.1 barry@f | third\n',
222
def test_annotate_limits_dotted_revnos(self):
223
"""Annotate should limit dotted revnos to a depth of 12"""
224
tree1 = self.create_deeply_merged_trees()
227
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
228
to_file=sio, verbose=False, full=False)
229
self.assertEqualDiff('1 joe@foo | first\n'
230
'2 joe@foo | second\n'
231
'1.1.1 barry@f | third\n'
232
'1.1.1.1.1 jerry@f | fourth\n'
233
'1.1.1.1.1.1> george@ | fifth\n'
238
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
239
to_file=sio, verbose=False, full=True)
240
self.assertEqualDiff('1 joe@foo | first\n'
241
'2 joe@foo | second\n'
242
'1.1.1 barry@f | third\n'
243
'1.1.1.1.1 jerry@f | fourth\n'
244
'1.1.1.1.1.1> george@ | fifth\n'
245
'1.1.1.1.1.1> george@ | sixth\n',
248
# verbose=True shows everything, the full revno, user id, and date
250
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
251
to_file=sio, verbose=True, full=False)
252
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
253
'2 joe@foo.com 20061213 | second\n'
254
'1.1.1 barry@foo.com 20061213 | third\n'
255
'1.1.1.1.1 jerry@foo.com 20061213 | fourth\n'
256
'1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
261
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
262
to_file=sio, verbose=True, full=True)
263
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
264
'2 joe@foo.com 20061213 | second\n'
265
'1.1.1 barry@foo.com 20061213 | third\n'
266
'1.1.1.1.1 jerry@foo.com 20061213 | fourth\n'
267
'1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
268
'1.1.1.1.1.1.1 george@foo.com 20061213 | sixth\n',
271
def test_annotate_uses_branch_context(self):
272
"""Dotted revnos should use the Branch context.
274
When annotating a non-mainline revision, the annotation should still
275
use dotted revnos from the mainline.
277
tree1 = self.create_deeply_merged_trees()
280
annotate.annotate_file(tree1.branch, 'rev-1_1_1_1_1_1_1', 'a-id',
281
to_file=sio, verbose=False, full=False)
282
self.assertEqualDiff('1 joe@foo | first\n'
283
'1.1.1 barry@f | third\n'
284
'1.1.1.1.1 jerry@f | fourth\n'
285
'1.1.1.1.1.1> george@ | fifth\n'
289
def test_annotate_show_ids(self):
290
tree1 = self.create_deeply_merged_trees()
293
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
294
to_file=sio, show_ids=True, full=False)
296
# It looks better with real revision ids :)
297
self.assertEqualDiff(' rev-1 | first\n'
299
' rev-1_1_1 | third\n'
300
' rev-1_1_1_1_1 | fourth\n'
301
'rev-1_1_1_1_1_1_1 | fifth\n'
306
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
307
to_file=sio, show_ids=True, full=True)
309
self.assertEqualDiff(' rev-1 | first\n'
311
' rev-1_1_1 | third\n'
312
' rev-1_1_1_1_1 | fourth\n'
313
'rev-1_1_1_1_1_1_1 | fifth\n'
314
'rev-1_1_1_1_1_1_1 | sixth\n',
317
def test_annotate_unicode_author(self):
318
tree1 = self.make_branch_and_tree('tree1')
320
self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
321
tree1.add(['a'], ['a-id'])
322
tree1.commit('a', rev_id='rev-1',
323
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
324
timestamp=1166046000.00, timezone=0)
326
self.build_tree_contents([('tree1/b', 'bye')])
327
tree1.add(['b'], ['b-id'])
328
tree1.commit('b', rev_id='rev-2',
329
committer=u'p\xe9rez',
330
timestamp=1166046000.00, timezone=0)
333
self.addCleanup(tree1.unlock)
334
# this passes if no exception is raised
336
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
339
to_file = codecs.getwriter('ascii')(sio)
340
to_file.encoding = 'ascii' # codecs does not set it
341
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
342
self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue())
344
# test now with to_file.encoding = None
345
to_file = tests.StringIOWrapper()
346
to_file.encoding = None
347
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
348
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
350
# and when it does not exist
352
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
353
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
355
def test_annotate_author_or_committer(self):
356
tree1 = self.make_branch_and_tree('tree1')
358
self.build_tree_contents([('tree1/a', 'hello')])
359
tree1.add(['a'], ['a-id'])
360
tree1.commit('a', rev_id='rev-1',
361
committer='Committer <committer@example.com>',
362
timestamp=1166046000.00, timezone=0)
364
self.build_tree_contents([('tree1/b', 'bye')])
365
tree1.add(['b'], ['b-id'])
366
tree1.commit('b', rev_id='rev-2',
367
committer='Committer <committer@example.com>',
368
author='Author <author@example.com>',
369
timestamp=1166046000.00, timezone=0)
372
self.addCleanup(tree1.unlock)
374
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
375
self.assertEqual('1 committ | hello\n', to_file.getvalue())
378
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
379
self.assertEqual('2 author@ | bye\n', to_file.getvalue())
382
class TestReannotate(tests.TestCase):
384
def annotateEqual(self, expected, parents, newlines, revision_id,
386
annotate_list = list(annotate.reannotate(parents, newlines,
387
revision_id, blocks))
388
self.assertEqual(len(expected), len(annotate_list))
389
for e, a in zip(expected, annotate_list):
390
self.assertEqual(e, a)
392
def test_reannotate(self):
393
self.annotateEqual(parent_1, [parent_1], new_1, 'blahblah')
394
self.annotateEqual(expected_2_1, [parent_2], new_1, 'blahblah')
395
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
398
def test_reannotate_no_parents(self):
399
self.annotateEqual(expected_1, [], new_1, 'blahblah')
401
def test_reannotate_left_matching_blocks(self):
402
"""Ensure that left_matching_blocks has an impact.
404
In this case, the annotation is ambiguous, so the hint isn't actually
407
parent = [('rev1', 'a\n')]
408
new_text = ['a\n', 'a\n']
409
blocks = [(0, 0, 1), (1, 2, 0)]
410
self.annotateEqual([('rev1', 'a\n'), ('rev2', 'a\n')], [parent],
411
new_text, 'rev2', blocks)
412
blocks = [(0, 1, 1), (1, 2, 0)]
413
self.annotateEqual([('rev2', 'a\n'), ('rev1', 'a\n')], [parent],
414
new_text, 'rev2', blocks)