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