-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_jprops.py
346 lines (276 loc) · 9.33 KB
/
test_jprops.py
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
import codecs
import copy
import io
import pickle
from io import BytesIO, StringIO, TextIOBase
import pytest
from pytest import raises
import jprops
from jprops import text_type
@pytest.mark.parametrize('lines', [
b'a\nb\nc\n', # Unix
b'a\r\nb\r\nc\r\n', # Windows
b'a\rb\rc\r', # Mac
])
def test_property_lines_platform_line_endings(lines):
expected = [u'a', u'b', u'c']
property_lines = lambda fp: list(jprops._property_lines(fp))
assert property_lines(BytesIO(lines)) == expected
assert property_lines(StringIO(lines.decode('ascii'))) == expected
@pytest.mark.parametrize('lines,expected', [
# skips blanks
(b'a\nb\n \t \n\nc\n', [u'a', u'b', u'c']),
# includes comments
(b'a\nb\n#foo\n!bar\nc\n', [u'a', u'b', u'#foo', u'!bar', u'c']),
# continuation
(b'a\nb\\\nc\nd\n', [u'a', u'bc', u'd']),
# continuation includes trailing blanks
(b'a\nb \\\nc\nd\n', [u'a', u'b c', u'd']),
# continuation skips leading blanks
(b'a\nb\\\n c\nd\n', [u'a', u'bc', u'd']),
# escaped backslash is not a continuation
(b'a\nb\\\\\nc\nd\n', [u'a', u'b\\\\', u'c', u'd']),
# escaped backslash before continuation
(b'a\nb\\\\\\\nc\nd\n', [u'a', u'b\\\\c', u'd']),
])
def test_property_lines_splitting(lines, expected):
property_lines = lambda fp: list(jprops._property_lines(fp))
assert property_lines(BytesIO(lines)) == expected
assert property_lines(StringIO(lines.decode('ascii'))) == expected
@pytest.mark.parametrize('line,expected', [
# with equals separator
(u'a=b', (u'a', u'b')),
(u'a= b', (u'a', u'b')),
(u'a = b', (u'a', u'b')),
(u'a =b', (u'a', u'b')),
# with colon separator
(u'a:b', (u'a', u'b')),
(u'a: b', (u'a', u'b')),
(u'a : b', (u'a', u'b')),
(u'a :b', (u'a', u'b')),
# only space separator
(u'a b', (u'a', u'b')),
# additional key terminator after already terminated
(u'a : : b', (u'a', u': b')),
(u'a::b', (u'a', u':b')),
(u'a = = b', (u'a', u'= b')),
(u'a==b', (u'a', u'=b')),
(u'a = : b', (u'a', u': b')),
(u'a : = b', (u'a', u'= b')),
# key terminator escaped
(u'a\\=b = c', (u'a\\=b', u'c')),
(u'a\\:b\\=c : d', (u'a\\:b\\=c', u'd')),
# empty value
(u'a', (u'a', u'')),
# comment
(u'#foo', (jprops.COMMENT, u'foo')),
# non-ascii
(u'\u00ff=\u00fe', (u'\u00ff', u'\u00fe')),
])
def test_split_key_value(line, expected):
assert jprops._split_key_value(line) == expected
@pytest.mark.parametrize('value,expected', [
# basic whitespace escapes
(u'\\t', '\t'),
(u'\\n', '\n'),
(u'\\f', '\f'),
(u'\\r', '\r'),
# unrecognized escape should just return the character
(u'\\=', '='),
(u'\\:', ':'),
(u'\\b', 'b'),
# unicode \u escapes
(u'\\u00ff', u'\u00ff'),
# backslash encoded as \u unicode escape
(u'\\u005cb', '\\b'),
# unicode with escaped backslashes
(u'\\\\u00ff', '\\u00ff'),
(u'\\\\\\u00ff', u'\\\u00ff'),
(u'\\\\\\\\u00ff', '\\\\u00ff'),
])
def test_unescape(value, expected):
actual = jprops._unescape(value)
assert actual == expected
assert type(actual) == type(expected)
@pytest.mark.parametrize('value,expected', [
# basic
(u'\\', u'\\\\'),
(u'\t', u'\\t'),
(u'\n', u'\\n'),
(u'\f', u'\\f'),
(u'\r', u'\\r'),
# escape comment markers
(u'#', u'\\#'),
(u'!', u'\\!'),
])
def test_escape(value, expected):
actual = jprops._escape(value)
assert actual == expected
assert type(actual) == type(expected)
@pytest.mark.parametrize('value,expected', [
# leading whitespace in value
(u' x\ty ', u'\\ \\ x\\ty '),
# space in middle value does not need escaped
(u'x y', u'x y'),
# key terminator in value
(u'=', u'\\='),
(u':', u'\\:'),
])
def test_escape_value(value, expected):
actual = jprops._escape_value(value)
assert actual == expected
assert type(actual) == type(expected)
@pytest.mark.parametrize('key,expected', [
(u'=', u'\\='),
(u':', u'\\:'),
(u' x ', u'\\ x\\ '),
])
def test_escape_keys(key, expected):
actual = jprops._escape_key(key)
assert actual == expected
assert type(actual) == type(expected)
@pytest.mark.parametrize('comment,expected', [
# newlines in comments should start the next line with a comment
(u'foo\nbar', u'#foo\n#bar'),
(u'foo\n\nbar', u'#foo\n#\n#bar'),
(u'foo\rbar', u'#foo\n#bar'),
(u'foo\r\rbar', u'#foo\n#\n#bar'),
(u'foo\r\nbar', u'#foo\n#bar'),
(u'foo\r\n\r\nbar', u'#foo\n#\n#bar'),
(u'foo\n', u'#foo\n#'),
# if the newline is already followed by a comment marker, keep it
(u'foo\n#bar', u'#foo\n#bar'),
(u'foo\n!bar', u'#foo\n!bar'),
])
def test_escape_comment_newline(comment, expected):
assert jprops._escape_comment(comment) == expected
@pytest.mark.parametrize('key,value,expected', [
(u'\x00', u'', b'\\u0000='),
(u'\u0000', u'', b'\\u0000='),
(u'\x19', u'', b'\\u0019='),
(u'\u0019', u'', b'\\u0019='),
(u'\x7f', u'', b'\\u007f='),
(u'\u007f', u'', b'\\u007f='),
(u'\uffff', u'', b'\\uffff='),
(jprops.COMMENT, u'\u00ff', b'#\xff'),
(jprops.COMMENT, u'\u0100', b'#\\u0100'),
])
def test_escape_unicode_in_bytes_output(key, value, expected):
b = BytesIO()
jprops.write_property(b, key, value)
actual = b.getvalue()[:-1] # strip the trailing newline
assert actual == expected
@pytest.mark.parametrize('key,value,expected', [
(u'\x00', u'', u'\u0000='),
(u'\u0000', u'', u'\u0000='),
(u'\x19', u'', u'\u0019='),
(u'\u0019', u'', u'\u0019='),
(u'\x7f', u'', u'\u007f='),
(u'\u007f', u'', u'\u007f='),
(u'\uffff', u'', u'\uffff='),
(jprops.COMMENT, u'\u00ff', u'#\xff'),
(jprops.COMMENT, u'\u0100', u'#\u0100'),
])
def test_unicode_in_text_output_not_escaped(key, value, expected):
b = StringIO()
jprops.write_property(b, key, value)
actual = b.getvalue()[:-1] # strip the trailing newline
assert actual == expected
def test_write_non_string_is_an_error():
with raises(TypeError):
jprops.write_property(BytesIO(), b'x', 1)
with raises(TypeError):
jprops.write_property(BytesIO(), 1, b'x')
def test_iter_properties_ignores_comments_by_default():
fp = BytesIO(b'a\n#foo\nb\n')
assert list(jprops.iter_properties(fp)) == [('a', ''), ('b', '')]
def test_iter_properties_includes_comments():
fp = BytesIO(b'a\n#foo\nb\n')
assert (list(jprops.iter_properties(fp, comments=True)) ==
[('a', ''), (jprops.COMMENT, 'foo'), ('b', '')])
def test_write_property_with_comment():
fp = BytesIO()
jprops.write_property(fp, jprops.COMMENT, 'foo')
assert fp.getvalue() == b'#foo\n'
def test_read_text():
fp = StringIO(u'a=\u00ff\n')
assert list(jprops.iter_properties(fp)) == [(u'a', u'\u00ff')]
def test_read_bytes():
fp = BytesIO(b'a=\\u00ff\n')
assert list(jprops.iter_properties(fp)) == [(u'a', u'\u00ff')]
def test_write_text():
fp = StringIO()
jprops.write_property(fp, u'a', u'\u00ff')
assert fp.getvalue() == u'a=\u00ff\n'
def test_write_bytes():
fp = BytesIO()
jprops.write_property(fp, u'a', u'\u00ff')
assert fp.getvalue() == b'a=\\u00ff\n'
def builtin_open(path, mode, encoding, newline):
if 'w' in mode and newline:
# jprops handles newline splitting on read, but relies on the underlying
# file for writing
raise pytest.skip("built-in open doesn't support newline for writing")
if 'r' in mode and not newline:
# for reading use universal-newlines support if newline is None or ''
mode += 'U'
if encoding is None:
return open(path, mode)
elif jprops.PY2:
raise pytest.skip("Python 2 built-in open doesn't support encoding")
return open(path, mode, encoding=encoding)
def codecs_open(path, mode, encoding, newline):
if 'w' in mode and newline:
# jprops handles newline splitting on read, but relies on the underlying
# file for writing
pytest.skip("codecs.open doesn't support newline modes")
return codecs.open(path, mode, encoding=encoding)
def io_open(path, mode, encoding, newline):
if 'b' in mode and newline is not None:
raise pytest.skip("io.open binary mode doesn't take a newline argument")
return io.open(path, mode, encoding=encoding, newline=newline)
@pytest.mark.parametrize('opener', [
builtin_open,
codecs_open,
io_open,
])
@pytest.mark.parametrize('encoding,file_data', [
(None, b'a=\\u0100\n'),
('utf-8', u'a=\u0100\n'.encode('utf-8')),
])
@pytest.mark.parametrize('mode', [
'r', 'w',
])
@pytest.mark.parametrize('newline', [
None, '', '\n', '\r', '\r\n',
])
def test_file_modes(tmpdir, opener, encoding, file_data, mode, newline):
# check common combinations of various methods of opening files with different
# encodings and line-endings
if encoding is None:
mode += 'b'
expected_props = {u'a': u'\u0100'}
if newline:
file_data = file_data.replace(b'\n', newline.encode('ascii'))
open_path = lambda path: opener(
str(path),
mode,
encoding=encoding,
newline=newline,
)
if 'r' in mode:
read_path = tmpdir.join('reading.properties')
read_path.write_binary(file_data)
with open_path(read_path) as fp:
actual_props = jprops.load_properties(fp)
assert actual_props == expected_props
else:
write_path = tmpdir.join('writing.properties')
with open_path(write_path) as fp:
jprops.store_properties(fp, expected_props, timestamp=False)
actual_data = write_path.read_binary()
assert actual_data == file_data
def test_comment_identity():
assert copy.copy(jprops.COMMENT) is jprops.COMMENT
assert copy.deepcopy(jprops.COMMENT) is jprops.COMMENT
assert pickle.loads(pickle.dumps(jprops.COMMENT)) is jprops.COMMENT