1
+ import os
2
+ import tempfile
3
+
1
4
import pytest
2
5
6
+ import matplotlib as mpl
3
7
import matplotlib .pyplot as plt
4
-
5
-
6
- pytest . importorskip ( "matplotlib.backends.backend_macosx" ,
7
- reason = "These are mac only tests" )
8
+ try :
9
+ from matplotlib . backends import _macosx
10
+ except ImportError :
11
+ pytest . skip ( "These are mac only tests" , allow_module_level = True )
8
12
9
13
10
14
@pytest .mark .backend ('macosx' )
@@ -18,3 +22,26 @@ def test_cached_renderer():
18
22
fig = plt .figure (2 )
19
23
fig .draw_without_rendering ()
20
24
assert fig ._cachedRenderer is not None
25
+
26
+
27
+ @pytest .mark .backend ('macosx' )
28
+ def test_savefig_rcparam (monkeypatch ):
29
+
30
+ def new_choose_save_file (title , directory , filename ):
31
+ # Replacement function instead of opening a GUI window
32
+ # Make a new directory for testing the update of the rcParams
33
+ os .makedirs (f"{ directory } /test" )
34
+ return f"{ directory } /test/{ filename } "
35
+
36
+ monkeypatch .setattr (_macosx , "choose_save_file" , new_choose_save_file )
37
+ fig = plt .figure ()
38
+ with tempfile .TemporaryDirectory () as temp_dir , \
39
+ mpl .rc_context ({"savefig.directory" : temp_dir }):
40
+ fig .canvas .toolbar .save_figure ()
41
+ # Check the saved location got created
42
+ save_file = f"{ temp_dir } /test/{ fig .canvas .get_default_filename ()} "
43
+ assert os .path .exists (save_file )
44
+
45
+ # Check the savefig.directory rcParam got updated because
46
+ # we added a subdirectory "test"
47
+ assert mpl .rcParams ["savefig.directory" ] == f"{ temp_dir } /test"
0 commit comments