[Piglit] [PATCH 09/20] unittests: use utils.nose.chdir

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


This fixes a number of tests on windows, where being in a directory
prevents it from being deleted.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 unittests/core_tests.py           | 18 ++++++------------
 unittests/junit_backends_tests.py | 12 +++++-------
 unittests/utils/nose.py           |  7 +------
 3 files changed, 12 insertions(+), 25 deletions(-)

diff --git a/unittests/core_tests.py b/unittests/core_tests.py
index 341663d..9c5949f 100644
--- a/unittests/core_tests.py
+++ b/unittests/core_tests.py
@@ -158,12 +158,10 @@ def test_config_home_fallback():
 def test_local():
     """core.get_config() finds ./piglit.conf"""
     with utils.nose.tempdir() as tdir:
-        os.chdir(tdir)
-
-        with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
-            f.write(_CONF_FILE)
-
-        core.get_config()
+        with utils.nose.chdir(tdir):
+            with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
+                f.write(_CONF_FILE)
+            core.get_config()
 
     nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
            msg='./piglit.conf not found')
@@ -174,12 +172,8 @@ def test_piglit_root():
     """core.get_config() finds "piglit root"/piglit.conf"""
     with open('piglit.conf', 'w') as f:
         f.write(_CONF_FILE)
-    return_dir = getcwd()
-    try:
-        os.chdir('..')
-        core.get_config()
-    finally:
-        os.chdir(return_dir)
+        with utils.nose.chdir('..'):
+            core.get_config()
         os.unlink('piglit.conf')
 
     nt.ok_(core.PIGLIT_CONFIG.has_section('nose-test'),
diff --git a/unittests/junit_backends_tests.py b/unittests/junit_backends_tests.py
index aaad5bf..d5f753a 100644
--- a/unittests/junit_backends_tests.py
+++ b/unittests/junit_backends_tests.py
@@ -323,12 +323,10 @@ def test_load_folder_name():
 def test_load_default_name():
     """backends.junit._load: uses 'junit result' for name as fallback"""
     with utils.nose.tempdir() as tdir:
-        os.chdir(tdir)
-
-        filename = 'results.xml'
-        with open(filename, 'w') as f:
-            f.write(_XML)
-
-        test = backends.junit.REGISTRY.load(filename, 'none')
+        with utils.nose.chdir(tdir):
+            filename = 'results.xml'
+            with open(filename, 'w') as f:
+                f.write(_XML)
+            test = backends.junit.REGISTRY.load(filename, 'none')
 
     nt.assert_equal(test.name, 'junit result')
diff --git a/unittests/utils/nose.py b/unittests/utils/nose.py
index 701b567..1d1ba93 100644
--- a/unittests/utils/nose.py
+++ b/unittests/utils/nose.py
@@ -339,16 +339,11 @@ def test_in_tempdir(func):
     returns to the original directory after the test completes.
 
     """
-    original_dir = getcwd()
-
     @functools.wraps(func)
     def wrapper(*args, **kwargs):
         with tempdir() as tdir:
-            try:
-                os.chdir(tdir)
+            with chdir(tdir):
                 return func(*args, **kwargs)
-            finally:
-                os.chdir(original_dir)
 
     return wrapper
 
-- 
2.8.3



More information about the Piglit mailing list