summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/concretize_requirements.py
blob: e1f546f7e14263d0f0ab40a67024ed79cc566648 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
# 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 os
import pathlib

import pytest

import spack.build_systems.generic
import spack.config
import spack.error
import spack.package_base
import spack.repo
import spack.util.spack_yaml as syaml
import spack.version
from spack.solver.asp import InternalConcretizerError, UnsatisfiableSpecError
from spack.spec import Spec
from spack.test.conftest import create_test_repo
from spack.util.url import path_to_file_url

pytestmark = [
    pytest.mark.not_on_windows("Windows uses old concretizer"),
    pytest.mark.only_clingo("Original concretizer does not support configuration requirements"),
]


def update_packages_config(conf_str):
    conf = syaml.load_config(conf_str)
    spack.config.set("packages", conf["packages"], scope="concretize")


_pkgx = (
    "x",
    """\
class X(Package):
    version("1.1")
    version("1.0")
    version("0.9")

    variant("shared", default=True,
            description="Build shared libraries")

    depends_on("y")
""",
)


_pkgy = (
    "y",
    """\
class Y(Package):
    version("2.5")
    version("2.4")
    version("2.3", deprecated=True)

    variant("shared", default=True,
            description="Build shared libraries")
""",
)


_pkgv = (
    "v",
    """\
class V(Package):
    version("2.1")
    version("2.0")
""",
)


_pkgt = (
    "t",
    """\
class T(Package):
    version('2.1')
    version('2.0')

    depends_on('u', when='@2.1:')
""",
)


_pkgu = (
    "u",
    """\
class U(Package):
    version('1.1')
    version('1.0')
""",
)


@pytest.fixture
def _create_test_repo(tmpdir, mutable_config):
    yield create_test_repo(tmpdir, [_pkgx, _pkgy, _pkgv, _pkgt, _pkgu])


@pytest.fixture
def test_repo(_create_test_repo, monkeypatch, mock_stage):
    with spack.repo.use_repositories(_create_test_repo) as mock_repo_path:
        yield mock_repo_path


class MakeStage:
    def __init__(self, stage):
        self.stage = stage

    def __call__(self, *args, **kwargs):
        return self.stage


@pytest.fixture
def fake_installs(monkeypatch, tmpdir):
    stage_path = str(tmpdir.ensure("fake-stage", dir=True))
    universal_unused_stage = spack.stage.DIYStage(stage_path)
    monkeypatch.setattr(
        spack.build_systems.generic.Package, "_make_stage", MakeStage(universal_unused_stage)
    )


def test_one_package_multiple_reqs(concretize_scope, test_repo):
    conf_str = """\
packages:
  y:
    require:
    - "@2.4"
    - "~shared"
"""
    update_packages_config(conf_str)
    y_spec = Spec("y").concretized()
    assert y_spec.satisfies("@2.4~shared")


def test_requirement_isnt_optional(concretize_scope, test_repo):
    """If a user spec requests something that directly conflicts
    with a requirement, make sure we get an error.
    """
    conf_str = """\
packages:
  x:
    require: "@1.0"
"""
    update_packages_config(conf_str)
    with pytest.raises(UnsatisfiableSpecError):
        Spec("x@1.1").concretize()


def test_require_undefined_version(concretize_scope, test_repo):
    """If a requirement specifies a numbered version that isn't in
    the associated package.py and isn't part of a Git hash
    equivalence (hash=number), then Spack should raise an error
    (it is assumed this is a typo, and raising the error here
    avoids a likely error when Spack attempts to fetch the version).
    """
    conf_str = """\
packages:
  x:
    require: "@1.2"
"""
    update_packages_config(conf_str)
    with pytest.raises(spack.config.ConfigError):
        Spec("x").concretize()


def test_require_truncated(concretize_scope, test_repo):
    """A requirement specifies a version range, with satisfying
    versions defined in the package.py. Make sure we choose one
    of the defined versions (vs. allowing the requirement to
    define a new version).
    """
    conf_str = """\
packages:
  x:
    require: "@1"
"""
    update_packages_config(conf_str)
    xspec = Spec("x").concretized()
    assert xspec.satisfies("@1.1")


def test_git_user_supplied_reference_satisfaction(
    concretize_scope, test_repo, mock_git_version_info, monkeypatch
):
    repo_path, filename, commits = mock_git_version_info

    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", path_to_file_url(repo_path), raising=False
    )

    hash_eq_ver = Spec(f"v@{commits[0]}=2.2")
    hash_eq_ver_copy = Spec(f"v@{commits[0]}=2.2")
    just_hash = Spec(f"v@{commits[0]}")
    just_ver = Spec("v@=2.2")
    hash_eq_other_ver = Spec(f"v@{commits[0]}=2.3")

    assert not hash_eq_ver == just_hash
    assert not hash_eq_ver.satisfies(just_hash)
    assert not hash_eq_ver.intersects(just_hash)

    # Git versions and literal versions are distinct versions, like
    # pkg@10.1.0 and pkg@10.1.0-suffix are distinct versions.
    assert not hash_eq_ver.satisfies(just_ver)
    assert not just_ver.satisfies(hash_eq_ver)
    assert not hash_eq_ver.intersects(just_ver)
    assert hash_eq_ver != just_ver
    assert just_ver != hash_eq_ver
    assert not hash_eq_ver == just_ver
    assert not just_ver == hash_eq_ver

    # When a different version is associated, they're not equal
    assert not hash_eq_ver.satisfies(hash_eq_other_ver)
    assert not hash_eq_other_ver.satisfies(hash_eq_ver)
    assert not hash_eq_ver.intersects(hash_eq_other_ver)
    assert not hash_eq_other_ver.intersects(hash_eq_ver)
    assert hash_eq_ver != hash_eq_other_ver
    assert hash_eq_other_ver != hash_eq_ver
    assert not hash_eq_ver == hash_eq_other_ver
    assert not hash_eq_other_ver == hash_eq_ver

    # These should be equal
    assert hash_eq_ver == hash_eq_ver_copy
    assert not hash_eq_ver != hash_eq_ver_copy
    assert hash_eq_ver.satisfies(hash_eq_ver_copy)
    assert hash_eq_ver_copy.satisfies(hash_eq_ver)
    assert hash_eq_ver.intersects(hash_eq_ver_copy)
    assert hash_eq_ver_copy.intersects(hash_eq_ver)


def test_requirement_adds_new_version(
    concretize_scope, test_repo, mock_git_version_info, monkeypatch
):
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", path_to_file_url(repo_path), raising=False
    )

    a_commit_hash = commits[0]
    conf_str = """\
packages:
  v:
    require: "@{0}=2.2"
""".format(
        a_commit_hash
    )
    update_packages_config(conf_str)

    s1 = Spec("v").concretized()
    assert s1.satisfies("@2.2")
    # Make sure the git commit info is retained
    assert isinstance(s1.version, spack.version.GitVersion)
    assert s1.version.ref == a_commit_hash


def test_requirement_adds_version_satisfies(
    concretize_scope, test_repo, mock_git_version_info, monkeypatch
):
    """Make sure that new versions added by requirements are factored into
    conditions. In this case create a new version that satisfies a
    depends_on condition and make sure it is triggered (i.e. the
    dependency is added).
    """
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", path_to_file_url(repo_path), raising=False
    )

    # Sanity check: early version of T does not include U
    s0 = Spec("t@2.0").concretized()
    assert not ("u" in s0)

    conf_str = """\
packages:
  t:
    require: "@{0}=2.2"
""".format(
        commits[0]
    )
    update_packages_config(conf_str)

    s1 = Spec("t").concretized()
    assert "u" in s1
    assert s1.satisfies("@2.2")


@pytest.mark.parametrize("require_checksum", (True, False))
def test_requirement_adds_git_hash_version(
    require_checksum, concretize_scope, test_repo, mock_git_version_info, monkeypatch, working_env
):
    # A full commit sha is a checksummed version, so this test should pass in both cases
    if require_checksum:
        os.environ["SPACK_CONCRETIZER_REQUIRE_CHECKSUM"] = "yes"

    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", path_to_file_url(repo_path), raising=False
    )

    a_commit_hash = commits[0]
    conf_str = f"""\
packages:
  v:
    require: "@{a_commit_hash}"
"""
    update_packages_config(conf_str)

    s1 = Spec("v").concretized()
    assert isinstance(s1.version, spack.version.GitVersion)
    assert s1.satisfies(f"v@{a_commit_hash}")


def test_requirement_adds_multiple_new_versions(
    concretize_scope, test_repo, mock_git_version_info, monkeypatch
):
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", path_to_file_url(repo_path), raising=False
    )

    conf_str = f"""\
packages:
  v:
    require:
    - one_of: ["@{commits[0]}=2.2", "@{commits[1]}=2.3"]
"""
    update_packages_config(conf_str)

    assert Spec("v").concretized().satisfies(f"@{commits[0]}=2.2")
    assert Spec("v@2.3").concretized().satisfies(f"v@{commits[1]}=2.3")


# TODO: this belongs in the concretize_preferences test module but uses
# fixtures defined only here
def test_preference_adds_new_version(
    concretize_scope, test_repo, mock_git_version_info, monkeypatch
):
    """Normally a preference cannot define a new version, but that constraint
    is ignored if the version is a Git hash-based version.
    """
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", path_to_file_url(repo_path), raising=False
    )

    conf_str = f"""\
packages:
  v:
    version: ["{commits[0]}=2.2", "{commits[1]}=2.3"]
"""
    update_packages_config(conf_str)

    assert Spec("v").concretized().satisfies(f"@{commits[0]}=2.2")
    assert Spec("v@2.3").concretized().satisfies(f"@{commits[1]}=2.3")

    # When installing by hash, a lookup is triggered, so it's not mapped to =2.3.
    s3 = Spec(f"v@{commits[1]}").concretized()
    assert s3.satisfies(f"v@{commits[1]}")
    assert not s3.satisfies("@2.3")


def test_external_adds_new_version_that_is_preferred(concretize_scope, test_repo):
    """Test that we can use a version, not declared in package recipe, as the
    preferred version if that version appears in an external spec.
    """
    conf_str = """\
packages:
  y:
    version: ["2.7"]
    externals:
    - spec: y@2.7 # Not defined in y
      prefix: /fake/nonexistent/path/
    buildable: false
"""
    update_packages_config(conf_str)

    spec = Spec("x").concretized()
    assert spec["y"].satisfies("@2.7")
    assert spack.version.Version("2.7") not in spec["y"].package.versions


def test_requirement_is_successfully_applied(concretize_scope, test_repo):
    """If a simple requirement can be satisfied, make sure the
    concretization succeeds and the requirement spec is applied.
    """
    s1 = Spec("x").concretized()
    # Without any requirements/preferences, the later version is preferred
    assert s1.satisfies("@1.1")

    conf_str = """\
packages:
  x:
    require: "@1.0"
"""
    update_packages_config(conf_str)
    s2 = Spec("x").concretized()
    # The requirement forces choosing the eariler version
    assert s2.satisfies("@1.0")


def test_multiple_packages_requirements_are_respected(concretize_scope, test_repo):
    """Apply requirements to two packages; make sure the concretization
    succeeds and both requirements are respected.
    """
    conf_str = """\
packages:
  x:
    require: "@1.0"
  y:
    require: "@2.4"
"""
    update_packages_config(conf_str)
    spec = Spec("x").concretized()
    assert spec["x"].satisfies("@1.0")
    assert spec["y"].satisfies("@2.4")


def test_oneof(concretize_scope, test_repo):
    """'one_of' allows forcing the concretizer to satisfy one of
    the specs in the group (but not all have to be satisfied).
    """
    conf_str = """\
packages:
  y:
    require:
    - one_of: ["@2.4", "~shared"]
"""
    update_packages_config(conf_str)
    spec = Spec("x").concretized()
    # The concretizer only has to satisfy one of @2.4/~shared, and @2.4
    # comes first so it is prioritized
    assert spec["y"].satisfies("@2.4+shared")


def test_one_package_multiple_oneof_groups(concretize_scope, test_repo):
    """One package has two 'one_of' groups; check that both are
    applied.
    """
    conf_str = """\
packages:
  y:
    require:
    - one_of: ["@2.4%gcc", "@2.5%clang"]
    - one_of: ["@2.5~shared", "@2.4+shared"]
"""
    update_packages_config(conf_str)

    s1 = Spec("y@2.5").concretized()
    assert s1.satisfies("%clang~shared")

    s2 = Spec("y@2.4").concretized()
    assert s2.satisfies("%gcc+shared")


@pytest.mark.regression("34241")
def test_require_cflags(concretize_scope, mock_packages):
    """Ensures that flags can be required from configuration."""
    conf_str = """\
packages:
  mpich2:
    require: cflags="-g"
  mpi:
    require: mpich cflags="-O1"
"""
    update_packages_config(conf_str)

    spec_mpich2 = Spec("mpich2").concretized()
    assert spec_mpich2.satisfies("cflags=-g")

    spec_mpi = Spec("mpi").concretized()
    assert spec_mpi.satisfies("mpich cflags=-O1")


def test_requirements_for_package_that_is_not_needed(concretize_scope, test_repo):
    """Specify requirements for specs that are not concretized or
    a dependency of a concretized spec (in other words, none of
    the requirements are used for the requested spec).
    """
    # Note that the exact contents aren't important since this isn't
    # intended to be used, but the important thing is that a number of
    # packages have requirements applied
    conf_str = """\
packages:
  x:
    require: "@1.0"
  y:
    require:
    - one_of: ["@2.4%gcc", "@2.5%clang"]
    - one_of: ["@2.5~shared", "@2.4+shared"]
"""
    update_packages_config(conf_str)

    s1 = Spec("v").concretized()
    assert s1.satisfies("@2.1")


def test_oneof_ordering(concretize_scope, test_repo):
    """Ensure that earlier elements of 'one_of' have higher priority.
    This priority should override default priority (e.g. choosing
    later versions).
    """
    conf_str = """\
packages:
  y:
    require:
    - one_of: ["@2.4", "@2.5"]
"""
    update_packages_config(conf_str)

    s1 = Spec("y").concretized()
    assert s1.satisfies("@2.4")

    s2 = Spec("y@2.5").concretized()
    assert s2.satisfies("@2.5")


def test_reuse_oneof(concretize_scope, _create_test_repo, mutable_database, fake_installs):
    conf_str = """\
packages:
  y:
    require:
    - one_of: ["@2.5", "%gcc"]
"""

    with spack.repo.use_repositories(_create_test_repo):
        s1 = Spec("y@2.5%gcc").concretized()
        s1.package.do_install(fake=True, explicit=True)

        update_packages_config(conf_str)

        with spack.config.override("concretizer:reuse", True):
            s2 = Spec("y").concretized()
            assert not s2.satisfies("@2.5 %gcc")


@pytest.mark.parametrize(
    "allow_deprecated,expected,not_expected",
    [(True, ["@=2.3", "%gcc"], []), (False, ["%gcc"], ["@=2.3"])],
)
def test_requirements_and_deprecated_versions(
    allow_deprecated, expected, not_expected, concretize_scope, test_repo
):
    """Tests the expected behavior of requirements and deprecated versions.

    If deprecated versions are not allowed, concretization should just pick
    the other requirement.

    If deprecated versions are allowed, both requirements are honored.
    """
    # 2.3 is a deprecated versions. Ensure that any_of picks both constraints,
    # since they are possible
    conf_str = """\
packages:
  y:
    require:
    - any_of: ["@=2.3", "%gcc"]
"""
    update_packages_config(conf_str)

    with spack.config.override("config:deprecated", allow_deprecated):
        s1 = Spec("y").concretized()
        for constrain in expected:
            assert s1.satisfies(constrain)

        for constrain in not_expected:
            assert not s1.satisfies(constrain)


@pytest.mark.parametrize("spec_str,requirement_str", [("x", "%gcc"), ("x", "%clang")])
def test_default_requirements_with_all(spec_str, requirement_str, concretize_scope, test_repo):
    """Test that default requirements are applied to all packages."""
    conf_str = """\
packages:
  all:
    require: "{}"
""".format(
        requirement_str
    )
    update_packages_config(conf_str)

    spec = Spec(spec_str).concretized()
    for s in spec.traverse():
        assert s.satisfies(requirement_str)


@pytest.mark.parametrize(
    "requirements,expectations",
    [
        (("%gcc", "%clang"), ("%gcc", "%clang")),
        (("%gcc ~shared", "@1.0"), ("%gcc ~shared", "@1.0 +shared")),
    ],
)
def test_default_and_package_specific_requirements(
    concretize_scope, requirements, expectations, test_repo
):
    """Test that specific package requirements override default package requirements."""
    generic_req, specific_req = requirements
    generic_exp, specific_exp = expectations
    conf_str = """\
packages:
  all:
    require: "{}"
  x:
    require: "{}"
""".format(
        generic_req, specific_req
    )
    update_packages_config(conf_str)

    spec = Spec("x").concretized()
    assert spec.satisfies(specific_exp)
    for s in spec.traverse(root=False):
        assert s.satisfies(generic_exp)


@pytest.mark.parametrize("mpi_requirement", ["mpich", "mpich2", "zmpi"])
def test_requirements_on_virtual(mpi_requirement, concretize_scope, mock_packages):
    conf_str = """\
packages:
  mpi:
    require: "{}"
""".format(
        mpi_requirement
    )
    update_packages_config(conf_str)

    spec = Spec("callpath").concretized()
    assert "mpi" in spec
    assert mpi_requirement in spec


@pytest.mark.parametrize(
    "mpi_requirement,specific_requirement",
    [("mpich", "@3.0.3"), ("mpich2", "%clang"), ("zmpi", "%gcc")],
)
def test_requirements_on_virtual_and_on_package(
    mpi_requirement, specific_requirement, concretize_scope, mock_packages
):
    conf_str = """\
packages:
  mpi:
    require: "{0}"
  {0}:
    require: "{1}"
""".format(
        mpi_requirement, specific_requirement
    )
    update_packages_config(conf_str)

    spec = Spec("callpath").concretized()
    assert "mpi" in spec
    assert mpi_requirement in spec
    assert spec["mpi"].satisfies(specific_requirement)


def test_incompatible_virtual_requirements_raise(concretize_scope, mock_packages):
    conf_str = """\
    packages:
      mpi:
        require: "mpich"
    """
    update_packages_config(conf_str)

    spec = Spec("callpath ^zmpi")
    # TODO (multiple nodes): recover a better error message later
    with pytest.raises((UnsatisfiableSpecError, InternalConcretizerError)):
        spec.concretize()


def test_non_existing_variants_under_all(concretize_scope, mock_packages):
    conf_str = """\
    packages:
      all:
        require:
        - any_of: ["~foo", "@:"]
    """
    update_packages_config(conf_str)

    spec = Spec("callpath ^zmpi").concretized()
    assert "~foo" not in spec


@pytest.mark.parametrize(
    "packages_yaml,spec_str,expected_satisfies",
    [
        # In the tests below we set the compiler preference to "gcc" to be explicit on the
        # fact that "clang" is not the preferred compiler. That helps making more robust the
        # tests that verify enforcing "%clang" as a requirement.
        (
            """\
    packages:
      all:
        compiler: ["gcc", "clang"]

      libelf:
        require:
        - one_of: ["%clang"]
          when: "@0.8.13"
""",
            "libelf",
            [("@0.8.13%clang", True), ("%gcc", False)],
        ),
        (
            """\
    packages:
      all:
        compiler: ["gcc", "clang"]

      libelf:
        require:
        - one_of: ["%clang"]
          when: "@0.8.13"
""",
            "libelf@0.8.12",
            [("%clang", False), ("%gcc", True)],
        ),
        (
            """\
    packages:
      all:
        compiler: ["gcc", "clang"]

      libelf:
        require:
        - spec: "%clang"
          when: "@0.8.13"
""",
            "libelf@0.8.12",
            [("%clang", False), ("%gcc", True)],
        ),
        (
            """\
    packages:
      all:
        compiler: ["gcc", "clang"]

      libelf:
        require:
        - spec: "@0.8.13"
          when: "%clang"
""",
            "libelf@0.8.13%gcc",
            [("%clang", False), ("%gcc", True), ("@0.8.13", True)],
        ),
    ],
)
def test_conditional_requirements_from_packages_yaml(
    packages_yaml, spec_str, expected_satisfies, concretize_scope, mock_packages
):
    """Test that conditional requirements are required when the condition is met,
    and optional when the condition is not met.
    """
    update_packages_config(packages_yaml)
    spec = Spec(spec_str).concretized()
    for match_str, expected in expected_satisfies:
        assert spec.satisfies(match_str) is expected


@pytest.mark.parametrize(
    "packages_yaml,spec_str,expected_message",
    [
        (
            """\
    packages:
      mpileaks:
        require:
        - one_of: ["~debug"]
          message: "debug is not allowed"
""",
            "mpileaks+debug",
            "debug is not allowed",
        ),
        (
            """\
    packages:
      libelf:
        require:
        - one_of: ["%clang"]
          message: "can only be compiled with clang"
""",
            "libelf%gcc",
            "can only be compiled with clang",
        ),
        (
            """\
        packages:
          libelf:
            require:
            - one_of: ["%clang"]
              when: platform=test
              message: "can only be compiled with clang on the test platform"
    """,
            "libelf%gcc",
            "can only be compiled with clang on ",
        ),
        (
            """\
            packages:
              libelf:
                require:
                - spec: "%clang"
                  when: platform=test
                  message: "can only be compiled with clang on the test platform"
        """,
            "libelf%gcc",
            "can only be compiled with clang on ",
        ),
        (
            """\
        packages:
          libelf:
            require:
            - one_of: ["%clang", "%intel"]
              when: platform=test
              message: "can only be compiled with clang or intel on the test platform"
    """,
            "libelf%gcc",
            "can only be compiled with clang or intel",
        ),
    ],
)
def test_requirements_fail_with_custom_message(
    packages_yaml, spec_str, expected_message, concretize_scope, mock_packages
):
    """Test that specs failing due to requirements not being satisfiable fail with a
    custom error message.
    """
    update_packages_config(packages_yaml)
    with pytest.raises(spack.error.SpackError, match=expected_message):
        Spec(spec_str).concretized()


def test_skip_requirement_when_default_requirement_condition_cannot_be_met(
    concretize_scope, mock_packages
):
    """Tests that we can express a requirement condition under 'all' also in cases where
    the corresponding condition spec mentions variants or versions that don't exist in the
    package. For those packages the requirement rule is not emitted, since it can be
    determined to be always false.
    """
    packages_yaml = """
        packages:
          all:
            require:
            - one_of: ["%clang"]
              when: "+shared"
    """
    update_packages_config(packages_yaml)
    s = Spec("mpileaks").concretized()

    assert s.satisfies("%clang +shared")
    # Sanity checks that 'callpath' doesn't have the shared variant, but that didn't
    # cause failures during concretization.
    assert "shared" not in s["callpath"].variants


def test_requires_directive(concretize_scope, mock_packages):
    compilers_yaml = pathlib.Path(concretize_scope) / "compilers.yaml"
    compilers_yaml.write_text(
        """
compilers::
- compiler:
    spec: gcc@12.0.0
    paths:
      cc: /usr/bin/clang-12
      cxx: /usr/bin/clang++-12
      f77: null
      fc: null
    operating_system: debian6
    target: x86_64
    modules: []
"""
    )
    spack.config.CONFIG.clear_caches()

    # This package requires either clang or gcc
    s = Spec("requires_clang_or_gcc").concretized()
    assert s.satisfies("%gcc@12.0.0")

    # This package can only be compiled with clang
    with pytest.raises(spack.error.SpackError, match="can only be compiled with Clang"):
        Spec("requires_clang").concretized()


@pytest.mark.parametrize(
    "packages_yaml",
    [
        # Simple string
        """
        packages:
          all:
            require: "+shared"
    """,
        # List of strings
        """
        packages:
          all:
            require:
            - "+shared"
    """,
        # Objects with attributes
        """
        packages:
          all:
            require:
            - spec: "+shared"
    """,
        """
        packages:
          all:
            require:
            - one_of: ["+shared"]
    """,
    ],
)
def test_default_requirements_semantic(packages_yaml, concretize_scope, mock_packages):
    """Tests that requirements under 'all:' are by default applied only if the variant/property
    required exists, but are strict otherwise.

    For example:

      packages:
        all:
          require: "+shared"

    should enforce the value of "+shared" when a Boolean variant named "shared" exists. This is
    not overridable from the command line, so with the configuration above:

    > spack spec zlib~shared

    is unsatisfiable.
    """
    update_packages_config(packages_yaml)

    # Regular zlib concretize to +shared
    s = Spec("zlib").concretized()
    assert s.satisfies("+shared")

    # If we specify the variant we can concretize only the one matching the constraint
    s = Spec("zlib +shared").concretized()
    assert s.satisfies("+shared")
    with pytest.raises(UnsatisfiableSpecError):
        Spec("zlib ~shared").concretized()

    # A spec without the shared variant still concretize
    s = Spec("a").concretized()
    assert not s.satisfies("a +shared")
    assert not s.satisfies("a ~shared")


@pytest.mark.parametrize(
    "packages_yaml,spec_str,expected,not_expected",
    [
        # The package has a 'libs' mv variant defaulting to 'libs=shared'
        (
            """
        packages:
          all:
            require: "+libs"
    """,
            "multivalue-variant",
            ["libs=shared"],
            ["libs=static", "+libs"],
        ),
        (
            """
        packages:
          all:
            require: "libs=foo"
    """,
            "multivalue-variant",
            ["libs=shared"],
            ["libs=static", "libs=foo"],
        ),
        (
            # (TODO): revisit this case when we'll have exact value semantic for mv variants
            """
        packages:
          all:
            require: "libs=static"
    """,
            "multivalue-variant",
            ["libs=static", "libs=shared"],
            [],
        ),
        (
            # Constraint apply as a whole, so having a non-existing variant
            # invalidate the entire constraint
            """
        packages:
          all:
            require: "libs=static +feefoo"
    """,
            "multivalue-variant",
            ["libs=shared"],
            ["libs=static"],
        ),
    ],
)
def test_default_requirements_semantic_with_mv_variants(
    packages_yaml, spec_str, expected, not_expected, concretize_scope, mock_packages
):
    """Tests that requirements under 'all:' are behaving correctly under cases that could stem
    from MV variants.
    """
    update_packages_config(packages_yaml)
    s = Spec(spec_str).concretized()

    for constraint in expected:
        assert s.satisfies(constraint), constraint

    for constraint in not_expected:
        assert not s.satisfies(constraint), constraint