/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 bzrlib/tests/blackbox/test_cat.py

(jelmer) Convert bzrlib.smtp_connection to use config stacks. (Jelmer
 Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
"""Black-box tests for bzr cat.
 
19
"""
 
20
 
 
21
import os
 
22
 
 
23
from bzrlib import tests
 
24
from bzrlib.tests.matchers import ContainsNoVfsCalls
 
25
from bzrlib.transport import memory
 
26
 
 
27
 
 
28
class TestCat(tests.TestCaseWithTransport):
 
29
 
 
30
    def test_cat(self):
 
31
        tree = self.make_branch_and_tree('branch')
 
32
        self.build_tree_contents([('branch/a', 'foo\n')])
 
33
        tree.add('a')
 
34
        os.chdir('branch')
 
35
        # 'bzr cat' without an option should cat the last revision
 
36
        self.run_bzr(['cat', 'a'], retcode=3)
 
37
 
 
38
        tree.commit(message='1')
 
39
        self.build_tree_contents([('a', 'baz\n')])
 
40
 
 
41
        self.assertEquals(self.run_bzr(['cat', 'a'])[0], 'foo\n')
 
42
 
 
43
        # On Windows, we used to have a bug where newlines got changed into
 
44
        # crlf, whereas cat ought to write out the file exactly as it's
 
45
        # recorded (by default.)  That problem can't be reproduced in-process,
 
46
        # so we need just one test here that 
 
47
        self.assertEquals(self.run_bzr_subprocess(['cat', 'a'])[0], 'foo\n')
 
48
 
 
49
        tree.commit(message='2')
 
50
        self.assertEquals(self.run_bzr(['cat', 'a'])[0], 'baz\n')
 
51
        self.assertEquals(self.run_bzr(
 
52
            ['cat', 'a', '-r', '1'])[0],
 
53
            'foo\n')
 
54
        self.assertEquals(self.run_bzr(
 
55
            ['cat', 'a', '-r', '-1'])[0],
 
56
            'baz\n')
 
57
 
 
58
        rev_id = tree.branch.last_revision()
 
59
 
 
60
        self.assertEquals(self.run_bzr(
 
61
            ['cat', 'a', '-r', 'revid:%s' % rev_id])[0],
 
62
            'baz\n')
 
63
 
 
64
        os.chdir('..')
 
65
 
 
66
        self.assertEquals(self.run_bzr(
 
67
            ['cat', 'branch/a', '-r', 'revno:1:branch'])[0],
 
68
            'foo\n')
 
69
        self.run_bzr(['cat', 'a'], retcode=3)
 
70
        self.run_bzr(
 
71
                ['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
 
72
                retcode=3)
 
73
 
 
74
    def test_cat_different_id(self):
 
75
        """'cat' works with old and new files"""
 
76
        self.disable_missing_extensions_warning()
 
77
        tree = self.make_branch_and_tree('.')
 
78
        # the files are named after their path in the revision and
 
79
        # current trees later in the test case
 
80
        # a-rev-tree is special because it appears in both the revision
 
81
        # tree and the working tree
 
82
        self.build_tree_contents([('a-rev-tree', 'foo\n'),
 
83
            ('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
 
84
        tree.lock_write()
 
85
        try:
 
86
            tree.add(['a-rev-tree', 'c-rev', 'd-rev', 'e-rev'])
 
87
            tree.commit('add test files', rev_id='first')
 
88
            # remove currently uses self._write_inventory -
 
89
            # work around that for now.
 
90
            tree.flush()
 
91
            tree.remove(['d-rev'])
 
92
            tree.rename_one('a-rev-tree', 'b-tree')
 
93
            tree.rename_one('c-rev', 'a-rev-tree')
 
94
            tree.rename_one('e-rev', 'old-rev')
 
95
            self.build_tree_contents([('e-rev', 'new\n')])
 
96
            tree.add(['e-rev'])
 
97
        finally:
 
98
            # calling bzr as another process require free lock on win32
 
99
            tree.unlock()
 
100
 
 
101
        # 'b-tree' is not present in the old tree.
 
102
        self.run_bzr_error(["^bzr: ERROR: u?'b-tree' "
 
103
                            "is not present in revision .+$"],
 
104
                           'cat b-tree --name-from-revision')
 
105
 
 
106
        # get to the old file automatically
 
107
        out, err = self.run_bzr('cat d-rev')
 
108
        self.assertEqual('bar\n', out)
 
109
        self.assertEqual('', err)
 
110
 
 
111
        out, err = \
 
112
                self.run_bzr('cat a-rev-tree --name-from-revision')
 
113
        self.assertEqual('foo\n', out)
 
114
        self.assertEqual('', err)
 
115
 
 
116
        out, err = self.run_bzr('cat a-rev-tree')
 
117
        self.assertEqual('baz\n', out)
 
118
        self.assertEqual('', err)
 
119
 
 
120
        # the actual file-id for e-rev doesn't exist in the old tree
 
121
        out, err = self.run_bzr('cat e-rev -rrevid:first')
 
122
        self.assertEqual('qux\n', out)
 
123
        self.assertEqual('', err)
 
124
 
 
125
    def test_remote_cat(self):
 
126
        wt = self.make_branch_and_tree('.')
 
127
        self.build_tree(['README'])
 
128
        wt.add('README')
 
129
        wt.commit('Making sure there is a basis_tree available')
 
130
 
 
131
        url = self.get_readonly_url() + '/README'
 
132
        out, err = self.run_bzr(['cat', url])
 
133
        self.assertEqual('contents of README\n', out)
 
134
 
 
135
    def test_cat_branch_revspec(self):
 
136
        wt = self.make_branch_and_tree('a')
 
137
        self.build_tree(['a/README'])
 
138
        wt.add('README')
 
139
        wt.commit('Making sure there is a basis_tree available')
 
140
        wt = self.make_branch_and_tree('b')
 
141
        os.chdir('b')
 
142
 
 
143
        out, err = self.run_bzr(
 
144
            ['cat', '-r', 'branch:../a', 'README'])
 
145
        self.assertEqual('contents of a/README\n', out)
 
146
 
 
147
    def test_cat_filters(self):
 
148
        wt = self.make_branch_and_tree('.')
 
149
        self.build_tree(['README'])
 
150
        wt.add('README')
 
151
        wt.commit('Making sure there is a basis_tree available')
 
152
        url = self.get_readonly_url() + '/README'
 
153
 
 
154
        # Test unfiltered output
 
155
        out, err = self.run_bzr(['cat', url])
 
156
        self.assertEqual('contents of README\n', out)
 
157
 
 
158
        # Test --filters option is legal but has no impact if no filters
 
159
        out, err = self.run_bzr(['cat', '--filters', url])
 
160
        self.assertEqual('contents of README\n', out)
 
161
 
 
162
    def test_cat_filters_applied(self):
 
163
        # Test filtering applied to output. This is tricky to do in a
 
164
        # subprocess because we really need to patch in a plugin that
 
165
        # registers the filters. Instead, we patch in a custom
 
166
        # filter_stack and use run_bzr() ...
 
167
        from cStringIO import StringIO
 
168
        from bzrlib.commands import run_bzr
 
169
        from bzrlib.tests.test_filters import _stack_2
 
170
        from bzrlib.trace import mutter
 
171
        from bzrlib.tree import Tree
 
172
        wt = self.make_branch_and_tree('.')
 
173
        self.build_tree_contents([
 
174
            ('README', "junk\nline 1 of README\nline 2 of README\n"),
 
175
            ])
 
176
        wt.add('README')
 
177
        wt.commit('Making sure there is a basis_tree available')
 
178
        url = self.get_readonly_url() + '/README'
 
179
        real_content_filter_stack = Tree._content_filter_stack
 
180
        def _custom_content_filter_stack(tree, path=None, file_id=None):
 
181
            return _stack_2
 
182
        Tree._content_filter_stack = _custom_content_filter_stack
 
183
        try:
 
184
            out, err = self.run_bzr(['cat', url, '--filters'])
 
185
            # The filter stack will remove the first line and swapcase the rest
 
186
            self.assertEqual('LINE 1 OF readme\nLINE 2 OF readme\n', out)
 
187
            self.assertEqual('', err)
 
188
        finally:
 
189
            Tree._content_filter_stack = real_content_filter_stack
 
190
 
 
191
    def test_cat_no_working_tree(self):
 
192
        wt = self.make_branch_and_tree('.')
 
193
        self.build_tree(['README'])
 
194
        wt.add('README')
 
195
        wt.commit('Making sure there is a basis_tree available')
 
196
        wt.branch.bzrdir.destroy_workingtree()
 
197
 
 
198
        url = self.get_readonly_url() + '/README'
 
199
        out, err = self.run_bzr(['cat', url])
 
200
        self.assertEqual('contents of README\n', out)
 
201
 
 
202
    def test_cat_nonexistent_branch(self):
 
203
        self.vfs_transport_factory = memory.MemoryServer
 
204
        self.run_bzr_error(['^bzr: ERROR: Not a branch'],
 
205
                           ['cat', self.get_url()])
 
206
 
 
207
    def test_cat_directory(self):
 
208
        wt = self.make_branch_and_tree('a')
 
209
        self.build_tree(['a/README'])
 
210
        wt.add('README')
 
211
        wt.commit('Making sure there is a basis_tree available')
 
212
 
 
213
        out, err = self.run_bzr(['cat', '--directory=a', 'README'])
 
214
        self.assertEqual('contents of a/README\n', out)
 
215
 
 
216
    def test_cat_remote_directory(self):
 
217
        wt = self.make_branch_and_tree('a')
 
218
        self.build_tree(['a/README'])
 
219
        wt.add('README')
 
220
        wt.commit('Making sure there is a basis_tree available')
 
221
 
 
222
        url = self.get_readonly_url() + '/a'
 
223
        out, err = self.run_bzr(['cat', '-d', url, 'README'])
 
224
        self.assertEqual('contents of a/README\n', out)
 
225
 
 
226
 
 
227
class TestSmartServerCat(tests.TestCaseWithTransport):
 
228
 
 
229
    def test_simple_branch_cat(self):
 
230
        self.setup_smart_server_with_call_log()
 
231
        t = self.make_branch_and_tree('branch')
 
232
        self.build_tree_contents([('branch/foo', 'thecontents')])
 
233
        t.add("foo")
 
234
        t.commit("message")
 
235
        self.reset_smart_call_log()
 
236
        out, err = self.run_bzr(['cat', "%s/foo" % self.get_url('branch')])
 
237
        # This figure represent the amount of work to perform this use case. It
 
238
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
239
        # being too low. If rpc_count increases, more network roundtrips have
 
240
        # become necessary for this use case. Please do not adjust this number
 
241
        # upwards without agreement from bzr's network support maintainers.
 
242
        self.assertLength(9, self.hpss_calls)
 
243
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)