[Piglit] [PATCH 15/20] unittests: reimplement utility function to get a unique file to be windows-safe

Dylan Baker dylan at pnwbakers.com
Wed Jun 1 23:50:22 UTC 2016


tempfile.NamedTemproraryFile doesn't work in all of the cases that it
needs to for piglit's unittests when running on windows (though it does
on POSIX systems), so this reimplements it to use a unique temporary
directory with a known filename. This serves the same purpose, but
doesn't break on windows.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 unittests/utils/nose.py | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/unittests/utils/nose.py b/unittests/utils/nose.py
index 1d1ba93..5852718 100644
--- a/unittests/utils/nose.py
+++ b/unittests/utils/nose.py
@@ -206,15 +206,15 @@ def tempfile(contents):
                 written directly into the file.
 
     """
-    # Do not delete the tempfile as soon as it is closed
-    temp = tempfile_.NamedTemporaryFile(mode=_WRITE_MODE, delete=False)
-    temp.write(contents)
-    temp.close()
-
-    try:
-        yield temp.name
-    finally:
-        os.remove(temp.name)
+    # It is tempting to use NamedTemporaryFile here (the original
+    # implementation did, in fact), but this won't work on windows beacuse of
+    # implementation details. Since the goal isn't security anyway, just a
+    # unique filename this is implemented in terms of tempdir.
+    with tempdir() as t:
+        name = os.path.join(t, 'tempfile')
+        with open(name, mode=_WRITE_MODE) as f:
+            f.write(contents)
+        yield name
 
 
 @contextmanager
-- 
2.8.3



More information about the Piglit mailing list