forked from pyutils/line_profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cli.py
83 lines (66 loc) · 2.24 KB
/
test_cli.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
from os.path import join
from sys import executable
def test_cli():
"""
Test command line interaction with kernprof and line_profiler.
References:
https://github.com/pyutils/line_profiler/issues/9
CommandLine:
xdoctest -m ./tests/test_cli.py test_cli
"""
import ubelt as ub
import tempfile
# Create a dummy source file
code = ub.codeblock(
'''
@profile
def my_inefficient_function():
a = 0
for i in range(10):
a += i
for j in range(10):
a += j
if __name__ == '__main__':
my_inefficient_function()
''')
tmp_dpath = tempfile.mkdtemp()
tmp_src_fpath = join(tmp_dpath, 'foo.py')
with open(tmp_src_fpath, 'w') as file:
file.write(code)
# Run kernprof on it
info = ub.cmd(f'kernprof -l {tmp_src_fpath}', verbose=3, cwd=tmp_dpath)
assert info['ret'] == 0
tmp_lprof_fpath = join(tmp_dpath, 'foo.py.lprof')
tmp_lprof_fpath
info = ub.cmd(f'{executable} -m line_profiler {tmp_lprof_fpath}',
cwd=tmp_dpath, verbose=3)
assert info['ret'] == 0
# Check for some patterns that should be in the output
assert '% Time' in info['out']
assert '7 100' in info['out']
def test_version_agreement():
"""
Ensure that line_profiler and kernprof have the same version info
"""
import ubelt as ub
info1 = ub.cmd(f'{executable} -m line_profiler --version')
info2 = ub.cmd(f'{executable} -m kernprof --version')
if info1['ret'] != 0:
print(f'Error querying line-profiler version: {info1}')
if info2['ret'] != 0:
print(f'Error querying kernprof version: {info2}')
# Strip local version suffixes
version1 = info1['out'].strip().split('+')[0]
version2 = info2['out'].strip().split('+')[0]
if version2 != version1:
raise AssertionError(
'Version Mismatch: kernprof and line_profiler must be in sync. '
f'kernprof.line_profiler = {version1}. '
f'kernprof.__version__ = {version2}. '
)
if __name__ == '__main__':
"""
CommandLine:
python ~/code/line_profiler/tests/test_cli.py
"""
test_version_agreement()