11import sys , os
2+ from contextlib import contextmanager
3+ import tinify , unittest , tempfile
24
35if not os .environ .get ("TINIFY_KEY" ):
46 sys .exit ("Set the TINIFY_KEY environment variable." )
57
6- import tinify , unittest , tempfile
8+ @contextmanager
9+ def create_named_tmpfile ():
10+ # Due to NamedTemporaryFile requiring to be closed when used on Windows
11+ # we create our own NamedTemporaryFile contextmanager
12+ # See note: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
13+
14+ tmp = tempfile .NamedTemporaryFile (delete = False )
15+ try :
16+ tmp .close ()
17+ yield tmp .name
18+ finally :
19+ os .unlink (tmp .name )
20+
721
822class ClientIntegrationTest (unittest .TestCase ):
923 tinify .key = os .environ .get ("TINIFY_KEY" )
@@ -13,11 +27,13 @@ class ClientIntegrationTest(unittest.TestCase):
1327 optimized = tinify .from_file (unoptimized_path )
1428
1529 def test_should_compress_from_file (self ):
16- with tempfile .NamedTemporaryFile () as tmp :
17- self .optimized .to_file (tmp .name )
30+ with create_named_tmpfile () as tmp :
31+ self .optimized .to_file (tmp )
32+
33+ size = os .path .getsize (tmp )
1834
19- size = os . path . getsize (tmp . name )
20- contents = tmp .read ()
35+ with open (tmp , 'rb' ) as f :
36+ contents = f .read ()
2137
2238 self .assertTrue (1000 < size < 1500 )
2339
@@ -27,11 +43,12 @@ def test_should_compress_from_file(self):
2743
2844 def test_should_compress_from_url (self ):
2945 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 )
46+ with create_named_tmpfile () as tmp :
47+ source .to_file (tmp )
3248
33- size = os .path .getsize (tmp .name )
34- contents = tmp .read ()
49+ size = os .path .getsize (tmp )
50+ with open (tmp , 'rb' ) as f :
51+ contents = f .read ()
3552
3653 self .assertTrue (1000 < size < 1500 )
3754
@@ -40,11 +57,12 @@ def test_should_compress_from_url(self):
4057 self .assertNotIn (b'Copyright Voormedia' , contents )
4158
4259 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 )
60+ with create_named_tmpfile () as tmp :
61+ self .optimized .resize (method = "fit" , width = 50 , height = 20 ).to_file (tmp )
4562
46- size = os .path .getsize (tmp .name )
47- contents = tmp .read ()
63+ size = os .path .getsize (tmp )
64+ with open (tmp , 'rb' ) as f :
65+ contents = f .read ()
4866
4967 self .assertTrue (500 < size < 1000 )
5068
@@ -53,14 +71,15 @@ def test_should_resize(self):
5371 self .assertNotIn (b'Copyright Voormedia' , contents )
5472
5573 def test_should_preserve_metadata (self ):
56- with tempfile . NamedTemporaryFile () as tmp :
57- self .optimized .preserve ("copyright" , "creation" ).to_file (tmp . name )
74+ with create_named_tmpfile () as tmp :
75+ self .optimized .preserve ("copyright" , "creation" ).to_file (tmp )
5876
59- size = os .path .getsize (tmp .name )
60- contents = tmp .read ()
77+ size = os .path .getsize (tmp )
78+ with open (tmp , 'rb' ) as f :
79+ contents = f .read ()
6180
6281 self .assertTrue (1000 < size < 2000 )
6382
6483 # width == 137
6584 self .assertIn (b'\x00 \x00 \x00 \x89 ' , contents )
66- self .assertIn (b'Copyright Voormedia' , contents )
85+ self .assertIn (b'Copyright Voormedia' , contents )
0 commit comments