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.tests.blackbox import TestCaseWithTransport
26
class TestCat(TestCaseWithTransport):
21
from bzrlib import tests
22
from bzrlib.tests.matchers import ContainsNoVfsCalls
23
from bzrlib.transport import memory
26
class TestCat(tests.TestCaseWithTransport):
28
28
def test_cat(self):
30
def bzr(*args, **kwargs):
31
return self.run_bzr_subprocess(*args, **kwargs)[0]
36
open('a', 'wb').write('foo\n')
29
tree = self.make_branch_and_tree('branch')
30
self.build_tree_contents([('branch/a', 'foo\n')])
39
32
# 'bzr cat' without an option should cat the last revision
40
bzr('cat', 'a', retcode=3)
42
bzr('commit', '-m', '1')
43
open('a', 'wb').write('baz\n')
45
self.assertEquals(bzr('cat', 'a'), 'foo\n')
47
bzr('commit', '-m', '2')
48
self.assertEquals(bzr('cat', 'a'), 'baz\n')
49
self.assertEquals(bzr('cat', 'a', '-r', '1'), 'foo\n')
50
self.assertEquals(bzr('cat', 'a', '-r', '-1'), 'baz\n')
52
rev_id = bzr('revision-history').strip().split('\n')[-1]
54
self.assertEquals(bzr('cat', 'a', '-r', 'revid:%s' % rev_id), 'baz\n')
58
self.assertEquals(bzr('cat', 'branch/a', '-r', 'revno:1:branch'),
60
bzr('cat', 'a', retcode=3)
61
bzr('cat', 'a', '-r', 'revno:1:branch-that-does-not-exist', retcode=3)
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'],
63
72
def test_cat_different_id(self):
64
73
"""'cat' works with old and new files"""
74
self.disable_missing_extensions_warning()
65
75
tree = self.make_branch_and_tree('.')
66
76
# the files are named after their path in the revision and
67
77
# current trees later in the test case
68
78
# a-rev-tree is special because it appears in both the revision
69
79
# tree and the working tree
70
80
self.build_tree_contents([('a-rev-tree', 'foo\n'),
71
('c-rev', 'baz\n'), ('d-rev', 'bar\n')])
81
('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
74
tree.add(['a-rev-tree', 'c-rev', 'd-rev'])
75
tree.commit('add test files')
76
# remove currently uses self._write_inventory -
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 -
77
87
# work around that for now.
79
89
tree.remove(['d-rev'])
80
90
tree.rename_one('a-rev-tree', 'b-tree')
81
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')])
83
96
# calling bzr as another process require free lock on win32
86
99
# 'b-tree' is not present in the old tree.
87
100
self.run_bzr_error(["^bzr: ERROR: u?'b-tree' "
88
101
"is not present in revision .+$"],
89
'cat', 'b-tree', '--name-from-revision')
102
'cat b-tree --name-from-revision')
91
104
# get to the old file automatically
92
out, err = self.run_bzr('cat', 'd-rev')
105
out, err = self.run_bzr('cat d-rev')
93
106
self.assertEqual('bar\n', out)
94
107
self.assertEqual('', err)
96
out, err = self.run_bzr('cat', 'a-rev-tree',
97
'--name-from-revision')
110
self.run_bzr('cat a-rev-tree --name-from-revision')
98
111
self.assertEqual('foo\n', out)
99
112
self.assertEqual('', err)
101
out, err = self.run_bzr('cat', 'a-rev-tree')
114
out, err = self.run_bzr('cat a-rev-tree')
102
115
self.assertEqual('baz\n', out)
103
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)
105
123
def test_remote_cat(self):
106
124
wt = self.make_branch_and_tree('.')
107
125
self.build_tree(['README'])
109
127
wt.commit('Making sure there is a basis_tree available')
111
129
url = self.get_readonly_url() + '/README'
112
out, err = self.run_bzr('cat', url)
113
self.assertEqual('contents of README\n', out)
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
115
188
def test_cat_no_working_tree(self):
116
189
wt = self.make_branch_and_tree('.')
120
193
wt.branch.bzrdir.destroy_workingtree()
122
195
url = self.get_readonly_url() + '/README'
123
out, err = self.run_bzr('cat', url)
196
out, err = self.run_bzr(['cat', url])
124
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)