From 889b729e524ce1a872c6789273afa7ed056cb6e9 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Wed, 29 Nov 2023 22:14:57 +0100 Subject: Refactor a test to not use the "working_env" fixture (#41308) Co-authored-by: Harmen Stoppels --- lib/spack/spack/test/conftest.py | 3 --- lib/spack/spack/test/module_parsing.py | 17 +++++++---------- lib/spack/spack/util/module_cmd.py | 20 +++++++++++++------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/spack/spack/test/conftest.py b/lib/spack/spack/test/conftest.py index 6cf4d7375a..326ee143e7 100644 --- a/lib/spack/spack/test/conftest.py +++ b/lib/spack/spack/test/conftest.py @@ -714,9 +714,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`.""" diff --git a/lib/spack/spack/test/module_parsing.py b/lib/spack/spack/test/module_parsing.py index 196c18fe2d..6ac4184721 100644 --- a/lib/spack/spack/test/module_parsing.py +++ b/lib/spack/spack/test/module_parsing.py @@ -27,16 +27,13 @@ test_module_lines = [ ] -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): diff --git a/lib/spack/spack/util/module_cmd.py b/lib/spack/spack/util/module_cmd.py index c05115768f..41a48f8191 100644 --- a/lib/spack/spack/util/module_cmd.py +++ b/lib/spack/spack/util/module_cmd.py @@ -10,6 +10,7 @@ parsing environment modules. import os import re import subprocess +from typing import MutableMapping, Optional import llnl.util.tty as tty @@ -21,8 +22,13 @@ module_change_commands = ["load", "swap", "unload", "purge", "use", "unuse"] 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 -- cgit v1.2.3-60-g2f50