summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/variant.py
blob: 62954138e58796e1213bbef099c47921726d3402 (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
# Copyright 2013-2023 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 numbers

import pytest

import spack.error
import spack.variant
from spack.variant import (
    BoolValuedVariant,
    DuplicateVariantError,
    InconsistentValidationError,
    InvalidVariantValueError,
    MultipleValuesInExclusiveVariantError,
    MultiValuedVariant,
    SingleValuedVariant,
    UnsatisfiableVariantSpecError,
    Variant,
    VariantMap,
    disjoint_sets,
)


class TestMultiValuedVariant:
    def test_initialization(self):
        # Basic properties
        a = MultiValuedVariant("foo", "bar,baz")
        assert repr(a) == "MultiValuedVariant('foo', 'bar,baz')"
        assert str(a) == "foo=bar,baz"
        assert a.value == ("bar", "baz")
        assert "bar" in a
        assert "baz" in a
        assert eval(repr(a)) == a

        # Spaces are trimmed
        b = MultiValuedVariant("foo", "bar, baz")
        assert repr(b) == "MultiValuedVariant('foo', 'bar, baz')"
        assert str(b) == "foo=bar,baz"
        assert b.value == ("bar", "baz")
        assert "bar" in b
        assert "baz" in b
        assert a == b
        assert hash(a) == hash(b)
        assert eval(repr(b)) == a

        # Order is not important
        c = MultiValuedVariant("foo", "baz, bar")
        assert repr(c) == "MultiValuedVariant('foo', 'baz, bar')"
        assert str(c) == "foo=bar,baz"
        assert c.value == ("bar", "baz")
        assert "bar" in c
        assert "baz" in c
        assert a == c
        assert hash(a) == hash(c)
        assert eval(repr(c)) == a

        # Check the copy
        d = a.copy()
        assert repr(a) == repr(d)
        assert str(a) == str(d)
        assert d.value == ("bar", "baz")
        assert "bar" in d
        assert "baz" in d
        assert a == d
        assert a is not d
        assert hash(a) == hash(d)
        assert eval(repr(d)) == a

    def test_satisfies(self):
        a = MultiValuedVariant("foo", "bar,baz")
        b = MultiValuedVariant("foo", "bar")
        c = MultiValuedVariant("fee", "bar,baz")
        d = MultiValuedVariant("foo", "True")

        # 'foo=bar,baz' satisfies 'foo=bar'
        assert a.satisfies(b)

        # 'foo=bar' does not satisfy 'foo=bar,baz'
        assert not b.satisfies(a)

        # 'foo=bar,baz' does not satisfy 'foo=bar,baz' and vice-versa
        assert not a.satisfies(c)
        assert not c.satisfies(a)

        # Implicit type conversion for variants of other types

        b_sv = SingleValuedVariant("foo", "bar")
        assert b.satisfies(b_sv)
        d_sv = SingleValuedVariant("foo", "True")
        assert d.satisfies(d_sv)
        almost_d_bv = SingleValuedVariant("foo", "true")
        assert not d.satisfies(almost_d_bv)

        d_bv = BoolValuedVariant("foo", "True")
        assert d.satisfies(d_bv)
        # This case is 'peculiar': the two BV instances are
        # equivalent, but if converted to MV they are not
        # as MV is case sensitive with respect to 'True' and 'False'
        almost_d_bv = BoolValuedVariant("foo", "true")
        assert not d.satisfies(almost_d_bv)

    def test_compatible(self):
        a = MultiValuedVariant("foo", "bar,baz")
        b = MultiValuedVariant("foo", "True")
        c = MultiValuedVariant("fee", "bar,baz")
        d = MultiValuedVariant("foo", "bar,barbaz")

        # If the name of two multi-valued variants is the same,
        # they are compatible
        assert a.compatible(b)
        assert not a.compatible(c)
        assert a.compatible(d)

        assert b.compatible(a)
        assert not b.compatible(c)
        assert b.compatible(d)

        assert not c.compatible(a)
        assert not c.compatible(b)
        assert not c.compatible(d)

        assert d.compatible(a)
        assert d.compatible(b)
        assert not d.compatible(c)

        # Implicit type conversion for other types

        b_sv = SingleValuedVariant("foo", "True")
        assert b.compatible(b_sv)
        assert not c.compatible(b_sv)

        b_bv = BoolValuedVariant("foo", "True")
        assert b.compatible(b_bv)
        assert not c.compatible(b_bv)

    def test_constrain(self):
        # Try to constrain on a value with less constraints than self
        a = MultiValuedVariant("foo", "bar,baz")
        b = MultiValuedVariant("foo", "bar")

        changed = a.constrain(b)
        assert not changed
        t = MultiValuedVariant("foo", "bar,baz")
        assert a == t

        # Try to constrain on a value with more constraints than self
        a = MultiValuedVariant("foo", "bar,baz")
        b = MultiValuedVariant("foo", "bar")

        changed = b.constrain(a)
        assert changed
        t = MultiValuedVariant("foo", "bar,baz")
        assert a == t

        # Try to constrain on the same value
        a = MultiValuedVariant("foo", "bar,baz")
        b = a.copy()

        changed = a.constrain(b)
        assert not changed
        t = MultiValuedVariant("foo", "bar,baz")
        assert a == t

        # Try to constrain on a different name
        a = MultiValuedVariant("foo", "bar,baz")
        b = MultiValuedVariant("fee", "bar")

        with pytest.raises(ValueError):
            a.constrain(b)

        # Implicit type conversion for variants of other types

        a = MultiValuedVariant("foo", "bar,baz")
        b_sv = SingleValuedVariant("foo", "bar")
        c_sv = SingleValuedVariant("foo", "barbaz")

        assert not a.constrain(b_sv)
        assert a.constrain(c_sv)

        d_bv = BoolValuedVariant("foo", "True")

        assert a.constrain(d_bv)
        assert not a.constrain(d_bv)

    def test_yaml_entry(self):
        a = MultiValuedVariant("foo", "bar,baz,barbaz")
        b = MultiValuedVariant("foo", "bar, baz,  barbaz")
        expected = ("foo", sorted(["bar", "baz", "barbaz"]))

        assert a.yaml_entry() == expected
        assert b.yaml_entry() == expected

        a = MultiValuedVariant("foo", "bar")
        expected = ("foo", sorted(["bar"]))

        assert a.yaml_entry() == expected


class TestSingleValuedVariant:
    def test_initialization(self):
        # Basic properties
        a = SingleValuedVariant("foo", "bar")
        assert repr(a) == "SingleValuedVariant('foo', 'bar')"
        assert str(a) == "foo=bar"
        assert a.value == "bar"
        assert "bar" in a
        assert eval(repr(a)) == a

        # Raise if multiple values are passed
        with pytest.raises(ValueError):
            SingleValuedVariant("foo", "bar, baz")

        # Check the copy
        b = a.copy()
        assert repr(a) == repr(b)
        assert str(a) == str(b)
        assert b.value == "bar"
        assert "bar" in b
        assert a == b
        assert a is not b
        assert hash(a) == hash(b)
        assert eval(repr(b)) == a

    def test_satisfies(self):
        a = SingleValuedVariant("foo", "bar")
        b = SingleValuedVariant("foo", "bar")
        c = SingleValuedVariant("foo", "baz")
        d = SingleValuedVariant("fee", "bar")
        e = SingleValuedVariant("foo", "True")

        # 'foo=bar' can only satisfy 'foo=bar'
        assert a.satisfies(b)
        assert not a.satisfies(c)
        assert not a.satisfies(d)

        assert b.satisfies(a)
        assert not b.satisfies(c)
        assert not b.satisfies(d)

        assert not c.satisfies(a)
        assert not c.satisfies(b)
        assert not c.satisfies(d)

        # Implicit type conversion for variants of other types

        a_mv = MultiValuedVariant("foo", "bar")
        assert a.satisfies(a_mv)
        multiple_values = MultiValuedVariant("foo", "bar,baz")
        assert not a.satisfies(multiple_values)

        e_bv = BoolValuedVariant("foo", "True")
        assert e.satisfies(e_bv)
        almost_e_bv = BoolValuedVariant("foo", "true")
        assert not e.satisfies(almost_e_bv)

    def test_compatible(self):
        a = SingleValuedVariant("foo", "bar")
        b = SingleValuedVariant("fee", "bar")
        c = SingleValuedVariant("foo", "baz")
        d = SingleValuedVariant("foo", "bar")

        # If the name of two multi-valued variants is the same,
        # they are compatible
        assert not a.compatible(b)
        assert not a.compatible(c)
        assert a.compatible(d)

        assert not b.compatible(a)
        assert not b.compatible(c)
        assert not b.compatible(d)

        assert not c.compatible(a)
        assert not c.compatible(b)
        assert not c.compatible(d)

        assert d.compatible(a)
        assert not d.compatible(b)
        assert not d.compatible(c)

        # Implicit type conversion for variants of other types

        a_mv = MultiValuedVariant("foo", "bar")
        b_mv = MultiValuedVariant("fee", "bar")
        c_mv = MultiValuedVariant("foo", "baz")
        d_mv = MultiValuedVariant("foo", "bar")

        assert not a.compatible(b_mv)
        assert not a.compatible(c_mv)
        assert a.compatible(d_mv)

        assert not b.compatible(a_mv)
        assert not b.compatible(c_mv)
        assert not b.compatible(d_mv)

        assert not c.compatible(a_mv)
        assert not c.compatible(b_mv)
        assert not c.compatible(d_mv)

        assert d.compatible(a_mv)
        assert not d.compatible(b_mv)
        assert not d.compatible(c_mv)

        e = SingleValuedVariant("foo", "True")
        e_bv = BoolValuedVariant("foo", "True")
        almost_e_bv = BoolValuedVariant("foo", "true")

        assert e.compatible(e_bv)
        assert not e.compatible(almost_e_bv)

    def test_constrain(self):
        # Try to constrain on a value equal to self
        a = SingleValuedVariant("foo", "bar")
        b = SingleValuedVariant("foo", "bar")

        changed = a.constrain(b)
        assert not changed
        t = SingleValuedVariant("foo", "bar")
        assert a == t

        # Try to constrain on a value with a different value
        a = SingleValuedVariant("foo", "bar")
        b = SingleValuedVariant("foo", "baz")

        with pytest.raises(UnsatisfiableVariantSpecError):
            b.constrain(a)

        # Try to constrain on a value with a different value
        a = SingleValuedVariant("foo", "bar")
        b = SingleValuedVariant("fee", "bar")

        with pytest.raises(ValueError):
            b.constrain(a)

        # Try to constrain on the same value
        a = SingleValuedVariant("foo", "bar")
        b = a.copy()

        changed = a.constrain(b)
        assert not changed
        t = SingleValuedVariant("foo", "bar")
        assert a == t

        # Implicit type conversion for variants of other types
        a = SingleValuedVariant("foo", "True")
        mv = MultiValuedVariant("foo", "True")
        bv = BoolValuedVariant("foo", "True")
        for v in (mv, bv):
            assert not a.constrain(v)

    def test_yaml_entry(self):
        a = SingleValuedVariant("foo", "bar")
        expected = ("foo", "bar")

        assert a.yaml_entry() == expected


class TestBoolValuedVariant:
    def test_initialization(self):
        # Basic properties - True value
        for v in (True, "True", "TRUE", "TrUe"):
            a = BoolValuedVariant("foo", v)
            assert repr(a) == "BoolValuedVariant('foo', {0})".format(repr(v))
            assert str(a) == "+foo"
            assert a.value is True
            assert True in a
            assert eval(repr(a)) == a

        # Copy - True value
        b = a.copy()
        assert repr(a) == repr(b)
        assert str(a) == str(b)
        assert b.value is True
        assert True in b
        assert a == b
        assert a is not b
        assert hash(a) == hash(b)
        assert eval(repr(b)) == a

        # Basic properties - False value
        for v in (False, "False", "FALSE", "FaLsE"):
            a = BoolValuedVariant("foo", v)
            assert repr(a) == "BoolValuedVariant('foo', {0})".format(repr(v))
            assert str(a) == "~foo"
            assert a.value is False
            assert False in a
            assert eval(repr(a)) == a

        # Copy - True value
        b = a.copy()
        assert repr(a) == repr(b)
        assert str(a) == str(b)
        assert b.value is False
        assert False in b
        assert a == b
        assert a is not b
        assert eval(repr(b)) == a

        # Invalid values
        for v in ("bar", "bar,baz"):
            with pytest.raises(ValueError):
                BoolValuedVariant("foo", v)

    def test_satisfies(self):
        a = BoolValuedVariant("foo", True)
        b = BoolValuedVariant("foo", False)
        c = BoolValuedVariant("fee", False)
        d = BoolValuedVariant("foo", "True")

        assert not a.satisfies(b)
        assert not a.satisfies(c)
        assert a.satisfies(d)

        assert not b.satisfies(a)
        assert not b.satisfies(c)
        assert not b.satisfies(d)

        assert not c.satisfies(a)
        assert not c.satisfies(b)
        assert not c.satisfies(d)

        assert d.satisfies(a)
        assert not d.satisfies(b)
        assert not d.satisfies(c)

        # BV variants are case insensitive to 'True' or 'False'
        d_mv = MultiValuedVariant("foo", "True")
        assert d.satisfies(d_mv)
        assert not b.satisfies(d_mv)

        d_mv = MultiValuedVariant("foo", "FaLsE")
        assert not d.satisfies(d_mv)
        assert b.satisfies(d_mv)

        d_mv = MultiValuedVariant("foo", "bar")
        assert not d.satisfies(d_mv)
        assert not b.satisfies(d_mv)

        d_sv = SingleValuedVariant("foo", "True")
        assert d.satisfies(d_sv)

    def test_compatible(self):
        a = BoolValuedVariant("foo", True)
        b = BoolValuedVariant("fee", True)
        c = BoolValuedVariant("foo", False)
        d = BoolValuedVariant("foo", "True")

        # If the name of two multi-valued variants is the same,
        # they are compatible
        assert not a.compatible(b)
        assert not a.compatible(c)
        assert a.compatible(d)

        assert not b.compatible(a)
        assert not b.compatible(c)
        assert not b.compatible(d)

        assert not c.compatible(a)
        assert not c.compatible(b)
        assert not c.compatible(d)

        assert d.compatible(a)
        assert not d.compatible(b)
        assert not d.compatible(c)

        for value in ("True", "TrUe", "TRUE"):
            d_mv = MultiValuedVariant("foo", value)
            assert d.compatible(d_mv)
            assert not c.compatible(d_mv)

            d_sv = SingleValuedVariant("foo", value)
            assert d.compatible(d_sv)
            assert not c.compatible(d_sv)

    def test_constrain(self):
        # Try to constrain on a value equal to self
        a = BoolValuedVariant("foo", "True")
        b = BoolValuedVariant("foo", True)

        changed = a.constrain(b)
        assert not changed
        t = BoolValuedVariant("foo", True)
        assert a == t

        # Try to constrain on a value with a different value
        a = BoolValuedVariant("foo", True)
        b = BoolValuedVariant("foo", False)

        with pytest.raises(UnsatisfiableVariantSpecError):
            b.constrain(a)

        # Try to constrain on a value with a different value
        a = BoolValuedVariant("foo", True)
        b = BoolValuedVariant("fee", True)

        with pytest.raises(ValueError):
            b.constrain(a)

        # Try to constrain on the same value
        a = BoolValuedVariant("foo", True)
        b = a.copy()

        changed = a.constrain(b)
        assert not changed
        t = BoolValuedVariant("foo", True)
        assert a == t

        # Try to constrain on other values
        a = BoolValuedVariant("foo", "True")
        sv = SingleValuedVariant("foo", "True")
        mv = MultiValuedVariant("foo", "True")
        for v in (sv, mv):
            assert not a.constrain(v)

    def test_yaml_entry(self):
        a = BoolValuedVariant("foo", "True")
        expected = ("foo", True)
        assert a.yaml_entry() == expected

        a = BoolValuedVariant("foo", "False")
        expected = ("foo", False)
        assert a.yaml_entry() == expected


def test_from_node_dict():
    a = MultiValuedVariant.from_node_dict("foo", ["bar"])
    assert type(a) is MultiValuedVariant

    a = MultiValuedVariant.from_node_dict("foo", "bar")
    assert type(a) is SingleValuedVariant

    a = MultiValuedVariant.from_node_dict("foo", "true")
    assert type(a) is BoolValuedVariant


class TestVariant:
    def test_validation(self):
        a = Variant(
            "foo", default="", description="", values=("bar", "baz", "foobar"), multi=False
        )
        # Valid vspec, shouldn't raise
        vspec = a.make_variant("bar")
        a.validate_or_raise(vspec)

        # Multiple values are not allowed
        with pytest.raises(MultipleValuesInExclusiveVariantError):
            vspec.value = "bar,baz"

        # Inconsistent vspec
        vspec.name = "FOO"
        with pytest.raises(InconsistentValidationError):
            a.validate_or_raise(vspec)

        # Valid multi-value vspec
        a.multi = True
        vspec = a.make_variant("bar,baz")
        a.validate_or_raise(vspec)
        # Add an invalid value
        vspec.value = "bar,baz,barbaz"
        with pytest.raises(InvalidVariantValueError):
            a.validate_or_raise(vspec)

    def test_callable_validator(self):
        def validator(x):
            try:
                return isinstance(int(x), numbers.Integral)
            except ValueError:
                return False

        a = Variant("foo", default=1024, description="", values=validator, multi=False)
        vspec = a.make_default()
        a.validate_or_raise(vspec)
        vspec.value = 2056
        a.validate_or_raise(vspec)
        vspec.value = "foo"
        with pytest.raises(InvalidVariantValueError):
            a.validate_or_raise(vspec)

    def test_representation(self):
        a = Variant(
            "foo", default="", description="", values=("bar", "baz", "foobar"), multi=False
        )
        assert a.allowed_values == "bar, baz, foobar"


class TestVariantMapTest:
    def test_invalid_values(self):
        # Value with invalid type
        a = VariantMap(None)
        with pytest.raises(TypeError):
            a["foo"] = 2

        # Duplicate variant
        a["foo"] = MultiValuedVariant("foo", "bar,baz")
        with pytest.raises(DuplicateVariantError):
            a["foo"] = MultiValuedVariant("foo", "bar")

        with pytest.raises(DuplicateVariantError):
            a["foo"] = SingleValuedVariant("foo", "bar")

        with pytest.raises(DuplicateVariantError):
            a["foo"] = BoolValuedVariant("foo", True)

        # Non matching names between key and vspec.name
        with pytest.raises(KeyError):
            a["bar"] = MultiValuedVariant("foo", "bar")

    def test_set_item(self):
        # Check that all the three types of variants are accepted
        a = VariantMap(None)

        a["foo"] = BoolValuedVariant("foo", True)
        a["bar"] = SingleValuedVariant("bar", "baz")
        a["foobar"] = MultiValuedVariant("foobar", "a, b, c, d, e")

    def test_substitute(self):
        # Check substitution of a key that exists
        a = VariantMap(None)
        a["foo"] = BoolValuedVariant("foo", True)
        a.substitute(SingleValuedVariant("foo", "bar"))

        # Trying to substitute something that is not
        # in the map will raise a KeyError
        with pytest.raises(KeyError):
            a.substitute(BoolValuedVariant("bar", True))

    def test_satisfies_and_constrain(self):
        # foo=bar foobar=fee feebar=foo
        a = VariantMap(None)
        a["foo"] = MultiValuedVariant("foo", "bar")
        a["foobar"] = SingleValuedVariant("foobar", "fee")
        a["feebar"] = SingleValuedVariant("feebar", "foo")

        # foo=bar,baz foobar=fee shared=True
        b = VariantMap(None)
        b["foo"] = MultiValuedVariant("foo", "bar, baz")
        b["foobar"] = SingleValuedVariant("foobar", "fee")
        b["shared"] = BoolValuedVariant("shared", True)

        assert a.intersects(b)
        assert b.intersects(a)

        assert not a.satisfies(b)
        assert not b.satisfies(a)

        # foo=bar,baz foobar=fee feebar=foo shared=True
        c = VariantMap(None)
        c["foo"] = MultiValuedVariant("foo", "bar, baz")
        c["foobar"] = SingleValuedVariant("foobar", "fee")
        c["feebar"] = SingleValuedVariant("feebar", "foo")
        c["shared"] = BoolValuedVariant("shared", True)

        assert a.constrain(b)
        assert a == c

    def test_copy(self):
        a = VariantMap(None)
        a["foo"] = BoolValuedVariant("foo", True)
        a["bar"] = SingleValuedVariant("bar", "baz")
        a["foobar"] = MultiValuedVariant("foobar", "a, b, c, d, e")

        c = a.copy()
        assert a == c

    def test_str(self):
        c = VariantMap(None)
        c["foo"] = MultiValuedVariant("foo", "bar, baz")
        c["foobar"] = SingleValuedVariant("foobar", "fee")
        c["feebar"] = SingleValuedVariant("feebar", "foo")
        c["shared"] = BoolValuedVariant("shared", True)
        assert str(c) == "+shared feebar=foo foo=bar,baz foobar=fee"


def test_disjoint_set_initialization_errors():
    # Constructing from non-disjoint sets should raise an exception
    with pytest.raises(spack.error.SpecError) as exc_info:
        disjoint_sets(("a", "b"), ("b", "c"))
    assert "sets in input must be disjoint" in str(exc_info.value)

    # A set containing the reserved item 'none' along with other items
    # should raise an exception
    with pytest.raises(spack.error.SpecError) as exc_info:
        disjoint_sets(("a", "b"), ("none", "c"))
    assert "The value 'none' represents the empty set," in str(exc_info.value)


def test_disjoint_set_initialization():
    # Test that no error is thrown when the sets are disjoint
    d = disjoint_sets(("a",), ("b", "c"), ("e", "f"))

    assert d.default == "none"
    assert d.multi is True
    assert set(x for x in d) == set(["none", "a", "b", "c", "e", "f"])


def test_disjoint_set_fluent_methods():
    # Construct an object without the empty set
    d = disjoint_sets(("a",), ("b", "c"), ("e", "f")).prohibit_empty_set()
    assert set(("none",)) not in d.sets

    # Call this 2 times to check that no matter whether
    # the empty set was allowed or not before, the state
    # returned is consistent.
    for _ in range(2):
        d = d.allow_empty_set()
        assert set(("none",)) in d.sets
        assert "none" in d
        assert "none" in [x for x in d]
        assert "none" in d.feature_values

    # Marking a value as 'non-feature' removes it from the
    # list of feature values, but not for the items returned
    # when iterating over the object.
    d = d.with_non_feature_values("none")
    assert "none" in d
    assert "none" in [x for x in d]
    assert "none" not in d.feature_values

    # Call this 2 times to check that no matter whether
    # the empty set was allowed or not before, the state
    # returned is consistent.
    for _ in range(2):
        d = d.prohibit_empty_set()
        assert set(("none",)) not in d.sets
        assert "none" not in d
        assert "none" not in [x for x in d]
        assert "none" not in d.feature_values


@pytest.mark.regression("32694")
@pytest.mark.parametrize("other", [True, False])
def test_conditional_value_comparable_to_bool(other):
    value = spack.variant.Value("98", when="@1.0")
    comparison = value == other
    assert comparison is False


@pytest.mark.regression("40405")
def test_wild_card_valued_variants_equivalent_to_str():
    """
    There was a bug prioro to PR 40406 in that variants with wildcard values "*"
    were being overwritten in the variant constructor.
    The expected/appropriate behavior is for it to behave like value=str and this
    test demonstrates that the two are now equivalent
    """
    str_var = spack.variant.Variant(
        name="str_var",
        default="none",
        values=str,
        description="str variant",
        multi=True,
        validator=None,
    )

    wild_var = spack.variant.Variant(
        name="wild_var",
        default="none",
        values="*",
        description="* variant",
        multi=True,
        validator=None,
    )

    several_arbitrary_values = ("doe", "re", "mi")
    # "*" case
    wild_output = wild_var.make_variant(several_arbitrary_values)
    wild_var.validate_or_raise(wild_output)
    # str case
    str_output = str_var.make_variant(several_arbitrary_values)
    str_var.validate_or_raise(str_output)
    # equivalence each instance already validated
    assert str_output.value == wild_output.value