12
12
from matplotlib .font_manager import (
13
13
findfont , findSystemFonts , FontProperties , fontManager , json_dump ,
14
14
json_load , get_font , get_fontconfig_fonts , is_opentype_cff_font ,
15
- MSUserFontDirectories )
15
+ MSUserFontDirectories , _call_fc_list )
16
16
from matplotlib import pyplot as plt , rc_context
17
17
18
18
has_fclist = shutil .which ('fc-list' ) is not None
@@ -131,7 +131,34 @@ def test_find_ttc():
131
131
fig .savefig (BytesIO (), format = "ps" )
132
132
133
133
134
- def test_user_fonts ():
134
+ @pytest .mark .skipif (sys .platform != 'linux' , reason = 'Linux only' )
135
+ def test_user_fonts_linux (tmpdir , monkeypatch ):
136
+ font_test_file = 'mpltest.ttf'
137
+
138
+ # Precondition: the test font should not be available
139
+ fonts = findSystemFonts ()
140
+ if any (font_test_file in font for font in fonts ):
141
+ pytest .skip (f'{ font_test_file } already exists in system fonts' )
142
+
143
+ # Prepare a temporary user font directory
144
+ user_fonts_dir = tmpdir .join ('fonts' )
145
+ user_fonts_dir .ensure (dir = True )
146
+ shutil .copyfile (Path (__file__ ).parent / font_test_file ,
147
+ user_fonts_dir .join (font_test_file ))
148
+
149
+ with monkeypatch .context () as m :
150
+ m .setenv ('XDG_DATA_HOME' , str (tmpdir ))
151
+ _call_fc_list .cache_clear ()
152
+ # Now, the font should be available
153
+ fonts = findSystemFonts ()
154
+ assert any (font_test_file in font for font in fonts )
155
+
156
+ # Make sure the temporary directory is no longer cached.
157
+ _call_fc_list .cache_clear ()
158
+
159
+
160
+ @pytest .mark .skipif (sys .platform != 'win32' , reason = 'Windows only' )
161
+ def test_user_fonts_win32 ():
135
162
if not os .environ .get ('APPVEYOR' , False ):
136
163
pytest .xfail ('This test does only work on appveyor since user fonts '
137
164
'are Windows specific and the developer\' s font '
@@ -141,7 +168,8 @@ def test_user_fonts():
141
168
142
169
# Precondition: the test font should not be available
143
170
fonts = findSystemFonts ()
144
- assert not any (font_test_file in font for font in fonts )
171
+ if any (font_test_file in font for font in fonts ):
172
+ pytest .skip (f'{ font_test_file } already exists in system fonts' )
145
173
146
174
user_fonts_dir = MSUserFontDirectories [0 ]
147
175
0 commit comments