66 import mock
77
88import os
9- import shutil
10- import stat
11- import tempfile
12-
13- from nose .tools import raises
149
1510from matplotlib import cbook
1611from matplotlib .testing ._conversion_cache import _ConversionCache , _CacheError
12+ import pytest
1713
1814
19- def test_cache_basic ():
20- tmpdir = tempfile .mkdtemp ()
21-
15+ def test_cache_basic (tmpdir ):
2216 def intmp (f ):
23- return os .path .join (tmpdir , f )
17+ return tmpdir .join (f )
18+ def fname (f ):
19+ return str (intmp (f ))
2420 try :
25- cache = _ConversionCache (intmp ('cache' ))
26- with open (intmp ('fake.pdf' ), 'w' ) as pdf :
27- pdf .write ('this is a fake pdf file' )
28- with open (intmp ('fake.svg' ), 'w' ) as svg :
29- svg .write ('this pretends to be an svg file' )
30-
31- assert not cache .get (intmp ('fake.pdf' ), intmp ('fakepdf.png' ))
32- assert not cache .get (intmp ('fake.svg' ), intmp ('fakesvg.png' ))
21+ cache = _ConversionCache (fname ('cache' ))
22+ intmp ('fake.pdf' ).write_binary (b'this is a fake pdf file' )
23+ intmp ('fake.svg' ).write_binary (b'this pretends to be an svg file' )
24+ assert not cache .get (fname ('fake.pdf' ), fname ('fakepdf.png' ))
25+ assert not cache .get (fname ('fake.svg' ), fname ('fakesvg.png' ))
3326 assert cache .report () == \
34- {'gets' : {intmp ('fake.pdf' ), intmp ('fake.svg' )},
27+ {'gets' : {fname ('fake.pdf' ), fname ('fake.svg' )},
3528 'hits' : set ()}
3629
37- with open (intmp ('fakepdf.png' ), 'w' ) as png :
38- png .write ('generated from the pdf file' )
39- cache .put (intmp ('fake.pdf' ), intmp ('fakepdf.png' ))
40- assert cache .get (intmp ('fake.pdf' ), intmp ('copypdf.png' ))
41- with open (intmp ('copypdf.png' ), 'r' ) as copy :
42- assert copy .read () == 'generated from the pdf file'
30+ intmp ('fakepdf.png' ).write_binary (b'generated from the pdf file' )
31+ cache .put (fname ('fake.pdf' ), fname ('fakepdf.png' ))
32+ assert cache .get (fname ('fake.pdf' ), fname ('copypdf.png' ))
33+ assert intmp ('copypdf.png' ).read () == 'generated from the pdf file'
4334 assert cache .report () == \
44- {'gets' : {intmp ('fake.pdf' ), intmp ('fake.svg' )},
45- 'hits' : set ([intmp ('fake.pdf' )])}
46-
47- with open (intmp ('fakesvg.png' ), 'w' ) as png :
48- png .write ('generated from the svg file' )
49- cache .put (intmp ('fake.svg' ), intmp ('fakesvg.png' ))
50- assert cache .get (intmp ('fake.svg' ), intmp ('copysvg.png' ))
51- with open (intmp ('copysvg.png' ), 'r' ) as copy :
52- assert copy .read () == 'generated from the svg file'
35+ {'gets' : {fname ('fake.pdf' ), fname ('fake.svg' )},
36+ 'hits' : {fname ('fake.pdf' )}}
37+
38+ intmp ('fakesvg.png' ).write_binary (b'generated from the svg file' )
39+ cache .put (fname ('fake.svg' ), fname ('fakesvg.png' ))
40+ assert cache .get (fname ('fake.svg' ), fname ('copysvg.png' ))
41+ assert intmp ('copysvg.png' ).read () == 'generated from the svg file'
5342 assert cache .report () == \
54- {'gets' : {intmp ('fake.pdf' ), intmp ('fake.svg' )},
55- 'hits' : {intmp ('fake.pdf' ), intmp ('fake.svg' )}}
43+ {'gets' : {fname ('fake.pdf' ), fname ('fake.svg' )},
44+ 'hits' : {fname ('fake.pdf' ), fname ('fake.svg' )}}
5645 finally :
57- shutil .rmtree (tmpdir )
58-
46+ tmpdir .remove (rec = 1 )
5947
60- def test_cache_expire ():
61- tmpdir = tempfile .mkdtemp ()
6248
49+ def test_cache_expire (tmpdir ):
6350 def intmp (* f ):
64- return os .path .join (tmpdir , * f )
51+ return tmpdir .join (* f )
52+ def fname (* f ):
53+ return str (intmp (* f ))
6554 try :
66- cache = _ConversionCache (intmp ('cache' ), 10 )
55+ cache = _ConversionCache (fname ('cache' ), 10 )
6756 for i in range (5 ):
68- filename = intmp ('cache' , 'file%d.png' % i )
69- with open (filename , 'w' ) as f :
70- f .write ('1234' )
71- os .utime (filename , (i * 1000 , i * 1000 ))
57+ pngfile = intmp ('cache' , 'file%d.png' % i )
58+ pngfile .write_binary (b'1234' )
59+ os .utime (str (pngfile ), (i * 1000 , i * 1000 ))
7260
7361 cache .expire ()
74- assert not os .path .exists (intmp ('cache' , 'file0.png' ))
75- assert not os .path .exists (intmp ('cache' , 'file1.png' ))
76- assert not os .path .exists (intmp ('cache' , 'file2.png' ))
77- assert os .path .exists (intmp ('cache' , 'file3.png' ))
78- assert os .path .exists (intmp ('cache' , 'file4.png' ))
62+ assert not os .path .exists (fname ('cache' , 'file0.png' ))
63+ assert not os .path .exists (fname ('cache' , 'file1.png' ))
64+ assert not os .path .exists (fname ('cache' , 'file2.png' ))
65+ assert os .path .exists (fname ('cache' , 'file3.png' ))
66+ assert os .path .exists (fname ('cache' , 'file4.png' ))
7967
80- with open (intmp ('cache' , 'onemore.png' ), 'w' ) as f :
81- f .write ('x' * 11 )
82- os .utime (intmp ('cache' , 'onemore.png' ), (5000 , 5000 ))
68+ intmp ('cache' , 'onemore.png' ).write_binary (b'x' * 11 )
69+ os .utime (fname ('cache' , 'onemore.png' ), (5000 , 5000 ))
8370
8471 cache .expire ()
85- assert not os .path .exists (intmp ('cache' , 'file0.png' ))
86- assert not os .path .exists (intmp ('cache' , 'file1.png' ))
87- assert not os .path .exists (intmp ('cache' , 'file2.png' ))
88- assert not os .path .exists (intmp ('cache' , 'file3.png' ))
89- assert not os .path .exists (intmp ('cache' , 'file4.png' ))
90- assert not os .path .exists (intmp ('cache' , 'onemore.png' ))
72+ assert not os .path .exists (fname ('cache' , 'file0.png' ))
73+ assert not os .path .exists (fname ('cache' , 'file1.png' ))
74+ assert not os .path .exists (fname ('cache' , 'file2.png' ))
75+ assert not os .path .exists (fname ('cache' , 'file3.png' ))
76+ assert not os .path .exists (fname ('cache' , 'file4.png' ))
77+ assert not os .path .exists (fname ('cache' , 'onemore.png' ))
9178
9279 finally :
93- shutil . rmtree ( tmpdir )
80+ tmpdir . remove ( rec = 1 )
9481
9582
9683def test_cache_default_dir ():
@@ -101,25 +88,23 @@ def test_cache_default_dir():
10188 pass
10289
10390
104- @raises (_CacheError )
10591@mock .patch ('matplotlib.testing._conversion_cache.cbook.mkdirs' ,
10692 side_effect = OSError )
107- def test_cache_mkdir_error (mkdirs ):
108- tmpdir = tempfile . mkdtemp ()
109- try :
110- c = _ConversionCache (os . path . join (tmpdir , 'cache' ))
111- finally :
112- shutil . rmtree ( tmpdir )
93+ def test_cache_mkdir_error (mkdirs , tmpdir ):
94+ with pytest . raises ( _CacheError ):
95+ try :
96+ c = _ConversionCache (str ( tmpdir . join ('cache' ) ))
97+ finally :
98+ tmpdir . remove ( rec = 1 )
11399
114100
115- @raises (_CacheError )
116101@mock .patch ('matplotlib.testing._conversion_cache.os.access' ,
117102 side_effect = [False ])
118- def test_cache_unwritable_error (access ):
119- tmpdir = tempfile . mkdtemp ()
120- cachedir = os . path . join (tmpdir , 'test_cache ' )
121- try :
122- cbook . mkdirs ( cachedir )
123- c = _ConversionCache (cachedir )
124- finally :
125- shutil . rmtree ( tmpdir )
103+ def test_cache_unwritable_error (access , tmpdir ):
104+ with pytest . raises ( _CacheError ):
105+ cachedir = tmpdir . join ('cache ' )
106+ cachedir . ensure ( dir = True )
107+ try :
108+ c = _ConversionCache (str ( cachedir ) )
109+ finally :
110+ tmpdir . remove ( rec = 1 )
0 commit comments