Skip to content

Commit 6d13946

Browse files
Copy the mocked Juju envvars into os.environ again.
1 parent 73419d5 commit 6d13946

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

testing/src/scenario/_runtime.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ def exec(
322322
juju_context = _JujuContext.from_dict(env)
323323
# We need to set JUJU_VERSION in os.environ, because charms may use
324324
# `JujuVersion.from_environ()` to get the (simulated) Juju version.
325-
# All of the other environment variables are exposed to the charm
326-
# through the framework machinery, so don't require this.
327-
previous_juju_version = os.environ.get("JUJU_VERSION")
328-
os.environ["JUJU_VERSION"] = env["JUJU_VERSION"]
325+
# For consistency, we put all the other ones there too, although we'd
326+
# like to change this in the future.
327+
previous_env = os.environ.copy()
328+
os.environ.update(env)
329329

330330
logger.info(" - entering ops.main (mocked)")
331331
from ._ops_main_mock import Ops # noqa: F811
@@ -352,10 +352,14 @@ def exec(
352352
) from e
353353

354354
finally:
355-
if previous_juju_version is None:
356-
del os.environ["JUJU_VERSION"]
357-
else:
358-
os.environ["JUJU_VERSION"] = previous_juju_version
355+
for key, value in os.environ.copy().items():
356+
if key in previous_env:
357+
os.environ[key] = value
358+
else:
359+
del os.environ[key]
360+
for key, value in previous_env.items():
361+
if key not in os.environ:
362+
os.environ[key] = value
359363
logger.info(" - exited ops.main")
360364

361365
context.emitted_events.extend(captured)

testing/tests/test_runtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_env_clean_on_charm_error():
110110
context=Context(my_charm_type, meta=meta),
111111
) as manager:
112112
assert manager._juju_context.remote_app_name == remote_name
113-
assert "JUJU_REMOTE_APP" not in os.environ
113+
assert "JUJU_REMOTE_APP" in os.environ
114114
_ = 1 / 0 # raise some error
115115
# Ensure that some other error didn't occur (like AssertionError!).
116116
assert "ZeroDivisionError" in str(exc.value)

0 commit comments

Comments
 (0)