Refactor a test to not use the "working_env" fixture (#41308)

Co-authored-by: Harmen Stoppels <me@harmenstoppels.nl>
This commit is contained in:
Massimiliano Culpo 2023-11-29 22:14:57 +01:00 committed by Harmen Stoppels
parent f57ac8d2da
commit 9f2b8eef7a
3 changed files with 20 additions and 20 deletions

View file

@ -713,9 +713,6 @@ def configuration_dir(tmpdir_factory, linux_os):
t.write(content)
yield tmpdir
# Once done, cleanup the directory
shutil.rmtree(str(tmpdir))
def _create_mock_configuration_scopes(configuration_dir):
"""Create the configuration scopes used in `config` and `mutable_config`."""

View file

@ -27,16 +27,13 @@
]
def test_module_function_change_env(tmpdir, working_env):
src_file = str(tmpdir.join("src_me"))
with open(src_file, "w") as f:
f.write("export TEST_MODULE_ENV_VAR=TEST_SUCCESS\n")
os.environ["NOT_AFFECTED"] = "NOT_AFFECTED"
module("load", src_file, module_template=". {0} 2>&1".format(src_file))
assert os.environ["TEST_MODULE_ENV_VAR"] == "TEST_SUCCESS"
assert os.environ["NOT_AFFECTED"] == "NOT_AFFECTED"
def test_module_function_change_env(tmp_path):
environb = {b"TEST_MODULE_ENV_VAR": b"TEST_FAIL", b"NOT_AFFECTED": b"NOT_AFFECTED"}
src_file = tmp_path / "src_me"
src_file.write_text("export TEST_MODULE_ENV_VAR=TEST_SUCCESS\n")
module("load", str(src_file), module_template=f". {src_file} 2>&1", environb=environb)
assert environb[b"TEST_MODULE_ENV_VAR"] == b"TEST_SUCCESS"
assert environb[b"NOT_AFFECTED"] == b"NOT_AFFECTED"
def test_module_function_no_change(tmpdir):

View file

@ -10,6 +10,7 @@
import os
import re
import subprocess
from typing import MutableMapping, Optional
import llnl.util.tty as tty
@ -21,8 +22,13 @@
awk_cmd = r"""awk 'BEGIN{for(name in ENVIRON)""" r"""printf("%s=%s%c", name, ENVIRON[name], 0)}'"""
def module(*args, **kwargs):
module_cmd = kwargs.get("module_template", "module " + " ".join(args))
def module(
*args,
module_template: Optional[str] = None,
environb: Optional[MutableMapping[bytes, bytes]] = None,
):
module_cmd = module_template or ("module " + " ".join(args))
environb = environb or os.environb
if args[0] in module_change_commands:
# Suppress module output
@ -33,10 +39,10 @@ def module(*args, **kwargs):
stderr=subprocess.STDOUT,
shell=True,
executable="/bin/bash",
env=environb,
)
# In Python 3, keys and values of `environ` are byte strings.
environ = {}
new_environb = {}
output = module_p.communicate()[0]
# Loop over each environment variable key=value byte string
@ -45,11 +51,11 @@ def module(*args, **kwargs):
parts = entry.split(b"=", 1)
if len(parts) != 2:
continue
environ[parts[0]] = parts[1]
new_environb[parts[0]] = parts[1]
# Update os.environ with new dict
os.environ.clear()
os.environb.update(environ) # novermin
environb.clear()
environb.update(new_environb) # novermin
else:
# Simply execute commands that don't change state and return output