diff options
Diffstat (limited to 'lib/spack/spack/test/build_environment.py')
-rw-r--r-- | lib/spack/spack/test/build_environment.py | 130 |
1 files changed, 60 insertions, 70 deletions
diff --git a/lib/spack/spack/test/build_environment.py b/lib/spack/spack/test/build_environment.py index 4d0f09c50f..a7a55af0ca 100644 --- a/lib/spack/spack/test/build_environment.py +++ b/lib/spack/spack/test/build_environment.py @@ -14,7 +14,6 @@ import spack.spec from spack.paths import build_env_path from spack.build_environment import dso_suffix, _static_to_shared_library from spack.util.executable import Executable -from spack.util.spack_yaml import syaml_dict, syaml_str from spack.util.environment import EnvironmentModifications from llnl.util.filesystem import LibraryList, HeaderList @@ -65,6 +64,18 @@ def build_environment(working_env): del os.environ[name] +@pytest.fixture +def ensure_env_variables(config, mock_packages, monkeypatch, working_env): + """Returns a function that takes a dictionary and updates os.environ + for the test lifetime accordingly. Plugs-in mock config and repo. + """ + def _ensure(env_mods): + for name, value in env_mods.items(): + monkeypatch.setenv(name, value) + + return _ensure + + def test_static_to_shared_library(build_environment): os.environ['SPACK_TEST_COMMAND'] = 'dump-args' @@ -119,79 +130,58 @@ def test_cc_not_changed_by_modules(monkeypatch, working_env): assert os.environ['ANOTHER_VAR'] == 'THIS_IS_SET' -@pytest.mark.usefixtures('config', 'mock_packages') -def test_compiler_config_modifications(monkeypatch, working_env): - s = spack.spec.Spec('cmake') - s.concretize() - pkg = s.package - - os.environ['SOME_VAR_STR'] = '' - os.environ['SOME_VAR_NUM'] = '0' - os.environ['PATH_LIST'] = '/path/third:/path/forth' - os.environ['EMPTY_PATH_LIST'] = '' - os.environ.pop('NEW_PATH_LIST', None) - - env_mod = syaml_dict() - set_cmd = syaml_dict() - env_mod[syaml_str('set')] = set_cmd - - set_cmd[syaml_str('SOME_VAR_STR')] = syaml_str('SOME_STR') - set_cmd[syaml_str('SOME_VAR_NUM')] = 1 - - monkeypatch.setattr(pkg.compiler, 'environment', env_mod) - spack.build_environment.setup_package(pkg, False) - assert os.environ['SOME_VAR_STR'] == 'SOME_STR' - assert os.environ['SOME_VAR_NUM'] == str(1) - - env_mod = syaml_dict() - unset_cmd = syaml_dict() - env_mod[syaml_str('unset')] = unset_cmd - - unset_cmd[syaml_str('SOME_VAR_STR')] = None - - monkeypatch.setattr(pkg.compiler, 'environment', env_mod) - assert 'SOME_VAR_STR' in os.environ - spack.build_environment.setup_package(pkg, False) - assert 'SOME_VAR_STR' not in os.environ - - env_mod = syaml_dict() - set_cmd = syaml_dict() - env_mod[syaml_str('set')] = set_cmd - append_cmd = syaml_dict() - env_mod[syaml_str('append-path')] = append_cmd - unset_cmd = syaml_dict() - env_mod[syaml_str('unset')] = unset_cmd - prepend_cmd = syaml_dict() - env_mod[syaml_str('prepend-path')] = prepend_cmd - - set_cmd[syaml_str('EMPTY_PATH_LIST')] = syaml_str('/path/middle') - - append_cmd[syaml_str('PATH_LIST')] = syaml_str('/path/last') - append_cmd[syaml_str('EMPTY_PATH_LIST')] = syaml_str('/path/last') - append_cmd[syaml_str('NEW_PATH_LIST')] = syaml_str('/path/last') - - unset_cmd[syaml_str('SOME_VAR_NUM')] = None +@pytest.mark.parametrize('initial,modifications,expected', [ + # Set and unset variables + ({'SOME_VAR_STR': '', 'SOME_VAR_NUM': '0'}, + {'set': {'SOME_VAR_STR': 'SOME_STR', 'SOME_VAR_NUM': 1}}, + {'SOME_VAR_STR': 'SOME_STR', 'SOME_VAR_NUM': '1'}), + ({'SOME_VAR_STR': ''}, + {'unset': ['SOME_VAR_STR']}, + {'SOME_VAR_STR': None}), + ({}, # Set a variable that was not defined already + {'set': {'SOME_VAR_STR': 'SOME_STR'}}, + {'SOME_VAR_STR': 'SOME_STR'}), + # Append and prepend to the same variable + ({'EMPTY_PATH_LIST': '/path/middle'}, + {'prepend_path': {'EMPTY_PATH_LIST': '/path/first'}, + 'append_path': {'EMPTY_PATH_LIST': '/path/last'}}, + {'EMPTY_PATH_LIST': '/path/first:/path/middle:/path/last'}), + # Append and prepend from empty variables + ({'EMPTY_PATH_LIST': '', 'SOME_VAR_STR': ''}, + {'prepend_path': {'EMPTY_PATH_LIST': '/path/first'}, + 'append_path': {'SOME_VAR_STR': '/path/last'}}, + {'EMPTY_PATH_LIST': '/path/first', 'SOME_VAR_STR': '/path/last'}), + ({}, # Same as before but on variables that were not defined + {'prepend_path': {'EMPTY_PATH_LIST': '/path/first'}, + 'append_path': {'SOME_VAR_STR': '/path/last'}}, + {'EMPTY_PATH_LIST': '/path/first', 'SOME_VAR_STR': '/path/last'}), + # Remove a path from a list + ({'EMPTY_PATH_LIST': '/path/first:/path/middle:/path/last'}, + {'remove_path': {'EMPTY_PATH_LIST': '/path/middle'}}, + {'EMPTY_PATH_LIST': '/path/first:/path/last'}), + ({'EMPTY_PATH_LIST': '/only/path'}, + {'remove_path': {'EMPTY_PATH_LIST': '/only/path'}}, + {'EMPTY_PATH_LIST': ''}), +]) +def test_compiler_config_modifications( + initial, modifications, expected, ensure_env_variables, monkeypatch +): + # Set the environment as per prerequisites + ensure_env_variables(initial) - prepend_cmd[syaml_str('PATH_LIST')] = syaml_str('/path/first:/path/second') - prepend_cmd[syaml_str('EMPTY_PATH_LIST')] = syaml_str('/path/first') - prepend_cmd[syaml_str('NEW_PATH_LIST')] = syaml_str('/path/first') - prepend_cmd[syaml_str('SOME_VAR_NUM')] = syaml_str('/8') + # Monkeypatch a pkg.compiler.environment with the required modifications + pkg = spack.spec.Spec('cmake').concretized().package + monkeypatch.setattr(pkg.compiler, 'environment', modifications) - assert 'SOME_VAR_NUM' in os.environ - monkeypatch.setattr(pkg.compiler, 'environment', env_mod) + # Trigger the modifications spack.build_environment.setup_package(pkg, False) - # Check that the order of modifications is respected and the - # variable was unset before it was prepended. - assert os.environ['SOME_VAR_NUM'] == '/8' - - expected = '/path/first:/path/second:/path/third:/path/forth:/path/last' - assert os.environ['PATH_LIST'] == expected - - expected = '/path/first:/path/middle:/path/last' - assert os.environ['EMPTY_PATH_LIST'] == expected - expected = '/path/first:/path/last' - assert os.environ['NEW_PATH_LIST'] == expected + # Check they were applied + for name, value in expected.items(): + if value is not None: + assert os.environ[name] == value + continue + assert name not in os.environ @pytest.mark.regression('9107') |