summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/versions.py
blob: a6d44027f4e2ccb5566d6c47ddf9956a7cb6cd44 (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
# Copyright 2013-2022 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)

"""These version tests were taken from the RPM source code.
We try to maintain compatibility with RPM's version semantics
where it makes sense.
"""
import os
import sys

import pytest

from llnl.util.filesystem import working_dir

import spack.package_base
import spack.spec
from spack.util.executable import which
from spack.version import (
    GitVersion,
    Version,
    VersionBase,
    VersionList,
    VersionRange,
    ver,
)


def assert_ver_lt(a, b):
    """Asserts the results of comparisons when 'a' is less than 'b'."""
    a, b = ver(a), ver(b)
    assert a < b
    assert a <= b
    assert a != b
    assert not a == b
    assert not a > b
    assert not a >= b


def assert_ver_gt(a, b):
    """Asserts the results of comparisons when 'a' is greater than 'b'."""
    a, b = ver(a), ver(b)
    assert a > b
    assert a >= b
    assert a != b
    assert not a == b
    assert not a < b
    assert not a <= b


def assert_ver_eq(a, b):
    """Asserts the results of comparisons when 'a' is equal to 'b'."""
    a, b = ver(a), ver(b)
    assert not a > b
    assert a >= b
    assert not a != b
    assert a == b
    assert not a < b
    assert a <= b


def assert_in(needle, haystack):
    """Asserts that 'needle' is in 'haystack'."""
    assert ver(needle) in ver(haystack)


def assert_not_in(needle, haystack):
    """Asserts that 'needle' is not in 'haystack'."""
    assert ver(needle) not in ver(haystack)


def assert_canonical(canonical_list, version_list):
    """Asserts that a redundant list is reduced to canonical form."""
    assert ver(canonical_list) == ver(version_list)


def assert_overlaps(v1, v2):
    """Asserts that two version ranges overlaps."""
    assert ver(v1).overlaps(ver(v2))


def assert_no_overlap(v1, v2):
    """Asserts that two version ranges do not overlap."""
    assert not ver(v1).overlaps(ver(v2))


def assert_satisfies(v1, v2):
    """Asserts that 'v1' satisfies 'v2'."""
    assert ver(v1).satisfies(ver(v2))


def assert_does_not_satisfy(v1, v2):
    """Asserts that 'v1' does not satisfy 'v2'."""
    assert not ver(v1).satisfies(ver(v2))


def check_intersection(expected, a, b):
    """Asserts that 'a' intersect 'b' == 'expected'."""
    assert ver(expected) == ver(a).intersection(ver(b))


def check_union(expected, a, b):
    """Asserts that 'a' union 'b' == 'expected'."""
    assert ver(expected) == ver(a).union(ver(b))


def test_string_prefix():
    assert_ver_eq("xsdk-0.2.0", "xsdk-0.2.0")
    assert_ver_lt("xsdk-0.2.0", "xsdk-0.3")
    assert_ver_gt("xsdk-0.3", "xsdk-0.2.0")


def test_two_segments():
    assert_ver_eq("1.0", "1.0")
    assert_ver_lt("1.0", "2.0")
    assert_ver_gt("2.0", "1.0")


def test_develop():
    assert_ver_eq("develop", "develop")
    assert_ver_eq("develop.local", "develop.local")
    assert_ver_lt("1.0", "develop")
    assert_ver_gt("develop", "1.0")
    assert_ver_eq("1.develop", "1.develop")
    assert_ver_lt("1.1", "1.develop")
    assert_ver_gt("1.develop", "1.0")
    assert_ver_gt("0.5.develop", "0.5")
    assert_ver_lt("0.5", "0.5.develop")
    assert_ver_lt("1.develop", "2.1")
    assert_ver_gt("2.1", "1.develop")
    assert_ver_lt("1.develop.1", "1.develop.2")
    assert_ver_gt("1.develop.2", "1.develop.1")
    assert_ver_lt("develop.1", "develop.2")
    assert_ver_gt("develop.2", "develop.1")
    # other +infinity versions
    assert_ver_gt("master", "9.0")
    assert_ver_gt("head", "9.0")
    assert_ver_gt("trunk", "9.0")
    assert_ver_gt("develop", "9.0")
    # hierarchical develop-like versions
    assert_ver_gt("develop", "master")
    assert_ver_gt("master", "head")
    assert_ver_gt("head", "trunk")
    assert_ver_gt("9.0", "system")
    # not develop
    assert_ver_lt("mydevelopmentnightmare", "1.1")
    assert_ver_lt("1.mydevelopmentnightmare", "1.1")
    assert_ver_gt("1.1", "1.mydevelopmentnightmare")


def test_isdevelop():
    assert ver("develop").isdevelop()
    assert ver("develop.1").isdevelop()
    assert ver("develop.local").isdevelop()
    assert ver("master").isdevelop()
    assert ver("head").isdevelop()
    assert ver("trunk").isdevelop()
    assert ver("1.develop").isdevelop()
    assert ver("1.develop.2").isdevelop()
    assert not ver("1.1").isdevelop()
    assert not ver("1.mydevelopmentnightmare.3").isdevelop()
    assert not ver("mydevelopmentnightmare.3").isdevelop()


def test_three_segments():
    assert_ver_eq("2.0.1", "2.0.1")
    assert_ver_lt("2.0", "2.0.1")
    assert_ver_gt("2.0.1", "2.0")


def test_alpha():
    # TODO: not sure whether I like this.  2.0.1a is *usually*
    # TODO: less than 2.0.1, but special-casing it makes version
    # TODO: comparison complicated.  See version.py
    assert_ver_eq("2.0.1a", "2.0.1a")
    assert_ver_gt("2.0.1a", "2.0.1")
    assert_ver_lt("2.0.1", "2.0.1a")


def test_patch():
    assert_ver_eq("5.5p1", "5.5p1")
    assert_ver_lt("5.5p1", "5.5p2")
    assert_ver_gt("5.5p2", "5.5p1")
    assert_ver_eq("5.5p10", "5.5p10")
    assert_ver_lt("5.5p1", "5.5p10")
    assert_ver_gt("5.5p10", "5.5p1")


def test_num_alpha_with_no_separator():
    assert_ver_lt("10xyz", "10.1xyz")
    assert_ver_gt("10.1xyz", "10xyz")
    assert_ver_eq("xyz10", "xyz10")
    assert_ver_lt("xyz10", "xyz10.1")
    assert_ver_gt("xyz10.1", "xyz10")


def test_alpha_with_dots():
    assert_ver_eq("xyz.4", "xyz.4")
    assert_ver_lt("xyz.4", "8")
    assert_ver_gt("8", "xyz.4")
    assert_ver_lt("xyz.4", "2")
    assert_ver_gt("2", "xyz.4")


def test_nums_and_patch():
    assert_ver_lt("5.5p2", "5.6p1")
    assert_ver_gt("5.6p1", "5.5p2")
    assert_ver_lt("5.6p1", "6.5p1")
    assert_ver_gt("6.5p1", "5.6p1")


def test_rc_versions():
    assert_ver_gt("6.0.rc1", "6.0")
    assert_ver_lt("6.0", "6.0.rc1")


def test_alpha_beta():
    assert_ver_gt("10b2", "10a1")
    assert_ver_lt("10a2", "10b2")


def test_double_alpha():
    assert_ver_eq("1.0aa", "1.0aa")
    assert_ver_lt("1.0a", "1.0aa")
    assert_ver_gt("1.0aa", "1.0a")


def test_padded_numbers():
    assert_ver_eq("10.0001", "10.0001")
    assert_ver_eq("10.0001", "10.1")
    assert_ver_eq("10.1", "10.0001")
    assert_ver_lt("10.0001", "10.0039")
    assert_ver_gt("10.0039", "10.0001")


def test_close_numbers():
    assert_ver_lt("4.999.9", "5.0")
    assert_ver_gt("5.0", "4.999.9")


def test_date_stamps():
    assert_ver_eq("20101121", "20101121")
    assert_ver_lt("20101121", "20101122")
    assert_ver_gt("20101122", "20101121")


def test_underscores():
    assert_ver_eq("2_0", "2_0")
    assert_ver_eq("2.0", "2_0")
    assert_ver_eq("2_0", "2.0")
    assert_ver_eq("2-0", "2_0")
    assert_ver_eq("2_0", "2-0")


def test_rpm_oddities():
    assert_ver_eq("1b.fc17", "1b.fc17")
    assert_ver_lt("1b.fc17", "1.fc17")
    assert_ver_gt("1.fc17", "1b.fc17")
    assert_ver_eq("1g.fc17", "1g.fc17")
    assert_ver_gt("1g.fc17", "1.fc17")
    assert_ver_lt("1.fc17", "1g.fc17")


# Stuff below here is not taken from RPM's tests and is
# unique to spack
def test_version_ranges():
    assert_ver_lt("1.2:1.4", "1.6")
    assert_ver_gt("1.6", "1.2:1.4")
    assert_ver_eq("1.2:1.4", "1.2:1.4")
    assert ver("1.2:1.4") != ver("1.2:1.6")

    assert_ver_lt("1.2:1.4", "1.5:1.6")
    assert_ver_gt("1.5:1.6", "1.2:1.4")


def test_contains():
    assert_in("1.3", "1.2:1.4")
    assert_in("1.2.5", "1.2:1.4")
    assert_in("1.3.5", "1.2:1.4")
    assert_in("1.3.5-7", "1.2:1.4")
    assert_not_in("1.1", "1.2:1.4")
    assert_not_in("1.5", "1.2:1.4")
    assert_not_in("1.5", "1.5.1:1.6")
    assert_not_in("1.5", "1.5.1:")

    assert_in("1.4.2", "1.2:1.4")
    assert_not_in("1.4.2", "1.2:1.4.0")

    assert_in("1.2.8", "1.2.7:1.4")
    assert_in("1.2.7:1.4", ":")
    assert_not_in("1.2.5", "1.2.7:1.4")

    assert_in("1.4.1", "1.2.7:1.4")
    assert_not_in("1.4.1", "1.2.7:1.4.0")


def test_in_list():
    assert_in("1.2", ["1.5", "1.2", "1.3"])
    assert_in("1.2.5", ["1.5", "1.2:1.3"])
    assert_in("1.5", ["1.5", "1.2:1.3"])
    assert_not_in("1.4", ["1.5", "1.2:1.3"])

    assert_in("1.2.5:1.2.7", [":"])
    assert_in("1.2.5:1.2.7", ["1.5", "1.2:1.3"])
    assert_not_in("1.2.5:1.5", ["1.5", "1.2:1.3"])
    assert_not_in("1.1:1.2.5", ["1.5", "1.2:1.3"])


def test_ranges_overlap():
    assert_overlaps("1.2", "1.2")
    assert_overlaps("1.2.1", "1.2.1")
    assert_overlaps("1.2.1b", "1.2.1b")

    assert_overlaps("1.2:1.7", "1.6:1.9")
    assert_overlaps(":1.7", "1.6:1.9")
    assert_overlaps(":1.7", ":1.9")
    assert_overlaps(":1.7", "1.6:")
    assert_overlaps("1.2:", "1.6:1.9")
    assert_overlaps("1.2:", ":1.9")
    assert_overlaps("1.2:", "1.6:")
    assert_overlaps(":", ":")
    assert_overlaps(":", "1.6:1.9")
    assert_overlaps("1.6:1.9", ":")


def test_overlap_with_containment():
    assert_in("1.6.5", "1.6")
    assert_in("1.6.5", ":1.6")

    assert_overlaps("1.6.5", ":1.6")
    assert_overlaps(":1.6", "1.6.5")

    assert_not_in(":1.6", "1.6.5")
    assert_in("1.6.5", ":1.6")


def test_lists_overlap():
    assert_overlaps("1.2b:1.7,5", "1.6:1.9,1")
    assert_overlaps("1,2,3,4,5", "3,4,5,6,7")
    assert_overlaps("1,2,3,4,5", "5,6,7")
    assert_overlaps("1,2,3,4,5", "5:7")
    assert_overlaps("1,2,3,4,5", "3, 6:7")
    assert_overlaps("1, 2, 4, 6.5", "3, 6:7")
    assert_overlaps("1, 2, 4, 6.5", ":, 5, 8")
    assert_overlaps("1, 2, 4, 6.5", ":")
    assert_no_overlap("1, 2, 4", "3, 6:7")
    assert_no_overlap("1,2,3,4,5", "6,7")
    assert_no_overlap("1,2,3,4,5", "6:7")


def test_canonicalize_list():
    assert_canonical(["1.2", "1.3", "1.4"], ["1.2", "1.3", "1.3", "1.4"])

    assert_canonical(["1.2", "1.3:1.4"], ["1.2", "1.3", "1.3:1.4"])

    assert_canonical(["1.2", "1.3:1.4"], ["1.2", "1.3:1.4", "1.4"])

    assert_canonical(["1.3:1.4"], ["1.3:1.4", "1.3", "1.3.1", "1.3.9", "1.4"])

    assert_canonical(["1.3:1.4"], ["1.3", "1.3.1", "1.3.9", "1.4", "1.3:1.4"])

    assert_canonical(["1.3:1.5"], ["1.3", "1.3.1", "1.3.9", "1.4:1.5", "1.3:1.4"])

    assert_canonical(["1.3:1.5"], ["1.3, 1.3.1,1.3.9,1.4:1.5,1.3:1.4"])

    assert_canonical(["1.3:1.5"], ["1.3, 1.3.1,1.3.9,1.4 : 1.5 , 1.3 : 1.4"])

    assert_canonical([":"], [":,1.3, 1.3.1,1.3.9,1.4 : 1.5 , 1.3 : 1.4"])


def test_intersection():
    check_intersection("2.5", "1.0:2.5", "2.5:3.0")
    check_intersection("2.5:2.7", "1.0:2.7", "2.5:3.0")
    check_intersection("0:1", ":", "0:1")

    check_intersection(["1.0", "2.5:2.7"], ["1.0:2.7"], ["2.5:3.0", "1.0"])
    check_intersection(["2.5:2.7"], ["1.1:2.7"], ["2.5:3.0", "1.0"])
    check_intersection(["0:1"], [":"], ["0:1"])


def test_intersect_with_containment():
    check_intersection("1.6.5", "1.6.5", ":1.6")
    check_intersection("1.6.5", ":1.6", "1.6.5")

    check_intersection("1.6:1.6.5", ":1.6.5", "1.6")
    check_intersection("1.6:1.6.5", "1.6", ":1.6.5")

    check_intersection("11.2", "11", "11.2")
    check_intersection("11.2", "11.2", "11")


def test_union_with_containment():
    check_union(":1.6", "1.6.5", ":1.6")
    check_union(":1.6", ":1.6", "1.6.5")

    check_union(":1.6", ":1.6.5", "1.6")
    check_union(":1.6", "1.6", ":1.6.5")

    check_union(":", "1.0:", ":2.0")

    check_union("1:4", "1:3", "2:4")
    check_union("1:4", "2:4", "1:3")

    # Tests successor/predecessor case.
    check_union("1:4", "1:2", "3:4")


def test_basic_version_satisfaction():
    assert_satisfies("4.7.3", "4.7.3")

    assert_satisfies("4.7.3", "4.7")
    assert_satisfies("4.7.3b2", "4.7")
    assert_satisfies("4.7b6", "4.7")

    assert_satisfies("4.7.3", "4")
    assert_satisfies("4.7.3b2", "4")
    assert_satisfies("4.7b6", "4")

    assert_does_not_satisfy("4.8.0", "4.9")
    assert_does_not_satisfy("4.8", "4.9")
    assert_does_not_satisfy("4", "4.9")


def test_basic_version_satisfaction_in_lists():
    assert_satisfies(["4.7.3"], ["4.7.3"])

    assert_satisfies(["4.7.3"], ["4.7"])
    assert_satisfies(["4.7.3b2"], ["4.7"])
    assert_satisfies(["4.7b6"], ["4.7"])

    assert_satisfies(["4.7.3"], ["4"])
    assert_satisfies(["4.7.3b2"], ["4"])
    assert_satisfies(["4.7b6"], ["4"])

    assert_does_not_satisfy(["4.8.0"], ["4.9"])
    assert_does_not_satisfy(["4.8"], ["4.9"])
    assert_does_not_satisfy(["4"], ["4.9"])


def test_version_range_satisfaction():
    assert_satisfies("4.7b6", "4.3:4.7")
    assert_satisfies("4.3.0", "4.3:4.7")
    assert_satisfies("4.3.2", "4.3:4.7")

    assert_does_not_satisfy("4.8.0", "4.3:4.7")
    assert_does_not_satisfy("4.3", "4.4:4.7")

    assert_satisfies("4.7b6", "4.3:4.7")
    assert_does_not_satisfy("4.8.0", "4.3:4.7")


def test_version_range_satisfaction_in_lists():
    assert_satisfies(["4.7b6"], ["4.3:4.7"])
    assert_satisfies(["4.3.0"], ["4.3:4.7"])
    assert_satisfies(["4.3.2"], ["4.3:4.7"])

    assert_does_not_satisfy(["4.8.0"], ["4.3:4.7"])
    assert_does_not_satisfy(["4.3"], ["4.4:4.7"])

    assert_satisfies(["4.7b6"], ["4.3:4.7"])
    assert_does_not_satisfy(["4.8.0"], ["4.3:4.7"])


def test_satisfaction_with_lists():
    assert_satisfies("4.7", "4.3, 4.6, 4.7")
    assert_satisfies("4.7.3", "4.3, 4.6, 4.7")
    assert_satisfies("4.6.5", "4.3, 4.6, 4.7")
    assert_satisfies("4.6.5.2", "4.3, 4.6, 4.7")

    assert_does_not_satisfy("4", "4.3, 4.6, 4.7")
    assert_does_not_satisfy("4.8.0", "4.2, 4.3:4.7")

    assert_satisfies("4.8.0", "4.2, 4.3:4.8")
    assert_satisfies("4.8.2", "4.2, 4.3:4.8")


def test_formatted_strings():
    versions = (
        "1.2.3b",
        "1_2_3b",
        "1-2-3b",
        "1.2-3b",
        "1.2_3b",
        "1-2.3b",
        "1-2_3b",
        "1_2.3b",
        "1_2-3b",
    )
    for item in versions:
        v = Version(item)
        assert v.dotted.string == "1.2.3b"
        assert v.dashed.string == "1-2-3b"
        assert v.underscored.string == "1_2_3b"
        assert v.joined.string == "123b"

        assert v.dotted.dashed.string == "1-2-3b"
        assert v.dotted.underscored.string == "1_2_3b"
        assert v.dotted.dotted.string == "1.2.3b"
        assert v.dotted.joined.string == "123b"


def test_up_to():
    v = Version("1.23-4_5b")

    assert v.up_to(1).string == "1"
    assert v.up_to(2).string == "1.23"
    assert v.up_to(3).string == "1.23-4"
    assert v.up_to(4).string == "1.23-4_5"
    assert v.up_to(5).string == "1.23-4_5b"

    assert v.up_to(-1).string == "1.23-4_5"
    assert v.up_to(-2).string == "1.23-4"
    assert v.up_to(-3).string == "1.23"
    assert v.up_to(-4).string == "1"

    assert v.up_to(2).dotted.string == "1.23"
    assert v.up_to(2).dashed.string == "1-23"
    assert v.up_to(2).underscored.string == "1_23"
    assert v.up_to(2).joined.string == "123"

    assert v.dotted.up_to(2).string == "1.23" == v.up_to(2).dotted.string
    assert v.dashed.up_to(2).string == "1-23" == v.up_to(2).dashed.string
    assert v.underscored.up_to(2).string == "1_23"
    assert v.up_to(2).underscored.string == "1_23"

    assert v.up_to(2).up_to(1).string == "1"


def test_repr_and_str():
    def check_repr_and_str(vrs):
        a = Version(vrs)
        assert repr(a) == "VersionBase('" + vrs + "')"
        b = eval(repr(a))
        assert a == b
        assert str(a) == vrs
        assert str(a) == str(b)

    check_repr_and_str("1.2.3")
    check_repr_and_str("R2016a")
    check_repr_and_str("R2016a.2-3_4")


def test_len():
    a = Version("1.2.3.4")
    assert len(a) == len(a.version)
    assert len(a) == 4
    b = Version("2018.0")
    assert len(b) == 2


def test_get_item():
    a = Version("0.1_2-3")
    assert isinstance(a[1], int)
    # Test slicing
    b = a[0:2]
    assert isinstance(b, VersionBase)
    assert b == Version("0.1")
    assert repr(b) == "VersionBase('0.1')"
    assert str(b) == "0.1"
    b = a[0:3]
    assert isinstance(b, VersionBase)
    assert b == Version("0.1_2")
    assert repr(b) == "VersionBase('0.1_2')"
    assert str(b) == "0.1_2"
    b = a[1:]
    assert isinstance(b, VersionBase)
    assert b == Version("1_2-3")
    assert repr(b) == "VersionBase('1_2-3')"
    assert str(b) == "1_2-3"
    # Raise TypeError on tuples
    with pytest.raises(TypeError):
        b.__getitem__(1, 2)


def test_list_highest():
    vl = VersionList(["master", "1.2.3", "develop", "3.4.5", "foobar"])
    assert vl.highest() == Version("develop")
    assert vl.lowest() == Version("foobar")
    assert vl.highest_numeric() == Version("3.4.5")

    vl2 = VersionList(["master", "develop"])
    assert vl2.highest_numeric() is None
    assert vl2.preferred() == Version("develop")
    assert vl2.lowest() == Version("master")


@pytest.mark.parametrize("version_str", ["foo 1.2.0", "!", "1!2", "=1.2.0"])
def test_invalid_versions(version_str):
    """Ensure invalid versions are rejected with a ValueError"""
    with pytest.raises(ValueError):
        Version(version_str)


@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows (yet)")
def test_versions_from_git(mock_git_version_info, monkeypatch, mock_packages):
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", "file://%s" % repo_path, raising=False
    )

    for commit in commits:
        spec = spack.spec.Spec("git-test-commit@%s" % commit)
        version = spec.version
        comparator = [
            str(v) if not isinstance(v, int) else v for v in version._cmp(version.ref_lookup)
        ]

        with working_dir(repo_path):
            which("git")("checkout", commit)
        with open(os.path.join(repo_path, filename), "r") as f:
            expected = f.read()

        assert str(comparator) == expected


@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows (yet)")
def test_git_hash_comparisons(mock_git_version_info, install_mockery, mock_packages, monkeypatch):
    """Check that hashes compare properly to versions"""
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", "file://%s" % repo_path, raising=False
    )

    # Spec based on earliest commit
    spec0 = spack.spec.Spec("git-test-commit@%s" % commits[-1])
    spec0.concretize()
    assert spec0.satisfies("@:0")
    assert not spec0.satisfies("@1.0")

    # Spec based on second commit (same as version 1.0)
    spec1 = spack.spec.Spec("git-test-commit@%s" % commits[-2])
    spec1.concretize()
    assert spec1.satisfies("@1.0")
    assert not spec1.satisfies("@1.1:")

    # Spec based on 4th commit (in timestamp order)
    spec4 = spack.spec.Spec("git-test-commit@%s" % commits[-4])
    spec4.concretize()
    assert spec4.satisfies("@1.1")
    assert spec4.satisfies("@1.0:1.2")


@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows (yet)")
def test_git_ref_comparisons(mock_git_version_info, install_mockery, mock_packages, monkeypatch):
    """Check that hashes compare properly to versions"""
    repo_path, filename, commits = mock_git_version_info
    monkeypatch.setattr(
        spack.package_base.PackageBase, "git", "file://%s" % repo_path, raising=False
    )

    # Spec based on tag v1.0
    spec_tag = spack.spec.Spec("git-test-commit@git.v1.0")
    spec_tag.concretize()
    assert spec_tag.satisfies("@1.0")
    assert not spec_tag.satisfies("@1.1:")
    assert str(spec_tag.version) == "git.v1.0"

    # Spec based on branch 1.x
    spec_branch = spack.spec.Spec("git-test-commit@git.1.x")
    spec_branch.concretize()
    assert spec_branch.satisfies("@1.2")
    assert spec_branch.satisfies("@1.1:1.3")
    assert str(spec_branch.version) == "git.1.x"


@pytest.mark.parametrize(
    "string,git",
    [
        ("1.2.9", False),
        ("gitmain", False),
        ("git.foo", True),
        ("git.abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", True),
        ("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", True),
    ],
)
def test_version_git_vs_base(string, git):
    assert isinstance(Version(string), GitVersion) == git


def test_version_range_nonempty():
    assert Version("1.2.9") in VersionRange("1.2.0", "1.2")
    assert Version("1.1.1") in ver("1.0:1")


def test_empty_version_range_raises():
    with pytest.raises(ValueError):
        assert VersionRange("2", "1.0")
    with pytest.raises(ValueError):
        assert ver("2:1.0")


def test_version_empty_slice():
    """Check an empty slice to confirm get "empty" version instead of
    an IndexError (#25953).
    """
    assert Version("1.")[1:] == Version("")


def test_version_wrong_idx_type():
    """Ensure exception raised if attempt to use non-integer index."""
    v = Version("1.1")
    with pytest.raises(TypeError):
        v["0:"]


@pytest.mark.regression("29170")
def test_version_range_satisfies_means_nonempty_intersection():
    x = VersionRange("3.7.0", "3")
    y = VersionRange("3.6.0", "3.6.0")
    assert not x.satisfies(y)
    assert not y.satisfies(x)


@pytest.mark.regression("26482")
def test_version_list_with_range_included_in_concrete_version_interpreted_as_range():
    # Note: this test only tests whether we can construct a version list of a range
    # and a version, where the range is contained in the version when it is interpreted
    # as a range. That is: Version('3.1') interpreted as VersionRange('3.1', '3.1').
    # Cleary it *shouldn't* be interpreted that way, but that is how Spack currently
    # behaves, and this test only ensures that creating a VersionList of this type
    # does not throw like reported in the linked Github issue.
    VersionList([Version("3.1"), VersionRange("3.1.1", "3.1.2")])


@pytest.mark.xfail
def test_version_list_with_range_and_concrete_version_is_not_concrete():
    v = VersionList([Version("3.1"), VersionRange("3.1.1", "3.1.2")])
    assert v.concrete


@pytest.mark.parametrize(
    "vstring, eq_vstring, is_commit",
    (
        ("abc12" * 8 + "=develop", "develop", True),
        ("git." + "abc12" * 8 + "=main", "main", True),
        ("a" * 40 + "=develop", "develop", True),
        ("b" * 40 + "=3.2", "3.2", True),
        ("git.foo=3.2", "3.2", False),
    ),
)
def test_git_ref_can_be_assigned_a_version(vstring, eq_vstring, is_commit):
    v = Version(vstring)
    v_equivalent = Version(eq_vstring)
    assert v.is_commit == is_commit
    assert v.is_ref
    assert not v._ref_lookup
    assert v_equivalent.version == v.ref_version