modules tcl: simplify env modification block in template (#36334)

Simplify environment modification block in modulefile Tcl template by
always setting a path delimiter to the prepend-path, append-path and
remove-path commands.

Remove --delim option to the setenv command as this command does not
allow such option.

Update test_prepend_path_separator test to explicitly check the 6
path-like commands that should be present in generated modulefile.
This commit is contained in:
Xavier Delaruelle 2023-03-24 10:28:10 +01:00 committed by GitHub
parent d0d5526110
commit 906151075d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 24 deletions

View File

@ -0,0 +1,5 @@
enable:
- tcl
tcl:
all:
autoload: none

View File

@ -2,7 +2,7 @@
# Spack Project Developers. See the top-level COPYRIGHT file for details. # Spack Project Developers. See the top-level COPYRIGHT file for details.
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import re
import sys import sys
import pytest import pytest
@ -128,21 +128,22 @@ def test_alter_environment(self, modulefile_content, module_configuration, confi
content = modulefile_content("libdwarf platform=test target=core2") content = modulefile_content("libdwarf platform=test target=core2")
assert len([x for x in content if x.startswith('prepend-path("CMAKE_PREFIX_PATH"')]) == 0 assert len([x for x in content if x.startswith('prepend_path("CMAKE_PREFIX_PATH"')]) == 0
assert len([x for x in content if 'setenv("FOO", "foo")' in x]) == 0 assert len([x for x in content if 'setenv("FOO", "foo")' in x]) == 0
assert len([x for x in content if 'unsetenv("BAR")' in x]) == 0 assert len([x for x in content if 'unsetenv("BAR")' in x]) == 0
def test_prepend_path_separator(self, modulefile_content, module_configuration): def test_prepend_path_separator(self, modulefile_content, module_configuration):
"""Tests modifications to run-time environment.""" """Tests that we can use custom delimiters to manipulate path lists."""
module_configuration("module_path_separator") module_configuration("module_path_separator")
content = modulefile_content("module-path-separator") content = modulefile_content("module-path-separator")
for line in content: assert len([x for x in content if 'append_path("COLON", "foo", ":")' in x]) == 1
if re.match(r'[a-z]+_path\("COLON"', line): assert len([x for x in content if 'prepend_path("COLON", "foo", ":")' in x]) == 1
assert line.endswith('"foo", ":")') assert len([x for x in content if 'remove_path("COLON", "foo", ":")' in x]) == 1
elif re.match(r'[a-z]+_path\("SEMICOLON"', line): assert len([x for x in content if 'append_path("SEMICOLON", "bar", ";")' in x]) == 1
assert line.endswith('"bar", ";")') assert len([x for x in content if 'prepend_path("SEMICOLON", "bar", ";")' in x]) == 1
assert len([x for x in content if 'remove_path("SEMICOLON", "bar", ";")' in x]) == 1
@pytest.mark.parametrize("config_name", ["exclude", "blacklist"]) @pytest.mark.parametrize("config_name", ["exclude", "blacklist"])
def test_exclude(self, modulefile_content, module_configuration, config_name): def test_exclude(self, modulefile_content, module_configuration, config_name):

View File

@ -108,6 +108,19 @@ def test_alter_environment(self, modulefile_content, module_configuration, confi
assert len([x for x in content if "module load foo/bar" in x]) == 1 assert len([x for x in content if "module load foo/bar" in x]) == 1
assert len([x for x in content if "setenv LIBDWARF_ROOT" in x]) == 1 assert len([x for x in content if "setenv LIBDWARF_ROOT" in x]) == 1
def test_prepend_path_separator(self, modulefile_content, module_configuration):
"""Tests that we can use custom delimiters to manipulate path lists."""
module_configuration("module_path_separator")
content = modulefile_content("module-path-separator")
assert len([x for x in content if 'append-path --delim ":" COLON "foo"' in x]) == 1
assert len([x for x in content if 'prepend-path --delim ":" COLON "foo"' in x]) == 1
assert len([x for x in content if 'remove-path --delim ":" COLON "foo"' in x]) == 1
assert len([x for x in content if 'append-path --delim ";" SEMICOLON "bar"' in x]) == 1
assert len([x for x in content if 'prepend-path --delim ";" SEMICOLON "bar"' in x]) == 1
assert len([x for x in content if 'remove-path --delim ";" SEMICOLON "bar"' in x]) == 1
@pytest.mark.parametrize("config_name", ["exclude", "blacklist"]) @pytest.mark.parametrize("config_name", ["exclude", "blacklist"])
def test_exclude(self, modulefile_content, module_configuration, config_name): def test_exclude(self, modulefile_content, module_configuration, config_name):
"""Tests excluding the generation of selected modules.""" """Tests excluding the generation of selected modules."""

View File

@ -41,8 +41,6 @@ conflict {{ name }}
{% block environment %} {% block environment %}
{% for command_name, cmd in environment_modifications %} {% for command_name, cmd in environment_modifications %}
{% if cmd.separator != ':' %}
{# A non-standard separator is required #}
{% if command_name == 'PrependPath' %} {% if command_name == 'PrependPath' %}
prepend-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}" prepend-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'AppendPath' %} {% elif command_name == 'AppendPath' %}
@ -50,25 +48,11 @@ append-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'RemovePath' %} {% elif command_name == 'RemovePath' %}
remove-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}" remove-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'SetEnv' %} {% elif command_name == 'SetEnv' %}
setenv --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'UnsetEnv' %}
unsetenv {{ cmd.name }}
{% endif %}
{% else %}
{# We are using the usual separator #}
{% if command_name == 'PrependPath' %}
prepend-path {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'AppendPath' %}
append-path {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'RemovePath' %}
remove-path {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'SetEnv' %}
setenv {{ cmd.name }} "{{ cmd.value }}" setenv {{ cmd.name }} "{{ cmd.value }}"
{% elif command_name == 'UnsetEnv' %} {% elif command_name == 'UnsetEnv' %}
unsetenv {{ cmd.name }} unsetenv {{ cmd.name }}
{% endif %} {% endif %}
{# #} {# #}
{% endif %}
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}