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."""
24
from bzrlib.tests.blackbox import ExternalBase
25
from bzrlib.tests import TestCaseInTempDir, TestCaseWithTransport
26
from bzrlib.tests.test_log import (
31
class TestLog(ExternalBase):
33
def _prepare(self, path='.', format=None):
34
tree = self.make_branch_and_tree(path, format=format)
36
[path + '/hello.txt', path + '/goodbye.txt', path + '/meep.txt'])
38
tree.commit(message='message1')
39
tree.add('goodbye.txt')
40
tree.commit(message='message2')
42
tree.commit(message='message3')
43
self.full_log = self.run_bzr(["log", path])[0]
46
def test_log_null_end_revspec(self):
48
self.assertTrue('revno: 1\n' in self.full_log)
49
self.assertTrue('revno: 2\n' in self.full_log)
50
self.assertTrue('revno: 3\n' in self.full_log)
51
self.assertTrue('message:\n message1\n' in self.full_log)
52
self.assertTrue('message:\n message2\n' in self.full_log)
53
self.assertTrue('message:\n message3\n' in self.full_log)
55
log = self.run_bzr("log -r 1..")[0]
56
self.assertEqualDiff(log, self.full_log)
58
def test_log_null_begin_revspec(self):
60
log = self.run_bzr("log -r ..3")[0]
61
self.assertEqualDiff(self.full_log, log)
63
def test_log_null_both_revspecs(self):
65
log = self.run_bzr("log -r ..")[0]
66
self.assertEqualDiff(self.full_log, log)
68
def test_log_zero_revspec(self):
70
self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
73
def test_log_zero_begin_revspec(self):
75
self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
78
def test_log_zero_end_revspec(self):
80
self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
83
def test_log_negative_begin_revspec_full_log(self):
85
log = self.run_bzr("log -r -3..")[0]
86
self.assertEqualDiff(self.full_log, log)
88
def test_log_negative_both_revspec_full_log(self):
90
log = self.run_bzr("log -r -3..-1")[0]
91
self.assertEqualDiff(self.full_log, log)
93
def test_log_negative_both_revspec_partial(self):
95
log = self.run_bzr("log -r -3..-2")[0]
96
self.assertTrue('revno: 1\n' in log)
97
self.assertTrue('revno: 2\n' in log)
98
self.assertTrue('revno: 3\n' not in log)
100
def test_log_negative_begin_revspec(self):
102
log = self.run_bzr("log -r -2..")[0]
103
self.assertTrue('revno: 1\n' not in log)
104
self.assertTrue('revno: 2\n' in log)
105
self.assertTrue('revno: 3\n' in log)
107
def test_log_positive_revspecs(self):
109
log = self.run_bzr("log -r 1..3")[0]
110
self.assertEqualDiff(self.full_log, log)
112
def test_log_reversed_revspecs(self):
114
self.run_bzr_error(('bzr: ERROR: Start revision must be older than '
115
'the end revision.\n',),
118
def test_log_revno_n_path(self):
119
self._prepare(path='branch1')
120
self._prepare(path='branch2')
121
log = self.run_bzr("log -r revno:2:branch1..revno:3:branch2",
123
log = self.run_bzr("log -r revno:1:branch2..revno:3:branch2")[0]
124
self.assertEqualDiff(self.full_log, log)
125
log = self.run_bzr("log -r revno:1:branch2")[0]
126
self.assertTrue('revno: 1\n' in log)
127
self.assertTrue('revno: 2\n' not in log)
128
self.assertTrue('branch nick: branch2\n' in log)
129
self.assertTrue('branch nick: branch1\n' not in log)
131
def test_log_nonexistent_file(self):
132
# files that don't exist in either the basis tree or working tree
133
# should give an error
134
wt = self.make_branch_and_tree('.')
135
out, err = self.run_bzr('log does-not-exist', retcode=3)
136
self.assertContainsRe(
137
err, 'Path does not have any revision history: does-not-exist')
139
def test_log_with_tags(self):
140
tree = self._prepare(format='dirstate-tags')
142
branch.tags.set_tag('tag1', branch.get_rev_id(1))
143
branch.tags.set_tag('tag1.1', branch.get_rev_id(1))
144
branch.tags.set_tag('tag3', branch.last_revision())
146
log = self.run_bzr("log -r-1")[0]
147
self.assertTrue('tags: tag3' in log)
149
log = self.run_bzr("log -r1")[0]
150
# I guess that we can't know the order of tags in the output
151
# since dicts are unordered, need to check both possibilities
152
self.assertContainsRe(log, r'tags: (tag1, tag1\.1|tag1\.1, tag1)')
154
def test_merged_log_with_tags(self):
155
branch1_tree = self._prepare(path='branch1', format='dirstate-tags')
156
branch1 = branch1_tree.branch
157
branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
158
branch1_tree.commit(message='foobar', allow_pointless=True)
159
branch1.tags.set_tag('tag1', branch1.last_revision())
161
self.run_bzr('merge ../branch1') # tags don't propagate otherwise
162
branch2_tree.commit(message='merge branch 1')
163
log = self.run_bzr("log -r-1")[0]
164
self.assertContainsRe(log, r' tags: tag1')
165
log = self.run_bzr("log -r3.1.1")[0]
166
self.assertContainsRe(log, r'tags: tag1')
168
def test_log_limit(self):
170
log = self.run_bzr("log --limit 2")[0]
171
self.assertTrue('revno: 1\n' not in log)
172
self.assertTrue('revno: 2\n' in log)
173
self.assertTrue('revno: 3\n' in log)
175
class TestLogMerges(ExternalBase):
178
parent_tree = self.make_branch_and_tree('parent')
179
parent_tree.commit(message='first post', allow_pointless=True)
180
child_tree = parent_tree.bzrdir.sprout('child').open_workingtree()
181
child_tree.commit(message='branch 1', allow_pointless=True)
183
child_tree.bzrdir.sprout('smallerchild').open_workingtree()
184
smaller_tree.commit(message='branch 2', allow_pointless=True)
185
child_tree.merge_from_branch(smaller_tree.branch)
186
child_tree.commit(message='merge branch 2')
187
parent_tree.merge_from_branch(child_tree.branch)
188
parent_tree.commit(message='merge branch 1')
191
def test_merges_are_indented_by_level(self):
193
out,err = self.run_bzr('log')
194
self.assertEqual('', err)
195
log = normalize_log(out)
196
self.assertEqualDiff(log, """\
197
------------------------------------------------------------
199
committer: Lorem Ipsum <test@example.com>
204
------------------------------------------------------------
206
committer: Lorem Ipsum <test@example.com>
211
------------------------------------------------------------
213
committer: Lorem Ipsum <test@example.com>
214
branch nick: smallerchild
218
------------------------------------------------------------
220
committer: Lorem Ipsum <test@example.com>
225
------------------------------------------------------------
227
committer: Lorem Ipsum <test@example.com>
234
def test_merges_single_merge_rev(self):
236
out,err = self.run_bzr('log -r1.1.2')
237
self.assertEqual('', err)
238
log = normalize_log(out)
239
self.assertEqualDiff(log, """\
240
------------------------------------------------------------
242
committer: Lorem Ipsum <test@example.com>
247
------------------------------------------------------------
249
committer: Lorem Ipsum <test@example.com>
250
branch nick: smallerchild
256
def test_merges_partial_range(self):
258
out,err = self.run_bzr('log -r1.1.1..1.1.2')
259
self.assertEqual('', err)
260
log = normalize_log(out)
261
self.assertEqualDiff(log, """\
262
------------------------------------------------------------
264
committer: Lorem Ipsum <test@example.com>
269
------------------------------------------------------------
271
committer: Lorem Ipsum <test@example.com>
272
branch nick: smallerchild
276
------------------------------------------------------------
278
committer: Lorem Ipsum <test@example.com>
285
def test_merges_nonsupporting_formatter(self):
287
err_msg = 'Selected log formatter only supports mainline revisions.'
288
out,err = self.run_bzr('log --short -r1.1.2', retcode=3)
289
self.assertContainsRe(err, err_msg)
290
out,err = self.run_bzr('log --short -r1..1.1.2', retcode=3)
291
self.assertContainsRe(err, err_msg)
292
out,err = self.run_bzr('log --short -r1.1.1..1.1.2', retcode=3)
293
self.assertContainsRe(err, err_msg)
296
class TestLogEncodings(TestCaseInTempDir):
299
_message = u'Message with \xb5'
301
# Encodings which can encode mu
306
'cp437', # Common windows encoding
307
'cp1251', # Alexander Belchenko's windows encoding
308
'cp1258', # Common windows encoding
310
# Encodings which cannot encode mu
318
TestCaseInTempDir.setUp(self)
319
self.user_encoding = bzrlib.user_encoding
322
bzrlib.user_encoding = self.user_encoding
323
TestCaseInTempDir.tearDown(self)
325
def create_branch(self):
328
open('a', 'wb').write('some stuff\n')
330
bzr(['commit', '-m', self._message])
332
def try_encoding(self, encoding, fail=False):
335
self.assertRaises(UnicodeEncodeError,
336
self._mu.encode, encoding)
337
encoded_msg = self._message.encode(encoding, 'replace')
339
encoded_msg = self._message.encode(encoding)
341
old_encoding = bzrlib.user_encoding
342
# This test requires that 'run_bzr' uses the current
343
# bzrlib, because we override user_encoding, and expect
346
bzrlib.user_encoding = 'ascii'
347
# We should be able to handle any encoding
348
out, err = bzr('log', encoding=encoding)
350
# Make sure we wrote mu as we expected it to exist
351
self.assertNotEqual(-1, out.find(encoded_msg))
352
out_unicode = out.decode(encoding)
353
self.assertNotEqual(-1, out_unicode.find(self._message))
355
self.assertNotEqual(-1, out.find('Message with ?'))
357
bzrlib.user_encoding = old_encoding
359
def test_log_handles_encoding(self):
362
for encoding in self.good_encodings:
363
self.try_encoding(encoding)
365
def test_log_handles_bad_encoding(self):
368
for encoding in self.bad_encodings:
369
self.try_encoding(encoding, fail=True)
371
def test_stdout_encoding(self):
373
bzrlib.user_encoding = "cp1251"
376
self.build_tree(['a'])
378
bzr(['commit', '-m', u'\u0422\u0435\u0441\u0442'])
379
stdout, stderr = self.run_bzr('log', encoding='cp866')
381
message = stdout.splitlines()[-1]
383
# explanation of the check:
384
# u'\u0422\u0435\u0441\u0442' is word 'Test' in russian
385
# in cp866 encoding this is string '\x92\xa5\xe1\xe2'
386
# in cp1251 encoding this is string '\xd2\xe5\xf1\xf2'
387
# This test should check that output of log command
388
# encoded to sys.stdout.encoding
389
test_in_cp866 = '\x92\xa5\xe1\xe2'
390
test_in_cp1251 = '\xd2\xe5\xf1\xf2'
391
# Make sure the log string is encoded in cp866
392
self.assertEquals(test_in_cp866, message[2:])
393
# Make sure the cp1251 string is not found anywhere
394
self.assertEquals(-1, stdout.find(test_in_cp1251))
397
class TestLogFile(TestCaseWithTransport):
399
def test_log_local_branch_file(self):
400
"""We should be able to log files in local treeless branches"""
401
tree = self.make_branch_and_tree('tree')
402
self.build_tree(['tree/file'])
404
tree.commit('revision 1')
405
tree.bzrdir.destroy_workingtree()
406
self.run_bzr('log tree/file')
408
def test_log_file(self):
409
"""The log for a particular file should only list revs for that file"""
410
tree = self.make_branch_and_tree('parent')
411
self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
413
tree.commit('add file1')
415
tree.commit('add file2')
417
tree.commit('add file3')
418
child_tree = tree.bzrdir.sprout('child').open_workingtree()
419
self.build_tree_contents([('child/file2', 'hello')])
420
child_tree.commit(message='branch 1')
421
tree.merge_from_branch(child_tree.branch)
422
tree.commit(message='merge child branch')
424
log = self.run_bzr('log file1')[0]
425
self.assertContainsRe(log, 'revno: 1\n')
426
self.assertNotContainsRe(log, 'revno: 2\n')
427
self.assertNotContainsRe(log, 'revno: 3\n')
428
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
429
self.assertNotContainsRe(log, 'revno: 4\n')
430
log = self.run_bzr('log file2')[0]
431
self.assertNotContainsRe(log, 'revno: 1\n')
432
self.assertContainsRe(log, 'revno: 2\n')
433
self.assertNotContainsRe(log, 'revno: 3\n')
434
self.assertContainsRe(log, 'revno: 3.1.1\n')
435
self.assertContainsRe(log, 'revno: 4\n')
436
log = self.run_bzr('log file3')[0]
437
self.assertNotContainsRe(log, 'revno: 1\n')
438
self.assertNotContainsRe(log, 'revno: 2\n')
439
self.assertContainsRe(log, 'revno: 3\n')
440
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
441
self.assertNotContainsRe(log, 'revno: 4\n')
442
log = self.run_bzr('log -r3.1.1 file2')[0]
443
self.assertNotContainsRe(log, 'revno: 1\n')
444
self.assertNotContainsRe(log, 'revno: 2\n')
445
self.assertNotContainsRe(log, 'revno: 3\n')
446
self.assertContainsRe(log, 'revno: 3.1.1\n')
447
self.assertNotContainsRe(log, 'revno: 4\n')
448
log = self.run_bzr('log -r4 file2')[0]
449
self.assertNotContainsRe(log, 'revno: 1\n')
450
self.assertNotContainsRe(log, 'revno: 2\n')
451
self.assertNotContainsRe(log, 'revno: 3\n')
452
self.assertContainsRe(log, 'revno: 3.1.1\n')
453
self.assertContainsRe(log, 'revno: 4\n')
454
log = self.run_bzr('log -r3.. file2')[0]
455
self.assertNotContainsRe(log, 'revno: 1\n')
456
self.assertNotContainsRe(log, 'revno: 2\n')
457
self.assertNotContainsRe(log, 'revno: 3\n')
458
self.assertContainsRe(log, 'revno: 3.1.1\n')
459
self.assertContainsRe(log, 'revno: 4\n')
460
log = self.run_bzr('log -r..3 file2')[0]
461
self.assertNotContainsRe(log, 'revno: 1\n')
462
self.assertContainsRe(log, 'revno: 2\n')
463
self.assertNotContainsRe(log, 'revno: 3\n')
464
self.assertNotContainsRe(log, 'revno: 3.1.1\n')
465
self.assertNotContainsRe(log, 'revno: 4\n')