[Piglit] [PATCH 08/20] unittests: add utility context manager for changing directories

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


Using this context manager ensures that you change back to the original
directory when leaving the context manager

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

diff --git a/unittests/utils/nose.py b/unittests/utils/nose.py
index 4298296..701b567 100644
--- a/unittests/utils/nose.py
+++ b/unittests/utils/nose.py
@@ -404,3 +404,19 @@ def capture_stderr(func):
             sys.stderr = restore
 
     return _inner
+
+
+ at contextmanager
+def chdir(goto):
+    """chdir to a directory and back, guaranteed.
+
+    This contextmanager ensures that after changing directory you cahgne back,
+    using a try/finally block.
+
+    """
+    returnto = getcwd()
+    os.chdir(goto)
+    try:
+        yield
+    finally:
+        os.chdir(returnto)
-- 
2.8.3



More information about the Piglit mailing list