1
1
import os
2
+ from pathlib import Path
2
3
3
4
import pytest
4
5
from unittest import mock
5
6
6
7
import matplotlib as mpl
7
8
import 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
12
10
13
11
14
- @pytest .mark .backend ('macosx' )
15
- def test_cached_renderer ():
12
+ _test_timeout = 60
13
+
14
+
15
+ def _test_cached_renderer ():
16
16
# Make sure that figures have an associated renderer after
17
17
# a fig.canvas.draw() call
18
18
fig = plt .figure (1 )
@@ -24,8 +24,14 @@ def test_cached_renderer():
24
24
assert fig .canvas .get_renderer ()._renderer is not None
25
25
26
26
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" ])
29
35
30
36
def new_choose_save_file (title , directory , filename ):
31
37
# Replacement function instead of opening a GUI window
@@ -34,9 +40,10 @@ def new_choose_save_file(title, directory, filename):
34
40
os .makedirs (f"{ directory } /test" )
35
41
return f"{ directory } /test/{ filename } "
36
42
37
- monkeypatch .setattr (_macosx , "choose_save_file" , new_choose_save_file )
38
43
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 })):
40
47
fig .canvas .toolbar .save_figure ()
41
48
# Check the saved location got created
42
49
save_file = f"{ tmp_path } /test/{ fig .canvas .get_default_filename ()} "
@@ -47,14 +54,20 @@ def new_choose_save_file(title, directory, filename):
47
54
assert mpl .rcParams ["savefig.directory" ] == f"{ tmp_path } /test"
48
55
49
56
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 )
51
65
def test_ipython ():
52
66
from matplotlib .testing import ipython_in_subprocess
53
67
ipython_in_subprocess ("osx" , {(8 , 24 ): "macosx" , (7 , 0 ): "MacOSX" })
54
68
55
69
56
- @pytest .mark .backend ('macosx' )
57
- def test_save_figure_return ():
70
+ def _test_save_figure_return ():
58
71
fig , ax = plt .subplots ()
59
72
ax .imshow ([[1 ]])
60
73
prop = "matplotlib.backends._macosx.choose_save_file"
@@ -65,3 +78,9 @@ def test_save_figure_return():
65
78
with mock .patch (prop , return_value = None ):
66
79
fname = fig .canvas .manager .toolbar .save_figure ()
67
80
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