14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
"""Black-box tests for bzr cat.
24
25
from bzrlib.tests.blackbox import TestCaseWithTransport
26
27
class TestCat(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
# 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],
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)
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'],
63
69
def test_cat_different_id(self):
64
70
"""'cat' works with old and new files"""
65
71
tree = self.make_branch_and_tree('.')
68
74
# a-rev-tree is special because it appears in both the revision
69
75
# tree and the working tree
70
76
self.build_tree_contents([('a-rev-tree', 'foo\n'),
71
('c-rev', 'baz\n'), ('d-rev', 'bar\n')])
77
('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 -
80
tree.add(['a-rev-tree', 'c-rev', 'd-rev', 'e-rev'])
81
tree.commit('add test files', rev_id='first')
82
# remove currently uses self._write_inventory -
77
83
# work around that for now.
79
85
tree.remove(['d-rev'])
80
86
tree.rename_one('a-rev-tree', 'b-tree')
81
87
tree.rename_one('c-rev', 'a-rev-tree')
88
tree.rename_one('e-rev', 'old-rev')
89
self.build_tree_contents([('e-rev', 'new\n')])
83
92
# calling bzr as another process require free lock on win32
89
98
'cat b-tree --name-from-revision')
91
100
# get to the old file automatically
92
out, err = self.run_bzr('cat d-rev')
101
out, err = self.run_bzr_subprocess('cat d-rev')
93
102
self.assertEqual('bar\n', out)
94
103
self.assertEqual('', err)
96
out, err = self.run_bzr('cat a-rev-tree --name-from-revision')
106
self.run_bzr_subprocess('cat a-rev-tree --name-from-revision')
97
107
self.assertEqual('foo\n', out)
98
108
self.assertEqual('', err)
100
out, err = self.run_bzr('cat a-rev-tree')
110
out, err = self.run_bzr_subprocess('cat a-rev-tree')
101
111
self.assertEqual('baz\n', out)
102
112
self.assertEqual('', err)
114
# the actual file-id for e-rev doesn't exist in the old tree
115
out, err = self.run_bzr_subprocess('cat e-rev -rrevid:first')
116
self.assertEqual('qux\n', out)
117
self.assertEqual('', err)
104
119
def test_remote_cat(self):
105
120
wt = self.make_branch_and_tree('.')
106
121
self.build_tree(['README'])
108
123
wt.commit('Making sure there is a basis_tree available')
110
125
url = self.get_readonly_url() + '/README'
111
out, err = self.run_bzr(['cat', url])
112
self.assertEqual('contents of README\n', out)
126
out, err = self.run_bzr_subprocess(['cat', url])
127
self.assertEqual('contents of README\n', out)
129
def test_cat_filters(self):
130
wt = self.make_branch_and_tree('.')
131
self.build_tree(['README'])
133
wt.commit('Making sure there is a basis_tree available')
134
url = self.get_readonly_url() + '/README'
136
# Test unfiltered output
137
out, err = self.run_bzr_subprocess(['cat', url])
138
self.assertEqual('contents of README\n', out)
140
# Test --filters option is legal but has no impact if no filters
141
out, err = self.run_bzr_subprocess(['cat', '--filters', url])
142
self.assertEqual('contents of README\n', out)
144
def test_cat_filters_applied(self):
145
# Test filtering applied to output. This is tricky to do in a
146
# subprocess because we really need to patch in a plugin that
147
# registers the filters. Instead, we patch in a custom
148
# filter_stack and use run_bzr() ...
149
from cStringIO import StringIO
150
from bzrlib.commands import run_bzr
151
from bzrlib.tests.test_filters import _stack_2
152
from bzrlib.trace import mutter
153
from bzrlib.tree import Tree
154
wt = self.make_branch_and_tree('.')
155
self.build_tree_contents([
156
('README', "junk\nline 1 of README\nline 2 of README\n"),
159
wt.commit('Making sure there is a basis_tree available')
160
url = self.get_readonly_url() + '/README'
161
real_content_filter_stack = Tree._content_filter_stack
162
def _custom_content_filter_stack(tree, path=None, file_id=None):
164
Tree._content_filter_stack = _custom_content_filter_stack
166
out, err = self.run_bzr(['cat', url, '--filters'])
167
# The filter stack will remove the first line and swapcase the rest
168
self.assertEqual('LINE 1 OF readme\nLINE 2 OF readme\n', out)
169
self.assertEqual('', err)
171
Tree._content_filter_stack = real_content_filter_stack
114
173
def test_cat_no_working_tree(self):
115
174
wt = self.make_branch_and_tree('.')
119
178
wt.branch.bzrdir.destroy_workingtree()
121
180
url = self.get_readonly_url() + '/README'
122
out, err = self.run_bzr(['cat', url])
181
out, err = self.run_bzr_subprocess(['cat', url])
123
182
self.assertEqual('contents of README\n', out)
184
def test_cat_nonexistent_branch(self):
185
if sys.platform == "win32":
186
location = "C:/i/do/not/exist"
188
location = "/i/do/not/exist"
189
self.run_bzr_error(['^bzr: ERROR: Not a branch'], ['cat', location])