mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-05 04:13:11 +03:00
Thread and race condition safe tempfiles for testing
This commit is contained in:
parent
24cd3d2cfa
commit
c824a15fe8
|
@ -3,39 +3,30 @@ Helper functions.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
|
||||||
if sys.version_info[:2] <= (2, 6):
|
if sys.version_info[:2] <= (2, 6):
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
else:
|
else:
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
# This should be imported into every test_XXX.py file to report
|
|
||||||
# any remaining temp files at the end of the run.
|
|
||||||
def tearDownModule():
|
def tearDownModule():
|
||||||
import glob
|
#remove me later
|
||||||
import os
|
pass
|
||||||
import tempfile
|
|
||||||
temp_root = os.path.join(tempfile.gettempdir(), 'pillow-tests')
|
|
||||||
tempfiles = glob.glob(os.path.join(temp_root, "temp_*"))
|
|
||||||
if tempfiles:
|
|
||||||
print("===", "remaining temporary files")
|
|
||||||
for file in tempfiles:
|
|
||||||
print(file)
|
|
||||||
print("-"*68)
|
|
||||||
|
|
||||||
|
|
||||||
class PillowTestCase(unittest.TestCase):
|
class PillowTestCase(unittest.TestCase):
|
||||||
|
|
||||||
currentResult = None # holds last result object passed to run method
|
def __init__(self, *args, **kwargs):
|
||||||
_tempfiles = []
|
unittest.TestCase.__init__(self, *args, **kwargs)
|
||||||
|
self.currentResult = None # holds last result object passed to run method
|
||||||
|
|
||||||
def run(self, result=None):
|
def run(self, result=None):
|
||||||
self.addCleanup(self.delete_tempfiles)
|
|
||||||
self.currentResult = result # remember result for use later
|
self.currentResult = result # remember result for use later
|
||||||
unittest.TestCase.run(self, result) # call superclass run method
|
unittest.TestCase.run(self, result) # call superclass run method
|
||||||
|
|
||||||
def delete_tempfiles(self):
|
def delete_tempfile(self, path):
|
||||||
try:
|
try:
|
||||||
ok = self.currentResult.wasSuccessful()
|
ok = self.currentResult.wasSuccessful()
|
||||||
except AttributeError: # for nosetests
|
except AttributeError: # for nosetests
|
||||||
|
@ -44,19 +35,14 @@ class PillowTestCase(unittest.TestCase):
|
||||||
|
|
||||||
if ok:
|
if ok:
|
||||||
# only clean out tempfiles if test passed
|
# only clean out tempfiles if test passed
|
||||||
import os
|
|
||||||
import os.path
|
|
||||||
import tempfile
|
|
||||||
for file in self._tempfiles:
|
|
||||||
try:
|
try:
|
||||||
os.remove(file)
|
print("Removing File: %s" % path)
|
||||||
|
os.remove(path)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass # report?
|
pass # report?
|
||||||
temp_root = os.path.join(tempfile.gettempdir(), 'pillow-tests')
|
else:
|
||||||
try:
|
print("=== orphaned temp file")
|
||||||
os.rmdir(temp_root)
|
print(path)
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def assert_almost_equal(self, a, b, msg=None, eps=1e-6):
|
def assert_almost_equal(self, a, b, msg=None, eps=1e-6):
|
||||||
self.assertLess(
|
self.assertLess(
|
||||||
|
@ -139,27 +125,13 @@ class PillowTestCase(unittest.TestCase):
|
||||||
self.assertTrue(found)
|
self.assertTrue(found)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def tempfile(self, template, *extra):
|
def tempfile(self, template):
|
||||||
import os
|
assert template[:5] in ("temp.", "temp_")
|
||||||
import os.path
|
(fd, path) = tempfile.mkstemp(template[4:], template[:4])
|
||||||
import sys
|
os.close(fd)
|
||||||
import tempfile
|
|
||||||
files = []
|
|
||||||
root = os.path.join(tempfile.gettempdir(), 'pillow-tests')
|
|
||||||
try:
|
|
||||||
os.mkdir(root)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
for temp in (template,) + extra:
|
|
||||||
assert temp[:5] in ("temp.", "temp_")
|
|
||||||
name = os.path.basename(sys.argv[0])
|
|
||||||
name = temp[:4] + os.path.splitext(name)[0][4:]
|
|
||||||
name = name + "_%d" % len(self._tempfiles) + temp[4:]
|
|
||||||
name = os.path.join(root, name)
|
|
||||||
files.append(name)
|
|
||||||
self._tempfiles.extend(files)
|
|
||||||
return files[0]
|
|
||||||
|
|
||||||
|
self.addCleanup(self.delete_tempfile, path)
|
||||||
|
return path
|
||||||
|
|
||||||
# helpers
|
# helpers
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ class TestFontPcf(PillowTestCase):
|
||||||
self.assertIsInstance(font, FontFile.FontFile)
|
self.assertIsInstance(font, FontFile.FontFile)
|
||||||
self.assertEqual(len([_f for _f in font.glyph if _f]), 192)
|
self.assertEqual(len([_f for _f in font.glyph if _f]), 192)
|
||||||
|
|
||||||
tempname = self.tempfile("temp.pil", "temp.pbm")
|
tempname = self.tempfile("temp.pil")
|
||||||
|
self.addCleanup(self.delete_tempfile, tempname[:-4]+'.pbm')
|
||||||
font.save(tempname)
|
font.save(tempname)
|
||||||
return tempname
|
return tempname
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user