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
|
//! Tests for the new feature resolver.
use std::fs::File;
use cargo_test_support::cross_compile::{self, alternate};
use cargo_test_support::paths;
use cargo_test_support::prelude::*;
use cargo_test_support::publish::validate_crate_contents;
use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, cargo_process, project, rustc_host, Project};
/// Switches Cargo.toml to use `resolver = "2"`.
pub fn switch_to_resolver_2(p: &Project) {
let mut manifest = p.read_file("Cargo.toml");
if manifest.contains("resolver =") {
panic!("did not expect manifest to already contain a resolver setting");
}
if let Some(index) = manifest.find("[workspace]\n") {
manifest.insert_str(index + 12, "resolver = \"2\"\n");
} else if let Some(index) = manifest.find("[package]\n") {
manifest.insert_str(index + 10, "resolver = \"2\"\n");
} else {
panic!("expected [package] or [workspace] in manifest");
}
p.change_file("Cargo.toml", &manifest);
}
#[cargo_test]
fn inactivate_targets() {
// Basic test of `itarget`. A shared dependency where an inactive [target]
// changes the features.
Package::new("common", "1.0.0")
.feature("f1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature = "f1")]
compile_error!("f1 should not activate");
"#,
)
.publish();
Package::new("bar", "1.0.0")
.add_dep(
Dependency::new("common", "1.0")
.target("cfg(whatever)")
.enable_features(&["f1"]),
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[dependencies]
common = "1.0"
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(
str![[r#"
...
[ERROR] f1 should not activate
...
"#]]
.unordered(),
)
.run();
switch_to_resolver_2(&p);
p.cargo("check").run();
}
#[cargo_test]
fn inactive_target_optional() {
// Activating optional [target] dependencies for inactivate target.
Package::new("common", "1.0.0")
.feature("f1", &[])
.feature("f2", &[])
.feature("f3", &[])
.feature("f4", &[])
.file(
"src/lib.rs",
r#"
pub fn f() {
if cfg!(feature="f1") { println!("f1"); }
if cfg!(feature="f2") { println!("f2"); }
if cfg!(feature="f3") { println!("f3"); }
if cfg!(feature="f4") { println!("f4"); }
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
common = "1.0"
[target.'cfg(whatever)'.dependencies]
dep1 = {path='dep1', optional=true}
dep2 = {path='dep2', optional=true, features=["f3"]}
common = {version="1.0", optional=true, features=["f4"]}
[features]
foo1 = ["dep1/f2"]
foo2 = ["dep2"]
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
if cfg!(feature="foo1") { println!("foo1"); }
if cfg!(feature="foo2") { println!("foo2"); }
if cfg!(feature="dep1") { println!("dep1"); }
if cfg!(feature="dep2") { println!("dep2"); }
if cfg!(feature="common") { println!("common"); }
common::f();
}
"#,
)
.file(
"dep1/Cargo.toml",
r#"
[package]
name = "dep1"
version = "0.1.0"
edition = "2015"
[dependencies]
common = {version="1.0", features=["f1"]}
[features]
f2 = ["common/f2"]
"#,
)
.file(
"dep1/src/lib.rs",
r#"compile_error!("dep1 should not build");"#,
)
.file(
"dep2/Cargo.toml",
r#"
[package]
name = "dep2"
version = "0.1.0"
edition = "2015"
[dependencies]
common = "1.0"
[features]
f3 = ["common/f3"]
"#,
)
.file(
"dep2/src/lib.rs",
r#"compile_error!("dep2 should not build");"#,
)
.build();
p.cargo("run --all-features")
.with_stdout_data(str![[r#"
foo1
foo2
dep1
dep2
common
f1
f2
f3
f4
"#]])
.run();
p.cargo("run --features dep1")
.with_stdout_data(str![[r#"
dep1
f1
"#]])
.run();
p.cargo("run --features foo1")
.with_stdout_data(str![[r#"
foo1
dep1
f1
f2
"#]])
.run();
p.cargo("run --features dep2")
.with_stdout_data(str![[r#"
dep2
f3
"#]])
.run();
p.cargo("run --features common")
.with_stdout_data(str![[r#"
common
f4
"#]])
.run();
switch_to_resolver_2(&p);
p.cargo("run --all-features")
.with_stdout_data(str![[r#"
foo1
foo2
dep1
dep2
common
"#]])
.run();
p.cargo("run --features dep1")
.with_stdout_data(str![[r#"
dep1
"#]])
.run();
p.cargo("run --features foo1")
.with_stdout_data(str![[r#"
foo1
"#]])
.run();
p.cargo("run --features dep2")
.with_stdout_data(str![[r#"
dep2
"#]])
.run();
p.cargo("run --features common")
.with_stdout_data(str![[r#"
common
"#]])
.run();
}
#[cargo_test]
fn itarget_proc_macro() {
// itarget inside a proc-macro while cross-compiling
if cross_compile::disabled() {
return;
}
Package::new("hostdep", "1.0.0").publish();
Package::new("pm", "1.0.0")
.proc_macro(true)
.target_dep("hostdep", "1.0", rustc_host())
.file("src/lib.rs", "extern crate hostdep;")
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[dependencies]
pm = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
// Old behavior
p.cargo("check").run();
p.cargo("check --target").arg(alternate()).run();
// New behavior
switch_to_resolver_2(&p);
p.cargo("check").run();
p.cargo("check --target").arg(alternate()).run();
// For good measure, just make sure things don't break.
p.cargo("check --target").arg(alternate()).run();
}
#[cargo_test]
fn decouple_host_deps() {
// Basic test for `host_dep` decouple.
Package::new("common", "1.0.0")
.feature("f1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature = "f1")]
pub fn foo() {}
#[cfg(not(feature = "f1"))]
pub fn bar() {}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[build-dependencies]
common = {version="1.0", features=["f1"]}
[dependencies]
common = "1.0"
"#,
)
.file(
"build.rs",
r#"
use common::foo;
fn main() {}
"#,
)
.file("src/lib.rs", "use common::bar;")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
...
error[E0432]: unresolved import `common::bar`
...
"#]])
.run();
switch_to_resolver_2(&p);
p.cargo("check").run();
}
#[cargo_test]
fn decouple_host_deps_nested() {
// `host_dep` decouple of transitive dependencies.
Package::new("common", "1.0.0")
.feature("f1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature = "f1")]
pub fn foo() {}
#[cfg(not(feature = "f1"))]
pub fn bar() {}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[build-dependencies]
bdep = {path="bdep"}
[dependencies]
common = "1.0"
"#,
)
.file(
"build.rs",
r#"
use bdep::foo;
fn main() {}
"#,
)
.file("src/lib.rs", "use common::bar;")
.file(
"bdep/Cargo.toml",
r#"
[package]
name = "bdep"
version = "0.1.0"
edition = "2018"
[dependencies]
common = {version="1.0", features=["f1"]}
"#,
)
.file("bdep/src/lib.rs", "pub use common::foo;")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
...
error[E0432]: unresolved import `common::bar`
...
"#]])
.run();
switch_to_resolver_2(&p);
p.cargo("check").run();
}
#[cargo_test]
fn decouple_dev_deps() {
// Basic test for `dev_dep` decouple.
Package::new("common", "1.0.0")
.feature("f1", &[])
.feature("f2", &[])
.file(
"src/lib.rs",
r#"
// const ensures it uses the correct dependency at *build time*
// compared to *link time*.
#[cfg(all(feature="f1", not(feature="f2")))]
pub const X: u32 = 1;
#[cfg(all(feature="f1", feature="f2"))]
pub const X: u32 = 3;
pub fn foo() -> u32 {
let mut res = 0;
if cfg!(feature = "f1") {
res |= 1;
}
if cfg!(feature = "f2") {
res |= 2;
}
res
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
common = {version="1.0", features=["f1"]}
[dev-dependencies]
common = {version="1.0", features=["f2"]}
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
let expected: u32 = std::env::args().skip(1).next().unwrap().parse().unwrap();
assert_eq!(foo::foo(), expected);
assert_eq!(foo::build_time(), expected);
assert_eq!(common::foo(), expected);
assert_eq!(common::X, expected);
}
#[test]
fn test_bin() {
assert_eq!(foo::foo(), 3);
assert_eq!(common::foo(), 3);
assert_eq!(common::X, 3);
assert_eq!(foo::build_time(), 3);
}
"#,
)
.file(
"src/lib.rs",
r#"
pub fn foo() -> u32 {
common::foo()
}
pub fn build_time() -> u32 {
common::X
}
#[test]
fn test_lib() {
assert_eq!(foo(), 3);
assert_eq!(common::foo(), 3);
assert_eq!(common::X, 3);
}
"#,
)
.file(
"tests/t1.rs",
r#"
#[test]
fn test_t1() {
assert_eq!(foo::foo(), 3);
assert_eq!(common::foo(), 3);
assert_eq!(common::X, 3);
assert_eq!(foo::build_time(), 3);
}
#[test]
fn test_main() {
// Features are unified for main when run with `cargo test`,
// even with the new resolver.
let s = std::process::Command::new("target/debug/foo")
.arg("3")
.status().unwrap();
assert!(s.success());
}
"#,
)
.build();
// Old behavior
p.cargo("run 3").run();
p.cargo("test").run();
// New behavior
switch_to_resolver_2(&p);
p.cargo("run 1").run();
p.cargo("test").run();
}
#[cargo_test]
fn build_script_runtime_features() {
// Check that the CARGO_FEATURE_* environment variable is set correctly.
//
// This has a common dependency between build/normal/dev-deps, and it
// queries which features it was built with in different circumstances.
Package::new("common", "1.0.0")
.feature("normal", &[])
.feature("dev", &[])
.feature("build", &[])
.file(
"build.rs",
r#"
fn is_set(name: &str) -> bool {
std::env::var(name) == Ok("1".to_string())
}
fn main() {
let mut res = 0;
if is_set("CARGO_FEATURE_NORMAL") {
res |= 1;
}
if is_set("CARGO_FEATURE_DEV") {
res |= 2;
}
if is_set("CARGO_FEATURE_BUILD") {
res |= 4;
}
println!("cargo::rustc-cfg=RunCustomBuild=\"{}\"", res);
let mut res = 0;
if cfg!(feature = "normal") {
res |= 1;
}
if cfg!(feature = "dev") {
res |= 2;
}
if cfg!(feature = "build") {
res |= 4;
}
println!("cargo::rustc-cfg=CustomBuild=\"{}\"", res);
}
"#,
)
.file(
"src/lib.rs",
r#"
pub fn foo() -> u32 {
let mut res = 0;
if cfg!(feature = "normal") {
res |= 1;
}
if cfg!(feature = "dev") {
res |= 2;
}
if cfg!(feature = "build") {
res |= 4;
}
res
}
pub fn build_time() -> u32 {
#[cfg(RunCustomBuild="1")] return 1;
#[cfg(RunCustomBuild="3")] return 3;
#[cfg(RunCustomBuild="4")] return 4;
#[cfg(RunCustomBuild="5")] return 5;
#[cfg(RunCustomBuild="7")] return 7;
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[build-dependencies]
common = {version="1.0", features=["build"]}
[dependencies]
common = {version="1.0", features=["normal"]}
[dev-dependencies]
common = {version="1.0", features=["dev"]}
"#,
)
.file(
"build.rs",
r#"
fn main() {
assert_eq!(common::foo(), common::build_time());
println!("cargo::rustc-cfg=from_build=\"{}\"", common::foo());
}
"#,
)
.file(
"src/lib.rs",
r#"
pub fn foo() -> u32 {
common::foo()
}
pub fn build_time() -> u32 {
common::build_time()
}
#[test]
fn test_lib() {
assert_eq!(common::foo(), common::build_time());
assert_eq!(common::foo(),
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
}
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
assert_eq!(common::foo(), common::build_time());
assert_eq!(common::foo(),
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
}
#[test]
fn test_bin() {
assert_eq!(common::foo(), common::build_time());
assert_eq!(common::foo(),
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
}
"#,
)
.file(
"tests/t1.rs",
r#"
#[test]
fn test_t1() {
assert_eq!(common::foo(), common::build_time());
assert_eq!(common::foo(),
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
}
#[test]
fn test_main() {
// Features are unified for main when run with `cargo test`,
// even with the new resolver.
let s = std::process::Command::new("target/debug/foo")
.status().unwrap();
assert!(s.success());
}
"#,
)
.build();
// Old way, unifies all 3.
p.cargo("run").env("CARGO_FEATURE_EXPECT", "7").run();
p.cargo("test").env("CARGO_FEATURE_EXPECT", "7").run();
// New behavior.
switch_to_resolver_2(&p);
// normal + build unify
p.cargo("run").env("CARGO_FEATURE_EXPECT", "1").run();
// dev_deps are still unified with `cargo test`
p.cargo("test").env("CARGO_FEATURE_EXPECT", "3").run();
}
#[cargo_test]
fn cyclical_dev_dep() {
// Check how a cyclical dev-dependency will work.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[features]
dev = []
[dev-dependencies]
foo = { path = '.', features = ["dev"] }
"#,
)
.file(
"src/lib.rs",
r#"
pub fn assert_dev(enabled: bool) {
assert_eq!(enabled, cfg!(feature="dev"));
}
#[test]
fn test_in_lib() {
assert_dev(true);
}
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
let expected: bool = std::env::args().skip(1).next().unwrap().parse().unwrap();
foo::assert_dev(expected);
}
"#,
)
.file(
"tests/t1.rs",
r#"
#[test]
fn integration_links() {
foo::assert_dev(true);
// The lib linked with main.rs will also be unified.
let s = std::process::Command::new("target/debug/foo")
.arg("true")
.status().unwrap();
assert!(s.success());
}
"#,
)
.build();
// Old way unifies features.
p.cargo("run true").run();
// dev feature should always be enabled in tests.
p.cargo("test").run();
// New behavior.
switch_to_resolver_2(&p);
// Should decouple main.
p.cargo("run false").run();
// And this should be no different.
p.cargo("test").run();
}
#[cargo_test]
fn all_feature_opts() {
// All feature options at once.
Package::new("common", "1.0.0")
.feature("normal", &[])
.feature("build", &[])
.feature("dev", &[])
.feature("itarget", &[])
.file(
"src/lib.rs",
r#"
pub fn feats() -> u32 {
let mut res = 0;
if cfg!(feature="normal") { res |= 1; }
if cfg!(feature="build") { res |= 2; }
if cfg!(feature="dev") { res |= 4; }
if cfg!(feature="itarget") { res |= 8; }
res
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
common = {version = "1.0", features=["normal"]}
[dev-dependencies]
common = {version = "1.0", features=["dev"]}
[build-dependencies]
common = {version = "1.0", features=["build"]}
[target.'cfg(whatever)'.dependencies]
common = {version = "1.0", features=["itarget"]}
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
expect();
}
fn expect() {
let expected: u32 = std::env::var("EXPECTED_FEATS").unwrap().parse().unwrap();
assert_eq!(expected, common::feats());
}
#[test]
fn from_test() {
expect();
}
"#,
)
.build();
p.cargo("run").env("EXPECTED_FEATS", "15").run();
p.cargo("test").env("EXPECTED_FEATS", "15").run();
// New behavior.
switch_to_resolver_2(&p);
// Only normal feature.
p.cargo("run").env("EXPECTED_FEATS", "1").run();
// only normal+dev
p.cargo("test").env("EXPECTED_FEATS", "5").run();
}
#[cargo_test]
fn required_features_host_dep() {
// Check that required-features handles build-dependencies correctly.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[[bin]]
name = "x"
required-features = ["bdep/f1"]
[build-dependencies]
bdep = {path="bdep"}
"#,
)
.file("build.rs", "fn main() {}")
.file(
"src/bin/x.rs",
r#"
fn main() {}
"#,
)
.file(
"bdep/Cargo.toml",
r#"
[package]
name = "bdep"
version = "0.1.0"
edition = "2015"
[features]
f1 = []
"#,
)
.file("bdep/src/lib.rs", "")
.build();
p.cargo("run")
.with_status(101)
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[ERROR] target `x` in package `foo` requires the features: `bdep/f1`
Consider enabling them by passing, e.g., `--features="bdep/f1"`
"#]])
.run();
// New behavior.
switch_to_resolver_2(&p);
p.cargo("run --features bdep/f1").run();
}
#[cargo_test]
fn disabled_shared_host_dep() {
// Check for situation where an optional dep of a shared dep is enabled in
// a normal dependency, but disabled in an optional one. The unit tree is:
// foo
// ├── foo build.rs
// | └── common (BUILD dependency, NO FEATURES)
// └── common (Normal dependency, default features)
// └── somedep
Package::new("somedep", "1.0.0")
.file(
"src/lib.rs",
r#"
pub fn f() { println!("hello from somedep"); }
"#,
)
.publish();
Package::new("common", "1.0.0")
.feature("default", &["somedep"])
.add_dep(Dependency::new("somedep", "1.0").optional(true))
.file(
"src/lib.rs",
r#"
pub fn check_somedep() -> bool {
#[cfg(feature="somedep")]
{
extern crate somedep;
somedep::f();
true
}
#[cfg(not(feature="somedep"))]
{
println!("no somedep");
false
}
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2018"
resolver = "2"
[dependencies]
common = "1.0"
[build-dependencies]
common = {version = "1.0", default-features = false}
"#,
)
.file(
"src/main.rs",
"fn main() { assert!(common::check_somedep()); }",
)
.file(
"build.rs",
"fn main() { assert!(!common::check_somedep()); }",
)
.build();
p.cargo("run -v")
.with_stdout_data(str![[r#"
hello from somedep
"#]])
.run();
}
#[cargo_test]
fn required_features_inactive_dep() {
// required-features with an inactivated dep.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "2"
[target.'cfg(whatever)'.dependencies]
bar = {path="bar"}
[[bin]]
name = "foo"
required-features = ["feat1"]
[features]
feat1 = []
"#,
)
.file("src/main.rs", "fn main() {}")
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("bar/src/lib.rs", "")
.build();
p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check --features=feat1")
.with_stderr_data(str![[r#"
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn decouple_proc_macro() {
// proc macro features are not shared
Package::new("common", "1.0.0")
.feature("somefeat", &[])
.file(
"src/lib.rs",
r#"
pub const fn foo() -> bool { cfg!(feature="somefeat") }
#[cfg(feature="somefeat")]
pub const FEAT_ONLY_CONST: bool = true;
"#,
)
.publish();
Package::new("pm", "1.0.0")
.proc_macro(true)
.feature_dep("common", "1.0", &["somefeat"])
.file(
"src/lib.rs",
r#"
extern crate proc_macro;
extern crate common;
#[proc_macro]
pub fn foo(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
assert!(common::foo());
"".parse().unwrap()
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2018"
[dependencies]
pm = "1.0"
common = "1.0"
"#,
)
.file(
"src/lib.rs",
r#"
//! Test with docs.
//!
//! ```rust
//! pm::foo!{}
//! fn main() {
//! let expected = std::env::var_os("TEST_EXPECTS_ENABLED").is_some();
//! assert_eq!(expected, common::foo(), "common is wrong");
//! }
//! ```
"#,
)
.file(
"src/main.rs",
r#"
pm::foo!{}
fn main() {
println!("it is {}", common::foo());
}
"#,
)
.build();
p.cargo("run")
.env("TEST_EXPECTS_ENABLED", "1")
.with_stdout_data(str![[r#"
it is true
"#]])
.run();
// Make sure the test is fallible.
p.cargo("test --doc")
.with_status(101)
.with_stdout_data("...\n[..]common is wrong[..]\n...")
.run();
p.cargo("test --doc").env("TEST_EXPECTS_ENABLED", "1").run();
p.cargo("doc").run();
assert!(p
.build_dir()
.join("doc/common/constant.FEAT_ONLY_CONST.html")
.exists());
// cargo doc should clean in-between runs, but it doesn't, and leaves stale files.
// https://github.com/rust-lang/cargo/issues/6783 (same for removed items)
p.build_dir().join("doc").rm_rf();
// New behavior.
switch_to_resolver_2(&p);
p.cargo("run")
.with_stdout_data(str![[r#"
it is false
"#]])
.run();
p.cargo("test --doc").run();
p.cargo("doc").run();
assert!(!p
.build_dir()
.join("doc/common/constant.FEAT_ONLY_CONST.html")
.exists());
}
#[cargo_test]
fn proc_macro_ws() {
// Checks for bug with proc-macro in a workspace with dependency (shouldn't panic).
//
// Note, debuginfo is explicitly requested here to preserve the intent of this non-regression
// test: that will disable the debuginfo build dependencies optimization. Otherwise, it would
// initially trigger when the crates are built independently, but rebuild them with debuginfo
// when it sees the shared build/runtime dependency when checking the complete workspace.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["foo", "pm"]
resolver = "2"
[profile.dev.build-override]
debug = true
"#,
)
.file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[features]
feat1 = []
"#,
)
.file("foo/src/lib.rs", "")
.file(
"pm/Cargo.toml",
r#"
[package]
name = "pm"
version = "0.1.0"
edition = "2015"
[lib]
proc-macro = true
[dependencies]
foo = { path = "../foo", features=["feat1"] }
"#,
)
.file("pm/src/lib.rs", "")
.build();
p.cargo("check -p pm -v")
.with_stderr_data(str![[r#"
...
[RUNNING] `rustc --crate-name foo [..]--cfg[..]feat1[..]`
...
"#]])
.run();
// This may be surprising that `foo` doesn't get built separately. It is
// because pm might have other units (binaries, tests, etc.), and so the
// feature resolver must assume that normal deps get unified with it. This
// is related to the bigger issue where the features selected in a
// workspace depend on which packages are selected.
p.cargo("check --workspace -v")
.with_stderr_data(str![[r#"
[FRESH] foo v0.1.0 ([ROOT]/foo/foo)
[FRESH] pm v0.1.0 ([ROOT]/foo/pm)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// Selecting just foo will build without unification.
p.cargo("check -p foo -v")
// Make sure `foo` is built without feat1
.with_stderr_line_without(
&["[RUNNING] `rustc --crate-name foo --edition=2015"],
&["--cfg[..]feat1"],
)
.run();
}
#[cargo_test]
fn has_dev_dep_for_test() {
// Check for a bug where the decision on whether or not "dev dependencies"
// should be used did not consider `check --profile=test`.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[dev-dependencies]
dep = { path = 'dep', features = ['f1'] }
"#,
)
.file(
"src/lib.rs",
r#"
#[test]
fn t1() {
dep::f();
}
"#,
)
.file(
"dep/Cargo.toml",
r#"
[package]
name = "dep"
version = "0.1.0"
edition = "2015"
[features]
f1 = []
"#,
)
.file(
"dep/src/lib.rs",
r#"
#[cfg(feature = "f1")]
pub fn f() {}
"#,
)
.build();
p.cargo("check -v")
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check -v --profile=test")
.with_stderr_data(str![[r#"
[CHECKING] dep v0.1.0 ([ROOT]/foo/dep)
[RUNNING] `rustc --crate-name dep [..]`
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo [..]`
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// New resolver should not be any different.
switch_to_resolver_2(&p);
p.cargo("check -v --profile=test")
.with_stderr_data(str![[r#"
[FRESH] dep v0.1.0 ([ROOT]/foo/dep)
[FRESH] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn build_dep_activated() {
// Build dependencies always match the host for [target.*.build-dependencies].
if cross_compile::disabled() {
return;
}
Package::new("somedep", "1.0.0")
.file("src/lib.rs", "")
.publish();
Package::new("targetdep", "1.0.0").publish();
Package::new("hostdep", "1.0.0")
// Check that "for_host" is sticky.
.target_dep("somedep", "1.0", rustc_host())
.feature("feat1", &[])
.file(
"src/lib.rs",
r#"
extern crate somedep;
#[cfg(not(feature="feat1"))]
compile_error!{"feat1 missing"}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
# This should never be selected.
[target.'{}'.build-dependencies]
targetdep = "1.0"
[target.'{}'.build-dependencies]
hostdep = {{version="1.0", features=["feat1"]}}
"#,
alternate(),
rustc_host()
),
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.build();
p.cargo("check").run();
p.cargo("check --target").arg(alternate()).run();
// New behavior.
switch_to_resolver_2(&p);
p.cargo("check").run();
p.cargo("check --target").arg(alternate()).run();
}
#[cargo_test]
fn resolver_bad_setting() {
// Unknown setting in `resolver`
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "foo"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
`resolver` setting `foo` is not valid, valid options are "1" or "2"
"#]])
.run();
}
#[cargo_test]
fn resolver_original() {
// resolver="1" uses old unification behavior.
Package::new("common", "1.0.0")
.feature("f1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature = "f1")]
compile_error!("f1 should not activate");
"#,
)
.publish();
Package::new("bar", "1.0.0")
.add_dep(
Dependency::new("common", "1.0")
.target("cfg(whatever)")
.enable_features(&["f1"]),
)
.publish();
let manifest = |resolver| {
format!(
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "{}"
[dependencies]
common = "1.0"
bar = "1.0"
"#,
resolver
)
};
let p = project()
.file("Cargo.toml", &manifest("1"))
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(
str![[r#"
...
[ERROR] f1 should not activate
...
"#]]
.unordered(),
)
.run();
p.change_file("Cargo.toml", &manifest("2"));
p.cargo("check").run();
}
#[cargo_test]
fn resolver_not_both() {
// Can't specify resolver in both workspace and package.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
resolver = "2"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "2"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
cannot specify `resolver` field in both `[workspace]` and `[package]`
"#]])
.run();
}
#[cargo_test]
fn resolver_ws_member() {
// Can't specify `resolver` in a ws member.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["a"]
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
edition = "2015"
resolver = "2"
"#,
)
.file("a/src/lib.rs", "")
.build();
p.cargo("check")
.with_stderr_data(str![[r#"
[WARNING] resolver for the non root package will be ignored, specify resolver at the workspace root:
package: [ROOT]/foo/a/Cargo.toml
workspace: [ROOT]/foo/Cargo.toml
[CHECKING] a v0.1.0 ([ROOT]/foo/a)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn edition_2021_workspace_member() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["a"]
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
edition = "2021"
"#,
)
.file("a/src/lib.rs", "")
.build();
p.cargo("check").with_stderr_data(str![[r#"
[WARNING] virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
[NOTE] to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
[NOTE] to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
[NOTE] for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
[CHECKING] a v0.1.0 ([ROOT]/foo/a)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]).run();
}
#[cargo_test]
fn resolver_ws_root_and_member() {
// Check when specified in both ws root and member.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["a"]
resolver = "2"
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
edition = "2015"
resolver = "2"
"#,
)
.file("a/src/lib.rs", "")
.build();
// Ignores if they are the same.
p.cargo("check")
.with_stderr_data(str![[r#"
[CHECKING] a v0.1.0 ([ROOT]/foo/a)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn resolver_enables_new_features() {
// resolver="2" enables all the things.
Package::new("common", "1.0.0")
.feature("normal", &[])
.feature("build", &[])
.feature("dev", &[])
.feature("itarget", &[])
.file(
"src/lib.rs",
r#"
pub fn feats() -> u32 {
let mut res = 0;
if cfg!(feature="normal") { res |= 1; }
if cfg!(feature="build") { res |= 2; }
if cfg!(feature="dev") { res |= 4; }
if cfg!(feature="itarget") { res |= 8; }
res
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["a", "b"]
resolver = "2"
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
edition = "2018"
[dependencies]
common = {version = "1.0", features=["normal"]}
[dev-dependencies]
common = {version = "1.0", features=["dev"]}
[build-dependencies]
common = {version = "1.0", features=["build"]}
[target.'cfg(whatever)'.dependencies]
common = {version = "1.0", features=["itarget"]}
"#,
)
.file(
"a/src/main.rs",
r#"
fn main() {
expect();
}
fn expect() {
let expected: u32 = std::env::var("EXPECTED_FEATS").unwrap().parse().unwrap();
assert_eq!(expected, common::feats());
}
#[test]
fn from_test() {
expect();
}
"#,
)
.file(
"b/Cargo.toml",
r#"
[package]
name = "b"
version = "0.1.0"
edition = "2015"
[features]
ping = []
"#,
)
.file(
"b/src/main.rs",
r#"
fn main() {
if cfg!(feature="ping") {
println!("pong");
}
}
"#,
)
.build();
// Only normal.
p.cargo("run --bin a")
.env("EXPECTED_FEATS", "1")
.with_stderr_data(str![[r#"
[UPDATING] `dummy-registry` index
[LOCKING] 1 package to latest compatible version
[DOWNLOADING] crates ...
[DOWNLOADED] common v1.0.0 (registry `dummy-registry`)
[COMPILING] common v1.0.0
[COMPILING] a v0.1.0 ([ROOT]/foo/a)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/a[EXE]`
"#]])
.run();
// only normal+dev
p.cargo("test").cwd("a").env("EXPECTED_FEATS", "5").run();
// Can specify features of packages from a different directory.
p.cargo("run -p b --features=ping")
.cwd("a")
.with_stdout_data(str![[r#"
pong
"#]])
.run();
}
#[cargo_test]
fn install_resolve_behavior() {
// install honors the resolver behavior.
Package::new("common", "1.0.0")
.feature("f1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature = "f1")]
compile_error!("f1 should not activate");
"#,
)
.publish();
Package::new("bar", "1.0.0").dep("common", "1.0").publish();
Package::new("foo", "1.0.0")
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
resolver = "2"
[target.'cfg(whatever)'.dependencies]
common = {version="1.0", features=["f1"]}
[dependencies]
bar = "1.0"
"#,
)
.file("src/main.rs", "fn main() {}")
.publish();
cargo_process("install foo").run();
}
#[cargo_test]
fn package_includes_resolve_behavior() {
// `cargo package` will inherit the correct resolve behavior.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["a"]
resolver = "2"
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
edition = "2015"
authors = ["Zzz"]
description = "foo"
license = "MIT"
homepage = "https://example.com/"
"#,
)
.file("a/src/lib.rs", "")
.build();
p.cargo("package").cwd("a").run();
let rewritten_toml = str![[r##"
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2015"
name = "a"
version = "0.1.0"
authors = ["Zzz"]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "foo"
homepage = "https://example.com/"
readme = false
license = "MIT"
resolver = "2"
[lib]
name = "a"
path = "src/lib.rs"
"##]];
let f = File::open(&p.root().join("target/package/a-0.1.0.crate")).unwrap();
validate_crate_contents(
f,
"a-0.1.0.crate",
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs", "Cargo.lock"],
[("Cargo.toml", rewritten_toml)],
);
}
#[cargo_test]
fn tree_all() {
// `cargo tree` with the new feature resolver.
Package::new("log", "0.4.8").feature("serde", &[]).publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "2"
[target.'cfg(whatever)'.dependencies]
log = {version="*", features=["serde"]}
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("tree --target=all")
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo)
└── log v0.4.8
"#]])
.run();
}
#[cargo_test]
fn shared_dep_same_but_dependencies() {
// Checks for a bug of nondeterminism. This scenario creates a shared
// dependency `dep` which needs to be built twice (once as normal, and
// once as a build dep). However, in both cases the flags to `dep` are the
// same, the only difference is what it links to. The normal dependency
// should link to `subdep` with the feature disabled, and the build
// dependency should link to it with it enabled. Crucially, the `--target`
// flag should not be specified, otherwise Unit.kind would be different
// and avoid the collision, and this bug won't manifest.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["bin1", "bin2"]
resolver = "2"
"#,
)
.file(
"bin1/Cargo.toml",
r#"
[package]
name = "bin1"
version = "0.1.0"
edition = "2015"
[dependencies]
dep = { path = "../dep" }
"#,
)
.file("bin1/src/main.rs", "fn main() { dep::feat_func(); }")
.file(
"bin2/Cargo.toml",
r#"
[package]
name = "bin2"
version = "0.1.0"
edition = "2015"
[build-dependencies]
dep = { path = "../dep" }
subdep = { path = "../subdep", features = ["feat"] }
"#,
)
.file("bin2/build.rs", "fn main() { dep::feat_func(); }")
.file("bin2/src/main.rs", "fn main() {}")
.file(
"dep/Cargo.toml",
r#"
[package]
name = "dep"
version = "0.1.0"
edition = "2015"
[dependencies]
subdep = { path = "../subdep" }
"#,
)
.file(
"dep/src/lib.rs",
"pub fn feat_func() { subdep::feat_func(); }",
)
.file(
"subdep/Cargo.toml",
r#"
[package]
name = "subdep"
version = "0.1.0"
edition = "2015"
[features]
feat = []
"#,
)
.file(
"subdep/src/lib.rs",
r#"
pub fn feat_func() {
#[cfg(feature = "feat")] println!("cargo::warning=feat: enabled");
#[cfg(not(feature = "feat"))] println!("cargo::warning=feat: not enabled");
}
"#,
)
.build();
p.cargo("build --bin bin1 --bin bin2")
// unordered because bin1 and bin2 build at the same time
.with_stderr_data(
str![[r#"
[COMPILING] subdep v0.1.0 ([ROOT]/foo/subdep)
[COMPILING] dep v0.1.0 ([ROOT]/foo/dep)
[COMPILING] bin1 v0.1.0 ([ROOT]/foo/bin1)
[COMPILING] bin2 v0.1.0 ([ROOT]/foo/bin2)
[WARNING] bin2@0.1.0: feat: enabled
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]
.unordered(),
)
.run();
p.process(p.bin("bin1"))
.with_stdout_data(str![[r#"
cargo::warning=feat: not enabled
"#]])
.run();
// Make sure everything stays cached.
p.cargo("build -v --bin bin1 --bin bin2")
.with_stderr_data(
str![[r#"
[FRESH] subdep v0.1.0 ([ROOT]/foo/subdep)
[FRESH] dep v0.1.0 ([ROOT]/foo/dep)
[FRESH] bin1 v0.1.0 ([ROOT]/foo/bin1)
[WARNING] bin2@0.1.0: feat: enabled
[FRESH] bin2 v0.1.0 ([ROOT]/foo/bin2)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]
.unordered(),
)
.run();
}
#[cargo_test]
fn test_proc_macro() {
// Running `cargo test` on a proc-macro, with a shared dependency that has
// different features.
//
// There was a bug where `shared` was built twice (once with feature "B"
// and once without), and both copies linked into the unit test. This
// would cause a type failure when used in an intermediate dependency
// (the-macro-support).
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "runtime"
version = "0.1.0"
edition = "2015"
resolver = "2"
[dependencies]
the-macro = { path = "the-macro", features = ['a'] }
[build-dependencies]
shared = { path = "shared", features = ['b'] }
"#,
)
.file("src/lib.rs", "")
.file(
"the-macro/Cargo.toml",
r#"
[package]
name = "the-macro"
version = "0.1.0"
edition = "2015"
[lib]
proc-macro = true
test = false
[dependencies]
the-macro-support = { path = "../the-macro-support" }
shared = { path = "../shared" }
[dev-dependencies]
runtime = { path = ".." }
[features]
a = []
"#,
)
.file(
"the-macro/src/lib.rs",
"
fn _test() {
the_macro_support::foo(shared::Foo);
}
",
)
.file(
"the-macro-support/Cargo.toml",
r#"
[package]
name = "the-macro-support"
version = "0.1.0"
edition = "2015"
[dependencies]
shared = { path = "../shared" }
"#,
)
.file(
"the-macro-support/src/lib.rs",
"
pub fn foo(_: shared::Foo) {}
",
)
.file(
"shared/Cargo.toml",
r#"
[package]
name = "shared"
version = "0.1.0"
edition = "2015"
[features]
b = []
"#,
)
.file("shared/src/lib.rs", "pub struct Foo;")
.build();
p.cargo("test --manifest-path the-macro/Cargo.toml").run();
}
#[cargo_test]
fn doc_optional() {
// Checks for a bug where `cargo doc` was failing with an inactive target
// that enables a shared optional dependency.
Package::new("spin", "1.0.0").publish();
Package::new("bar", "1.0.0")
.add_dep(Dependency::new("spin", "1.0").optional(true))
.publish();
// The enabler package enables the `spin` feature, which we don't want.
Package::new("enabler", "1.0.0")
.feature_dep("bar", "1.0", &["spin"])
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "2"
[target.'cfg(whatever)'.dependencies]
enabler = "1.0"
[dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("doc")
.with_stderr_data(
str![[r#"
[UPDATING] `dummy-registry` index
[LOCKING] 3 packages to latest compatible versions
[DOWNLOADING] crates ...
[DOWNLOADED] spin v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] bar v1.0.0 (registry `dummy-registry`)
[DOCUMENTING] bar v1.0.0
[CHECKING] bar v1.0.0
[DOCUMENTING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[GENERATED] [ROOT]/foo/target/doc/foo/index.html
"#]]
.unordered(),
)
.run();
}
#[cargo_test]
fn minimal_download() {
// Various checks that it only downloads the minimum set of dependencies
// needed in various situations.
//
// This checks several permutations of the different
// host_dep/dev_dep/itarget settings. These 3 are planned to be stabilized
// together, so there isn't much need to be concerned about how the behave
// independently. However, there are some cases where they do behave
// independently. Specifically:
//
// * `cargo test` forces dev_dep decoupling to be disabled.
// * `cargo tree --target=all` forces ignore_inactive_targets off and decouple_dev_deps off.
// * `cargo tree --target=all -e normal` forces ignore_inactive_targets off.
//
// However, `cargo tree` is a little weird because it downloads everything
// anyways.
//
// So to summarize the different permutations:
//
// dev_dep | host_dep | itarget | Notes
// --------|----------|---------|----------------------------
// | | | -Zfeatures=compare (new resolver should behave same as old)
// | | ✓ | This scenario should not happen.
// | ✓ | | `cargo tree --target=all -Zfeatures=all`†
// | ✓ | ✓ | `cargo test`
// ✓ | | | This scenario should not happen.
// ✓ | | ✓ | This scenario should not happen.
// ✓ | ✓ | | `cargo tree --target=all -e normal -Z features=all`†
// ✓ | ✓ | ✓ | A normal build.
//
// † — However, `cargo tree` downloads everything.
Package::new("normal", "1.0.0").publish();
Package::new("normal_pm", "1.0.0").publish();
Package::new("normal_opt", "1.0.0").publish();
Package::new("dev_dep", "1.0.0").publish();
Package::new("dev_dep_pm", "1.0.0").publish();
Package::new("build_dep", "1.0.0").publish();
Package::new("build_dep_pm", "1.0.0").publish();
Package::new("build_dep_opt", "1.0.0").publish();
Package::new("itarget_normal", "1.0.0").publish();
Package::new("itarget_normal_pm", "1.0.0").publish();
Package::new("itarget_dev_dep", "1.0.0").publish();
Package::new("itarget_dev_dep_pm", "1.0.0").publish();
Package::new("itarget_build_dep", "1.0.0").publish();
Package::new("itarget_build_dep_pm", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[dependencies]
normal = "1.0"
normal_pm = "1.0"
normal_opt = { version = "1.0", optional = true }
[dev-dependencies]
dev_dep = "1.0"
dev_dep_pm = "1.0"
[build-dependencies]
build_dep = "1.0"
build_dep_pm = "1.0"
build_dep_opt = { version = "1.0", optional = true }
[target.'cfg(whatever)'.dependencies]
itarget_normal = "1.0"
itarget_normal_pm = "1.0"
[target.'cfg(whatever)'.dev-dependencies]
itarget_dev_dep = "1.0"
itarget_dev_dep_pm = "1.0"
[target.'cfg(whatever)'.build-dependencies]
itarget_build_dep = "1.0"
itarget_build_dep_pm = "1.0"
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.build();
let clear = || {
paths::cargo_home().join("registry/cache").rm_rf();
paths::cargo_home().join("registry/src").rm_rf();
p.build_dir().rm_rf();
};
// none
// Should be the same as `-Zfeatures=all`
p.cargo("check -Zfeatures=compare")
.masquerade_as_nightly_cargo(&["features=compare"])
.with_stderr_data(
str![[r#"
[UPDATING] `dummy-registry` index
[LOCKING] 14 packages to latest compatible versions
[DOWNLOADING] crates ...
[DOWNLOADED] normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep v1.0.0 (registry `dummy-registry`)
[COMPILING] build_dep v1.0.0
[COMPILING] build_dep_pm v1.0.0
[CHECKING] normal_pm v1.0.0
[CHECKING] normal v1.0.0
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]
.unordered(),
)
.run();
clear();
// New behavior
switch_to_resolver_2(&p);
// all
p.cargo("check")
.with_stderr_data(
str![[r#"
[DOWNLOADING] crates ...
[DOWNLOADED] normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep v1.0.0 (registry `dummy-registry`)
[COMPILING] build_dep_pm v1.0.0
[COMPILING] build_dep v1.0.0
[CHECKING] normal_pm v1.0.0
[CHECKING] normal v1.0.0
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]
.unordered(),
)
.run();
clear();
// This disables decouple_dev_deps.
p.cargo("test --no-run")
.with_stderr_data(
str![[r#"
[DOWNLOADING] crates ...
[DOWNLOADED] normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] dev_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] dev_dep v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep v1.0.0 (registry `dummy-registry`)
[COMPILING] build_dep_pm v1.0.0
[COMPILING] build_dep v1.0.0
[COMPILING] normal_pm v1.0.0
[COMPILING] normal v1.0.0
[COMPILING] dev_dep v1.0.0
[COMPILING] dev_dep_pm v1.0.0
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[EXECUTABLE] unittests src/lib.rs (target/debug/deps/foo-[HASH][EXE])
"#]]
.unordered(),
)
.run();
clear();
// This disables itarget, but leaves decouple_dev_deps enabled.
p.cargo("tree -e normal --target=all")
.with_stderr_data(
str![[r#"
[DOWNLOADING] crates ...
[DOWNLOADED] normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_build_dep v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep v1.0.0 (registry `dummy-registry`)
"#]]
.unordered(),
)
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo)
├── itarget_normal v1.0.0
├── itarget_normal_pm v1.0.0
├── normal v1.0.0
└── normal_pm v1.0.0
"#]])
.run();
clear();
// This disables itarget and decouple_dev_deps.
p.cargo("tree --target=all")
.with_stderr_data(
str![[r#"
[DOWNLOADING] crates ...
[DOWNLOADED] normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_normal_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_normal v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_dev_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_dev_dep v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] itarget_build_dep v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] dev_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] dev_dep v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep_pm v1.0.0 (registry `dummy-registry`)
[DOWNLOADED] build_dep v1.0.0 (registry `dummy-registry`)
"#]]
.unordered(),
)
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo)
├── itarget_normal v1.0.0
├── itarget_normal_pm v1.0.0
├── normal v1.0.0
└── normal_pm v1.0.0
[build-dependencies]
├── build_dep v1.0.0
├── build_dep_pm v1.0.0
├── itarget_build_dep v1.0.0
└── itarget_build_dep_pm v1.0.0
[dev-dependencies]
├── dev_dep v1.0.0
├── dev_dep_pm v1.0.0
├── itarget_dev_dep v1.0.0
└── itarget_dev_dep_pm v1.0.0
"#]])
.run();
clear();
}
#[cargo_test]
fn pm_with_int_shared() {
// This is a somewhat complex scenario of a proc-macro in a workspace with
// an integration test where the proc-macro is used for other things, and
// *everything* is built at once (`--workspace --all-targets
// --all-features`). There was a bug where the UnitFor settings were being
// incorrectly computed based on the order that the graph was traversed.
//
// There are some uncertainties about exactly how proc-macros should behave
// with `--workspace`, see https://github.com/rust-lang/cargo/issues/8312.
//
// This uses a const-eval hack to do compile-time feature checking.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["foo", "pm", "shared"]
resolver = "2"
"#,
)
.file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
pm = { path = "../pm" }
shared = { path = "../shared", features = ["norm-feat"] }
"#,
)
.file(
"foo/src/lib.rs",
r#"
// foo->shared always has both features set
const _CHECK: [(); 0] = [(); 0-!(shared::FEATS==3) as usize];
"#,
)
.file(
"pm/Cargo.toml",
r#"
[package]
name = "pm"
version = "0.1.0"
edition = "2015"
[lib]
proc-macro = true
[dependencies]
shared = { path = "../shared", features = ["host-feat"] }
"#,
)
.file(
"pm/src/lib.rs",
r#"
// pm->shared always has just host
const _CHECK: [(); 0] = [(); 0-!(shared::FEATS==1) as usize];
"#,
)
.file(
"pm/tests/pm_test.rs",
r#"
// integration test gets both set
const _CHECK: [(); 0] = [(); 0-!(shared::FEATS==3) as usize];
"#,
)
.file(
"shared/Cargo.toml",
r#"
[package]
name = "shared"
version = "0.1.0"
edition = "2015"
[features]
norm-feat = []
host-feat = []
"#,
)
.file(
"shared/src/lib.rs",
r#"
pub const FEATS: u32 = {
if cfg!(feature="norm-feat") && cfg!(feature="host-feat") {
3
} else if cfg!(feature="norm-feat") {
2
} else if cfg!(feature="host-feat") {
1
} else {
0
}
};
"#,
)
.build();
p.cargo("build --workspace --all-targets --all-features -v")
.with_stderr_data(
str![[r#"
[COMPILING] shared v0.1.0 ([ROOT]/foo/shared)
[RUNNING] `rustc --crate-name shared [..]--crate-type lib [..]`
[RUNNING] `rustc --crate-name shared [..]--crate-type lib [..]`
[RUNNING] `rustc --crate-name shared [..]--test[..]`
[COMPILING] pm v0.1.0 ([ROOT]/foo/pm)
[RUNNING] `rustc --crate-name pm [..]--crate-type proc-macro[..]`
[RUNNING] `rustc --crate-name pm [..]--test[..]`
[COMPILING] foo v0.1.0 ([ROOT]/foo/foo)
[RUNNING] `rustc --crate-name foo [..]--crate-type lib [..]`
[RUNNING] `rustc --crate-name foo [..]--test[..]`
[RUNNING] `rustc --crate-name pm_test [..]--test[..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]
.unordered(),
)
.run();
// And again, should stay fresh.
p.cargo("build --workspace --all-targets --all-features -v")
.with_stderr_data(
str![[r#"
[FRESH] pm v0.1.0 ([ROOT]/foo/pm)
[FRESH] foo v0.1.0 ([ROOT]/foo/foo)
[FRESH] shared v0.1.0 ([ROOT]/foo/shared)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]]
.unordered(),
)
.run();
}
#[cargo_test]
fn doc_proc_macro() {
// Checks for a bug when documenting a proc-macro with a dependency. The
// doc unit builder was not carrying the "for host" setting through the
// dependencies, and the `pm-dep` dependency was causing a panic because
// it was looking for target features instead of host features.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
resolver = "2"
[dependencies]
pm = { path = "pm" }
"#,
)
.file("src/lib.rs", "")
.file(
"pm/Cargo.toml",
r#"
[package]
name = "pm"
version = "0.1.0"
edition = "2015"
[lib]
proc-macro = true
[dependencies]
pm-dep = { path = "../pm-dep" }
"#,
)
.file("pm/src/lib.rs", "")
.file("pm-dep/Cargo.toml", &basic_manifest("pm-dep", "0.1.0"))
.file("pm-dep/src/lib.rs", "")
.build();
// Unfortunately this cannot check the output because what it prints is
// nondeterministic. Sometimes it says "Compiling pm-dep" and sometimes
// "Checking pm-dep". This is because it is both building it and checking
// it in parallel (building so it can build the proc-macro, and checking
// so rustdoc can load it).
p.cargo("doc").run();
}
#[cargo_test]
fn edition_2021_default_2() {
// edition = 2021 defaults to v2 resolver.
Package::new("common", "1.0.0")
.feature("f1", &[])
.file("src/lib.rs", "")
.publish();
Package::new("bar", "1.0.0")
.add_dep(
Dependency::new("common", "1.0")
.target("cfg(whatever)")
.enable_features(&["f1"]),
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2015"
[dependencies]
common = "1.0"
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
// First without edition.
p.cargo("tree -f")
.arg("{p} feats:{f}")
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo) feats:
├── bar v1.0.0 feats:
└── common v1.0.0 feats:f1
"#]])
.run();
p.change_file(
"Cargo.toml",
r#"
cargo-features = ["edition2021"]
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
common = "1.0"
bar = "1.0"
"#,
);
// Importantly, this does not include `f1` on `common`.
p.cargo("tree -f")
.arg("{p} feats:{f}")
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo) feats:
├── bar v1.0.0 feats:
└── common v1.0.0 feats:
"#]])
.run();
}
#[cargo_test]
fn all_features_merges_with_features() {
Package::new("dep", "0.1.0")
.feature("feat1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature="feat1")]
pub fn work() {
println!("it works");
}
"#,
)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[features]
a = []
[dependencies]
dep = "0.1"
[[example]]
name = "ex"
required-features = ["a", "dep/feat1"]
"#,
)
.file(
"examples/ex.rs",
r#"
fn main() {
dep::work();
}
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("run --example ex --all-features --features dep/feat1")
.with_stderr_data(str![[r#"
[UPDATING] `dummy-registry` index
[LOCKING] 1 package to latest compatible version
[DOWNLOADING] crates ...
[DOWNLOADED] dep v0.1.0 (registry `dummy-registry`)
[COMPILING] dep v0.1.0
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/examples/ex[EXE]`
"#]])
.with_stdout_data(str![[r#"
it works
"#]])
.run();
switch_to_resolver_2(&p);
p.cargo("run --example ex --all-features --features dep/feat1")
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/examples/ex[EXE]`
"#]])
.with_stdout_data(str![[r#"
it works
"#]])
.run();
}
#[cargo_test]
fn dep_with_optional_host_deps_activated() {
// To prevent regression like rust-lang/cargo#11330
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { path = "serde", features = ["derive", "build"] }
"#,
)
.file("src/lib.rs", "")
.file(
"serde/Cargo.toml",
r#"
[package]
name = "serde"
version = "0.1.0"
edition = "2021"
[dependencies]
serde_derive = { path = "../serde_derive", optional = true }
[build-dependencies]
serde_build = { path = "../serde_build", optional = true }
[features]
derive = ["dep:serde_derive"]
build = ["dep:serde_build"]
"#,
)
.file("serde/src/lib.rs", "")
.file("serde/build.rs", "fn main() {}")
.file(
"serde_derive/Cargo.toml",
r#"
[package]
name = "serde_derive"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
"#,
)
.file("serde_derive/src/lib.rs", "")
.file(
"serde_build/Cargo.toml",
&basic_manifest("serde_build", "0.1.0"),
)
.file("serde_build/src/lib.rs", "")
.build();
p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 3 packages to latest compatible versions
[COMPILING] serde_build v0.1.0 ([ROOT]/foo/serde_build)
[COMPILING] serde_derive v0.1.0 ([ROOT]/foo/serde_derive)
[COMPILING] serde v0.1.0 ([ROOT]/foo/serde)
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn dont_unify_proc_macro_example_from_dependency() {
// See https://github.com/rust-lang/cargo/issues/13726
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
edition = "2021"
[dependencies]
pm_helper = { path = "pm_helper" }
"#,
)
.file("src/lib.rs", "")
.file(
"pm_helper/Cargo.toml",
r#"
[package]
name = "pm_helper"
[[example]]
name = "pm"
proc-macro = true
crate-type = ["proc-macro"]
"#,
)
.file("pm_helper/src/lib.rs", "")
.file("pm_helper/examples/pm.rs", "")
.build();
p.cargo("check")
.with_stderr_data(str![[r#"
[LOCKING] 1 package to latest compatible version
[CHECKING] pm_helper v0.0.0 ([ROOT]/foo/pm_helper)
[CHECKING] foo v0.0.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
|