diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2016-06-01 09:20:14 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2016-06-02 11:12:28 -0700 |
commit | 84e4d93215ef4ca3c069b6208ec82c09fda93d0c (patch) | |
tree | db5502c8acbed45e25d3907a559aa6c1213ceddf | |
parent | e2bc6e2f63125542879172d36f92e7daba991360 (diff) |
unittests: reimplement utility function to get a unique file to be windows-safe
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@intel.com>
-rw-r--r-- | unittests/utils/nose.py | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/unittests/utils/nose.py b/unittests/utils/nose.py index 1d1ba93df..585271802 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 |