/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: Martin
  • Date: 2018-11-16 16:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 7172.
  • Revision ID: gzlist@googlemail.com-20181116163822-yg1h1cdng6w7w9kn
Make --profile-imports work on Python 3

Also tweak heading to line up correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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
22
22
but this depends on the transport.
23
23
"""
24
24
 
25
 
import cStringIO
26
 
import os
27
25
import re
28
 
import sys
29
26
from tempfile import TemporaryFile
30
27
 
31
 
from bzrlib import (
 
28
from breezy import (
32
29
    rio,
33
30
    )
34
 
from bzrlib.tests import TestCaseInTempDir, TestCase
35
 
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
36
 
                        RioReader)
 
31
from breezy.tests import TestCase
 
32
from breezy.rio import (
 
33
    RioReader,
 
34
    Stanza,
 
35
    read_stanza,
 
36
    read_stanzas,
 
37
    rio_file,
 
38
    )
37
39
 
38
40
 
39
41
class TestRio(TestCase):
44
46
        self.assertTrue('number' in s)
45
47
        self.assertFalse('color' in s)
46
48
        self.assertFalse('42' in s)
47
 
        self.assertEquals(list(s.iter_pairs()),
 
49
        self.assertEqual(list(s.iter_pairs()),
48
50
                [('name', 'fred'), ('number', '42')])
49
 
        self.assertEquals(s.get('number'), '42')
50
 
        self.assertEquals(s.get('name'), 'fred')
51
 
 
52
 
    def test_value_checks(self):
53
 
        """rio checks types on construction"""
54
 
        # these aren't enforced at construction time
55
 
        ## self.assertRaises(ValueError,
56
 
        ##        Stanza, complex=42 + 3j)
57
 
        ## self.assertRaises(ValueError,
58
 
        ##        Stanza, several=range(10))
 
51
        self.assertEqual(s.get('number'), '42')
 
52
        self.assertEqual(s.get('name'), 'fred')
59
53
 
60
54
    def test_empty_value(self):
61
55
        """Serialize stanza with empty field"""
62
56
        s = Stanza(empty='')
63
 
        self.assertEqualDiff(s.to_string(),
64
 
                "empty: \n")
 
57
        self.assertEquals(s.to_string(),
 
58
                b"empty: \n")
65
59
 
66
60
    def test_to_lines(self):
67
61
        """Write simple rio stanza to string"""
68
62
        s = Stanza(number='42', name='fred')
69
 
        self.assertEquals(list(s.to_lines()),
70
 
                ['name: fred\n',
71
 
                 'number: 42\n'])
 
63
        self.assertEqual(list(s.to_lines()),
 
64
                [b'name: fred\n',
 
65
                 b'number: 42\n'])
72
66
 
73
67
    def test_as_dict(self):
74
68
        """Convert rio Stanza to dictionary"""
75
69
        s = Stanza(number='42', name='fred')
76
70
        sd = s.as_dict()
77
 
        self.assertEquals(sd, dict(number='42', name='fred'))
 
71
        self.assertEqual(sd, dict(number='42', name='fred'))
78
72
 
79
73
    def test_to_file(self):
80
74
        """Write rio to file"""
82
76
        s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
83
77
        s.write(tmpf)
84
78
        tmpf.seek(0)
85
 
        self.assertEqualDiff(tmpf.read(), r'''
86
 
a_thing: something with "quotes like \"this\""
 
79
        self.assertEqual(tmpf.read(), b'''\
 
80
a_thing: something with "quotes like \\"this\\""
87
81
name: fred
88
82
number: 42
89
 
'''[1:])
 
83
''')
90
84
 
91
85
    def test_multiline_string(self):
92
86
        tmpf = TemporaryFile()
93
87
        s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
94
88
        s.write(tmpf)
95
89
        tmpf.seek(0)
96
 
        self.assertEqualDiff(tmpf.read(), '''\
 
90
        self.assertEqual(tmpf.read(), b'''\
97
91
motto: war is peace
98
92
\tfreedom is slavery
99
93
\tignorance is strength
100
94
''')
101
95
        tmpf.seek(0)
102
96
        s2 = read_stanza(tmpf)
103
 
        self.assertEquals(s, s2)
 
97
        self.assertEqual(s, s2)
104
98
 
105
99
    def test_read_stanza(self):
106
100
        """Load stanza from string"""
107
 
        lines = """\
 
101
        lines = b"""\
108
102
revision: mbp@sourcefrog.net-123-abc
109
103
timestamp: 1130653962
110
104
timezone: 36000
112
106
""".splitlines(True)
113
107
        s = read_stanza(lines)
114
108
        self.assertTrue('revision' in s)
115
 
        self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
116
 
        self.assertEquals(list(s.iter_pairs()),
 
109
        self.assertEqual(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
 
110
        self.assertEqual(list(s.iter_pairs()),
117
111
                [('revision', 'mbp@sourcefrog.net-123-abc'),
118
112
                 ('timestamp', '1130653962'),
119
113
                 ('timezone', '36000'),
120
114
                 ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
121
 
        self.assertEquals(len(s), 4)
 
115
        self.assertEqual(len(s), 4)
122
116
 
123
117
    def test_repeated_field(self):
124
118
        """Repeated field in rio"""
127
121
                     ('a', '1000'), ('b', '2000')]:
128
122
            s.add(k, v)
129
123
        s2 = read_stanza(s.to_lines())
130
 
        self.assertEquals(s, s2)
131
 
        self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
132
 
        self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
 
124
        self.assertEqual(s, s2)
 
125
        self.assertEqual(s.get_all('a'), ['10', '100', '1000'])
 
126
        self.assertEqual(s.get_all('b'), ['20', '200', '2000'])
133
127
 
134
128
    def test_backslash(self):
135
129
        s = Stanza(q='\\')
136
130
        t = s.to_string()
137
 
        self.assertEqualDiff(t, 'q: \\\n')
 
131
        self.assertEqual(t, b'q: \\\n')
138
132
        s2 = read_stanza(s.to_lines())
139
 
        self.assertEquals(s, s2)
 
133
        self.assertEqual(s, s2)
140
134
 
141
135
    def test_blank_line(self):
142
136
        s = Stanza(none='', one='\n', two='\n\n')
143
 
        self.assertEqualDiff(s.to_string(), """\
 
137
        self.assertEqual(s.to_string(), b"""\
144
138
none:\x20
145
139
one:\x20
146
140
\t
149
143
\t
150
144
""")
151
145
        s2 = read_stanza(s.to_lines())
152
 
        self.assertEquals(s, s2)
 
146
        self.assertEqual(s, s2)
153
147
 
154
148
    def test_whitespace_value(self):
155
149
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
156
 
        self.assertEqualDiff(s.to_string(), """\
 
150
        self.assertEqual(s.to_string(), b"""\
157
151
combo:\x20
158
152
\t\t\t
159
153
\t
161
155
tabs: \t\t\t
162
156
""")
163
157
        s2 = read_stanza(s.to_lines())
164
 
        self.assertEquals(s, s2)
 
158
        self.assertEqual(s, s2)
165
159
        self.rio_file_stanzas([s])
166
160
 
167
161
    def test_quoted(self):
177
171
                   q9='\\"\\"',
178
172
                   )
179
173
        s2 = read_stanza(s.to_lines())
180
 
        self.assertEquals(s, s2)
 
174
        self.assertEqual(s, s2)
181
175
        # apparent bug in read_stanza
182
176
        # s3 = read_stanza(self.stanzas_to_str([s]))
183
 
        # self.assertEquals(s, s3)
 
177
        # self.assertEqual(s, s3)
184
178
 
185
179
    def test_read_empty(self):
186
180
        """Detect end of rio file"""
188
182
        self.assertEqual(s, None)
189
183
        self.assertTrue(s is None)
190
184
 
 
185
    def test_read_nul_byte(self):
 
186
        """File consisting of a nul byte causes an error."""
 
187
        self.assertRaises(ValueError, read_stanza, [b'\0'])
 
188
 
 
189
    def test_read_nul_bytes(self):
 
190
        """File consisting of many nul bytes causes an error."""
 
191
        self.assertRaises(ValueError, read_stanza, [b'\0' * 100])
 
192
 
191
193
    def test_read_iter(self):
192
194
        """Read several stanzas from file"""
193
195
        tmpf = TemporaryFile()
194
 
        tmpf.write("""\
 
196
        tmpf.write(b"""\
195
197
version_header: 1
196
198
 
197
199
name: foo
212
214
    def test_read_several(self):
213
215
        """Read several stanzas from file"""
214
216
        tmpf = TemporaryFile()
215
 
        tmpf.write("""\
 
217
        tmpf.write(b"""\
216
218
version_header: 1
217
219
 
218
220
name: foo
228
230
""")
229
231
        tmpf.seek(0)
230
232
        s = read_stanza(tmpf)
231
 
        self.assertEquals(s, Stanza(version_header='1'))
232
 
        s = read_stanza(tmpf)
233
 
        self.assertEquals(s, Stanza(name="foo", val='123'))
234
 
        s = read_stanza(tmpf)
235
 
        self.assertEqualDiff(s.get('name'), 'quoted')
236
 
        self.assertEqualDiff(s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
237
 
        s = read_stanza(tmpf)
238
 
        self.assertEquals(s, Stanza(name="bar", val='129319'))
239
 
        s = read_stanza(tmpf)
240
 
        self.assertEquals(s, None)
 
233
        self.assertEqual(s, Stanza(version_header='1'))
 
234
        s = read_stanza(tmpf)
 
235
        self.assertEqual(s, Stanza(name="foo", val='123'))
 
236
        s = read_stanza(tmpf)
 
237
        self.assertEqual(s.get('name'), 'quoted')
 
238
        self.assertEqual(
 
239
            s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
 
240
        s = read_stanza(tmpf)
 
241
        self.assertEqual(s, Stanza(name="bar", val='129319'))
 
242
        s = read_stanza(tmpf)
 
243
        self.assertEqual(s, None)
241
244
        self.check_rio_file(tmpf)
242
245
 
243
246
    def check_rio_file(self, real_file):
244
247
        real_file.seek(0)
245
248
        read_write = rio_file(RioReader(real_file)).read()
246
249
        real_file.seek(0)
247
 
        self.assertEquals(read_write, real_file.read())
 
250
        self.assertEqual(read_write, real_file.read())
248
251
 
249
252
    @staticmethod
250
253
    def stanzas_to_str(stanzas):
256
259
 
257
260
    def test_tricky_quoted(self):
258
261
        tmpf = TemporaryFile()
259
 
        tmpf.write('''\
 
262
        tmpf.write(b'''\
260
263
s: "one"
261
264
 
262
265
s:\x20
305
308
        for expected in expected_vals:
306
309
            stanza = read_stanza(tmpf)
307
310
            self.rio_file_stanzas([stanza])
308
 
            self.assertEquals(len(stanza), 1)
309
 
            self.assertEqualDiff(stanza.get('s'), expected)
 
311
            self.assertEqual(len(stanza), 1)
 
312
            self.assertEqual(stanza.get('s'), expected)
310
313
 
311
314
    def test_write_empty_stanza(self):
312
315
        """Write empty stanza"""
313
316
        l = list(Stanza().to_lines())
314
 
        self.assertEquals(l, [])
 
317
        self.assertEqual(l, [])
315
318
 
316
319
    def test_rio_raises_type_error(self):
317
320
        """TypeError on adding invalid type to Stanza"""
326
329
    def test_rio_unicode(self):
327
330
        uni_data = u'\N{KATAKANA LETTER O}'
328
331
        s = Stanza(foo=uni_data)
329
 
        self.assertEquals(s.get('foo'), uni_data)
 
332
        self.assertEqual(s.get('foo'), uni_data)
330
333
        raw_lines = s.to_lines()
331
 
        self.assertEquals(raw_lines,
332
 
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
 
334
        self.assertEqual(raw_lines,
 
335
                [b'foo: ' + uni_data.encode('utf-8') + b'\n'])
333
336
        new_s = read_stanza(raw_lines)
334
 
        self.assertEquals(new_s.get('foo'), uni_data)
 
337
        self.assertEqual(new_s.get('foo'), uni_data)
335
338
 
336
339
    def test_rio_to_unicode(self):
337
340
        uni_data = u'\N{KATAKANA LETTER O}'
346
349
        s = Stanza(foo=uni_data)
347
350
        parent_stanza = Stanza(child=s.to_unicode())
348
351
        raw_lines = parent_stanza.to_lines()
349
 
        self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
350
 
                          '\t\n',
 
352
        self.assertEqual([b'child: foo: ' + uni_data.encode('utf-8') + b'\n',
 
353
                          b'\t\n',
351
354
                         ], raw_lines)
352
355
        new_parent = read_stanza(raw_lines)
353
356
        child_text = new_parent.get('child')
358
361
    def mail_munge(self, lines, dos_nl=True):
359
362
        new_lines = []
360
363
        for line in lines:
361
 
            line = re.sub(' *\n', '\n', line)
 
364
            line = re.sub(b' *\n', b'\n', line)
362
365
            if dos_nl:
363
 
                line = re.sub('([^\r])\n', '\\1\r\n', line)
 
366
                line = re.sub(b'([^\r])\n', b'\\1\r\n', line)
364
367
            new_lines.append(line)
365
368
        return new_lines
366
369
 
368
371
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
369
372
        lines = rio.to_patch_lines(stanza)
370
373
        for line in lines:
371
 
            self.assertContainsRe(line, '^# ')
 
374
            self.assertContainsRe(line, b'^# ')
372
375
            self.assertTrue(72 >= len(line))
373
376
        for line in rio.to_patch_lines(stanza, max_width=12):
374
377
            self.assertTrue(12 >= len(line))
383
386
    def test_patch_rio_linebreaks(self):
384
387
        stanza = Stanza(breaktest='linebreak -/'*30)
385
388
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
386
 
                              'linebreak\\\\\n')
 
389
                              b'linebreak\\\\\n')
387
390
        stanza = Stanza(breaktest='linebreak-/'*30)
388
391
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
389
 
                              'linebreak-\\\\\n')
 
392
                              b'linebreak-\\\\\n')
390
393
        stanza = Stanza(breaktest='linebreak/'*30)
391
394
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
392
 
                              'linebreak\\\\\n')
 
395
                              b'linebreak\\\\\n')