/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 bzrlib/tests/test_rio.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

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