aboutsummaryrefslogtreecommitdiff log msg author committer range
path: root/doc/testscript.cli
blob: 43be258b7c68ccf39afbdaaf454d72d4b4def77f (plain)
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941  // file : doc/testscript.cli // license : MIT; see accompanying LICENSE file "\name=build2-testscript-manual" "\subject=Testscript language" "\title=Testscript Language" // NOTES // // - Maximum
line is 70 characters. //  " \h0#preface|Preface|  This document describes the \c{build2} Testscript language. It starts with a discussion of the motivation behind a separate domain-specific language for running tests and then introduces a number of Testscript concepts with examples. The remainder of the document provides a more formal specification of the language, including its integration into the build system, conceptual model and execution, lexical structure, as well as syntax and semantics. The final chapter describes the testing guidelines and the Testscript style as used in the \c{build2} project itself.  In this document we use the term \i{Testscript} (capitalized) to refer to the Testscript language. Just \i{testscript} means code written in this language. For example: \"We can pass additional information to testscripts using target-specific variables.\" Finally, \c{testscript} refers to the file name.  We also use the equivalent distinction between \i{Buildfile} (language), \i{buildfile} (code), and \c{buildfile} (file).  \h1#intro|Introduction|  The \c{build2} \c{test} module provides the ability to run an executable target as a test along with passing options and arguments, providing the \c{stdin} input, as well as comparing the \c{stdout} output to the expected result. For example:  \ exe{hello}: file{names.txt}: test.stdin = true exe{hello}: file{greetings.txt}: test.stdout = true exe{hello}: test.options   = --greeting 'Hi' exe{hello}: test.arguments = - # Read names from stdin. \  This works well for simple, single-run tests. If, however, our testing required multiple runs with varying inputs and/or analyzing output, traditionally, we would resort to using a scripting language, for instance Bash or Python. This, however, has a number of drawbacks. Firstly, this approach is not portable (there is no Bash or Python on Windows \i{out of the box}). It is also hard to write concise tests in a general-purpose scripting language. The result is often a test suite that has grown incomprehensible with everyone dreading adding new tests. Secondly, it is hard to run such tests in parallel without major effort. Usually this involves having a separate script for each test and implementing some kind of a test harness.  Testscript is a domain-specific language for running tests. It vaguely resembles Bash and is optimized for concise test description and fast execution by focusing on the following functionality:  \ul|  \li|Supplying input via command line and \c{stdin}.|  \li|Comparing to expected exit status.|  \li|Comparing to expected output for \c{stdout}/\c{stderr}, including     using regex.|  \li|Setup/teardown commands and automatic file/directory cleanups.|  \li|Simple (single-command) and compound (multi-command) tests.|  \li|Test groups with common setup/teardown.|  \li|Test isolation for parallel execution.|  \li|Portable POSIX-like builtins subset.|  \li|Test documentation.||  Note that Testscript is a \i{test runner}, not a testing framework for a particular programming language. It does not concern itself with how the test executables themselves are implemented. As a result, it is mostly geared towards functional testing but can also be used for unit testing if external input/output is required. Testscript is part of the \c{build2} build system and is implemented by its \c{test} module.  As a quick introduction to the Testscript's capabilities, let's \i{properly} test a \"Hello, World\" program. For a simple implementation the corresponding \c{buildfile} might look like this:  \ exe{hello}: cxx{hello} \  We also assume that the project's \c{bootstrap.build} loads the \c{test} module which implements the execution of testscripts.  To start, we create an empty file called \c{testscript}. To indicate that a testscript file tests a specific target we simply list it as a target's prerequisite, for example:  \ exe{hello}: cxx{hello} testscript \  Let's assume our \c{hello} program expects us to pass the name to greet as a command line argument. And if we don't pass anything, it prints an error followed by usage and terminates with a non-zero exit code. We can test this failure case by adding the following line to the \c{testscript} file:  \ $* 2>- != 0 \ While it sure is concise, it may look cryptic without an explanation. When the \c{test} module runs tests, it passes to each testscript the path to the target of which this testscript is a prerequisite. So in our case the testscript will receive the path to our \c{hello} executable. The buildfile can also pass along additional options and arguments (see \l{#integration Build System Integration} for details). Inside the testscript, all of this (target path, options, and arguments) are bound to the \c{$*} variable. So, in our case, if we expand the above line, it would be something like this:  \ /tmp/hello/hello 2>- != 0 \  Or, if we are on Windows, something like this:  \ C:\projects\hello\hello.exe 2>- != 0 \  The \c{2>-} redirect is the Testscript equivalent of \c{2>/dev/null} that is both portable and more concise (\c{2} here is the \c{stderr} file descriptor). If we don't specify it and our program prints anything to \c{stderr}, then the test fails (unexpected output).  The remainder of the command (\c{!= 0}) is the exit status check. If we don't specify it, then the test is expected to return zero exit code (which is equivalent to specifying \c{== 0}).  If we run our test, it will pass provided our program behaves as expected. One thing our test doesn't verify, however, is the diagnostics that gets printed to \c{stderr} (remember, we ignored it with \c{2>-}). Let's fix that assuming this is the code that prints it:  \ cerr << \"error: missing name\" << endl      << \"usage: \" << argv[0] << \" \" << endl; \  In Testscript you can compare output to the expected result for both \c{stdout} and \c{stderr}. We can supply the expected result as either a \i{here-string} or \i{here-document}, both which can be either literal or regex. The here-string approach works best for short, single-line output and we will use it for another test in a minute. For this test let's use the here-document since the expected diagnostics has two lines:  \ $* 2>>EOE != 0 error: missing name usage: hello EOE \ Let's decrypt this: the \c{2>>EOE} is a here-document redirect with \c{EOE} (stands for End-Of-Error) being the string we chose to mark the end of the here-document fragment. Next comes the here-document fragment followed by the end marker. Now, when executing this test, the \c{test} module will check two things: it will compare the \c{stderr} output to the expected result using the \c{diff} tool and it will make sure the test returns a non-zero exit code. Let's give it a go: \$ b test testscript:1:1: error: hello stderr doesn't match expected   info: stderr: test-hello/1/stderr   info: expected stderr: test-hello/1/stderr.orig   info: stderr diff: test-hello/1/stderr.diff --- test-hello/1/stderr.orig +++ test-hello/1/stderr @@ -1,2 +1,2 @@  error: missing name -usage: hello  +usage: /tmp/hello/hello  \  While not what we hoped for, at least the problem is clear: the program name varies at runtime so we cannot just hardcode \c{hello} in our expected output. How do we solve this? The best fix would be to use the actual path to the target; after all, we know it's the first element in \c{$*}: \$* 2>>\"EOE\" != 0 error: missing name usage: $0 EOE \ You can probably guess what \c{$0} expands to. But did you notice another change? Yes, those double quotes in \c{2>>\"EOE\"}. Here is what's going on: similar to Bash, single-quoted strings (\c{'foo'}) are taken literally while double-quoted ones (\c{\"foo\"}) have variable expansions, escaping, and so on. In Testscript this semantics is extended to here-documents in a curious way: if the end marker is single-quoted then the here-document lines are taken literally and if it is double-quoted, then there can be variable expansions, etc. An unquoted end marker is treated as single-quoted (note that this is unlike Bash where here-documents always have variable expansions).  This example illustrated a fairly common testing problem: output variability. In our case we could fix it perfectly since we could easily calculate the varying part exactly. But often figuring out the varying part is difficult if not outright impossible. A good example would be a system error message based on the \c{errno} code, such as file not being found. Different C runtimes can phrase the message slightly differently or it can be localized. Worse, it can be a slightly different error code, for example \c{ENOENT} vs \c{ENOTDIR}.  To handle output variability, Testscript allows us to specify the expected output as a regular expression. For example, this is an alternative fix to our usage problem that simply ignores the program name:  \ $* 2>>~/EOE/ != 0 error: missing name /usage: .+ / EOE \ Let's explain what's going here: to use a regex here-string or here-document we add the \c{~} \i{redirect modifier}. In this case the here-document end marker must start and end with the regex introducer character of your choice (\c{/} in our case). Any line inside the here-document fragment that begins with this introducer is then treated as a regular expression rather than a literal (see \l{#syntax-regex Output Regex} for details). While this was a fairly deep rabbit hole for a first example, it is a good illustration of how quickly things get complicated when testing real-world software. Now that we have tested the failure case, let's test the normal functionality. While we could have used a here-document, in this case a here-string will be more concise: \$* 'World' >'Hello, World!' \  It's also a good idea to document our tests. Testscript has a formalized test description that can capture the test \i{id}, \i{summary}, and \i{details}. All three components are optional and how thoroughly you document your tests is up to you.  The description lines precede the test command. They start with a colon (\c{:}), and have the following layout:  \ :  :  : :
: ... \ The recommended format for \c{} is \c{-...} with at least two keywords. The id is used in diagnostics, to name the test working directory, as well as to run individual tests. It can only contain alphanumeric characters as well as underscores, pluses, and minuses. The recommended style for \c{} is that of the \c{git(1)} commit summary. The detailed description is free-form. Here are some examples (\c{#} starts a comment): \ # Only id. # : missing-name $* 2>>\"EOE\" != 0 ... # Only summary. # : Test handling of missing name ... # Both id and summary. # : missing-name : Test handling of missing name ... # All three: id, summary, and a detailed description. # : missing-name : Test handling of missing name : : This test makes sure the program detects that the name to greet : was not specified on the command line and both prints usage and : exits with non-zero code. ... \ The recommended way to come up with an id is to distill the summary to its essential keywords by removing generic words like \"test\", \"handle\", and so on. If you do this, then both the id and summary will convey essentially the same information. As a result, to keep things concise, you may choose to drop the summary and only have the id (this is what we often do in \c{build2} tests). If the id is not provided, then it will be automatically derived from the line number in testscript (we have already seen one in the earlier failed test diagnostics). Either the id or summary (but not both) can alternatively be specified inline in the test command after a colon (\c{:}), for example: \$* 'World' >'Hello, World!' : command-name \ Similar to handling output, Testscript provides a convenient way to supply input to the test's \c{stdin}. Let's say our \c{hello} program recognizes the \c{-} argument as an instruction to read the names from \c{stdin}. This is how we could test this functionality: \ $* - <>EOO : stdin-names Jane John EOI Hello, Jane! Hello, John! EOO \ As you might suspect, we can also use here-strings to supply \c{stdin}, for example: \$* - <'World' >'Hello, World!' : stdin-name \ Let's say our \c{hello} program has a configuration file that captures custom name-to-greeting mappings. A path to this file can be passed with the \c{-c} option. To test this functionality we first need to create a sample configuration file. This calls for a multi-command or \i{compound} test, for example: \ cat <=hello.conf; John = Howdy Jane = Good day EOI $* -c hello.conf 'Jane' >'Good day, Jane!' : config-greet \ Notice the semicolon (\c{;}) at the end of the first command: it indicates that the following command is part of the same test. Other than that, you may be wondering what exactly is \c{cat}? While most POSIX systems will have a program with this name, there is no such thing, say, on vanilla Windows. To help with portability Testscript provides a subset (both in terms of the number and supported features) of POSIX utilities, such as, \c{echo}, \c{touch}, \c{cat}, \c{mkdir}, \c{rm}, and so on (see \l{#builtins Builtins} for details). You may also be wondering why we don't have a third command, such as \c{rm}, that removes \c{hello.conf}? It is not necessary because this file will be automatically registered for cleanup that happens at the end of the test. We can also register our own files and directories for automatic cleanup. For example, if the \c{hello} program created the \c{hello.log} file on unsuccessful runs, then this is how we could have cleaned it up: \$* ... &hello.log != 0 \ What if we wanted to run two tests for this configuration file functionality? For example, we may want to test the custom greeting as above but also make sure the default greeting is not affected. One way to do this would be to repeat the \c{cat} command in each test. But there is a better way: in Testscript we can combine related tests into groups. For example: \ : config { conf = $~/hello.conf +cat <=$conf John = Howdy Jane = Good day EOI $* -c$conf 'John' >'Howdy, John!' : custom-greet $* -c$conf 'Jack' >'Hello, Jack!' : default-greet } \ A test group is a scope that contains several tests. Variables set inside a scope (like our \c{conf}) are only in effect until the end of this scope. Groups can also perform common, non-test actions with \i{setup} and \i{teardown} commands. The setup commands start with the plus sign (\c{+}) and must come before the tests while teardown \- with minus (\c{-}) and must come after the tests. Note that setup and teardown commands are not part of any test (notice the lack of \c{;} after \c{+cat}), rather they are associated with the group itself. Their automatic cleanup only happens at the end of the scope (so our \c{hello.conf} will only be removed after all the tests in the group have completed). A scope can also have a description. In particular, assigning a test group an id (\c{config} in our example) allows us to run tests only from this specific group. The last thing we need to discuss in this example is \c{$~}. This variable stands for the scope working directory (we will talk more about working directories at the end of this introduction). Besides explicit group scopes, each test is automatically placed in its own implicit test scope. However, we can make the test scope explicit, for example, for better visual separation of complex tests: \ : config-greet { conf = hello.conf cat <'Jane = Good day' >=$conf; $* -c$conf 'Jane' >'Good day, Jane!' } \ We can conditionally exclude sections of a testscript using the \c{if-else} branching. This can be done both at the scope level to exclude test or group scopes as well as at the command level to exclude individual commands or variable assignments. Let's start with a scope example by providing a Windows-specific implementation of a test: \ : config-empty : if ($cxx.target.class != windows) {$* -c /dev/null 'Jane' >'Hello, Jane!' } else { $* -c nul 'Jane' >'Hello, Jane!' } \ Note that the scopes in the \c{if-else} chain are treated as variants of the same test or group thus the single description at the beginning. Let's now see an example of command-level \c{if-else} by reimplementing the above as a single test with some branching and without using the \c{nul} device on Windows (notice the semicolon after \c{end}): \ : config-empty : if ($cxx.target.class != windows) conf = /dev/null else conf = empty touch $conf end;$* -c $conf 'Jane' >'Hello, Jane!' \ You may have noticed that in the above examples we referenced the \c{cxx.target.class} variable as if we were in a buildfile. We could do that because the testscript variable lookup continues in the buildfile starting from the target being tested, then the testscript target, and continuing with the standard scope lookup (see \l{#model Model and Execution} for details). In particular, this means we can pass arbitrary information to testscripts using target-specific variables. For example, this is how we can move the above platform test to \c{buildfile}: \ # buildfile exe{hello}: cxx{hello} testscript testscript{*}: windows = ($cxx.target.class == windows) \ \ # testscript if! $windows conf = /dev/null else ... \ Note also that in cases where you simply need to conditionally pick a value for a variable, the \c{build2} evaluation context will often be a more concise option. For example: \ : config-empty : conf = ($windows ? nul : /dev/null); $* -c$conf 'Jane' >'Hello, Jane!' \ Similar to Bash, test commands can be chained with pipes (\c{|}) and combined with logical operators (\c{||} and \c{&&}). Let's say our \c{hello} program provided the \c{-o} option to write the result to a file instead of \c{stdout}. Here is how we could test it: \ $* -o hello.out - <>EOO John Jane EOI Hello, John! Hello, Jane! EOO \ Similarly, if it had the \c{-r} option to reverse the greetings back to their names (as every \c{hello} program should), then we could write a test like this: \$* - <>EOO John Jane EOI John Jane EOO \ To conclude, let's put all our (sensible) tests together so that we can have a complete picture: \ $* 'World' >'Hello, World!' : command-name$* 'John' 'Jane' >>EOO : command-names Hello, Jane! Hello, John! EOO $* - <>EOO : stdin-names Jane John EOI Hello, Jane! Hello, John! EOO : config { conf =$~/hello.conf +cat <=$conf John = Howdy Jane = Good day EOI$* -c $conf 'John' >'Howdy, John!' : custom-greet$* -c $conf 'Jack' >'Hello, Jack!' : default-greet }$* 2>>\"EOE\" != 0 : missing-name error: missing name usage: $0 EOE \ Testscript isolates tests from each other by running each test in its own temporary working directory under \c{out_base}. For the above \c{testscript} the working directory structure will be as follows: \$out_base/ └── test-hello/ ├── command-name/ ├── command-names/ ├── stdin-names/ ├── config/ │ ├── hello.conf │ ├── custom-greet/ │ └── default-greet/ └── missing-name/ \ If all the tests succeed, then this working directory structure is automatically removed. In case of a failure, however, it is left behind in case you need to examine the output of the failed tests. It will be automatically cleaned on the subsequent run, before executing any tests. The execution of tests happens in parallel. In the above case Testscript can start running all the top-level tests as well as the \c{config} group immediately. Inside \c{config}, once the setup command (\c{cat}) is completed, the two inner tests are executed in parallel as well. Refer to \l{#model Model and Execution} for details on the working directory structure and test execution. \h1#integration|Build System Integration| The integration of testscripts into buildfiles is done using the standard \c{build2} \i{target-prerequisite} mechanism. In this sense, a testscript is a prerequisite that describes how to test the target similar to how, for example, the \c{INSTALL} file describes how to install it. For example: \ exe{hello}: testscript doc{INSTALL README} \ By convention, the testscript file should be called either \c{testscript} if you only have one or have the \c{.testscript} extension, for example, \c{basics.testscript}. The \c{test} module registers the \c{testscript{\}} target type to be used for testscript files. We don't have to use explicit target type for the \c{testscript} file. A testscript prerequisite can be specified for any target. For example, if our directory contains a bunch of shell scripts that we want to test together, then it makes sense to specify the testscript prerequisite for the directory target: \ ./: testscript{basics} \ During variable lookup if a variable is not found in one of the testscript scopes (see \l{#model Model and Execution}), then the search continues in the \c{buildfile} starting with the target-specific variables of the target being tested (e.g., \c{exe{hello\}}; called \i{test target}), then target-specific variables of the testscript target (e.g., \c{testscript{basics\}}; called \i{script target}), and then continuing with the scopes starting with the one containing the script target. As a result, a testscript can \"see\" all the existing buildfile variables plus we can use target-specific variables to pass additional, test-specific, information to testscrips. As an example, consider this testscript and buildfile pair: \ # basics.testscript if ($cxx.target.class == windows) test.arguments +=$foo end if $windows test.arguments +=$bar end \ \ # buildfile exe{hello}: testscript{basics} # All testscripts in this scope. # testscript{*}: windows = ($cxx.target.class == windows) # All testscripts for target exe{hello}. # exe{hello}: bar = BAR # Only basics.testscript. # testscript{basics}@./: foo = FOO \ Additionally, by convention, a number of pre-defined \c{test.*} variables are used to pass commonly required information to testscripts, as described next. Unless set manually as a test or script target-specific variable, the \c{test} variable is automatically set to the target path being tested. For example, given this \c{buildfile}: \ exe{hello}: testscript \ The value of \c{test} inside the testscript will be the absolute path to the \c{hello} executable. If the \c{test} variable is set manually to a name of a target, then it is automatically converted to the target path. This can be useful when testing a program that is built in another subdirectory of a project (or even in another project, via import). For example, our \c{hello} may reside in the \c{hello/} subdirectory while we may want to keep the tests in \c{tests/}: \ hello/ ├── hello/ │ └── hello* └── tests/ ├── buildfile └── testscript \ This is how we can implement \c{tests/buildfile} for this setup: \ hello = ../hello/exe{hello} ./:$hello testscript ./: test = $hello include ../hello/ \ The rest of the special \c{test.*} variables are \c{test.options}, \c{test.arguments}, \c{test.redirects}, and \c{test.cleanups}. You can use them to pass additional command line options, arguments, redirects, and cleanups to your test scripts. Together with \c{test} these variables form the \i{test target command} line which, for conciseness, is bound to the following aliases: \$* - $test$test.options $test.arguments$test.redirects $test.cleanups$0 - $test$N - (N-1)-th element in the {$test.options$test.arguments} array \ Note that these aliases are read-only; if you need to modify any of these values from within testscripts, then you should use the original variable names, for example: \ test.options += --foo $* bar # Includes --foo. \ Note also that these \c{test.*} variables only establish a convention. You could also put everything into, say \c{test.arguments}, and it will still work as expected. Another pre-defined variable is \c{test.target}. It is used to specify the test target platform when cross-testing (for example, when running Windows test on Linux under Wine). Normally, you would set it in your \c{build/root.build} to the cross-compilation target of your toolchain, for example: \ # root.build # using cxx # Load the C++ module (sets sets cxx.target). test.target =$cxx.target # Set test target to the C++ compiler target. \ If this variable is not set explicitly, then it defaults to \c{build.host} (which is the platform on which the build system is running) and only native testing will be supported. All the testscripts for a particular test target are executed in a subdirectory of \c{out_base} (or, more precisely, in subdirectories of this subdirectory; see \l{#model Model and Execution}). If the test target is a directory, then the subdirectory is called \c{test}. Otherwise, it is the name of the target prefixed with\c{test-}. For example: \ ./: testscript{foo} # $out_base/test/ exe{hello}: testscript{bar} #$out_base/test-hello/ \ \h1#model|Model and Execution| A testscript file is a set of nested scopes. A scope is either a group scope or a test scope. Group scopes can contain nested group and test scopes. Test scopes can only contain test commands. Group scopes are used to organize related tests with shared variables as well as setup and teardown commands. Explicit test scopes are normally used for better visual separation of complex tests. The top level scope is always an implicit group scope corresponding to the entire script file. If there is no explicit scope for a test, one is established implicitly. As a result, a testscript file always starts with a group scope which then contains other group scopes and/or test scopes, recursively. A scope (both group and test) has an \i{id}. If not specified explicitly (as part of the description), it is derived automatically from the group/test location in the testscript file (see \l{#syntax-description Description} for details). The id of the implicit outermost scope is the script file name without the \c{.testscript} extension, except if the file name is \c{testscript}, in which case the id is empty. Based on the ids each nested group and test has an \i{id path} that uniquely identifies it. It starts with the id of the implied outermost group (unless empty), may include a number of intermediate group ids, and ends with the final test or group id. The ids in the path are separated with a forward slash (\c{/}). Note that this also happens to be the relative filesystem path to the temporary directory where the test is executed (as described below). Inside a scope its id path is available via the special \c{$@} variable (read-only). As an example, consider the following testscript file which we assume is called \c{basics.testscript}: \ test0: test0 : group { test1 : test2 { test2a; test2b } } \ Below is its version annotated with the id paths that also shows all the implicit scopes: \ # basics { # basics/test0 { test0 } # basics/group { # basics/group/5 { test1 } # basics/group/test2 { test2a; test2b } } } \ A scope establishes a nested variable context. A variable set within a scope will only have effect until the end of this scope. Variable lookup is performed starting from the scope where the variable is referenced (expanded), continuing with the outer testscript scopes, and then continuing in the buildfile as described in \l{#integration Build System Integration}. A scope also establishes a cleanup context. All cleanups (\l{#syntax-cleanup Cleanup}) registered in a scope are performed at the end of that scope's execution in the reverse order of their registration. Prior to executing a scope, a nested temporary directory is created with the scope id as its name. This directory then becomes the scope's working directory. After executing the scope (and after performing cleanups) this temporary directory is automatically removed provided that it is empty. If it is not empty, then the test is considered to have failed (unexpected output). Inside a scope its working directory is available via the special \c{$~} variable (read-only). As an example, consider the following version of \c{basics.testscript}. We also assume that its test target is a directory (so the target test directory is \c{$out_base/test/}). \ : group { foo = FOO bar = BAR +setup &out-setup : test1 { bar = BAZ test1$foo $bar } test2$bar: test2 } test3 $foo &out-test \ Below is its annotated version: \ { #$~ = $out_base/test/basics/ { #$~ = .../test/basics/group/ foo = FOO bar = BAR +setup &out-setup { # $~ = .../basics/group/test1/ bar = BAZ test1$foo $bar # test1 FOO BAZ } { #$~ = .../basics/group/test2/ test2 $bar # test2 BAR } } # Remove out-setup. { #$~ = .../test/basics/17/ test3 $foo &out-test # test3 } # Remove out-test. } \ A test should normally create files or directories, if any, in its working directory to ensure test isolation. A test can, however, access (but normally should not modify) files created by an outer group's setup commands. Because of this nested directory structure this can be done using \c{../}-based relative paths, for example: \ { +setup >=test.conf test1 ../test.conf test2 ../test.conf } \ Alternatively, we can use an absolute path: \ { conf =$~/test.conf +setup >=$conf test1$conf test2 $conf } \ Inside the scope working directory filesystem names that start with \c{stdin}, \c{stdout}, and \c{stderr} are reserved. To execute a test scope its commands (including variable assignments) are executed sequentially and in the order specified. If any of the commands fails, no further commands are executed and the test is considered to have failed. Executing a group scope starts with performing its setup commands (including variable assignments) sequentially and in the order specified. If any of them fail, the group execution is terminated and the group is considered to have failed. After completing the setup, inner scopes (both group and test) are executed. Because scopes are isolated and tests are assumed not to depend on each other, the execution of inner scopes can be performed in parallel. After completing the execution of the inner scopes, if all of them succeeded, the teardown commands are executed sequentially and in the order specified. Again, if any of them fail, the group execution is terminated and the group is considered to have failed. As an example, consider the following version of \c{basics.testscript}: \ test0 : group { +setup1 +setup2 test1 test2 test3 -teardown2 -teardown1 } \ At the top level, both \c{test0} and \c{group} can start executing in parallel. Inside \c{group}, first the two setup commands are executed sequentially. Once the setup is completed, \c{test1}, \c{test2}, \c{test3} can all be executed in parallel (along with \c{test0} which may still be running). Once the three inner tests complete successfully, the \c{group}'s teardown commands are executed sequentially. At the top level, the script is completed only when both \c{test0} and \c{group} complete. The following annotated version illustrates a possible thread scheduling for this example: \ { # thread 1 test0 # thread 2 : group # thread 1 { +setup1 # thread 1 +setup2 # thread 1 test1 # thread 3 test2 # thread 4 test3 # thread 1 # thread 1 (wait for 3 & 4) -teardown2 # thread 1 -teardown1 # thread 1 } # thread 1 (wait for 2) } \ A testscript would normally contain multiple tests and sometimes it is desirable to only execute a specific test or a group of tests. For example, you may be debugging a failing test and would like to re-run it. As an example, consider the following testscript file called \c{basics.testscript}: \$* foo : foo : fox { $* fox bar : bar$* fox baz : baz } \ The id paths for these three test will then be: \ basics/foo basics/fox/bar basics/fox/baz \ To only run individual tests, test groups, or testscript files we can specify their id paths in the \c{config.testscript} variable, for example: \ $b test config.test=basics # All in basics.testscript$ b test config.test=basics/fox # All in fox $b test config.test=basics/foo # Only foo$ b test 'config.test=basics/foo basics/fox/bar' # Only foo and bar \ The script working directory may exist before the execution (for example, because of a failed previous run) or it may be desirable not to clean it up after the execution (for example, to examine test setup, output, etc). Before the execution the default behavior is to warn and then automatically remove the working directory if it exists. After the execution the default behavior is to perform all the cleanups and teardowns and then remove the working directory failing if it is not empty. This default behaviors can, however, be overridden with the \c{config.test.output} variable. The \c{config.test.output} variable contains a pair of values with the first signifying the \i{before} behavior and the second \- \i{after}. The valid \i{before} values are \c{fail} (fail if the directory exists), \c{warn} (warn if the directory exists then remove), \c{clean} (silently remove the existing directory). The valid \i{after} values are \c{clean} (remove the directory failing if it is not empty) and \c{keep} (do not run cleanups and teardowns and do not remove the working directory). The default behavior is thus equivalent to specifying the \c{warn@clean} pair. If only a single value is specified in \c{config.test.output} then it is assumed to be the \i{after} value and the \i{before} value is assumed to be \c{clean}. In other words: \ $b test config.test.output=clean # config.test.output=clean@clean$ b test config.test.output=keep # config.test.output=clean@keep \ Note also that selecting the \c{keep} behavior may result in some test failures (due to unexpected output) to go undetected. \h1#lexical|Lexical Structure| At the lexical level, testscripts are UTF-8 encoded text restricted to the Unicode graphic characters, tabs (\c{\\t}), carriage returns (\c{\\r}), and line feeds (\c{\\n}). Testscript is a line-oriented language with a context-dependent lexical structure. It \"borrows\" several building blocks (variable expansion, function calls, and evaluation contexts; collectively called \i{expansions} from now on) from the Buildfile language. In a sense, testscripts are specialized (for testing) continuations of buildfiles. Except in here-document fragments, leading whitespaces and blank lines are ignored except for the line/column counting. A non-empty testscript must end with a newline. Except in single-quoted strings and single-quoted here-document fragments, the backslash (\c{\\}) character followed by a newline signals the line continuation. Both this character and the newline are removed (note: not replaced with a whitespace) and the following line is read as if it was part of the first line. Note that \c{'\\'} followed by EOF is invalid. For example: \ $* foo | \$* bar \ Except in quoted strings and here-document fragments, an unquoted and unescaped \c{'#'} character starts a comment; everything from this character until the end of the line is ignored. For example: \ # Setup foo. $* foo$* bar # Setup bar. \ There is no line continuation support in comments; the trailing \c{'\\'} is ignored except in one case: if the comment is just \c{'#\\'} followed by the newline, then it starts a multi-line comment that spans until the closing \c{'#\\'} is encountered. For example: \ #\ $* foo$* bar #\ $* foo #\$* bar $* baz #\ \ Similar to Buildfile, the Testscript language supports two types of quoting: single (\c{'}) and double (\c{\"}). Both can span multiple lines. The single-quoted strings and single-quoted here-document fragments do not recognize any expansions or escape sequences (not even for the single quote itself or line continuations) with all the characters taken literally until the closing single quote or here-document end marker is encountered. The double-quoted strings and double-quoted here-document fragments recognize expansions and escape sequences (including line continuations). For example: \ foo = FOO # 'FOO true' # bar = \"$foo ($foo == FOO)\" # 'FOO bool' #$* <<\"EOI\" $foo$type($foo == FOO) EOI \ Characters that have special syntactic meaning (for example \c{'$'}) can be escaped with a backslash (\c{\\}) to preserve their literal meaning (to specify literal backslash you need to escape it as well). For example: \ foo = \$foo\\\\bar # '$foo\bar' \ Note that quoting could often be a more readable way to achieve the same result, for example: \ foo = '$foo\bar' \ Inside double-quoted strings only the \c{\"\\$(} character set needs to be escaped. Inside double-quoted here-document fragments \- only \c{\\$(} (since in here-documents quotes are taken literally). The lexical structure of a line depends on its type. The line type could be dictated by the preceding construct, as is the case for here-document fragments. Otherwise, the line type is determined by examining the leading character and, if that fails to determine the line type, leading tokens, as described next. A character is said to be \i{unquoted} and \i{unescaped} if it is not escaped and is not part of a quoted string. A token is said to be unquoted and unescaped if all its characters are unquoted and unescaped. The following characters determine the line type if they appear unquoted and unescaped at the beginning of the line: \ ':' - description line '.' - directive line '{' - scope start '}' - scope end '+' - setup command line '-' - teardown command line \ If the line doesn't start with any of these characters then the first token of the line is examined in the \c{first_token} mode (see below). If the first token is an unquoted word, then the second token of the line is examined in the \c{second_token} mode (see below). If it is a variable assignment (either \c{+=}, \c{=+}, or \c{=}), then the line type is a variable line. Otherwise, it is a test command line. Note that variables with computed names can only be set using the \l{#builtins-set \c{set} pseudo-builtin}. The Testscript language defines the following distinct lexing modes (or contexts): \dl| \li|\n\n\cb{command_line}\n Whitespaces are token separators. The following characters and character sequences (read vertically, for example, \c{==}, \c{!=} below) are recognized as tokens: \ :;=!|&<>$(# == \ | \li|\n\n\cb{first_token}\n Like \c{command_line} but recognizes variable assignments as separators.| \li|\n\n\cb{second_token}\n Like \c{command_line} but recognizes variable assignments as tokens.| \li|\n\n\cb{command_expansion}\n Subset of \c{command_line} used for re-lexing expansions (described below). Only the \c{|&<>} characters are recognized as tokens. Note that whitespaces are not separators in this mode.| \li|\n\n\cb{variable_line}\n Similar to the Buildfile \cb{value} mode. The \c{;$([]} characters are recognized as tokens.| \li|\n\n\cb{description_line}\n Like a single-quoted string.| \li|\n\n\cb{here_line_single}\n Like a single-quoted string except it treats newlines as separators and quotes as literals.| \li|\n\n\cb{here_line_double}\n Like a double-quoted string except it treats newlines as separators and quotes as literals. The \c{$(} characters are recognized as tokens.|| Besides having a varying lexical structure, parsing some line types involves performing expansions (variable expansions, function calls, and evaluation contexts). The following table summarizes the mapping of line types to lexing modes and indicates whether they are parsed with expansions: \ variable line variable_line expansions directive line command_line expansions description line description_line test command line command_line expansions setup command line command_line expansions teardown command line command_line expansions here-document single-quoted here_line_single here-document double-quoted here_line_double expansions \ Finally, unquoted expansions in command lines (test, setup, and teardown) are re-lexed in the \c{command_expansion} mode in order to recognize command line syntax tokens (redirects, pipes, etc). To illustrate why this re-lexing is necessary, consider the following example of a \"canned\" command line: \ x = echo >- $x foo \ The test command line token sequence will be \c{$}, \c{x}, \c{foo}. After the expansion we have \c{echo}, \c{>-}, \c{foo}, however, the second element (\c{>-}) is not (yet) recognized as a redirect. To recognize it we re-lex the result of the expansion. Note that besides the few command line syntax characters, re-lexing will also \"consume\" quotes and escapes, for example: \ args = \"'foo'\" # 'foo' echo $args # echo foo \ To preserve quotes in this context we need to escape them: \ args = \"\\\\'foo\\\\'\" # \'foo\' echo$args # echo 'foo' \ Alternatively, for a single value, we could quote the expansion (in order to suppress re-lexing; note, however, that quoting will also inhibit word-splitting): \ arg = \"'foo'\" # 'foo' echo \"$arg\" # echo 'foo' \ To minimize unhelpful consumption of escape sequences (for example, in Windows paths), re-lexing only performs the \i{effective escaping} for the \c{'\"\\} characters. All other escape sequences are passed through uninterpreted. Note that this means there is no way to escape command line syntax characters. The recommendation is to use quoting except for passing literal quotes, for example: \ args = \'&foo\' # '&foo' echo$args # echo &foo \ To make sure that a string is passed as is through both expansions use the \i{doubled single-quoting} idiom, for example: \ filter = sed -e \''s/foo (bar|baz)/$&/'\'$* <>EOO ... EOI ... EOO \ \h1#syntax|Syntax and Semantics| \h#syntax-notation|Notation| The formal grammar of the Testscript language is specified using an EBNF-like notation with the following elements: \ foo: ... - production rule foo - non-terminal - terminal 'foo' - literal foo* - zero or more multiplier foo+ - one or more multiplier foo? - zero or one multiplier foo bar - concatenation (foo then bar) foo | bar - alternation (foo or bar) (foo bar) - grouping {foo bar} - grouping in any order (foo then bar or bar then foo) foo\ bar - line continuation # foo - comment \ A rule's right-hand-sides that start on a new line describe the line-level syntax and ones that start on the same line describes the syntax inside the line. If a rule contains multiple lines, then each line matches a separate line in the input. If a multiplier appears in front of a line then it specifies the number of repetitions of the entire line. For example, from the following three rules, the first describes a single line of multiple literals, the second \- multiple lines of a single literal, and the third \- multiple lines of multiple literals. \ # foofoofoo # text-line: 'foo'+ # foo # foo # foo # text-lines: +'foo' # foo # foofoo # foofoofoo # text-lines: +('foo'+) \ A newline in the grammar matches any standard newline separator sequence (CR/LF combinations). An unquoted space in the grammar matches zero or more non-newline whitespaces (spaces and tabs). A quoted space matches exactly one non-newline whitespace. Note also that in some cases components within lines may not be whitespace-separated in which case they will be written without any spaces between them, for example: \ foo: 'foo' ';' # 'foo;' or 'foo ;' or 'foo ;' bar: 'bar'';' # 'bar;' baz: 'baz'' '+';' # 'baz ;' or 'baz ;' fox: bar''bar # 'bar;bar;' \ You may also notice that several production rules below end with \c{-line} while potentially spanning several physical lines. The \c{-line} suffix here signifies a \i{logical line}, for example, a command line plus its here-document fragments. \h#syntax-grammar|Grammar| The complete grammar of the Testscript language is presented next with the following sections discussing the semantics of each production rule. \ script: scope-body scope-body: *setup *(scope|directive|test) *tdown scope: ?description scope-block|scope-if scope-block: '{' scope-body '}' scope-if: ('if'|'if!') command-line scope-block *scope-elif ?scope-else scope-elif: ('elif'|'elif!') command-line scope-block scope-else: 'else' scope-block directive: '.' include include: 'include' (' '+'--once')*(' '+)* setup: variable-like|setup-line tdown: variable-like|tdown-line setup-line: '+' command-like tdown-line: '-' command-like test: ?description +(variable-line|command-like) variable-like: variable-line|variable-if variable-line: ('='|'+='|'=+') value-attributes? ';'? value-attributes: '[' ']' variable-if: ('if'|'if!') command-line variable-if-body *variable-elif ?variable-else 'end' variable-elif: ('elif'|'elif!') command-line variable-if-body variable-else: 'else' variable-if-body variable-if-body: *variable-like command-like: command-line|command-if command-line: command-expr (';'|(':' ))? *here-document command-expr: command-pipe (('||'|'&&') command-pipe)* command-pipe: command ('|' command)* command: (' '+(|redirect|cleanup))* command-exit? command-exit: ('=='|'!=') command-if: ('if'|'if!') command-line command-if-body *command-elif ?command-else 'end' (';'|(':' ))? command-elif: ('elif'|'elif!') command-line command-if-body command-else: 'else' command-if-body command-if-body: *(variable-line|command-like) redirect: stdin|stdout|stderr stdin: '0'?(in-redirect) stdout: '1'?(out-redirect) stderr: '2'(out-redirect) in-redirect: '<-'|\ '<|'|\ ('<<<'|'<=') |\ ('<<'|'<<='){':'?'/'?} |\ ('<'|'<<<='){':'?'/'?} out-redirect: '>-'|\ '>|'|\ '>!'|\ '>=' |\ '>+' |\ '>&' ('1'|'2')|\ ('>>>'|'>?') |\ ('>>'|'>>?'){':'?'/'?}'~'? |\ ('>'|'>>>?'){':'?'/'?}'~'? here-document: * cleanup: ('&'|'&?'|'&!') (|) description: +(':' ) \ \h#syntax-script|Script| \ script: scope-body \ A testscript file is an implicit group scope (see \l{#model Model and Execution} for details). \h#syntax-scope|Scope| \ scope-body: *setup *(scope|directive|test) *tdown scope: ?description scope-block|scope-if scope-block: '{' scope-body '}' \ A scope is either a test group scope or an explicit test scope. An explicit scope is a test scope if it contains a single test, only variable assignments in setup commands, no teardown commands, and only the scope having the description, if any. Otherwise, it is a group scope. If there is no explicit scope for a test, one is established implicitly. \h#syntax-scope-if|Scope-If| \ scope-if: ('if'|'if!') command-line scope-block *scope-elif ?scope-else scope-elif: ('elif'|'elif!') command-line scope-block scope-else: 'else' scope-block \ A scope, either test or group, can be executed conditionally. The condition \c{command-line} is executed in the context of the outer scope. Note that all the scopes in an \c{if-else} chain are alternative implementations of the same group/test (thus the single description). If at least one of them is a group scope, then all the others are treated as groups as well. \h#syntax-directive|Directive| \ directive: '.' include \ A line that starts with \c{.} is a Testscript directive. Note that directives are evaluated during parsing, before any command is executed or (testscript) variable is assigned. You can, however, use variables assigned in the buildfile. For example: \ include common-$(cxx.target.class).testscript \ \h2#syntax-directive-include|Include| \ include: 'include' (' '+'--once')*(' '+)* \ While in the grammar the \c{include} directive is shown to only appear interleaving with scopes and tests, it can be used anywhere in the scope body. It can also contain several parts of a scope, for example, setup and test lines. The \c{--once} option signals that files that have already been included in this scope should not be included again. The implementation is not required to handle links when determining if two paths are to the same file. Relative paths are assumed to be relative to the including testscript file. \h#syntax-setup-teardown|Setup and Teardown| \ setup: variable-like|setup-line tdown: variable-like|tdown-line setup-line: '+' command-like tdown-line: '-' command-like \ Note that variable assignments (including \c{variable-if}) do not use the \c{'+'} and \c{'-'} prefixes. A standalone (not part of a test) variable assignment is automatically treated as a setup if no tests have yet been encountered in this scope and as a teardown otherwise. \h#syntax-test|Test| \ test: ?description +(variable-line|command-like) \ A test that contains multiple lines is called \i{compound}. In this case each (logical) line except the last must end with a semicolon to signal the test continuation. For example: \ conf = test.conf; cat <'verbose = true' >=$conf; test1 $conf \ \h#syntax-variable|Variable| \ variable-like: variable-line|variable-if variable-line: ('='|'+='|'=+') value-attributes? ';'? value-attributes: '[' ']' \ The Testscript variable assignment semantics is equivalent to Buildfile except that no \c{{\}}-based name-generation is performed. For example: \ args = [strings] foo bar 'fox baz' echo$args # foo bar fox baz \ The value can only be followed by \c{;} inside a test to signal the test continuation. \h#syntax-variable-if|Variable-If| \ variable-if: ('if'|'if!') command-line variable-if-body *variable-elif ?variable-else 'end' variable-elif: ('elif'|'elif!') command-line variable-if-body variable-else: 'else' variable-if-body variable-if-body: *variable-like \ A group of variables can be set conditionally. The condition \c{command-line} semantics is the same as in \c{scope-if}. For example: \ if ($cxx.target.class == 'windows') slash = \\\\ case = false else slash = / case = true end \ When conditionally setting a single variable, using the evaluation context with a ternary operator is often more concise: \ slash = ($cxx.target.class == 'windows' ? \\\\ : /) \ Note also that the only purpose of having a separate (from \c{command-if}) variable-only if-block is to remove the error-prone requirement of having to specify \c{+} and \c{-} prefixes in group setup/teardown. \h#syntax-command|Command| \ command-like: command-line|command-if command-line: command-expr (';'|(':' ))? *here-document command-expr: command-pipe (('||'|'&&') command-pipe)* command-pipe: command ('|' command)* command: (' '+(|redirect|cleanup))* command-exit? command-exit: ('=='|'!=') \ A command line is a command expression. If it appears directly (as opposed to inside \c{command-if}) in a test, then it can be followed by \c{;} to signal the test continuation or by \c{:} and the trailing description. A command expression can combine several command pipes with logical AND and OR operators. Note that the evaluation order is always from left to right (left-associative), both operators have the same precedence, and are short-circuiting. Note, however, that short-circuiting does not apply to expansions (variable, function calls, evaluation contexts). The logical result of a command expression is the result of the last command pipe executed. A command pipe can combine several commands with a pipe (\c{stdout} of the left-hand-side command is connected to \c{stdin} of the right-hand-side). The logical result of a command pipe is the logical AND of all its commands. A command begins with a command path followed by options/arguments, redirects, and cleanups, all optional and in any order. A command may specify an exit code check. If executing a command results in an abnormal process termination, then the whole outer construct (e.g., test, setup/teardown, etc) summarily fails. Otherwise (that is, in case of a normal termination), the exit code is checked. If omitted, then the test is expected to succeed (0 exit code). The logical result of executing a command is therefore a boolean value which is used in the higher-level constructs (pipe and expression). \h#syntax-command-if|Command-If| \ command-if: ('if'|'if!') command-line command-if-body *command-elif ?command-else 'end' (';'|(':' ))? command-elif: ('elif'|'elif!') command-line command-if-body command-else: 'else' command-if-body command-if-body: *(variable-line|command-like) \ A group of commands can be executed conditionally. The condition \c{command-line} semantics is the same as in \c{scope-if}. Note that in a compound test, commands inside \c{command-if} must not end with \c{;}. Rather, \c{;} may follow \c{end}. For example: \ if ($cxx.target.class == 'windows') foo = windows setup1 setup2 else foo = posix end; test1$foo \ \h#syntax-redirect|Redirect| \ redirect: stdin|stdout|stderr stdin: '0'?(in-redirect) stdout: '1'?(out-redirect) stderr: '2'(out-redirect) \ In redirects the file descriptors must not be separated from the redirect operators with whitespaces. And if leading text is not separated from the redirect operators, then it is expected to be the file descriptor. As an example, the first command below has \c{2} as an argument (and therefore redirects \c{stdout}, not \c{stderr}). While the second is invalid since \c{a1} is not a valid file descriptor. \ $* 2 >-$* a1>- \ \h#syntax-in-redirect|Input Redirect| \ in-redirect: '<-'|\ '<|'|\ ('<<<'|'<=') |\ ('<<'|'<<='){':'?'/'?} |\ ('<'|'<<<='){':'?'/'?} \ The \c{stdin} data can come from a pipe, here-string (\c{<}), here-document (\c{<<}), a file (\c{<<<}), or \c{/dev/null}-equivalent (\c{<-}). Specifying both a pipe and a redirect is an error. If no pipe or \c{stdin} redirect is specified and the test tries to read from \c{stdin}, it is considered to have failed (unexpected input). However, whether this is detected and diagnosed is implementation-defined. To allow reading from the default \c{stdin} (for instance, if the test is really an example), the \c{<|} redirect is used. \N|The \c{<=}, \c{<<=}, and \c{<<<=} redirects are a stable syntax across various \c{build2} scripting language flavors (Testscript, Buildscript, etc). While the \c{<}, \c{<<}, and \c{<<<} redirects are their Testscript aliases with the mapping chosen to be more convenient for this flavor of the scripting language. This mapping is as follows: \ < <<<= here-string << <<= here-document <<< <= file \ | Here-string and here-document redirects may specify the following redirect modifiers: The \c{:} modifier is used to suppress the otherwise automatically-added terminating newline. The \c{/} modifier causes all the forward slashes in the here-string or here-document to be translated to the directory separator of the test target platform (as indicated by \c{test.target}). A here-document redirect must be specified \i{literally} on the command line. Specifically, it must not be the result of an expansion (which rarely makes sense anyway since the following here-document fragment itself cannot be the result of an expansion either). See \l{#syntax-here-document Here Document} for details. \h#syntax-in-output|Output Redirect| \ out-redirect: '>-'|\ '>|'|\ '>!'|\ '>=' |\ '>+' |\ '>&' ('1'|'2')|\ ('>>>'|'>?') |\ ('>>'|'>>?'){':'?'/'?}'~'? |\ ('>'|'>>>?'){':'?'/'?}'~'? \ The \c{stdout} and \c{stderr} data can go to a pipe (\c{stdout} only), file (\c{>=} to overwrite and \c{>+} to append), or \c{/dev/null}-equivalent (\c{>-}). It can also be compared to a here-string (\c{>}), a here-document (\c{>>}), or a file contents (\c{>>>}). For \c{stdout} specifying both a pipe and a redirect is an error. A test that tries to write to an un-redirected stream (either \c{stdout} or \c{stderr}) is considered to have failed (unexpected output). To allow writing to the default \c{stdout} or \c{stderr} (for instance, if the test is really an example), the \c{>|} redirect is used. \N|The \c{>?}, \c{>>?}, and \c{>>>?} redirects are a stable syntax across various \c{build2} scripting language flavors (Testscript, Buildscript, etc). While the \c{>}, \c{>>}, and \c{>>>} redirects are their Testscript aliases with the mapping chosen to be more convenient for this flavor of the scripting language. This mapping is as follows: \ > >>>? here-string comparison >> >>? here-document comparison >>> >? file contents comparison \ | The \c{>!} redirect acts like \c{>-} if the build system verbosity level is below 2 and as \c{>|} otherwise. It is normally used to ignore diagnostics (as opposed to data) during normal operation but to still be able to examine it, for example, when debugging a failing test. It is also possible to merge \c{stderr} to \c{stdout} or vice versa with a merge redirect (\c{>&}). In this case the left-hand-side descriptor (implied or explicit) must not be the same as the right-hand-side. Having both merge redirects at the same time is an error. The \c{:} and \c{/} redirect modifiers have the same semantics as in the input redirects. The \c{~} modifier is used to indicate that the following here-string/here-document is a regular expression (see \l{#syntax-regex Regex}) rather than a literal. Note that if present, it must be specified last. Similar to the input redirects, an output here-document redirect must be specified literally on the command line. See \l{#syntax-here-document Here Document} for details. \h#syntax-here-document|Here-Document| \ here-document: * \ A here-document can be used to supply data to \c{stdin} or to compare output to the expected result for \c{stdout} and \c{stderr}. The order of here-document fragments must match the order of redirects, for example: \ : select-no-table-error $* --interactive >>EOO <>EOE enter query: EOO SELECT * FROM no_such_table EOI error: no such table 'no_such_table' EOE \ Two or more here-document redirects can use the same end marker. In this case all the redirects must have the same modifiers, if any. Only the here-document fragment corresponding to the first occurrence of the end marker must be present (called \i{shared} here-document) with the subsequent redirects reusing the same data. This mechanism is primarily useful for round-trip testing, for example: \ : xml-round-trip$* <>EOD Hello, World! EOD \ Here-strings can be single-quoted literals or double-quoted with expansion. This semantics is extended to here-documents as follows: If the end marker on the command line is single-quoted, then the here-document lines are parsed as if they were single-quoted except that the single quote itself is not treated as special. In this mode there are no expansions, escape sequences, not even line continuations \- each line is taken literally. If the end marker on the command line is double-quoted, then the here-document lines are parsed as if they were double-quoted except that the double quote itself is not treated as special. In this mode we can use variable expansions, function calls, and evaluation contexts. However, we have to escape the \c{$(\\} character set. If the end marker is not quoted then it is treated as if it were single-quoted. Note also that quoted end markers must be quoted entirely, that is, from the beginning and until the end and without any interruptions. Here-document fragments can be indented. The leading whitespaces of the end marker line (called \i{strip prefix}) determine the indentation. Every other line in the here-document should start with this prefix which is then automatically stripped. The only exception is a blank line. For example, the following two testscripts are equivalent: \ {$* <~'/fo+/' 2>>~/EOE/ /ba+r/ baz EOE \ The regular expression used for output matching is \i{two-level}. At the outer level the expression is over lines with each line treated as a single character. We will refer to this outer expression as \i{line-regex} and to its characters as \i{line-char}. A line-char can be a literal line (like \c{baz} in the example above) in which case it will only be equal to an identical line in the output. Alternatively, a line-char can be an inner level regex (like \c{ba+r} above) in which case it will be equal to any line in the output that matches this regex. Where not clear from context we will refer to this inner expression as \i{char-regex} and its characters as \i{char}. A line is treated as literal unless it starts with the \i{regex introducer character} (\c{/} in the above example). In contrast, the line-regex is always in effect (in a sense, the \c{~} modifier is its introducer). Note that the here-string regex naturally (since there is only one line) must start with an introducer. A char-regex line that starts with an introducer must also end with one optionally followed by \i{match flags}, for example: \ $* >>~/EOO/ /ba+r/i /ba+z/i EOO \ The following match flags are recognized: \dl| \li|\n\c{i} Perform case-insensitive match.| \li|\n\c{d} Invert the dot character (\c{.}) escaping. With this flag unescaped dots are treated as literal characters while the escaped ones (\c{\\.}) \- as matching any character. Note that dots specified within character classes (\c{[.]}) are not affected.|| Any character can act as a regex introducer. For here-strings it is the first character in the string. For here-documents the introducer is specified as part of the end marker. In this case the first character is the introducer, everything after that and until the second occurrence of the introducer is the actual end marker, and everything after that are global match flags. Global match flags apply to every char-regex (but not literal lines or the line-regex itself) in this here-document. Note that there is no way to escape the introducer character inside the regex. As an example, here is a shorter version of the previous example that also uses a different introducer character. \$* >>~%EOO%i %ba+r% %ba+z% EOO \ A line-char is treated as an ordinary, non-syntax character with regards to the outer-level line-regex. Lines that start with a regex introducer but do not end with one are used to specify syntax line-chars. Such syntax line-chars can also be specified after (or instead of) match flags. For example: \ $* >>~/EOO/ /( /fo+x/| /ba+r/| /ba+z/ /)+ EOO \ As an illustration, if we call the \c{/fo+x/} expression \c{A}, \c{/ba+r/} \- \c{B}, and \c{/ba+z/} \- C, then we can represent the above line-regex in the following more traditional form: \ (A|B|C)+ \ Only characters from the \c{.()|*+?{\}\\0123456789,=!} set are allowed as syntax line-chars with the presence of any other characters being an error. A blank line as well as the \c{//} sequence (assuming \c{/} is the introducer) are treated as an empty line-char. For the purpose of matching, newlines are viewed as separators rather than being part of a line. In particular, in this model, the customary trailing newline at the end of the output introduces a trailing empty line-char. As a result, unless the \c{:} (no newline) redirect modifier is used, an empty line-char is implicitly added at the end of line-regex. \h#syntax-cleanup|Cleanup| \ cleanup: ('&'|'&?'|'&!') (|) \ If a command creates extra files or directories, then they can be registered for automatic cleanup at the end of the scope (test or group). Files mentioned in redirects are registered automatically. Additionally, certain builtins (for example \c{touch} and \c{mkdir}) also register their output files/directories automatically (as described in each builtin's documentation). If the path ends with a directory separator (slash), then it is assumed to be a directory. Otherwise \- a file. A directory about to be removed must be empty (no unexpected output). The \c{&} syntax registers a normal or \i{always} cleanup: the test fails if the file/directory does not exist. The \c{&?} syntax is a \i{maybe} cleanup: the file/directory is removed if it exists. Finally, \c{&!} is a \i{never} cleanup: it disables a previously registered cleanup for this file/directory (primarily used to disable automatic cleanups registered by builtins). The path components may contain the \c{*} and \c{?} wildcard characters with the following semantics: \ ? - any single character * - all immediate files */ - all immediate sub-directories (which must be empty) ** - all files recursively **/ - all sub-directories recursively (which must be empty) ***/ - all sub-directories recursively (which must be empty) as well as the start directory itself \ In addition, if the last component in the path is \c{***} (without trailing directory separator), then it matches all files and sub-directories recursively as well as the start directory itself. For example, the following cleanup will remove \c{dir/} and its content recursively. \$* &dir/*** \ Registering a path for cleanup that is outside the script working directory is an error. You can, however, clean them up manually with \c{rm/rmdir -f}. \h#syntax-description|Description| \ description: +(':' ) \ Description lines start with a colon (\c{:}) and are used to document tests and test groups. In a sense they are formalized comments. A description can be \i{leading}, that is, specified before the test or group. For tests it can also be \i{trailing} \- specified as a single line after the (last) command of the test. It is an error to specify both leading and trailing descriptions. By convention the leading description has the following format with all three components being optional. \ : : : :