Skip to content

Commit

Permalink
Migrate to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Nov 8, 2024
1 parent f4a58a7 commit 38a9f75
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 154 deletions.
25 changes: 7 additions & 18 deletions .github/workflows/ci-sage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
# Ubuntu packages to install so that the project's "setup.py sdist" can succeed
DIST_PREREQ: python3-setuptools autoconf
# Name of this project in the Sage distribution
SPKG: cysignals
# Sage distribution packages to build
TARGETS_PRE: build/make/Makefile
TARGETS: SAGE_CHECK=no SAGE_CHECK_PACKAGES="cysignals,cypari" cysignals cypari
TARGETS_OPTIONAL: build/make/Makefile
# Standard setting: Test the current beta release of Sage
SAGE_REPO: sagemath/sage
SAGE_REF: develop
REMOVE_PATCHES: "*"

jobs:

dist:
Expand All @@ -84,7 +70,8 @@ jobs:
&& echo "sage-package create ${{ env.SPKG }} --version git --tarball ${{ env.SPKG }}-git.tar.gz --type=standard" > upstream/update-pkgs.sh \
&& if [ -n "${{ env.REMOVE_PATCHES }}" ]; then echo "(cd ../build/pkgs/${{ env.SPKG }}/patches && rm -f ${{ env.REMOVE_PATCHES }}; :)" >> upstream/update-pkgs.sh; fi \
&& ls -l upstream/
- uses: actions/upload-artifact@v2
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
path: upstream
name: upstream
Expand Down Expand Up @@ -112,7 +99,7 @@ jobs:
choco install make autoconf gcc-core gcc-g++ python3${{ matrix.python-version }}-devel --source cygwin
- name: Install dependencies
run: |
C:\\tools\\cygwin\\bin\\bash -l -x -c 'export PATH=/usr/local/bin:/usr/bin && cd $(cygpath -u "$GITHUB_WORKSPACE") && python3.${{ matrix.python-version }} -m pip install --upgrade pip'
C:\\tools\\cygwin\\bin\\bash -l -x -c 'export PATH=/usr/local/bin:/usr/bin && cd $(cygpath -u "$GITHUB_WORKSPACE") && python3.${{ matrix.python-version }} -m pip install --upgrade pip && python3.${{ matrix.python-version }} -m pip install --upgrade -r requirements.txt'
- name: Build and check
run: |
C:\\tools\\cygwin\\bin\\bash -l -x -c 'export PATH=/usr/local/bin:/usr/bin && cd $(cygpath -u "$GITHUB_WORKSPACE") && make check PYTHON=python3.${{ matrix.python-version }}'
Expand All @@ -135,7 +122,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade pip
pip install --upgrade -r requirements.txt
- name: Build and check
run: |
make -j4 check
Expand Down Expand Up @@ -196,7 +184,8 @@ jobs:
- name: Install dependencies
run: |
brew install autoconf
python -m pip install --upgrade pip
pip install --upgrade pip
pip install --upgrade -r requirements.txt
- name: Build and check
# Work around https://github.com/sagemath/cysignals/issues/179
run: |
Expand Down
4 changes: 3 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
global-include README README.rst VERSION LICENSE
global-include Makefile configure configure.ac
global-include setup.py rundoctests.py testgdb.py *.pyx
global-include setup.py testgdb.py *.pyx
graft src
graft docs/source
prune build
Expand All @@ -11,3 +11,5 @@ prune example/.*
exclude src/config.h
exclude src/cysignals/signals.pxd
exclude src/cysignals/cysignals_config.h
exclude src/cysignals/conftest.py
exclude src/scripts/conftest.py
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ PYTHON = python3
PIP = $(PYTHON) -m pip -v
LS_R = ls -Ra1

DOCTEST = $(PYTHON) -B rundoctests.py


#####################
# Build
Expand Down Expand Up @@ -66,11 +64,11 @@ check-all:
check-install: check-doctest check-example

check-doctest: install
$(DOCTEST) src/cysignals/*.pyx
$(PYTHON) -m pytest .

check-example: install
$(PYTHON) -m pip install -U build setuptools wheel Cython
cd example && $(PYTHON) -m build --no-isolation .
$(PYTHON) -m build --no-isolation example

check-gdb: install
$(PYTHON) testgdb.py
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ['setuptools', 'Cython>=0.28']
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
addopts = "--doctest-modules --import-mode importlib"
norecursedirs = "builddir docs example"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ wheel
Cython
Sphinx
flake8
pytest>=8.0.0
116 changes: 0 additions & 116 deletions rundoctests.py

This file was deleted.

41 changes: 41 additions & 0 deletions src/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pathlib

from _pytest.nodes import Collector
from _pytest.doctest import DoctestModule


def pytest_collect_file(
file_path: pathlib.Path,
parent: Collector,
):
"""Collect doctests in cython files and run them as test modules."""
config = parent.config
if file_path.suffix == ".pyx":
if config.option.doctestmodules:
return DoctestModule.from_parent(parent, path=file_path)
return None


# Need to import cysignals to initialize it
import cysignals # noqa: E402

try:
import cysignals.alarm
except ImportError:
pass
try:
import cysignals.signals
except ImportError:
pass
try:
import cysignals.pselect
except ImportError:
pass
try:
import cysignals.pysignals
except ImportError:
pass
try:
import cysignals.tests # noqa: F401
except ImportError:
pass
5 changes: 4 additions & 1 deletion src/cysignals/pselect.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ def interruptible_sleep(double seconds):
>>> import signal, time
>>> def alarm_handler(sig, frame):
... pass
>>> _ = signal.signal(signal.SIGALRM, alarm_handler)
>>> old_handler = signal.signal(signal.SIGALRM, alarm_handler)
>>> t0 = time.time()
>>> _ = signal.alarm(1)
>>> interruptible_sleep(2)
>>> t = time.time() - t0
>>> (0.9 <= t <= 1.9) or t
True
>>> _ = signal.signal(signal.SIGALRM, old_handler)
TESTS::
Expand Down Expand Up @@ -346,6 +347,7 @@ cdef class PSelecter:
The file ``/dev/null`` should always be available for reading
and writing::
>>> import os
>>> from cysignals.pselect import PSelecter
>>> f = open(os.devnull, "r+")
>>> sel = PSelecter()
Expand Down Expand Up @@ -389,6 +391,7 @@ cdef class PSelecter:
Open a file and close it, but save the (invalid) file
descriptor::
>>> import os
>>> f = open(os.devnull, "r")
>>> n = f.fileno()
>>> f.close()
Expand Down
7 changes: 5 additions & 2 deletions src/cysignals/pysignals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def getossignal(int sig):
>>> getossignal(signal.SIGUSR1)
<SigAction with sa_handler=SIG_DFL>
>>> def handler(*args): pass
>>> _ = signal.signal(signal.SIGUSR1, handler)
>>> old_handler = signal.signal(signal.SIGUSR1, handler)
>>> getossignal(signal.SIGUSR1)
<SigAction with sa_handler=0x...>
Expand All @@ -182,6 +182,7 @@ def getossignal(int sig):
False
>>> getossignal(signal.SIGABRT) == python_os_handler
False
>>> _ = signal.signal(signal.SIGUSR1, old_handler)
TESTS::
Expand Down Expand Up @@ -274,11 +275,13 @@ def setsignal(int sig, action, osaction=None):
got signal
>>> setsignal(signal.SIGILL, signal.SIG_DFL)
<function handler at 0x...>
>>> _ = setsignal(signal.SIGALRM, signal.SIG_DFL, signal.SIG_IGN)
>>> old_handler = getossignal(signal.SIGALRM)
>>> old_py_handler = setsignal(signal.SIGALRM, signal.SIG_DFL, signal.SIG_IGN)
>>> os.kill(os.getpid(), signal.SIGALRM)
>>> _ = setsignal(signal.SIGALRM, handler, getossignal(signal.SIGSEGV))
>>> os.kill(os.getpid(), signal.SIGALRM)
got signal
>>> _ = setsignal(signal.SIGALRM, old_py_handler, old_handler)
TESTS::
Expand Down
4 changes: 2 additions & 2 deletions src/cysignals/signals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ class AlarmInterrupt(KeyboardInterrupt):
>>> from cysignals import AlarmInterrupt
>>> from signal import alarm
>>> from time import sleep
>>> try:
... _ = alarm(1)
... while True:
... pass
... sleep(2)
... except AlarmInterrupt:
... print("alarm!")
alarm!
Expand Down
14 changes: 4 additions & 10 deletions src/cysignals/tests.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ We disable crash debugging for this test run::
>>> import os
>>> os.environ["CYSIGNALS_CRASH_NDEBUG"] = ""
Verify that the doctester was set up correctly::
>>> import os
>>> os.name == "posix" # doctest: +SKIP_POSIX
False
>>> os.name == "nt" # doctest: +SKIP_WINDOWS
False
"""

#*****************************************************************************
Expand Down Expand Up @@ -787,8 +778,11 @@ def test_interrupt_bomb(long n=100, long p=10):
TESTS::
>>> import sys, pytest
>>> if sys.platform == 'cygwin':
... pytest.skip('this doctest does not work on Windows')
>>> from cysignals.tests import *
>>> test_interrupt_bomb() # doctest: +SKIP_CYGWIN
>>> test_interrupt_bomb()
Received ... interrupts
"""
Expand Down
1 change: 1 addition & 0 deletions src/scripts/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
collect_ignore = ["cysignals-CSI-helper.py"]

0 comments on commit 38a9f75

Please sign in to comment.