1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Black-box tests for bzr log."""
23
from bzrlib import osutils
24
from bzrlib.tests.blackbox import ExternalBase
25
from bzrlib.tests import TestCaseInTempDir, TestCaseWithTransport
26
from bzrlib.tests.test_log import (
29
from bzrlib.tests import test_log
32
class TestCaseWithoutPropsHandler(ExternalBase,
33
test_log.TestCaseWithoutPropsHandler):
37
class TestLog(ExternalBase):
39
def _prepare(self, path='.', format=None):
40
tree = self.make_branch_and_tree(path, format=format)
42
[path + '/hello.txt', path + '/goodbye.txt', path + '/meep.txt'])
44
tree.commit(message='message1')
45
tree.add('goodbye.txt')
46
tree.commit(message='message2')
48
tree.commit(message='message3')
49
self.full_log = self.run_bzr(["log", path])[0]
52
def test_log_null_end_revspec(self):
54
self.assertTrue('revno: 1\n' in self.full_log)
55
self.assertTrue('revno: 2\n' in self.full_log)
56
self.assertTrue('revno: 3\n' in self.full_log)
57
self.assertTrue('message:\n message1\n' in self.full_log)
58
self.assertTrue('message:\n message2\n' in self.full_log)
59
self.assertTrue('message:\n message3\n' in self.full_log)
61
log = self.run_bzr("log -r 1..")[0]
62
self.assertEqualDiff(log, self.full_log)
64
def test_log_null_begin_revspec(self):
66
log = self.run_bzr("log -r ..3")[0]
67
self.assertEqualDiff(self.full_log, log)
69
def test_log_null_both_revspecs(self):
71
log = self.run_bzr("log -r ..")[0]
72
self.assertEqualDiff(self.full_log, log)
74
def test_log_zero_revspec(self):
76
self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
79
def test_log_zero_begin_revspec(self):
81
self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
84
def test_log_zero_end_revspec(self):
86
self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
89
def test_log_unsupported_timezone(self):
91
self.run_bzr_error('bzr: ERROR: Unsupported timezone format "foo", '
92
'options are "utc", "original", "local".',
93
['log', '--timezone', 'foo'])
95
def test_log_negative_begin_revspec_full_log(self):
97
log = self.run_bzr("log -r -3..")[0]
98
self.assertEqualDiff(self.full_log, log)
100
def test_log_negative_both_revspec_full_log(self):
102
log = self.run_bzr("log -r -3..-1")[0]
103
self.assertEqualDiff(self.full_log, log)
105
def test_log_negative_both_revspec_partial(self):
107
log = self.run_bzr("log -r -3..-2")[0]
108
self.assertTrue('revno: 1\n' in log)
109
self.assertTrue('revno: 2\n' in log)
110
self.assertTrue('revno: 3\n' not in log)
112
def test_log_negative_begin_revspec(self):
114
log = self.run_bzr("log -r -2..")[0]
115
self.assertTrue('revno: 1\n' not in log)
116
self.assertTrue('revno: 2\n' in log)
117
self.assertTrue('revno: 3\n' in log)
119
def test_log_positive_revspecs(self):
121
log = self.run_bzr("log -r 1..3")[0]
122
self.assertEqualDiff(self.full_log, log)
124
def test_log_reversed_revspecs(self):
126
self.run_bzr_error(('bzr: ERROR: Start revision must be older than '
127
'the end revision.\n',),
130
def test_log_revno_n_path(self):
131
self._prepare(path='branch1')
132
self._prepare(path='branch2')
133
log = self.run_bzr("log -r revno:2:branch1..revno:3:branch2",
135
log = self.run_bzr("log -r revno:1:branch2..revno:3:branch2")[0]
136
self.assertEqualDiff(self.full_log, log)
137
log = self.run_bzr("log -r revno:1:branch2")[0]
138
self.assertTrue('revno: 1\n' in log)
139
self.assertTrue('revno: 2\n' not in log)
140
self.assertTrue('branch nick: branch2\n' in log)
141
self.assertTrue('branch nick: branch1\n' not in log)
143
def test_log_nonexistent_revno(self):
145
(out, err) = self.run_bzr_error(args="log -r 1234",
146
error_regexes=["bzr: ERROR: Requested revision: '1234' "
147
"does not exist in branch:"])
149
def test_log_nonexistent_dotted_revno(self):
151
(out, err) = self.run_bzr_error(args="log -r 123.123",
152
error_regexes=["bzr: ERROR: Requested revision: '123.123' "
153
"does not exist in branch:"])
155
def test_log_change_revno(self):
157
expected_log = self.run_bzr("log -r 1")[0]
158
log = self.run_bzr("log -c 1")[0]
159
self.assertEqualDiff(expected_log, log)
161
def test_log_change_nonexistent_revno(self):
163
(out, err) = self.run_bzr_error(args="log -c 1234",
164
error_regexes=["bzr: ERROR: Requested revision: '1234' "
165
"does not exist in branch:"])
167
def test_log_change_nonexistent_dotted_revno(self):
169
(out, err) = self.run_bzr_error(args="log -c 123.123",
170
error_regexes=["bzr: ERROR: Requested revision: '123.123' "
171
"does not exist in branch:"])
173
def test_log_change_single_revno(self):
175
self.run_bzr_error('bzr: ERROR: Option --change does not'
176
' accept revision ranges',
177
['log', '--change', '2..3'])
179
def test_log_change_incompatible_with_revision(self):
181
self.run_bzr_error('bzr: ERROR: --revision and --change'
182
' are mutually exclusive',
183
['log', '--change', '2', '--revision', '3'])
185
def test_log_nonexistent_file(self):
186
# files that don't exist in either the basis tree or working tree
187
# should give an error
188
wt = self.make_branch_and_tree('.')
189
out, err = self.run_bzr('log does-not-exist', retcode=3)
190
self.assertContainsRe(
191
err, 'Path does not have any revision history: does-not-exist')
193
def test_log_with_tags(self):
194
tree = self._prepare(format='dirstate-tags')
196
branch.tags.set_tag('tag1', branch.get_rev_id(1))
197
branch.tags.set_tag('tag1.1', branch.get_rev_id(1))
198
branch.tags.set_tag('tag3', branch.last_revision())
200
log = self.run_bzr("log -r-1")[0]
201
self.assertTrue('tags: tag3' in log)
203
log = self.run_bzr("log -r1")[0]
204
# I guess that we can't know the order of tags in the output
205
# since dicts are unordered, need to check both possibilities
206
self.assertContainsRe(log, r'tags: (tag1, tag1\.1|tag1\.1, tag1)')
208
def test_merged_log_with_tags(self):
209
branch1_tree = self._prepare(path='branch1', format='dirstate-tags')
210
branch1 = branch1_tree.branch
211
branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
212
branch1_tree.commit(message='foobar', allow_pointless=True)
213
branch1.tags.set_tag('tag1', branch1.last_revision())
215
self.run_bzr('merge ../branch1') # tags don't propagate otherwise
216
branch2_tree.commit(message='merge branch 1')
217
log = self.run_bzr("log -r-1")[0]
218
self.assertContainsRe(log, r' tags: tag1')
219
log = self.run_bzr("log -r3.1.1")[0]
220
self.assertContainsRe(log, r'tags: tag1')
222
def test_log_limit(self):
223
tree = self.make_branch_and_tree('.')
224
# We want more commits than our batch size starts at
225
for pos in range(10):
226
tree.commit("%s" % pos)
227
log = self.run_bzr("log --limit 2")[0]
228
self.assertNotContainsRe(log, r'revno: 1\n')
229
self.assertNotContainsRe(log, r'revno: 2\n')
230
self.assertNotContainsRe(log, r'revno: 3\n')
231
self.assertNotContainsRe(log, r'revno: 4\n')
232
self.assertNotContainsRe(log, r'revno: 5\n')
233
self.assertNotContainsRe(log, r'revno: 6\n')
234
self.assertNotContainsRe(log, r'revno: 7\n')
235
self.assertNotContainsRe(log, r'revno: 8\n')
236
self.assertContainsRe(log, r'revno: 9\n')
237
self.assertContainsRe(log, r'revno: 10\n')
239
def test_log_limit_short(self):
241
log = self.run_bzr("log -l 2")[0]
242
self.assertNotContainsRe(log, r'revno: 1\n')
243
self.assertContainsRe(log, r'revno: 2\n')
244
self.assertContainsRe(log, r'revno: 3\n')
247
class TestLogVerbose(TestCaseWithTransport):
250
super(TestLogVerbose, self).setUp()
251
tree = self.make_branch_and_tree('.')
252
self.build_tree(['hello.txt'])
253
tree.add('hello.txt')
254
tree.commit(message='message1')
256
def assertUseShortDeltaFormat(self, cmd):
257
log = self.run_bzr(cmd)[0]
258
# Check that we use the short status format
259
self.assertContainsRe(log, '(?m)^A hello.txt$')
260
self.assertNotContainsRe(log, '(?m)^added:$')
262
def assertUseLongDeltaFormat(self, cmd):
263
log = self.run_bzr(cmd)[0]
264
# Check that we use the long status format
265
self.assertNotContainsRe(log, '(?m)^A hello.txt$')
266
self.assertContainsRe(log, '(?m)^added:$')
268
def test_log_short_verbose(self):
269
self.assertUseShortDeltaFormat(['log', '--short', '-v'])
271
def test_log_short_verbose_verbose(self):
272
self.assertUseLongDeltaFormat(['log', '--short', '-vv'])
274
def test_log_long_verbose(self):
275
# Check that we use the long status format, ignoring the verbosity
277
self.assertUseLongDeltaFormat(['log', '--long', '-v'])
279
def test_log_long_verbose_verbose(self):
280
# Check that we use the long status format, ignoring the verbosity
282
self.assertUseLongDeltaFormat(['log', '--long', '-vv'])
285
class TestLogMerges(TestCaseWithoutPropsHandler):
288
parent_tree = self.make_branch_and_tree('parent')
289
parent_tree.commit(message='first post', allow_pointless=True)
290
child_tree = parent_tree.bzrdir.sprout('child').open_workingtree()
291
child_tree.commit(message='branch 1', allow_pointless=True)
293
child_tree.bzrdir.sprout('smallerchild').open_workingtree()
294
smaller_tree.commit(message='branch 2', allow_pointless=True)
295
child_tree.merge_from_branch(smaller_tree.branch)
296
child_tree.commit(message='merge branch 2')
297
parent_tree.merge_from_branch(child_tree.branch)
298
parent_tree.commit(message='merge branch 1')
301
def test_merges_are_indented_by_level(self):
303
out,err = self.run_bzr('log')
304
self.assertEqual('', err)
305
log = normalize_log(out)
306
self.assertEqualDiff(log, """\
307
------------------------------------------------------------
309
committer: Lorem Ipsum <test@example.com>
314
------------------------------------------------------------
316
committer: Lorem Ipsum <test@example.com>
321
------------------------------------------------------------
323
committer: Lorem Ipsum <test@example.com>
324
branch nick: smallerchild
328
------------------------------------------------------------
330
committer: Lorem Ipsum <test@example.com>
335
------------------------------------------------------------
337
committer: Lorem Ipsum <test@example.com>
344
def test_merges_single_merge_rev(self):
346
out,err = self.run_bzr('log -r1.1.2')
347
self.assertEqual('', err)
348
log = normalize_log(out)
349
self.assertEqualDiff(log, """\
350
------------------------------------------------------------
352
committer: Lorem Ipsum <test@example.com>
357
------------------------------------------------------------
359
committer: Lorem Ipsum <test@example.com>
360
branch nick: smallerchild
366
def test_merges_partial_range(self):
368
out, err = self.run_bzr('log -r1.1.1..1.1.2')
369
self.assertEqual('', err)
370
log = normalize_log(out)
371
self.assertEqualDiff(log, """\
372
------------------------------------------------------------
374
committer: Lorem Ipsum <test@example.com>
379
------------------------------------------------------------
381
committer: Lorem Ipsum <test@example.com>
382
branch nick: smallerchild
386
------------------------------------------------------------
388
committer: Lorem Ipsum <test@example.com>
395
def test_merges_nonsupporting_formatter(self):
397
err_msg = 'Selected log formatter only supports mainline revisions.'
398
# The single revision case is tested in the core tests
399
# since all standard formatters support single merge revisions.
400
out,err = self.run_bzr('log --short -r1..1.1.2', retcode=3)
401
self.assertContainsRe(err, err_msg)
402
out,err = self.run_bzr('log --short -r1.1.1..1.1.2', retcode=3)
403
self.assertContainsRe(err, err_msg)
406
class TestLogEncodings(TestCaseInTempDir):
409
_message = u'Message with \xb5'
411
# Encodings which can encode mu
416
'cp437', # Common windows encoding
417
'cp1251', # Alexander Belchenko's windows encoding
418
'cp1258', # Common windows encoding
420
# Encodings which cannot encode mu
428
TestCaseInTempDir.setUp(self)
429
self.user_encoding = osutils._cached_user_encoding
432
osutils._cached_user_encoding = self.user_encoding
433
TestCaseInTempDir.tearDown(self)
435
def create_branch(self):
438
open('a', 'wb').write('some stuff\n')
440
bzr(['commit', '-m', self._message])
442
def try_encoding(self, encoding, fail=False):
445
self.assertRaises(UnicodeEncodeError,
446
self._mu.encode, encoding)
447
encoded_msg = self._message.encode(encoding, 'replace')
449
encoded_msg = self._message.encode(encoding)
451
old_encoding = osutils._cached_user_encoding
452
# This test requires that 'run_bzr' uses the current
453
# bzrlib, because we override user_encoding, and expect
456
osutils._cached_user_encoding = 'ascii'
457
# We should be able to handle any encoding
458
out, err = bzr('log', encoding=encoding)
460
# Make sure we wrote mu as we expected it to exist
461
self.assertNotEqual(-1, out.find(encoded_msg))
462
out_unicode = out.decode(encoding)
463
self.assertNotEqual(-1, out_unicode.find(self._message))
465
self.assertNotEqual(-1, out.find('Message with ?'))
467
osutils._cached_user_encoding = old_encoding
469
def test_log_handles_encoding(self):
472
for encoding in self.good_encodings:
473
self.try_encoding(encoding)
475
def test_log_handles_bad_encoding(self):
478
for encoding in self.bad_encodings:
479
self.try_encoding(encoding, fail=True)
481
def test_stdout_encoding(self):
483
osutils._cached_user_encoding = "cp1251"
486
self.build_tree(['a'])
488
bzr(['commit', '-m', u'\u0422\u0435\u0441\u0442'])
489
stdout, stderr = self.run_bzr('log', encoding='cp866')
491
message = stdout.splitlines()[-1]
493
# explanation of the check:
494
# u'\u0422\u0435\u0441\u0442' is word 'Test' in russian
495
# in cp866 encoding this is string '\x92\xa5\xe1\xe2'
496
# in cp1251 encoding this is string '\xd2\xe5\xf1\xf2'
497
# This test should check that output of log command
498
# encoded to sys.stdout.encoding
499
test_in_cp866 = '\x92\xa5\xe1\xe2'
500
test_in_cp1251 = '\xd2\xe5\xf1\xf2'
501
# Make sure the log string is encoded in cp866
502
self.assertEquals(test_in_cp866, message[2:])
503
# Make sure the cp1251 string is not found anywhere
504
self.assertEquals(-1, stdout.find(test_in_cp1251))
507
class TestLogFile(TestCaseWithTransport):
509
def test_log_local_branch_file(self):
510
"""We should be able to log files in local treeless branches"""
511
tree = self.make_branch_and_tree('tree')
512
self.build_tree(['tree/file'])
514
tree.commit('revision 1')
515
tree.bzrdir.destroy_workingtree()
516
self.run_bzr('log tree/file')
518
def test_log_file(self):
519
"""The log for a particular file should only list revs for that file"""
520
tree = self.make_branch_and_tree('parent')
521
self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
523
tree.commit('add file1')
525
tree.commit('add file2')
527
tree.commit('add file3')
528
child_tree = tree.bzrdir.sprout('child').open_workingtree()
529
self.build_tree_contents([('child/file2', 'hello')])
530
child_tree.commit(message='branch 1')
531
tree.merge_from_branch(child_tree.branch)
532
tree.commit(message='merge child branch')
534
log = self.run_bzr('log file1')[0]
535
self.assertContainsRe(log, 'revno: 1\n')
536
self.assertNotContainsRe(log, 'revno: 2\n')
537
self.assertNotContainsRe(log, 'revno: 3\n')
538
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
539
self.assertNotContainsRe(log, 'revno: 4\n')
540
log = self.run_bzr('log file2')[0]
541
self.assertNotContainsRe(log, 'revno: 1\n')
542
self.assertContainsRe(log, 'revno: 2\n')
543
self.assertNotContainsRe(log, 'revno: 3\n')
544
self.assertContainsRe(log, 'revno: 3.1.1\n')
545
self.assertContainsRe(log, 'revno: 4\n')
546
log = self.run_bzr('log file3')[0]
547
self.assertNotContainsRe(log, 'revno: 1\n')
548
self.assertNotContainsRe(log, 'revno: 2\n')
549
self.assertContainsRe(log, 'revno: 3\n')
550
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
551
self.assertNotContainsRe(log, 'revno: 4\n')
552
log = self.run_bzr('log -r3.1.1 file2')[0]
553
self.assertNotContainsRe(log, 'revno: 1\n')
554
self.assertNotContainsRe(log, 'revno: 2\n')
555
self.assertNotContainsRe(log, 'revno: 3\n')
556
self.assertContainsRe(log, 'revno: 3.1.1\n')
557
self.assertNotContainsRe(log, 'revno: 4\n')
558
log = self.run_bzr('log -r4 file2')[0]
559
self.assertNotContainsRe(log, 'revno: 1\n')
560
self.assertNotContainsRe(log, 'revno: 2\n')
561
self.assertNotContainsRe(log, 'revno: 3\n')
562
self.assertContainsRe(log, 'revno: 3.1.1\n')
563
self.assertContainsRe(log, 'revno: 4\n')
564
log = self.run_bzr('log -r3.. file2')[0]
565
self.assertNotContainsRe(log, 'revno: 1\n')
566
self.assertNotContainsRe(log, 'revno: 2\n')
567
self.assertNotContainsRe(log, 'revno: 3\n')
568
self.assertContainsRe(log, 'revno: 3.1.1\n')
569
self.assertContainsRe(log, 'revno: 4\n')
570
log = self.run_bzr('log -r..3 file2')[0]
571
self.assertNotContainsRe(log, 'revno: 1\n')
572
self.assertContainsRe(log, 'revno: 2\n')
573
self.assertNotContainsRe(log, 'revno: 3\n')
574
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
575
self.assertNotContainsRe(log, 'revno: 4\n')