From 66bb19084ce7d2161354dcc31b08f61da955b19d Mon Sep 17 00:00:00 2001 From: Gregory Becker Date: Sun, 18 May 2025 11:12:09 -0700 Subject: [PATCH] add test file for conditional dependencies Signed-off-by: Gregory Becker --- .../conditional_dependencies.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lib/spack/spack/test/concretization/conditional_dependencies.py diff --git a/lib/spack/spack/test/concretization/conditional_dependencies.py b/lib/spack/spack/test/concretization/conditional_dependencies.py new file mode 100644 index 00000000000..c10745fec12 --- /dev/null +++ b/lib/spack/spack/test/concretization/conditional_dependencies.py @@ -0,0 +1,67 @@ +# Copyright Spack Project Developers. See COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) +import pytest + +import spack.concretize +import spack.config +import spack.spec + + +@pytest.mark.parametrize("holds,mpi", [(True, "zmpi"), (True, "mpich"), (False, "mpich")]) +def test_conditional_deps(holds, mpi, config, mock_packages): + sigil = "+" if holds else "~" + request = f"hdf5{sigil}mpi ^[when='^mpi' virtuals=mpi]{mpi}" + concrete = spack.concretize.concretize_one(request) + + assert (mpi in concrete) == holds + assert ("mpi" in concrete) == holds + + +@pytest.mark.parametrize("c", [True, False]) +@pytest.mark.parametrize("cxx", [True, False]) +@pytest.mark.parametrize("fortran", [True, False]) +def test_conditional_compilers(c, cxx, fortran, mutable_config, mock_packages): + # Configure two gcc compilers that could be concretized to + # We will confirm concretization matches the less preferred one + extra_attributes_block = { + "compilers": {"c": "/path/to/gcc", "cxx": "/path/to/g++", "fortran": "/path/to/fortran"} + } + spack.config.CONFIG.set( + "packages:gcc:externals::", + [ + { + "spec": "gcc@12.3.1 languages=c,c++,fortran", + "prefix": "/path", + "extra_attributes": extra_attributes_block, + }, + { + "spec": "gcc@10.3.1 languages=c,c++,fortran", + "prefix": "/path", + "extra_attributes": extra_attributes_block, + }, + ], + ) + + # Abstract spec parametrized to depend/not on c/cxx/fortran + # and with conditional dependencies for each on the less preferred gcc + abstract = spack.spec.Spec("conditional-languages") + abstract.constrain(f"c={c}") + abstract.constrain(f"cxx={cxx}") + abstract.constrain(f"fortran={fortran}") + + preferred_gcc = spack.concretize.concretize_one(abstract) + abstract.constrain( + "^[when='%c' virtuals=c]gcc@10.3.1 " + "^[when='%cxx' virtuals=cxx]gcc@10.3.1 " + "^[when='%fortran' virtuals=fortran]gcc@10.3.1" + ) + concrete = spack.concretize.concretize_one(abstract) + + # We should get the dependency we specified for each language we enabled + assert concrete.satisfies("%[virtuals=c]gcc@10.3.1") == c + assert concrete.satisfies("%[virtuals=cxx]gcc@10.3.1") == cxx + assert concrete.satisfies("%[virtuals=fortran]gcc@10.3.1") == fortran + + # The only time the two concrete specs are the same is if we don't use gcc at all + assert (concrete == preferred_gcc) == (not any((c, cxx, fortran)))