~bzr-pqm/bzr/bzr.dev

5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
1
# Copyright (C) 2005-2011 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
16
17
18
"""Black-box tests for bzr cat.
19
"""
20
21
import os
22
4702.2.1 by Vincent Ladeuil
Remove bogus config imports left in previous tweak
23
from bzrlib import tests
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
24
from bzrlib.transport import memory
4695.3.1 by Vincent Ladeuil
Fix test failures with no C extensions loaded.
25
26
27
class TestCat(tests.TestCaseWithTransport):
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
28
29
    def test_cat(self):
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
30
        tree = self.make_branch_and_tree('branch')
31
        self.build_tree_contents([('branch/a', 'foo\n')])
32
        tree.add('a')
1907.4.3 by Matthieu Moy
bzr cat now works nicely with revno:N:path too
33
        os.chdir('branch')
1185.50.9 by John Arbash Meinel
[bug 3632] Matthieu Moy- bzr cat should default to last revision
34
        # 'bzr cat' without an option should cat the last revision
2738.4.7 by Martin Pool
Fix up calls to run_bzr from test_cat
35
        self.run_bzr(['cat', 'a'], retcode=3)
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
36
37
        tree.commit(message='1')
38
        self.build_tree_contents([('a', 'baz\n')])
39
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
40
        self.assertEquals(self.run_bzr(['cat', 'a'])[0], 'foo\n')
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
41
5967.10.2 by Martin Pool
Re-add one out-of-process test for cat on Windows
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')
47
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
48
        tree.commit(message='2')
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
49
        self.assertEquals(self.run_bzr(['cat', 'a'])[0], 'baz\n')
50
        self.assertEquals(self.run_bzr(
2738.4.7 by Martin Pool
Fix up calls to run_bzr from test_cat
51
            ['cat', 'a', '-r', '1'])[0],
52
            'foo\n')
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
53
        self.assertEquals(self.run_bzr(
2738.4.7 by Martin Pool
Fix up calls to run_bzr from test_cat
54
            ['cat', 'a', '-r', '-1'])[0],
55
            'baz\n')
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
56
2738.4.1 by Daniel Watkins
Merged original changes and tweaked cat.
57
        rev_id = tree.branch.last_revision()
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
58
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
59
        self.assertEquals(self.run_bzr(
2738.4.7 by Martin Pool
Fix up calls to run_bzr from test_cat
60
            ['cat', 'a', '-r', 'revid:%s' % rev_id])[0],
61
            'baz\n')
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
62
1907.4.3 by Matthieu Moy
bzr cat now works nicely with revno:N:path too
63
        os.chdir('..')
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
64
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
65
        self.assertEquals(self.run_bzr(
2738.4.7 by Martin Pool
Fix up calls to run_bzr from test_cat
66
            ['cat', 'branch/a', '-r', 'revno:1:branch'])[0],
67
            'foo\n')
68
        self.run_bzr(['cat', 'a'], retcode=3)
69
        self.run_bzr(
70
                ['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
71
                retcode=3)
2664.13.1 by Daniel Watkins
tests.blackbox.test_cat now uses internals where appropriate.
72
2073.2.4 by wang
Fix syntax error in test.
73
    def test_cat_different_id(self):
2073.2.1 by wang
``bzr cat`` can look up contents of removed or renamed files. If the
74
        """'cat' works with old and new files"""
4695.3.2 by Vincent Ladeuil
Simplified and claried as per Robert's review.
75
        self.disable_missing_extensions_warning()
2073.2.4 by wang
Fix syntax error in test.
76
        tree = self.make_branch_and_tree('.')
2073.2.3 by wang
Change option name to --name-from-revision. Always make new tree the
77
        # the files are named after their path in the revision and
78
        # current trees later in the test case
79
        # a-rev-tree is special because it appears in both the revision
80
        # tree and the working tree
81
        self.build_tree_contents([('a-rev-tree', 'foo\n'),
4112.1.1 by Vincent Ladeuil
Fallback to old revision id if the current one doesn't exist in
82
            ('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
2073.2.3 by wang
Change option name to --name-from-revision. Always make new tree the
83
        tree.lock_write()
84
        try:
4112.1.1 by Vincent Ladeuil
Fallback to old revision id if the current one doesn't exist in
85
            tree.add(['a-rev-tree', 'c-rev', 'd-rev', 'e-rev'])
86
            tree.commit('add test files', rev_id='first')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
87
            # remove currently uses self._write_inventory -
2255.7.70 by Robert Collins
Workaround WorkingTree4 not having a native remove() in test_cat.
88
            # work around that for now.
89
            tree.flush()
2073.2.3 by wang
Change option name to --name-from-revision. Always make new tree the
90
            tree.remove(['d-rev'])
91
            tree.rename_one('a-rev-tree', 'b-tree')
92
            tree.rename_one('c-rev', 'a-rev-tree')
4112.1.1 by Vincent Ladeuil
Fallback to old revision id if the current one doesn't exist in
93
            tree.rename_one('e-rev', 'old-rev')
94
            self.build_tree_contents([('e-rev', 'new\n')])
95
            tree.add(['e-rev'])
2073.2.3 by wang
Change option name to --name-from-revision. Always make new tree the
96
        finally:
2379.6.1 by Alexander Belchenko
blackbox: test_cat_different_id: calling bzr as another process require free lock on win32
97
            # calling bzr as another process require free lock on win32
2073.2.3 by wang
Change option name to --name-from-revision. Always make new tree the
98
            tree.unlock()
99
2379.6.1 by Alexander Belchenko
blackbox: test_cat_different_id: 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 .+$"],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
103
                           'cat b-tree --name-from-revision')
2379.6.1 by Alexander Belchenko
blackbox: test_cat_different_id: calling bzr as another process require free lock on win32
104
105
        # get to the old file automatically
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
106
        out, err = self.run_bzr('cat d-rev')
2379.6.1 by Alexander Belchenko
blackbox: test_cat_different_id: calling bzr as another process require free lock on win32
107
        self.assertEqual('bar\n', out)
108
        self.assertEqual('', err)
109
2738.4.6 by Daniel Watkins
Rewrapped lines longer than 79 characters.
110
        out, err = \
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
111
                self.run_bzr('cat a-rev-tree --name-from-revision')
2379.6.1 by Alexander Belchenko
blackbox: test_cat_different_id: calling bzr as another process require free lock on win32
112
        self.assertEqual('foo\n', out)
113
        self.assertEqual('', err)
114
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
115
        out, err = self.run_bzr('cat a-rev-tree')
2379.6.1 by Alexander Belchenko
blackbox: test_cat_different_id: calling bzr as another process require free lock on win32
116
        self.assertEqual('baz\n', out)
117
        self.assertEqual('', err)
118
4112.1.1 by Vincent Ladeuil
Fallback to old revision id if the current one doesn't exist in
119
        # the actual file-id for e-rev doesn't exist in the old tree
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
120
        out, err = self.run_bzr('cat e-rev -rrevid:first')
4112.1.1 by Vincent Ladeuil
Fallback to old revision id if the current one doesn't exist in
121
        self.assertEqual('qux\n', out)
122
        self.assertEqual('', err)
123
2158.1.1 by Wouter van Heyst
Fix #73500 mostly by catching a NotLocalUrl exception in cmd_cat.
124
    def test_remote_cat(self):
125
        wt = self.make_branch_and_tree('.')
2158.1.2 by Wouter van Heyst
Actually check if cat returns the right content.
126
        self.build_tree(['README'])
2158.1.1 by Wouter van Heyst
Fix #73500 mostly by catching a NotLocalUrl exception in cmd_cat.
127
        wt.add('README')
128
        wt.commit('Making sure there is a basis_tree available')
129
130
        url = self.get_readonly_url() + '/README'
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
131
        out, err = self.run_bzr(['cat', url])
2158.1.2 by Wouter van Heyst
Actually check if cat returns the right content.
132
        self.assertEqual('contents of README\n', out)
2309.1.1 by James Westby
Allow bzr cat to be used against treeless repositories.
133
4948.4.1 by Andrew Bennetts
Fix ObjectNotLocked error in cmd_cat (and also release locks slightly sooner).
134
    def test_cat_branch_revspec(self):
135
        wt = self.make_branch_and_tree('a')
136
        self.build_tree(['a/README'])
137
        wt.add('README')
138
        wt.commit('Making sure there is a basis_tree available')
139
        wt = self.make_branch_and_tree('b')
140
        os.chdir('b')
141
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
142
        out, err = self.run_bzr(
4948.4.1 by Andrew Bennetts
Fix ObjectNotLocked error in cmd_cat (and also release locks slightly sooner).
143
            ['cat', '-r', 'branch:../a', 'README'])
144
        self.assertEqual('contents of a/README\n', out)
145
3368.2.31 by Ian Clatworthy
add --filters option to cat command
146
    def test_cat_filters(self):
147
        wt = self.make_branch_and_tree('.')
148
        self.build_tree(['README'])
149
        wt.add('README')
150
        wt.commit('Making sure there is a basis_tree available')
151
        url = self.get_readonly_url() + '/README'
3368.2.48 by Ian Clatworthy
apply first round of poolie's review feedback
152
3368.2.31 by Ian Clatworthy
add --filters option to cat command
153
        # Test unfiltered output
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
154
        out, err = self.run_bzr(['cat', url])
3368.2.31 by Ian Clatworthy
add --filters option to cat command
155
        self.assertEqual('contents of README\n', out)
3368.2.48 by Ian Clatworthy
apply first round of poolie's review feedback
156
157
        # Test --filters option is legal but has no impact if no filters
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
158
        out, err = self.run_bzr(['cat', '--filters', url])
3368.2.31 by Ian Clatworthy
add --filters option to cat command
159
        self.assertEqual('contents of README\n', out)
160
3368.2.48 by Ian Clatworthy
apply first round of poolie's review feedback
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"),
174
            ])
175
        wt.add('README')
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):
180
            return _stack_2
181
        Tree._content_filter_stack = _custom_content_filter_stack
182
        try:
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)
187
        finally:
188
            Tree._content_filter_stack = real_content_filter_stack
189
2309.1.1 by James Westby
Allow bzr cat to be used against treeless repositories.
190
    def test_cat_no_working_tree(self):
191
        wt = self.make_branch_and_tree('.')
192
        self.build_tree(['README'])
193
        wt.add('README')
194
        wt.commit('Making sure there is a basis_tree available')
195
        wt.branch.bzrdir.destroy_workingtree()
196
197
        url = self.get_readonly_url() + '/README'
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
198
        out, err = self.run_bzr(['cat', url])
2309.1.1 by James Westby
Allow bzr cat to be used against treeless repositories.
199
        self.assertEqual('contents of README\n', out)
2738.4.6 by Daniel Watkins
Rewrapped lines longer than 79 characters.
200
3063.4.2 by John Arbash Meinel
Fix 'nonexistent'
201
    def test_cat_nonexistent_branch(self):
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
202
        self.vfs_transport_factory = memory.MemoryServer
4702.2.1 by Vincent Ladeuil
Remove bogus config imports left in previous tweak
203
        self.run_bzr_error(['^bzr: ERROR: Not a branch'],
204
                           ['cat', self.get_url()])
5171.3.5 by Martin von Gagern
Add blackbox test for cat --directory.
205
206
    def test_cat_directory(self):
207
        wt = self.make_branch_and_tree('a')
208
        self.build_tree(['a/README'])
209
        wt.add('README')
210
        wt.commit('Making sure there is a basis_tree available')
211
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
212
        out, err = self.run_bzr(['cat', '--directory=a', 'README'])
5171.3.5 by Martin von Gagern
Add blackbox test for cat --directory.
213
        self.assertEqual('contents of a/README\n', out)
214
215
    def test_cat_remote_directory(self):
216
        wt = self.make_branch_and_tree('a')
217
        self.build_tree(['a/README'])
218
        wt.add('README')
219
        wt.commit('Making sure there is a basis_tree available')
220
221
        url = self.get_readonly_url() + '/a'
5967.10.1 by Martin Pool
Stop unnecessary use of subprocesses in blackbox.test_cat: about 7x faster on wine
222
        out, err = self.run_bzr(['cat', '-d', url, 'README'])
5171.3.5 by Martin von Gagern
Add blackbox test for cat --directory.
223
        self.assertEqual('contents of a/README\n', out)