~ubuntu-branches/ubuntu/trusty/bzr-xmloutput/trusty

« back to all changes in this revision

Viewing changes to .pc/02_elementtree/tests/test_ls_xml.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2012-02-21 11:30:46 UTC
  • Revision ID: package-import@ubuntu.com-20120221113046-n854cxkvrn6upq61
Tags: 0.8.8+bzr160-2
* Add patches to improve compatibility with newer versions of bzr:
 + 01_no_inventory: Don't rely on inventories being available
 + 02_elementtree: Use correct location of elementtree. Closes: #660728
 + 03_info_controldir: Fix support for 'bzr xmlinfo' in empty control
   directories.
 + 04_no_revision_history: Avoid using deprecated `Branch.revision_history`
   call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Blackbox tests of 'bzr xmlls'"""
 
18
 
 
19
import os
 
20
 
 
21
from bzrlib import ignores
 
22
from bzrlib.tests import TestCaseWithTransport
 
23
from bzrlib.xml_serializer import elementtree as elementtree
 
24
fromstring = elementtree.ElementTree.fromstring
 
25
 
 
26
 
 
27
class TestLSXML(TestCaseWithTransport):
 
28
 
 
29
    def setUp(self):
 
30
        super(TestLSXML, self).setUp()
 
31
 
 
32
        # Create a simple branch that can be used in testing
 
33
        ignores._set_user_ignores(['user-ignore'])
 
34
 
 
35
        self.wt = self.make_branch_and_tree('.')
 
36
        self.build_tree_contents([
 
37
                                 ('.bzrignore', '*.pyo\n'),
 
38
                                 ('a', 'hello\n'),
 
39
                                 ])
 
40
 
 
41
    def run_xmlls(self, args=None):
 
42
        command = 'xmlls'
 
43
        if args:
 
44
            command += ' ' + args
 
45
        out, err = self.run_bzr(command)
 
46
        self.assertEqual('', err)
 
47
        # parse the output and convert it into more usable structure:
 
48
        #   [ { 'kind': 'file, 'path': 'foo', ... }, ... ]
 
49
        lst = fromstring(out)
 
50
        items = []
 
51
        for item_elem in lst.findall('item'):
 
52
            item = {}
 
53
            for attr in item_elem:
 
54
                item[attr.tag] = attr.text
 
55
            items.append(item)
 
56
        return items
 
57
 
 
58
    #def test_lsxml_null_verbose(self):
 
59
    #    # Can't supply both
 
60
    #    self.run_bzr_error(['Cannot set both --verbose and --null'],
 
61
    #                       'xmlls --verbose --null')
 
62
 
 
63
    def test_null_arg(self):
 
64
        """Test that null arg is accepted."""
 
65
        command = 'xmlls --null'
 
66
        out, err = self.run_bzr(command)
 
67
        self.assertEqual('', err)
 
68
        self.assertEqual(out[-2], '\0')
 
69
 
 
70
    def test_lsxml_basic(self):
 
71
        """Test the abilities of 'bzr xmlls'"""
 
72
        expected_items = [{'kind': 'file',
 
73
                           'path': '.bzrignore',
 
74
                           'status_kind': 'unknown'},
 
75
                          {'kind': 'file',
 
76
                           'path': 'a',
 
77
                           'status_kind': 'unknown'}]
 
78
        self.assertEquals(expected_items, self.run_xmlls())
 
79
        self.assertEquals(expected_items, self.run_xmlls('--unknown'))
 
80
        self.assertEquals([], self.run_xmlls('--ignored'))
 
81
        self.assertEquals([], self.run_xmlls('--versioned'))
 
82
        self.assertEquals([], self.run_xmlls('-V'))
 
83
        self.assertEquals(expected_items,
 
84
                          self.run_xmlls('--unknown --ignored --version'))
 
85
        self.assertEquals(expected_items,
 
86
                          self.run_xmlls('--unknown --ignored -V'))
 
87
        self.assertEquals([], self.run_xmlls('--ignored -V'))
 
88
 
 
89
    def test_lsxml_added(self):
 
90
        self.wt.add(['a'], ['a-id'])
 
91
        expected_items = [{'kind': 'file',
 
92
                           'path': '.bzrignore',
 
93
                           'status_kind': 'unknown'},
 
94
                          {'id': 'a-id',
 
95
                           'kind': 'file',
 
96
                           'path': 'a',
 
97
                           'status_kind': 'versioned'}]
 
98
        self.assertEquals(expected_items, self.run_xmlls())
 
99
 
 
100
        self.wt.commit('add')
 
101
        self.build_tree(['subdir/'])
 
102
        expected_items = [{'kind': 'file',
 
103
                           'path': '.bzrignore',
 
104
                           'status_kind': 'unknown'},
 
105
                          {'id': 'a-id',
 
106
                           'kind': 'file',
 
107
                           'path': 'a',
 
108
                           'status_kind': 'versioned'},
 
109
                          {'kind': 'directory',
 
110
                           'path': 'subdir',
 
111
                           'status_kind': 'unknown'}]
 
112
        self.assertEquals(expected_items, self.run_xmlls())
 
113
 
 
114
        self.build_tree(['subdir/b'])
 
115
        self.wt.add(['subdir/', 'subdir/b', '.bzrignore'],
 
116
            ['subdir-id', 'subdirb-id', 'bzrignore-id'])
 
117
        expected_items = [{'id': 'bzrignore-id',
 
118
                           'kind': 'file',
 
119
                           'path': '.bzrignore',
 
120
                           'status_kind': 'versioned'},
 
121
                          {'id': 'a-id',
 
122
                           'kind': 'file',
 
123
                           'path': 'a',
 
124
                           'status_kind': 'versioned'},
 
125
                          {'id': 'subdir-id',
 
126
                           'kind': 'directory',
 
127
                           'path': 'subdir',
 
128
                           'status_kind': 'versioned'},
 
129
                          {'id': 'subdirb-id',
 
130
                           'kind': 'file',
 
131
                           'path': 'subdir/b',
 
132
                           'status_kind': 'versioned'}]
 
133
        self.assertEquals(expected_items, self.run_xmlls())
 
134
 
 
135
    def test_lsxml_non_recursive(self):
 
136
        self.build_tree(['subdir/', 'subdir/b'])
 
137
        self.wt.add(['a', 'subdir/', 'subdir/b', '.bzrignore'],
 
138
            ['a-id', 'subdir-id', 'subdirb-id', 'bzrignore-id'])
 
139
        expected_items = [{'id': 'bzrignore-id',
 
140
                           'kind': 'file',
 
141
                           'path': '.bzrignore',
 
142
                           'status_kind': 'versioned'},
 
143
                          {'id': 'a-id',
 
144
                           'kind': 'file',
 
145
                           'path': 'a',
 
146
                           'status_kind': 'versioned'},
 
147
                          {'id': 'subdir-id',
 
148
                           'kind': 'directory',
 
149
                           'path': 'subdir',
 
150
                           'status_kind': 'versioned'}]
 
151
        self.assertEquals(expected_items, self.run_xmlls('--non-recursive'))
 
152
 
 
153
        # Check what happens in a sub-directory
 
154
        os.chdir('subdir')
 
155
        expcted_items = [{'id': 'subdirb-id',
 
156
                          'kind': 'file',
 
157
                          'path': 'b',
 
158
                          'status_kind': 'versioned'}]
 
159
        self.assertEquals(expcted_items, self.run_xmlls())
 
160
        expcted_items = [{'id': 'subdirb-id',
 
161
                          'kind': 'file',
 
162
                          'path': 'subdir/b',
 
163
                          'status_kind': 'versioned'}]
 
164
        self.assertEquals(expcted_items, self.run_xmlls('--from-root'))
 
165
        expected_items = [{'id': 'subdirb-id',
 
166
                           'kind': 'file',
 
167
                           'path': 'subdir/b',
 
168
                           'status_kind': 'versioned'}]
 
169
        self.assertEquals(expected_items,
 
170
                          self.run_xmlls('--from-root --non-recursive'))
 
171
 
 
172
    def test_lsxml_path(self):
 
173
        """If a path is specified, files are listed with that prefix"""
 
174
        self.build_tree(['subdir/', 'subdir/b'])
 
175
        self.wt.add(['subdir', 'subdir/b'], ['subdir-id', 'subdirb-id'])
 
176
        expected_items = [{'id': 'subdirb-id',
 
177
                           'kind': 'file',
 
178
                           'path': 'subdir/b',
 
179
                           'status_kind': 'versioned'}]
 
180
        self.assertEquals(expected_items, self.run_xmlls('subdir'))
 
181
 
 
182
        # Check what happens in a sub-directory, referring to parent
 
183
        os.chdir('subdir')
 
184
        expected_items = [{'kind': 'file',
 
185
                           'path': '../.bzrignore',
 
186
                           'status_kind': 'unknown'},
 
187
                          {'kind': 'file',
 
188
                           'path': '../a',
 
189
                           'status_kind': 'unknown'},
 
190
                          {'id': 'subdir-id',
 
191
                           'kind': 'directory',
 
192
                           'path': '../subdir',
 
193
                           'status_kind': 'versioned'},
 
194
                          {'id': 'subdirb-id',
 
195
                           'kind': 'file',
 
196
                           'path': '../subdir/b',
 
197
                           'status_kind': 'versioned'}]
 
198
        self.assertEquals(expected_items, self.run_xmlls('..'))
 
199
        self.run_bzr_error(['cannot specify both --from-root and PATH'],
 
200
                           'xmlls --from-root ..')
 
201
 
 
202
    def test_lsxml_revision(self):
 
203
        self.wt.add(['a'], ['a-id'])
 
204
        self.wt.commit('add')
 
205
 
 
206
        self.build_tree(['subdir/'])
 
207
 
 
208
        # Check what happens when we supply a specific revision
 
209
        expected_items = [{'id': 'a-id',
 
210
                           'kind': 'file',
 
211
                           'path': 'a',
 
212
                           'status_kind': 'versioned'}]
 
213
        self.assertEquals(expected_items, self.run_xmlls('--revision 1'))
 
214
 
 
215
        os.chdir('subdir')
 
216
        self.assertEquals([], self.run_xmlls('--revision 1'))
 
217
 
 
218
    def test_lsxml_branch(self):
 
219
        """If a branch is specified, files are listed from it"""
 
220
        self.build_tree(['subdir/', 'subdir/b'])
 
221
        self.wt.add(['subdir', 'subdir/b'], ['subdir-id', 'subdirb-id'])
 
222
        self.wt.commit('committing')
 
223
        branch = self.make_branch('branchdir')
 
224
        branch.pull(self.wt.branch)
 
225
        expected_items = [{'id': 'subdir-id',
 
226
                           'kind': 'directory',
 
227
                           'path': 'branchdir/subdir',
 
228
                           'status_kind': 'versioned'},
 
229
                          {'id': 'subdirb-id',
 
230
                           'kind': 'file',
 
231
                           'path': 'branchdir/subdir/b',
 
232
                           'status_kind': 'versioned'}]
 
233
        self.assertEquals(expected_items, self.run_xmlls('branchdir'))
 
234
        self.assertEquals(expected_items,
 
235
                          self.run_xmlls('branchdir --revision 1'))
 
236
 
 
237
    def test_lsxml_ignored(self):
 
238
        # Now try to do ignored files.
 
239
        self.wt.add(['a', '.bzrignore'], ['a-id', 'bzrignore-id'])
 
240
        self.build_tree(['blah.py', 'blah.pyo', 'user-ignore'])
 
241
        expected_items = [{'id': 'bzrignore-id',
 
242
                           'kind': 'file',
 
243
                           'path': '.bzrignore',
 
244
                           'status_kind': 'versioned'},
 
245
                          {'id': 'a-id',
 
246
                           'kind': 'file',
 
247
                           'path': 'a',
 
248
                           'status_kind': 'versioned'},
 
249
                          {'kind': 'file',
 
250
                           'path': 'blah.py',
 
251
                           'status_kind': 'unknown'},
 
252
                          {'kind': 'file',
 
253
                           'path': 'blah.pyo',
 
254
                           'status_kind': 'ignored'},
 
255
                          {'kind': 'file',
 
256
                           'path': 'user-ignore',
 
257
                           'status_kind': 'ignored'}]
 
258
        self.assertEquals(expected_items, self.run_xmlls())
 
259
        expected_items = [{'kind': 'file',
 
260
                           'path': 'blah.pyo',
 
261
                           'pattern': '*.pyo',
 
262
                           'status_kind': 'ignored'},
 
263
                          {'kind': 'file',
 
264
                           'path': 'user-ignore',
 
265
                           'pattern': 'user-ignore',
 
266
                           'status_kind': 'ignored'}]
 
267
        self.assertEquals(expected_items, self.run_xmlls('--ignored'))
 
268
        expected_items = [{'kind': 'file',
 
269
                           'path': 'blah.py',
 
270
                           'status_kind': 'unknown'}]
 
271
        self.assertEquals(expected_items, self.run_xmlls('--unknown'))
 
272
        expected_items = [{'id': 'bzrignore-id',
 
273
                           'kind': 'file',
 
274
                           'path': '.bzrignore',
 
275
                           'status_kind': 'versioned'},
 
276
                          {'id': 'a-id',
 
277
                           'kind': 'file',
 
278
                           'path': 'a',
 
279
                           'status_kind': 'versioned'}]
 
280
        self.assertEquals(expected_items, self.run_xmlls('--versioned'))
 
281
 
 
282
    def test_lsxml_kinds(self):
 
283
        self.build_tree(['subdir/'])
 
284
        expected_items = [{'kind': 'file',
 
285
                           'path': '.bzrignore',
 
286
                           'status_kind': 'unknown'},
 
287
                          {'kind': 'file',
 
288
                           'path': 'a',
 
289
                           'status_kind': 'unknown'}]
 
290
        self.assertEquals(expected_items, self.run_xmlls('--kind=file'))
 
291
        expected_items = [{'kind': 'directory',
 
292
                           'path': 'subdir',
 
293
                           'status_kind': 'unknown'}]
 
294
        self.assertEquals(expected_items, self.run_xmlls('--kind=directory'))
 
295
        self.assertEquals([], self.run_xmlls('--kind=symlink'))
 
296
        self.run_bzr_error(['invalid kind specified'], 'xmlls --kind=pile')
 
297
 
 
298
    def test_lsxml_path_nonrecursive(self):
 
299
        expected_items = [{'kind': 'file',
 
300
                           'path': '%s/.bzrignore' % self.test_dir,
 
301
                           'status_kind': 'unknown'},
 
302
                          {'kind': 'file',
 
303
                           'path': '%s/a' % self.test_dir,
 
304
                           'status_kind': 'unknown'}]
 
305
        self.assertEquals(expected_items,
 
306
                          self.run_xmlls('%s --non-recursive' % self.test_dir))
 
307
 
 
308
    def test_escape_id(self):
 
309
        """Test that the id is properly escaped."""
 
310
        self.wt.add(['a'], ['Hola_<user@foobar>_Mundo'])
 
311
        self.wt.commit('add')
 
312
 
 
313
        self.build_tree(['subdir/'])
 
314
 
 
315
        # Check what happens if the id contains <, @ and >
 
316
        expected_items = [{'id': 'Hola_<user@foobar>_Mundo',
 
317
                           'kind': 'file',
 
318
                           'path': 'a',
 
319
                           'status_kind': 'versioned'}]
 
320
        self.assertEquals(expected_items, self.run_xmlls('--revision 1'))
 
321