Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(set_env): add set_env contextmanager utility #87

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions modflow_devtools/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ def set_dir(path: PathLike):
print(f"Returned to previous directory: {origin}")


@contextmanager
def set_env(*remove, **update):
"""
Temporarily updates the ``os.environ`` dictionary in-place.

Referenced from https://stackoverflow.com/a/34333710/6514033.

The ``os.environ`` dictionary is updated in-place so that the modification
is sure to work in all situations.

:param remove: Environment variables to remove.
:param update: Dictionary of environment variables and values to add/update.
"""
env = environ
update = update or {}
remove = remove or []

# List of environment variables being updated or removed.
stomped = (set(update.keys()) | set(remove)) & set(env.keys())
# Environment variables and values to restore on exit.
update_after = {k: env[k] for k in stomped}
# Environment variables and values to remove on exit.
remove_after = frozenset(k for k in update if k not in env)

try:
env.update(update)
[env.pop(k, None) for k in remove]
yield
finally:
env.update(update_after)
[env.pop(k) for k in remove_after]


class add_sys_path:
"""
Context manager for temporarily editing the system path
Expand Down
17 changes: 17 additions & 0 deletions modflow_devtools/test/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get_packages,
has_package,
set_dir,
set_env,
)


Expand All @@ -21,6 +22,22 @@ def test_set_dir(tmp_path):
assert Path(os.getcwd()) != tmp_path


def test_set_env(tmp_path):
# test adding a variable
key = "TEST_ENV"
val = "test"
assert environ.get(key) is None
with set_env(**{key: val}):
assert environ.get(key) == val
with set_env(TEST_ENV=val):
assert environ.get(key) == val

# test removing a variable
with set_env(**{key: val}):
with set_env(key):
assert environ.get(key) is None


_repos_path = environ.get("REPOS_PATH")
if _repos_path is None:
_repos_path = Path(__file__).parent.parent.parent.parent
Expand Down