-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest_snoop.py
305 lines (235 loc) · 8.49 KB
/
test_snoop.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
from __future__ import print_function
import io
import os
import re
import sys
import traceback
from importlib import import_module
from tempfile import mkstemp
from threading import current_thread
import pytest
from cheap_repr import cheap_repr, register_repr
from cheap_repr.utils import safe_qualname
from littleutils import file_to_string, group_by_key_func, string_to_file
# Hide 3rd party pretty-printing modules
sys.modules['prettyprinter'] = {}
sys.modules['pprintpp'] = {}
from snoop import formatting, install, spy
from snoop.configuration import Config
from snoop.pp_module import is_deep_arg
from snoop.utils import (NO_BIRDSEYE, PYPY, needs_parentheses,
truncate_list, truncate_string)
formatting._get_filename = lambda _: "/path/to_file.py"
install()
tests_dir = os.path.dirname(__file__)
cheap_repr.raise_exceptions = True
@register_repr(type(cheap_repr))
def repr_function(func, _helper):
return '<function %s at %#x>' % (
safe_qualname(func), id(func))
@register_repr(type(sys))
def repr_module(module, _helper):
return "<module '%s'>" % module.__name__
@register_repr(set)
def repr_set(x, helper):
if not x:
return repr(x)
return helper.repr_iterable(x, '{', '}')
def sample_traceback():
raw = u''.join(
traceback.format_exception(*sys.exc_info())
).splitlines(True)
tb = u''.join(
line for line in raw
if not line.strip().startswith("File")
)
sys.stderr.write(tb)
def assert_sample_output(module_name):
module = import_module('tests.samples.' + module_name)
old = sys.stderr
out = io.StringIO()
sys.stderr = out
try:
assert sys.gettrace() is None
module.main()
assert sys.gettrace() is None
finally:
sys.stderr = old
time = '12:34:56.78'
time_pattern = re.sub(r'\d', r'\\d', time)
normalised = re.sub(time_pattern, time, out.getvalue()).strip()
normalised = re.sub(r'0x\w+', '0xABC', normalised)
normalised = normalised.replace('<genexpr>.<genexpr>', '<genexpr>')
normalised = normalised.replace('<list_iterator', '<tupleiterator')
normalised = normalised.replace('<listiterator', '<tupleiterator')
normalised = normalised.replace('<tuple_iterator', '<tupleiterator')
normalised = normalised.replace('<sequenceiterator', '<tupleiterator')
normalised = normalised.replace(str(current_thread().ident), '123456789')
result_filename = os.path.join(
tests_dir,
'sample_results',
PYPY * 'pypy' + '.'.join(sys.version.split('.')[:2]),
module_name + '.txt',
)
compare_to_file(normalised, result_filename)
def compare_to_file(text, filename):
if os.environ.get('FIX_SNOOP_TESTS'):
string_to_file(text, filename)
else:
expected_output = file_to_string(filename)
assert text == expected_output
def generate_test_samples():
samples_dir = os.path.join(tests_dir, 'samples')
for filename in os.listdir(samples_dir):
if filename.endswith('.pyc'):
continue
module_name = str(filename.split('.')[0])
if module_name in '__init__ __pycache__':
continue
if PYPY or sys.version_info[:2] in ((3, 4), (3, 10)):
if module_name in 'pandas_sample'.split():
continue
if NO_BIRDSEYE:
if module_name in 'spy enabled'.split():
continue
if module_name in 'f_string' and sys.version_info[:2] < (3, 6):
continue
if module_name == 'color' and sys.version_info[:2] == (3, 12) and os.environ.get('CI'):
continue
yield module_name
@pytest.mark.parametrize("module_name", generate_test_samples())
@pytest.mark.order(1)
def test_sample(module_name):
assert_sample_output(module_name)
@pytest.mark.order(2) # Execute after all test_samples have run.
def test_compare_versions():
out = [""]
def prn(*args):
out[0] += " ".join(map(str, args)) + "\n"
samples_dir = os.path.join(tests_dir, "samples")
results_dir = os.path.join(tests_dir, "sample_results")
versions = sorted(os.listdir(results_dir))
for filename in sorted(os.listdir(samples_dir)):
if not filename.endswith(".py"):
continue
module_name = filename[:-3]
if module_name == "__init__":
continue
doesnt_exist = "Doesn't exist:"
def get_results(version):
path = os.path.join(results_dir, version, module_name + ".txt")
if os.path.exists(path):
return file_to_string(path)
else:
return doesnt_exist
grouped = group_by_key_func(versions, get_results)
if len(grouped) > 1:
prn(module_name)
doesnt_exist_versions = grouped.pop(doesnt_exist, None)
if doesnt_exist_versions:
prn(doesnt_exist, ", ".join(doesnt_exist_versions))
if len(grouped) > 1:
prn("Differing versions:")
for version_group in sorted(grouped.values()):
prn(", ".join(version_group))
prn()
compare_to_file(out[0], os.path.join(tests_dir, "version_differences.txt"))
def test_string_io():
string_io = io.StringIO()
config = Config(out=string_io)
contents = u'stuff'
config.write(contents)
assert string_io.getvalue() == contents
def test_callable():
string_io = io.StringIO()
def write(msg):
string_io.write(msg)
string_io = io.StringIO()
config = Config(out=write)
contents = u'stuff'
config.write(contents)
assert string_io.getvalue() == contents
def test_file_output():
_, path = mkstemp()
config = Config(out=path)
contents = u'stuff'
config.write(contents)
with open(path) as output_file:
output = output_file.read()
assert output == contents
def test_no_overwrite_by_default():
_, path = mkstemp()
with open(path, 'w') as output_file:
output_file.write(u'lala')
config = Config(str(path))
config.write(u' doo be doo')
with open(path) as output_file:
output = output_file.read()
assert output == u'lala doo be doo'
def test_overwrite():
_, path = mkstemp()
with open(path, 'w') as output_file:
output_file.write(u'lala')
config = Config(out=str(path), overwrite=True)
config.write(u'doo be')
config.write(u' doo')
with open(path) as output_file:
output = output_file.read()
assert output == u'doo be doo'
def test_needs_parentheses():
assert not needs_parentheses('x')
assert not needs_parentheses('x.y')
assert not needs_parentheses('x.y.z')
assert not needs_parentheses('x.y.z[0]')
assert not needs_parentheses('x.y.z[0]()')
assert not needs_parentheses('x.y.z[0]()(3, 4 * 5)')
assert not needs_parentheses('foo(x)')
assert not needs_parentheses('foo(x+y)')
assert not needs_parentheses('(x+y)')
assert not needs_parentheses('[x+1 for x in ()]')
assert needs_parentheses('x + y')
assert needs_parentheses('x * y')
assert needs_parentheses('x and y')
assert needs_parentheses('x if z else y')
def test_truncate_string():
max_length = 20
for i in range(max_length * 2):
string = i * 'a'
truncated = truncate_string(string, max_length)
if len(string) <= max_length:
assert string == truncated
else:
assert truncated == 'aaaaaaaa...aaaaaaaaa'
assert len(truncated) == max_length
def test_truncate_list():
max_length = 5
for i in range(max_length * 2):
lst = i * ['a']
truncated = truncate_list(lst, max_length)
if len(lst) <= max_length:
assert lst == truncated
else:
assert truncated == ['a', 'a', '...', 'a', 'a']
assert len(truncated) == max_length
class Foo(object):
def bar1(self):
pass
@staticmethod
def bar2():
pass
def test_is_deep_arg():
assert is_deep_arg(lambda: 0)
assert not is_deep_arg(lambda x: 0)
assert not is_deep_arg(lambda x=None: 0)
assert not is_deep_arg(lambda *x: 0)
assert not is_deep_arg(lambda **x: 0)
assert not is_deep_arg(test_is_deep_arg)
assert not is_deep_arg(Foo)
assert not is_deep_arg(Foo.bar1)
assert not is_deep_arg(Foo().bar1)
assert not is_deep_arg(Foo.bar2)
assert not is_deep_arg(Foo().bar2)
def test_no_asttokens_spy():
if PYPY:
with pytest.raises(Exception, match="birdseye doesn't support this version of Python"):
spy(test_is_deep_arg)