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_change_revno(self):
145
expected_log = self.run_bzr("log -r 1")[0]
146
log = self.run_bzr("log -c 1")[0]
147
self.assertEqualDiff(expected_log, log)
149
def test_log_change_single_revno(self):
151
self.run_bzr_error('bzr: ERROR: Option --change does not'
152
' accept revision ranges',
153
['log', '--change', '2..3'])
155
def test_log_change_incompatible_with_revision(self):
157
self.run_bzr_error('bzr: ERROR: --revision and --change'
158
' are mutually exclusive',
159
['log', '--change', '2', '--revision', '3'])
161
def test_log_nonexistent_file(self):
162
# files that don't exist in either the basis tree or working tree
163
# should give an error
164
wt = self.make_branch_and_tree('.')
165
out, err = self.run_bzr('log does-not-exist', retcode=3)
166
self.assertContainsRe(
167
err, 'Path does not have any revision history: does-not-exist')
169
def test_log_with_tags(self):
170
tree = self._prepare(format='dirstate-tags')
172
branch.tags.set_tag('tag1', branch.get_rev_id(1))
173
branch.tags.set_tag('tag1.1', branch.get_rev_id(1))
174
branch.tags.set_tag('tag3', branch.last_revision())
176
log = self.run_bzr("log -r-1")[0]
177
self.assertTrue('tags: tag3' in log)
179
log = self.run_bzr("log -r1")[0]
180
# I guess that we can't know the order of tags in the output
181
# since dicts are unordered, need to check both possibilities
182
self.assertContainsRe(log, r'tags: (tag1, tag1\.1|tag1\.1, tag1)')
184
def test_merged_log_with_tags(self):
185
branch1_tree = self._prepare(path='branch1', format='dirstate-tags')
186
branch1 = branch1_tree.branch
187
branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
188
branch1_tree.commit(message='foobar', allow_pointless=True)
189
branch1.tags.set_tag('tag1', branch1.last_revision())
191
self.run_bzr('merge ../branch1') # tags don't propagate otherwise
192
branch2_tree.commit(message='merge branch 1')
193
log = self.run_bzr("log -r-1")[0]
194
self.assertContainsRe(log, r' tags: tag1')
195
log = self.run_bzr("log -r3.1.1")[0]
196
self.assertContainsRe(log, r'tags: tag1')
198
def test_log_limit(self):
199
tree = self.make_branch_and_tree('.')
200
# We want more commits than our batch size starts at
201
for pos in range(10):
202
tree.commit("%s" % pos)
203
log = self.run_bzr("log --limit 2")[0]
204
self.assertNotContainsRe(log, r'revno: 1\n')
205
self.assertNotContainsRe(log, r'revno: 2\n')
206
self.assertNotContainsRe(log, r'revno: 3\n')
207
self.assertNotContainsRe(log, r'revno: 4\n')
208
self.assertNotContainsRe(log, r'revno: 5\n')
209
self.assertNotContainsRe(log, r'revno: 6\n')
210
self.assertNotContainsRe(log, r'revno: 7\n')
211
self.assertNotContainsRe(log, r'revno: 8\n')
212
self.assertContainsRe(log, r'revno: 9\n')
213
self.assertContainsRe(log, r'revno: 10\n')
215
def test_log_limit_short(self):
217
log = self.run_bzr("log -l 2")[0]
218
self.assertNotContainsRe(log, r'revno: 1\n')
219
self.assertContainsRe(log, r'revno: 2\n')
220
self.assertContainsRe(log, r'revno: 3\n')
223
class TestLogMerges(TestCaseWithoutPropsHandler):
226
parent_tree = self.make_branch_and_tree('parent')
227
parent_tree.commit(message='first post', allow_pointless=True)
228
child_tree = parent_tree.bzrdir.sprout('child').open_workingtree()
229
child_tree.commit(message='branch 1', allow_pointless=True)
231
child_tree.bzrdir.sprout('smallerchild').open_workingtree()
232
smaller_tree.commit(message='branch 2', allow_pointless=True)
233
child_tree.merge_from_branch(smaller_tree.branch)
234
child_tree.commit(message='merge branch 2')
235
parent_tree.merge_from_branch(child_tree.branch)
236
parent_tree.commit(message='merge branch 1')
239
def test_merges_are_indented_by_level(self):
241
out,err = self.run_bzr('log')
242
self.assertEqual('', err)
243
log = normalize_log(out)
244
self.assertEqualDiff(log, """\
245
------------------------------------------------------------
247
committer: Lorem Ipsum <test@example.com>
252
------------------------------------------------------------
254
committer: Lorem Ipsum <test@example.com>
259
------------------------------------------------------------
261
committer: Lorem Ipsum <test@example.com>
262
branch nick: smallerchild
266
------------------------------------------------------------
268
committer: Lorem Ipsum <test@example.com>
273
------------------------------------------------------------
275
committer: Lorem Ipsum <test@example.com>
282
def test_merges_single_merge_rev(self):
284
out,err = self.run_bzr('log -r1.1.2')
285
self.assertEqual('', err)
286
log = normalize_log(out)
287
self.assertEqualDiff(log, """\
288
------------------------------------------------------------
290
committer: Lorem Ipsum <test@example.com>
295
------------------------------------------------------------
297
committer: Lorem Ipsum <test@example.com>
298
branch nick: smallerchild
304
def test_merges_partial_range(self):
306
out, err = self.run_bzr('log -r1.1.1..1.1.2')
307
self.assertEqual('', err)
308
log = normalize_log(out)
309
self.assertEqualDiff(log, """\
310
------------------------------------------------------------
312
committer: Lorem Ipsum <test@example.com>
317
------------------------------------------------------------
319
committer: Lorem Ipsum <test@example.com>
320
branch nick: smallerchild
324
------------------------------------------------------------
326
committer: Lorem Ipsum <test@example.com>
333
def test_merges_nonsupporting_formatter(self):
335
err_msg = 'Selected log formatter only supports mainline revisions.'
336
# The single revision case is tested in the core tests
337
# since all standard formatters support single merge revisions.
338
out,err = self.run_bzr('log --short -r1..1.1.2', retcode=3)
339
self.assertContainsRe(err, err_msg)
340
out,err = self.run_bzr('log --short -r1.1.1..1.1.2', retcode=3)
341
self.assertContainsRe(err, err_msg)
344
class TestLogEncodings(TestCaseInTempDir):
347
_message = u'Message with \xb5'
349
# Encodings which can encode mu
354
'cp437', # Common windows encoding
355
'cp1251', # Alexander Belchenko's windows encoding
356
'cp1258', # Common windows encoding
358
# Encodings which cannot encode mu
366
TestCaseInTempDir.setUp(self)
367
self.user_encoding = osutils._cached_user_encoding
370
osutils._cached_user_encoding = self.user_encoding
371
TestCaseInTempDir.tearDown(self)
373
def create_branch(self):
376
open('a', 'wb').write('some stuff\n')
378
bzr(['commit', '-m', self._message])
380
def try_encoding(self, encoding, fail=False):
383
self.assertRaises(UnicodeEncodeError,
384
self._mu.encode, encoding)
385
encoded_msg = self._message.encode(encoding, 'replace')
387
encoded_msg = self._message.encode(encoding)
389
old_encoding = osutils._cached_user_encoding
390
# This test requires that 'run_bzr' uses the current
391
# bzrlib, because we override user_encoding, and expect
394
osutils._cached_user_encoding = 'ascii'
395
# We should be able to handle any encoding
396
out, err = bzr('log', encoding=encoding)
398
# Make sure we wrote mu as we expected it to exist
399
self.assertNotEqual(-1, out.find(encoded_msg))
400
out_unicode = out.decode(encoding)
401
self.assertNotEqual(-1, out_unicode.find(self._message))
403
self.assertNotEqual(-1, out.find('Message with ?'))
405
osutils._cached_user_encoding = old_encoding
407
def test_log_handles_encoding(self):
410
for encoding in self.good_encodings:
411
self.try_encoding(encoding)
413
def test_log_handles_bad_encoding(self):
416
for encoding in self.bad_encodings:
417
self.try_encoding(encoding, fail=True)
419
def test_stdout_encoding(self):
421
osutils._cached_user_encoding = "cp1251"
424
self.build_tree(['a'])
426
bzr(['commit', '-m', u'\u0422\u0435\u0441\u0442'])
427
stdout, stderr = self.run_bzr('log', encoding='cp866')
429
message = stdout.splitlines()[-1]
431
# explanation of the check:
432
# u'\u0422\u0435\u0441\u0442' is word 'Test' in russian
433
# in cp866 encoding this is string '\x92\xa5\xe1\xe2'
434
# in cp1251 encoding this is string '\xd2\xe5\xf1\xf2'
435
# This test should check that output of log command
436
# encoded to sys.stdout.encoding
437
test_in_cp866 = '\x92\xa5\xe1\xe2'
438
test_in_cp1251 = '\xd2\xe5\xf1\xf2'
439
# Make sure the log string is encoded in cp866
440
self.assertEquals(test_in_cp866, message[2:])
441
# Make sure the cp1251 string is not found anywhere
442
self.assertEquals(-1, stdout.find(test_in_cp1251))
445
class TestLogFile(TestCaseWithTransport):
447
def test_log_local_branch_file(self):
448
"""We should be able to log files in local treeless branches"""
449
tree = self.make_branch_and_tree('tree')
450
self.build_tree(['tree/file'])
452
tree.commit('revision 1')
453
tree.bzrdir.destroy_workingtree()
454
self.run_bzr('log tree/file')
456
def test_log_file(self):
457
"""The log for a particular file should only list revs for that file"""
458
tree = self.make_branch_and_tree('parent')
459
self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
461
tree.commit('add file1')
463
tree.commit('add file2')
465
tree.commit('add file3')
466
child_tree = tree.bzrdir.sprout('child').open_workingtree()
467
self.build_tree_contents([('child/file2', 'hello')])
468
child_tree.commit(message='branch 1')
469
tree.merge_from_branch(child_tree.branch)
470
tree.commit(message='merge child branch')
472
log = self.run_bzr('log file1')[0]
473
self.assertContainsRe(log, 'revno: 1\n')
474
self.assertNotContainsRe(log, 'revno: 2\n')
475
self.assertNotContainsRe(log, 'revno: 3\n')
476
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
477
self.assertNotContainsRe(log, 'revno: 4\n')
478
log = self.run_bzr('log file2')[0]
479
self.assertNotContainsRe(log, 'revno: 1\n')
480
self.assertContainsRe(log, 'revno: 2\n')
481
self.assertNotContainsRe(log, 'revno: 3\n')
482
self.assertContainsRe(log, 'revno: 3.1.1\n')
483
self.assertContainsRe(log, 'revno: 4\n')
484
log = self.run_bzr('log file3')[0]
485
self.assertNotContainsRe(log, 'revno: 1\n')
486
self.assertNotContainsRe(log, 'revno: 2\n')
487
self.assertContainsRe(log, 'revno: 3\n')
488
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
489
self.assertNotContainsRe(log, 'revno: 4\n')
490
log = self.run_bzr('log -r3.1.1 file2')[0]
491
self.assertNotContainsRe(log, 'revno: 1\n')
492
self.assertNotContainsRe(log, 'revno: 2\n')
493
self.assertNotContainsRe(log, 'revno: 3\n')
494
self.assertContainsRe(log, 'revno: 3.1.1\n')
495
self.assertNotContainsRe(log, 'revno: 4\n')
496
log = self.run_bzr('log -r4 file2')[0]
497
self.assertNotContainsRe(log, 'revno: 1\n')
498
self.assertNotContainsRe(log, 'revno: 2\n')
499
self.assertNotContainsRe(log, 'revno: 3\n')
500
self.assertContainsRe(log, 'revno: 3.1.1\n')
501
self.assertContainsRe(log, 'revno: 4\n')
502
log = self.run_bzr('log -r3.. file2')[0]
503
self.assertNotContainsRe(log, 'revno: 1\n')
504
self.assertNotContainsRe(log, 'revno: 2\n')
505
self.assertNotContainsRe(log, 'revno: 3\n')
506
self.assertContainsRe(log, 'revno: 3.1.1\n')
507
self.assertContainsRe(log, 'revno: 4\n')
508
log = self.run_bzr('log -r..3 file2')[0]
509
self.assertNotContainsRe(log, 'revno: 1\n')
510
self.assertContainsRe(log, 'revno: 2\n')
511
self.assertNotContainsRe(log, 'revno: 3\n')
512
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
513
self.assertNotContainsRe(log, 'revno: 4\n')