-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_functional.py
259 lines (200 loc) · 4.88 KB
/
test_functional.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
import io
import os
import tempfile
import pytest
import jpype
import jprops
@pytest.fixture(scope="module")
def java(request):
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea")
request.addfinalizer(jpype.shutdownJVM)
return jpype.java
def java_load(data, encoding=None):
java = jpype.java
props = java.util.Properties()
r = java.io.ByteArrayInputStream(data)
if encoding is not None:
c = java.nio.charset.Charset.forName(encoding)
r = java.io.InputStreamReader(r, )
props.load(r)
return dict(props)
def jprops_load(data):
return jprops.load_properties(io.BytesIO(data))
def jprops_store(fp, props):
jprops.store_properties(fp, props, timestamp=False)
def jproperties_load(data):
import jproperties
props = jproperties.Properties()
if str == bytes:
buf = io.BytesIO(data)
else:
# jproperties on Python 3 doesn't support BytesIO, but we can decode
# the data for it
buf = io.StringIO(data.decode('latin-1'))
props.load(buf)
return dict(props.items())
def jproperties_store(fp, props):
import jproperties
p = jproperties.Properties()
for k, v in props.items():
p[k] = v
data = str(p)
if not isinstance(data, bytes):
data = data.encode('latin-1')
fp.write(data)
def pyjavaproperties_load(data):
import pyjavaproperties
p = pyjavaproperties.Properties()
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
with tmp:
tmp.write(data)
with open(tmp.name) as fp:
p.load(fp)
finally:
os.remove(tmp.name)
return p.getPropertyDict()
def pyjavaproperties_store(fp, props):
import pyjavaproperties
p = pyjavaproperties.Properties()
for k, v in props.items():
p[k] = v
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
p.store(tmp)
with open(tmp.name, 'rb') as tmpfp:
fp.write(tmpfp.read())
finally:
os.remove(tmp.name)
implementations = {
'jprops': (jprops_load, jprops_store),
'jproperties': (jproperties_load, jproperties_store),
'pyjavaproperties': (pyjavaproperties_load, pyjavaproperties_store),
}
@pytest.fixture(params=sorted(implementations))
def properties_impl(request):
impl_name = request.param
if impl_name != 'jprops':
request.applymarker(pytest.mark.xfail)
request.applymarker(pytest.mark.third_party)
return implementations[impl_name]
load_tests = [
# Key value with equals
b'a=b',
b'a= b',
b'a = b',
b'a =b',
# Key value with colon
b'a:b',
b'a: b',
b'a : b',
b'a :b',
# Key terminator after terminator
b'a : : b',
b'a::b',
b'a = = b',
b'a==b',
b'a = : b',
b'a : = b',
# Key terminator escaped
br'a\=b = c',
br'a\:b\=c : d',
# Key value with space only
b'a b',
# Key value empty value
b'a',
# Escaping
# Basic backslash escapes
b'a \\t',
b'a \\n',
b'a \\f',
b'a \\r',
# Unrecognized escapes
br'a \=',
br'a \:',
br'a \b',
# Unicode escape
br'a \u00ff',
# Unicode encoded backslash
br'a \u005cb',
# Unicode with preceding escaped backslashes
br'a \\u00ff',
br'a \\\u00ff',
br'a \\\\u00ff',
# Latin-1 encoded bytes
b'a \x7f',
# Escaped backslash
b'a \\\\x7f',
# Line endings
# Simple
b'a\nb\nc\n',
# Windows newlines
b'a\r\nb\r\nc\r\n',
# Mac newlines
b'a\rb\rc\r',
# Blank lines
b'a\nb\n \t \n\nc\n',
# Line continuation
b'a\nb\\\nc\nd\n',
# Line continuation trailing blanks
b'a\nb \\\nc\nd\n',
# Line continuation skips leading blanks
b'a\nb\\\n c\nd\n',
# Escaped backslash not continuation
b'a\nb\\\\\nc\nd\n',
# Escaped backslash before continuation
b'a\nb\\\\\\\nc\nd\n',
]
@pytest.mark.parametrize('data', load_tests)
def test_load(properties_impl, data, java):
reader, _ = properties_impl
assert reader(data) == java_load(data)
store_tests = [
# Simple
{'a': 'b'},
# Escaping basic
{'a': '\\'},
{'a': 'x \t'},
{'a': 'x \n'},
{'a': 'x \f'},
{'a': 'x \r'},
# Escape whitespace-only value
{'a': '\t'},
{'a': '\n'},
{'a': '\f'},
{'a': '\r'},
# Escape unicode
{'a': '\x00'},
{'a': u'\u0000'},
{'a': '\x19'},
{'a': u'\u0019'},
{'a': '\x7f'},
{'a': u'\u007f'},
{'a': u'\uffff'},
# Escape value leading whitespace
{'a': ' x \t y '},
# Escape key terminators
{'=': ''},
{':': ''},
{' x ': ''},
# Escape key terminators in value
{'a': '='},
{'a': ':'},
# Escape comment markers
{'a': '#'},
{'a': '!'},
]
@pytest.mark.parametrize('props', store_tests)
def test_store(properties_impl, props, java):
_, writer = properties_impl
fp = io.BytesIO()
writer(fp, props)
assert java_load(fp.getvalue()) == props
@pytest.mark.parametrize('props', store_tests)
@pytest.mark.parametrize('encoding', ['utf-8'])
def test_file_encoding(props, java, encoding):
bytes_fp = io.BytesIO()
text_fp = io.TextIOWrapper(bytes_fp, encoding=encoding)
jprops.store_properties(text_fp, props)
text_fp.flush()
assert java_load(bytes_fp.getvalue(), encoding=encoding) == props