Skip to content

Commit

Permalink
Split out updated_env function
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Dec 22, 2012
1 parent fa10658 commit f515c0f
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tests/fetcher_tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -236,16 +236,26 @@ def assert_raises_message(expected_error_type, expected_message, func):
def temporary_xdg_cache_dir(): def temporary_xdg_cache_dir():
key = "XDG_CACHE_HOME" key = "XDG_CACHE_HOME"
with temporary_directory() as cache_dir: with temporary_directory() as cache_dir:
original_value = os.environ.get(key) with updated_env({key: cache_dir}):
try:
os.environ[key] = cache_dir
yield 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: if original_value is None:
del os.environ[key] del os.environ[key]
else: else:
os.environ[key] = original_value os.environ[key] = original_value



@contextlib.contextmanager @contextlib.contextmanager
def temporary_empty_dir(): def temporary_empty_dir():
with temporary_directory() as directory: with temporary_directory() as directory:
Expand Down

0 comments on commit f515c0f

Please sign in to comment.