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,32 @@ 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 ():
29
+ # Store the save file so we can overwrite it and
30
+ # then put back the real one after this
31
+ old_save_file_func = _macosx .choose_save_file
32
+
33
+ def new_choose_save_file (title ,directory ,filename ):
34
+ # Replacement function instead of opening a GUI window
35
+ # Make a new directory for testing the update of the rcParams
36
+ os .makedirs (f"{ directory } /test" )
37
+ return f"{ directory } /test/{ filename } "
38
+
39
+ _macosx .choose_save_file = new_choose_save_file
40
+ fig = plt .figure ()
41
+ with tempfile .TemporaryDirectory ()as temp_dir , \
42
+ mpl .rc_context ({"savefig.directory" :temp_dir }):
43
+ fig .canvas .toolbar .save_figure ()
44
+ # Check the saved location got created
45
+ save_file = f"{ temp_dir } /test/{ fig .canvas .get_default_filename ()} "
46
+ assert os .path .exists (save_file )
47
+
48
+ # Check the savefig.directory rcParam got updated because
49
+ # we added a subdirectory "test"
50
+ assert mpl .rcParams ["savefig.directory" ]== f"{ temp_dir } /test"
51
+
52
+ # Replace the original function
53
+ _macosx .choose_save_file = old_save_file_func