From c97d30d4c7cc96932d80755563de79a3a6402f72 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 16 Nov 2024 04:22:48 -0500 Subject: [PATCH] TST: Run macosx backends in a subprocess In the macosx backend, `event_loop_is_running` returns true after `lazy_init` is called, which happens in the `new` constructor for _any_ class. This means it is "running" as soon as a figure is created, and this will block any other backend tests. --- lib/matplotlib/tests/test_backend_macosx.py | 45 +++++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_macosx.py b/lib/matplotlib/tests/test_backend_macosx.py index 8e50ddf84024..fe4c9a6fba3c 100644 --- a/lib/matplotlib/tests/test_backend_macosx.py +++ b/lib/matplotlib/tests/test_backend_macosx.py @@ -1,18 +1,18 @@ import os +from pathlib import Path import pytest from unittest import mock import matplotlib as mpl import matplotlib.pyplot as plt -try: - from matplotlib.backends import _macosx -except ImportError: - pytest.skip("These are mac only tests", allow_module_level=True) +from matplotlib.testing import subprocess_run_helper -@pytest.mark.backend('macosx') -def test_cached_renderer(): +_test_timeout = 60 + + +def _test_cached_renderer(): # Make sure that figures have an associated renderer after # a fig.canvas.draw() call fig = plt.figure(1) @@ -24,8 +24,14 @@ def test_cached_renderer(): assert fig.canvas.get_renderer()._renderer is not None -@pytest.mark.backend('macosx') -def test_savefig_rcparam(monkeypatch, tmp_path): +@pytest.mark.backend('macosx', skip_on_importerror=True) +def test_cached_renderer(): + subprocess_run_helper(_test_cached_renderer, timeout=_test_timeout, + extra_env={"MPLBACKEND": "macosx"}) + + +def _test_savefig_rcparam(): + tmp_path = Path(os.environ["TEST_SAVEFIG_PATH"]) def new_choose_save_file(title, directory, filename): # Replacement function instead of opening a GUI window @@ -34,9 +40,10 @@ def new_choose_save_file(title, directory, filename): os.makedirs(f"{directory}/test") return f"{directory}/test/{filename}" - monkeypatch.setattr(_macosx, "choose_save_file", new_choose_save_file) fig = plt.figure() - with mpl.rc_context({"savefig.directory": tmp_path}): + with (mock.patch("matplotlib.backends._macosx.choose_save_file", + new_choose_save_file), + mpl.rc_context({"savefig.directory": tmp_path})): fig.canvas.toolbar.save_figure() # Check the saved location got created save_file = f"{tmp_path}/test/{fig.canvas.get_default_filename()}" @@ -47,14 +54,20 @@ def new_choose_save_file(title, directory, filename): assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test" -@pytest.mark.backend('macosx') +@pytest.mark.backend('macosx', skip_on_importerror=True) +def test_savefig_rcparam(tmp_path): + subprocess_run_helper( + _test_savefig_rcparam, timeout=_test_timeout, + extra_env={"MPLBACKEND": "macosx", "TEST_SAVEFIG_PATH": tmp_path}) + + +@pytest.mark.backend('macosx', skip_on_importerror=True) def test_ipython(): from matplotlib.testing import ipython_in_subprocess ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"}) -@pytest.mark.backend('macosx') -def test_save_figure_return(): +def _test_save_figure_return(): fig, ax = plt.subplots() ax.imshow([[1]]) prop = "matplotlib.backends._macosx.choose_save_file" @@ -65,3 +78,9 @@ def test_save_figure_return(): with mock.patch(prop, return_value=None): fname = fig.canvas.manager.toolbar.save_figure() assert fname is None + + +@pytest.mark.backend('macosx', skip_on_importerror=True) +def test_save_figure_return(): + subprocess_run_helper(_test_save_figure_return, timeout=_test_timeout, + extra_env={"MPLBACKEND": "macosx"})