24
from bzrlib.tests.blackbox import TestCaseWithTransport
26
class TestCat(TestCaseWithTransport):
23
from bzrlib import tests
24
from bzrlib.transport import memory
27
class TestCat(tests.TestCaseWithTransport):
28
29
def test_cat(self):
30
def bzr(*args, **kwargs):
31
return self.run_bzr_subprocess(*args, **kwargs)[0]
30
tree = self.make_branch_and_tree('branch')
31
self.build_tree_contents([('branch/a', 'foo\n')])
36
open('a', 'wb').write('foo\n')
39
34
# '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')
35
self.run_bzr(['cat', 'a'], retcode=3)
37
tree.commit(message='1')
38
self.build_tree_contents([('a', 'baz\n')])
40
self.assertEquals(self.run_bzr(['cat', 'a'])[0], 'foo\n')
42
# On Windows, we used to have a bug where newlines got changed into
43
# crlf, whereas cat ought to write out the file exactly as it's
44
# recorded (by default.) That problem can't be reproduced in-process,
45
# so we need just one test here that
46
self.assertEquals(self.run_bzr_subprocess(['cat', 'a'])[0], 'foo\n')
48
tree.commit(message='2')
49
self.assertEquals(self.run_bzr(['cat', 'a'])[0], 'baz\n')
50
self.assertEquals(self.run_bzr(
51
['cat', 'a', '-r', '1'])[0],
53
self.assertEquals(self.run_bzr(
54
['cat', 'a', '-r', '-1'])[0],
57
rev_id = tree.branch.last_revision()
59
self.assertEquals(self.run_bzr(
60
['cat', 'a', '-r', 'revid:%s' % rev_id])[0],
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)
65
self.assertEquals(self.run_bzr(
66
['cat', 'branch/a', '-r', 'revno:1:branch'])[0],
68
self.run_bzr(['cat', 'a'], retcode=3)
70
['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
63
73
def test_cat_different_id(self):
64
74
"""'cat' works with old and new files"""
75
self.disable_missing_extensions_warning()
65
76
tree = self.make_branch_and_tree('.')
66
77
# the files are named after their path in the revision and
67
78
# current trees later in the test case
68
79
# a-rev-tree is special because it appears in both the revision
69
80
# tree and the working tree
70
81
self.build_tree_contents([('a-rev-tree', 'foo\n'),
71
('c-rev', 'baz\n'), ('d-rev', 'bar\n')])
82
('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 -
85
tree.add(['a-rev-tree', 'c-rev', 'd-rev', 'e-rev'])
86
tree.commit('add test files', rev_id='first')
87
# remove currently uses self._write_inventory -
77
88
# work around that for now.
79
90
tree.remove(['d-rev'])
80
91
tree.rename_one('a-rev-tree', 'b-tree')
81
92
tree.rename_one('c-rev', 'a-rev-tree')
83
# 'b-tree' is not present in the old tree.
84
self.run_bzr_error([], 'cat', 'b-tree', '--name-from-revision')
86
# get to the old file automatically
87
out, err = self.run_bzr('cat', 'd-rev')
88
self.assertEqual('bar\n', out)
89
self.assertEqual('', err)
91
out, err = self.run_bzr('cat', 'a-rev-tree',
92
'--name-from-revision')
93
self.assertEqual('foo\n', out)
94
self.assertEqual('', err)
96
out, err = self.run_bzr('cat', 'a-rev-tree')
97
self.assertEqual('baz\n', out)
98
self.assertEqual('', err)
93
tree.rename_one('e-rev', 'old-rev')
94
self.build_tree_contents([('e-rev', 'new\n')])
97
# calling bzr as another process require free lock on win32
100
# 'b-tree' is not present in the old tree.
101
self.run_bzr_error(["^bzr: ERROR: u?'b-tree' "
102
"is not present in revision .+$"],
103
'cat b-tree --name-from-revision')
105
# get to the old file automatically
106
out, err = self.run_bzr('cat d-rev')
107
self.assertEqual('bar\n', out)
108
self.assertEqual('', err)
111
self.run_bzr('cat a-rev-tree --name-from-revision')
112
self.assertEqual('foo\n', out)
113
self.assertEqual('', err)
115
out, err = self.run_bzr('cat a-rev-tree')
116
self.assertEqual('baz\n', out)
117
self.assertEqual('', err)
119
# the actual file-id for e-rev doesn't exist in the old tree
120
out, err = self.run_bzr('cat e-rev -rrevid:first')
121
self.assertEqual('qux\n', out)
122
self.assertEqual('', err)
102
124
def test_remote_cat(self):
103
125
wt = self.make_branch_and_tree('.')
104
126
self.build_tree(['README'])
106
128
wt.commit('Making sure there is a basis_tree available')
108
130
url = self.get_readonly_url() + '/README'
109
out, err = self.run_bzr('cat', url)
110
self.assertEqual('contents of README\n', out)
131
out, err = self.run_bzr(['cat', url])
132
self.assertEqual('contents of README\n', out)
134
def test_cat_branch_revspec(self):
135
wt = self.make_branch_and_tree('a')
136
self.build_tree(['a/README'])
138
wt.commit('Making sure there is a basis_tree available')
139
wt = self.make_branch_and_tree('b')
142
out, err = self.run_bzr(
143
['cat', '-r', 'branch:../a', 'README'])
144
self.assertEqual('contents of a/README\n', out)
146
def test_cat_filters(self):
147
wt = self.make_branch_and_tree('.')
148
self.build_tree(['README'])
150
wt.commit('Making sure there is a basis_tree available')
151
url = self.get_readonly_url() + '/README'
153
# Test unfiltered output
154
out, err = self.run_bzr(['cat', url])
155
self.assertEqual('contents of README\n', out)
157
# Test --filters option is legal but has no impact if no filters
158
out, err = self.run_bzr(['cat', '--filters', url])
159
self.assertEqual('contents of README\n', out)
161
def test_cat_filters_applied(self):
162
# Test filtering applied to output. This is tricky to do in a
163
# subprocess because we really need to patch in a plugin that
164
# registers the filters. Instead, we patch in a custom
165
# filter_stack and use run_bzr() ...
166
from cStringIO import StringIO
167
from bzrlib.commands import run_bzr
168
from bzrlib.tests.test_filters import _stack_2
169
from bzrlib.trace import mutter
170
from bzrlib.tree import Tree
171
wt = self.make_branch_and_tree('.')
172
self.build_tree_contents([
173
('README', "junk\nline 1 of README\nline 2 of README\n"),
176
wt.commit('Making sure there is a basis_tree available')
177
url = self.get_readonly_url() + '/README'
178
real_content_filter_stack = Tree._content_filter_stack
179
def _custom_content_filter_stack(tree, path=None, file_id=None):
181
Tree._content_filter_stack = _custom_content_filter_stack
183
out, err = self.run_bzr(['cat', url, '--filters'])
184
# The filter stack will remove the first line and swapcase the rest
185
self.assertEqual('LINE 1 OF readme\nLINE 2 OF readme\n', out)
186
self.assertEqual('', err)
188
Tree._content_filter_stack = real_content_filter_stack
112
190
def test_cat_no_working_tree(self):
113
191
wt = self.make_branch_and_tree('.')
117
195
wt.branch.bzrdir.destroy_workingtree()
119
197
url = self.get_readonly_url() + '/README'
120
out, err = self.run_bzr('cat', url)
198
out, err = self.run_bzr(['cat', url])
121
199
self.assertEqual('contents of README\n', out)
201
def test_cat_nonexistent_branch(self):
202
self.vfs_transport_factory = memory.MemoryServer
203
self.run_bzr_error(['^bzr: ERROR: Not a branch'],
204
['cat', self.get_url()])
206
def test_cat_directory(self):
207
wt = self.make_branch_and_tree('a')
208
self.build_tree(['a/README'])
210
wt.commit('Making sure there is a basis_tree available')
212
out, err = self.run_bzr(['cat', '--directory=a', 'README'])
213
self.assertEqual('contents of a/README\n', out)
215
def test_cat_remote_directory(self):
216
wt = self.make_branch_and_tree('a')
217
self.build_tree(['a/README'])
219
wt.commit('Making sure there is a basis_tree available')
221
url = self.get_readonly_url() + '/a'
222
out, err = self.run_bzr(['cat', '-d', url, 'README'])
223
self.assertEqual('contents of a/README\n', out)