summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/spec_list.py
blob: db31146df7cc9a68226814a4581875de3ec3fe12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import itertools

import pytest

from spack.spec import Spec
from spack.spec_list import SpecList


class TestSpecList:
    default_input = ["mpileaks", "$mpis", {"matrix": [["hypre"], ["$gccs", "$clangs"]]}, "libelf"]

    default_reference = {
        "gccs": SpecList("gccs", ["%gcc@4.5.0"]),
        "clangs": SpecList("clangs", ["%clang@3.3"]),
        "mpis": SpecList("mpis", ["zmpi@1.0", "mpich@3.0"]),
    }

    default_expansion = [
        "mpileaks",
        "zmpi@1.0",
        "mpich@3.0",
        {"matrix": [["hypre"], ["%gcc@4.5.0", "%clang@3.3"]]},
        "libelf",
    ]

    default_constraints = [
        [Spec("mpileaks")],
        [Spec("zmpi@1.0")],
        [Spec("mpich@3.0")],
        [Spec("hypre"), Spec("%gcc@4.5.0")],
        [Spec("hypre"), Spec("%clang@3.3")],
        [Spec("libelf")],
    ]

    default_specs = [
        Spec("mpileaks"),
        Spec("zmpi@1.0"),
        Spec("mpich@3.0"),
        Spec("hypre%gcc@4.5.0"),
        Spec("hypre%clang@3.3"),
        Spec("libelf"),
    ]

    def test_spec_list_expansions(self):
        speclist = SpecList("specs", self.default_input, self.default_reference)

        assert speclist.specs_as_yaml_list == self.default_expansion
        assert speclist.specs_as_constraints == self.default_constraints
        assert speclist.specs == self.default_specs

    @pytest.mark.regression("28749")
    @pytest.mark.parametrize(
        "specs,expected",
        [
            # Constraints are ordered randomly
            (
                [
                    {
                        "matrix": [
                            ["^zmpi"],
                            ["%gcc@4.5.0"],
                            ["hypre", "libelf"],
                            ["~shared"],
                            ["cflags=-O3", 'cflags="-g -O0"'],
                            ["^foo"],
                        ]
                    }
                ],
                [
                    "hypre cflags=-O3 ~shared %gcc@4.5.0 ^foo ^zmpi",
                    'hypre cflags="-g -O0" ~shared %gcc@4.5.0 ^foo ^zmpi',
                    "libelf cflags=-O3 ~shared %gcc@4.5.0 ^foo ^zmpi",
                    'libelf cflags="-g -O0" ~shared %gcc@4.5.0 ^foo ^zmpi',
                ],
            ),
            # A constraint affects both the root and a dependency
            (
                [{"matrix": [["gromacs"], ["%gcc"], ["+plumed ^plumed%gcc"]]}],
                ["gromacs+plumed%gcc ^plumed%gcc"],
            ),
        ],
    )
    def test_spec_list_constraint_ordering(self, specs, expected):
        speclist = SpecList("specs", specs)
        expected_specs = [Spec(x) for x in expected]
        assert speclist.specs == expected_specs

    def test_spec_list_add(self):
        speclist = SpecList("specs", self.default_input, self.default_reference)

        assert speclist.specs_as_yaml_list == self.default_expansion
        assert speclist.specs_as_constraints == self.default_constraints
        assert speclist.specs == self.default_specs

        speclist.add("libdwarf")

        assert speclist.specs_as_yaml_list == self.default_expansion + ["libdwarf"]
        assert speclist.specs_as_constraints == self.default_constraints + [[Spec("libdwarf")]]
        assert speclist.specs == self.default_specs + [Spec("libdwarf")]

    def test_spec_list_remove(self):
        speclist = SpecList("specs", self.default_input, self.default_reference)

        assert speclist.specs_as_yaml_list == self.default_expansion
        assert speclist.specs_as_constraints == self.default_constraints
        assert speclist.specs == self.default_specs

        speclist.remove("libelf")

        assert speclist.specs_as_yaml_list + ["libelf"] == self.default_expansion

        assert speclist.specs_as_constraints + [[Spec("libelf")]] == self.default_constraints

        assert speclist.specs + [Spec("libelf")] == self.default_specs

    def test_spec_list_update_reference(self):
        speclist = SpecList("specs", self.default_input, self.default_reference)

        assert speclist.specs_as_yaml_list == self.default_expansion
        assert speclist.specs_as_constraints == self.default_constraints
        assert speclist.specs == self.default_specs

        new_mpis = SpecList("mpis", self.default_reference["mpis"].yaml_list)
        new_mpis.add("mpich@3.3")
        new_reference = self.default_reference.copy()
        new_reference["mpis"] = new_mpis

        speclist.update_reference(new_reference)

        expansion = list(self.default_expansion)
        expansion.insert(3, "mpich@3.3")
        constraints = list(self.default_constraints)
        constraints.insert(3, [Spec("mpich@3.3")])
        specs = list(self.default_specs)
        specs.insert(3, Spec("mpich@3.3"))

        assert speclist.specs_as_yaml_list == expansion
        assert speclist.specs_as_constraints == constraints
        assert speclist.specs == specs

    def test_spec_list_extension(self):
        speclist = SpecList("specs", self.default_input, self.default_reference)

        assert speclist.specs_as_yaml_list == self.default_expansion
        assert speclist.specs_as_constraints == self.default_constraints
        assert speclist.specs == self.default_specs

        new_ref = self.default_reference.copy()
        otherlist = SpecList("specs", ["zlib", {"matrix": [["callpath"], ["%intel@18"]]}], new_ref)

        speclist.extend(otherlist)

        assert speclist.specs_as_yaml_list == (
            self.default_expansion + otherlist.specs_as_yaml_list
        )
        assert speclist.specs == self.default_specs + otherlist.specs
        assert speclist._reference is new_ref

    def test_spec_list_nested_matrices(self):
        inner_matrix = [{"matrix": [["zlib", "libelf"], ["%gcc", "%intel"]]}]
        outer_addition = ["+shared", "~shared"]
        outer_matrix = [{"matrix": [inner_matrix, outer_addition]}]
        speclist = SpecList("specs", outer_matrix)

        expected_components = itertools.product(
            ["zlib", "libelf"], ["%gcc", "%intel"], ["+shared", "~shared"]
        )
        expected = [Spec(" ".join(combo)) for combo in expected_components]
        assert set(speclist.specs) == set(expected)

    @pytest.mark.regression("16897")
    def test_spec_list_recursion_specs_as_constraints(self):
        input = ["mpileaks", "$mpis", {"matrix": [["hypre"], ["$%gccs", "$%clangs"]]}, "libelf"]

        reference = {
            "gccs": SpecList("gccs", ["gcc@4.5.0"]),
            "clangs": SpecList("clangs", ["clang@3.3"]),
            "mpis": SpecList("mpis", ["zmpi@1.0", "mpich@3.0"]),
        }

        speclist = SpecList("specs", input, reference)

        assert speclist.specs_as_yaml_list == self.default_expansion
        assert speclist.specs_as_constraints == self.default_constraints
        assert speclist.specs == self.default_specs

    def test_spec_list_matrix_exclude(self, mock_packages):
        # Test on non-boolean variants for regression for #16841
        matrix = [
            {"matrix": [["multivalue-variant"], ["foo=bar", "foo=baz"]], "exclude": ["foo=bar"]}
        ]
        speclist = SpecList("specs", matrix)
        assert len(speclist.specs) == 1

    @pytest.mark.regression("22991")
    def test_spec_list_constraints_with_structure(
        self, mock_packages, mock_fetch, install_mockery
    ):
        # Setup by getting hash and installing package with dep
        libdwarf_spec = Spec("libdwarf").concretized()
        libdwarf_spec.package.do_install()

        # Create matrix
        matrix = {
            "matrix": [["mpileaks"], ["^callpath"], ["^libdwarf/%s" % libdwarf_spec.dag_hash()]]
        }

        # ensure the concrete spec was retained in the matrix entry of which
        # it is a dependency
        speclist = SpecList("specs", [matrix])
        assert len(speclist.specs) == 1
        assert libdwarf_spec in speclist.specs[0]