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