24
from bzrlib import tests
25
from bzrlib.transport import memory
28
class TestCat(tests.TestCaseWithTransport):
24
from bzrlib.tests.blackbox import TestCaseWithTransport
26
class TestCat(TestCaseWithTransport):
30
28
def test_cat(self):
31
tree = self.make_branch_and_tree('branch')
32
self.build_tree_contents([('branch/a', 'foo\n')])
30
def bzr(*args, **kwargs):
31
return self.run_bzr_subprocess(*args, **kwargs)[0]
36
open('a', 'wb').write('foo\n')
35
39
# 'bzr cat' without an option should cat the last revision
36
self.run_bzr(['cat', 'a'], retcode=3)
38
tree.commit(message='1')
39
self.build_tree_contents([('a', 'baz\n')])
41
# We use run_bzr_subprocess rather than run_bzr here so that we can
42
# test mangling of line-endings on Windows.
43
self.assertEquals(self.run_bzr_subprocess(['cat', 'a'])[0], 'foo\n')
45
tree.commit(message='2')
46
self.assertEquals(self.run_bzr_subprocess(['cat', 'a'])[0], 'baz\n')
47
self.assertEquals(self.run_bzr_subprocess(
48
['cat', 'a', '-r', '1'])[0],
50
self.assertEquals(self.run_bzr_subprocess(
51
['cat', 'a', '-r', '-1'])[0],
54
rev_id = tree.branch.last_revision()
56
self.assertEquals(self.run_bzr_subprocess(
57
['cat', 'a', '-r', 'revid:%s' % rev_id])[0],
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')
62
self.assertEquals(self.run_bzr_subprocess(
63
['cat', 'branch/a', '-r', 'revno:1:branch'])[0],
65
self.run_bzr(['cat', 'a'], retcode=3)
67
['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
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)
70
63
def test_cat_different_id(self):
71
64
"""'cat' works with old and new files"""
72
self.disable_missing_extensions_warning()
73
65
tree = self.make_branch_and_tree('.')
74
66
# the files are named after their path in the revision and
75
67
# current trees later in the test case
76
68
# a-rev-tree is special because it appears in both the revision
77
69
# tree and the working tree
78
70
self.build_tree_contents([('a-rev-tree', 'foo\n'),
79
('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
71
('c-rev', 'baz\n'), ('d-rev', 'bar\n')])
82
tree.add(['a-rev-tree', 'c-rev', 'd-rev', 'e-rev'])
83
tree.commit('add test files', rev_id='first')
84
# remove currently uses self._write_inventory -
74
tree.add(['a-rev-tree', 'c-rev', 'd-rev'])
75
tree.commit('add test files')
76
# remove currently uses self._write_inventory -
85
77
# work around that for now.
87
79
tree.remove(['d-rev'])
88
80
tree.rename_one('a-rev-tree', 'b-tree')
89
81
tree.rename_one('c-rev', 'a-rev-tree')
90
tree.rename_one('e-rev', 'old-rev')
91
self.build_tree_contents([('e-rev', 'new\n')])
94
83
# calling bzr as another process require free lock on win32
97
86
# 'b-tree' is not present in the old tree.
98
87
self.run_bzr_error(["^bzr: ERROR: u?'b-tree' "
99
88
"is not present in revision .+$"],
100
'cat b-tree --name-from-revision')
89
'cat', 'b-tree', '--name-from-revision')
102
91
# get to the old file automatically
103
out, err = self.run_bzr_subprocess('cat d-rev')
92
out, err = self.run_bzr('cat', 'd-rev')
104
93
self.assertEqual('bar\n', out)
105
94
self.assertEqual('', err)
108
self.run_bzr_subprocess('cat a-rev-tree --name-from-revision')
96
out, err = self.run_bzr('cat', 'a-rev-tree',
97
'--name-from-revision')
109
98
self.assertEqual('foo\n', out)
110
99
self.assertEqual('', err)
112
out, err = self.run_bzr_subprocess('cat a-rev-tree')
101
out, err = self.run_bzr('cat', 'a-rev-tree')
113
102
self.assertEqual('baz\n', out)
114
103
self.assertEqual('', err)
116
# the actual file-id for e-rev doesn't exist in the old tree
117
out, err = self.run_bzr_subprocess('cat e-rev -rrevid:first')
118
self.assertEqual('qux\n', out)
119
self.assertEqual('', err)
121
105
def test_remote_cat(self):
122
106
wt = self.make_branch_and_tree('.')
123
107
self.build_tree(['README'])
125
109
wt.commit('Making sure there is a basis_tree available')
127
111
url = self.get_readonly_url() + '/README'
128
out, err = self.run_bzr_subprocess(['cat', url])
129
self.assertEqual('contents of README\n', out)
131
def test_cat_branch_revspec(self):
132
wt = self.make_branch_and_tree('a')
133
self.build_tree(['a/README'])
135
wt.commit('Making sure there is a basis_tree available')
136
wt = self.make_branch_and_tree('b')
139
out, err = self.run_bzr_subprocess(
140
['cat', '-r', 'branch:../a', 'README'])
141
self.assertEqual('contents of a/README\n', out)
143
def test_cat_filters(self):
144
wt = self.make_branch_and_tree('.')
145
self.build_tree(['README'])
147
wt.commit('Making sure there is a basis_tree available')
148
url = self.get_readonly_url() + '/README'
150
# Test unfiltered output
151
out, err = self.run_bzr_subprocess(['cat', url])
152
self.assertEqual('contents of README\n', out)
154
# Test --filters option is legal but has no impact if no filters
155
out, err = self.run_bzr_subprocess(['cat', '--filters', url])
156
self.assertEqual('contents of README\n', out)
158
def test_cat_filters_applied(self):
159
# Test filtering applied to output. This is tricky to do in a
160
# subprocess because we really need to patch in a plugin that
161
# registers the filters. Instead, we patch in a custom
162
# filter_stack and use run_bzr() ...
163
from cStringIO import StringIO
164
from bzrlib.commands import run_bzr
165
from bzrlib.tests.test_filters import _stack_2
166
from bzrlib.trace import mutter
167
from bzrlib.tree import Tree
168
wt = self.make_branch_and_tree('.')
169
self.build_tree_contents([
170
('README', "junk\nline 1 of README\nline 2 of README\n"),
173
wt.commit('Making sure there is a basis_tree available')
174
url = self.get_readonly_url() + '/README'
175
real_content_filter_stack = Tree._content_filter_stack
176
def _custom_content_filter_stack(tree, path=None, file_id=None):
178
Tree._content_filter_stack = _custom_content_filter_stack
180
out, err = self.run_bzr(['cat', url, '--filters'])
181
# The filter stack will remove the first line and swapcase the rest
182
self.assertEqual('LINE 1 OF readme\nLINE 2 OF readme\n', out)
183
self.assertEqual('', err)
185
Tree._content_filter_stack = real_content_filter_stack
112
out, err = self.run_bzr('cat', url)
113
self.assertEqual('contents of README\n', out)
187
115
def test_cat_no_working_tree(self):
188
116
wt = self.make_branch_and_tree('.')