From 05083a70f74664c892ccf216cf1eeba4d4aa156e Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 11 Jul 2023 14:39:17 -0400 Subject: [PATCH 1/4] gitignore: drop extension module build path from Python 2.x Signed-off-by: Benjamin Gilbert --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 78dc7415..87d0e176 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ /dist /MANIFEST /*.egg-info -/openslide/_convert*.so *.pyc From 8796961e291b0c7a9f7c43fed6c353ba59dc50ce Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 11 Jul 2023 14:22:31 -0400 Subject: [PATCH 2/4] tests: move fixtures into subdirectory Minor cleanup. Signed-off-by: Benjamin Gilbert --- .github/workflows/python.yml | 4 ++-- tests/__init__.py | 2 +- tests/{ => fixtures}/boxes.png | Bin tests/{ => fixtures}/boxes.tiff | Bin tests/{ => fixtures}/small.svs | Bin tests/{ => fixtures}/unopenable.tiff | Bin tests/{ => fixtures}/unreadable.svs | Bin 7 files changed, 3 insertions(+), 3 deletions(-) rename tests/{ => fixtures}/boxes.png (100%) rename tests/{ => fixtures}/boxes.tiff (100%) rename tests/{ => fixtures}/small.svs (100%) rename tests/{ => fixtures}/unopenable.tiff (100%) rename tests/{ => fixtures}/unreadable.svs (100%) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 3d6ef46b..cce2e116 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -57,7 +57,7 @@ jobs: - name: Run tests run: pytest -v - name: Tile slide - run: python examples/deepzoom/deepzoom_tile.py --viewer -o tiled tests/small.svs + run: python examples/deepzoom/deepzoom_tile.py --viewer -o tiled tests/fixtures/small.svs windows: name: Windows needs: pre-commit @@ -117,7 +117,7 @@ jobs: python examples/deepzoom/deepzoom_tile.py -h >/dev/null - name: Tile slide # Reads OPENSLIDE_PATH - run: python examples/deepzoom/deepzoom_tile.py --viewer -o tiled tests/small.svs + run: python examples/deepzoom/deepzoom_tile.py --viewer -o tiled tests/fixtures/small.svs - name: Archive wheel uses: actions/upload-artifact@v3 with: diff --git a/tests/__init__.py b/tests/__init__.py index b65dafdf..1ca8fc25 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -45,7 +45,7 @@ def file_path(name): - return Path(__file__).parent / name + return Path(__file__).parent / 'fixtures' / name def maybe_supported(f): diff --git a/tests/boxes.png b/tests/fixtures/boxes.png similarity index 100% rename from tests/boxes.png rename to tests/fixtures/boxes.png diff --git a/tests/boxes.tiff b/tests/fixtures/boxes.tiff similarity index 100% rename from tests/boxes.tiff rename to tests/fixtures/boxes.tiff diff --git a/tests/small.svs b/tests/fixtures/small.svs similarity index 100% rename from tests/small.svs rename to tests/fixtures/small.svs diff --git a/tests/unopenable.tiff b/tests/fixtures/unopenable.tiff similarity index 100% rename from tests/unopenable.tiff rename to tests/fixtures/unopenable.tiff diff --git a/tests/unreadable.svs b/tests/fixtures/unreadable.svs similarity index 100% rename from tests/unreadable.svs rename to tests/fixtures/unreadable.svs From 509af984051a6837957c59629f0b0701fa8bae59 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 11 Jul 2023 17:17:18 -0400 Subject: [PATCH 3/4] tests: move __init__ to common Treat tests as a directory full of modules, rather than as a package. This will allow us to stop adding the root of the repo to the Python path. Signed-off-by: Benjamin Gilbert --- tests/{__init__.py => common.py} | 0 tests/test_base.py | 4 ++-- tests/test_deepzoom.py | 4 ++-- tests/test_imageslide.py | 3 +-- tests/test_openslide.py | 3 +-- 5 files changed, 6 insertions(+), 8 deletions(-) rename tests/{__init__.py => common.py} (100%) diff --git a/tests/__init__.py b/tests/common.py similarity index 100% rename from tests/__init__.py rename to tests/common.py diff --git a/tests/test_base.py b/tests/test_base.py index dc961625..1ffc186f 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -19,9 +19,9 @@ import unittest -from openslide import ImageSlide, OpenSlide, open_slide +from common import file_path -from . import file_path +from openslide import ImageSlide, OpenSlide, open_slide class TestLibrary(unittest.TestCase): diff --git a/tests/test_deepzoom.py b/tests/test_deepzoom.py index 332cf0a4..892d9d04 100644 --- a/tests/test_deepzoom.py +++ b/tests/test_deepzoom.py @@ -19,11 +19,11 @@ import unittest +from common import file_path + from openslide import ImageSlide, OpenSlide from openslide.deepzoom import DeepZoomGenerator -from . import file_path - class _BoxesDeepZoomTest: def setUp(self): diff --git a/tests/test_imageslide.py b/tests/test_imageslide.py index 64540dc5..d3a22977 100644 --- a/tests/test_imageslide.py +++ b/tests/test_imageslide.py @@ -20,11 +20,10 @@ import unittest from PIL import Image +from common import file_path, maybe_supported from openslide import ImageSlide, OpenSlideCache, OpenSlideError -from . import file_path, maybe_supported - class TestImageWithoutOpening(unittest.TestCase): def test_detect_format(self): diff --git a/tests/test_openslide.py b/tests/test_openslide.py index 62de390a..52bffdd2 100644 --- a/tests/test_openslide.py +++ b/tests/test_openslide.py @@ -23,6 +23,7 @@ import unittest from PIL import Image +from common import file_path, maybe_supported from openslide import ( OpenSlide, @@ -31,8 +32,6 @@ OpenSlideUnsupportedFormatError, ) -from . import file_path, maybe_supported - class TestCache(unittest.TestCase): @maybe_supported From 7d2b9ddee02434178a6e0c4c162e2e9d373a0e64 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 11 Jul 2023 15:00:28 -0400 Subject: [PATCH 4/4] Configure pytest to use importlib import mode By default, running `pytest` prepends the source directory to the Python path, since that's where the `tests` package is. That causes Python to import `openslide` from the source directory instead of using the installed copy, and this fails because the source directory doesn't have the compiled extension module. Configure pytest not to change the Python path, as recommended by pytest docs: https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#choosing-an-import-mode Then, manually set the Python path to allow tests to load common code. Signed-off-by: Benjamin Gilbert --- .github/workflows/python.yml | 2 +- MANIFEST.in | 2 +- pytest.ini | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 pytest.ini diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index cce2e116..d8790842 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -53,7 +53,7 @@ jobs: ;; esac - name: Install - run: pip install -e . + run: pip install . - name: Run tests run: pytest -v - name: Tile slide diff --git a/MANIFEST.in b/MANIFEST.in index f0efabdd..c0b2779b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -include *.md +include *.md pytest.ini recursive-include doc *.py *.rst recursive-include examples *.html *.js *.png *.py recursive-include tests *.png *.py *.svs *.tiff diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..218e0493 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,7 @@ +[pytest] +minversion = 6.0 +# don't try to import openslide from the source directory, since it doesn't +# have the compiled extension module +addopts = --import-mode importlib +# allow tests to import common module +pythonpath = tests