/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/blackbox/test_annotate.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
 
 
19
"""Black-box tests for bzr.
 
20
 
 
21
These check that it behaves properly when it's invoked through the regular
 
22
command-line interface. This doesn't actually run a new interpreter but
 
23
rather starts again from the run_brz function.
 
24
"""
 
25
 
 
26
 
 
27
from breezy import (
 
28
    config,
 
29
    tests,
 
30
    )
 
31
 
 
32
from breezy.tests.matchers import ContainsNoVfsCalls
 
33
from breezy.urlutils import joinpath
 
34
 
 
35
from ..test_bedding import override_whoami
 
36
 
 
37
class TestAnnotate(tests.TestCaseWithTransport):
 
38
 
 
39
    def setUp(self):
 
40
        super(TestAnnotate, self).setUp()
 
41
        wt = self.make_branch_and_tree('.')
 
42
        b = wt.branch
 
43
        self.build_tree_contents([('hello.txt', b'my helicopter\n'),
 
44
                                  ('nomail.txt', b'nomail\n')])
 
45
        wt.add(['hello.txt'])
 
46
        self.revision_id_1 = wt.commit('add hello',
 
47
                                       committer='test@user',
 
48
                                       timestamp=1165960000.00, timezone=0)
 
49
        wt.add(['nomail.txt'])
 
50
        self.revision_id_2 = wt.commit('add nomail',
 
51
                                       committer='no mail',
 
52
                                       timestamp=1165970000.00, timezone=0)
 
53
        self.build_tree_contents([('hello.txt', b'my helicopter\n'
 
54
                                                b'your helicopter\n')])
 
55
        self.revision_id_3 = wt.commit('mod hello',
 
56
                                       committer='user@test',
 
57
                                       timestamp=1166040000.00, timezone=0)
 
58
        self.build_tree_contents([('hello.txt', b'my helicopter\n'
 
59
                                                b'your helicopter\n'
 
60
                                                b'all of\n'
 
61
                                                b'our helicopters\n'
 
62
                                   )])
 
63
        self.revision_id_4 = wt.commit('mod hello',
 
64
                                       committer='user@test',
 
65
                                       timestamp=1166050000.00, timezone=0)
 
66
 
 
67
    def test_help_annotate(self):
 
68
        """Annotate command exists"""
 
69
        out, err = self.run_bzr('--no-plugins annotate --help')
 
70
 
 
71
    def test_annotate_cmd(self):
 
72
        out, err = self.run_bzr('annotate hello.txt')
 
73
        self.assertEqual('', err)
 
74
        self.assertEqualDiff('''\
 
75
1   test@us | my helicopter
 
76
3   user@te | your helicopter
 
77
4   user@te | all of
 
78
            | our helicopters
 
79
''', out)
 
80
 
 
81
    def test_annotate_cmd_full(self):
 
82
        out, err = self.run_bzr('annotate hello.txt --all')
 
83
        self.assertEqual('', err)
 
84
        self.assertEqualDiff('''\
 
85
1   test@us | my helicopter
 
86
3   user@te | your helicopter
 
87
4   user@te | all of
 
88
4   user@te | our helicopters
 
89
''', out)
 
90
 
 
91
    def test_annotate_cmd_long(self):
 
92
        out, err = self.run_bzr('annotate hello.txt --long')
 
93
        self.assertEqual('', err)
 
94
        self.assertEqualDiff('''\
 
95
1   test@user 20061212 | my helicopter
 
96
3   user@test 20061213 | your helicopter
 
97
4   user@test 20061213 | all of
 
98
                       | our helicopters
 
99
''', out)
 
100
 
 
101
    def test_annotate_cmd_show_ids(self):
 
102
        out, err = self.run_bzr('annotate hello.txt --show-ids')
 
103
        max_len = max([len(self.revision_id_1),
 
104
                       len(self.revision_id_3),
 
105
                       len(self.revision_id_4)])
 
106
        self.assertEqual('', err)
 
107
        self.assertEqualDiff('''\
 
108
%*s | my helicopter
 
109
%*s | your helicopter
 
110
%*s | all of
 
111
%*s | our helicopters
 
112
''' % (max_len, self.revision_id_1.decode('utf-8'),
 
113
            max_len, self.revision_id_3.decode('utf-8'),
 
114
            max_len, self.revision_id_4.decode('utf-8'),
 
115
            max_len, '',
 
116
       ), out)
 
117
 
 
118
    def test_no_mail(self):
 
119
        out, err = self.run_bzr('annotate nomail.txt')
 
120
        self.assertEqual('', err)
 
121
        self.assertEqualDiff('''\
 
122
2   no mail | nomail
 
123
''', out)
 
124
 
 
125
    def test_annotate_cmd_revision(self):
 
126
        out, err = self.run_bzr('annotate hello.txt -r1')
 
127
        self.assertEqual('', err)
 
128
        self.assertEqualDiff('''\
 
129
1   test@us | my helicopter
 
130
''', out)
 
131
 
 
132
    def test_annotate_cmd_revision3(self):
 
133
        out, err = self.run_bzr('annotate hello.txt -r3')
 
134
        self.assertEqual('', err)
 
135
        self.assertEqualDiff('''\
 
136
1   test@us | my helicopter
 
137
3   user@te | your helicopter
 
138
''', out)
 
139
 
 
140
    def test_annotate_cmd_unknown_revision(self):
 
141
        out, err = self.run_bzr('annotate hello.txt -r 10',
 
142
                                retcode=3)
 
143
        self.assertEqual('', out)
 
144
        self.assertContainsRe(err, "Requested revision: '10' does not exist")
 
145
 
 
146
    def test_annotate_cmd_two_revisions(self):
 
147
        out, err = self.run_bzr('annotate hello.txt -r1..2',
 
148
                                retcode=3)
 
149
        self.assertEqual('', out)
 
150
        self.assertEqual('brz: ERROR: brz annotate --revision takes'
 
151
                         ' exactly one revision identifier\n',
 
152
                         err)
 
153
 
 
154
 
 
155
class TestSimpleAnnotate(tests.TestCaseWithTransport):
 
156
    """Annotate tests with no complex setup."""
 
157
 
 
158
    def _setup_edited_file(self, relpath='.'):
 
159
        """Create a tree with a locally edited file."""
 
160
        tree = self.make_branch_and_tree(relpath)
 
161
        file_relpath = joinpath(relpath, 'file')
 
162
        self.build_tree_contents([(file_relpath, b'foo\ngam\n')])
 
163
        tree.add('file')
 
164
        tree.commit('add file', committer="test@host", rev_id=b"rev1")
 
165
        self.build_tree_contents([(file_relpath, b'foo\nbar\ngam\n')])
 
166
        return tree
 
167
 
 
168
    def test_annotate_cmd_revspec_branch(self):
 
169
        tree = self._setup_edited_file('trunk')
 
170
        tree.branch.create_checkout(self.get_url('work'), lightweight=True)
 
171
        out, err = self.run_bzr(['annotate', 'file', '-r', 'branch:../trunk'],
 
172
                                working_dir='work')
 
173
        self.assertEqual('', err)
 
174
        self.assertEqual(
 
175
            '1   test@ho | foo\n'
 
176
            '            | gam\n',
 
177
            out)
 
178
 
 
179
    def test_annotate_edited_file(self):
 
180
        tree = self._setup_edited_file()
 
181
        self.overrideEnv('BRZ_EMAIL', 'current@host2')
 
182
        out, err = self.run_bzr('annotate file')
 
183
        self.assertEqual(
 
184
            '1   test@ho | foo\n'
 
185
            '2?  current | bar\n'
 
186
            '1   test@ho | gam\n',
 
187
            out)
 
188
 
 
189
    def test_annotate_edited_file_no_default(self):
 
190
        # Ensure that when no username is available annotate still works.
 
191
        override_whoami(self)
 
192
        tree = self._setup_edited_file()
 
193
        out, err = self.run_bzr('annotate file')
 
194
        self.assertEqual(
 
195
            '1   test@ho | foo\n'
 
196
            '2?  local u | bar\n'
 
197
            '1   test@ho | gam\n',
 
198
            out)
 
199
 
 
200
    def test_annotate_edited_file_show_ids(self):
 
201
        tree = self._setup_edited_file()
 
202
        self.overrideEnv('BRZ_EMAIL', 'current@host2')
 
203
        out, err = self.run_bzr('annotate file --show-ids')
 
204
        self.assertEqual(
 
205
            '    rev1 | foo\n'
 
206
            'current: | bar\n'
 
207
            '    rev1 | gam\n',
 
208
            out)
 
209
 
 
210
    def _create_merged_file(self):
 
211
        """Create a file with a pending merge and local edit."""
 
212
        tree = self.make_branch_and_tree('.')
 
213
        self.build_tree_contents([('file', b'foo\ngam\n')])
 
214
        tree.add('file')
 
215
        tree.commit('add file', rev_id=b"rev1", committer="test@host")
 
216
        # right side
 
217
        self.build_tree_contents([('file', b'foo\nbar\ngam\n')])
 
218
        tree.commit("right", rev_id=b"rev1.1.1", committer="test@host")
 
219
        tree.pull(tree.branch, True, b"rev1")
 
220
        # left side
 
221
        self.build_tree_contents([('file', b'foo\nbaz\ngam\n')])
 
222
        tree.commit("left", rev_id=b"rev2", committer="test@host")
 
223
        # merge
 
224
        tree.merge_from_branch(tree.branch, b"rev1.1.1")
 
225
        # edit the file to be 'resolved' and have a further local edit
 
226
        self.build_tree_contents([('file', b'local\nfoo\nbar\nbaz\ngam\n')])
 
227
        return tree
 
228
 
 
229
    def test_annotated_edited_merged_file_revnos(self):
 
230
        wt = self._create_merged_file()
 
231
        out, err = self.run_bzr(['annotate', 'file'])
 
232
        email = config.extract_email_address(
 
233
            wt.branch.get_config_stack().get('email'))
 
234
        self.assertEqual(
 
235
            '3?    %-7s | local\n'
 
236
            '1     test@ho | foo\n'
 
237
            '1.1.1 test@ho | bar\n'
 
238
            '2     test@ho | baz\n'
 
239
            '1     test@ho | gam\n' % email[:7],
 
240
            out)
 
241
 
 
242
    def test_annotated_edited_merged_file_ids(self):
 
243
        self._create_merged_file()
 
244
        out, err = self.run_bzr(['annotate', 'file', '--show-ids'])
 
245
        self.assertEqual(
 
246
            'current: | local\n'
 
247
            '    rev1 | foo\n'
 
248
            'rev1.1.1 | bar\n'
 
249
            '    rev2 | baz\n'
 
250
            '    rev1 | gam\n',
 
251
            out)
 
252
 
 
253
    def test_annotate_empty_file(self):
 
254
        tree = self.make_branch_and_tree('.')
 
255
        self.build_tree_contents([('empty', b'')])
 
256
        tree.add('empty')
 
257
        tree.commit('add empty file')
 
258
        out, err = self.run_bzr(['annotate', 'empty'])
 
259
        self.assertEqual('', out)
 
260
 
 
261
    def test_annotate_removed_file(self):
 
262
        tree = self.make_branch_and_tree('.')
 
263
        self.build_tree_contents([('empty', b'')])
 
264
        tree.add('empty')
 
265
        tree.commit('add empty file')
 
266
        # delete the file.
 
267
        tree.remove('empty')
 
268
        tree.commit('remove empty file')
 
269
        out, err = self.run_bzr(['annotate', '-r1', 'empty'])
 
270
        self.assertEqual('', out)
 
271
 
 
272
    def test_annotate_empty_file_show_ids(self):
 
273
        tree = self.make_branch_and_tree('.')
 
274
        self.build_tree_contents([('empty', b'')])
 
275
        tree.add('empty')
 
276
        tree.commit('add empty file')
 
277
        out, err = self.run_bzr(['annotate', '--show-ids', 'empty'])
 
278
        self.assertEqual('', out)
 
279
 
 
280
    def test_annotate_nonexistant_file(self):
 
281
        tree = self.make_branch_and_tree('.')
 
282
        self.build_tree(['file'])
 
283
        tree.add(['file'])
 
284
        tree.commit('add a file')
 
285
        out, err = self.run_bzr(['annotate', 'doesnotexist'], retcode=3)
 
286
        self.assertEqual('', out)
 
287
        self.assertEqual("brz: ERROR: doesnotexist is not versioned.\n", err)
 
288
 
 
289
    def test_annotate_without_workingtree(self):
 
290
        tree = self.make_branch_and_tree('.')
 
291
        self.build_tree_contents([('empty', b'')])
 
292
        tree.add('empty')
 
293
        tree.commit('add empty file')
 
294
        bzrdir = tree.branch.controldir
 
295
        bzrdir.destroy_workingtree()
 
296
        self.assertFalse(bzrdir.has_workingtree())
 
297
        out, err = self.run_bzr(['annotate', 'empty'])
 
298
        self.assertEqual('', out)
 
299
 
 
300
    def test_annotate_directory(self):
 
301
        """Test --directory option"""
 
302
        wt = self.make_branch_and_tree('a')
 
303
        self.build_tree_contents([('a/hello.txt', b'my helicopter\n')])
 
304
        wt.add(['hello.txt'])
 
305
        wt.commit('commit', committer='test@user')
 
306
        out, err = self.run_bzr(['annotate', '-d', 'a', 'hello.txt'])
 
307
        self.assertEqualDiff('1   test@us | my helicopter\n', out)
 
308
 
 
309
 
 
310
class TestSmartServerAnnotate(tests.TestCaseWithTransport):
 
311
 
 
312
    def test_simple_annotate(self):
 
313
        self.setup_smart_server_with_call_log()
 
314
        wt = self.make_branch_and_tree('branch')
 
315
        self.build_tree_contents([('branch/hello.txt', b'my helicopter\n')])
 
316
        wt.add(['hello.txt'])
 
317
        wt.commit('commit', committer='test@user')
 
318
        self.reset_smart_call_log()
 
319
        out, err = self.run_bzr(['annotate', "-d", self.get_url('branch'),
 
320
                                 "hello.txt"])
 
321
        # This figure represent the amount of work to perform this use case. It
 
322
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
323
        # being too low. If rpc_count increases, more network roundtrips have
 
324
        # become necessary for this use case. Please do not adjust this number
 
325
        # upwards without agreement from bzr's network support maintainers.
 
326
        self.assertLength(9, self.hpss_calls)
 
327
        self.assertLength(1, self.hpss_connections)
 
328
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)