Skip to content

Commit 4899c3f

Browse files
committed
Fix integration tests on windows
1 parent 7742309 commit 4899c3f

4 files changed

Lines changed: 52 additions & 21 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ pip install -r requirements.txt -r test-requirements.txt
3232
nosetests
3333
```
3434

35+
To test more runtimes, tox can be used
36+
37+
```
38+
tox
39+
```
40+
41+
42+
3543
### Integration tests
3644

3745
```

test/helper.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, division, print_function, unicode_literals
3-
3+
from contextlib import contextmanager
4+
from tempfile import NamedTemporaryFile
45
import json
56
import sys
67
import os
@@ -55,3 +56,18 @@ def assertJsonEqual(self, expected, actual):
5556
@property
5657
def request(self):
5758
return httpretty.last_request()
59+
60+
61+
62+
@contextmanager
63+
def create_named_tmpfile():
64+
# Due to NamedTemporaryFile requiring to be closed when used on Windows
65+
# we create our own NamedTemporaryFile contextmanager
66+
# See note: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
67+
68+
tmp = NamedTemporaryFile(delete=False)
69+
try:
70+
tmp.close()
71+
yield tmp.name
72+
finally:
73+
os.unlink(tmp.name)

test/integration.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys, os
2+
from test.helper import create_named_tmpfile
23

34
if 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)

test/tinify_source_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def test_to_file_with_path_should_store_image_data(self):
147147
self.assertEqual(b'compressed file', tmp.read())
148148

149149
def test_to_file_with_file_object_should_store_image_data(self):
150-
with tempfile.NamedTemporaryFile() as tmp:
151-
Source.from_buffer('png file').to_file(tmp.name)
152-
self.assertEqual(b'compressed file', tmp.read())
150+
with create_named_tmpfile() as name:
151+
Source.from_buffer('png file').to_file(name)
152+
with open(name, 'rb') as f:
153+
self.assertEqual(b'compressed file', f.read())

0 commit comments

Comments
 (0)