~bzr-pqm/bzr/bzr.dev

1551.10.18 by Aaron Bentley
Log works in local treeless branches (#84247)
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
2
# -*- coding: utf-8 -*-
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
3
#
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
8
#
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
13
#
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
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
17
18
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
19
"""Black-box tests for bzr log."""
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
20
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
21
import os
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
22
1685.1.5 by John Arbash Meinel
Merged test_log.py.moved into test_log.py
23
import bzrlib
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
24
from bzrlib.tests.blackbox import ExternalBase
1551.10.18 by Aaron Bentley
Log works in local treeless branches (#84247)
25
from bzrlib.tests import TestCaseInTempDir, TestCaseWithTransport
1540.2.6 by Robey Pointer
make 'log' and 'status' treat '-r N..' as implicitly '-r N..-1'
26
27
28
class TestLog(ExternalBase):
29
2388.1.3 by Erik Bagfors
tests for tags in log output
30
    def _prepare(self, format=None):
31
        if format:
2530.3.4 by Martin Pool
Deprecate run_bzr_captured in favour of just run_bzr
32
            self.run_bzr(["init", "--format="+format])
2388.1.3 by Erik Bagfors
tests for tags in log output
33
        else:
2530.3.4 by Martin Pool
Deprecate run_bzr_captured in favour of just run_bzr
34
            self.run_bzr("init")
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
35
        self.build_tree(['hello.txt', 'goodbye.txt', 'meep.txt'])
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
36
        self.run_bzr("add hello.txt")
37
        self.run_bzr("commit -m message1 hello.txt")
38
        self.run_bzr("add goodbye.txt")
39
        self.run_bzr("commit -m message2 goodbye.txt")
40
        self.run_bzr("add meep.txt")
41
        self.run_bzr("commit -m message3 meep.txt")
42
        self.full_log = self.run_bzr("log")[0]
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
43
44
    def test_log_null_end_revspec(self):
45
        self._prepare()
46
        self.assertTrue('revno: 1\n' in self.full_log)
47
        self.assertTrue('revno: 2\n' in self.full_log)
48
        self.assertTrue('revno: 3\n' in self.full_log)
49
        self.assertTrue('message:\n  message1\n' in self.full_log)
50
        self.assertTrue('message:\n  message2\n' in self.full_log)
51
        self.assertTrue('message:\n  message3\n' in self.full_log)
52
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
53
        log = self.run_bzr("log -r 1..")[0]
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
54
        self.assertEqualDiff(log, self.full_log)
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
55
56
    def test_log_null_begin_revspec(self):
57
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
58
        log = self.run_bzr("log -r ..3")[0]
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
59
        self.assertEqualDiff(self.full_log, log)
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
60
61
    def test_log_null_both_revspecs(self):
62
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
63
        log = self.run_bzr("log -r ..")[0]
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
64
        self.assertEquals(self.full_log, log)
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
65
        self.assertEqualDiff(self.full_log, log)
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
66
67
    def test_log_negative_begin_revspec_full_log(self):
68
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
69
        log = self.run_bzr("log -r -3..")[0]
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
70
        self.assertEqualDiff(self.full_log, log)
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
71
72
    def test_log_negative_both_revspec_full_log(self):
73
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
74
        log = self.run_bzr("log -r -3..-1")[0]
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
75
        self.assertEqualDiff(self.full_log, log)
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
76
77
    def test_log_negative_both_revspec_partial(self):
78
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
79
        log = self.run_bzr("log -r -3..-2")[0]
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
80
        self.assertTrue('revno: 1\n' in log)
81
        self.assertTrue('revno: 2\n' in log)
82
        self.assertTrue('revno: 3\n' not in log)
83
84
    def test_log_negative_begin_revspec(self):
85
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
86
        log = self.run_bzr("log -r -2..")[0]
1553.4.2 by Michael Ellerman
Make bzr log -r .. work, fixes bug #4609. Add a bunch of tests to make sure
87
        self.assertTrue('revno: 1\n' not in log)
88
        self.assertTrue('revno: 2\n' in log)
89
        self.assertTrue('revno: 3\n' in log)
90
91
    def test_log_postive_revspecs(self):
92
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
93
        log = self.run_bzr("log -r 1..3")[0]
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
94
        self.assertEqualDiff(self.full_log, log)
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
95
1907.4.2 by Matthieu Moy
Make log work nicely with revno:N:path too.
96
    def test_log_revno_n_path(self):
97
        os.mkdir('branch1')
98
        os.chdir('branch1')
99
        self._prepare()
100
        os.chdir('..')
101
        os.mkdir('branch2')
102
        os.chdir('branch2')
103
        self._prepare()
104
        os.chdir('..')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
105
        log = self.run_bzr("log -r revno:2:branch1..revno:3:branch2",
1907.4.2 by Matthieu Moy
Make log work nicely with revno:N:path too.
106
                          retcode=3)[0]
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
107
        log = self.run_bzr("log -r revno:1:branch2..revno:3:branch2")[0]
2466.11.2 by Kent Gibson
Add tests for deltas in merge revisions
108
        self.assertEqualDiff(self.full_log, log)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
109
        log = self.run_bzr("log -r revno:1:branch2")[0]
1907.4.2 by Matthieu Moy
Make log work nicely with revno:N:path too.
110
        self.assertTrue('revno: 1\n' in log)
111
        self.assertTrue('revno: 2\n' not in log)
112
        self.assertTrue('branch nick: branch2\n' in log)
113
        self.assertTrue('branch nick: branch1\n' not in log)
114
        
2100.1.1 by wang
Running ``bzr log`` on nonexistent file gives an error instead of the
115
    def test_log_nonexistent_file(self):
116
        # files that don't exist in either the basis tree or working tree
117
        # should give an error
118
        wt = self.make_branch_and_tree('.')
119
        out, err = self.run_bzr('log', 'does-not-exist', retcode=3)
120
        self.assertContainsRe(
121
            err, 'Path does not have any revision history: does-not-exist')
2388.1.11 by Alexander Belchenko
changes after John's review
122
2388.1.3 by Erik Bagfors
tests for tags in log output
123
    def test_log_with_tags(self):
124
        self._prepare(format='dirstate-tags')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
125
        self.run_bzr('tag -r1 tag1')
126
        self.run_bzr('tag -r1 tag1.1')
127
        self.run_bzr('tag tag3')
2388.1.3 by Erik Bagfors
tests for tags in log output
128
        
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
129
        log = self.run_bzr("log -r-1")[0]
2388.1.3 by Erik Bagfors
tests for tags in log output
130
        self.assertTrue('tags: tag3' in log)
131
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
132
        log = self.run_bzr("log -r1")[0]
2388.1.3 by Erik Bagfors
tests for tags in log output
133
        # I guess that we can't know the order of tags in the output
134
        # since dicts are unordered, need to check both possibilities
2388.1.11 by Alexander Belchenko
changes after John's review
135
        self.assertContainsRe(log, r'tags: (tag1, tag1\.1|tag1\.1, tag1)')
136
2388.1.9 by Erik Bagfors
test for merges with tags in log
137
    def test_merged_log_with_tags(self):
138
        os.mkdir('branch1')
139
        os.chdir('branch1')
140
        self._prepare(format='dirstate-tags')
141
        os.chdir('..')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
142
        self.run_bzr('branch branch1 branch2')
2388.1.9 by Erik Bagfors
test for merges with tags in log
143
        os.chdir('branch1')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
144
        self.run_bzr('commit -m foobar --unchanged')
145
        self.run_bzr('tag tag1')
2388.1.9 by Erik Bagfors
test for merges with tags in log
146
        os.chdir('../branch2')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
147
        self.run_bzr('merge ../branch1')
148
        self.run_bzr('commit -m merge_branch_1')
149
        log = self.run_bzr("log -r-1")[0]
2388.1.11 by Alexander Belchenko
changes after John's review
150
        self.assertContainsRe(log, r'    tags: tag1')
2388.1.3 by Erik Bagfors
tests for tags in log output
151
2466.9.1 by Kent Gibson
add bzr log --limit
152
    def test_log_limit(self):
153
        self._prepare()
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
154
        log = self.run_bzr("log --limit 2")[0]
2466.9.1 by Kent Gibson
add bzr log --limit
155
        self.assertTrue('revno: 1\n' not in log)
156
        self.assertTrue('revno: 2\n' in log)
157
        self.assertTrue('revno: 3\n' in log)
158
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
159
1685.1.5 by John Arbash Meinel
Merged test_log.py.moved into test_log.py
160
class TestLogEncodings(TestCaseInTempDir):
161
162
    _mu = u'\xb5'
163
    _message = u'Message with \xb5'
164
165
    # Encodings which can encode mu
166
    good_encodings = [
167
        'utf-8',
168
        'latin-1',
169
        'iso-8859-1',
170
        'cp437', # Common windows encoding
171
        'cp1251', # Alexander Belchenko's windows encoding
172
        'cp1258', # Common windows encoding
173
    ]
174
    # Encodings which cannot encode mu
175
    bad_encodings = [
176
        'ascii',
177
        'iso-8859-2',
178
        'koi8_r',
179
    ]
180
181
    def setUp(self):
182
        TestCaseInTempDir.setUp(self)
183
        self.user_encoding = bzrlib.user_encoding
184
185
    def tearDown(self):
186
        bzrlib.user_encoding = self.user_encoding
187
        TestCaseInTempDir.tearDown(self)
188
189
    def create_branch(self):
190
        bzr = self.run_bzr
191
        bzr('init')
192
        open('a', 'wb').write('some stuff\n')
193
        bzr('add', 'a')
194
        bzr('commit', '-m', self._message)
195
196
    def try_encoding(self, encoding, fail=False):
197
        bzr = self.run_bzr
198
        if fail:
199
            self.assertRaises(UnicodeEncodeError,
200
                self._mu.encode, encoding)
201
            encoded_msg = self._message.encode(encoding, 'replace')
202
        else:
203
            encoded_msg = self._message.encode(encoding)
204
205
        old_encoding = bzrlib.user_encoding
206
        # This test requires that 'run_bzr' uses the current
207
        # bzrlib, because we override user_encoding, and expect
208
        # it to be used
209
        try:
210
            bzrlib.user_encoding = 'ascii'
211
            # We should be able to handle any encoding
212
            out, err = bzr('log', encoding=encoding)
213
            if not fail:
214
                # Make sure we wrote mu as we expected it to exist
215
                self.assertNotEqual(-1, out.find(encoded_msg))
216
                out_unicode = out.decode(encoding)
217
                self.assertNotEqual(-1, out_unicode.find(self._message))
218
            else:
219
                self.assertNotEqual(-1, out.find('Message with ?'))
220
        finally:
221
            bzrlib.user_encoding = old_encoding
222
223
    def test_log_handles_encoding(self):
224
        self.create_branch()
225
226
        for encoding in self.good_encodings:
227
            self.try_encoding(encoding)
228
229
    def test_log_handles_bad_encoding(self):
230
        self.create_branch()
231
232
        for encoding in self.bad_encodings:
233
            self.try_encoding(encoding, fail=True)
234
235
    def test_stdout_encoding(self):
236
        bzr = self.run_bzr
237
        bzrlib.user_encoding = "cp1251"
238
239
        bzr('init')
240
        self.build_tree(['a'])
241
        bzr('add', 'a')
242
        bzr('commit', '-m', u'\u0422\u0435\u0441\u0442')
243
        stdout, stderr = self.run_bzr('log', encoding='cp866')
244
245
        message = stdout.splitlines()[-1]
246
247
        # explanation of the check:
248
        # u'\u0422\u0435\u0441\u0442' is word 'Test' in russian
249
        # in cp866  encoding this is string '\x92\xa5\xe1\xe2'
250
        # in cp1251 encoding this is string '\xd2\xe5\xf1\xf2'
251
        # This test should check that output of log command
252
        # encoded to sys.stdout.encoding
253
        test_in_cp866 = '\x92\xa5\xe1\xe2'
254
        test_in_cp1251 = '\xd2\xe5\xf1\xf2'
255
        # Make sure the log string is encoded in cp866
256
        self.assertEquals(test_in_cp866, message[2:])
257
        # Make sure the cp1251 string is not found anywhere
258
        self.assertEquals(-1, stdout.find(test_in_cp1251))
259
1551.10.18 by Aaron Bentley
Log works in local treeless branches (#84247)
260
261
class TestLogFile(TestCaseWithTransport):
262
263
    def test_log_local_branch_file(self):
264
        """We should be able to log files in local treeless branches"""
265
        tree = self.make_branch_and_tree('tree')
266
        self.build_tree(['tree/file'])
267
        tree.add('file')
268
        tree.commit('revision 1')
269
        tree.bzrdir.destroy_workingtree()
270
        self.run_bzr('log', 'tree/file')
2359.1.1 by Kent Gibson
Fix ``bzr log <file>`` so it only logs the revisions that changed the file, and does it faster.
271
272
    def test_log_file(self):
273
        """The log for a particular file should only list revs for that file"""
274
        tree = self.make_branch_and_tree('parent')
275
        self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
276
        tree.add('file1')
277
        tree.commit('add file1')
278
        tree.add('file2')
279
        tree.commit('add file2')
280
        tree.add('file3')
281
        tree.commit('add file3')
282
        self.run_bzr('branch', 'parent', 'child')
283
        print >> file('child/file2', 'wb'), 'hello'
284
        self.run_bzr('commit', '-m', 'branch 1', 'child')
285
        os.chdir('parent')
286
        self.run_bzr('merge', '../child')
2359.1.2 by Kent Gibson
add logging of merge revisions
287
        self.run_bzr('commit', '-m', 'merge child branch')
2359.1.1 by Kent Gibson
Fix ``bzr log <file>`` so it only logs the revisions that changed the file, and does it faster.
288
        
289
        log = self.run_bzr('log', 'file1')[0]
290
        self.assertContainsRe(log, 'revno: 1\n')
291
        self.assertNotContainsRe(log, 'revno: 2\n')
292
        self.assertNotContainsRe(log, 'revno: 3\n')
293
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
294
        self.assertNotContainsRe(log, 'revno: 4\n')
295
        log = self.run_bzr('log', 'file2')[0]
296
        self.assertNotContainsRe(log, 'revno: 1\n')
297
        self.assertContainsRe(log, 'revno: 2\n')
298
        self.assertNotContainsRe(log, 'revno: 3\n')
299
        self.assertContainsRe(log, 'revno: 3.1.1\n')
2359.1.2 by Kent Gibson
add logging of merge revisions
300
        self.assertContainsRe(log, 'revno: 4\n')
2359.1.1 by Kent Gibson
Fix ``bzr log <file>`` so it only logs the revisions that changed the file, and does it faster.
301
        log = self.run_bzr('log', 'file3')[0]
302
        self.assertNotContainsRe(log, 'revno: 1\n')
303
        self.assertNotContainsRe(log, 'revno: 2\n')
304
        self.assertContainsRe(log, 'revno: 3\n')
305
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
306
        self.assertNotContainsRe(log, 'revno: 4\n')