From 275bd8e387440e1116552a27be3f580a3ce64993 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Wed, 29 May 2024 20:40:27 +0200 Subject: [PATCH] test get renderer fcn --- .github/workflows/ci.yaml | 4 +-- mplotutils/tests/test_get_renderer.py | 44 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 mplotutils/tests/test_get_renderer.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 31d8df4..93e7a9c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -21,7 +21,7 @@ jobs: strategy: fail-fast: false matrix: - os: ["ubuntu-latest"] + os: ["ubuntu-latest", "macos-latest", "windows-latest"] # Bookend python versions python-version: ["3.9", "3.11", "3.12"] steps: @@ -52,7 +52,7 @@ jobs: python -c "import mplotutils" - name: Run tests timeout-minutes: 5 - run: python -u -m pytest + run: python -u -m pytest -vv --cov=mplotutils --cov-report=xml --junitxml=test-results/${{ runner.os }}-${{ matrix.python-version }}.xml diff --git a/mplotutils/tests/test_get_renderer.py b/mplotutils/tests/test_get_renderer.py new file mode 100644 index 0000000..b66ffaa --- /dev/null +++ b/mplotutils/tests/test_get_renderer.py @@ -0,0 +1,44 @@ +from contextlib import contextmanager + +import matplotlib +import pytest + +from . import figure_context + + +def get_renderer(f): + + if hasattr(f.canvas, "get_renderer"): + return f.canvas.get_renderer() + elif hasattr(f, "_get_renderer"): + return f._get_renderer() + + backend = matplotlib.get_backend() + + raise AttributeError( + f"Could not find a renderer for the '{backend}' backend. Please raise an issue" + ) + + +@contextmanager +def restore_backend(): + + backend = matplotlib.get_backend() + + try: + yield + except Exception: + matplotlib.use(backend) + + +@pytest.mark.parametrize("backend", matplotlib.rcsetup.all_backends) +def test_get_renderer(backend): + + with restore_backend(): + try: + matplotlib.use(backend) + except ImportError: + pytest.skip(backend) + + with figure_context() as f: + get_renderer(f)