/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_rio.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
25
25
import re
26
26
from tempfile import TemporaryFile
27
27
 
28
 
from bzrlib import (
 
28
from breezy import (
29
29
    rio,
30
30
    )
31
 
from bzrlib.tests import TestCase
32
 
from bzrlib.rio import (
 
31
from breezy.tests import TestCase
 
32
from breezy.rio import (
33
33
    RioReader,
34
34
    Stanza,
35
35
    read_stanza,
46
46
        self.assertTrue('number' in s)
47
47
        self.assertFalse('color' in s)
48
48
        self.assertFalse('42' in s)
49
 
        self.assertEquals(list(s.iter_pairs()),
50
 
                [('name', 'fred'), ('number', '42')])
51
 
        self.assertEquals(s.get('number'), '42')
52
 
        self.assertEquals(s.get('name'), 'fred')
53
 
 
54
 
    def test_value_checks(self):
55
 
        """rio checks types on construction"""
56
 
        # these aren't enforced at construction time
57
 
        ## self.assertRaises(ValueError,
58
 
        ##        Stanza, complex=42 + 3j)
59
 
        ## self.assertRaises(ValueError,
60
 
        ##        Stanza, several=range(10))
 
49
        self.assertEqual(list(s.iter_pairs()),
 
50
                         [('name', 'fred'), ('number', '42')])
 
51
        self.assertEqual(s.get('number'), '42')
 
52
        self.assertEqual(s.get('name'), 'fred')
61
53
 
62
54
    def test_empty_value(self):
63
55
        """Serialize stanza with empty field"""
64
56
        s = Stanza(empty='')
65
 
        self.assertEqualDiff(s.to_string(),
66
 
                "empty: \n")
 
57
        self.assertEquals(s.to_string(),
 
58
                          b"empty: \n")
67
59
 
68
60
    def test_to_lines(self):
69
61
        """Write simple rio stanza to string"""
70
62
        s = Stanza(number='42', name='fred')
71
 
        self.assertEquals(list(s.to_lines()),
72
 
                ['name: fred\n',
73
 
                 'number: 42\n'])
 
63
        self.assertEqual(list(s.to_lines()),
 
64
                         [b'name: fred\n',
 
65
                          b'number: 42\n'])
74
66
 
75
67
    def test_as_dict(self):
76
68
        """Convert rio Stanza to dictionary"""
77
69
        s = Stanza(number='42', name='fred')
78
70
        sd = s.as_dict()
79
 
        self.assertEquals(sd, dict(number='42', name='fred'))
 
71
        self.assertEqual(sd, dict(number='42', name='fred'))
80
72
 
81
73
    def test_to_file(self):
82
74
        """Write rio to file"""
83
75
        tmpf = TemporaryFile()
84
 
        s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
 
76
        s = Stanza(a_thing='something with "quotes like \\"this\\""',
 
77
                   number='42', name='fred')
85
78
        s.write(tmpf)
86
79
        tmpf.seek(0)
87
 
        self.assertEqualDiff(tmpf.read(), r'''
88
 
a_thing: something with "quotes like \"this\""
 
80
        self.assertEqual(tmpf.read(), b'''\
 
81
a_thing: something with "quotes like \\"this\\""
89
82
name: fred
90
83
number: 42
91
 
'''[1:])
 
84
''')
92
85
 
93
86
    def test_multiline_string(self):
94
87
        tmpf = TemporaryFile()
95
 
        s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
 
88
        s = Stanza(
 
89
            motto="war is peace\nfreedom is slavery\nignorance is strength")
96
90
        s.write(tmpf)
97
91
        tmpf.seek(0)
98
 
        self.assertEqualDiff(tmpf.read(), '''\
 
92
        self.assertEqual(tmpf.read(), b'''\
99
93
motto: war is peace
100
94
\tfreedom is slavery
101
95
\tignorance is strength
102
96
''')
103
97
        tmpf.seek(0)
104
98
        s2 = read_stanza(tmpf)
105
 
        self.assertEquals(s, s2)
 
99
        self.assertEqual(s, s2)
106
100
 
107
101
    def test_read_stanza(self):
108
102
        """Load stanza from string"""
109
 
        lines = """\
 
103
        lines = b"""\
110
104
revision: mbp@sourcefrog.net-123-abc
111
105
timestamp: 1130653962
112
106
timezone: 36000
114
108
""".splitlines(True)
115
109
        s = read_stanza(lines)
116
110
        self.assertTrue('revision' in s)
117
 
        self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
118
 
        self.assertEquals(list(s.iter_pairs()),
119
 
                [('revision', 'mbp@sourcefrog.net-123-abc'),
120
 
                 ('timestamp', '1130653962'),
121
 
                 ('timezone', '36000'),
122
 
                 ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
123
 
        self.assertEquals(len(s), 4)
 
111
        self.assertEqual(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
 
112
        self.assertEqual(list(s.iter_pairs()),
 
113
                         [('revision', 'mbp@sourcefrog.net-123-abc'),
 
114
                          ('timestamp', '1130653962'),
 
115
                          ('timezone', '36000'),
 
116
                          ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
 
117
        self.assertEqual(len(s), 4)
124
118
 
125
119
    def test_repeated_field(self):
126
120
        """Repeated field in rio"""
129
123
                     ('a', '1000'), ('b', '2000')]:
130
124
            s.add(k, v)
131
125
        s2 = read_stanza(s.to_lines())
132
 
        self.assertEquals(s, s2)
133
 
        self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
134
 
        self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
 
126
        self.assertEqual(s, s2)
 
127
        self.assertEqual(s.get_all('a'), ['10', '100', '1000'])
 
128
        self.assertEqual(s.get_all('b'), ['20', '200', '2000'])
135
129
 
136
130
    def test_backslash(self):
137
131
        s = Stanza(q='\\')
138
132
        t = s.to_string()
139
 
        self.assertEqualDiff(t, 'q: \\\n')
 
133
        self.assertEqual(t, b'q: \\\n')
140
134
        s2 = read_stanza(s.to_lines())
141
 
        self.assertEquals(s, s2)
 
135
        self.assertEqual(s, s2)
142
136
 
143
137
    def test_blank_line(self):
144
138
        s = Stanza(none='', one='\n', two='\n\n')
145
 
        self.assertEqualDiff(s.to_string(), """\
 
139
        self.assertEqual(s.to_string(), b"""\
146
140
none:\x20
147
141
one:\x20
148
142
\t
151
145
\t
152
146
""")
153
147
        s2 = read_stanza(s.to_lines())
154
 
        self.assertEquals(s, s2)
 
148
        self.assertEqual(s, s2)
155
149
 
156
150
    def test_whitespace_value(self):
157
151
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
158
 
        self.assertEqualDiff(s.to_string(), """\
 
152
        self.assertEqual(s.to_string(), b"""\
159
153
combo:\x20
160
154
\t\t\t
161
155
\t
163
157
tabs: \t\t\t
164
158
""")
165
159
        s2 = read_stanza(s.to_lines())
166
 
        self.assertEquals(s, s2)
 
160
        self.assertEqual(s, s2)
167
161
        self.rio_file_stanzas([s])
168
162
 
169
163
    def test_quoted(self):
179
173
                   q9='\\"\\"',
180
174
                   )
181
175
        s2 = read_stanza(s.to_lines())
182
 
        self.assertEquals(s, s2)
 
176
        self.assertEqual(s, s2)
183
177
        # apparent bug in read_stanza
184
178
        # s3 = read_stanza(self.stanzas_to_str([s]))
185
 
        # self.assertEquals(s, s3)
 
179
        # self.assertEqual(s, s3)
186
180
 
187
181
    def test_read_empty(self):
188
182
        """Detect end of rio file"""
192
186
 
193
187
    def test_read_nul_byte(self):
194
188
        """File consisting of a nul byte causes an error."""
195
 
        self.assertRaises(ValueError, read_stanza, ['\0'])
 
189
        self.assertRaises(ValueError, read_stanza, [b'\0'])
196
190
 
197
191
    def test_read_nul_bytes(self):
198
192
        """File consisting of many nul bytes causes an error."""
199
 
        self.assertRaises(ValueError, read_stanza, ['\0' * 100])
 
193
        self.assertRaises(ValueError, read_stanza, [b'\0' * 100])
200
194
 
201
195
    def test_read_iter(self):
202
196
        """Read several stanzas from file"""
203
197
        tmpf = TemporaryFile()
204
 
        tmpf.write("""\
 
198
        tmpf.write(b"""\
205
199
version_header: 1
206
200
 
207
201
name: foo
215
209
        read_iter = iter(reader)
216
210
        stuff = list(reader)
217
211
        self.assertEqual(stuff,
218
 
                [ Stanza(version_header='1'),
219
 
                  Stanza(name="foo", val='123'),
220
 
                  Stanza(name="bar", val='129319'), ])
 
212
                         [Stanza(version_header='1'),
 
213
                          Stanza(name="foo", val='123'),
 
214
                             Stanza(name="bar", val='129319'), ])
221
215
 
222
216
    def test_read_several(self):
223
217
        """Read several stanzas from file"""
224
218
        tmpf = TemporaryFile()
225
 
        tmpf.write("""\
 
219
        tmpf.write(b"""\
226
220
version_header: 1
227
221
 
228
222
name: foo
238
232
""")
239
233
        tmpf.seek(0)
240
234
        s = read_stanza(tmpf)
241
 
        self.assertEquals(s, Stanza(version_header='1'))
242
 
        s = read_stanza(tmpf)
243
 
        self.assertEquals(s, Stanza(name="foo", val='123'))
244
 
        s = read_stanza(tmpf)
245
 
        self.assertEqualDiff(s.get('name'), 'quoted')
246
 
        self.assertEqualDiff(s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
247
 
        s = read_stanza(tmpf)
248
 
        self.assertEquals(s, Stanza(name="bar", val='129319'))
249
 
        s = read_stanza(tmpf)
250
 
        self.assertEquals(s, None)
 
235
        self.assertEqual(s, Stanza(version_header='1'))
 
236
        s = read_stanza(tmpf)
 
237
        self.assertEqual(s, Stanza(name="foo", val='123'))
 
238
        s = read_stanza(tmpf)
 
239
        self.assertEqual(s.get('name'), 'quoted')
 
240
        self.assertEqual(
 
241
            s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
 
242
        s = read_stanza(tmpf)
 
243
        self.assertEqual(s, Stanza(name="bar", val='129319'))
 
244
        s = read_stanza(tmpf)
 
245
        self.assertEqual(s, None)
251
246
        self.check_rio_file(tmpf)
252
247
 
253
248
    def check_rio_file(self, real_file):
254
249
        real_file.seek(0)
255
250
        read_write = rio_file(RioReader(real_file)).read()
256
251
        real_file.seek(0)
257
 
        self.assertEquals(read_write, real_file.read())
 
252
        self.assertEqual(read_write, real_file.read())
258
253
 
259
254
    @staticmethod
260
255
    def stanzas_to_str(stanzas):
266
261
 
267
262
    def test_tricky_quoted(self):
268
263
        tmpf = TemporaryFile()
269
 
        tmpf.write('''\
 
264
        tmpf.write(b'''\
270
265
s: "one"
271
266
 
272
267
s:\x20
300
295
''')
301
296
        tmpf.seek(0)
302
297
        expected_vals = ['"one"',
303
 
            '\n"one"\n',
304
 
            '"',
305
 
            '""',
306
 
            '"""',
307
 
            '\n',
308
 
            '\\',
309
 
            '\n\\\n\\\\\n',
310
 
            'word\\',
311
 
            'quote\"',
312
 
            'backslashes\\\\\\',
313
 
            'both\\\"',
314
 
            ]
 
298
                         '\n"one"\n',
 
299
                         '"',
 
300
                         '""',
 
301
                         '"""',
 
302
                         '\n',
 
303
                         '\\',
 
304
                         '\n\\\n\\\\\n',
 
305
                         'word\\',
 
306
                         'quote\"',
 
307
                         'backslashes\\\\\\',
 
308
                         'both\\\"',
 
309
                         ]
315
310
        for expected in expected_vals:
316
311
            stanza = read_stanza(tmpf)
317
312
            self.rio_file_stanzas([stanza])
318
 
            self.assertEquals(len(stanza), 1)
319
 
            self.assertEqualDiff(stanza.get('s'), expected)
 
313
            self.assertEqual(len(stanza), 1)
 
314
            self.assertEqual(stanza.get('s'), expected)
320
315
 
321
316
    def test_write_empty_stanza(self):
322
317
        """Write empty stanza"""
323
318
        l = list(Stanza().to_lines())
324
 
        self.assertEquals(l, [])
 
319
        self.assertEqual(l, [])
325
320
 
326
321
    def test_rio_raises_type_error(self):
327
322
        """TypeError on adding invalid type to Stanza"""
333
328
        s = Stanza()
334
329
        self.assertRaises(TypeError, s.add, 10, {})
335
330
 
 
331
    def test_rio_surrogateescape(self):
 
332
        raw_bytes = b'\xcb'
 
333
        self.assertRaises(UnicodeDecodeError, raw_bytes.decode, 'utf-8')
 
334
        try:
 
335
            uni_data = raw_bytes.decode('utf-8', 'surrogateescape')
 
336
        except LookupError:
 
337
            self.skipTest('surrogateescape is not available on Python < 3')
 
338
        s = Stanza(foo=uni_data)
 
339
        self.assertEqual(s.get('foo'), uni_data)
 
340
        raw_lines = s.to_lines()
 
341
        self.assertEqual(raw_lines,
 
342
                         [b'foo: ' + uni_data.encode('utf-8', 'surrogateescape') + b'\n'])
 
343
        new_s = read_stanza(raw_lines)
 
344
        self.assertEqual(new_s.get('foo'), uni_data)
 
345
 
336
346
    def test_rio_unicode(self):
337
347
        uni_data = u'\N{KATAKANA LETTER O}'
338
348
        s = Stanza(foo=uni_data)
339
 
        self.assertEquals(s.get('foo'), uni_data)
 
349
        self.assertEqual(s.get('foo'), uni_data)
340
350
        raw_lines = s.to_lines()
341
 
        self.assertEquals(raw_lines,
342
 
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
 
351
        self.assertEqual(raw_lines,
 
352
                         [b'foo: ' + uni_data.encode('utf-8') + b'\n'])
343
353
        new_s = read_stanza(raw_lines)
344
 
        self.assertEquals(new_s.get('foo'), uni_data)
 
354
        self.assertEqual(new_s.get('foo'), uni_data)
345
355
 
346
356
    def test_rio_to_unicode(self):
347
357
        uni_data = u'\N{KATAKANA LETTER O}'
356
366
        s = Stanza(foo=uni_data)
357
367
        parent_stanza = Stanza(child=s.to_unicode())
358
368
        raw_lines = parent_stanza.to_lines()
359
 
        self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
360
 
                          '\t\n',
361
 
                         ], raw_lines)
 
369
        self.assertEqual([b'child: foo: ' + uni_data.encode('utf-8') + b'\n',
 
370
                          b'\t\n',
 
371
                          ], raw_lines)
362
372
        new_parent = read_stanza(raw_lines)
363
373
        child_text = new_parent.get('child')
364
374
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
368
378
    def mail_munge(self, lines, dos_nl=True):
369
379
        new_lines = []
370
380
        for line in lines:
371
 
            line = re.sub(' *\n', '\n', line)
 
381
            line = re.sub(b' *\n', b'\n', line)
372
382
            if dos_nl:
373
 
                line = re.sub('([^\r])\n', '\\1\r\n', line)
 
383
                line = re.sub(b'([^\r])\n', b'\\1\r\n', line)
374
384
            new_lines.append(line)
375
385
        return new_lines
376
386
 
378
388
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
379
389
        lines = rio.to_patch_lines(stanza)
380
390
        for line in lines:
381
 
            self.assertContainsRe(line, '^# ')
 
391
            self.assertContainsRe(line, b'^# ')
382
392
            self.assertTrue(72 >= len(line))
383
393
        for line in rio.to_patch_lines(stanza, max_width=12):
384
394
            self.assertTrue(12 >= len(line))
387
397
        lines = self.mail_munge(lines)
388
398
        new_stanza = rio.read_patch_stanza(lines)
389
399
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
390
 
        self.assertEqual(' '* 255, new_stanza.get('space'))
391
 
        self.assertEqual('#'* 255, new_stanza.get('hash'))
 
400
        self.assertEqual(' ' * 255, new_stanza.get('space'))
 
401
        self.assertEqual('#' * 255, new_stanza.get('hash'))
392
402
 
393
403
    def test_patch_rio_linebreaks(self):
394
 
        stanza = Stanza(breaktest='linebreak -/'*30)
 
404
        stanza = Stanza(breaktest='linebreak -/' * 30)
395
405
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
396
 
                              'linebreak\\\\\n')
397
 
        stanza = Stanza(breaktest='linebreak-/'*30)
398
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
399
 
                              'linebreak-\\\\\n')
400
 
        stanza = Stanza(breaktest='linebreak/'*30)
401
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
402
 
                              'linebreak\\\\\n')
 
406
                              b'linebreak\\\\\n')
 
407
        stanza = Stanza(breaktest='linebreak-/' * 30)
 
408
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
 
409
                              b'linebreak-\\\\\n')
 
410
        stanza = Stanza(breaktest='linebreak/' * 30)
 
411
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
 
412
                              b'linebreak\\\\\n')