Skip to content

Commit 38a9f75

Browse files
committed
Migrate to pytest
1 parent f4a58a7 commit 38a9f75

File tree

12 files changed

+74
-154
lines changed

12 files changed

+74
-154
lines changed

.github/workflows/ci-sage.yml

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,6 @@ concurrency:
5050
group: ${{ github.workflow }}-${{ github.ref }}
5151
cancel-in-progress: true
5252

53-
env:
54-
# Ubuntu packages to install so that the project's "setup.py sdist" can succeed
55-
DIST_PREREQ: python3-setuptools autoconf
56-
# Name of this project in the Sage distribution
57-
SPKG: cysignals
58-
# Sage distribution packages to build
59-
TARGETS_PRE: build/make/Makefile
60-
TARGETS: SAGE_CHECK=no SAGE_CHECK_PACKAGES="cysignals,cypari" cysignals cypari
61-
TARGETS_OPTIONAL: build/make/Makefile
62-
# Standard setting: Test the current beta release of Sage
63-
SAGE_REPO: sagemath/sage
64-
SAGE_REF: develop
65-
REMOVE_PATCHES: "*"
66-
6753
jobs:
6854

6955
dist:
@@ -84,7 +70,8 @@ jobs:
8470
&& echo "sage-package create ${{ env.SPKG }} --version git --tarball ${{ env.SPKG }}-git.tar.gz --type=standard" > upstream/update-pkgs.sh \
8571
&& if [ -n "${{ env.REMOVE_PATCHES }}" ]; then echo "(cd ../build/pkgs/${{ env.SPKG }}/patches && rm -f ${{ env.REMOVE_PATCHES }}; :)" >> upstream/update-pkgs.sh; fi \
8672
&& ls -l upstream/
87-
- uses: actions/upload-artifact@v2
73+
- name: Upload artifact
74+
uses: actions/upload-artifact@v4
8875
with:
8976
path: upstream
9077
name: upstream
@@ -112,7 +99,7 @@ jobs:
11299
choco install make autoconf gcc-core gcc-g++ python3${{ matrix.python-version }}-devel --source cygwin
113100
- name: Install dependencies
114101
run: |
115-
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'
102+
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'
116103
- name: Build and check
117104
run: |
118105
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 }}'
@@ -135,7 +122,8 @@ jobs:
135122
python-version: ${{ matrix.python-version }}
136123
- name: Install dependencies
137124
run: |
138-
python -m pip install --upgrade pip
125+
pip install --upgrade pip
126+
pip install --upgrade -r requirements.txt
139127
- name: Build and check
140128
run: |
141129
make -j4 check
@@ -196,7 +184,8 @@ jobs:
196184
- name: Install dependencies
197185
run: |
198186
brew install autoconf
199-
python -m pip install --upgrade pip
187+
pip install --upgrade pip
188+
pip install --upgrade -r requirements.txt
200189
- name: Build and check
201190
# Work around https://github.com/sagemath/cysignals/issues/179
202191
run: |

MANIFEST.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
global-include README README.rst VERSION LICENSE
22
global-include Makefile configure configure.ac
3-
global-include setup.py rundoctests.py testgdb.py *.pyx
3+
global-include setup.py testgdb.py *.pyx
44
graft src
55
graft docs/source
66
prune build
@@ -11,3 +11,5 @@ prune example/.*
1111
exclude src/config.h
1212
exclude src/cysignals/signals.pxd
1313
exclude src/cysignals/cysignals_config.h
14+
exclude src/cysignals/conftest.py
15+
exclude src/scripts/conftest.py

Makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ PYTHON = python3
66
PIP = $(PYTHON) -m pip -v
77
LS_R = ls -Ra1
88

9-
DOCTEST = $(PYTHON) -B rundoctests.py
10-
119

1210
#####################
1311
# Build
@@ -66,11 +64,11 @@ check-all:
6664
check-install: check-doctest check-example
6765

6866
check-doctest: install
69-
$(DOCTEST) src/cysignals/*.pyx
67+
$(PYTHON) -m pytest .
7068

7169
check-example: install
7270
$(PYTHON) -m pip install -U build setuptools wheel Cython
73-
cd example && $(PYTHON) -m build --no-isolation .
71+
$(PYTHON) -m build --no-isolation example
7472

7573
check-gdb: install
7674
$(PYTHON) testgdb.py

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[build-system]
22
requires = ['setuptools', 'Cython>=0.28']
33
build-backend = "setuptools.build_meta"
4+
5+
[tool.pytest.ini_options]
6+
addopts = "--doctest-modules --import-mode importlib"
7+
norecursedirs = "builddir docs example"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ wheel
33
Cython
44
Sphinx
55
flake8
6+
pytest>=8.0.0

rundoctests.py

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pathlib
2+
3+
from _pytest.nodes import Collector
4+
from _pytest.doctest import DoctestModule
5+
6+
7+
def pytest_collect_file(
8+
file_path: pathlib.Path,
9+
parent: Collector,
10+
):
11+
"""Collect doctests in cython files and run them as test modules."""
12+
config = parent.config
13+
if file_path.suffix == ".pyx":
14+
if config.option.doctestmodules:
15+
return DoctestModule.from_parent(parent, path=file_path)
16+
return None
17+
18+
19+
# Need to import cysignals to initialize it
20+
import cysignals # noqa: E402
21+
22+
try:
23+
import cysignals.alarm
24+
except ImportError:
25+
pass
26+
try:
27+
import cysignals.signals
28+
except ImportError:
29+
pass
30+
try:
31+
import cysignals.pselect
32+
except ImportError:
33+
pass
34+
try:
35+
import cysignals.pysignals
36+
except ImportError:
37+
pass
38+
try:
39+
import cysignals.tests # noqa: F401
40+
except ImportError:
41+
pass

src/cysignals/pselect.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ def interruptible_sleep(double seconds):
8888
>>> import signal, time
8989
>>> def alarm_handler(sig, frame):
9090
... pass
91-
>>> _ = signal.signal(signal.SIGALRM, alarm_handler)
91+
>>> old_handler = signal.signal(signal.SIGALRM, alarm_handler)
9292
>>> t0 = time.time()
9393
>>> _ = signal.alarm(1)
9494
>>> interruptible_sleep(2)
9595
>>> t = time.time() - t0
9696
>>> (0.9 <= t <= 1.9) or t
9797
True
98+
>>> _ = signal.signal(signal.SIGALRM, old_handler)
9899
99100
TESTS::
100101
@@ -346,6 +347,7 @@ cdef class PSelecter:
346347
The file ``/dev/null`` should always be available for reading
347348
and writing::
348349
350+
>>> import os
349351
>>> from cysignals.pselect import PSelecter
350352
>>> f = open(os.devnull, "r+")
351353
>>> sel = PSelecter()
@@ -389,6 +391,7 @@ cdef class PSelecter:
389391
Open a file and close it, but save the (invalid) file
390392
descriptor::
391393
394+
>>> import os
392395
>>> f = open(os.devnull, "r")
393396
>>> n = f.fileno()
394397
>>> f.close()

src/cysignals/pysignals.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def getossignal(int sig):
168168
>>> getossignal(signal.SIGUSR1)
169169
<SigAction with sa_handler=SIG_DFL>
170170
>>> def handler(*args): pass
171-
>>> _ = signal.signal(signal.SIGUSR1, handler)
171+
>>> old_handler = signal.signal(signal.SIGUSR1, handler)
172172
>>> getossignal(signal.SIGUSR1)
173173
<SigAction with sa_handler=0x...>
174174
@@ -182,6 +182,7 @@ def getossignal(int sig):
182182
False
183183
>>> getossignal(signal.SIGABRT) == python_os_handler
184184
False
185+
>>> _ = signal.signal(signal.SIGUSR1, old_handler)
185186
186187
TESTS::
187188
@@ -274,11 +275,13 @@ def setsignal(int sig, action, osaction=None):
274275
got signal
275276
>>> setsignal(signal.SIGILL, signal.SIG_DFL)
276277
<function handler at 0x...>
277-
>>> _ = setsignal(signal.SIGALRM, signal.SIG_DFL, signal.SIG_IGN)
278+
>>> old_handler = getossignal(signal.SIGALRM)
279+
>>> old_py_handler = setsignal(signal.SIGALRM, signal.SIG_DFL, signal.SIG_IGN)
278280
>>> os.kill(os.getpid(), signal.SIGALRM)
279281
>>> _ = setsignal(signal.SIGALRM, handler, getossignal(signal.SIGSEGV))
280282
>>> os.kill(os.getpid(), signal.SIGALRM)
281283
got signal
284+
>>> _ = setsignal(signal.SIGALRM, old_py_handler, old_handler)
282285
283286
TESTS::
284287

src/cysignals/signals.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ class AlarmInterrupt(KeyboardInterrupt):
105105
106106
>>> from cysignals import AlarmInterrupt
107107
>>> from signal import alarm
108+
>>> from time import sleep
108109
>>> try:
109110
... _ = alarm(1)
110-
... while True:
111-
... pass
111+
... sleep(2)
112112
... except AlarmInterrupt:
113113
... print("alarm!")
114114
alarm!

src/cysignals/tests.pyx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ We disable crash debugging for this test run::
88
99
>>> import os
1010
>>> os.environ["CYSIGNALS_CRASH_NDEBUG"] = ""
11-
12-
Verify that the doctester was set up correctly::
13-
14-
>>> import os
15-
>>> os.name == "posix" # doctest: +SKIP_POSIX
16-
False
17-
>>> os.name == "nt" # doctest: +SKIP_WINDOWS
18-
False
19-
2011
"""
2112

2213
#*****************************************************************************
@@ -787,8 +778,11 @@ def test_interrupt_bomb(long n=100, long p=10):
787778
788779
TESTS::
789780
781+
>>> import sys, pytest
782+
>>> if sys.platform == 'cygwin':
783+
... pytest.skip('this doctest does not work on Windows')
790784
>>> from cysignals.tests import *
791-
>>> test_interrupt_bomb() # doctest: +SKIP_CYGWIN
785+
>>> test_interrupt_bomb()
792786
Received ... interrupts
793787
794788
"""

src/scripts/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
collect_ignore = ["cysignals-CSI-helper.py"]

0 commit comments

Comments
 (0)