11import os
2+ from pathlib import Path
23
34import pytest
45from unittest import mock
56
67import matplotlib as mpl
78import matplotlib .pyplot as plt
8- try :
9- from matplotlib .backends import _macosx
10- except ImportError :
11- pytest .skip ("These are mac only tests" , allow_module_level = True )
9+ from matplotlib .testing import subprocess_run_helper
1210
1311
14- @pytest .mark .backend ('macosx' )
15- def test_cached_renderer ():
12+ _test_timeout = 60
13+
14+
15+ def _test_cached_renderer ():
1616 # Make sure that figures have an associated renderer after
1717 # a fig.canvas.draw() call
1818 fig = plt .figure (1 )
@@ -24,8 +24,14 @@ def test_cached_renderer():
2424 assert fig .canvas .get_renderer ()._renderer is not None
2525
2626
27- @pytest .mark .backend ('macosx' )
28- def test_savefig_rcparam (monkeypatch , tmp_path ):
27+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
28+ def test_cached_renderer ():
29+ subprocess_run_helper (_test_cached_renderer , timeout = _test_timeout ,
30+ extra_env = {"MPLBACKEND" : "macosx" })
31+
32+
33+ def _test_savefig_rcparam ():
34+ tmp_path = Path (os .environ ["TEST_SAVEFIG_PATH" ])
2935
3036 def new_choose_save_file (title , directory , filename ):
3137 # Replacement function instead of opening a GUI window
@@ -34,9 +40,10 @@ def new_choose_save_file(title, directory, filename):
3440 os .makedirs (f"{ directory } /test" )
3541 return f"{ directory } /test/{ filename } "
3642
37- monkeypatch .setattr (_macosx , "choose_save_file" , new_choose_save_file )
3843 fig = plt .figure ()
39- with mpl .rc_context ({"savefig.directory" : tmp_path }):
44+ with (mock .patch ("matplotlib.backends._macosx.choose_save_file" ,
45+ new_choose_save_file ),
46+ mpl .rc_context ({"savefig.directory" : tmp_path })):
4047 fig .canvas .toolbar .save_figure ()
4148 # Check the saved location got created
4249 save_file = f"{ tmp_path } /test/{ fig .canvas .get_default_filename ()} "
@@ -47,14 +54,20 @@ def new_choose_save_file(title, directory, filename):
4754 assert mpl .rcParams ["savefig.directory" ] == f"{ tmp_path } /test"
4855
4956
50- @pytest .mark .backend ('macosx' )
57+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
58+ def test_savefig_rcparam (tmp_path ):
59+ subprocess_run_helper (
60+ _test_savefig_rcparam , timeout = _test_timeout ,
61+ extra_env = {"MPLBACKEND" : "macosx" , "TEST_SAVEFIG_PATH" : tmp_path })
62+
63+
64+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
5165def test_ipython ():
5266 from matplotlib .testing import ipython_in_subprocess
5367 ipython_in_subprocess ("osx" , {(8 , 24 ): "macosx" , (7 , 0 ): "MacOSX" })
5468
5569
56- @pytest .mark .backend ('macosx' )
57- def test_save_figure_return ():
70+ def _test_save_figure_return ():
5871 fig , ax = plt .subplots ()
5972 ax .imshow ([[1 ]])
6073 prop = "matplotlib.backends._macosx.choose_save_file"
@@ -65,3 +78,9 @@ def test_save_figure_return():
6578 with mock .patch (prop , return_value = None ):
6679 fname = fig .canvas .manager .toolbar .save_figure ()
6780 assert fname is None
81+
82+
83+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
84+ def test_save_figure_return ():
85+ subprocess_run_helper (_test_save_figure_return , timeout = _test_timeout ,
86+ extra_env = {"MPLBACKEND" : "macosx" })
0 commit comments