5
PYTHON -c "import shutil; shutil.copy('pkg/coverage_test_pyx.pyx', 'pkg/coverage_test_pyx.pxi')"
6
PYTHON setup.py build_ext -i
10
######## setup.py ########
12
from distutils.core import setup
13
from Cython.Build import cythonize
15
setup(ext_modules = cythonize([
16
'coverage_test_*.py*',
17
'pkg/coverage_test_*.py*',
18
'Package2/CoverageTest_*.py*'
22
######## .coveragerc ########
24
plugins = Cython.Coverage
27
######## pkg/__init__.py ########
29
######## pkg/coverage_test_py.py ########
30
# cython: linetrace=True
31
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_USE_SYS_MONITORING=0
43
######## pkg/coverage_test_pyx.pyx ########
44
# cython: linetrace=True
45
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_USE_SYS_MONITORING=0
47
def func1(int a, int b):
57
######## coverage_test_include_pyx.pyx ########
58
# cython: linetrace=True
59
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_USE_SYS_MONITORING=0
63
cdef int cfunc1(int x): # 6
66
include "pkg/coverage_test_pyx.pxi" # 9
68
def main_func(int x): # 11
69
return cfunc1(x) + func1(x, 4) + func2(x) # 12
72
######## Package2/__init__.py ########
73
# Add MixedCase package and filenames to test if the files are found
75
######## Package2/CoverageTest_py.py ########
76
# cython: linetrace=True
77
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_USE_SYS_MONITORING=0
89
######## Package2/CoverageTest_pyx.pyx ########
90
# cython: linetrace=True
91
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_USE_SYS_MONITORING=0
93
def func1(int a, int b):
103
######## coverage_test_include_pyx.pyx ########
104
# cython: linetrace=True
105
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_USE_SYS_MONITORING=0
109
cdef int cfunc1(int x): # 6
112
include "pkg/coverage_test_pyx.pxi" # 9
114
def main_func(int x): # 11
115
return cfunc1(x) + func1(x, 4) + func2(x) # 12
118
######## coverage_test.py ########
123
# io.StringIO in Py2.x cannot handle str ...
124
from StringIO import StringIO
126
from io import StringIO
128
from coverage import coverage
130
from pkg import coverage_test_py
131
from pkg import coverage_test_pyx
132
import coverage_test_include_pyx
134
# test the MixedCase Files and packages
135
from Package2 import CoverageTest_py
136
from Package2 import CoverageTest_pyx
138
for module in [coverage_test_py, coverage_test_pyx, coverage_test_include_pyx,
139
CoverageTest_py, CoverageTest_pyx]:
140
assert not any(module.__file__.endswith(ext) for ext in '.py .pyc .pyo .pyw .pyx .pxi'.split()), \
144
def source_file_for(module):
145
module_name = module.__name__
146
path, ext = os.path.splitext(module.__file__)
148
# Linux/Unix/Mac extension module
149
platform_suffix = re.search(r'[.](?:cpython|pypy)-[0-9]+[-_a-z0-9]*$', path, re.I)
151
path = path[:platform_suffix.start()]
153
# Windows extension module
154
platform_suffix = re.search(r'[.]cp[0-9]+-win[_a-z0-9]*$', path, re.I)
156
path = path[:platform_suffix.start()]
157
source_filepath = path + '.' + module_name.rsplit('_', 1)[-1]
158
return source_filepath
161
def run_coverage(module):
162
module_name = module.__name__
163
module_path = module_name.replace('.', os.path.sep) + '.' + module_name.rsplit('_', 1)[-1]
167
assert module.func1(1, 2) == (1 * 2) + 2 + 1
168
assert module.func2(2) == 2 * 2
169
if '_include_' in module_name:
170
assert module.main_func(2) == (2 * 3) + ((2 * 2) + 4 + 1) + (2 * 2)
175
#cov.report([module], file=out)
176
lines = out.getvalue().splitlines()
177
assert any(module_path in line for line in lines), "'%s' not found in coverage report:\n\n%s" % (
178
module_path, out.getvalue())
180
mod_file, exec_lines, excl_lines, missing_lines, _ = cov.analysis2(source_file_for(module))
181
assert module_path in mod_file
183
if '_include_' in module_name:
184
executed = set(exec_lines) - set(missing_lines)
185
assert all(line in executed for line in [7, 12]), '%s / %s' % (exec_lines, missing_lines)
187
# rest of test if for include file
188
mod_file, exec_lines, excl_lines, missing_lines, _ = cov.analysis2(
189
os.path.join(os.path.dirname(module.__file__), "pkg", "coverage_test_pyx.pxi"))
191
executed = set(exec_lines) - set(missing_lines)
192
assert all(line in executed for line in [5, 6, 7, 11]), '%s / %s' % (exec_lines, missing_lines)
195
if __name__ == '__main__':
196
run_coverage(coverage_test_py)
197
run_coverage(coverage_test_pyx)
198
run_coverage(coverage_test_include_pyx)
199
run_coverage(CoverageTest_py)
200
run_coverage(CoverageTest_pyx)