From a3b6bc8b3efdd65117dd1cc451eee5f8452d90de Mon Sep 17 00:00:00 2001 From: Oscar Benjamin Date: Wed, 14 Dec 2022 11:50:41 +0000 Subject: [PATCH] test: add coverage testing --- .coveragerc | 2 ++ .gitignore | 1 + bin/activate | 4 ++++ bin/coverage.sh | 40 ++++++++++++++++++++++++++++++++++++++++ setup.py | 20 ++++++++++++++++++-- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 .coveragerc create mode 100644 bin/activate create mode 100755 bin/coverage.sh diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 00000000..d9a48b4b --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +plugins = Cython.Coverage diff --git a/.gitignore b/.gitignore index d746d05a..98b14e5d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ MANIFEST .eggs .local *.egg-info +.coverage diff --git a/bin/activate b/bin/activate new file mode 100644 index 00000000..83f5d452 --- /dev/null +++ b/bin/activate @@ -0,0 +1,4 @@ +export C_INCLUDE_PATH=$(pwd)/.local/include +export LIBRARY_PATH=$(pwd)/.local/lib +export LD_LIBRARY_PATH=$(pwd)/.local/lib +export PYTHONPATH=$(pwd)/src diff --git a/bin/coverage.sh b/bin/coverage.sh new file mode 100755 index 00000000..de4ba0ab --- /dev/null +++ b/bin/coverage.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# +# Note: cython's Cython/Coverage.py fails for pyx files that are included in +# other pyx files. This gives the following error: +# +# $ coverage report -m +# Plugin 'Cython.Coverage.Plugin' did not provide a file reporter for +# '.../python-flint/src/flint/fmpz.pyx'. +# +# A patch to the file is needed: +# +# --- Coverage.py.backup 2022-12-09 17:36:35.387690467 +0000 +# +++ Coverage.py 2022-12-09 17:08:06.282516837 +0000 +# @@ -172,7 +172,9 @@ class Plugin(CoveragePlugin): +# else: +# c_file, _ = self._find_source_files(filename) +# if not c_file: +# - return None +# + c_file = os.path.join(os.path.dirname(filename), 'pyflint.c') +# + if not os.path.exists(c_file): +# + return None +# rel_file_path, code = self._read_source_lines(c_file, filename) +# if code is None: +# return None # no source found +# +# + +set -o errexit + +source bin/activate + +export PYTHON_FLINT_COVERAGE=true + +python setup.py build_ext --inplace + +coverage run test/test.py +coverage run --append test/dtest.py + +coverage report -m +coverage html diff --git a/setup.py b/setup.py index 1ebb8a11..6a195f69 100644 --- a/setup.py +++ b/setup.py @@ -5,10 +5,12 @@ from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext +from Cython.Build import cythonize from numpy.distutils.system_info import default_include_dirs, default_lib_dirs from distutils.sysconfig import get_config_vars + if sys.platform == 'win32': # # This is used in CI to build wheels with mingw64 @@ -36,16 +38,30 @@ (opt,) = get_config_vars('OPT') os.environ['OPT'] = " ".join(flag for flag in opt.split() if flag != '-Wstrict-prototypes') + default_include_dirs += [ os.path.join(d, "flint") for d in default_include_dirs ] + +define_macros = [] +compiler_directives = {'language_level':2} + + +# Enable coverage tracing +if os.getenv('PYTHON_FLINT_COVERAGE'): + define_macros.append(('CYTHON_TRACE', 1)) + compiler_directives['linetrace'] = True + + ext_modules = [ Extension( "flint._flint", ["src/flint/pyflint.pyx"], libraries=libraries, library_dirs=default_lib_dirs, - include_dirs=default_include_dirs) + include_dirs=default_include_dirs, + define_macros=define_macros, + ) ] for e in ext_modules: @@ -54,7 +70,7 @@ setup( name='python-flint', cmdclass={'build_ext': build_ext}, - ext_modules=ext_modules, + ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives), packages=['flint'], package_dir={'': 'src'}, description='Bindings for FLINT and Arb',