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) t.write(content)
yield tmpdir yield tmpdir
# Once done, cleanup the directory
shutil.rmtree(str(tmpdir))
def _create_mock_configuration_scopes(configuration_dir): def _create_mock_configuration_scopes(configuration_dir):
"""Create the configuration scopes used in `config` and `mutable_config`.""" """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): def test_module_function_change_env(tmp_path):
src_file = str(tmpdir.join("src_me")) environb = {b"TEST_MODULE_ENV_VAR": b"TEST_FAIL", b"NOT_AFFECTED": b"NOT_AFFECTED"}
with open(src_file, "w") as f: src_file = tmp_path / "src_me"
f.write("export TEST_MODULE_ENV_VAR=TEST_SUCCESS\n") 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)
os.environ["NOT_AFFECTED"] = "NOT_AFFECTED" assert environb[b"TEST_MODULE_ENV_VAR"] == b"TEST_SUCCESS"
module("load", src_file, module_template=". {0} 2>&1".format(src_file)) assert environb[b"NOT_AFFECTED"] == b"NOT_AFFECTED"
assert os.environ["TEST_MODULE_ENV_VAR"] == "TEST_SUCCESS"
assert os.environ["NOT_AFFECTED"] == "NOT_AFFECTED"
def test_module_function_no_change(tmpdir): def test_module_function_no_change(tmpdir):

View file

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