[Piglit] [PATCH 07/20] unittests: replace set_env decorator with mock
Dylan Baker
dylan at pnwbakers.com
Wed Jun 1 23:50:14 UTC 2016
---
unittests/compressed_backend_tests.py | 20 ++++++++++++--------
unittests/deqp_tests.py | 12 ++++++------
unittests/utils/nose.py | 35 -----------------------------------
3 files changed, 18 insertions(+), 49 deletions(-)
diff --git a/unittests/compressed_backend_tests.py b/unittests/compressed_backend_tests.py
index 18786c1..8ff2b0c 100644
--- a/unittests/compressed_backend_tests.py
+++ b/unittests/compressed_backend_tests.py
@@ -32,6 +32,10 @@ import os
import functools
import nose.tools as nt
+try:
+ import mock
+except ImportError:
+ from unittest import mock
from . import utils
from framework import results
@@ -146,21 +150,21 @@ def test_decompress_none():
@_add_compression('foobar')
- at utils.nose.set_env(PIGLIT_COMPRESSION='foobar')
+ at mock.patch.dict('os.environ', {'PIGLIT_COMPRESSION': 'foobar'})
def testget_mode_env():
"""framework.backends.compression.get_mode: uses PIGlIT_COMPRESSION environment variable"""
nt.eq_(compression.get_mode(), 'foobar')
@_add_compression('foobar')
- at utils.nose.set_env(PIGLIT_COMPRESSION=None)
+ at mock.patch.dict('os.environ', {}, True)
@utils.piglit.set_piglit_conf(('core', 'compression', 'foobar'))
def testget_mode_piglit_conf():
"""framework.backends.compression.get_mode: uses piglit.conf [core]:compression value if env is unset"""
nt.eq_(compression.get_mode(), 'foobar')
- at utils.nose.set_env(PIGLIT_COMPRESSION=None)
+ at mock.patch.dict('os.environ', {}, True)
@utils.piglit.set_piglit_conf(('core', 'compression', None))
def testget_mode_default():
"""framework.backends.compression.get_mode: uses DEFAULT if env and piglit.conf are unset"""
@@ -178,7 +182,7 @@ def test_decompress_gz():
_test_decompressor('gz')
- at utils.nose.set_env(PIGLIT_COMPRESSION='gz')
+ at mock.patch.dict('os.environ', {'PIGLIT_COMPRESSION': 'gz'})
def test_gz_output():
"""framework.backends: when using gz compression a gz file is created"""
nt.eq_(_test_extension(), '.gz')
@@ -195,7 +199,7 @@ def test_decompress_bz2():
_test_decompressor('bz2')
- at utils.nose.set_env(PIGLIT_COMPRESSION='bz2')
+ at mock.patch.dict('os.environ', {'PIGLIT_COMPRESSION': 'bz2'})
def test_bz2_output():
"""framework.backends: when using bz2 compression a bz2 file is created"""
nt.eq_(_test_extension(), '.bz2')
@@ -212,14 +216,14 @@ def test_decompress_xz():
_test_decompressor('xz')
- at utils.nose.set_env(PIGLIT_COMPRESSION='xz')
+ at mock.patch.dict('os.environ', {'PIGLIT_COMPRESSION': 'xz'})
def test_xz_output():
"""framework.backends: when using xz compression a xz file is created"""
nt.eq_(_test_extension(), '.xz')
@_add_compression('foobar')
- at utils.nose.set_env(PIGLIT_COMPRESSION=None)
+ at mock.patch.dict('os.environ', {}, True)
@utils.piglit.set_piglit_conf(('core', 'compression', 'foobar'))
def test_update_piglit_conf():
"""framework.backends.compression: The compression mode honors updates to piglit.conf.
@@ -233,7 +237,7 @@ def test_update_piglit_conf():
@utils.nose.Skip.py3
@utils.nose.Skip.module('backports.lzma', available=False)
- at utils.nose.set_env(PIGLIT_COMPRESSION='xz')
+ at mock.patch.dict('os.environ', {'PIGLIT_COMPRESSION': 'xz'})
@utils.nose.test_in_tempdir
def test_xz_shell_override():
"""framework.backends.compression: the xz shell utility path can overwrite"""
diff --git a/unittests/deqp_tests.py b/unittests/deqp_tests.py
index b0e314f..c237b81 100644
--- a/unittests/deqp_tests.py
+++ b/unittests/deqp_tests.py
@@ -53,7 +53,7 @@ class _DEQPTestTest(deqp.DEQPBaseTest):
@utils.piglit.set_piglit_conf(('deqp_test', 'test_env', 'from conf'))
- at utils.nose.set_env(_PIGLIT_TEST_ENV='from env')
+ at mock.patch.dict('os.environ', {'_PIGLIT_TEST_ENV': 'from env'})
def test_get_option_env():
"""deqp.get_option: if env is set it overrides piglit.conf"""
nt.eq_(deqp.get_option('_PIGLIT_TEST_ENV', ('deqp_test', 'test_env')),
@@ -61,14 +61,14 @@ def test_get_option_env():
@utils.piglit.set_piglit_conf(('deqp_test', 'test_env', 'from conf'))
- at utils.nose.set_env(_PIGLIT_TEST_ENV=None)
+ at mock.patch.dict('os.environ', {}, True)
def test_get_option_conf():
"""deqp.get_option: if env is not set a value is taken from piglit.conf"""
nt.eq_(deqp.get_option('_PIGLIT_TEST_ENV', ('deqp_test', 'test_env')),
'from conf')
- at utils.nose.set_env(_PIGLIT_TEST_ENV=None)
+ at mock.patch.dict('os.environ', {}, True)
def test_get_option_default():
"""deqp.get_option: default value is returned when env and conf are unset
"""
@@ -77,7 +77,7 @@ def test_get_option_default():
'foobar')
- at utils.nose.set_env(_PIGLIT_TEST_ENV=None)
+ at mock.patch.dict('os.environ', {}, True)
def test_get_option_conf_no_section():
"""deqp.get_option: if a no_section error is raised and env is unset None is return
"""
@@ -86,8 +86,8 @@ def test_get_option_conf_no_section():
# The first argument ensures the sectio exists
@utils.piglit.set_piglit_conf(('deqp_test', 'test_env', 'from conf'),
- ('deqp_test', 'not_exists', None))
- at utils.nose.set_env(_PIGLIT_TEST_ENV=None)
+ ('deqp_test', 'not_exists', None))
+ at mock.patch.dict('os.environ', {}, True)
def test_get_option_conf_no_option():
"""deqp.get_option: if a no_option error is raised and env is unset None is return
"""
diff --git a/unittests/utils/nose.py b/unittests/utils/nose.py
index 739089c..4298296 100644
--- a/unittests/utils/nose.py
+++ b/unittests/utils/nose.py
@@ -404,38 +404,3 @@ def capture_stderr(func):
sys.stderr = restore
return _inner
-
-
-def set_env(**envargs):
- """Decorator that sets environment variables and then unsets them.
-
- If an value is set to None that key will be deleted from os.environ
-
- """
-
- def _decorator(func):
- """The actual decorator."""
-
- @functools.wraps(func)
- def _inner(*args, **kwargs):
- """The returned function."""
- backup = {}
- for key, value in six.iteritems(envargs):
- backup[key] = os.environ.get(key, "__DONOTRESTORE__")
- if value is not None:
- os.environ[key] = value
- elif key in os.environ:
- del os.environ[key]
-
- try:
- func(*args, **kwargs)
- finally:
- for key, value in six.iteritems(backup):
- if value == "__DONOTRESTORE__" and key in os.environ:
- del os.environ[key]
- elif value != '__DONOTRESTORE__':
- os.environ[key] = value
-
- return _inner
-
- return _decorator
--
2.8.3
More information about the Piglit
mailing list