11import sys , os
2+ from test .helper import create_named_tmpfile
23
34if not os .environ .get ("TINIFY_KEY" ):
45 sys .exit ("Set the TINIFY_KEY environment variable." )
@@ -13,11 +14,13 @@ class ClientIntegrationTest(unittest.TestCase):
1314 optimized = tinify .from_file (unoptimized_path )
1415
1516 def test_should_compress_from_file (self ):
16- with tempfile . NamedTemporaryFile () as tmp :
17- self .optimized .to_file (tmp . name )
17+ with create_named_tmpfile () as tmp :
18+ self .optimized .to_file (tmp )
1819
19- size = os .path .getsize (tmp .name )
20- contents = tmp .read ()
20+ size = os .path .getsize (tmp )
21+
22+ with open (tmp , 'rb' ) as f :
23+ contents = f .read ()
2124
2225 self .assertTrue (1000 < size < 1500 )
2326
@@ -27,11 +30,12 @@ def test_should_compress_from_file(self):
2730
2831 def test_should_compress_from_url (self ):
2932 source = tinify .from_url ('https://raw.githubusercontent.com/tinify/tinify-python/master/test/examples/voormedia.png' )
30- with tempfile . NamedTemporaryFile () as tmp :
31- source .to_file (tmp . name )
33+ with create_named_tmpfile () as tmp :
34+ source .to_file (tmp )
3235
33- size = os .path .getsize (tmp .name )
34- contents = tmp .read ()
36+ size = os .path .getsize (tmp )
37+ with open (tmp , 'rb' ) as f :
38+ contents = f .read ()
3539
3640 self .assertTrue (1000 < size < 1500 )
3741
@@ -40,11 +44,12 @@ def test_should_compress_from_url(self):
4044 self .assertNotIn (b'Copyright Voormedia' , contents )
4145
4246 def test_should_resize (self ):
43- with tempfile . NamedTemporaryFile () as tmp :
44- self .optimized .resize (method = "fit" , width = 50 , height = 20 ).to_file (tmp . name )
47+ with create_named_tmpfile () as tmp :
48+ self .optimized .resize (method = "fit" , width = 50 , height = 20 ).to_file (tmp )
4549
46- size = os .path .getsize (tmp .name )
47- contents = tmp .read ()
50+ size = os .path .getsize (tmp )
51+ with open (tmp , 'rb' ) as f :
52+ contents = f .read ()
4853
4954 self .assertTrue (500 < size < 1000 )
5055
@@ -53,14 +58,15 @@ def test_should_resize(self):
5358 self .assertNotIn (b'Copyright Voormedia' , contents )
5459
5560 def test_should_preserve_metadata (self ):
56- with tempfile . NamedTemporaryFile () as tmp :
57- self .optimized .preserve ("copyright" , "creation" ).to_file (tmp . name )
61+ with create_named_tmpfile () as tmp :
62+ self .optimized .preserve ("copyright" , "creation" ).to_file (tmp )
5863
59- size = os .path .getsize (tmp .name )
60- contents = tmp .read ()
64+ size = os .path .getsize (tmp )
65+ with open (tmp , 'rb' ) as f :
66+ contents = f .read ()
6167
6268 self .assertTrue (1000 < size < 2000 )
6369
6470 # width == 137
6571 self .assertIn (b'\x00 \x00 \x00 \x89 ' , contents )
66- self .assertIn (b'Copyright Voormedia' , contents )
72+ self .assertIn (b'Copyright Voormedia' , contents )
0 commit comments