~greatmay12/+junk/test1

« back to all changes in this revision

Viewing changes to build/lib.linux-x86_64-2.6/bzrlib/tests/blackbox/test_cat.py

  • Committer: thitipong at ndrsolution
  • Date: 2011-11-14 06:31:02 UTC
  • Revision ID: thitipong@ndrsolution.com-20111114063102-9obte3yfi2azku7d
ndr redirect version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2010 Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
 
 
19
"""Black-box tests for bzr cat.
 
20
"""
 
21
 
 
22
import os
 
23
 
 
24
from bzrlib import tests
 
25
from bzrlib.transport import memory
 
26
 
 
27
 
 
28
class TestCat(tests.TestCaseWithTransport):
 
29
 
 
30
    def test_cat(self):
 
31
        tree = self.make_branch_and_tree('branch')
 
32
        self.build_tree_contents([('branch/a', 'foo\n')])
 
33
        tree.add('a')
 
34
        os.chdir('branch')
 
35
        # 'bzr cat' without an option should cat the last revision
 
36
        self.run_bzr(['cat', 'a'], retcode=3)
 
37
 
 
38
        tree.commit(message='1')
 
39
        self.build_tree_contents([('a', 'baz\n')])
 
40
 
 
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')
 
44
 
 
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],
 
49
            'foo\n')
 
50
        self.assertEquals(self.run_bzr_subprocess(
 
51
            ['cat', 'a', '-r', '-1'])[0],
 
52
            'baz\n')
 
53
 
 
54
        rev_id = tree.branch.last_revision()
 
55
 
 
56
        self.assertEquals(self.run_bzr_subprocess(
 
57
            ['cat', 'a', '-r', 'revid:%s' % rev_id])[0],
 
58
            'baz\n')
 
59
 
 
60
        os.chdir('..')
 
61
 
 
62
        self.assertEquals(self.run_bzr_subprocess(
 
63
            ['cat', 'branch/a', '-r', 'revno:1:branch'])[0],
 
64
            'foo\n')
 
65
        self.run_bzr(['cat', 'a'], retcode=3)
 
66
        self.run_bzr(
 
67
                ['cat', 'a', '-r', 'revno:1:branch-that-does-not-exist'],
 
68
                retcode=3)
 
69
 
 
70
    def test_cat_different_id(self):
 
71
        """'cat' works with old and new files"""
 
72
        self.disable_missing_extensions_warning()
 
73
        tree = self.make_branch_and_tree('.')
 
74
        # the files are named after their path in the revision and
 
75
        # current trees later in the test case
 
76
        # a-rev-tree is special because it appears in both the revision
 
77
        # tree and the working tree
 
78
        self.build_tree_contents([('a-rev-tree', 'foo\n'),
 
79
            ('c-rev', 'baz\n'), ('d-rev', 'bar\n'), ('e-rev', 'qux\n')])
 
80
        tree.lock_write()
 
81
        try:
 
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 -
 
85
            # work around that for now.
 
86
            tree.flush()
 
87
            tree.remove(['d-rev'])
 
88
            tree.rename_one('a-rev-tree', 'b-tree')
 
89
            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')])
 
92
            tree.add(['e-rev'])
 
93
        finally:
 
94
            # calling bzr as another process require free lock on win32
 
95
            tree.unlock()
 
96
 
 
97
        # 'b-tree' is not present in the old tree.
 
98
        self.run_bzr_error(["^bzr: ERROR: u?'b-tree' "
 
99
                            "is not present in revision .+$"],
 
100
                           'cat b-tree --name-from-revision')
 
101
 
 
102
        # get to the old file automatically
 
103
        out, err = self.run_bzr_subprocess('cat d-rev')
 
104
        self.assertEqual('bar\n', out)
 
105
        self.assertEqual('', err)
 
106
 
 
107
        out, err = \
 
108
                self.run_bzr_subprocess('cat a-rev-tree --name-from-revision')
 
109
        self.assertEqual('foo\n', out)
 
110
        self.assertEqual('', err)
 
111
 
 
112
        out, err = self.run_bzr_subprocess('cat a-rev-tree')
 
113
        self.assertEqual('baz\n', out)
 
114
        self.assertEqual('', err)
 
115
 
 
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)
 
120
 
 
121
    def test_remote_cat(self):
 
122
        wt = self.make_branch_and_tree('.')
 
123
        self.build_tree(['README'])
 
124
        wt.add('README')
 
125
        wt.commit('Making sure there is a basis_tree available')
 
126
 
 
127
        url = self.get_readonly_url() + '/README'
 
128
        out, err = self.run_bzr_subprocess(['cat', url])
 
129
        self.assertEqual('contents of README\n', out)
 
130
 
 
131
    def test_cat_branch_revspec(self):
 
132
        wt = self.make_branch_and_tree('a')
 
133
        self.build_tree(['a/README'])
 
134
        wt.add('README')
 
135
        wt.commit('Making sure there is a basis_tree available')
 
136
        wt = self.make_branch_and_tree('b')
 
137
        os.chdir('b')
 
138
 
 
139
        out, err = self.run_bzr_subprocess(
 
140
            ['cat', '-r', 'branch:../a', 'README'])
 
141
        self.assertEqual('contents of a/README\n', out)
 
142
 
 
143
    def test_cat_filters(self):
 
144
        wt = self.make_branch_and_tree('.')
 
145
        self.build_tree(['README'])
 
146
        wt.add('README')
 
147
        wt.commit('Making sure there is a basis_tree available')
 
148
        url = self.get_readonly_url() + '/README'
 
149
 
 
150
        # Test unfiltered output
 
151
        out, err = self.run_bzr_subprocess(['cat', url])
 
152
        self.assertEqual('contents of README\n', out)
 
153
 
 
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)
 
157
 
 
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"),
 
171
            ])
 
172
        wt.add('README')
 
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):
 
177
            return _stack_2
 
178
        Tree._content_filter_stack = _custom_content_filter_stack
 
179
        try:
 
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)
 
184
        finally:
 
185
            Tree._content_filter_stack = real_content_filter_stack
 
186
 
 
187
    def test_cat_no_working_tree(self):
 
188
        wt = self.make_branch_and_tree('.')
 
189
        self.build_tree(['README'])
 
190
        wt.add('README')
 
191
        wt.commit('Making sure there is a basis_tree available')
 
192
        wt.branch.bzrdir.destroy_workingtree()
 
193
 
 
194
        url = self.get_readonly_url() + '/README'
 
195
        out, err = self.run_bzr_subprocess(['cat', url])
 
196
        self.assertEqual('contents of README\n', out)
 
197
 
 
198
    def test_cat_nonexistent_branch(self):
 
199
        self.vfs_transport_factory = memory.MemoryServer
 
200
        self.run_bzr_error(['^bzr: ERROR: Not a branch'],
 
201
                           ['cat', self.get_url()])
 
202
 
 
203
    def test_cat_directory(self):
 
204
        wt = self.make_branch_and_tree('a')
 
205
        self.build_tree(['a/README'])
 
206
        wt.add('README')
 
207
        wt.commit('Making sure there is a basis_tree available')
 
208
 
 
209
        out, err = self.run_bzr_subprocess(['cat', '--directory=a', 'README'])
 
210
        self.assertEqual('contents of a/README\n', out)
 
211
 
 
212
    def test_cat_remote_directory(self):
 
213
        wt = self.make_branch_and_tree('a')
 
214
        self.build_tree(['a/README'])
 
215
        wt.add('README')
 
216
        wt.commit('Making sure there is a basis_tree available')
 
217
 
 
218
        url = self.get_readonly_url() + '/a'
 
219
        out, err = self.run_bzr_subprocess(['cat', '-d', url, 'README'])
 
220
        self.assertEqual('contents of a/README\n', out)