~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright (C) 2005, 2006 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Tests for rio serialization

A simple, reproducible structured IO format.

rio itself works in Unicode strings.  It is typically encoded to UTF-8,
but this depends on the transport.
"""

import cStringIO
import os
import re
import sys
from tempfile import TemporaryFile

from bzrlib import (
    rio,
    )
from bzrlib.tests import TestCaseInTempDir, TestCase
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
                        RioReader)


class TestRio(TestCase):

    def test_stanza(self):
        """Construct rio stanza in memory"""
        s = Stanza(number='42', name="fred")
        self.assertTrue('number' in s)
        self.assertFalse('color' in s)
        self.assertFalse('42' in s)
        self.assertEquals(list(s.iter_pairs()),
                [('name', 'fred'), ('number', '42')])
        self.assertEquals(s.get('number'), '42')
        self.assertEquals(s.get('name'), 'fred')

    def test_value_checks(self):
        """rio checks types on construction"""
        # these aren't enforced at construction time
        ## self.assertRaises(ValueError,
        ##        Stanza, complex=42 + 3j)
        ## self.assertRaises(ValueError,
        ##        Stanza, several=range(10))

    def test_empty_value(self):
        """Serialize stanza with empty field"""
        s = Stanza(empty='')
        self.assertEqualDiff(s.to_string(),
                "empty: \n")

    def test_to_lines(self):
        """Write simple rio stanza to string"""
        s = Stanza(number='42', name='fred')
        self.assertEquals(list(s.to_lines()),
                ['name: fred\n',
                 'number: 42\n'])

    def test_as_dict(self):
        """Convert rio Stanza to dictionary"""
        s = Stanza(number='42', name='fred')
        sd = s.as_dict()
        self.assertEquals(sd, dict(number='42', name='fred'))

    def test_to_file(self):
        """Write rio to file"""
        tmpf = TemporaryFile()
        s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
        s.write(tmpf)
        tmpf.seek(0)
        self.assertEqualDiff(tmpf.read(), r'''
a_thing: something with "quotes like \"this\""
name: fred
number: 42
'''[1:])

    def test_multiline_string(self):
        tmpf = TemporaryFile()
        s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
        s.write(tmpf)
        tmpf.seek(0)
        self.assertEqualDiff(tmpf.read(), '''\
motto: war is peace
\tfreedom is slavery
\tignorance is strength
''')
        tmpf.seek(0)
        s2 = read_stanza(tmpf)
        self.assertEquals(s, s2)

    def test_read_stanza(self):
        """Load stanza from string"""
        lines = """\
revision: mbp@sourcefrog.net-123-abc
timestamp: 1130653962
timezone: 36000
committer: Martin Pool <mbp@test.sourcefrog.net>
""".splitlines(True)
        s = read_stanza(lines)
        self.assertTrue('revision' in s)
        self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
        self.assertEquals(list(s.iter_pairs()),
                [('revision', 'mbp@sourcefrog.net-123-abc'),
                 ('timestamp', '1130653962'),
                 ('timezone', '36000'),
                 ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
        self.assertEquals(len(s), 4)

    def test_repeated_field(self):
        """Repeated field in rio"""
        s = Stanza()
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
                     ('a', '1000'), ('b', '2000')]:
            s.add(k, v)
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)
        self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
        self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))

    def test_backslash(self):
        s = Stanza(q='\\')
        t = s.to_string()
        self.assertEqualDiff(t, 'q: \\\n')
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)

    def test_blank_line(self):
        s = Stanza(none='', one='\n', two='\n\n')
        self.assertEqualDiff(s.to_string(), """\
none:\x20
one:\x20
\t
two:\x20
\t
\t
""")
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)

    def test_whitespace_value(self):
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
        self.assertEqualDiff(s.to_string(), """\
combo:\x20
\t\t\t
\t
space:\x20\x20
tabs: \t\t\t
""")
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)
        self.rio_file_stanzas([s])

    def test_quoted(self):
        """rio quoted string cases"""
        s = Stanza(q1='"hello"',
                   q2=' "for',
                   q3='\n\n"for"\n',
                   q4='for\n"\nfor',
                   q5='\n',
                   q6='"',
                   q7='""',
                   q8='\\',
                   q9='\\"\\"',
                   )
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)
        # apparent bug in read_stanza
        # s3 = read_stanza(self.stanzas_to_str([s]))
        # self.assertEquals(s, s3)

    def test_read_empty(self):
        """Detect end of rio file"""
        s = read_stanza([])
        self.assertEqual(s, None)
        self.assertTrue(s is None)

    def test_read_iter(self):
        """Read several stanzas from file"""
        tmpf = TemporaryFile()
        tmpf.write("""\
version_header: 1

name: foo
val: 123

name: bar
val: 129319
""")
        tmpf.seek(0)
        reader = read_stanzas(tmpf)
        read_iter = iter(reader)
        stuff = list(reader)
        self.assertEqual(stuff,
                [ Stanza(version_header='1'),
                  Stanza(name="foo", val='123'),
                  Stanza(name="bar", val='129319'), ])

    def test_read_several(self):
        """Read several stanzas from file"""
        tmpf = TemporaryFile()
        tmpf.write("""\
version_header: 1

name: foo
val: 123

name: quoted
address:   "Willowglen"
\t  42 Wallaby Way
\t  Sydney

name: bar
val: 129319
""")
        tmpf.seek(0)
        s = read_stanza(tmpf)
        self.assertEquals(s, Stanza(version_header='1'))
        s = read_stanza(tmpf)
        self.assertEquals(s, Stanza(name="foo", val='123'))
        s = read_stanza(tmpf)
        self.assertEqualDiff(s.get('name'), 'quoted')
        self.assertEqualDiff(s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
        s = read_stanza(tmpf)
        self.assertEquals(s, Stanza(name="bar", val='129319'))
        s = read_stanza(tmpf)
        self.assertEquals(s, None)
        self.check_rio_file(tmpf)

    def check_rio_file(self, real_file):
        real_file.seek(0)
        read_write = rio_file(RioReader(real_file)).read()
        real_file.seek(0)
        self.assertEquals(read_write, real_file.read())

    @staticmethod
    def stanzas_to_str(stanzas):
        return rio_file(stanzas).read()

    def rio_file_stanzas(self, stanzas):
        new_stanzas = list(RioReader(rio_file(stanzas)))
        self.assertEqual(new_stanzas, stanzas)

    def test_tricky_quoted(self):
        tmpf = TemporaryFile()
        tmpf.write('''\
s: "one"

s:\x20
\t"one"
\t

s: "

s: ""

s: """

s:\x20
\t

s: \\

s:\x20
\t\\
\t\\\\
\t

s: word\\

s: quote"

s: backslashes\\\\\\

s: both\\\"

''')
        tmpf.seek(0)
        expected_vals = ['"one"',
            '\n"one"\n',
            '"',
            '""',
            '"""',
            '\n',
            '\\',
            '\n\\\n\\\\\n',
            'word\\',
            'quote\"',
            'backslashes\\\\\\',
            'both\\\"',
            ]
        for expected in expected_vals:
            stanza = read_stanza(tmpf)
            self.rio_file_stanzas([stanza])
            self.assertEquals(len(stanza), 1)
            self.assertEqualDiff(stanza.get('s'), expected)

    def test_write_empty_stanza(self):
        """Write empty stanza"""
        l = list(Stanza().to_lines())
        self.assertEquals(l, [])

    def test_rio_raises_type_error(self):
        """TypeError on adding invalid type to Stanza"""
        s = Stanza()
        self.assertRaises(TypeError, s.add, 'foo', {})

    def test_rio_raises_type_error_key(self):
        """TypeError on adding invalid type to Stanza"""
        s = Stanza()
        self.assertRaises(TypeError, s.add, 10, {})

    def test_rio_unicode(self):
        uni_data = u'\N{KATAKANA LETTER O}'
        s = Stanza(foo=uni_data)
        self.assertEquals(s.get('foo'), uni_data)
        raw_lines = s.to_lines()
        self.assertEquals(raw_lines,
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
        new_s = read_stanza(raw_lines)
        self.assertEquals(new_s.get('foo'), uni_data)

    def test_rio_to_unicode(self):
        uni_data = u'\N{KATAKANA LETTER O}'
        s = Stanza(foo=uni_data)
        unicode_str = s.to_unicode()
        self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
        new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
        self.assertEqual(uni_data, new_s.get('foo'))

    def test_nested_rio_unicode(self):
        uni_data = u'\N{KATAKANA LETTER O}'
        s = Stanza(foo=uni_data)
        parent_stanza = Stanza(child=s.to_unicode())
        raw_lines = parent_stanza.to_lines()
        self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
                          '\t\n',
                         ], raw_lines)
        new_parent = read_stanza(raw_lines)
        child_text = new_parent.get('child')
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
        self.assertEqual(uni_data, new_child.get('foo'))

    def mail_munge(self, lines, dos_nl=True):
        new_lines = []
        for line in lines:
            line = re.sub(' *\n', '\n', line)
            if dos_nl:
                line = re.sub('([^\r])\n', '\\1\r\n', line)
            new_lines.append(line)
        return new_lines

    def test_patch_rio(self):
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
        lines = rio.to_patch_lines(stanza)
        for line in lines:
            self.assertContainsRe(line, '^# ')
            self.assertTrue(72 >= len(line))
        for line in rio.to_patch_lines(stanza, max_width=12):
            self.assertTrue(12 >= len(line))
        new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
                                                           dos_nl=False))
        lines = self.mail_munge(lines)
        new_stanza = rio.read_patch_stanza(lines)
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
        self.assertEqual(' '* 255, new_stanza.get('space'))
        self.assertEqual('#'* 255, new_stanza.get('hash'))

    def test_patch_rio_linebreaks(self):
        stanza = Stanza(breaktest='linebreak -/'*30)
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
                              'linebreak\\\\\n')
        stanza = Stanza(breaktest='linebreak-/'*30)
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
                              'linebreak-\\\\\n')
        stanza = Stanza(breaktest='linebreak/'*30)
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
                              'linebreak\\\\\n')