6
6
import mock
7
7
8
8
import os
9
- import shutil
10
- import stat
11
- import tempfile
12
-
13
- from nose .tools import raises
14
9
15
10
from matplotlib import cbook
16
11
from matplotlib .testing ._conversion_cache import _ConversionCache , _CacheError
12
+ import pytest
17
13
18
14
19
- def test_cache_basic ():
20
- tmpdir = tempfile .mkdtemp ()
21
-
15
+ def test_cache_basic (tmpdir ):
22
16
def intmp (f ):
23
- return os .path .join (tmpdir , f )
17
+ return tmpdir .join (f )
18
+ def fname (f ):
19
+ return str (intmp (f ))
24
20
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' ))
33
26
assert cache .report () == \
34
- {'gets' : {intmp ('fake.pdf' ), intmp ('fake.svg' )},
27
+ {'gets' : {fname ('fake.pdf' ), fname ('fake.svg' )},
35
28
'hits' : set ()}
36
29
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'
43
34
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'
53
42
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' )}}
56
45
finally :
57
- shutil .rmtree (tmpdir )
58
-
46
+ tmpdir .remove (rec = 1 )
59
47
60
- def test_cache_expire ():
61
- tmpdir = tempfile .mkdtemp ()
62
48
49
+ def test_cache_expire (tmpdir ):
63
50
def intmp (* f ):
64
- return os .path .join (tmpdir , * f )
51
+ return tmpdir .join (* f )
52
+ def fname (* f ):
53
+ return str (intmp (* f ))
65
54
try :
66
- cache = _ConversionCache (intmp ('cache' ), 10 )
55
+ cache = _ConversionCache (fname ('cache' ), 10 )
67
56
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 ))
72
60
73
61
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' ))
79
67
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 ))
83
70
84
71
cache .expire ()
85
- assert not os .path .exists (intmp ('cache' , 'file0.png' ))
86
- assert not os .path .exists (intmp ('cache' , 'file1.png' ))
87
- assert <
10000
span class="pl-c1">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' ))
91
78
92
79
finally :
93
- shutil . rmtree ( tmpdir )
80
+ tmpdir . remove ( rec = 1 )
94
81
95
82
96
83
def test_cache_default_dir ():
@@ -101,25 +88,23 @@ def test_cache_default_dir():
101
88
pass
102
89
103
90
104
- @raises (_CacheError )
105
91
@mock .patch ('matplotlib.testing._conversion_cache.cbook.mkdirs' ,
106
92
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 )
113
99
114
100
115
- @raises (_CacheError )
116
101
@mock .patch ('matplotlib.testing._conversion_cache.os.access' ,
117
102
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