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)
154
def create_deeply_merged_trees(self):
155
"""Create some trees with a more complex merge history.
163
rev-3 rev-1_1_2 rev-1_1_1_1_1 --+
167
rev-4 rev-1_1_1_1_2 rev-1_1_1_1_1_1_1
169
+-----------------+ |
173
+--------------------------------+
177
tree1, tree2 = self.create_merged_trees()
179
tree3 = tree2.bzrdir.clone('tree3').open_workingtree()
181
tree2.commit('noop', rev_id='rev-1_1_2')
182
self.assertEqual(0, tree1.merge_from_branch(tree2.branch))
183
tree1.commit('noop merge', rev_id='rev-4')
185
self.build_tree_contents([('tree3/a', 'first\nthird\nfourth\n')])
186
tree3.commit('four', rev_id='rev-1_1_1_1_1',
187
committer='jerry@foo.com',
188
timestamp=1166046003.00, timezone=0)
190
tree4 = tree3.bzrdir.clone('tree4').open_workingtree()
192
tree3.commit('noop', rev_id='rev-1_1_1_1_2',
193
committer='jerry@foo.com',
194
timestamp=1166046004.00, timezone=0)
195
self.assertEqual(0, tree1.merge_from_branch(tree3.branch))
196
tree1.commit('merge four', rev_id='rev-5')
198
self.build_tree_contents([('tree4/a',
199
'first\nthird\nfourth\nfifth\nsixth\n')])
200
tree4.commit('five and six', rev_id='rev-1_1_1_1_1_1_1',
201
committer='george@foo.com',
202
timestamp=1166046005.00, timezone=0)
203
self.assertEqual(0, tree1.merge_from_branch(tree4.branch))
204
tree1.commit('merge five and six', rev_id='rev-6')
207
def test_annotate_shows_dotted_revnos(self):
208
tree1, tree2 = self.create_merged_trees()
211
annotate.annotate_file(tree1.branch, 'rev-3', 'a-id',
213
self.assertEqualDiff('1 joe@foo | first\n'
214
'2 joe@foo | second\n'
215
'1.1.1 barry@f | third\n',
218
def test_annotate_limits_dotted_revnos(self):
219
"""Annotate should limit dotted revnos to a depth of 12"""
220
tree1 = self.create_deeply_merged_trees()
223
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
224
to_file=sio, verbose=False, full=False)
225
self.assertEqualDiff('1 joe@foo | first\n'
226
'2 joe@foo | second\n'
227
'1.1.1 barry@f | third\n'
228
'1.1.1.1.1 jerry@f | fourth\n'
229
'1.1.1.1.1.1> george@ | fifth\n'
234
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
235
to_file=sio, verbose=False, full=True)
236
self.assertEqualDiff('1 joe@foo | first\n'
237
'2 joe@foo | second\n'
238
'1.1.1 barry@f | third\n'
239
'1.1.1.1.1 jerry@f | fourth\n'
240
'1.1.1.1.1.1> george@ | fifth\n'
241
'1.1.1.1.1.1> george@ | sixth\n',
244
# verbose=True shows everything, the full revno, user id, and date
246
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
247
to_file=sio, verbose=True, full=False)
248
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
249
'2 joe@foo.com 20061213 | second\n'
250
'1.1.1 barry@foo.com 20061213 | third\n'
251
'1.1.1.1.1 jerry@foo.com 20061213 | fourth\n'
252
'1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
257
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
258
to_file=sio, verbose=True, full=True)
259
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
260
'2 joe@foo.com 20061213 | second\n'
261
'1.1.1 barry@foo.com 20061213 | third\n'
262
'1.1.1.1.1 jerry@foo.com 20061213 | fourth\n'
263
'1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
264
'1.1.1.1.1.1.1 george@foo.com 20061213 | sixth\n',
267
def test_annotate_uses_branch_context(self):
268
"""Dotted revnos should use the Branch context.
270
When annotating a non-mainline revision, the annotation should still
271
use dotted revnos from the mainline.
273
tree1 = self.create_deeply_merged_trees()
276
annotate.annotate_file(tree1.branch, 'rev-1_1_1_1_1_1_1', 'a-id',
277
to_file=sio, verbose=False, full=False)
278
self.assertEqualDiff('1 joe@foo | first\n'
279
'1.1.1 barry@f | third\n'
280
'1.1.1.1.1 jerry@f | fourth\n'
281
'1.1.1.1.1.1> george@ | fifth\n'
285
def test_annotate_show_ids(self):
286
tree1 = self.create_deeply_merged_trees()
289
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
290
to_file=sio, show_ids=True, full=False)
292
# It looks better with real revision ids :)
293
self.assertEqualDiff(' rev-1 | first\n'
295
' rev-1_1_1 | third\n'
296
' rev-1_1_1_1_1 | fourth\n'
297
'rev-1_1_1_1_1_1_1 | fifth\n'
302
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
303
to_file=sio, show_ids=True, full=True)
305
self.assertEqualDiff(' rev-1 | first\n'
307
' rev-1_1_1 | third\n'
308
' rev-1_1_1_1_1 | fourth\n'
309
'rev-1_1_1_1_1_1_1 | fifth\n'
310
'rev-1_1_1_1_1_1_1 | sixth\n',
313
def test_annotate_unicode_author(self):
314
tree1 = self.make_branch_and_tree('tree1')
316
self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
317
tree1.add(['a'], ['a-id'])
318
tree1.commit('a', rev_id='rev-1',
319
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
320
timestamp=1166046000.00, timezone=0)
322
self.build_tree_contents([('tree1/b', 'bye')])
323
tree1.add(['b'], ['b-id'])
324
tree1.commit('b', rev_id='rev-2',
325
committer=u'p\xe9rez',
326
timestamp=1166046000.00, timezone=0)
328
# this passes if no exception is raised
330
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
333
to_file = codecs.getwriter('ascii')(sio)
334
to_file.encoding = 'ascii' # codecs does not set it
335
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
336
self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue())
338
# test now with to_file.encoding = None
339
to_file = tests.StringIOWrapper()
340
to_file.encoding = None
341
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
342
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
344
# and when it does not exist
346
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
347
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
349
def test_annotate_author_or_committer(self):
350
tree1 = self.make_branch_and_tree('tree1')
352
self.build_tree_contents([('tree1/a', 'hello')])
353
tree1.add(['a'], ['a-id'])
354
tree1.commit('a', rev_id='rev-1',
355
committer='Committer <committer@example.com>',
356
timestamp=1166046000.00, timezone=0)
358
self.build_tree_contents([('tree1/b', 'bye')])
359
tree1.add(['b'], ['b-id'])
360
tree1.commit('b', rev_id='rev-2',
361
committer='Committer <committer@example.com>',
362
author='Author <author@example.com>',
363
timestamp=1166046000.00, timezone=0)
366
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
367
self.assertEqual('1 committ | hello\n', to_file.getvalue())
370
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
371
self.assertEqual('2 author@ | bye\n', to_file.getvalue())
374
class TestReannotate(tests.TestCase):
376
def annotateEqual(self, expected, parents, newlines, revision_id,
378
annotate_list = list(annotate.reannotate(parents, newlines,
379
revision_id, blocks))
380
self.assertEqual(len(expected), len(annotate_list))
381
for e, a in zip(expected, annotate_list):
382
self.assertEqual(e, a)
384
def test_reannotate(self):
385
self.annotateEqual(parent_1, [parent_1], new_1, 'blahblah')
386
self.annotateEqual(expected_2_1, [parent_2], new_1, 'blahblah')
387
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
390
def test_reannotate_no_parents(self):
391
self.annotateEqual(expected_1, [], new_1, 'blahblah')
393
def test_reannotate_left_matching_blocks(self):
394
"""Ensure that left_matching_blocks has an impact.
396
In this case, the annotation is ambiguous, so the hint isn't actually
399
parent = [('rev1', 'a\n')]
400
new_text = ['a\n', 'a\n']
401
blocks = [(0, 0, 1), (1, 2, 0)]
402
self.annotateEqual([('rev1', 'a\n'), ('rev2', 'a\n')], [parent],
403
new_text, 'rev2', blocks)
404
blocks = [(0, 1, 1), (1, 2, 0)]
405
self.annotateEqual([('rev2', 'a\n'), ('rev1', 'a\n')], [parent],
406
new_text, 'rev2', blocks)