Skip to content

Commit df9204b

Browse files
committed
TST: add a test for tilde in tempfile for the PS backend
The PS uses latex in some cases (usetex=True) and latex does not like to be called with dirnames with ~ in it (reserved char). The Fix for that was in #5928, this is just a testcase to test for the fix.
1 parent 7b517da commit df9204b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

lib/matplotlib/tests/test_backend_ps.py

+40
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,46 @@ def test_patheffects():
134134
fig.savefig(ps, format='ps')
135135

136136

137+
@cleanup
138+
@needs_tex
139+
@needs_ghostscript
140+
def test_tilde_in_tempfilename():
141+
# Tilde ~ in the tempdir path (e.g. TMPDIR, TMP oder TEMP on windows
142+
# when the username is very long and windows uses a short name) breaks
143+
# latex before https://github.com/matplotlib/matplotlib/pull/5928
144+
import tempfile
145+
import shutil
146+
import os
147+
import os.path
148+
149+
tempdir = None
150+
old_tempdir = tempfile.tempdir
151+
try:
152+
# change the path for new tempdirs, which is used
153+
# internally by the ps backend to write a file
154+
tempdir = tempfile.mkdtemp()
155+
base_tempdir = os.path.join(tempdir, "short~1")
156+
os.makedirs(base_tempdir)
157+
tempfile.tempdir = base_tempdir
158+
159+
# usetex results in the latex call, which does not like the ~
160+
plt.rc('text', usetex=True)
161+
plt.plot([1, 2, 3, 4])
162+
plt.xlabel(r'\textbf{time} (s)')
163+
#matplotlib.verbose.set_level("debug")
164+
output_eps = os.path.join(base_tempdir, 'tex_demo.eps')
165+
# use the PS backend to write the file...
166+
plt.savefig(output_eps, format="ps")
167+
finally:
168+
tempfile.tempdir = old_tempdir
169+
if tempdir:
170+
try:
171+
shutil.rmtree(tempdir)
172+
except Exception as e:
173+
# do not break if this is not removeable...
174+
print(e)
175+
176+
137177
if __name__ == '__main__':
138178
import nose
139179
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)