Permalink
Browse files

Split out updated_env function

  • Loading branch information...
1 parent fa10658 commit f515c0fbb746383ade6037f5c512535580c7facc @mwilliamson committed Dec 22, 2012
Showing with 14 additions and 4 deletions.
  1. +14 −4 tests/fetcher_tests.py
@@ -236,16 +236,26 @@ def assert_raises_message(expected_error_type, expected_message, func):
def temporary_xdg_cache_dir():
key = "XDG_CACHE_HOME"
with temporary_directory() as cache_dir:
- original_value = os.environ.get(key)
- try:
- os.environ[key] = cache_dir
+ with updated_env({key: cache_dir}):
yield
- finally:
+
+@contextlib.contextmanager
+def updated_env(env_updates):
+ original_env = {}
+ for key, updated_value in env_updates.iteritems():
+ original_env[key] = os.environ.get(key)
+ os.environ[key] = updated_value
+
+ try:
+ yield
+ finally:
+ for key, original_value in original_env.iteritems():
if original_value is None:
del os.environ[key]
else:
os.environ[key] = original_value
+
@contextlib.contextmanager
def temporary_empty_dir():
with temporary_directory() as directory:

0 comments on commit f515c0f

Please sign in to comment.