1
# Copyright (C) 2005-2010 Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
"""Black-box tests for bzr cat.
24
from bzrlib import tests
27
class TestCat(tests.TestCaseWithTransport):
30
tree = self.make_branch_and_tree('branch')
31
self.build_tree_contents([('branch/a', 'foo\n')])
34
# 'bzr cat' without an option should cat the last revision
35
self.run_bzr(['cat', 'a'], retcode=3)
37
tree.commit(message='1')
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')
44
tree.commit(message='2')
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],
53
rev_id = tree.branch.last_revision()
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],
64
self.run_bzr(['cat', 'a'], retcode=3)
66
['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
69
def test_cat_different_id(self):
70
"""'cat' works with old and new files"""
71
self.disable_missing_extensions_warning()
72
tree = self.make_branch_and_tree('.')
73
# the files are named after their path in the revision and
74
# current trees later in the test case
75
# a-rev-tree is special because it appears in both the revision
76
# tree and the working tree
77
self.build_tree_contents([('a-rev-tree', 'foo\n'),
78
('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
81
tree.add(['a-rev-tree', 'c-rev', 'd-rev', 'e-rev'])
82
tree.commit('add test files', rev_id='first')
83
# remove currently uses self._write_inventory -
84
# work around that for now.
86
tree.remove(['d-rev'])
87
tree.rename_one('a-rev-tree', 'b-tree')
88
tree.rename_one('c-rev', 'a-rev-tree')
89
tree.rename_one('e-rev', 'old-rev')
90
self.build_tree_contents([('e-rev', 'new\n')])
93
# calling bzr as another process require free lock on win32
96
# 'b-tree' is not present in the old tree.
97
self.run_bzr_error(["^bzr: ERROR: u?'b-tree' "
98
"is not present in revision .+$"],
99
'cat b-tree --name-from-revision')
101
# get to the old file automatically
102
out, err = self.run_bzr_subprocess('cat d-rev')
103
self.assertEqual('bar\n', out)
104
self.assertEqual('', err)
107
self.run_bzr_subprocess('cat a-rev-tree --name-from-revision')
108
self.assertEqual('foo\n', out)
109
self.assertEqual('', err)
111
out, err = self.run_bzr_subprocess('cat a-rev-tree')
112
self.assertEqual('baz\n', out)
113
self.assertEqual('', err)
115
# the actual file-id for e-rev doesn't exist in the old tree
116
out, err = self.run_bzr_subprocess('cat e-rev -rrevid:first')
117
self.assertEqual('qux\n', out)
118
self.assertEqual('', err)
120
def test_remote_cat(self):
121
wt = self.make_branch_and_tree('.')
122
self.build_tree(['README'])
124
wt.commit('Making sure there is a basis_tree available')
126
url = self.get_readonly_url() + '/README'
127
out, err = self.run_bzr_subprocess(['cat', url])
128
self.assertEqual('contents of README\n', out)
130
def test_cat_branch_revspec(self):
131
wt = self.make_branch_and_tree('a')
132
self.build_tree(['a/README'])
134
wt.commit('Making sure there is a basis_tree available')
135
wt = self.make_branch_and_tree('b')
138
out, err = self.run_bzr_subprocess(
139
['cat', '-r', 'branch:../a', 'README'])
140
self.assertEqual('contents of a/README\n', out)
142
def test_cat_filters(self):
143
wt = self.make_branch_and_tree('.')
144
self.build_tree(['README'])
146
wt.commit('Making sure there is a basis_tree available')
147
url = self.get_readonly_url() + '/README'
149
# Test unfiltered output
150
out, err = self.run_bzr_subprocess(['cat', url])
151
self.assertEqual('contents of README\n', out)
153
# Test --filters option is legal but has no impact if no filters
154
out, err = self.run_bzr_subprocess(['cat', '--filters', url])
155
self.assertEqual('contents of README\n', out)
157
def test_cat_filters_applied(self):
158
# Test filtering applied to output. This is tricky to do in a
159
# subprocess because we really need to patch in a plugin that
160
# registers the filters. Instead, we patch in a custom
161
# filter_stack and use run_bzr() ...
162
from cStringIO import StringIO
163
from bzrlib.commands import run_bzr
164
from bzrlib.tests.test_filters import _stack_2
165
from bzrlib.trace import mutter
166
from bzrlib.tree import Tree
167
wt = self.make_branch_and_tree('.')
168
self.build_tree_contents([
169
('README', "junk\nline 1 of README\nline 2 of README\n"),
172
wt.commit('Making sure there is a basis_tree available')
173
url = self.get_readonly_url() + '/README'
174
real_content_filter_stack = Tree._content_filter_stack
175
def _custom_content_filter_stack(tree, path=None, file_id=None):
177
Tree._content_filter_stack = _custom_content_filter_stack
179
out, err = self.run_bzr(['cat', url, '--filters'])
180
# The filter stack will remove the first line and swapcase the rest
181
self.assertEqual('LINE 1 OF readme\nLINE 2 OF readme\n', out)
182
self.assertEqual('', err)
184
Tree._content_filter_stack = real_content_filter_stack
186
def test_cat_no_working_tree(self):
187
wt = self.make_branch_and_tree('.')
188
self.build_tree(['README'])
190
wt.commit('Making sure there is a basis_tree available')
191
wt.branch.bzrdir.destroy_workingtree()
193
url = self.get_readonly_url() + '/README'
194
out, err = self.run_bzr_subprocess(['cat', url])
195
self.assertEqual('contents of README\n', out)
197
def test_cat_nonexistent_branch(self):
198
self.vfs_transport_factory = tests.MemoryServer
199
self.run_bzr_error(['^bzr: ERROR: Not a branch'],
200
['cat', self.get_url()])