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("""\
 
 
102
class TestAnnotate(tests.TestCaseWithTransport):
 
 
104
    def create_merged_trees(self):
 
 
105
        """create 2 trees with merges between them.
 
 
116
        tree1 = self.make_branch_and_tree('tree1')
 
 
117
        self.build_tree_contents([('tree1/a', 'first\n')])
 
 
118
        tree1.add(['a'], ['a-id'])
 
 
119
        tree1.commit('a', rev_id='rev-1',
 
 
120
                     committer="joe@foo.com",
 
 
121
                     timestamp=1166046000.00, timezone=0)
 
 
123
        tree2 = tree1.bzrdir.clone('tree2').open_workingtree()
 
 
125
        self.build_tree_contents([('tree1/a', 'first\nsecond\n')])
 
 
126
        tree1.commit('b', rev_id='rev-2',
 
 
127
                     committer='joe@foo.com',
 
 
128
                     timestamp=1166046001.00, timezone=0)
 
 
130
        self.build_tree_contents([('tree2/a', 'first\nthird\n')])
 
 
131
        tree2.commit('c', rev_id='rev-1_1_1',
 
 
132
                     committer="barry@foo.com",
 
 
133
                     timestamp=1166046002.00, timezone=0)
 
 
135
        num_conflicts = tree1.merge_from_branch(tree2.branch)
 
 
136
        self.assertEqual(1, num_conflicts)
 
 
138
        self.build_tree_contents([('tree1/a',
 
 
139
                                 'first\nsecond\nthird\n')])
 
 
140
        tree1.set_conflicts(conflicts.ConflictList())
 
 
141
        tree1.commit('merge 2', rev_id='rev-3',
 
 
142
                     committer='sal@foo.com',
 
 
143
                     timestamp=1166046003.00, timezone=0)
 
 
146
    def create_deeply_merged_trees(self):
 
 
147
        """Create some trees with a more complex merge history.
 
 
155
        rev-3  rev-1_1_2  rev-1_1_1_1_1 --+
 
 
159
        rev-4             rev-1_1_1_1_2  rev-1_1_1_1_1_1_1
 
 
161
         +-----------------+              |
 
 
165
         +--------------------------------+
 
 
169
        tree1, tree2 = self.create_merged_trees()
 
 
171
        tree3 = tree2.bzrdir.clone('tree3').open_workingtree()
 
 
173
        tree2.commit('noop', rev_id='rev-1_1_2')
 
 
174
        self.assertEqual(0, tree1.merge_from_branch(tree2.branch))
 
 
175
        tree1.commit('noop merge', rev_id='rev-4')
 
 
177
        self.build_tree_contents([('tree3/a', 'first\nthird\nfourth\n')])
 
 
178
        tree3.commit('four', rev_id='rev-1_1_1_1_1',
 
 
179
                     committer='jerry@foo.com',
 
 
180
                     timestamp=1166046003.00, timezone=0)
 
 
182
        tree4 = tree3.bzrdir.clone('tree4').open_workingtree()
 
 
184
        tree3.commit('noop', rev_id='rev-1_1_1_1_2',
 
 
185
                     committer='jerry@foo.com',
 
 
186
                     timestamp=1166046004.00, timezone=0)
 
 
187
        self.assertEqual(0, tree1.merge_from_branch(tree3.branch))
 
 
188
        tree1.commit('merge four', rev_id='rev-5')
 
 
190
        self.build_tree_contents([('tree4/a',
 
 
191
                                   'first\nthird\nfourth\nfifth\nsixth\n')])
 
 
192
        tree4.commit('five and six', rev_id='rev-1_1_1_1_1_1_1',
 
 
193
                     committer='george@foo.com',
 
 
194
                     timestamp=1166046005.00, timezone=0)
 
 
195
        self.assertEqual(0, tree1.merge_from_branch(tree4.branch))
 
 
196
        tree1.commit('merge five and six', rev_id='rev-6')
 
 
199
    def test_annotate_shows_dotted_revnos(self):
 
 
200
        tree1, tree2 = self.create_merged_trees()
 
 
203
        annotate.annotate_file(tree1.branch, 'rev-3', 'a-id',
 
 
205
        self.assertEqualDiff('1     joe@foo | first\n'
 
 
206
                             '2     joe@foo | second\n'
 
 
207
                             '1.1.1 barry@f | third\n',
 
 
210
    def test_annotate_limits_dotted_revnos(self):
 
 
211
        """Annotate should limit dotted revnos to a depth of 12"""
 
 
212
        tree1 = self.create_deeply_merged_trees()
 
 
215
        annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
 
 
216
                               to_file=sio, verbose=False, full=False)
 
 
217
        self.assertEqualDiff('1            joe@foo | first\n'
 
 
218
                             '2            joe@foo | second\n'
 
 
219
                             '1.1.1        barry@f | third\n'
 
 
220
                             '1.1.1.1.1    jerry@f | fourth\n'
 
 
221
                             '1.1.1.1.1.1> george@ | fifth\n'
 
 
226
        annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
 
 
227
                               to_file=sio, verbose=False, full=True)
 
 
228
        self.assertEqualDiff('1            joe@foo | first\n'
 
 
229
                             '2            joe@foo | second\n'
 
 
230
                             '1.1.1        barry@f | third\n'
 
 
231
                             '1.1.1.1.1    jerry@f | fourth\n'
 
 
232
                             '1.1.1.1.1.1> george@ | fifth\n'
 
 
233
                             '1.1.1.1.1.1> george@ | sixth\n',
 
 
236
        # verbose=True shows everything, the full revno, user id, and date
 
 
238
        annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
 
 
239
                               to_file=sio, verbose=True, full=False)
 
 
240
        self.assertEqualDiff('1             joe@foo.com    20061213 | first\n'
 
 
241
                             '2             joe@foo.com    20061213 | second\n'
 
 
242
                             '1.1.1         barry@foo.com  20061213 | third\n'
 
 
243
                             '1.1.1.1.1     jerry@foo.com  20061213 | fourth\n'
 
 
244
                             '1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
 
 
249
        annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
 
 
250
                               to_file=sio, verbose=True, full=True)
 
 
251
        self.assertEqualDiff('1             joe@foo.com    20061213 | first\n'
 
 
252
                             '2             joe@foo.com    20061213 | second\n'
 
 
253
                             '1.1.1         barry@foo.com  20061213 | third\n'
 
 
254
                             '1.1.1.1.1     jerry@foo.com  20061213 | fourth\n'
 
 
255
                             '1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
 
 
256
                             '1.1.1.1.1.1.1 george@foo.com 20061213 | sixth\n',
 
 
259
    def test_annotate_uses_branch_context(self):
 
 
260
        """Dotted revnos should use the Branch context.
 
 
262
        When annotating a non-mainline revision, the annotation should still
 
 
263
        use dotted revnos from the mainline.
 
 
265
        tree1 = self.create_deeply_merged_trees()
 
 
268
        annotate.annotate_file(tree1.branch, 'rev-1_1_1_1_1_1_1', 'a-id',
 
 
269
                               to_file=sio, verbose=False, full=False)
 
 
270
        self.assertEqualDiff('1            joe@foo | first\n'
 
 
271
                             '1.1.1        barry@f | third\n'
 
 
272
                             '1.1.1.1.1    jerry@f | fourth\n'
 
 
273
                             '1.1.1.1.1.1> george@ | fifth\n'
 
 
277
    def test_annotate_show_ids(self):
 
 
278
        tree1 = self.create_deeply_merged_trees()
 
 
281
        annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
 
 
282
                               to_file=sio, show_ids=True, full=False)
 
 
284
        # It looks better with real revision ids :)
 
 
285
        self.assertEqualDiff('            rev-1 | first\n'
 
 
287
                             '        rev-1_1_1 | third\n'
 
 
288
                             '    rev-1_1_1_1_1 | fourth\n'
 
 
289
                             'rev-1_1_1_1_1_1_1 | fifth\n'
 
 
294
        annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
 
 
295
                               to_file=sio, show_ids=True, full=True)
 
 
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'
 
 
302
                             'rev-1_1_1_1_1_1_1 | sixth\n',
 
 
305
    def test_annotate_unicode_author(self):
 
 
306
        tree1 = self.make_branch_and_tree('tree1')
 
 
308
        self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
 
 
309
        tree1.add(['a'], ['a-id'])
 
 
310
        tree1.commit('a', rev_id='rev-1',
 
 
311
                     committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
 
 
312
                     timestamp=1166046000.00, timezone=0)
 
 
314
        self.build_tree_contents([('tree1/b', 'bye')])
 
 
315
        tree1.add(['b'], ['b-id'])
 
 
316
        tree1.commit('b', rev_id='rev-2',
 
 
317
                     committer=u'p\xe9rez',
 
 
318
                     timestamp=1166046000.00, timezone=0)
 
 
320
        # this passes if no exception is raised
 
 
322
        annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
 
 
325
        to_file = codecs.getwriter('ascii')(sio)
 
 
326
        to_file.encoding = 'ascii' # codecs does not set it
 
 
327
        annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
 
 
328
        self.assertEqualDiff('2   p?rez   | bye\n', sio.getvalue())
 
 
330
        # test now with to_file.encoding = None
 
 
331
        to_file = tests.StringIOWrapper()
 
 
332
        to_file.encoding = None
 
 
333
        annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
 
 
334
        self.assertContainsRe('2   p.rez   | bye\n', to_file.getvalue())
 
 
336
        # and when it does not exist
 
 
338
        annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
 
 
339
        self.assertContainsRe('2   p.rez   | bye\n', to_file.getvalue())
 
 
342
class TestReannotate(tests.TestCase):
 
 
344
    def annotateEqual(self, expected, parents, newlines, revision_id):
 
 
345
        annotate_list = list(annotate.reannotate(parents, newlines,
 
 
347
        self.assertEqual(len(expected), len(annotate_list))
 
 
348
        for e, a in zip(expected, annotate_list):
 
 
349
            self.assertEqual(e, a)
 
 
351
    def test_reannotate(self):
 
 
352
        self.annotateEqual(parent_1, [parent_1], new_1, 'blahblah')
 
 
353
        self.annotateEqual(expected_2_1, [parent_2], new_1, 'blahblah')
 
 
354
        self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,