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
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Prints out and aggregates test execution statuses.
//!
//! The main structure in this module is [`TestReporter`].
use super::{helpers::ByteSubslice, structured::StructuredReporter};
use crate::{
config::{CompiledDefaultFilter, EvaluatableProfile, ScriptId},
errors::WriteEventError,
helpers::{io_write_test_name, plural},
list::{SkipCounts, TestInstance, TestList},
reporter::{aggregator::EventAggregator, helpers::highlight_end},
runner::{
AbortStatus, ExecuteStatus, ExecutionDescription, ExecutionResult, ExecutionStatuses,
FinalRunStats, RetryData, RunStats, RunStatsFailureKind, SetupScriptExecuteStatus,
},
test_output::{ChildSingleOutput, TestExecutionOutput, TestOutput},
};
use bstr::ByteSlice;
use chrono::{DateTime, FixedOffset};
use debug_ignore::DebugIgnore;
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use nextest_metadata::MismatchReason;
use owo_colors::{OwoColorize, Style};
use quick_junit::ReportUuid;
use serde::Deserialize;
use std::{
borrow::Cow,
cmp::Reverse,
fmt, io,
io::{BufWriter, Write},
time::Duration,
};
use swrite::{swrite, SWrite};
/// When to display test output in the reporter.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[serde(rename_all = "kebab-case")]
pub enum TestOutputDisplay {
/// Show output immediately on execution completion.
///
/// This is the default for failing tests.
Immediate,
/// Show output immediately, and at the end of a test run.
ImmediateFinal,
/// Show output at the end of execution.
Final,
/// Never show output.
Never,
}
impl TestOutputDisplay {
/// Returns true if test output is shown immediately.
pub fn is_immediate(self) -> bool {
match self {
TestOutputDisplay::Immediate | TestOutputDisplay::ImmediateFinal => true,
TestOutputDisplay::Final | TestOutputDisplay::Never => false,
}
}
/// Returns true if test output is shown at the end of the run.
pub fn is_final(self) -> bool {
match self {
TestOutputDisplay::Final | TestOutputDisplay::ImmediateFinal => true,
TestOutputDisplay::Immediate | TestOutputDisplay::Never => false,
}
}
}
/// Status level to show in the reporter output.
///
/// Status levels are incremental: each level causes all the statuses listed above it to be output. For example,
/// [`Slow`](Self::Slow) implies [`Retry`](Self::Retry) and [`Fail`](Self::Fail).
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum StatusLevel {
/// No output.
None,
/// Only output test failures.
Fail,
/// Output retries and failures.
Retry,
/// Output information about slow tests, and all variants above.
Slow,
/// Output information about leaky tests, and all variants above.
Leak,
/// Output passing tests in addition to all variants above.
Pass,
/// Output skipped tests in addition to all variants above.
Skip,
/// Currently has the same meaning as [`Skip`](Self::Skip).
All,
}
/// Status level to show at the end of test runs in the reporter output.
///
/// Status levels are incremental.
///
/// This differs from [`StatusLevel`] in two ways:
/// * It has a "flaky" test indicator that's different from "retry" (though "retry" works as an alias.)
/// * It has a different ordering: skipped tests are prioritized over passing ones.
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum FinalStatusLevel {
/// No output.
None,
/// Only output test failures.
Fail,
/// Output flaky tests.
#[serde(alias = "retry")]
Flaky,
/// Output information about slow tests, and all variants above.
Slow,
/// Output skipped tests in addition to all variants above.
Skip,
/// Output leaky tests in addition to all variants above.
Leak,
/// Output passing tests in addition to all variants above.
Pass,
/// Currently has the same meaning as [`Pass`](Self::Pass).
All,
}
/// Standard error destination for the reporter.
///
/// This is usually a terminal, but can be an in-memory buffer for tests.
pub enum ReporterStderr<'a> {
/// Produce output on the (possibly piped) terminal.
///
/// If the terminal isn't piped, produce output to a progress bar.
Terminal,
/// Write output to a buffer.
Buffer(&'a mut Vec<u8>),
}
/// Test reporter builder.
#[derive(Debug, Default)]
pub struct TestReporterBuilder {
no_capture: bool,
failure_output: Option<TestOutputDisplay>,
success_output: Option<TestOutputDisplay>,
status_level: Option<StatusLevel>,
final_status_level: Option<FinalStatusLevel>,
verbose: bool,
hide_progress_bar: bool,
}
impl TestReporterBuilder {
/// Sets no-capture mode.
///
/// In this mode, `failure_output` and `success_output` will be ignored, and `status_level`
/// will be at least [`StatusLevel::Pass`].
pub fn set_no_capture(&mut self, no_capture: bool) -> &mut Self {
self.no_capture = no_capture;
self
}
/// Sets the conditions under which test failures are output.
pub fn set_failure_output(&mut self, failure_output: TestOutputDisplay) -> &mut Self {
self.failure_output = Some(failure_output);
self
}
/// Sets the conditions under which test successes are output.
pub fn set_success_output(&mut self, success_output: TestOutputDisplay) -> &mut Self {
self.success_output = Some(success_output);
self
}
/// Sets the kinds of statuses to output.
pub fn set_status_level(&mut self, status_level: StatusLevel) -> &mut Self {
self.status_level = Some(status_level);
self
}
/// Sets the kinds of statuses to output at the end of the run.
pub fn set_final_status_level(&mut self, final_status_level: FinalStatusLevel) -> &mut Self {
self.final_status_level = Some(final_status_level);
self
}
/// Sets verbose output.
pub fn set_verbose(&mut self, verbose: bool) -> &mut Self {
self.verbose = verbose;
self
}
/// Sets visibility of the progress bar.
/// The progress bar is also hidden if `no_capture` is set.
pub fn set_hide_progress_bar(&mut self, hide_progress_bar: bool) -> &mut Self {
self.hide_progress_bar = hide_progress_bar;
self
}
}
impl TestReporterBuilder {
/// Creates a new test reporter.
pub fn build<'a>(
&self,
test_list: &TestList,
profile: &EvaluatableProfile<'a>,
output: ReporterStderr<'a>,
structured_reporter: StructuredReporter<'a>,
) -> TestReporter<'a> {
let styles = Box::default();
let binary_id_width = test_list
.iter()
.filter_map(|test_suite| {
(test_suite.status.test_count() > 0).then_some(test_suite.binary_id.len())
})
.max()
.unwrap_or_default();
let aggregator = EventAggregator::new(profile);
let status_level = self.status_level.unwrap_or_else(|| profile.status_level());
let status_level = match self.no_capture {
// In no-capture mode, the status level is treated as at least pass.
true => status_level.max(StatusLevel::Pass),
false => status_level,
};
let final_status_level = self
.final_status_level
.unwrap_or_else(|| profile.final_status_level());
// failure_output and success_output are meaningless if the runner isn't capturing any
// output.
let force_success_output = match self.no_capture {
true => Some(TestOutputDisplay::Never),
false => self.success_output,
};
let force_failure_output = match self.no_capture {
true => Some(TestOutputDisplay::Never),
false => self.failure_output,
};
let stderr = match output {
ReporterStderr::Terminal if self.no_capture => {
// Do not use a progress bar if --no-capture is passed in. This is required since we
// pass down stderr to the child process.
//
// In the future, we could potentially switch to using a pty, in which case we could
// still potentially use the progress bar as a status bar. However, that brings
// about its own complications: what if a test's output doesn't include a newline?
// We might have to use a curses-like UI which would be a lot of work for not much
// gain.
ReporterStderrImpl::TerminalWithoutBar
}
ReporterStderr::Terminal if is_ci::uncached() => {
// Some CI environments appear to pretend to be a terminal. Disable the progress bar
// in these environments.
ReporterStderrImpl::TerminalWithoutBar
}
ReporterStderr::Terminal if self.hide_progress_bar => {
ReporterStderrImpl::TerminalWithoutBar
}
ReporterStderr::Terminal => {
let progress_bar = ProgressBar::new(test_list.test_count() as u64);
// Emulate Cargo's style.
let test_count_width = format!("{}", test_list.test_count()).len();
// Create the template using the width as input. This is a little confusing -- {{foo}}
// is what's passed into the ProgressBar, while {bar} is inserted by the format!() statement.
//
// Note: ideally we'd use the same format as our other duration displays for the elapsed time,
// but that isn't possible due to https://github.com/console-rs/indicatif/issues/440. Use
// {{elapsed_precise}} as an OK tradeoff here.
let template = format!(
"{{prefix:>12}} [{{elapsed_precise:>9}}] [{{wide_bar}}] \
{{pos:>{test_count_width}}}/{{len:{test_count_width}}}: {{msg}} "
);
progress_bar.set_style(
ProgressStyle::default_bar()
.progress_chars("=> ")
.template(&template)
.expect("template is known to be valid"),
);
// NOTE: set_draw_target must be called before enable_steady_tick to avoid a
// spurious extra line from being printed as the draw target changes.
//
// This used to be unbuffered, but that option went away from indicatif 0.17.0. The
// refresh rate is now 20hz so that it's double the steady tick rate.
progress_bar.set_draw_target(ProgressDrawTarget::stderr_with_hz(20));
// Enable a steady tick 10 times a second.
progress_bar.enable_steady_tick(Duration::from_millis(100));
ReporterStderrImpl::TerminalWithBar(progress_bar)
}
ReporterStderr::Buffer(buf) => ReporterStderrImpl::Buffer(buf),
};
// Ordinarily, empty stdout and stderr are not displayed. This
// environment variable is set in integration tests to ensure that they
// are.
let display_empty_outputs =
std::env::var_os("__NEXTEST_DISPLAY_EMPTY_OUTPUTS").map_or(false, |v| v == "1");
TestReporter {
inner: TestReporterImpl {
default_filter: profile.default_filter().clone(),
status_levels: StatusLevels {
status_level,
final_status_level,
},
force_success_output,
force_failure_output,
no_capture: self.no_capture,
binary_id_width,
styles,
cancel_status: None,
final_outputs: DebugIgnore(vec![]),
display_empty_outputs,
},
stderr,
structured_reporter,
metadata_reporter: aggregator,
}
}
}
enum ReporterStderrImpl<'a> {
TerminalWithBar(ProgressBar),
TerminalWithoutBar,
Buffer(&'a mut Vec<u8>),
}
impl ReporterStderrImpl<'_> {
fn finish_and_clear_bar(&self) {
match self {
ReporterStderrImpl::TerminalWithBar(bar) => {
bar.finish_and_clear();
}
ReporterStderrImpl::TerminalWithoutBar | ReporterStderrImpl::Buffer(_) => {}
}
}
}
/// Functionality to report test results to stderr, JUnit, and/or structured,
/// machine-readable results to stdout
pub struct TestReporter<'a> {
inner: TestReporterImpl<'a>,
stderr: ReporterStderrImpl<'a>,
/// Used to aggregate events for JUnit reports written to disk
metadata_reporter: EventAggregator<'a>,
/// Used to emit test events in machine-readable format(s) to stdout
structured_reporter: StructuredReporter<'a>,
}
impl<'a> TestReporter<'a> {
/// Colorizes output.
pub fn colorize(&mut self) {
self.inner.styles.colorize();
}
/// Report a test event.
pub fn report_event(&mut self, event: TestEvent<'a>) -> Result<(), WriteEventError> {
self.write_event(event)
}
/// Mark the reporter done.
pub fn finish(&mut self) {
self.stderr.finish_and_clear_bar();
}
// ---
// Helper methods
// ---
/// Report this test event to the given writer.
fn write_event(&mut self, event: TestEvent<'a>) -> Result<(), WriteEventError> {
match &mut self.stderr {
ReporterStderrImpl::TerminalWithBar(progress_bar) => {
// Write to a string that will be printed as a log line.
let mut buf: Vec<u8> = Vec::new();
self.inner
.write_event_impl(&event, &mut buf)
.map_err(WriteEventError::Io)?;
// ProgressBar::println doesn't print status lines if the bar is hidden. The suspend
// method prints it in both cases.
progress_bar.suspend(|| {
_ = std::io::stderr().write_all(&buf);
});
update_progress_bar(&event, &self.inner.styles, progress_bar);
}
ReporterStderrImpl::TerminalWithoutBar => {
// Write to a buffered stderr.
let mut writer = BufWriter::new(std::io::stderr());
self.inner
.write_event_impl(&event, &mut writer)
.map_err(WriteEventError::Io)?;
writer.flush().map_err(WriteEventError::Io)?;
}
ReporterStderrImpl::Buffer(buf) => {
self.inner
.write_event_impl(&event, *buf)
.map_err(WriteEventError::Io)?;
}
}
self.structured_reporter.write_event(&event)?;
self.metadata_reporter.write_event(event)?;
Ok(())
}
}
fn update_progress_bar(event: &TestEvent<'_>, styles: &Styles, progress_bar: &ProgressBar) {
match &event.kind {
TestEventKind::SetupScriptStarted { no_capture, .. } => {
// Hide the progress bar if either stderr or stdout are being passed through.
if *no_capture {
progress_bar.set_draw_target(ProgressDrawTarget::hidden());
}
}
TestEventKind::SetupScriptFinished { no_capture, .. } => {
// Restore the progress bar if it was hidden.
if *no_capture {
progress_bar.set_draw_target(ProgressDrawTarget::stderr());
}
}
TestEventKind::TestStarted {
current_stats,
running,
cancel_state,
..
}
| TestEventKind::TestFinished {
current_stats,
running,
cancel_state,
..
} => {
progress_bar.set_prefix(progress_bar_prefix(current_stats, *cancel_state, styles));
progress_bar.set_message(progress_bar_msg(current_stats, *running, styles));
// If there are skipped tests, the initial run count will be lower than when constructed
// in ProgressBar::new.
progress_bar.set_length(current_stats.initial_run_count as u64);
progress_bar.set_position(current_stats.finished_count as u64);
}
TestEventKind::RunBeginCancel { .. } => {
progress_bar.set_prefix(progress_bar_cancel_prefix(styles));
}
_ => {}
}
}
fn progress_bar_cancel_prefix(styles: &Styles) -> String {
format!("{:>12}", "Cancelling".style(styles.fail))
}
fn progress_bar_prefix(
run_stats: &RunStats,
cancel_reason: Option<CancelReason>,
styles: &Styles,
) -> String {
if cancel_reason.is_some() {
return progress_bar_cancel_prefix(styles);
}
let style = if run_stats.has_failures() {
styles.fail
} else {
styles.pass
};
format!("{:>12}", "Running".style(style))
}
fn progress_bar_msg(current_stats: &RunStats, running: usize, styles: &Styles) -> String {
let mut s = format!("{} running, ", running.style(styles.count));
write_summary_str(current_stats, styles, &mut s);
s
}
fn write_summary_str(run_stats: &RunStats, styles: &Styles, out: &mut String) {
swrite!(
out,
"{} {}",
run_stats.passed.style(styles.count),
"passed".style(styles.pass)
);
if run_stats.passed_slow > 0 || run_stats.flaky > 0 || run_stats.leaky > 0 {
let mut text = Vec::with_capacity(3);
if run_stats.passed_slow > 0 {
text.push(format!(
"{} {}",
run_stats.passed_slow.style(styles.count),
"slow".style(styles.skip),
));
}
if run_stats.flaky > 0 {
text.push(format!(
"{} {}",
run_stats.flaky.style(styles.count),
"flaky".style(styles.skip),
));
}
if run_stats.leaky > 0 {
text.push(format!(
"{} {}",
run_stats.leaky.style(styles.count),
"leaky".style(styles.skip),
));
}
swrite!(out, " ({})", text.join(", "));
}
swrite!(out, ", ");
if run_stats.failed > 0 {
swrite!(
out,
"{} {}, ",
run_stats.failed.style(styles.count),
"failed".style(styles.fail),
);
}
if run_stats.exec_failed > 0 {
swrite!(
out,
"{} {}, ",
run_stats.exec_failed.style(styles.count),
"exec failed".style(styles.fail),
);
}
if run_stats.timed_out > 0 {
swrite!(
out,
"{} {}, ",
run_stats.timed_out.style(styles.count),
"timed out".style(styles.fail),
);
}
swrite!(
out,
"{} {}",
run_stats.skipped.style(styles.count),
"skipped".style(styles.skip),
);
}
#[derive(Debug)]
enum FinalOutput {
Skipped(#[allow(dead_code)] MismatchReason),
Executed {
run_statuses: ExecutionStatuses,
display_output: bool,
},
}
impl FinalOutput {
fn final_status_level(&self) -> FinalStatusLevel {
match self {
Self::Skipped(_) => FinalStatusLevel::Skip,
Self::Executed { run_statuses, .. } => run_statuses.describe().final_status_level(),
}
}
}
struct TestReporterImpl<'a> {
default_filter: CompiledDefaultFilter,
status_levels: StatusLevels,
force_success_output: Option<TestOutputDisplay>,
force_failure_output: Option<TestOutputDisplay>,
no_capture: bool,
binary_id_width: usize,
styles: Box<Styles>,
cancel_status: Option<CancelReason>,
final_outputs: DebugIgnore<Vec<(TestInstance<'a>, FinalOutput)>>,
display_empty_outputs: bool,
}
impl<'a> TestReporterImpl<'a> {
fn write_event_impl(
&mut self,
event: &TestEvent<'a>,
writer: &mut dyn Write,
) -> io::Result<()> {
match &event.kind {
TestEventKind::RunStarted {
test_list,
run_id,
profile_name,
cli_args: _,
} => {
writeln!(writer, "------------")?;
write!(writer, "{:>12} ", "Nextest run".style(self.styles.pass))?;
writeln!(
writer,
"ID {} with nextest profile: {}",
run_id.style(self.styles.count),
profile_name.style(self.styles.count),
)?;
write!(writer, "{:>12} ", "Starting".style(self.styles.pass))?;
let count_style = self.styles.count;
let tests_str = plural::tests_str(test_list.run_count());
let binaries_str = plural::binaries_str(test_list.listed_binary_count());
write!(
writer,
"{} {tests_str} across {} {binaries_str}",
test_list.run_count().style(count_style),
test_list.listed_binary_count().style(count_style),
)?;
write_skip_counts(
test_list.skip_counts(),
&self.default_filter,
&self.styles,
writer,
)?;
writeln!(writer)?;
}
TestEventKind::SetupScriptStarted {
index,
total,
script_id,
command,
args,
..
} => {
write!(writer, "{:>12} ", "SETUP".style(self.styles.pass))?;
// index + 1 so that it displays as e.g. "1/2" and "2/2".
write!(writer, "[{:>9}] ", format!("{}/{}", index + 1, total))?;
self.write_setup_script(script_id, command, args, writer)?;
writeln!(writer)?;
}
TestEventKind::SetupScriptSlow {
script_id,
command,
args,
elapsed,
will_terminate,
} => {
if !*will_terminate && self.status_levels.status_level >= StatusLevel::Slow {
write!(writer, "{:>12} ", "SETUP SLOW".style(self.styles.skip))?;
} else if *will_terminate {
write!(writer, "{:>12} ", "TERMINATING".style(self.styles.fail))?;
}
self.write_slow_duration(*elapsed, writer)?;
self.write_setup_script(script_id, command, args, writer)?;
writeln!(writer)?;
}
TestEventKind::SetupScriptFinished {
script_id,
index,
total,
command,
args,
run_status,
..
} => {
self.write_setup_script_status_line(
script_id, *index, *total, command, args, run_status, writer,
)?;
// Always display failing setup script output if it exists. We may change this in
// the future.
if !run_status.result.is_success() {
self.write_setup_script_stdout_stderr(
script_id, command, args, run_status, writer,
)?;
}
}
TestEventKind::TestStarted { test_instance, .. } => {
// In no-capture mode, print out a test start event.
if self.no_capture {
// The spacing is to align test instances.
write!(
writer,
"{:>12} ",
"START".style(self.styles.pass),
)?;
self.write_instance(*test_instance, writer)?;
writeln!(writer)?;
}
}
TestEventKind::TestSlow {
test_instance,
retry_data,
elapsed,
will_terminate,
} => {
if !*will_terminate && self.status_levels.status_level >= StatusLevel::Slow {
if retry_data.total_attempts > 1 {
write!(
writer,
"{:>12} ",
format!("TRY {} SLOW", retry_data.attempt).style(self.styles.skip)
)?;
} else {
write!(writer, "{:>12} ", "SLOW".style(self.styles.skip))?;
}
} else if *will_terminate {
let (required_status_level, style) = if retry_data.is_last_attempt() {
(StatusLevel::Fail, self.styles.fail)
} else {
(StatusLevel::Retry, self.styles.retry)
};
if retry_data.total_attempts > 1
&& self.status_levels.status_level > required_status_level
{
write!(
writer,
"{:>12} ",
format!("TRY {} TRMNTG", retry_data.attempt).style(style)
)?;
} else {
write!(writer, "{:>12} ", "TERMINATING".style(style))?;
};
}
self.write_slow_duration(*elapsed, writer)?;
self.write_instance(*test_instance, writer)?;
writeln!(writer)?;
}
TestEventKind::TestAttemptFailedWillRetry {
test_instance,
run_status,
delay_before_next_attempt,
failure_output,
} => {
if self.status_levels.status_level >= StatusLevel::Retry {
let try_status_string = format!(
"TRY {} {}",
run_status.retry_data.attempt,
short_status_str(run_status.result),
);
write!(
writer,
"{:>12} ",
try_status_string.style(self.styles.retry)
)?;
// Next, print the time taken.
self.write_duration(run_status.time_taken, writer)?;
// Print the name of the test.
self.write_instance(*test_instance, writer)?;
writeln!(writer)?;
// This test is guaranteed to have failed.
assert!(
!run_status.result.is_success(),
"only failing tests are retried"
);
if self.failure_output(*failure_output).is_immediate() {
self.write_output(test_instance, run_status, true, writer)?;
}
// The final output doesn't show retries, so don't store this result in
// final_outputs.
if !delay_before_next_attempt.is_zero() {
// Print a "DELAY {}/{}" line.
let delay_string = format!(
"DELAY {}/{}",
run_status.retry_data.attempt + 1,
run_status.retry_data.total_attempts,
);
write!(writer, "{:>12} ", delay_string.style(self.styles.retry))?;
self.write_duration_by(*delay_before_next_attempt, writer)?;
// Print the name of the test.
self.write_instance(*test_instance, writer)?;
writeln!(writer)?;
}
}
}
TestEventKind::TestRetryStarted {
test_instance,
retry_data:
RetryData {
attempt,
total_attempts,
},
} => {
let retry_string = format!("RETRY {attempt}/{total_attempts}");
write!(writer, "{:>12} ", retry_string.style(self.styles.retry))?;
// Add spacing to align test instances.
write!(writer, "[{:<9}] ", "")?;
// Print the name of the test.
self.write_instance(*test_instance, writer)?;
writeln!(writer)?;
}
TestEventKind::TestFinished {
test_instance,
success_output,
failure_output,
run_statuses,
..
} => {
let describe = run_statuses.describe();
let last_status = run_statuses.last_status();
let test_output_display = match last_status.result.is_success() {
true => self.success_output(*success_output),
false => self.failure_output(*failure_output),
};
let output_on_test_finished = self.status_levels.compute_output_on_test_finished(
test_output_display,
self.cancel_status,
describe.status_level(),
describe.final_status_level(),
);
if output_on_test_finished.write_status_line {
self.write_status_line(*test_instance, describe, writer)?;
}
if output_on_test_finished.show_immediate {
self.write_output(test_instance, last_status, true, writer)?;
}
if let OutputStoreFinal::Yes { display_output } =
output_on_test_finished.store_final
{
self.final_outputs.push((
*test_instance,
FinalOutput::Executed {
run_statuses: run_statuses.clone(),
display_output,
},
));
}
}
TestEventKind::TestSkipped {
test_instance,
reason,
} => {
if self.status_levels.status_level >= StatusLevel::Skip {
self.write_skip_line(*test_instance, writer)?;
}
if self.status_levels.final_status_level >= FinalStatusLevel::Skip {
self.final_outputs
.push((*test_instance, FinalOutput::Skipped(*reason)));
}
}
TestEventKind::RunBeginCancel {
setup_scripts_running,
running,
reason,
} => {
self.cancel_status = self.cancel_status.max(Some(*reason));
write!(
writer,
"{:>12} due to {}",
"Cancelling".style(self.styles.fail),
reason.to_static_str().style(self.styles.fail)
)?;
// At the moment, we can have either setup scripts or tests running, but not both.
if *setup_scripts_running > 0 {
let s = plural::setup_scripts_str(*setup_scripts_running);
write!(
writer,
": {} {s} still running",
setup_scripts_running.style(self.styles.count),
)?;
} else if *running > 0 {
let tests_str = plural::tests_str(*running);
write!(
writer,
": {} {tests_str} still running",
running.style(self.styles.count),
)?;
}
writeln!(writer)?;
}
TestEventKind::RunPaused {
setup_scripts_running,
running,
} => {
write!(
writer,
"{:>12} due to {}",
"Pausing".style(self.styles.pass),
"signal".style(self.styles.count)
)?;
// At the moment, we can have either setup scripts or tests running, but not both.
if *setup_scripts_running > 0 {
let s = plural::setup_scripts_str(*setup_scripts_running);
write!(
writer,
": {} {s} running",
setup_scripts_running.style(self.styles.count),
)?;
} else if *running > 0 {
let tests_str = plural::tests_str(*running);
write!(
writer,
": {} {tests_str} running",
running.style(self.styles.count),
)?;
}
writeln!(writer)?;
}
TestEventKind::RunContinued {
setup_scripts_running,
running,
} => {
write!(
writer,
"{:>12} due to {}",
"Continuing".style(self.styles.pass),
"signal".style(self.styles.count)
)?;
// At the moment, we can have either setup scripts or tests running, but not both.
if *setup_scripts_running > 0 {
let s = plural::setup_scripts_str(*setup_scripts_running);
write!(
writer,
": {} {s} running",
setup_scripts_running.style(self.styles.count),
)?;
} else if *running > 0 {
let tests_str = plural::tests_str(*running);
write!(
writer,
": {} {tests_str} running",
running.style(self.styles.count),
)?;
}
writeln!(writer)?;
}
TestEventKind::RunFinished {
start_time: _start_time,
elapsed,
run_stats,
..
} => {
let stats_summary = run_stats.summarize_final();
let summary_style = match stats_summary {
FinalRunStats::Success => self.styles.pass,
FinalRunStats::NoTestsRun => self.styles.skip,
FinalRunStats::Failed(_) | FinalRunStats::Cancelled(_) => self.styles.fail,
};
write!(
writer,
"------------\n{:>12} ",
"Summary".style(summary_style)
)?;
// Next, print the total time taken.
// * > means right-align.
// * 8 is the number of characters to pad to.
// * .3 means print two digits after the decimal point.
write!(writer, "[{:>8.3?}s] ", elapsed.as_secs_f64())?;
write!(
writer,
"{}",
run_stats.finished_count.style(self.styles.count)
)?;
if run_stats.finished_count != run_stats.initial_run_count {
write!(
writer,
"/{}",
run_stats.initial_run_count.style(self.styles.count)
)?;
}
// Both initial and finished counts must be 1 for the singular form.
let tests_str = plural::tests_plural_if(
run_stats.initial_run_count != 1 || run_stats.finished_count != 1,
);
let mut summary_str = String::new();
write_summary_str(run_stats, &self.styles, &mut summary_str);
writeln!(writer, " {tests_str} run: {summary_str}")?;
// Don't print out test outputs after Ctrl-C, but *do* print them after SIGTERM or
// SIGHUP since those tend to be automated tasks performing kills.
if self.cancel_status < Some(CancelReason::Interrupt) {
// Sort the final outputs for a friendlier experience.
self.final_outputs
.sort_by_key(|(test_instance, final_output)| {
// Use the final status level, reversed (i.e. failing tests are printed at the very end).
(
Reverse(final_output.final_status_level()),
test_instance.sort_key(),
)
});
for (test_instance, final_output) in &*self.final_outputs {
match final_output {
FinalOutput::Skipped(_) => {
self.write_skip_line(*test_instance, writer)?;
}
FinalOutput::Executed {
run_statuses,
display_output,
} => {
let last_status = run_statuses.last_status();
self.write_final_status_line(
*test_instance,
run_statuses.describe(),
writer,
)?;
if *display_output {
self.write_output(test_instance, last_status, false, writer)?;
}
}
}
}
}
// Print out warnings at the end, if any.
write_final_warnings(stats_summary, self.cancel_status, &self.styles, writer)?;
}
}
Ok(())
}
fn write_skip_line(
&self,
test_instance: TestInstance<'a>,
writer: &mut dyn Write,
) -> io::Result<()> {
write!(writer, "{:>12} ", "SKIP".style(self.styles.skip))?;
// same spacing [ 0.034s]
write!(writer, "[ ] ")?;
self.write_instance(test_instance, writer)?;
writeln!(writer)?;
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn write_setup_script_status_line(
&self,
script_id: &ScriptId,
index: usize,
total: usize,
command: &str,
args: &[String],
status: &SetupScriptExecuteStatus,
writer: &mut dyn Write,
) -> io::Result<()> {
match status.result {
ExecutionResult::Pass => {
write!(writer, "{:>12} ", "SETUP PASS".style(self.styles.pass))?;
}
ExecutionResult::Leak => {
write!(writer, "{:>12} ", "SETUP LEAK".style(self.styles.skip))?;
}
other => {
let status_str = short_status_str(other);
write!(
writer,
"{:>12} ",
format!("SETUP {status_str}").style(self.styles.fail),
)?;
}
}
write!(writer, "[{:>9}] ", format!("{}/{}", index + 1, total))?;
self.write_setup_script(script_id, command, args, writer)?;
writeln!(writer)?;
Ok(())
}
fn write_status_line(
&self,
test_instance: TestInstance<'a>,
describe: ExecutionDescription<'_>,
writer: &mut dyn Write,
) -> io::Result<()> {
let last_status = describe.last_status();
match describe {
ExecutionDescription::Success { .. } => {
if last_status.result == ExecutionResult::Leak {
write!(writer, "{:>12} ", "LEAK".style(self.styles.skip))?;
} else {
write!(writer, "{:>12} ", "PASS".style(self.styles.pass))?;
}
}
ExecutionDescription::Flaky { .. } => {
// Use the skip color to also represent a flaky test.
write!(
writer,
"{:>12} ",
format!("TRY {} PASS", last_status.retry_data.attempt).style(self.styles.skip)
)?;
}
ExecutionDescription::Failure { .. } => {
if last_status.retry_data.attempt == 1 {
write!(
writer,
"{:>12} ",
status_str(last_status.result).style(self.styles.fail)
)?;
} else {
let status_str = short_status_str(last_status.result);
write!(
writer,
"{:>12} ",
format!("TRY {} {}", last_status.retry_data.attempt, status_str)
.style(self.styles.fail)
)?;
}
}
};
// Next, print the time taken.
self.write_duration(last_status.time_taken, writer)?;
// Print the name of the test.
self.write_instance(test_instance, writer)?;
writeln!(writer)?;
// On Windows, also print out the exception if available.
#[cfg(windows)]
if let ExecutionResult::Fail {
abort_status: Some(AbortStatus::WindowsNtStatus(nt_status)),
leaked: _,
} = last_status.result
{
self.write_windows_message_line(nt_status, writer)?;
}
Ok(())
}
fn write_final_status_line(
&self,
test_instance: TestInstance<'a>,
describe: ExecutionDescription<'_>,
writer: &mut dyn Write,
) -> io::Result<()> {
let last_status = describe.last_status();
match describe {
ExecutionDescription::Success { .. } => {
match (last_status.is_slow, last_status.result) {
(true, ExecutionResult::Leak) => {
write!(writer, "{:>12} ", "SLOW + LEAK".style(self.styles.skip))?;
}
(true, _) => {
write!(writer, "{:>12} ", "SLOW".style(self.styles.skip))?;
}
(false, ExecutionResult::Leak) => {
write!(writer, "{:>12} ", "LEAK".style(self.styles.skip))?;
}
(false, _) => {
write!(writer, "{:>12} ", "PASS".style(self.styles.pass))?;
}
}
}
ExecutionDescription::Flaky { .. } => {
// Use the skip color to also represent a flaky test.
write!(
writer,
"{:>12} ",
format!(
"FLAKY {}/{}",
last_status.retry_data.attempt, last_status.retry_data.total_attempts
)
.style(self.styles.skip)
)?;
}
ExecutionDescription::Failure { .. } => {
if last_status.retry_data.attempt == 1 {
write!(
writer,
"{:>12} ",
status_str(last_status.result).style(self.styles.fail)
)?;
} else {
let status_str = short_status_str(last_status.result);
write!(
writer,
"{:>12} ",
format!("TRY {} {}", last_status.retry_data.attempt, status_str)
.style(self.styles.fail)
)?;
}
}
};
// Next, print the time taken.
self.write_duration(last_status.time_taken, writer)?;
// Print the name of the test.
self.write_instance(test_instance, writer)?;
writeln!(writer)?;
// On Windows, also print out the exception if available.
#[cfg(windows)]
if let ExecutionResult::Fail {
abort_status: Some(AbortStatus::WindowsNtStatus(nt_status)),
leaked: _,
} = last_status.result
{
self.write_windows_message_line(nt_status, writer)?;
}
Ok(())
}
fn write_instance(&self, instance: TestInstance<'a>, writer: &mut dyn Write) -> io::Result<()> {
write!(
writer,
"{:>width$} ",
instance
.suite_info
.binary_id
.style(self.styles.list_styles.binary_id),
width = self.binary_id_width
)?;
io_write_test_name(instance.name, &self.styles.list_styles, writer)
}
fn write_setup_script(
&self,
script_id: &ScriptId,
command: &str,
args: &[String],
writer: &mut dyn Write,
) -> io::Result<()> {
let full_command =
shell_words::join(std::iter::once(command).chain(args.iter().map(|arg| arg.as_ref())));
write!(
writer,
"{}: {}",
script_id.style(self.styles.script_id),
full_command
)
}
fn write_duration(&self, duration: Duration, writer: &mut dyn Write) -> io::Result<()> {
// * > means right-align.
// * 8 is the number of characters to pad to.
// * .3 means print three digits after the decimal point.
write!(writer, "[{:>8.3?}s] ", duration.as_secs_f64())
}
fn write_duration_by(&self, duration: Duration, writer: &mut dyn Write) -> io::Result<()> {
// * > means right-align.
// * 7 is the number of characters to pad to.
// * .3 means print three digits after the decimal point.
write!(writer, "by {:>7.3?}s ", duration.as_secs_f64())
}
fn write_slow_duration(&self, duration: Duration, writer: &mut dyn Write) -> io::Result<()> {
// Inside the curly braces:
// * > means right-align.
// * 7 is the number of characters to pad to.
// * .3 means print three digits after the decimal point.
write!(writer, "[>{:>7.3?}s] ", duration.as_secs_f64())
}
#[cfg(windows)]
fn write_windows_message_line(
&self,
nt_status: windows_sys::Win32::Foundation::NTSTATUS,
writer: &mut dyn Write,
) -> io::Result<()> {
write!(writer, "{:>12} ", "Message".style(self.styles.fail))?;
write!(writer, "[ ] ")?;
writeln!(
writer,
"code {}",
crate::helpers::display_nt_status(nt_status)
)?;
Ok(())
}
fn write_setup_script_stdout_stderr(
&self,
script_id: &ScriptId,
command: &str,
args: &[String],
run_status: &SetupScriptExecuteStatus,
writer: &mut dyn Write,
) -> io::Result<()> {
let (header_style, _output_style) = if run_status.result.is_success() {
(self.styles.pass, self.styles.pass_output)
} else {
(self.styles.fail, self.styles.fail_output)
};
if let Some(stdout) = &run_status.output.stdout {
if self.display_empty_outputs || !stdout.is_empty() {
write!(writer, "\n{}", "--- ".style(header_style))?;
write!(writer, "{:21}", "STDOUT:".style(header_style))?;
self.write_setup_script(script_id, command, args, writer)?;
writeln!(writer, "{}", " ---".style(header_style))?;
self.write_test_single_output(stdout, writer)?;
}
}
if let Some(stderr) = &run_status.output.stderr {
if self.display_empty_outputs || !stderr.is_empty() {
write!(writer, "\n{}", "--- ".style(header_style))?;
write!(writer, "{:21}", "STDERR:".style(header_style))?;
self.write_setup_script(script_id, command, args, writer)?;
writeln!(writer, "{}", " ---".style(header_style))?;
self.write_test_single_output(stderr, writer)?;
}
}
writeln!(writer)
}
fn write_output(
&self,
test_instance: &TestInstance<'a>,
run_status: &ExecuteStatus,
is_retry: bool,
writer: &mut dyn Write,
) -> io::Result<()> {
let (header_style, _output_style) = if is_retry {
(self.styles.retry, self.styles.retry_output)
} else if run_status.result.is_success() {
(self.styles.pass, self.styles.pass_output)
} else {
(self.styles.fail, self.styles.fail_output)
};
match &run_status.output {
TestExecutionOutput::Output(output) => {
let description = if self.styles.is_colorized {
output.heuristic_extract_description(run_status.result)
} else {
None
};
match output {
TestOutput::Split(split) => {
if let Some(stdout) = &split.stdout {
if self.display_empty_outputs || !stdout.is_empty() {
write!(writer, "\n{}", "--- ".style(header_style))?;
let out_len =
self.write_attempt(run_status, header_style, writer)?;
// The width is to align test instances.
write!(
writer,
"{:width$}",
"STDOUT:".style(header_style),
width = (21 - out_len)
)?;
self.write_instance(*test_instance, writer)?;
writeln!(writer, "{}", " ---".style(header_style))?;
self.write_test_single_output_with_description(
stdout,
description.and_then(|d| d.stdout_subslice()),
writer,
)?;
}
}
if let Some(stderr) = &split.stderr {
if self.display_empty_outputs || !stderr.is_empty() {
write!(writer, "\n{}", "--- ".style(header_style))?;
let out_len =
self.write_attempt(run_status, header_style, writer)?;
// The width is to align test instances.
write!(
writer,
"{:width$}",
"STDERR:".style(header_style),
width = (21 - out_len)
)?;
self.write_instance(*test_instance, writer)?;
writeln!(writer, "{}", " ---".style(header_style))?;
self.write_test_single_output_with_description(
stderr,
description.and_then(|d| d.stderr_subslice()),
writer,
)?;
}
}
}
TestOutput::Combined { output } => {
if self.display_empty_outputs || !output.is_empty() {
write!(writer, "\n{}", "--- ".style(header_style))?;
let out_len = self.write_attempt(run_status, header_style, writer)?;
// The width is to align test instances.
write!(
writer,
"{:width$}",
"OUTPUT:".style(header_style),
width = (21 - out_len)
)?;
self.write_instance(*test_instance, writer)?;
writeln!(writer, "{}", " ---".style(header_style))?;
self.write_test_single_output_with_description(
output,
description.and_then(|d| d.combined_subslice()),
writer,
)?;
}
}
}
}
TestExecutionOutput::ExecFail { description, .. } => {
write!(writer, "\n{}", "--- ".style(header_style))?;
let out_len = self.write_attempt(run_status, header_style, writer)?;
// The width is to align test instances.
write!(
writer,
"{:width$}",
"EXECFAIL:".style(header_style),
width = (21 - out_len)
)?;
self.write_instance(*test_instance, writer)?;
writeln!(writer, "{}", " ---".style(header_style))?;
writeln!(writer, "{description}")?;
}
}
writeln!(writer)
}
/// Writes a test output to the writer.
fn write_test_single_output(
&self,
output: &ChildSingleOutput,
writer: &mut dyn Write,
) -> io::Result<()> {
// SAFETY: The description is not provided.
self.write_test_single_output_with_description(output, None, writer)
}
/// Writes a test output to the writer, along with optionally a subslice of the output to
/// highlight.
///
/// The description must be a subslice of the output.
fn write_test_single_output_with_description(
&self,
output: &ChildSingleOutput,
description: Option<ByteSubslice<'_>>,
writer: &mut dyn Write,
) -> io::Result<()> {
if self.styles.is_colorized {
if let Some(subslice) = description {
write_output_with_highlight(&output.buf, subslice, &self.styles.fail, writer)?;
} else {
// Output the text without stripping ANSI escapes, then reset the color afterwards
// in case the output is malformed.
writer.write_all(&output.buf)?;
writer.write_all(RESET_COLOR)?;
}
} else {
// Strip ANSI escapes from the output if nextest itself isn't colorized.
let mut no_color = strip_ansi_escapes::Writer::new(writer);
no_color.write_all(&output.buf)?;
}
Ok(())
}
// Returns the number of characters written out to the screen.
fn write_attempt(
&self,
run_status: &ExecuteStatus,
style: Style,
writer: &mut dyn Write,
) -> io::Result<usize> {
if run_status.retry_data.total_attempts > 1 {
// 3 for 'TRY' + 1 for ' ' + length of the current attempt + 1 for following space.
let attempt_str = format!("{}", run_status.retry_data.attempt);
let out_len = 3 + 1 + attempt_str.len() + 1;
write!(
writer,
"{} {} ",
"TRY".style(style),
attempt_str.style(style)
)?;
Ok(out_len)
} else {
Ok(0)
}
}
fn success_output(&self, test_setting: TestOutputDisplay) -> TestOutputDisplay {
self.force_success_output.unwrap_or(test_setting)
}
fn failure_output(&self, test_setting: TestOutputDisplay) -> TestOutputDisplay {
self.force_failure_output.unwrap_or(test_setting)
}
}
impl<'a> fmt::Debug for TestReporter<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("TestReporter")
.field("stdout", &"BufferWriter { .. }")
.field("stderr", &"BufferWriter { .. }")
.finish()
}
}
const RESET_COLOR: &[u8] = b"\x1b[0m";
fn write_output_with_highlight(
output: &[u8],
ByteSubslice { slice, start }: ByteSubslice,
highlight_style: &Style,
mut writer: &mut dyn Write,
) -> io::Result<()> {
let end = start + highlight_end(slice);
// Output the start and end of the test without stripping ANSI escapes, then reset
// the color afterwards in case the output is malformed.
writer.write_all(&output[..start])?;
writer.write_all(RESET_COLOR)?;
// Some systems (e.g. GitHub Actions, Buildomat) don't handle multiline ANSI
// coloring -- they reset colors after each line. To work around that,
// we reset and re-apply colors for each line.
for line in output[start..end].lines_with_terminator() {
write!(writer, "{}", FmtPrefix(highlight_style))?;
// Write everything before the newline, stripping ANSI escapes.
let mut no_color = strip_ansi_escapes::Writer::new(writer);
let trimmed = line.trim_end_with(|c| c == '\n' || c == '\r');
no_color.write_all(trimmed.as_bytes())?;
writer = no_color.into_inner()?;
// End coloring.
write!(writer, "{}", FmtSuffix(highlight_style))?;
// Now write the newline, if present.
writer.write_all(&line[trimmed.len()..])?;
}
// `end` is guaranteed to be within the bounds of `output.buf`. (It is actually safe
// for it to be equal to `output.buf.len()` -- it gets treated as an empty list in
// that case.)
writer.write_all(&output[end..])?;
writer.write_all(RESET_COLOR)?;
Ok(())
}
struct FmtPrefix<'a>(&'a Style);
impl fmt::Display for FmtPrefix<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_prefix(f)
}
}
struct FmtSuffix<'a>(&'a Style);
impl fmt::Display for FmtSuffix<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_suffix(f)
}
}
fn write_skip_counts(
skip_counts: &SkipCounts,
default_filter: &CompiledDefaultFilter,
styles: &Styles,
writer: &mut dyn Write,
) -> io::Result<()> {
if skip_counts.skipped_tests > 0 || skip_counts.skipped_binaries > 0 {
write!(writer, " (")?;
write_skip_counts_impl(
skip_counts.skipped_tests,
skip_counts.skipped_binaries,
styles,
writer,
)?;
// Were all tests and binaries that were skipped, skipped due to being in the
// default set?
if skip_counts.skipped_tests == skip_counts.skipped_tests_default_filter
&& skip_counts.skipped_binaries == skip_counts.skipped_binaries_default_filter
{
write!(
writer,
" {} via {}",
"skipped".style(styles.skip),
default_filter.display_config(styles.count)
)?;
} else {
write!(writer, " {}", "skipped".style(styles.skip))?;
// Were *any* tests in the default set?
if skip_counts.skipped_binaries_default_filter > 0
|| skip_counts.skipped_tests_default_filter > 0
{
write!(writer, ", including ")?;
write_skip_counts_impl(
skip_counts.skipped_tests_default_filter,
skip_counts.skipped_binaries_default_filter,
styles,
writer,
)?;
write!(
writer,
" via {}",
default_filter.display_config(styles.count)
)?;
}
}
write!(writer, ")")?;
}
Ok(())
}
fn write_skip_counts_impl(
skipped_tests: usize,
skipped_binaries: usize,
styles: &Styles,
writer: &mut dyn Write,
) -> io::Result<()> {
// X tests and Y binaries skipped, or X tests skipped, or Y binaries skipped.
if skipped_tests > 0 && skipped_binaries > 0 {
write!(
writer,
"{} {} and {} {}",
skipped_tests.style(styles.count),
plural::tests_str(skipped_tests),
skipped_binaries.style(styles.count),
plural::binaries_str(skipped_binaries),
)?;
} else if skipped_tests > 0 {
write!(
writer,
"{} {}",
skipped_tests.style(styles.count),
plural::tests_str(skipped_tests),
)?;
} else if skipped_binaries > 0 {
write!(
writer,
"{} {}",
skipped_binaries.style(styles.count),
plural::binaries_str(skipped_binaries),
)?;
}
Ok(())
}
struct StatusLevels {
status_level: StatusLevel,
final_status_level: FinalStatusLevel,
}
impl StatusLevels {
fn compute_output_on_test_finished(
&self,
display: TestOutputDisplay,
cancel_status: Option<CancelReason>,
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) -> OutputOnTestFinished {
let write_status_line = self.status_level >= test_status_level;
let is_immediate = display.is_immediate();
// We store entries in the final output map if either the final status level is high enough or
// if `display` says we show the output at the end.
let is_final = display.is_final() || self.final_status_level >= test_final_status_level;
// This table is tested below. The basic invariant is that we generally follow what
// is_immediate and is_final suggests, except:
//
// - if the run is cancelled due to a non-interrupt signal, we display test output at most
// once.
// - if the run is cancelled due to an interrupt, we hide the output because dumping a bunch
// of output at the end is likely to not be helpful (though in the future we may want to
// at least dump outputs into files and write their names out, or whenever nextest gains
// the ability to replay test runs to be able to display it then.)
//
// is_immediate is_final cancel_status | show_immediate store_final
//
// false false <= Signal | false false
// false true <= Signal | false true [1]
// true false <= Signal | true false [1]
// true true < Signal | true true
// true true Signal | true false [2]
// * * Interrupt | false false
//
// [1] In non-interrupt cases, we want to display output if specified once.
//
// [2] If there's a signal, we shouldn't display output twice at the end since it's
// redundant -- instead, just show the output as part of the immediate display.
let show_immediate = is_immediate && cancel_status <= Some(CancelReason::Signal);
let store_final = if is_final && cancel_status < Some(CancelReason::Signal)
|| !is_immediate && is_final && cancel_status == Some(CancelReason::Signal)
{
OutputStoreFinal::Yes {
display_output: display.is_final(),
}
} else if is_immediate && is_final && cancel_status == Some(CancelReason::Signal) {
// In this special case, we already display the output once as the test is being
// cancelled, so don't display it again at the end since that's redundant.
OutputStoreFinal::Yes {
display_output: false,
}
} else {
OutputStoreFinal::No
};
OutputOnTestFinished {
write_status_line,
show_immediate,
store_final,
}
}
}
#[derive(Debug, PartialEq, Eq)]
struct OutputOnTestFinished {
write_status_line: bool,
show_immediate: bool,
store_final: OutputStoreFinal,
}
#[derive(Debug, PartialEq, Eq)]
enum OutputStoreFinal {
/// Do not store the output.
No,
/// Store the output. display_output controls whether stdout and stderr should actually be
/// displayed at the end.
Yes { display_output: bool },
}
fn status_str(result: ExecutionResult) -> Cow<'static, str> {
// Max 12 characters here.
match result {
#[cfg(unix)]
ExecutionResult::Fail {
abort_status: Some(AbortStatus::UnixSignal(sig)),
leaked: _,
} => match crate::helpers::signal_str(sig) {
Some(s) => format!("SIG{s}").into(),
None => format!("ABORT SIG {sig}").into(),
},
#[cfg(windows)]
ExecutionResult::Fail {
abort_status: Some(AbortStatus::WindowsNtStatus(_)),
leaked: _,
} => {
// Going to print out the full error message on the following line -- just "ABORT" will
// do for now.
"ABORT".into()
}
ExecutionResult::Fail {
abort_status: None,
leaked: true,
} => "FAIL + LEAK".into(),
ExecutionResult::Fail {
abort_status: None,
leaked: false,
} => "FAIL".into(),
ExecutionResult::ExecFail => "XFAIL".into(),
ExecutionResult::Pass => "PASS".into(),
ExecutionResult::Leak => "LEAK".into(),
ExecutionResult::Timeout => "TIMEOUT".into(),
}
}
fn short_status_str(result: ExecutionResult) -> Cow<'static, str> {
// Use shorter strings for this (max 6 characters).
match result {
#[cfg(unix)]
ExecutionResult::Fail {
abort_status: Some(AbortStatus::UnixSignal(sig)),
leaked: _,
} => match crate::helpers::signal_str(sig) {
Some(s) => s.into(),
None => format!("SIG {sig}").into(),
},
#[cfg(windows)]
ExecutionResult::Fail {
abort_status: Some(AbortStatus::WindowsNtStatus(_)),
leaked: _,
} => {
// Going to print out the full error message on the following line -- just "ABORT" will
// do for now.
"ABORT".into()
}
ExecutionResult::Fail {
abort_status: None,
leaked: _,
} => "FAIL".into(),
ExecutionResult::ExecFail => "XFAIL".into(),
ExecutionResult::Pass => "PASS".into(),
ExecutionResult::Leak => "LEAK".into(),
ExecutionResult::Timeout => "TMT".into(),
}
}
fn write_final_warnings(
final_stats: FinalRunStats,
cancel_status: Option<CancelReason>,
styles: &Styles,
writer: &mut dyn Write,
) -> io::Result<()> {
match final_stats {
FinalRunStats::Failed(RunStatsFailureKind::Test {
initial_run_count,
not_run,
})
| FinalRunStats::Cancelled(RunStatsFailureKind::Test {
initial_run_count,
not_run,
}) if not_run > 0 => {
if cancel_status == Some(CancelReason::TestFailure) {
writeln!(
writer,
"{}: {}/{} {} {} not run due to {} (run with {} to run all tests)",
"warning".style(styles.skip),
not_run.style(styles.count),
initial_run_count.style(styles.count),
plural::tests_plural_if(initial_run_count != 1 || not_run != 1),
plural::were_plural_if(initial_run_count != 1 || not_run != 1),
CancelReason::TestFailure.to_static_str().style(styles.skip),
"--no-fail-fast".style(styles.count),
)?;
} else {
let due_to_reason = match cancel_status {
Some(reason) => {
format!(" due to {}", reason.to_static_str().style(styles.skip))
}
None => "".to_string(),
};
writeln!(
writer,
"{}: {}/{} {} {} not run{}",
"warning".style(styles.skip),
not_run.style(styles.count),
initial_run_count.style(styles.count),
plural::tests_plural_if(initial_run_count != 1 || not_run != 1),
plural::were_plural_if(initial_run_count != 1 || not_run != 1),
due_to_reason,
)?;
}
}
_ => {}
}
Ok(())
}
/// A test event.
///
/// Events are produced by a [`TestRunner`](crate::runner::TestRunner) and consumed by a
/// [`TestReporter`].
#[derive(Clone, Debug)]
pub struct TestEvent<'a> {
/// The time at which the event was generated, including the offset from UTC.
pub timestamp: DateTime<FixedOffset>,
/// The amount of time elapsed since the start of the test run.
pub elapsed: Duration,
/// The kind of test event this is.
pub kind: TestEventKind<'a>,
}
/// The kind of test event this is.
///
/// Forms part of [`TestEvent`].
#[derive(Clone, Debug)]
pub enum TestEventKind<'a> {
/// The test run started.
RunStarted {
/// The list of tests that will be run.
///
/// The methods on the test list indicate the number of tests that will be run.
test_list: &'a TestList<'a>,
/// The UUID for this run.
run_id: ReportUuid,
/// The nextest profile chosen for this run.
profile_name: String,
/// The command-line arguments for the process.
cli_args: Vec<String>,
},
/// A setup script started.
SetupScriptStarted {
/// The setup script index.
index: usize,
/// The total number of setup scripts.
total: usize,
/// The script ID.
script_id: ScriptId,
/// The command to run.
command: &'a str,
/// The arguments to the command.
args: &'a [String],
/// True if some output from the setup script is being passed through.
no_capture: bool,
},
/// A setup script was slow.
SetupScriptSlow {
/// The script ID.
script_id: ScriptId,
/// The command to run.
command: &'a str,
/// The arguments to the command.
args: &'a [String],
/// The amount of time elapsed since the start of execution.
elapsed: Duration,
/// True if the script has hit its timeout and is about to be terminated.
will_terminate: bool,
},
/// A setup script completed execution.
SetupScriptFinished {
/// The setup script index.
index: usize,
/// The total number of setup scripts.
total: usize,
/// The script ID.
script_id: ScriptId,
/// The command to run.
command: &'a str,
/// The arguments to the command.
args: &'a [String],
/// True if some output from the setup script was passed through.
no_capture: bool,
/// The execution status of the setup script.
run_status: SetupScriptExecuteStatus,
},
// TODO: add events for BinaryStarted and BinaryFinished? May want a slightly different way to
// do things, maybe a couple of reporter traits (one for the run as a whole and one for each
// binary).
/// A test started running.
TestStarted {
/// The test instance that was started.
test_instance: TestInstance<'a>,
/// Current run statistics so far.
current_stats: RunStats,
/// The number of tests currently running, including this one.
running: usize,
/// The cancel status of the run. This is None if the run is still ongoing.
cancel_state: Option<CancelReason>,
},
/// A test was slower than a configured soft timeout.
TestSlow {
/// The test instance that was slow.
test_instance: TestInstance<'a>,
/// Retry data.
retry_data: RetryData,
/// The amount of time that has elapsed since the beginning of the test.
elapsed: Duration,
/// True if the test has hit its timeout and is about to be terminated.
will_terminate: bool,
},
/// A test attempt failed and will be retried in the future.
///
/// This event does not occur on the final run of a failing test.
TestAttemptFailedWillRetry {
/// The test instance that is being retried.
test_instance: TestInstance<'a>,
/// The status of this attempt to run the test. Will never be success.
run_status: ExecuteStatus,
/// The delay before the next attempt to run the test.
delay_before_next_attempt: Duration,
/// Whether failure outputs are printed out.
failure_output: TestOutputDisplay,
},
/// A retry has started.
TestRetryStarted {
/// The test instance that is being retried.
test_instance: TestInstance<'a>,
/// Data related to retries.
retry_data: RetryData,
},
/// A test finished running.
TestFinished {
/// The test instance that finished running.
test_instance: TestInstance<'a>,
/// Test setting for success output.
success_output: TestOutputDisplay,
/// Test setting for failure output.
failure_output: TestOutputDisplay,
/// Whether the JUnit report should store success output for this test.
junit_store_success_output: bool,
/// Whether the JUnit report should store failure output for this test.
junit_store_failure_output: bool,
/// Information about all the runs for this test.
run_statuses: ExecutionStatuses,
/// Current statistics for number of tests so far.
current_stats: RunStats,
/// The number of tests that are currently running, excluding this one.
running: usize,
/// The cancel status of the run. This is None if the run is still ongoing.
cancel_state: Option<CancelReason>,
},
/// A test was skipped.
TestSkipped {
/// The test instance that was skipped.
test_instance: TestInstance<'a>,
/// The reason this test was skipped.
reason: MismatchReason,
},
/// A cancellation notice was received.
RunBeginCancel {
/// The number of setup scripts still running.
setup_scripts_running: usize,
/// The number of tests still running.
running: usize,
/// The reason this run was cancelled.
reason: CancelReason,
},
/// A SIGTSTP event was received and the run was paused.
RunPaused {
/// The number of setup scripts running.
setup_scripts_running: usize,
/// The number of tests currently running.
running: usize,
},
/// A SIGCONT event was received and the run is being continued.
RunContinued {
/// The number of setup scripts that will be started up again.
setup_scripts_running: usize,
/// The number of tests that will be started up again.
running: usize,
},
/// The test run finished.
RunFinished {
/// The unique ID for this run.
run_id: ReportUuid,
/// The time at which the run was started.
start_time: DateTime<FixedOffset>,
/// The amount of time it took for the tests to run.
elapsed: Duration,
/// Statistics for the run.
run_stats: RunStats,
},
}
// Note: the order here matters -- it indicates severity of cancellation
/// The reason why a test run is being cancelled.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub enum CancelReason {
/// A setup script failed.
SetupScriptFailure,
/// A test failed and --no-fail-fast wasn't specified.
TestFailure,
/// An error occurred while reporting results.
ReportError,
/// A termination signal (on Unix, SIGTERM or SIGHUP) was received.
Signal,
/// An interrupt (on Unix, Ctrl-C) was received.
Interrupt,
}
impl CancelReason {
pub(crate) fn to_static_str(self) -> &'static str {
match self {
CancelReason::SetupScriptFailure => "setup script failure",
CancelReason::TestFailure => "test failure",
CancelReason::ReportError => "reporting error",
CancelReason::Signal => "signal",
CancelReason::Interrupt => "interrupt",
}
}
}
#[derive(Debug, Default)]
struct Styles {
is_colorized: bool,
count: Style,
pass: Style,
retry: Style,
fail: Style,
pass_output: Style,
retry_output: Style,
fail_output: Style,
skip: Style,
script_id: Style,
list_styles: crate::list::Styles,
}
impl Styles {
fn colorize(&mut self) {
self.is_colorized = true;
self.count = Style::new().bold();
self.pass = Style::new().green().bold();
self.retry = Style::new().magenta().bold();
self.fail = Style::new().red().bold();
self.pass_output = Style::new().green();
self.retry_output = Style::new().magenta();
self.fail_output = Style::new().magenta();
self.skip = Style::new().yellow().bold();
self.script_id = Style::new().blue().bold();
self.list_styles.colorize();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
config::{CompiledDefaultFilterSection, NextestConfig},
platform::BuildPlatforms,
reporter::structured::StructuredReporter,
};
use nextest_filtering::CompiledExpr;
use test_strategy::proptest;
// ---
// The proptests here are probabilistically exhaustive, and it's just easier to express them
// as property-based tests. We could also potentially use a model checker like Kani here.
// ---
#[proptest(cases = 64)]
fn on_test_finished_dont_write_status_line(
display: TestOutputDisplay,
cancel_status: Option<CancelReason>,
#[filter(StatusLevel::Pass < #test_status_level)] test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
cancel_status,
test_status_level,
test_final_status_level,
);
assert!(!actual.write_status_line);
}
#[proptest(cases = 64)]
fn on_test_finished_write_status_line(
display: TestOutputDisplay,
cancel_status: Option<CancelReason>,
#[filter(StatusLevel::Pass >= #test_status_level)] test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
cancel_status,
test_status_level,
test_final_status_level,
);
assert!(actual.write_status_line);
}
#[proptest(cases = 64)]
fn on_test_finished_with_interrupt(
// We always hide output on interrupt.
display: TestOutputDisplay,
// cancel_status is fixed to Interrupt.
// In this case, the status levels are not relevant for is_immediate and is_final.
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
Some(CancelReason::Interrupt),
test_status_level,
test_final_status_level,
);
assert!(!actual.show_immediate);
assert_eq!(actual.store_final, OutputStoreFinal::No);
}
#[proptest(cases = 64)]
fn on_test_finished_dont_show_immediate(
#[filter(!#display.is_immediate())] display: TestOutputDisplay,
cancel_status: Option<CancelReason>,
// The status levels are not relevant for show_immediate.
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
cancel_status,
test_status_level,
test_final_status_level,
);
assert!(!actual.show_immediate);
}
#[proptest(cases = 64)]
fn on_test_finished_show_immediate(
#[filter(#display.is_immediate())] display: TestOutputDisplay,
#[filter(#cancel_status <= Some(CancelReason::Signal))] cancel_status: Option<CancelReason>,
// The status levels are not relevant for show_immediate.
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
cancel_status,
test_status_level,
test_final_status_level,
);
assert!(actual.show_immediate);
}
// Where we don't store final output: if display.is_final() is false, and if the test final
// status level is too high.
#[proptest(cases = 64)]
fn on_test_finished_dont_store_final(
#[filter(!#display.is_final())] display: TestOutputDisplay,
cancel_status: Option<CancelReason>,
// The status level is not relevant for store_final.
test_status_level: StatusLevel,
// But the final status level is.
#[filter(FinalStatusLevel::Fail < #test_final_status_level)]
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
cancel_status,
test_status_level,
test_final_status_level,
);
assert_eq!(actual.store_final, OutputStoreFinal::No);
}
// Case 1 where we store final output: if display is exactly TestOutputDisplay::Final, and if
// the cancel status is not Interrupt.
#[proptest(cases = 64)]
fn on_test_finished_store_final_1(
#[filter(#cancel_status <= Some(CancelReason::Signal))] cancel_status: Option<CancelReason>,
// In this case, it isn't relevant what test_status_level and test_final_status_level are.
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
TestOutputDisplay::Final,
cancel_status,
test_status_level,
test_final_status_level,
);
assert_eq!(
actual.store_final,
OutputStoreFinal::Yes {
display_output: true
}
);
}
// Case 2 where we store final output: if display is TestOutputDisplay::ImmediateFinal and the
// cancel status is not Signal or Interrupt
#[proptest(cases = 64)]
fn on_test_finished_store_final_2(
#[filter(#cancel_status < Some(CancelReason::Signal))] cancel_status: Option<CancelReason>,
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
TestOutputDisplay::ImmediateFinal,
cancel_status,
test_status_level,
test_final_status_level,
);
assert_eq!(
actual.store_final,
OutputStoreFinal::Yes {
display_output: true
}
);
}
// Case 3 where we store final output: if display is TestOutputDisplay::ImmediateFinal and the
// cancel status is exactly Signal. In this special case, we don't display the output.
#[proptest(cases = 64)]
fn on_test_finished_store_final_3(
test_status_level: StatusLevel,
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
TestOutputDisplay::ImmediateFinal,
Some(CancelReason::Signal),
test_status_level,
test_final_status_level,
);
assert_eq!(
actual.store_final,
OutputStoreFinal::Yes {
display_output: false,
}
);
}
// Case 4: if display.is_final() is *false* but the test_final_status_level is low enough.
#[proptest(cases = 64)]
fn on_test_finished_store_final_4(
#[filter(!#display.is_final())] display: TestOutputDisplay,
#[filter(#cancel_status <= Some(CancelReason::Signal))] cancel_status: Option<CancelReason>,
// The status level is not relevant for store_final.
test_status_level: StatusLevel,
// But the final status level is.
#[filter(FinalStatusLevel::Fail >= #test_final_status_level)]
test_final_status_level: FinalStatusLevel,
) {
let status_levels = StatusLevels {
status_level: StatusLevel::Pass,
final_status_level: FinalStatusLevel::Fail,
};
let actual = status_levels.compute_output_on_test_finished(
display,
cancel_status,
test_status_level,
test_final_status_level,
);
assert_eq!(
actual.store_final,
OutputStoreFinal::Yes {
display_output: false,
}
);
}
// ---
#[test]
fn test_write_skip_counts() {
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 1,
skipped_tests_default_filter: 1,
skipped_binaries: 0,
skipped_binaries_default_filter: 0,
}, false), @" (1 test skipped via profile.my-profile.default-filter)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 2,
skipped_tests_default_filter: 2,
skipped_binaries: 0,
skipped_binaries_default_filter: 0,
}, false), @" (2 tests skipped via profile.my-profile.default-filter)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 1,
skipped_tests_default_filter: 0,
skipped_binaries: 0,
skipped_binaries_default_filter: 0,
}, false), @" (1 test skipped)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 2,
skipped_tests_default_filter: 0,
skipped_binaries: 0,
skipped_binaries_default_filter: 0,
}, false), @" (2 tests skipped)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 0,
skipped_tests_default_filter: 0,
skipped_binaries: 1,
skipped_binaries_default_filter: 1,
}, false), @" (1 binary skipped via profile.my-profile.default-filter)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 0,
skipped_tests_default_filter: 0,
skipped_binaries: 2,
skipped_binaries_default_filter: 2,
}, true), @" (2 binaries skipped via default-filter in profile.my-profile.overrides)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 0,
skipped_tests_default_filter: 0,
skipped_binaries: 1,
skipped_binaries_default_filter: 0,
}, false), @" (1 binary skipped)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 0,
skipped_tests_default_filter: 0,
skipped_binaries: 2,
skipped_binaries_default_filter: 0,
}, false), @" (2 binaries skipped)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 1,
skipped_tests_default_filter: 1,
skipped_binaries: 1,
skipped_binaries_default_filter: 1,
}, true), @" (1 test and 1 binary skipped via default-filter in profile.my-profile.overrides)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 2,
skipped_tests_default_filter: 2,
skipped_binaries: 3,
skipped_binaries_default_filter: 3,
}, false), @" (2 tests and 3 binaries skipped via profile.my-profile.default-filter)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 1,
skipped_tests_default_filter: 0,
skipped_binaries: 1,
skipped_binaries_default_filter: 0,
}, false), @" (1 test and 1 binary skipped)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 2,
skipped_tests_default_filter: 0,
skipped_binaries: 3,
skipped_binaries_default_filter: 0,
}, false), @" (2 tests and 3 binaries skipped)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 1,
skipped_tests_default_filter: 0,
skipped_binaries: 1,
skipped_binaries_default_filter: 1,
}, true), @" (1 test and 1 binary skipped, including 1 binary via default-filter in profile.my-profile.overrides)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 3,
skipped_tests_default_filter: 2,
skipped_binaries: 1,
skipped_binaries_default_filter: 0,
}, false), @" (3 tests and 1 binary skipped, including 2 tests via profile.my-profile.default-filter)");
insta::assert_snapshot!(skip_counts_str(&SkipCounts {
skipped_tests: 0,
skipped_tests_default_filter: 0,
skipped_binaries: 0,
skipped_binaries_default_filter: 0,
}, false), @"");
}
fn skip_counts_str(skip_counts: &SkipCounts, override_section: bool) -> String {
let mut buf = Vec::new();
write_skip_counts(
skip_counts,
&CompiledDefaultFilter {
expr: CompiledExpr::ALL,
profile: "my-profile".to_owned(),
section: if override_section {
CompiledDefaultFilterSection::Override(0)
} else {
CompiledDefaultFilterSection::Profile
},
},
&Styles::default(),
&mut buf,
)
.unwrap();
String::from_utf8(buf).unwrap()
}
#[test]
fn test_write_output_with_highlight() {
const RESET_COLOR: &str = "\u{1b}[0m";
const BOLD_RED: &str = "\u{1b}[31;1m";
assert_eq!(
write_output_with_highlight_buf("output", 0, Some(6)),
format!("{RESET_COLOR}{BOLD_RED}output{RESET_COLOR}{RESET_COLOR}")
);
assert_eq!(
write_output_with_highlight_buf("output", 1, Some(5)),
format!("o{RESET_COLOR}{BOLD_RED}utpu{RESET_COLOR}t{RESET_COLOR}")
);
assert_eq!(
write_output_with_highlight_buf("output\nhighlight 1\nhighlight 2", 7, None),
format!(
"output\n{RESET_COLOR}\
{BOLD_RED}highlight 1{RESET_COLOR}\n\
{BOLD_RED}highlight 2{RESET_COLOR}{RESET_COLOR}"
)
);
assert_eq!(
write_output_with_highlight_buf(
"output\nhighlight 1\nhighlight 2\nnot highlighted",
7,
None
),
format!(
"output\n{RESET_COLOR}\
{BOLD_RED}highlight 1{RESET_COLOR}\n\
{BOLD_RED}highlight 2{RESET_COLOR}\n\
not highlighted{RESET_COLOR}"
)
);
}
fn write_output_with_highlight_buf(output: &str, start: usize, end: Option<usize>) -> String {
// We're not really testing non-UTF-8 output here, and using strings results in much more
// readable error messages.
let mut buf = Vec::new();
let end = end.unwrap_or(output.len());
let subslice = ByteSubslice {
start,
slice: &output.as_bytes()[start..end],
};
write_output_with_highlight(
output.as_bytes(),
subslice,
&Style::new().red().bold(),
&mut buf,
)
.unwrap();
String::from_utf8(buf).unwrap()
}
#[test]
fn no_capture_settings() {
// Ensure that output settings are ignored with no-capture.
let mut builder = TestReporterBuilder::default();
builder
.set_no_capture(true)
.set_failure_output(TestOutputDisplay::Immediate)
.set_success_output(TestOutputDisplay::Immediate)
.set_status_level(StatusLevel::Fail);
let test_list = TestList::empty();
let config = NextestConfig::default_config("/fake/dir");
let profile = config.profile(NextestConfig::DEFAULT_PROFILE).unwrap();
let build_platforms = BuildPlatforms::new_with_no_target().unwrap();
let mut buf: Vec<u8> = Vec::new();
let output = ReporterStderr::Buffer(&mut buf);
let reporter = builder.build(
&test_list,
&profile.apply_build_platforms(&build_platforms),
output,
StructuredReporter::new(),
);
assert!(reporter.inner.no_capture, "no_capture is true");
assert_eq!(
reporter.inner.force_failure_output,
Some(TestOutputDisplay::Never),
"failure output is never, overriding other settings"
);
assert_eq!(
reporter.inner.force_success_output,
Some(TestOutputDisplay::Never),
"success output is never, overriding other settings"
);
assert_eq!(
reporter.inner.status_levels.status_level,
StatusLevel::Pass,
"status level is pass, overriding other settings"
);
}
#[test]
fn test_progress_bar_prefix() {
let mut styles = Styles::default();
styles.colorize();
for stats in run_stats_test_failure_examples() {
let prefix = progress_bar_prefix(&stats, Some(CancelReason::TestFailure), &styles);
assert_eq!(prefix, " Cancelling".style(styles.fail).to_string());
}
for stats in run_stats_setup_script_failure_examples() {
let prefix =
progress_bar_prefix(&stats, Some(CancelReason::SetupScriptFailure), &styles);
assert_eq!(prefix, " Cancelling".style(styles.fail).to_string());
}
let prefix = progress_bar_prefix(&RunStats::default(), Some(CancelReason::Signal), &styles);
assert_eq!(prefix, " Cancelling".style(styles.fail).to_string());
let prefix = progress_bar_prefix(&RunStats::default(), None, &styles);
assert_eq!(prefix, " Running".style(styles.pass).to_string());
for stats in run_stats_test_failure_examples() {
let prefix = progress_bar_prefix(&stats, None, &styles);
assert_eq!(prefix, " Running".style(styles.fail).to_string());
}
for stats in run_stats_setup_script_failure_examples() {
let prefix = progress_bar_prefix(&stats, None, &styles);
assert_eq!(prefix, " Running".style(styles.fail).to_string());
}
}
fn run_stats_test_failure_examples() -> Vec<RunStats> {
vec![
RunStats {
failed: 1,
..RunStats::default()
},
RunStats {
failed: 1,
passed: 1,
..RunStats::default()
},
RunStats {
exec_failed: 1,
..RunStats::default()
},
RunStats {
timed_out: 1,
..RunStats::default()
},
]
}
fn run_stats_setup_script_failure_examples() -> Vec<RunStats> {
vec![
RunStats {
setup_scripts_failed: 1,
..RunStats::default()
},
RunStats {
setup_scripts_exec_failed: 1,
..RunStats::default()
},
RunStats {
setup_scripts_timed_out: 1,
..RunStats::default()
},
]
}
#[test]
fn test_final_warnings() {
let warnings = final_warnings_for(
FinalRunStats::Failed(RunStatsFailureKind::Test {
initial_run_count: 3,
not_run: 1,
}),
Some(CancelReason::TestFailure),
);
assert_eq!(
warnings,
"warning: 1/3 tests were not run due to test failure \
(run with --no-fail-fast to run all tests)\n"
);
let warnings = final_warnings_for(
FinalRunStats::Failed(RunStatsFailureKind::Test {
initial_run_count: 8,
not_run: 5,
}),
Some(CancelReason::Signal),
);
assert_eq!(warnings, "warning: 5/8 tests were not run due to signal\n");
let warnings = final_warnings_for(
FinalRunStats::Cancelled(RunStatsFailureKind::Test {
initial_run_count: 1,
not_run: 1,
}),
Some(CancelReason::Interrupt),
);
assert_eq!(warnings, "warning: 1/1 test was not run due to interrupt\n");
// These warnings are taken care of by cargo-nextest.
let warnings = final_warnings_for(FinalRunStats::NoTestsRun, None);
assert_eq!(warnings, "");
let warnings = final_warnings_for(FinalRunStats::NoTestsRun, Some(CancelReason::Signal));
assert_eq!(warnings, "");
// No warnings for success.
let warnings = final_warnings_for(FinalRunStats::Success, None);
assert_eq!(warnings, "");
// No warnings for setup script failure.
let warnings = final_warnings_for(
FinalRunStats::Failed(RunStatsFailureKind::SetupScript),
Some(CancelReason::SetupScriptFailure),
);
assert_eq!(warnings, "");
// No warnings for setup script cancellation.
let warnings = final_warnings_for(
FinalRunStats::Cancelled(RunStatsFailureKind::SetupScript),
Some(CancelReason::Interrupt),
);
assert_eq!(warnings, "");
}
fn final_warnings_for(stats: FinalRunStats, cancel_status: Option<CancelReason>) -> String {
let mut buf: Vec<u8> = Vec::new();
let styles = Styles::default();
write_final_warnings(stats, cancel_status, &styles, &mut buf).unwrap();
String::from_utf8(buf).unwrap()
}
}