Use tmp dir instead of keeping track of files
This allows to clean up behind ourselves more effectively and requires less
logic.
diff --git a/OpenSSL/test/util.py b/OpenSSL/test/util.py
index c26a57a..78b4a3f 100644
--- a/OpenSSL/test/util.py
+++ b/OpenSSL/test/util.py
@@ -7,18 +7,20 @@
U{Twisted<http://twistedmatrix.com/>}.
"""
+import os
import shutil
-import traceback
-import os, os.path
-from tempfile import mktemp
-from unittest import TestCase
import sys
+import traceback
+
+from tempfile import mktemp, mkdtemp
+from unittest import TestCase
from six import PY3
from OpenSSL._util import exception_from_error_queue
from OpenSSL.crypto import Error
+
try:
import memdbg
except Exception:
@@ -28,14 +30,16 @@
from OpenSSL._util import ffi, lib, byte_string as b
+
# This is the UTF-8 encoding of the SNOWMAN unicode code point.
NON_ASCII = b("\xe2\x98\x83").decode("utf-8")
+
class TestCase(TestCase):
"""
- :py:class:`TestCase` adds useful testing functionality beyond what is available
- from the standard library :py:class:`unittest.TestCase`.
+ :py:class:`TestCase` adds useful testing functionality beyond what is
+ available from the standard library :py:class:`unittest.TestCase`.
"""
def run(self, result):
run = super(TestCase, self).run
@@ -157,24 +161,38 @@
(None, Exception(stack % (allocs_report,)), None))
+ _tmpdir = None
+
+
+ @property
+ def tmpdir(self):
+ """
+ On demand create a temporary directory.
+ """
+ if self._tmpdir is not None:
+ return self._tmpdir
+
+ self._tmpdir = mkdtemp(dir=".")
+ return self._tmpdir
+
+
def tearDown(self):
"""
- Clean up any files or directories created using :py:meth:`TestCase.mktemp`.
- Subclasses must invoke this method if they override it or the
- cleanup will not occur.
+ Clean up any files or directories created using
+ :py:meth:`TestCase.mktemp`. Subclasses must invoke this method if they
+ override it or the cleanup will not occur.
"""
- if self._temporaryFiles is not None:
- for temp in self._temporaryFiles:
- if os.path.isdir(temp):
- shutil.rmtree(temp)
- elif os.path.exists(temp):
- os.unlink(temp)
+ if self._tmpdir is not None:
+ shutil.rmtree(self._tmpdir)
+
try:
exception_from_error_queue(Error)
except Error:
e = sys.exc_info()[1]
if e.args != ([],):
- self.fail("Left over errors in OpenSSL error queue: " + repr(e))
+ self.fail(
+ "Left over errors in OpenSSL error queue: " + repr(e)
+ )
def assertIsInstance(self, instance, classOrTuple, message=None):
@@ -295,16 +313,13 @@
assertRaises = failUnlessRaises
- _temporaryFiles = None
- def mktemp(self, suffix=""):
+ def mktemp(self):
"""
- Pathetic substitute for twisted.trial.unittest.TestCase.mktemp.
+ Return UTF-8-encoded bytes of a path to a tmp file.
+
+ The file will be cleaned up after the test run.
"""
- if self._temporaryFiles is None:
- self._temporaryFiles = []
- temp = mktemp(suffix=suffix, dir=".").encode("utf-8")
- self._temporaryFiles.append(temp)
- return temp
+ return mktemp(dir=self.tmpdir).encode("utf-8")
# Other stuff