-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_pyintercept.py
107 lines (88 loc) · 2.27 KB
/
test_pyintercept.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
import pyintercept
import pytest
import uncompyle6
from pyintercept.lib import Patcher
def print_code(code):
print(uncompyle6.deparse_code(2.7, code).text)
def test_patch_file(capsys):
p = Patcher()
p.loads("""
from setuptools import setup
setup(package='foobar')
""")
p.patch_run(function='setuptools.setup', handler=pyintercept.print_)
out, _err = capsys.readouterr()
assert out == "()\n{'package': 'foobar'}\n"
def test_patch_file2(capsys):
p = Patcher()
p.loads("""
from setuptools import setup
setup(package='foo')
setup(package='bar')
""")
p.patch_run(function='setuptools.setup', handler=pyintercept.print_)
out, _err = capsys.readouterr()
assert out == "()\n{'package': 'foo'}\n()\n{'package': 'bar'}\n"
def test_patch_file_globals(capsys):
p = Patcher()
p.filepath = 'foo.py'
p.loads("""
print(__file__)
from setuptools import setup
setup(package='foobar')
""")
p.patch_run(function='setuptools.setup', handler=pyintercept.print_)
out, _err = capsys.readouterr()
assert out == "foo.py\n()\n{'package': 'foobar'}\n"
def test_patch_local_function(capsys):
def double(origfn, i):
print(i * 3)
p = Patcher()
p.loads("""
def double(i):
print(i * 2)
double(1)
double(2)
""")
p.patch_run(function='double', handler=double)
out, _err = capsys.readouterr()
assert out == '3\n6\n'
def test_patch_multiple_calls(capsys):
p = Patcher()
p.loads("""
def x(*args):
pass
x(1)
def foo(**kwargs):
pass
foo(bar=2)
""")
p.patch(function='x', handler=pyintercept.print_)
p.patch(function='foo', handler=pyintercept.print_)
p.run()
out, _err = capsys.readouterr()
assert out == "(1,)\n{}\n()\n{'bar': 2}\n"
def test_patch_all_calls(capsys):
p = Patcher()
p.loads("""
def x(*args):
pass
x(1)
def foo(**kwargs):
pass
foo(bar=2)
""")
p.patch(handler=pyintercept.print_)
p.run()
out, _err = capsys.readouterr()
assert out == "(1,)\n{}\n()\n{'bar': 2}\n"
def test_not_loaded():
p = Patcher()
with pytest.raises(AssertionError):
p.patch_run(function='x')
def test_not_patched():
p = Patcher()
with pytest.raises(AssertionError):
p.run()
with pytest.raises(AssertionError):
p.save('x.pyc')