[Piglit] [PATCH 08/13] framework/tests/utils.py: Add module for utility functions
Dylan Baker
baker.dylan.c at gmail.com
Thu Mar 6 14:47:18 PST 2014
As the framework tests grow the number of duplicated functions in each
module also grow. Adding a utilities module to store those shared
functions in makes a lot of sense.
Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
framework/tests/utils.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
create mode 100644 framework/tests/utils.py
diff --git a/framework/tests/utils.py b/framework/tests/utils.py
new file mode 100644
index 0000000..4b7746d
--- /dev/null
+++ b/framework/tests/utils.py
@@ -0,0 +1,61 @@
+# Copyright (c) 2014 Intel Coporation
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+""" Common helpers for framework tests
+
+This module collects common tools that are needed in more than one test module
+in a single place.
+
+"""
+
+import os
+import tempfile
+from contextlib import contextmanager
+
+
+__all__ = ['with_tempfile']
+
+
+class UtilsException(Exception):
+ """ An exception to be raised by utils """
+ pass
+
+
+ at contextmanager
+def with_tempfile(contents):
+ """ Provides a context manager for a named tempfile
+
+ This contextmanager creates a named tempfile, writes data into that
+ tempfile, then closes it and yields the filepath. After the context is
+ returned it closes and removes the tempfile.
+
+ Arguments:
+ contests -- This should be a string (unicode or str), in which case it is
+ written directly into the file.
+
+ """
+ # Do not delete the tempfile as soon as it is closed
+ temp = tempfile.NamedTemporaryFile(delete=False)
+ temp.write(contents)
+ temp.close()
+
+ yield temp.name
+
+ os.remove(temp.name)
--
1.9.0
More information about the Piglit
mailing list