~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_annotate.py

  • Committer: Martin Pool
  • Date: 2010-01-29 10:36:23 UTC
  • mto: This revision was merged to the branch mainline in revision 4992.
  • Revision ID: mbp@sourcefrog.net-20100129103623-hywka5hymo5z13jw
Change url to canonical.com or wiki, plus some doc improvements in passing

Show diffs side-by-side

added added

removed removed

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