13
14
# You should have received a copy of the GNU General Public License
14
15
# 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
"""Black-box tests for bzr cat.
21
from bzrlib import tests
22
from bzrlib.tests.matchers import ContainsNoVfsCalls
23
from bzrlib.transport import memory
26
class TestCat(tests.TestCaseWithTransport):
25
from bzrlib.tests.blackbox import TestCaseWithTransport
27
class TestCat(TestCaseWithTransport):
28
29
def test_cat(self):
29
30
tree = self.make_branch_and_tree('branch')
30
31
self.build_tree_contents([('branch/a', 'foo\n')])
32
34
# 'bzr cat' without an option should cat the last revision
33
self.run_bzr(['cat', 'a'], retcode=3, working_dir='branch')
35
self.run_bzr(['cat', 'a'], retcode=3)
35
37
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])
38
self.build_tree_contents([('a', 'baz\n')])
40
# We use run_bzr_subprocess rather than run_bzr here so that we can
41
# test mangling of line-endings on Windows.
42
self.assertEquals(self.run_bzr_subprocess(['cat', 'a'])[0], 'foo\n')
49
44
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])
45
self.assertEquals(self.run_bzr_subprocess(['cat', 'a'])[0], 'baz\n')
46
self.assertEquals(self.run_bzr_subprocess(
47
['cat', 'a', '-r', '1'])[0],
49
self.assertEquals(self.run_bzr_subprocess(
50
['cat', 'a', '-r', '-1'])[0],
59
53
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])
55
self.assertEquals(self.run_bzr_subprocess(
56
['cat', 'a', '-r', 'revid:%s' % rev_id])[0],
61
self.assertEquals(self.run_bzr_subprocess(
62
['cat', 'branch/a', '-r', 'revno:1:branch'])[0],
68
64
self.run_bzr(['cat', 'a'], retcode=3)
69
self.run_bzr(['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
66
['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
72
69
def test_cat_different_id(self):
73
70
"""'cat' works with old and new files"""
74
self.disable_missing_extensions_warning()
75
71
tree = self.make_branch_and_tree('.')
76
72
# the files are named after their path in the revision and
77
73
# current trees later in the test case
78
74
# a-rev-tree is special because it appears in both the revision
79
75
# tree and the working tree
80
76
self.build_tree_contents([('a-rev-tree', 'foo\n'),
81
('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
77
('c-rev', 'baz\n'), ('d-rev', 'bar\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 -
80
tree.add(['a-rev-tree', 'c-rev', 'd-rev'])
81
tree.commit('add test files')
82
# remove currently uses self._write_inventory -
87
83
# work around that for now.
89
85
tree.remove(['d-rev'])
90
86
tree.rename_one('a-rev-tree', 'b-tree')
91
87
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
89
# calling bzr as another process require free lock on win32
102
95
'cat b-tree --name-from-revision')
104
97
# get to the old file automatically
105
out, err = self.run_bzr('cat d-rev')
98
out, err = self.run_bzr_subprocess('cat d-rev')
106
99
self.assertEqual('bar\n', out)
107
100
self.assertEqual('', err)
110
self.run_bzr('cat a-rev-tree --name-from-revision')
103
self.run_bzr_subprocess('cat a-rev-tree --name-from-revision')
111
104
self.assertEqual('foo\n', out)
112
105
self.assertEqual('', err)
114
out, err = self.run_bzr('cat a-rev-tree')
107
out, err = self.run_bzr_subprocess('cat a-rev-tree')
115
108
self.assertEqual('baz\n', out)
116
109
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
111
def test_remote_cat(self):
124
112
wt = self.make_branch_and_tree('.')
125
113
self.build_tree(['README'])
127
115
wt.commit('Making sure there is a basis_tree available')
129
117
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
118
out, err = self.run_bzr_subprocess(['cat', url])
119
self.assertEqual('contents of README\n', out)
188
121
def test_cat_no_working_tree(self):
189
122
wt = self.make_branch_and_tree('.')
193
126
wt.branch.bzrdir.destroy_workingtree()
195
128
url = self.get_readonly_url() + '/README'
196
out, err = self.run_bzr(['cat', url])
129
out, err = self.run_bzr_subprocess(['cat', url])
197
130
self.assertEqual('contents of README\n', out)
199
132
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)
133
if sys.platform == "win32":
134
location = "C:/i/do/not/exist"
136
location = "/i/do/not/exist"
137
self.run_bzr_error(['^bzr: ERROR: Not a branch'], ['cat', location])