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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
|
# 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)
import glob
import os
import sys
from argparse import Namespace
import pytest
from six import StringIO
import llnl.util.filesystem as fs
import llnl.util.link_tree
import spack.cmd.env
import spack.environment as ev
import spack.environment.shell
import spack.hash_types as ht
import spack.modules
import spack.repo
import spack.util.spack_json as sjson
from spack.cmd.env import _env_create
from spack.main import SpackCommand, SpackCommandError
from spack.spec import Spec
from spack.stage import stage_prefix
from spack.util.mock_package import MockPackageMultiRepo
from spack.util.path import substitute_path_variables
# everything here uses the mock_env_path
pytestmark = [
pytest.mark.usefixtures('mutable_mock_env_path', 'config', 'mutable_mock_repo'),
pytest.mark.maybeslow
]
env = SpackCommand('env')
install = SpackCommand('install')
add = SpackCommand('add')
remove = SpackCommand('remove')
concretize = SpackCommand('concretize')
stage = SpackCommand('stage')
uninstall = SpackCommand('uninstall')
find = SpackCommand('find')
if sys.platform == 'win32':
sep = 'C:\\'
else:
sep = os.sep
def check_mpileaks_and_deps_in_view(viewdir):
"""Check that the expected install directories exist."""
assert os.path.exists(str(viewdir.join('.spack', 'mpileaks')))
assert os.path.exists(str(viewdir.join('.spack', 'libdwarf')))
def check_viewdir_removal(viewdir):
"""Check that the uninstall/removal worked."""
assert (not os.path.exists(str(viewdir.join('.spack'))) or
os.listdir(str(viewdir.join('.spack'))) == ['projections.yaml'])
def test_add():
e = ev.create('test')
e.add('mpileaks')
assert Spec('mpileaks') in e.user_specs
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_add_virtual():
env('create', 'test')
e = ev.read('test')
e.add('mpi')
e.concretize()
hashes = e.concretized_order
assert len(hashes) == 1
spec = e.specs_by_hash[hashes[0]]
assert spec.satisfies('mpi')
def test_env_add_nonexistant_fails():
env('create', 'test')
e = ev.read('test')
with pytest.raises(ev.SpackEnvironmentError, match=r'no such package'):
e.add('thispackagedoesnotexist')
def test_env_list(mutable_mock_env_path):
env('create', 'foo')
env('create', 'bar')
env('create', 'baz')
out = env('list')
assert 'foo' in out
assert 'bar' in out
assert 'baz' in out
# make sure `spack env list` skips invalid things in var/spack/env
mutable_mock_env_path.join('.DS_Store').ensure(file=True)
out = env('list')
assert 'foo' in out
assert 'bar' in out
assert 'baz' in out
assert '.DS_Store' not in out
def test_env_remove(capfd):
env('create', 'foo')
env('create', 'bar')
out = env('list')
assert 'foo' in out
assert 'bar' in out
foo = ev.read('foo')
with foo:
with pytest.raises(spack.main.SpackCommandError):
with capfd.disabled():
env('remove', '-y', 'foo')
assert 'foo' in env('list')
env('remove', '-y', 'foo')
out = env('list')
assert 'foo' not in out
assert 'bar' in out
env('remove', '-y', 'bar')
out = env('list')
assert 'foo' not in out
assert 'bar' not in out
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_concretize():
e = ev.create('test')
e.add('mpileaks')
e.concretize()
env_specs = e._get_environment_specs()
assert any(x.name == 'mpileaks' for x in env_specs)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="InstallError:Wrong cmake was in environment")
def test_env_uninstalled_specs(install_mockery, mock_fetch):
e = ev.create('test')
e.add('cmake-client')
e.concretize()
assert any(s.name == 'cmake-client' for s in e.uninstalled_specs())
e.install_all()
assert not any(s.name == 'cmake-client' for s in e.uninstalled_specs())
e.add('mpileaks')
e.concretize()
assert not any(s.name == 'cmake-client' for s in e.uninstalled_specs())
assert any(s.name == 'mpileaks' for s in e.uninstalled_specs())
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='InstallError: Wrong cmake was in environment')
def test_env_install_all(install_mockery, mock_fetch):
e = ev.create('test')
e.add('cmake-client')
e.concretize()
e.install_all()
env_specs = e._get_environment_specs()
spec = next(x for x in env_specs if x.name == 'cmake-client')
assert spec.package.installed
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='InstallError: Wrong cmake was in environment')
def test_env_install_single_spec(install_mockery, mock_fetch):
env('create', 'test')
install = SpackCommand('install')
e = ev.read('test')
with e:
install('cmake-client')
e = ev.read('test')
assert e.user_specs[0].name == 'cmake-client'
assert e.concretized_user_specs[0].name == 'cmake-client'
assert e.specs_by_hash[e.concretized_order[0]].name == 'cmake-client'
@pytest.mark.skipif(sys.platform == 'win32', reason="Error on Win")
def test_env_roots_marked_explicit(install_mockery, mock_fetch):
install = SpackCommand('install')
install('dependent-install')
# Check one explicit, one implicit install
dependent = spack.store.db.query(explicit=True)
dependency = spack.store.db.query(explicit=False)
assert len(dependent) == 1
assert len(dependency) == 1
env('create', 'test')
with ev.read('test') as e:
# make implicit install a root of the env
e.add(dependency[0].name)
e.concretize()
e.install_all()
explicit = spack.store.db.query(explicit=True)
assert len(explicit) == 2
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='InstallError: Wrong cmake was in environment')
def test_env_modifications_error_on_activate(
install_mockery, mock_fetch, monkeypatch, capfd):
env('create', 'test')
install = SpackCommand('install')
e = ev.read('test')
with e:
install('cmake-client')
def setup_error(pkg, env):
raise RuntimeError("cmake-client had issues!")
pkg = spack.repo.path.get_pkg_class("cmake-client")
monkeypatch.setattr(pkg, "setup_run_environment", setup_error)
spack.environment.shell.activate(e)
_, err = capfd.readouterr()
assert "cmake-client had issues!" in err
assert "Warning: couldn't get environment settings" in err
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Error: filename or extension is too long')
def test_activate_adds_transitive_run_deps_to_path(
install_mockery, mock_fetch, monkeypatch):
env('create', 'test')
install = SpackCommand('install')
e = ev.read('test')
with e:
install('depends-on-run-env')
env_variables = {}
spack.environment.shell.activate(e).apply_modifications(env_variables)
assert env_variables['DEPENDENCY_ENV_VAR'] == '1'
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='InstallError: Wrong cmake was in environment:')
def test_env_install_same_spec_twice(install_mockery, mock_fetch):
env('create', 'test')
e = ev.read('test')
with e:
# The first installation outputs the package prefix, updates the view
out = install('cmake-client')
assert 'Updating view at' in out
# The second installation reports all packages already installed
out = install('cmake-client')
assert 'already installed' in out
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_env_definition_symlink(install_mockery, mock_fetch, tmpdir):
filepath = str(tmpdir.join('spack.yaml'))
filepath_mid = str(tmpdir.join('spack_mid.yaml'))
env('create', 'test')
e = ev.read('test')
e.add('mpileaks')
os.rename(e.manifest_path, filepath)
os.symlink(filepath, filepath_mid)
os.symlink(filepath_mid, e.manifest_path)
e.concretize()
e.write()
assert os.path.islink(e.manifest_path)
assert os.path.islink(filepath_mid)
@pytest.mark.skipif(sys.platform == "win32",
reason='Logging error')
def test_env_install_two_specs_same_dep(
install_mockery, mock_fetch, tmpdir, capsys):
"""Test installation of two packages that share a dependency with no
connection and the second specifying the dependency as a 'build'
dependency.
"""
path = tmpdir.join('spack.yaml')
with tmpdir.as_cwd():
with open(str(path), 'w') as f:
f.write("""\
env:
specs:
- a
- depb
""")
env('create', 'test', 'spack.yaml')
with ev.read('test'):
with capsys.disabled():
out = install()
# Ensure both packages reach install phase processing and are installed
out = str(out)
assert 'depb: Executing phase:' in out
assert 'a: Executing phase:' in out
depb = spack.store.db.query_one('depb', installed=True)
assert depb, 'Expected depb to be installed'
a = spack.store.db.query_one('a', installed=True)
assert a, 'Expected a to be installed'
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_remove_after_concretize():
e = ev.create('test')
e.add('mpileaks')
e.concretize()
e.add('python')
e.concretize()
e.remove('mpileaks')
assert Spec('mpileaks') not in e.user_specs
env_specs = e._get_environment_specs()
assert any(s.name == 'mpileaks' for s in env_specs)
e.add('mpileaks')
assert any(s.name == 'mpileaks' for s in e.user_specs)
e.remove('mpileaks', force=True)
assert Spec('mpileaks') not in e.user_specs
env_specs = e._get_environment_specs()
assert not any(s.name == 'mpileaks' for s in env_specs)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_remove_command():
env('create', 'test')
assert 'test' in env('list')
with ev.read('test'):
add('mpileaks')
assert 'mpileaks' in find()
assert 'mpileaks@' not in find()
assert 'mpileaks@' not in find('--show-concretized')
with ev.read('test'):
remove('mpileaks')
assert 'mpileaks' not in find()
assert 'mpileaks@' not in find()
assert 'mpileaks@' not in find('--show-concretized')
with ev.read('test'):
add('mpileaks')
assert 'mpileaks' in find()
assert 'mpileaks@' not in find()
assert 'mpileaks@' not in find('--show-concretized')
with ev.read('test'):
concretize()
assert 'mpileaks' in find()
assert 'mpileaks@' not in find()
assert 'mpileaks@' in find('--show-concretized')
with ev.read('test'):
remove('mpileaks')
assert 'mpileaks' not in find()
# removed but still in last concretized specs
assert 'mpileaks@' in find('--show-concretized')
with ev.read('test'):
concretize()
assert 'mpileaks' not in find()
assert 'mpileaks@' not in find()
# now the lockfile is regenerated and it's gone.
assert 'mpileaks@' not in find('--show-concretized')
def test_environment_status(capsys, tmpdir):
with tmpdir.as_cwd():
with capsys.disabled():
assert 'No active environment' in env('status')
with ev.create('test'):
with capsys.disabled():
assert 'In environment test' in env('status')
with ev.Environment('local_dir'):
with capsys.disabled():
assert os.path.join(os.getcwd(), 'local_dir') in env('status')
e = ev.Environment('myproject')
e.write()
with tmpdir.join('myproject').as_cwd():
with e:
with capsys.disabled():
assert 'in current directory' in env('status')
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows - uses pkgconf')
def test_env_status_broken_view(
mutable_mock_env_path, mock_archive, mock_fetch, mock_packages,
install_mockery, tmpdir
):
env_dir = str(tmpdir)
with ev.Environment(env_dir):
install('trivial-install-test-package')
# switch to a new repo that doesn't include the installed package
# test that Spack detects the missing package and warns the user
with spack.repo.use_repositories(MockPackageMultiRepo()):
with ev.Environment(env_dir):
output = env('status')
assert 'includes out of date packages or repos' in output
# Test that the warning goes away when it's fixed
with ev.Environment(env_dir):
output = env('status')
assert 'includes out of date packages or repos' not in output
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows - uses pkgconf')
def test_env_activate_broken_view(
mutable_mock_env_path, mock_archive, mock_fetch, mock_packages,
install_mockery
):
with ev.create('test'):
install('trivial-install-test-package')
# switch to a new repo that doesn't include the installed package
# test that Spack detects the missing package and fails gracefully
new_repo = MockPackageMultiRepo()
with spack.repo.use_repositories(new_repo):
with pytest.raises(SpackCommandError):
env('activate', '--sh', 'test')
# test replacing repo fixes it
env('activate', '--sh', 'test')
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_to_lockfile_dict():
e = ev.create('test')
e.add('mpileaks')
e.concretize()
context_dict = e._to_lockfile_dict()
e_copy = ev.create('test_copy')
e_copy._read_lockfile_dict(context_dict)
assert e.specs_by_hash == e_copy.specs_by_hash
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_env_repo():
e = ev.create('test')
e.add('mpileaks')
e.write()
with ev.read('test'):
concretize()
package = e.repo.get('mpileaks')
assert package.name == 'mpileaks'
assert package.namespace == 'builtin.mock'
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_user_removed_spec():
"""Ensure a user can remove from any position in the spack.yaml file."""
initial_yaml = StringIO("""\
env:
specs:
- mpileaks
- hypre
- libelf
""")
before = ev.create('test', initial_yaml)
before.concretize()
before.write()
# user modifies yaml externally to spack and removes hypre
with open(before.manifest_path, 'w') as f:
f.write("""\
env:
specs:
- mpileaks
- libelf
""")
after = ev.read('test')
after.concretize()
after.write()
env_specs = after._get_environment_specs()
read = ev.read('test')
env_specs = read._get_environment_specs()
assert not any(x.name == 'hypre' for x in env_specs)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_init_from_lockfile(tmpdir):
"""Test that an environment can be instantiated from a lockfile."""
initial_yaml = StringIO("""\
env:
specs:
- mpileaks
- hypre
- libelf
""")
e1 = ev.create('test', initial_yaml)
e1.concretize()
e1.write()
e2 = ev.Environment(str(tmpdir), e1.lock_path)
for s1, s2 in zip(e1.user_specs, e2.user_specs):
assert s1 == s2
for h1, h2 in zip(e1.concretized_order, e2.concretized_order):
assert h1 == h2
assert e1.specs_by_hash[h1] == e2.specs_by_hash[h2]
for s1, s2 in zip(e1.concretized_user_specs, e2.concretized_user_specs):
assert s1 == s2
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_init_from_yaml(tmpdir):
"""Test that an environment can be instantiated from a lockfile."""
initial_yaml = StringIO("""\
env:
specs:
- mpileaks
- hypre
- libelf
""")
e1 = ev.create('test', initial_yaml)
e1.concretize()
e1.write()
e2 = ev.Environment(str(tmpdir), e1.manifest_path)
for s1, s2 in zip(e1.user_specs, e2.user_specs):
assert s1 == s2
assert not e2.concretized_order
assert not e2.concretized_user_specs
assert not e2.specs_by_hash
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows - uses pkgconf')
@pytest.mark.usefixtures('config')
def test_env_view_external_prefix(
tmpdir_factory, mutable_database, mock_packages
):
fake_prefix = tmpdir_factory.mktemp('a-prefix')
fake_bin = fake_prefix.join('bin')
fake_bin.ensure(dir=True)
initial_yaml = StringIO("""\
env:
specs:
- a
view: true
""")
external_config = StringIO("""\
packages:
a:
externals:
- spec: a@2.0
prefix: {a_prefix}
buildable: false
""".format(a_prefix=str(fake_prefix)))
external_config_dict = spack.util.spack_yaml.load_config(external_config)
test_scope = spack.config.InternalConfigScope(
'env-external-test', data=external_config_dict)
with spack.config.override(test_scope):
e = ev.create('test', initial_yaml)
e.concretize()
# Note: normally installing specs in a test environment requires doing
# a fake install, but not for external specs since no actions are
# taken to install them. The installation commands also include
# post-installation functions like DB-registration, so are important
# to do (otherwise the package is not considered installed).
e.install_all()
e.write()
env_mod = spack.util.environment.EnvironmentModifications()
e.add_default_view_to_env(env_mod)
env_variables = {}
env_mod.apply_modifications(env_variables)
assert str(fake_bin) in env_variables['PATH']
def test_init_with_file_and_remove(tmpdir):
"""Ensure a user can remove from any position in the spack.yaml file."""
path = tmpdir.join('spack.yaml')
with tmpdir.as_cwd():
with open(str(path), 'w') as f:
f.write("""\
env:
specs:
- mpileaks
""")
env('create', 'test', 'spack.yaml')
out = env('list')
assert 'test' in out
with ev.read('test'):
assert 'mpileaks' in find()
env('remove', '-y', 'test')
out = env('list')
assert 'test' not in out
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_with_config():
test_config = """\
env:
specs:
- mpileaks
packages:
mpileaks:
version: [2.2]
"""
_env_create('test', StringIO(test_config))
e = ev.read('test')
with e:
e.concretize()
assert any(x.satisfies('mpileaks@2.2')
for x in e._get_environment_specs())
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_with_config_bad_include(capfd):
env_name = 'test_bad_include'
test_config = """\
spack:
include:
- /no/such/directory
- no/such/file.yaml
"""
_env_create(env_name, StringIO(test_config))
e = ev.read(env_name)
with pytest.raises(SystemExit):
with e:
e.concretize()
out, err = capfd.readouterr()
assert 'missing include' in err
assert '/no/such/directory' in err
assert os.path.join('no', 'such', 'file.yaml') in err
assert ev.active_environment() is None
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_with_include_config_files_same_basename():
test_config = """\
env:
include:
- ./path/to/included-config.yaml
- ./second/path/to/include-config.yaml
specs:
[libelf, mpileaks]
"""
_env_create('test', StringIO(test_config))
e = ev.read('test')
fs.mkdirp(os.path.join(e.path, 'path', 'to'))
with open(os.path.join(
e.path,
'./path/to/included-config.yaml'), 'w') as f:
f.write("""\
packages:
libelf:
version: [0.8.10]
""")
fs.mkdirp(os.path.join(e.path, 'second', 'path', 'to'))
with open(os.path.join(
e.path,
'./second/path/to/include-config.yaml'), 'w') as f:
f.write("""\
packages:
mpileaks:
version: [2.2]
""")
with e:
e.concretize()
environment_specs = e._get_environment_specs(False)
assert(environment_specs[0].satisfies('libelf@0.8.10'))
assert(environment_specs[1].satisfies('mpileaks@2.2'))
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_with_included_config_file():
test_config = """\
env:
include:
- ./included-config.yaml
specs:
- mpileaks
"""
_env_create('test', StringIO(test_config))
e = ev.read('test')
with open(os.path.join(e.path, 'included-config.yaml'), 'w') as f:
f.write("""\
packages:
mpileaks:
version: [2.2]
""")
with e:
e.concretize()
assert any(x.satisfies('mpileaks@2.2')
for x in e._get_environment_specs())
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_with_included_config_scope():
config_scope_path = os.path.join(ev.root('test'), 'config')
test_config = """\
env:
include:
- %s
specs:
- mpileaks
""" % config_scope_path
_env_create('test', StringIO(test_config))
e = ev.read('test')
fs.mkdirp(config_scope_path)
with open(os.path.join(config_scope_path, 'packages.yaml'), 'w') as f:
f.write("""\
packages:
mpileaks:
version: [2.2]
""")
with e:
e.concretize()
assert any(x.satisfies('mpileaks@2.2')
for x in e._get_environment_specs())
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_with_included_config_var_path():
config_var_path = os.path.join('$tempdir', 'included-config.yaml')
test_config = """\
env:
include:
- %s
specs:
- mpileaks
""" % config_var_path
_env_create('test', StringIO(test_config))
e = ev.read('test')
config_real_path = substitute_path_variables(config_var_path)
fs.mkdirp(os.path.dirname(config_real_path))
with open(config_real_path, 'w') as f:
f.write("""\
packages:
mpileaks:
version: [2.2]
""")
with e:
e.concretize()
assert any(x.satisfies('mpileaks@2.2')
for x in e._get_environment_specs())
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_config_precedence():
test_config = """\
env:
packages:
libelf:
version: [0.8.12]
include:
- ./included-config.yaml
specs:
- mpileaks
"""
_env_create('test', StringIO(test_config))
e = ev.read('test')
with open(os.path.join(e.path, 'included-config.yaml'), 'w') as f:
f.write("""\
packages:
mpileaks:
version: [2.2]
libelf:
version: [0.8.11]
""")
with e:
e.concretize()
# ensure included scope took effect
assert any(
x.satisfies('mpileaks@2.2') for x in e._get_environment_specs())
# ensure env file takes precedence
assert any(
x.satisfies('libelf@0.8.12') for x in e._get_environment_specs())
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_included_config_precedence():
test_config = """\
env:
include:
- ./high-config.yaml # this one should take precedence
- ./low-config.yaml
specs:
- mpileaks
"""
_env_create('test', StringIO(test_config))
e = ev.read('test')
with open(os.path.join(e.path, 'high-config.yaml'), 'w') as f:
f.write("""\
packages:
libelf:
version: [0.8.10] # this should override libelf version below
""")
with open(os.path.join(e.path, 'low-config.yaml'), 'w') as f:
f.write("""\
packages:
mpileaks:
version: [2.2]
libelf:
version: [0.8.12]
""")
with e:
e.concretize()
assert any(
x.satisfies('mpileaks@2.2') for x in e._get_environment_specs())
assert any(
[x.satisfies('libelf@0.8.10') for x in e._get_environment_specs()])
def test_bad_env_yaml_format(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
spacks:
- mpileaks
""")
with tmpdir.as_cwd():
with pytest.raises(spack.config.ConfigFormatError) as e:
env('create', 'test', './spack.yaml')
assert './spack.yaml:2' in str(e)
assert "'spacks' was unexpected" in str(e)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_loads(install_mockery, mock_fetch):
env('create', 'test')
with ev.read('test'):
add('mpileaks')
concretize()
install('--fake')
with ev.read('test'):
env('loads')
e = ev.read('test')
loads_file = os.path.join(e.path, 'loads')
assert os.path.exists(loads_file)
with open(loads_file) as f:
contents = f.read()
assert 'module load mpileaks' in contents
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
@pytest.mark.disable_clean_stage_check
def test_stage(mock_stage, mock_fetch, install_mockery):
env('create', 'test')
with ev.read('test'):
add('mpileaks')
add('zmpi')
concretize()
stage()
root = str(mock_stage)
def check_stage(spec):
spec = Spec(spec).concretized()
for dep in spec.traverse():
stage_name = "{0}{1}-{2}-{3}".format(stage_prefix, dep.name,
dep.version, dep.dag_hash())
assert os.path.isdir(os.path.join(root, stage_name))
check_stage('mpileaks')
check_stage('zmpi')
def test_env_commands_die_with_no_env_arg():
# these fail in argparse when given no arg
with pytest.raises(SystemExit):
env('create')
with pytest.raises(SystemExit):
env('remove')
# these have an optional env arg and raise errors via tty.die
with pytest.raises(spack.main.SpackCommandError):
env('loads')
# This should NOT raise an error with no environment
# it just tells the user there isn't an environment
env('status')
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_blocks_uninstall(mock_stage, mock_fetch, install_mockery):
env('create', 'test')
with ev.read('test'):
add('mpileaks')
install('--fake')
out = uninstall('mpileaks', fail_on_error=False)
assert uninstall.returncode == 1
assert 'used by the following environments' in out
def test_roots_display_with_variants():
env('create', 'test')
with ev.read('test'):
add('boost+shared')
with ev.read('test'):
out = find(output=str)
assert "boost +shared" in out
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_uninstall_removes_from_env(mock_stage, mock_fetch, install_mockery):
env('create', 'test')
with ev.read('test'):
add('mpileaks')
add('libelf')
install('--fake')
test = ev.read('test')
assert any(s.name == 'mpileaks' for s in test.specs_by_hash.values())
assert any(s.name == 'libelf' for s in test.specs_by_hash.values())
with ev.read('test'):
uninstall('-ya')
test = ev.read('test')
assert not test.specs_by_hash
assert not test.concretized_order
assert not test.user_specs
def create_v1_lockfile_dict(roots, all_specs):
test_lockfile_dict = {
"_meta": {
"lockfile-version": 1,
"file-type": "spack-lockfile"
},
"roots": list(
{
"hash": s.dag_hash(),
"spec": s.name
} for s in roots
),
# Version one lockfiles use the dag hash without build deps as keys,
# but they write out the full node dict (including build deps)
"concrete_specs": dict(
(s.dag_hash(), s.to_node_dict(hash=ht.build_hash))
for s in all_specs
)
}
return test_lockfile_dict
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows - uses pkgconf")
@pytest.mark.usefixtures('config')
def test_read_old_lock_and_write_new(tmpdir):
build_only = ('build',)
mock_repo = MockPackageMultiRepo()
y = mock_repo.add_package('y', [], [])
mock_repo.add_package('x', [y], [build_only])
with spack.repo.use_repositories(mock_repo):
x = Spec('x')
x.concretize()
y = x['y']
test_lockfile_dict = create_v1_lockfile_dict([x], [x, y])
test_lockfile_path = str(tmpdir.join('test.lock'))
with open(test_lockfile_path, 'w') as f:
sjson.dump(test_lockfile_dict, stream=f)
_env_create('test', test_lockfile_path, with_view=False)
e = ev.read('test')
hashes = set(e._to_lockfile_dict()['concrete_specs'])
# When the lockfile is rewritten, it should adopt the new hash scheme
# which accounts for all dependencies, including build dependencies
assert hashes == set([
x.build_hash(),
y.build_hash()])
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows - uses pkgconf")
@pytest.mark.usefixtures('config')
def test_read_old_lock_creates_backup(tmpdir):
"""When reading a version-1 lockfile, make sure that a backup of that file
is created.
"""
mock_repo = MockPackageMultiRepo()
y = mock_repo.add_package('y', [], [])
with spack.repo.use_repositories(mock_repo):
y = Spec('y')
y.concretize()
test_lockfile_dict = create_v1_lockfile_dict([y], [y])
env_root = tmpdir.mkdir('test-root')
test_lockfile_path = str(env_root.join(ev.lockfile_name))
with open(test_lockfile_path, 'w') as f:
sjson.dump(test_lockfile_dict, stream=f)
e = ev.Environment(str(env_root))
assert os.path.exists(e._lock_backup_v1_path)
with open(e._lock_backup_v1_path, 'r') as backup_v1_file:
lockfile_dict_v1 = sjson.load(backup_v1_file)
# Make sure that the backup file follows the v1 hash scheme
assert y.dag_hash() in lockfile_dict_v1['concrete_specs']
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows - uses pkgconf")
@pytest.mark.usefixtures('config')
def test_indirect_build_dep():
"""Simple case of X->Y->Z where Y is a build/link dep and Z is a
build-only dep. Make sure this concrete DAG is preserved when writing the
environment out and reading it back.
"""
default = ('build', 'link')
build_only = ('build',)
mock_repo = MockPackageMultiRepo()
z = mock_repo.add_package('z', [], [])
y = mock_repo.add_package('y', [z], [build_only])
mock_repo.add_package('x', [y], [default])
def noop(*args):
pass
setattr(mock_repo, 'dump_provenance', noop)
with spack.repo.use_repositories(mock_repo):
x_spec = Spec('x')
x_concretized = x_spec.concretized()
_env_create('test', with_view=False)
e = ev.read('test')
e.add(x_spec)
e.concretize()
e.write()
e_read = ev.read('test')
x_env_hash, = e_read.concretized_order
x_env_spec = e_read.specs_by_hash[x_env_hash]
assert x_env_spec == x_concretized
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows - uses pkgconf")
@pytest.mark.usefixtures('config')
def test_store_different_build_deps():
r"""Ensure that an environment can store two instances of a build-only
dependency::
x y
/| (l) | (b)
(b) | y z2
\| (b)
z1
"""
default = ('build', 'link')
build_only = ('build',)
mock_repo = MockPackageMultiRepo()
z = mock_repo.add_package('z', [], [])
y = mock_repo.add_package('y', [z], [build_only])
mock_repo.add_package('x', [y, z], [default, build_only])
def noop(*args):
pass
setattr(mock_repo, 'dump_provenance', noop)
with spack.repo.use_repositories(mock_repo):
y_spec = Spec('y ^z@3')
y_concretized = y_spec.concretized()
x_spec = Spec('x ^z@2')
x_concretized = x_spec.concretized()
# Even though x chose a different 'z', it should choose the same y
# according to the DAG hash (since build deps are excluded from
# comparison by default). Although the dag hashes are equal, the specs
# are not considered equal because they compare build deps.
assert x_concretized['y'].dag_hash() == y_concretized.dag_hash()
_env_create('test', with_view=False)
e = ev.read('test')
e.add(y_spec)
e.add(x_spec)
e.concretize()
e.write()
e_read = ev.read('test')
y_env_hash, x_env_hash = e_read.concretized_order
y_read = e_read.specs_by_hash[y_env_hash]
x_read = e_read.specs_by_hash[x_env_hash]
assert x_read['z'] != y_read['z']
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Fails on windows')
def test_env_updates_view_install(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
with ev.read('test'):
add('mpileaks')
install('--fake')
check_mpileaks_and_deps_in_view(view_dir)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Fails on windows')
def test_env_view_fails(
tmpdir, mock_packages, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
with ev.read('test'):
add('libelf')
add('libelf cflags=-g')
with pytest.raises(llnl.util.link_tree.MergeConflictError,
match='merge blocked by file'):
install('--fake')
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_without_view_install(
tmpdir, mock_stage, mock_fetch, install_mockery):
# Test enabling a view after installing specs
env('create', '--without-view', 'test')
test_env = ev.read('test')
with pytest.raises(ev.SpackEnvironmentError):
test_env.default_view
view_dir = tmpdir.join('view')
with ev.read('test'):
add('mpileaks')
install('--fake')
env('view', 'enable', str(view_dir))
# After enabling the view, the specs should be linked into the environment
# view dir
check_mpileaks_and_deps_in_view(view_dir)
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_env_config_view_default(
tmpdir, mock_stage, mock_fetch, install_mockery):
# This config doesn't mention whether a view is enabled
test_config = """\
env:
specs:
- mpileaks
"""
_env_create('test', StringIO(test_config))
with ev.read('test'):
install('--fake')
e = ev.read('test')
# Try retrieving the view object
view = e.default_view.view()
assert view.get_spec('mpileaks')
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_updates_view_install_package(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
with ev.read('test'):
install('--fake', 'mpileaks')
assert os.path.exists(str(view_dir.join('.spack/mpileaks')))
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_updates_view_add_concretize(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
install('--fake', 'mpileaks')
with ev.read('test'):
add('mpileaks')
concretize()
check_mpileaks_and_deps_in_view(view_dir)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_updates_view_uninstall(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
with ev.read('test'):
install('--fake', 'mpileaks')
check_mpileaks_and_deps_in_view(view_dir)
with ev.read('test'):
uninstall('-ay')
check_viewdir_removal(view_dir)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_updates_view_uninstall_referenced_elsewhere(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
install('--fake', 'mpileaks')
with ev.read('test'):
add('mpileaks')
concretize()
check_mpileaks_and_deps_in_view(view_dir)
with ev.read('test'):
uninstall('-ay')
check_viewdir_removal(view_dir)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_updates_view_remove_concretize(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
install('--fake', 'mpileaks')
with ev.read('test'):
add('mpileaks')
concretize()
check_mpileaks_and_deps_in_view(view_dir)
with ev.read('test'):
remove('mpileaks')
concretize()
check_viewdir_removal(view_dir)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_updates_view_force_remove(
tmpdir, mock_stage, mock_fetch, install_mockery):
view_dir = tmpdir.join('view')
env('create', '--with-view=%s' % view_dir, 'test')
with ev.read('test'):
install('--fake', 'mpileaks')
check_mpileaks_and_deps_in_view(view_dir)
with ev.read('test'):
remove('-f', 'mpileaks')
check_viewdir_removal(view_dir)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason='Not supported on Windows (yet)')
def test_env_activate_view_fails(
tmpdir, mock_stage, mock_fetch, install_mockery):
"""Sanity check on env activate to make sure it requires shell support"""
out = env('activate', 'test')
assert "To set up shell support" in out
def test_stack_yaml_definitions(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') in test.user_specs
def test_stack_yaml_definitions_as_constraints(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- mpis: [mpich, openmpi]
specs:
- matrix:
- [$packages]
- [$^mpis]
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('mpileaks^mpich') in test.user_specs
assert Spec('callpath^mpich') in test.user_specs
assert Spec('mpileaks^openmpi') in test.user_specs
assert Spec('callpath^openmpi') in test.user_specs
def test_stack_yaml_definitions_as_constraints_on_matrix(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- mpis:
- matrix:
- [mpich]
- ['@3.0.4', '@3.0.3']
specs:
- matrix:
- [$packages]
- [$^mpis]
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('mpileaks^mpich@3.0.4') in test.user_specs
assert Spec('callpath^mpich@3.0.4') in test.user_specs
assert Spec('mpileaks^mpich@3.0.3') in test.user_specs
assert Spec('callpath^mpich@3.0.3') in test.user_specs
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
@pytest.mark.regression('12095')
def test_stack_yaml_definitions_write_reference(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- indirect: [$packages]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
concretize()
test = ev.read('test')
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') in test.user_specs
def test_stack_yaml_add_to_list(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
add('-l', 'packages', 'libelf')
test = ev.read('test')
assert Spec('libelf') in test.user_specs
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') in test.user_specs
def test_stack_yaml_remove_from_list(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
remove('-l', 'packages', 'mpileaks')
test = ev.read('test')
assert Spec('mpileaks') not in test.user_specs
assert Spec('callpath') in test.user_specs
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_stack_yaml_remove_from_list_force(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
specs:
- matrix:
- [$packages]
- [^mpich, ^zmpi]
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
concretize()
remove('-f', '-l', 'packages', 'mpileaks')
find_output = find('-c')
assert 'mpileaks' not in find_output
test = ev.read('test')
assert len(test.user_specs) == 2
assert Spec('callpath ^zmpi') in test.user_specs
assert Spec('callpath ^mpich') in test.user_specs
def test_stack_yaml_remove_from_matrix_no_effect(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages:
- matrix:
- [mpileaks, callpath]
- [target=be]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test') as e:
before = e.user_specs.specs
remove('-l', 'packages', 'mpileaks')
after = e.user_specs.specs
assert before == after
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_stack_yaml_force_remove_from_matrix(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages:
- matrix:
- [mpileaks, callpath]
- [target=be]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test') as e:
concretize()
before_user = e.user_specs.specs
before_conc = e.concretized_user_specs
remove('-f', '-l', 'packages', 'mpileaks')
after_user = e.user_specs.specs
after_conc = e.concretized_user_specs
assert before_user == after_user
mpileaks_spec = Spec('mpileaks target=be')
assert mpileaks_spec in before_conc
assert mpileaks_spec not in after_conc
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_stack_concretize_extraneous_deps(tmpdir, config, mock_packages):
# FIXME: The new concretizer doesn't handle yet soft
# FIXME: constraints for stacks
if spack.config.get('config:concretizer') == 'clingo':
pytest.skip('Clingo concretizer does not support soft constraints')
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- install:
- matrix:
- [$packages]
- ['^zmpi', '^mpich']
specs:
- $install
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
concretize()
test = ev.read('test')
for user, concrete in test.concretized_specs():
assert concrete.concrete
assert not user.concrete
if user.name == 'libelf':
assert not concrete.satisfies('^mpi', strict=True)
elif user.name == 'mpileaks':
assert concrete.satisfies('^mpi', strict=True)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_stack_concretize_extraneous_variants(tmpdir, config, mock_packages):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- install:
- matrix:
- [$packages]
- ['~shared', '+shared']
specs:
- $install
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
concretize()
test = ev.read('test')
for user, concrete in test.concretized_specs():
assert concrete.concrete
assert not user.concrete
if user.name == 'libelf':
assert 'shared' not in concrete.variants
if user.name == 'mpileaks':
assert (concrete.variants['shared'].value ==
user.variants['shared'].value)
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_stack_concretize_extraneous_variants_with_dash(tmpdir, config,
mock_packages):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- install:
- matrix:
- [$packages]
- ['shared=False', '+shared-libs']
specs:
- $install
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
concretize()
ev.read('test')
# Regression test for handling of variants with dashes in them
# will fail before this point if code regresses
assert True
def test_stack_definition_extension(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('libelf') in test.user_specs
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') in test.user_specs
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_definition_conditional_false(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
when: 'False'
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('libelf') in test.user_specs
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') not in test.user_specs
def test_stack_definition_conditional_true(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
when: 'True'
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('libelf') in test.user_specs
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') in test.user_specs
def test_stack_definition_conditional_with_variable(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
when: platform == 'test'
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('libelf') in test.user_specs
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') in test.user_specs
def test_stack_definition_conditional_with_satisfaction(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
when: arch.satisfies('platform=foo') # will be "test" when testing
- packages: [callpath]
when: arch.satisfies('platform=test')
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('libelf') not in test.user_specs
assert Spec('mpileaks') not in test.user_specs
assert Spec('callpath') in test.user_specs
def test_stack_definition_complex_conditional(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
when: re.search(r'foo', hostname) and env['test'] == 'THISSHOULDBEFALSE'
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
test = ev.read('test')
assert Spec('libelf') in test.user_specs
assert Spec('mpileaks') in test.user_specs
assert Spec('callpath') not in test.user_specs
def test_stack_definition_conditional_invalid_variable(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
when: bad_variable == 'test'
specs:
- $packages
""")
with tmpdir.as_cwd():
with pytest.raises(NameError):
env('create', 'test', './spack.yaml')
def test_stack_definition_conditional_add_write(tmpdir):
filename = str(tmpdir.join('spack.yaml'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [libelf, mpileaks]
- packages: [callpath]
when: platform == 'test'
specs:
- $packages
""")
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
add('-l', 'packages', 'zmpi')
test = ev.read('test')
packages_lists = list(filter(lambda x: 'packages' in x,
test.yaml['env']['definitions']))
assert len(packages_lists) == 2
assert 'callpath' not in packages_lists[0]['packages']
assert 'callpath' in packages_lists[1]['packages']
assert 'zmpi' in packages_lists[0]['packages']
assert 'zmpi' not in packages_lists[1]['packages']
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_combinatorial_view(tmpdir, mock_fetch, mock_packages,
mock_archive, install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
combinatorial:
root: %s
projections:
'all': '{name}/{version}-{compiler.name}'""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test._get_environment_specs():
assert os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_view_select(tmpdir, mock_fetch, mock_packages,
mock_archive, install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
combinatorial:
root: %s
select: ['%%gcc']
projections:
'all': '{name}/{version}-{compiler.name}'""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test._get_environment_specs():
if spec.satisfies('%gcc'):
assert os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
else:
assert not os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_view_exclude(tmpdir, mock_fetch, mock_packages,
mock_archive, install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
combinatorial:
root: %s
exclude: [callpath]
projections:
'all': '{name}/{version}-{compiler.name}'""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test._get_environment_specs():
if not spec.satisfies('callpath'):
assert os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
else:
assert not os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_view_select_and_exclude(tmpdir, mock_fetch, mock_packages,
mock_archive, install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
combinatorial:
root: %s
select: ['%%gcc']
exclude: [callpath]
projections:
'all': '{name}/{version}-{compiler.name}'""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test._get_environment_specs():
if spec.satisfies('%gcc') and not spec.satisfies('callpath'):
assert os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
else:
assert not os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_view_link_roots(tmpdir, mock_fetch, mock_packages, mock_archive,
install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
combinatorial:
root: %s
select: ['%%gcc']
exclude: [callpath]
link: 'roots'
projections:
'all': '{name}/{version}-{compiler.name}'""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test._get_environment_specs():
if spec in test.roots() and (spec.satisfies('%gcc') and
not spec.satisfies('callpath')):
assert os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
else:
assert not os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_view_link_run(tmpdir, mock_fetch, mock_packages, mock_archive,
install_mockery):
yaml = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
envdir = str(tmpdir)
with open(yaml, 'w') as f:
f.write("""
spack:
specs:
- dttop
view:
combinatorial:
root: %s
link: run
projections:
all: '{name}'""" % viewdir)
with ev.Environment(envdir):
install()
# make sure transitive run type deps are in the view
for pkg in ('dtrun1', 'dtrun3'):
assert os.path.exists(os.path.join(viewdir, pkg))
# and non-run-type deps are not.
for pkg in ('dtlink1', 'dtlink2', 'dtlink3', 'dtlink4', 'dtlink5'
'dtbuild1', 'dtbuild2', 'dtbuild3'):
assert not os.path.exists(os.path.join(viewdir, pkg))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
@pytest.mark.parametrize('link_type', ['hardlink', 'copy', 'symlink'])
def test_view_link_type(link_type, tmpdir, mock_fetch, mock_packages, mock_archive,
install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
specs:
- mpileaks
view:
default:
root: %s
link_type: %s""" % (viewdir, link_type))
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test.roots():
file_path = test.default_view.view()._root
file_to_test = os.path.join(
file_path, spec.name)
assert os.path.isfile(file_to_test)
assert os.path.islink(file_to_test) == (link_type == 'symlink')
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_view_link_all(tmpdir, mock_fetch, mock_packages, mock_archive,
install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, callpath]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
combinatorial:
root: %s
select: ['%%gcc']
exclude: [callpath]
link: 'all'
projections:
'all': '{name}/{version}-{compiler.name}'""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
test = ev.read('test')
for spec in test._get_environment_specs():
if spec.satisfies('%gcc') and not spec.satisfies('callpath'):
assert os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
else:
assert not os.path.exists(
os.path.join(viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_view_activate_from_default(tmpdir, mock_fetch, mock_packages,
mock_archive, install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, cmake]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
default:
root: %s
select: ['%%gcc']""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
shell = env('activate', '--sh', 'test')
assert 'PATH' in shell
assert os.path.join(viewdir, 'bin') in shell
assert 'FOOBAR=mpileaks' in shell
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_view_no_activate_without_default(tmpdir, mock_fetch,
mock_packages, mock_archive,
install_mockery):
filename = str(tmpdir.join('spack.yaml'))
viewdir = str(tmpdir.join('view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, cmake]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
not-default:
root: %s
select: ['%%gcc']""" % viewdir)
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
shell = env('activate', '--sh', 'test')
assert 'PATH' not in shell
assert viewdir not in shell
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_stack_view_multiple_views(tmpdir, mock_fetch, mock_packages,
mock_archive, install_mockery):
filename = str(tmpdir.join('spack.yaml'))
default_viewdir = str(tmpdir.join('default-view'))
combin_viewdir = str(tmpdir.join('combinatorial-view'))
with open(filename, 'w') as f:
f.write("""\
env:
definitions:
- packages: [mpileaks, cmake]
- compilers: ['%%gcc', '%%clang']
specs:
- matrix:
- [$packages]
- [$compilers]
view:
default:
root: %s
select: ['%%gcc']
combinatorial:
root: %s
exclude: [callpath %%gcc]
projections:
'all': '{name}/{version}-{compiler.name}'""" % (default_viewdir,
combin_viewdir))
with tmpdir.as_cwd():
env('create', 'test', './spack.yaml')
with ev.read('test'):
install()
shell = env('activate', '--sh', 'test')
assert 'PATH' in shell
assert os.path.join(default_viewdir, 'bin') in shell
test = ev.read('test')
for spec in test._get_environment_specs():
if not spec.satisfies('callpath%gcc'):
assert os.path.exists(
os.path.join(combin_viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
else:
assert not os.path.exists(
os.path.join(combin_viewdir, spec.name, '%s-%s' %
(spec.version, spec.compiler.name)))
def test_env_activate_sh_prints_shell_output(
tmpdir, mock_stage, mock_fetch, install_mockery
):
"""Check the shell commands output by ``spack env activate --sh``.
This is a cursory check; ``share/spack/qa/setup-env-test.sh`` checks
for correctness.
"""
env('create', 'test', add_view=True)
out = env('activate', '--sh', 'test')
assert "export SPACK_ENV=" in out
assert "export PS1=" not in out
assert "alias despacktivate=" in out
out = env('activate', '--sh', '--prompt', 'test')
assert "export SPACK_ENV=" in out
assert "export PS1=" in out
assert "alias despacktivate=" in out
def test_env_activate_csh_prints_shell_output(
tmpdir, mock_stage, mock_fetch, install_mockery
):
"""Check the shell commands output by ``spack env activate --csh``."""
env('create', 'test', add_view=True)
out = env('activate', '--csh', 'test')
assert "setenv SPACK_ENV" in out
assert "setenv set prompt" not in out
assert "alias despacktivate" in out
out = env('activate', '--csh', '--prompt', 'test')
assert "setenv SPACK_ENV" in out
assert "set prompt=" in out
assert "alias despacktivate" in out
@pytest.mark.regression('12719')
def test_env_activate_default_view_root_unconditional(mutable_mock_env_path):
"""Check that the root of the default view in the environment is added
to the shell unconditionally."""
env('create', 'test', add_view=True)
with ev.read('test') as e:
viewdir = e.default_view.root
out = env('activate', '--sh', 'test')
viewdir_bin = os.path.join(viewdir, 'bin')
assert "export PATH={0}".format(viewdir_bin) in out or \
"export PATH='{0}".format(viewdir_bin) in out or \
'export PATH="{0}'.format(viewdir_bin) in out
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_concretize_user_specs_together():
e = ev.create('coconcretization')
e.concretization = 'together'
# Concretize a first time using 'mpich' as the MPI provider
e.add('mpileaks')
e.add('mpich')
e.concretize()
assert all('mpich' in spec for _, spec in e.concretized_specs())
assert all('mpich2' not in spec for _, spec in e.concretized_specs())
# Concretize a second time using 'mpich2' as the MPI provider
e.remove('mpich')
e.add('mpich2')
e.concretize()
assert all('mpich2' in spec for _, spec in e.concretized_specs())
assert all('mpich' not in spec for _, spec in e.concretized_specs())
# Concretize again without changing anything, check everything
# stays the same
e.concretize()
assert all('mpich2' in spec for _, spec in e.concretized_specs())
assert all('mpich' not in spec for _, spec in e.concretized_specs())
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_cant_install_single_spec_when_concretizing_together():
e = ev.create('coconcretization')
e.concretization = 'together'
with pytest.raises(ev.SpackEnvironmentError, match=r'cannot install'):
e.concretize_and_add('zlib')
e.install_all()
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_duplicate_packages_raise_when_concretizing_together():
e = ev.create('coconcretization')
e.concretization = 'together'
e.add('mpileaks+opt')
e.add('mpileaks~opt')
e.add('mpich')
with pytest.raises(ev.SpackEnvironmentError, match=r'cannot contain more'):
e.concretize()
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
def test_env_write_only_non_default():
env('create', 'test')
e = ev.read('test')
with open(e.manifest_path, 'r') as f:
yaml = f.read()
assert yaml == ev.default_manifest_yaml
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
@pytest.mark.regression('20526')
def test_env_write_only_non_default_nested(tmpdir):
# setup an environment file
# the environment includes configuration because nested configs proved the
# most difficult to avoid writing.
filename = 'spack.yaml'
filepath = str(tmpdir.join(filename))
contents = """\
env:
specs:
- matrix:
- [mpileaks]
packages:
mpileaks:
compiler: [gcc]
view: true
"""
# create environment with some structure
with open(filepath, 'w') as f:
f.write(contents)
env('create', 'test', filepath)
# concretize
with ev.read('test') as e:
concretize()
e.write()
with open(e.manifest_path, 'r') as f:
manifest = f.read()
assert manifest == contents
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
@pytest.fixture
def packages_yaml_v015(tmpdir):
"""Return the path to an existing manifest in the v0.15.x format
and the path to a non yet existing backup file.
"""
raw_yaml = """
spack:
specs:
- mpich
packages:
cmake:
paths:
cmake@3.17.3: /usr
"""
manifest = tmpdir.ensure('spack.yaml')
backup_file = tmpdir.join('spack.yaml.bkp')
manifest.write(raw_yaml)
return manifest, backup_file
def test_update_anonymous_env(packages_yaml_v015):
manifest, backup_file = packages_yaml_v015
env('update', '-y', str(manifest.dirname))
# The environment is now at the latest format
assert ev.is_latest_format(str(manifest))
# A backup file has been created and it's not at the latest format
assert os.path.exists(str(backup_file))
assert not ev.is_latest_format(str(backup_file))
def test_double_update(packages_yaml_v015):
manifest, backup_file = packages_yaml_v015
# Update the environment
env('update', '-y', str(manifest.dirname))
# Try to read the environment (it should not error)
ev.create('test', str(manifest))
# Updating again does nothing since the manifest is up-to-date
env('update', '-y', str(manifest.dirname))
# The environment is at the latest format
assert ev.is_latest_format(str(manifest))
# A backup file has been created and it's not at the latest format
assert os.path.exists(str(backup_file))
assert not ev.is_latest_format(str(backup_file))
def test_update_and_revert(packages_yaml_v015):
manifest, backup_file = packages_yaml_v015
# Update the environment
env('update', '-y', str(manifest.dirname))
assert os.path.exists(str(backup_file))
assert not ev.is_latest_format(str(backup_file))
assert ev.is_latest_format(str(manifest))
# Revert to previous state
env('revert', '-y', str(manifest.dirname))
assert not os.path.exists(str(backup_file))
assert not ev.is_latest_format(str(manifest))
def test_old_format_cant_be_updated_implicitly(packages_yaml_v015):
manifest, backup_file = packages_yaml_v015
env('activate', str(manifest.dirname))
with pytest.raises(spack.main.SpackCommandError):
add('hdf5')
@pytest.mark.regression('18147')
def test_can_update_attributes_with_override(tmpdir):
spack_yaml = """
spack:
mirrors::
test: /foo/bar
packages:
cmake:
paths:
cmake@3.18.1: /usr
specs:
- hdf5
"""
abspath = tmpdir.join('spack.yaml')
abspath.write(spack_yaml)
# Check that an update does not raise
env('update', '-y', str(abspath.dirname))
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
@pytest.mark.regression('18338')
def test_newline_in_commented_sequence_is_not_an_issue(tmpdir):
spack_yaml = """
spack:
specs:
- dyninst
packages:
libelf:
externals:
- spec: libelf@0.8.13
modules:
- libelf/3.18.1
concretization: together
"""
abspath = tmpdir.join('spack.yaml')
abspath.write(spack_yaml)
def extract_build_hash(environment):
_, dyninst = next(iter(environment.specs_by_hash.items()))
return dyninst['libelf'].build_hash()
# Concretize a first time and create a lockfile
with ev.Environment(str(tmpdir)) as e:
concretize()
libelf_first_hash = extract_build_hash(e)
# Check that a second run won't error
with ev.Environment(str(tmpdir)) as e:
concretize()
libelf_second_hash = extract_build_hash(e)
assert libelf_first_hash == libelf_second_hash
@pytest.mark.skipif(str(spack.platforms.host()) == 'windows',
reason="Not supported on Windows (yet)")
@pytest.mark.regression('18441')
def test_lockfile_not_deleted_on_write_error(tmpdir, monkeypatch):
raw_yaml = """
spack:
specs:
- dyninst
packages:
libelf:
externals:
- spec: libelf@0.8.13
prefix: /usr
"""
spack_yaml = tmpdir.join('spack.yaml')
spack_yaml.write(raw_yaml)
spack_lock = tmpdir.join('spack.lock')
# Concretize a first time and create a lockfile
with ev.Environment(str(tmpdir)):
concretize()
assert os.path.exists(str(spack_lock))
# If I run concretize again and there's an error during write,
# the spack.lock file shouldn't disappear from disk
def _write_helper_raise(self, x, y):
raise RuntimeError('some error')
monkeypatch.setattr(
ev.Environment, '_update_and_write_manifest', _write_helper_raise
)
with ev.Environment(str(tmpdir)) as e:
e.concretize(force=True)
with pytest.raises(RuntimeError):
e.clear()
e.write()
assert os.path.exists(str(spack_lock))
def _setup_develop_packages(tmpdir):
"""Sets up a structure ./init_env/spack.yaml, ./build_folder, ./dest_env
where spack.yaml has a relative develop path to build_folder"""
init_env = tmpdir.join('init_env')
build_folder = tmpdir.join('build_folder')
dest_env = tmpdir.join('dest_env')
fs.mkdirp(str(init_env))
fs.mkdirp(str(build_folder))
fs.mkdirp(str(dest_env))
raw_yaml = """
spack:
specs: ['mypkg1', 'mypkg2']
develop:
mypkg1:
path: ../build_folder
spec: mypkg@main
mypkg2:
path: /some/other/path
spec: mypkg@main
"""
spack_yaml = init_env.join('spack.yaml')
spack_yaml.write(raw_yaml)
return init_env, build_folder, dest_env, spack_yaml
def test_rewrite_rel_dev_path_new_dir(tmpdir):
"""Relative develop paths should be rewritten for new environments in
a different directory from the original manifest file"""
_, build_folder, dest_env, spack_yaml = _setup_develop_packages(tmpdir)
env('create', '-d', str(dest_env), str(spack_yaml))
with ev.Environment(str(dest_env)) as e:
assert e.dev_specs['mypkg1']['path'] == str(build_folder)
assert e.dev_specs['mypkg2']['path'] == sep + os.path.join('some',
'other', 'path')
def test_rewrite_rel_dev_path_named_env(tmpdir):
"""Relative develop paths should by default be rewritten for new named
environment"""
_, build_folder, _, spack_yaml = _setup_develop_packages(tmpdir)
env('create', 'named_env', str(spack_yaml))
with ev.read('named_env') as e:
assert e.dev_specs['mypkg1']['path'] == str(build_folder)
assert e.dev_specs['mypkg2']['path'] == sep + os.path.join('some',
'other', 'path')
def test_rewrite_rel_dev_path_original_dir(tmpdir):
"""Relative devevelop paths should not be rewritten when initializing an
environment with root path set to the same directory"""
init_env, _, _, spack_yaml = _setup_develop_packages(tmpdir)
with ev.Environment(str(init_env), str(spack_yaml)) as e:
assert e.dev_specs['mypkg1']['path'] == '../build_folder'
assert e.dev_specs['mypkg2']['path'] == '/some/other/path'
def test_rewrite_rel_dev_path_create_original_dir(tmpdir):
"""Relative develop paths should not be rewritten when creating an
environment in the original directory"""
init_env, _, _, spack_yaml = _setup_develop_packages(tmpdir)
env('create', '-d', str(init_env), str(spack_yaml))
with ev.Environment(str(init_env)) as e:
assert e.dev_specs['mypkg1']['path'] == '../build_folder'
assert e.dev_specs['mypkg2']['path'] == '/some/other/path'
def test_does_not_rewrite_rel_dev_path_when_keep_relative_is_set(tmpdir):
"""Relative develop paths should not be rewritten when --keep-relative is
passed to create"""
_, _, _, spack_yaml = _setup_develop_packages(tmpdir)
env('create', '--keep-relative', 'named_env', str(spack_yaml))
with ev.read('named_env') as e:
print(e.dev_specs)
assert e.dev_specs['mypkg1']['path'] == '../build_folder'
assert e.dev_specs['mypkg2']['path'] == '/some/other/path'
@pytest.mark.regression('23440')
def test_custom_version_concretize_together(tmpdir):
# Custom versions should be permitted in specs when
# concretizing together
e = ev.create('custom_version')
e.concretization = 'together'
# Concretize a first time using 'mpich' as the MPI provider
e.add('hdf5@myversion')
e.add('mpich')
e.concretize()
assert any('hdf5@myversion' in spec for _, spec in e.concretized_specs())
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_modules_relative_to_views(tmpdir, install_mockery, mock_fetch):
spack_yaml = """
spack:
specs:
- trivial-install-test-package
modules:
default:
enable:: [tcl]
use_view: true
roots:
tcl: modules
"""
_env_create('test', StringIO(spack_yaml))
with ev.read('test') as e:
install()
spec = e.specs_by_hash[e.concretized_order[0]]
view_prefix = e.default_view.get_projection_for_spec(spec)
modules_glob = '%s/modules/**/*' % e.path
modules = glob.glob(modules_glob)
assert len(modules) == 1
module = modules[0]
with open(module, 'r') as f:
contents = f.read()
assert view_prefix in contents
assert spec.prefix not in contents
@pytest.mark.skipif(sys.platform == "win32",
reason='Not supported on Windows (yet)')
def test_multiple_modules_post_env_hook(tmpdir, install_mockery, mock_fetch):
spack_yaml = """
spack:
specs:
- trivial-install-test-package
modules:
default:
enable:: [tcl]
use_view: true
roots:
tcl: modules
full:
enable:: [tcl]
roots:
tcl: full_modules
"""
_env_create('test', StringIO(spack_yaml))
with ev.read('test') as e:
install()
spec = e.specs_by_hash[e.concretized_order[0]]
view_prefix = e.default_view.get_projection_for_spec(spec)
modules_glob = '%s/modules/**/*' % e.path
modules = glob.glob(modules_glob)
assert len(modules) == 1
module = modules[0]
full_modules_glob = '%s/full_modules/**/*' % e.path
full_modules = glob.glob(full_modules_glob)
assert len(full_modules) == 1
full_module = full_modules[0]
with open(module, 'r') as f:
contents = f.read()
with open(full_module, 'r') as f:
full_contents = f.read()
assert view_prefix in contents
assert spec.prefix not in contents
assert view_prefix not in full_contents
assert spec.prefix in full_contents
@pytest.mark.regression('24148')
def test_virtual_spec_concretize_together(tmpdir):
# An environment should permit to concretize "mpi"
e = ev.create('virtual_spec')
e.concretization = 'together'
e.add('mpi')
e.concretize()
assert any(s.package.provides('mpi') for _, s in e.concretized_specs())
def test_query_develop_specs():
"""Test whether a spec is develop'ed or not"""
env('create', 'test')
with ev.read('test') as e:
e.add('mpich')
e.add('mpileaks')
e.develop(Spec('mpich@1'), 'here', clone=False)
assert e.is_develop(Spec('mpich'))
assert not e.is_develop(Spec('mpileaks'))
@pytest.mark.parametrize('method', [
spack.cmd.env.env_activate,
spack.cmd.env.env_deactivate
])
@pytest.mark.parametrize(
'env,no_env,env_dir',
[
('b', False, None),
(None, True, None),
(None, False, 'path/'),
])
def test_activation_and_deactiviation_ambiguities(method, env, no_env, env_dir, capsys):
"""spack [-e x | -E | -D x/] env [activate | deactivate] y are ambiguous"""
args = Namespace(shell='sh', activate_env='a',
env=env, no_env=no_env, env_dir=env_dir)
with pytest.raises(SystemExit):
method(args)
_, err = capsys.readouterr()
assert 'is ambiguous' in err
@pytest.mark.regression('26548')
def test_custom_store_in_environment(mutable_config, tmpdir):
spack_yaml = tmpdir.join('spack.yaml')
spack_yaml.write("""
spack:
specs:
- libelf
config:
install_tree:
root: /tmp/store
""")
if sys.platform == 'win32':
sep = '\\'
else:
sep = '/'
current_store_root = str(spack.store.root)
assert str(current_store_root) != sep + os.path.join('tmp', 'store')
with spack.environment.Environment(str(tmpdir)):
assert str(spack.store.root) == sep + os.path.join('tmp', 'store')
assert str(spack.store.root) == current_store_root
def test_activate_temp(monkeypatch, tmpdir):
"""Tests whether `spack env activate --temp` creates an environment in a
temporary directory"""
env_dir = lambda: str(tmpdir)
monkeypatch.setattr(spack.cmd.env, "create_temp_env_directory", env_dir)
shell = env('activate', '--temp', '--sh')
active_env_var = next(line for line in shell.splitlines()
if ev.spack_env_var in line)
assert str(tmpdir) in active_env_var
assert ev.is_env_dir(str(tmpdir))
|