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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2007-2025. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(interactive_shell_SUITE).
-include_lib("kernel/include/file.hrl").
-include_lib("common_test/include/ct.hrl").
%% Things to add tests for:
%% - TERM=dumb
%% - Editing line > MAXSIZE (1 << 16)
%% - \t tests (use io:format("\t"))
%% - xn fix after Delete and Backspace
%% - octal_to_hex > 255 length (is this possible?)
%% 1222 0 : } else if (lastput == 0) { /* A multibyte UTF8 character */
%% 1223 0 : for (i = 0; i < ubytes; ++i) {
%% 1224 0 : outc(ubuf[i]);
%% 1225 : }
%% 1226 : } else {
%% 1227 0 : outc(lastput);
%% - $TERM set to > 1024 long value
-export([all/0, suite/0, groups/0, init_per_suite/1, end_per_suite/1,
init_per_group/2, end_per_group/2,
init_per_testcase/2, end_per_testcase/2,
get_columns_and_rows/1, exit_initial/1, job_control_local/1,
job_control_remote/1,stop_during_init/1,wrap/1,
shell_history/1, shell_history_resize/1, shell_history_eaccess/1,
shell_history_repair/1, shell_history_repair_corrupt/1,
shell_history_corrupt/1,
shell_history_custom/1, shell_history_custom_errors/1,
job_control_remote_noshell/1,ctrl_keys/1,
get_columns_and_rows_escript/1,
shell_get_password/1,
shell_navigation/1, shell_multiline_navigation/1, shell_multiline_prompt/1,
shell_xnfix/1, shell_delete/1,
shell_transpose/1, shell_search/1, shell_insert/1,
shell_update_window/1, shell_small_window_multiline_navigation/1, shell_huge_input/1,
shell_invalid_unicode/1, shell_support_ansi_input/1,
shell_invalid_ansi/1, shell_suspend/1, shell_full_queue/1,
shell_unicode_wrap/1, shell_delete_unicode_wrap/1,
shell_delete_unicode_not_at_cursor_wrap/1,
shell_expand_location_above/1,
shell_expand_location_below/1,
shell_update_window_unicode_wrap/1,
shell_receive_standard_out/1,
shell_standard_error_nlcr/1, shell_clear/1,
shell_format/1, shell_help/1,
remsh_basic/1, remsh_error/1, remsh_longnames/1, remsh_no_epmd/1,
remsh_dont_terminate_remote/1,
remsh_expand_compatibility_25/1, remsh_expand_compatibility_later_version/1,
external_editor/1, external_editor_visual/1,
external_editor_unicode/1, shell_ignore_pager_commands/1]).
-export([test_invalid_keymap/1, test_valid_keymap/1]).
%% Exports for custom shell history module
-export([load/0, add/1]).
%% For custom prompt testing
-export([prompt/1]).
-record(tmux, {peer, node, name, orig_location }).
suite() ->
[{ct_hooks,[ts_install_cth]},
{timetrap,{minutes,3}}].
all() ->
[{group, to_erl},
{group, tty}].
groups() ->
[{to_erl,[],
[get_columns_and_rows_escript,get_columns_and_rows,
exit_initial, job_control_local,
job_control_remote, job_control_remote_noshell,
ctrl_keys, stop_during_init, wrap,
shell_invalid_ansi,
shell_get_password,
{group, shell_history},
{group, remsh}]},
{shell_history, [],
[shell_history,
shell_history_resize,
shell_history_eaccess,
shell_history_repair,
shell_history_repair_corrupt,
shell_history_corrupt,
{group, sh_custom}
]},
{sh_custom, [],
[shell_history_custom,
shell_history_custom_errors]},
{remsh, [],
[remsh_basic,
remsh_error,
remsh_longnames,
remsh_no_epmd,
remsh_dont_terminate_remote,
remsh_expand_compatibility_25,
remsh_expand_compatibility_later_version]},
{tty,[],
[{group,tty_unicode},
{group,tty_latin1},
test_invalid_keymap, test_valid_keymap,
shell_suspend,
shell_full_queue,
external_editor,
external_editor_visual,
shell_ignore_pager_commands
]},
{tty_unicode,[parallel],
[{group,tty_tests},
shell_invalid_unicode,
external_editor_unicode
%% unicode wrapping does not work right yet
%% shell_unicode_wrap,
%% shell_delete_unicode_wrap,
%% shell_delete_unicode_not_at_cursor_wrap,
%% shell_update_window_unicode_wrap
]},
{tty_latin1,[],[{group,tty_tests}]},
{tty_tests, [parallel],
[shell_navigation, shell_multiline_navigation, shell_multiline_prompt,
shell_xnfix, shell_delete, shell_format, shell_help,
shell_transpose, shell_search, shell_insert,
shell_update_window, shell_small_window_multiline_navigation, shell_huge_input,
shell_support_ansi_input,
shell_receive_standard_out,
shell_standard_error_nlcr,
shell_expand_location_above,
shell_expand_location_below,
shell_clear]}
].
init_per_suite(Config) ->
Term = os:getenv("TERM", "dumb"),
os:putenv("TERM", "vt100"),
[{term,Term}|Config].
end_per_suite(Config) ->
Term = proplists:get_value(term,Config),
os:putenv("TERM",Term),
ok.
init_per_group(to_erl, Config) ->
case rtnode:get_progs() of
{error, Error} ->
{skip, Error};
_ ->
DefShell = rtnode:get_default_shell(),
[{default_shell,DefShell}|Config]
end;
init_per_group(remsh, Config) ->
case proplists:get_value(default_shell, Config) of
old -> {skip, "Not supported in old shell"};
new -> Config
end;
init_per_group(shell_history, Config) ->
case proplists:get_value(default_shell, Config) of
old -> {skip, "Not supported in old shell"};
new -> Config
end;
init_per_group(tty, Config) ->
case string:split(tmux("-V")," ") of
["tmux",[Num,$.|_]] when Num >= $3, Num =< $9 ->
tmux("kill-session"),
"" = tmux("-u new-session -x 50 -y 60 -d"),
["" = tmux(["set-environment '",Name,"' '",Value,"'"])
|| {Name,Value} <- os:env()],
Config;
["tmux", Vsn] ->
{skip, "invalid tmux version " ++ Vsn ++ ". Need vsn 3 or later"};
Error ->
{skip, "tmux not installed " ++ Error}
end;
init_per_group(Group, Config) when Group =:= tty_unicode;
Group =:= tty_latin1 ->
[Lang,_] =
string:split(
os:getenv("LC_ALL",
os:getenv("LC_CTYPE",
os:getenv("LANG","en_US.UTF-8"))),"."),
case Group of
tty_unicode ->
[{encoding, unicode},{env,[{"LC_ALL",Lang++".UTF-8"}]}|Config];
tty_latin1 ->
% [{encoding, latin1},{env,[{"LC_ALL",Lang++".ISO-8859-1"}]}|Config],
{skip, "latin1 tests not implemented yet"}
end;
init_per_group(sh_custom, Config) ->
%% Ensure that ERL_AFLAGS will not override the value of the shell_history variable.
{ok, Peer, Node} = ?CT_PEER(["-noshell","-kernel","shell_history","not_overridden"]),
try erpc:call(Node, application, get_env, [kernel, shell_history], rtnode:timeout(normal)) of
{ok, not_overridden} ->
Config;
_ ->
SkipText = "shell_history variable is overridden (probably by ERL_AFLAGS)",
{skip, SkipText}
catch
C:R:Stk ->
io:format("~p\n~p\n~p\n", [C,R,Stk]),
{skip, "Unexpected error"}
after
peer:stop(Peer)
end;
init_per_group(_GroupName, Config) ->
Config.
end_per_group(tty, _Config) ->
Windows = string:split(tmux("list-windows"), "\n", all),
lists:foreach(
fun(W) ->
case string:split(W, " ", all) of
["0:" | _] -> ok;
[No, _Name | _] ->
"" = os:cmd(["tmux select-window -t ", string:split(No,":")]),
ct:log("~ts~n~ts",[W, os:cmd(lists:concat(["tmux capture-pane -p -e"]))])
end
end, Windows),
% "" = os:cmd("tmux kill-session")
ok;
end_per_group(_GroupName, Config) ->
Config.
init_per_testcase(Func, Config) ->
Path = [Func,
[proplists:get_value(name,P) ||
P <- [proplists:get_value(tc_group_properties,Config,[])] ++
proplists:get_value(tc_group_path,Config,[])]],
[{tc_path, lists:concat(lists:join("-",lists:flatten(Path)))} | Config].
end_per_testcase(_Case, Config) ->
GroupProperties = proplists:get_value(tc_group_properties, Config),
case (GroupProperties =/= false) andalso proplists:get_value(parallel, GroupProperties, false) of
true -> ok;
false ->
%% Terminate any connected nodes. They may disturb test cases that follow.
lists:foreach(fun(Node) ->
catch erpc:call(Node, erlang, halt, [])
end, nodes()),
ok
end.
%%-define(DEBUG,1).
-ifdef(DEBUG).
-define(dbg(Data),ct:pal("~p",[Data])).
-else.
-define(dbg(Data),noop).
-endif.
string_to_term(Str) ->
{ok,Tokens,_EndLine} = erl_scan:string(Str ++ "."),
{ok,AbsForm} = erl_parse:parse_exprs(Tokens),
{value,Value,_Bs} = erl_eval:exprs(AbsForm, erl_eval:new_bindings()),
Value.
run_unbuffer_escript(Rows, Columns, EScript, NoTermStdIn, NoTermStdOut) ->
DataDir = filename:join(filename:dirname(code:which(?MODULE)), "interactive_shell_SUITE_data"),
TmpFile = filename:join(DataDir, "tmp"),
ok = file:write_file(TmpFile, <<>>),
CommandModifier =
case {NoTermStdIn, NoTermStdOut} of
{false, false} -> "";
{true, false} -> io_lib:format(" < ~s", [TmpFile]);
{false, true} -> io_lib:format(" > ~s ; cat ~s", [TmpFile, TmpFile]);
{true, true} -> io_lib:format(" > ~s < ~s ; cat ~s", [TmpFile, TmpFile, TmpFile])
end,
Command = io_lib:format("unbuffer -p bash -c \"stty rows ~p; stty columns ~p; escript ~s ~s\"",
[Rows, Columns, EScript, CommandModifier]),
%% io:format("Command: ~s ~n", [Command]),
Out = os:cmd(Command),
%% io:format("Out: ~p ~n", [Out]),
string_to_term(Out).
get_columns_and_rows_escript(Config) when is_list(Config) ->
ExpectUnbufferInstalled =
try
"79" = string:trim(os:cmd("unbuffer -p bash -c \"stty columns 79 ; tput cols\"")),
true
catch
_:_ -> false
end,
case ExpectUnbufferInstalled of
false ->
{skip,
"The unbuffer tool (https://core.tcl-lang.org/expect/index) does not seem to be installed.~n"
"On Ubuntu/Debian: \"sudo apt-get install expect\""};
true ->
DataDir = filename:join(filename:dirname(code:which(?MODULE)), "interactive_shell_SUITE_data"),
IoColumnsErl = filename:join(DataDir, "io_columns.erl"),
IoRowsErl = filename:join(DataDir, "io_rows.erl"),
[
begin
{ok, 42} = run_unbuffer_escript(99, 42, IoColumnsErl, NoTermStdIn, NoTermStdOut),
{ok, 99} = run_unbuffer_escript(99, 42, IoRowsErl, NoTermStdIn, NoTermStdOut)
end
||
{NoTermStdIn, NoTermStdOut} <- [{false, false}, {true, false}, {false, true}]
],
{error,enotsup} = run_unbuffer_escript(99, 42, IoRowsErl, true, true),
{error,enotsup} = run_unbuffer_escript(99, 42, IoColumnsErl, true, true),
ok
end.
%% Test that the shell can access columns and rows.
get_columns_and_rows(Config) when is_list(Config) ->
case proplists:get_value(default_shell, Config) of
old ->
test_columns_and_rows(old, []);
new ->
test_columns_and_rows(old, ["-oldshell"]),
test_columns_and_rows(new, [])
end,
ok.
test_columns_and_rows(old, Args) ->
rtnode:run(
[{putline, ""},
{putline, "2."},
{expect, "2\r\n"},
{putline, "io:columns()."},
{expect, "{error,enotsup}\r\n"},
{putline, "io:rows()."},
{expect, "{error,enotsup}\r\n"}
], [], [], Args),
rtnode:run(
[{putline, ""},
{putline, "2."},
{expect, "2\r\n"},
{putline, "io:columns()."},
{expect, "{ok,90}\r\n"},
{putline,"io:rows()."},
{expect, "{ok,40}\r\n"}],
[],
"stty rows 40; stty columns 90; ",
Args);
test_columns_and_rows(new, _Args) ->
rtnode:run(
[{putline, ""},
{expect, "1> $"},
{putline, "2."},
{expect, "\r\n2\r\n"},
{expect, "> $"},
{putline, "io:columns()."},
{expect, "{ok,80}\r\n"},
{expect, "> $"},
{putline, "io:rows()."},
{expect, "\r\n{ok,24}\r\n"}
]),
rtnode:run(
[{putline, ""},
{expect, "1> $"},
{putline, "2."},
{expect, "\r\n2\r\n"},
{expect, "> $"},
{putline, "io:columns()."},
{expect, "\r\n{ok,90}\r\n"},
{expect, "> $"},
{putline, "io:rows()."},
{expect, "\r\n{ok,40}\r\n"}],
[],
"stty rows 40; stty columns 90; ").
shell_navigation(Config) ->
Term = start_tty(Config),
try
[begin
send_tty(Term,"{aaa,'b"++U++"b',ccc}"),
check_location(Term, {0, 0}), %% Check that cursor jump backward
check_content(Term, "{aaa,'b"++U++"b',ccc}$"),
timer:sleep(1000), %% Wait for cursor to jump back
check_location(Term, {0, width("{aaa,'b"++U++"b',ccc}")}),
send_tty(Term,"Home"),
check_location(Term, {0, 0}),
send_tty(Term,"End"),
check_location(Term, {0, width("{aaa,'b"++U++"b',ccc}")}),
send_tty(Term,"Left"),
check_location(Term, {0, width("{aaa,'b"++U++"b',ccc")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, width("{aaa,'b"++U++"b',")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, width("{aaa,")}),
send_tty(Term,"C-Right"),
check_location(Term, {0, width("{aaa,'b"++U++"b'")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, width("{aaa,")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, width("{")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, 0}),
send_tty(Term,"C-E"),
check_location(Term, {0, width("{aaa,'b"++U++"b',ccc}")}),
send_tty(Term,"C-H"),
check_location(Term, {0, width("{aaa,'b"++U++"b',ccc")}),
send_tty(Term,"C-A"),
check_location(Term, {0, 0}),
send_tty(Term,"Enter")
end || U <- hard_unicode()],
ok
after
stop_tty(Term)
end.
shell_multiline_navigation(Config) ->
Term = start_tty(Config),
try
[begin
check_location(Term, {0, 0}),
send_tty(Term,"{aaa,"),
check_location(Term, {0,width("{aaa,")}),
send_tty(Term,"\n'b"++U++"b',"),
check_location(Term, {0, width("'b"++U++"b',")}),
send_tty(Term,"\nccc}"),
check_location(Term, {-2, 0}), %% Check that cursor jump backward (blink)
timer:sleep(1000), %% Wait for cursor to jump back
check_location(Term, {0, width("ccc}")}),
send_tty(Term,"Home"),
check_location(Term, {0, 0}),
send_tty(Term,"End"),
check_location(Term, {0, width("ccc}")}),
send_tty(Term,"Left"),
check_location(Term, {0, width("ccc")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, 0}),
send_tty(Term,"C-Left"),
check_location(Term, {-1, width("'b"++U++"b',")}),
send_tty(Term,"C-Left"),
check_location(Term, {-1, 0}),
%send_tty(Term,"C-Left"),
%check_location(Term, {-1, 0}),
%send_tty(Term,"C-Right"),
%check_location(Term, {-1, 1}),
send_tty(Term,"C-Right"),
check_location(Term, {-1, width("'b"++U++"b'")}),
send_tty(Term,"C-Up"),
check_location(Term, {-2, width("{aaa,")}),
send_tty(Term,"C-Down"),
send_tty(Term,"C-Down"),
check_location(Term, {0, width("ccc}")}),
send_tty(Term,"Left"),
send_tty(Term,"C-Up"),
check_location(Term, {-1, width("'b"++U)}),
send_tty(Term,"M-<"),
check_location(Term, {-2, 0}),
send_tty(Term,"M->"),
send_tty(Term,"Left"),
check_location(Term, {0,width("ccc")}),
send_tty(Term,"Enter"),
send_tty(Term,"Right"),
check_location(Term, {0,0}),
send_tty(Term,"C-h"), % Backspace
check_location(Term, {-1,width("ccc}")}),
send_tty(Term, "C-Up"),
send_tty(Term, "End"),
send_tty(Term,"Left"),
send_tty(Term,"M-Enter"),
check_content(Term, ".. ,"),
check_location(Term, {-1,0}),
send_tty(Term,"M-c"),
check_location(Term, {-3,0}),
send_tty(Term,"{'"++U++"',\n\n\nworks}.\n")
end || U <- hard_unicode()],
ok
after
stop_tty(Term)
end.
shell_format(Config) ->
Term1 = start_tty([{args,["-stdlib","format_shell_func","{shell,erl_pp_format_func}"]}|Config]),
DataDir = proplists:get_value(data_dir, Config),
EmacsFormat = "\""++DataDir ++ "emacs-format-file\"\n",
try
send_tty(Term1,"fun(X) ->\n X\nend.\n"),
send_tty(Term1,"Up"),
%% Note, erl_pp puts 7 spaces before X
check_content(Term1, "fun\\(X\\) ->\\s*.. X\\s*.. end."),
send_tty(Term1, "Down"),
tmux(["resize-window -t ",tty_name(Term1)," -x ",200]),
timer:sleep(1000),
send_stdin(Term1, "shell:format_shell_func(\"emacs -batch \${file} -l \"\n"),
send_stdin(Term1, EmacsFormat),
send_stdin(Term1, "\" -f emacs-format-function\").\n"),
check_content(Term1, "{shell,erl_pp_format_func}"),
send_tty(Term1, "Up"),
send_tty(Term1, "Up"),
send_tty(Term1, "\n"),
timer:sleep(1000),
send_tty(Term1, "Up"),
%% Note, emacs-format puts 8 spaces before X
check_content(Term1, "fun\\(X\\) ->\\s*.. X\\s*.. end."),
send_tty(Term1, "Down"),
check_content(Term1, ">$"),
send_stdin(Term1, "shell:format_shell_func({bad,format}).\n"),
send_tty(Term1, "Up"),
send_tty(Term1, "Up"),
send_tty(Term1, "\n"),
timer:sleep(1000),
check_content(Term1, "\\Q* Bad format function: {bad,format}\\E"),
send_tty(Term1, "Up"),
%% No modifications should be made, when default format function is used
check_content(Term1, "fun\\(X\\) ->\\s*.. X\\s*.. end."),
ok
after
stop_tty(Term1)
end.
shell_multiline_prompt(Config) ->
Term1 = start_tty([{args,["-stdlib","shell_multiline_prompt","{shell,inverted_space_prompt}"]}|Config]),
Term2 = start_tty([{args,["-stdlib","shell_multiline_prompt","\"...> \""]}|Config]),
Term3 = start_tty([{args,["-stdlib","shell_multiline_prompt","edlin"]}|Config]),
Term4 = start_tty(Config),
try
check_location(Term1, {0, 0}),
send_tty(Term1,"\na"),
check_location(Term1, {0, 1}),
check_content(Term1, " a"),
ok
after
stop_tty(Term1)
end,
try
check_location(Term2, {0, 0}),
send_tty(Term2,"\na"),
check_location(Term2, {0, 1}),
check_content(Term2, "...> a"),
ok
after
stop_tty(Term2)
end,
try
send_tty(Term3,"\na"),
check_location(Term3, {0, 1}),
check_content(Term3, ".. a"),
ok
after
stop_tty(Term3)
end,
try
send_tty(Term4, "shell:multiline_prompt_func(\"-> \").\n"),
check_content(Term4, "default"),
send_tty(Term4, "a\nb"),
check_content(Term4, "-> b"),
ok
after
stop_tty(Term4)
end.
shell_clear(Config) ->
Term = start_tty(Config),
{Rows, _Cols} = get_window_size(Term),
try
send_tty(Term,"foobar."),
send_tty(Term,"Enter"),
check_content(Term, "foobar"),
check_location(Term, {0, 0}),
send_tty(Term,"bazbat"),
check_location(Term, {0, 6}),
send_tty(Term,"C-L"),
check_location(Term, {-Rows+1, 6}),
check_content(Term, "bazbat")
after
stop_tty(Term)
end.
shell_xnfix(Config) ->
Term = start_tty(Config),
{_Rows, Cols} = get_window_size(Term),
{_Row, Col} = get_location(Term),
As = lists:duplicate(Cols - Col - 1,"a"),
try
[begin
check_location(Term, {0, 0}),
send_tty(Term,As),
check_content(Term,[As,$$]),
check_location(Term, {0, Cols - Col - 1}),
send_tty(Term,"a"),
check_location(Term, {0, -Col}),
send_tty(Term,"aaa"),
check_location(Term, {0, -Col + 3}),
[send_tty(Term,"Left") || _ <- lists:seq(1,3 + width(U))],
send_tty(Term,U),
%% a{Cols-1}U\naaaaa
check_content(Term,[lists:duplicate(Cols - Col - 1 - width(U),$a),
U,"\n",lists:duplicate(3+width(U), $a),"$"]),
check_location(Term, {0, -Col}),
send_tty(Term,"Left"),
send_tty(Term,U),
%% a{Cols-1}U\nUaaaaa
check_content(Term,[lists:duplicate(Cols - Col - 1 - width(U),$a),
U,"\n",U,lists:duplicate(3+width(U), $a),"$"]),
check_location(Term, {0, -Col}),
%% send_tty(Term,"Left"),
%% send_tty(Term,"BSpace"),
%% a{Cols-2}U\nUaaaaa
%% check_content(Term,[lists:duplicate(Cols - Col - 2 - width(U),$a),
%% U,"\n",U,lists:duplicate(3+width(U), $a),"$"]),
%% send_tty(Term,"BSpace"),
%% check_content(Term,[lists:duplicate(Cols - Col - 1 - width(U),$a),
%% U,U,"\n",lists:duplicate(3+width(U), $a),"$"]),
%% send_tty(Term,"aa"),
%% check_content(Term,[lists:duplicate(Cols - Col - 2 - width(U),$a),
%% U,"a\n",U,lists:duplicate(3+width(U), $a),"$"]),
%% check_location(Term, {0, -Col}),
send_tty(Term,"C-K"),
check_location(Term, {0, -Col}),
send_tty(Term,"C-A"),
check_location(Term, {-1, 0}),
send_tty(Term,"C-E"),
check_location(Term, {0, -Col}),
send_tty(Term,"Enter"),
ok
end || U <- hard_unicode()]
after
stop_tty(Term)
end.
%% Characters that are larger than 2 wide need special handling when they
%% are at the end of the current line.
shell_unicode_wrap(Config) ->
Term = start_tty(Config),
{_Rows, Cols} = get_window_size(Term),
{_Row, Col} = get_location(Term),
try
[begin
FirstLine = [U,lists:duplicate(Cols - Col - width(U)*2 + 1,"a")],
OtherLineA = [U,lists:duplicate(Cols - width(U) * 2+1,"a")],
OtherLineB = [U,lists:duplicate(Cols - width(U) * 2+1,"b")],
OtherLineC = [U,lists:duplicate(Cols - width(U) * 2+1,"c")],
OtherLineD = [U,lists:duplicate(Cols - width(U) * 2+1,"d")],
send_tty(Term,FirstLine),
check_content(Term, [FirstLine,$$]),
check_location(Term, {0, Cols - Col - width(U)+1}),
send_tty(Term,OtherLineA),
check_content(Term, [OtherLineA,$$]),
check_location(Term, {0, Cols - Col - width(U)+1}),
send_tty(Term,OtherLineB),
check_content(Term, [OtherLineB,$$]),
check_location(Term, {0, Cols - Col - width(U)+1}),
send_tty(Term,OtherLineC),
check_content(Term, [OtherLineC,$$]),
check_location(Term, {0, Cols - Col - width(U)+1}),
send_tty(Term,OtherLineD),
check_content(Term, [OtherLineD,$$]),
check_location(Term, {0, Cols - Col - width(U)+1}),
send_tty(Term,"C-A"),
check_location(Term, {-4, 0}), %% Broken
send_tty(Term,"Right"),
check_location(Term, {-4, width(U)}), %% Broken
send_tty(Term,"DC"), %% Broken
check_content(Term, ["a.*",U,"$"]),
check_content(Term, ["^b.*",U,"c$"]),
check_content(Term, ["^c.*",U,"dd$"]),
send_tty(Term,"a"),
check_content(Term, [FirstLine,$$]),
check_content(Term, [OtherLineA,$$]),
check_content(Term, [OtherLineB,$$]),
check_content(Term, [OtherLineC,$$]),
check_content(Term, [OtherLineD,$$]),
send_tty(Term,"Enter")
end || U <- hard_unicode()]
after
stop_tty(Term)
end.
shell_delete(Config) ->
Term = start_tty(Config),
try
[ begin
send_tty(Term,"a"),
check_content(Term, "> a$"),
check_location(Term, {0, 1}),
send_tty(Term,"BSpace"),
check_location(Term, {0, 0}),
check_content(Term, ">$"),
send_tty(Term,"a"),
send_tty(Term,U),
check_location(Term, {0, width([$a, U])}),
send_tty(Term,"a"),
send_tty(Term,U),
check_location(Term, {0, width([$a,U,$a,U])}),
check_content(Term, ["> a",U,$a,U,"$"]),
send_tty(Term,"Left"),
send_tty(Term,"Left"),
send_tty(Term,"BSpace"),
check_location(Term, {0, width([$a])}),
check_content(Term, ["> aa",U,"$"]),
send_tty(Term,U),
check_location(Term, {0, width([$a,U])}),
send_tty(Term,"Left"),
send_tty(Term,"DC"),
check_location(Term, {0, width([$a])}),
check_content(Term, ["> aa",U,"$"]),
send_tty(Term,"DC"),
send_tty(Term,"DC"),
check_content(Term, ["> a$"]),
send_tty(Term,"C-E"),
check_location(Term, {0, width([$a])}),
send_tty(Term,"BSpace"),
check_location(Term, {0, width([])})
end || U <- hard_unicode()]
after
stop_tty(Term)
end.
%% When deleting characters at the edge of the screen that are "large",
%% we need to take special care.
shell_delete_unicode_wrap(Config) ->
Term = start_tty(Config),
{_Rows, Cols} = get_window_size(Term),
{_Row, Col} = get_location(Term),
try
[begin
send_tty(Term,lists:duplicate(Cols - Col,"a")),
check_content(Term,"> a*$"),
send_tty(Term,[U,U,"aaaaa"]),
check_content(Term,["\n",U,U,"aaaaa$"]),
[send_tty(Term,"Left") || _ <- lists:seq(1,5+2)],
check_location(Term,{0,-Col}),
send_tty(Term,"BSpace"),
check_content(Term,"> a* \n"),
check_location(Term,{-1,Cols - Col - 1}),
send_tty(Term,"BSpace"),
check_content(Term,["> a*",U,"\n"]),
check_location(Term,{-1,Cols - Col - 2}),
send_tty(Term,"BSpace"),
check_content(Term,["> a*",U," \n"]),
check_location(Term,{-1,Cols - Col - 3}),
send_tty(Term,"BSpace"),
check_content(Term,["> a*",U,U,"\n"]),
check_content(Term,["\naaaaa$"]),
check_location(Term,{-1,Cols - Col - 4}),
send_tty(Term,"BSpace"),
check_content(Term,["> a*",U,U,"a\n"]),
check_content(Term,["\naaaa$"]),
check_location(Term,{-1,Cols - Col - 5}),
send_tty(Term,"Enter")
end || U <- hard_unicode()]
after
stop_tty(Term)
end.
%% When deleting characters and a "large" characters is changing line we need
%% to take extra care
shell_delete_unicode_not_at_cursor_wrap(Config) ->
Term = start_tty(Config),
{_Rows, Cols} = get_window_size(Term),
{_Row, Col} = get_location(Term),
try
[begin
send_tty(Term,lists:duplicate(Cols - Col,"a")),
check_content(Term,"> a*$"),
send_tty(Term,["a",U,"aaaaa"]),
check_content(Term,["\na",U,"aaaaa$"]),
send_tty(Term,"C-A"),
send_tty(Term,"DC"),
check_content(Term,["\n",U,"aaaaa$"]),
send_tty(Term,"DC"),
check_content(Term,["\n",U,"aaaaa$"]),
check_content(Term,["> a* \n"]),
send_tty(Term,"DC"),
check_content(Term,["\naaaaa$"]),
check_content(Term,["> a*",U,"\n"]),
send_tty(Term,"DC"),
check_content(Term,["\naaaa$"]),
check_content(Term,["> a*",U,"a\n"]),
send_tty(Term,"Enter")
end || U <- hard_unicode()]
after
stop_tty(Term)
end.
%% When deleting characters and a "large" characters is changing line we need
%% to take extra care
shell_update_window_unicode_wrap(Config) ->
Term = start_tty(Config),
{_Rows, Cols} = get_window_size(Term),
{_Row, Col} = get_location(Term),
try
[begin
send_tty(Term,lists:duplicate(Cols - Col - width(U) + 1,"a")),
check_content(Term,"> a*$"),
send_tty(Term,[U,"aaaaa"]),
check_content(Term,["> a* ?\n",U,"aaaaa$"]),
tmux(["resize-window -t ",tty_name(Term)," -x ",Cols+1]),
check_content(Term,["> a*",U,"\naaaaa$"]),
tmux(["resize-window -t ",tty_name(Term)," -x ",Cols]),
check_content(Term,["> a* ?\n",U,"aaaaa$"]),
send_tty(Term,"Enter")
end || U <- hard_unicode()]
after
stop_tty(Term)
end.
shell_transpose(Config) ->
Term = start_tty(Config),
Unicode = [[$a]] ++ hard_unicode(),
try
[
begin
send_tty(Term,"a"),
[send_tty(Term,[CP]) || CP <- U],
send_tty(Term,"b"),
[[send_tty(Term,[CP]) || CP <- U2] || U2 <- Unicode],
send_tty(Term,"cde"),
check_content(Term, ["a",U,"b",Unicode,"cde$"]),
check_location(Term, {0, width(["a",U,"b",Unicode,"cde"])}),
send_tty(Term,"Home"),
check_location(Term, {0, 0}),
send_tty(Term,"Right"),
send_tty(Term,"Right"),
check_location(Term, {0, 1+width([U])}),
send_tty(Term,"C-T"),
check_content(Term, ["ab",U,Unicode,"cde$"]),
send_tty(Term,"C-T"),
check_content(Term, ["ab",hd(Unicode),U,tl(Unicode),"cde$"]),
[send_tty(Term,"C-T") || _ <- lists:seq(1,length(Unicode)-1)],
check_content(Term, ["ab",Unicode,U,"cde$"]),
send_tty(Term,"C-T"),
check_content(Term, ["ab",Unicode,"c",U,"de$"]),
check_location(Term, {0, width(["ab",Unicode,"c",U])}),
send_tty(Term,"End"),
check_location(Term, {0, width(["ab",Unicode,"c",U,"de"])}),
send_tty(Term,"Left"),
send_tty(Term,"Left"),
send_tty(Term,"BSpace"),
check_content(Term, ["ab",Unicode,"cde$"]),
send_tty(Term,"End"),
send_tty(Term,"Enter")
end || U <- Unicode],
ok
after
stop_tty(Term),
ok
end.
shell_search(C) ->
Term = start_tty(C),
try
send_tty(Term,"a"),
send_tty(Term,"."),
send_tty(Term,"Enter"),
send_tty(Term,"'"),
send_tty(Term,"a"),
send_tty(Term,[16#1f600]),
send_tty(Term,"'"),
send_tty(Term,"."),
send_tty(Term,"Enter"),
check_location(Term, {0, 0}),
send_tty(Term,"C-r"),
check_content(Term, "search:\\s*\n\\s*'a😀'."),
send_tty(Term,"C-a"),
check_location(Term, {-1, width(C, "'a😀'.")}),
send_tty(Term,"Enter"),
send_tty(Term,"C-r"),
check_content(Term, "search:\\s*\n\\s*'a😀'."),
send_tty(Term,"a"),
check_content(Term, "search: a\\s*\n\\s*'a😀'."),
send_tty(Term,"C-r"),
check_content(Term, "search: a\\s*\n\\s*a."),
send_tty(Term,"BSpace"),
check_content(Term, "search:\\s*\n\\s*'a😀'."),
send_tty(Term,"BSpace"),
check_content(Term, "search:\\s*\n\\s*'a😀'."),
send_tty(Term,"M-c"),
check_location(Term, {-1, 0}),
ok
after
stop_tty(Term),
ok
end.
shell_insert(Config) ->
Term = start_tty(Config),
try
send_tty(Term,"abcdefghijklm"),
check_content(Term, "abcdefghijklm$"),
check_location(Term, {0, 13}),
send_tty(Term,"Home"),
send_tty(Term,"Right"),
send_tty(Term,"C-T"),
send_tty(Term,"C-T"),
send_tty(Term,"C-T"),
send_tty(Term,"C-T"),
check_content(Term, "bcdeafghijklm$"),
send_tty(Term,"End"),
send_tty(Term,"Left"),
send_tty(Term,"Left"),
send_tty(Term,"BSpace"),
check_content(Term, "bcdeafghijlm$"),
ok
after
stop_tty(Term)
end.
shell_update_window(Config) ->
Term = start_tty(Config),
Text = lists:flatten(["abcdefghijklmabcdefghijklm"]),
{_Row, Col} = get_location(Term),
try
send_tty(Term,Text),
check_content(Term,Text),
check_location(Term, {0, width(Text)}),
tmux(["resize-window -t ",tty_name(Term)," -x ",width(Text)+Col+1]),
send_tty(Term,"a"),
check_location(Term, {0, -Col}),
send_tty(Term,"BSpace"),
check_location(Term, {-1, width(Text)}),
tmux(["resize-window -t ",tty_name(Term)," -x ",width(Text)+Col]),
%% When resizing, tmux does not xnfix the cursor, so it will remain
%% at the previous locations
check_location(Term, {-1, width(Text)}),
send_tty(Term,"a"),
check_location(Term, {0, -Col + 1}),
%% When we do backspace here, tmux seems to place the cursor in an
%% incorrect position except when a terminal is attached.
send_tty(Term,"BSpace"),
%% This really should be {0, -Col}, but sometimes tmux sets it to
%% {-1, width(Text)} instead.
check_location(Term, [{0, -Col}, {-1, width(Text)}]),
tmux(["resize-window -t ",tty_name(Term)," -x ",width(Text) div 2 + Col]),
%% Depending on what happened with the cursor above, the line will be
%% different here.
check_location(Term, [{0, -Col + width(Text) div 2},
{-1, -Col + width(Text) div 2}]),
ok
after
stop_tty(Term)
end.
shell_small_window_multiline_navigation(Config) ->
Term0 = start_tty(Config),
tmux(["resize-window -t ",tty_name(Term0)," -x ",30, " -y ", 6]),
{Row, Col} = get_location(Term0),
Term = Term0#tmux{orig_location = {Row, Col}},
Text = ("xbcdefghijklmabcdefghijklm\n"++
"abcdefghijkl\n"++
"abcdefghijklmabcdefghijklm\n"++
"abcdefghijklmabcdefghijklx"),
try
send_tty(Term,Text),
check_location(Term, {0, -4}),
send_tty(Term,"Home"),
check_location(Term, {-1, 0}),
send_tty(Term, "C-Up"),
check_location(Term, {-2, 0}),
send_tty(Term, "C-Down"),
check_location(Term, {-1, 0}),
send_tty(Term, "Left"),
check_location(Term, {-1, -4}),
send_tty(Term, "Right"),
check_location(Term, {-1, 0}),
send_tty(Term, "\e[1;4A"),
check_location(Term, {-5, 0}),
check_content(Term,"xbc"),
send_tty(Term, "\e[1;4B"),
check_location(Term, {0, -4}),
check_content(Term,"klx"),
send_tty(Term, " sets:is_e\t"),
check_content(Term,"is_element"),
check_content(Term,"is_empty"),
check_location(Term, {-3, 6}),
send_tty(Term, "C-Up"),
send_tty(Term,"Home"),
check_location(Term, {-2, 0}),
send_tty(Term, "sets:is_e\t"),
check_content(Term,"is_element"),
check_content(Term,"is_empty"),
check_location(Term, {-4, 9}),
send_tty(Term, "M-Enter"),
check_location(Term, {-1, 0}),
ok
after
stop_tty(Term)
end.
shell_huge_input(Config) ->
Term = start_tty(Config),
ManyUnicode = lists:duplicate(100,hard_unicode()),
try
send_tty(Term,ManyUnicode),
check_content(Term, hard_unicode_match(Config) ++ "$",
#{ replace => {"\n",""} }),
send_tty(Term,"Enter"),
ok
after
stop_tty(Term)
end.
shell_receive_standard_out(Config) ->
Term = start_tty(Config),
try
send_tty(Term,"my_fun(5) -> ok; my_fun(N) -> receive after 100 -> io:format(\"~p\\n\", [N]), my_fun(N+1) end.\n"),
send_tty(Term, "spawn(shell_default, my_fun, [0]). ABC\n"),
timer:sleep(1000),
check_location(Term, {0, 0}), %% Check that we are at the same location relative to the start.
check_content(Term, "3\\s+4\\s+.+>\\sABC"),
ok
after
stop_tty(Term)
end.
%% Test that the shell works when invalid utf-8 (aka latin1) is sent to it
shell_invalid_unicode(Config) ->
Term = start_tty(Config),
InvalidUnicode = <<$å,$ä,$ö>>, %% åäö in latin1
try
send_tty(Term,hard_unicode()),
check_content(Term, hard_unicode() ++ "$"),
send_tty(Term,"Enter"),
check_content(Term, "illegal character"),
%% Send invalid utf-8
send_stdin(Term,InvalidUnicode),
%% Check that the utf-8 was echoed
check_content(Term, "\\\\345\\\\344\\\\366$"),
send_tty(Term,"Enter"),
%% Check that the terminal entered "latin1" mode
send_tty(Term,"😀한."),
check_content(Term, "\\Q\\360\\237\\230\\200\\355\\225\\234.\\E$"),
send_tty(Term,"Enter"),
%% Check that we can reset the encoding to unicode
send_tty(Term,"io:setopts([{encoding,unicode}])."),
send_tty(Term,"Enter"),
check_content(Term, "\nok\n"),
send_tty(Term,"😀한"),
check_content(Term, "😀한$"),
ok
after
stop_tty(Term),
ok
end.
%% Test the we can handle ansi insert, navigation and delete
%% We currently can not so skip this test
shell_support_ansi_input(Config) ->
Term = start_tty(Config),
BoldText = "\e[;1m",
ClearText = "\e[0m",
try
send_stdin(Term,["{",BoldText,"a😀b",ClearText,"}"]),
timer:sleep(1000),
try check_location(Term, {0, width("{1ma😀bm}")}) of
_ ->
throw({skip, "Do not support ansi input"})
catch _:_ ->
ok
end,
check_location(Term, {0, width("{a😀b}")}),
check_content(fun() -> get_content(Term,"-e") end,
["{", BoldText, "a😀b", ClearText, "}"]),
send_tty(Term,"Left"),
send_tty(Term,"Left"),
check_location(Term, {0, width("{a😀")}),
send_tty(Term,"C-Left"),
check_location(Term, {0, width("{")}),
send_tty(Term,"End"),
send_tty(Term,"BSpace"),
send_tty(Term,"BSpace"),
check_content(Term, ["{", BoldText, "a😀"]),
ok
after
stop_tty(Term),
ok
end.
shell_expand_location_below(Config) ->
Term = start_tty(Config),
{Rows, _} = get_window_size(Term),
{Row, Col} = get_location(Term),
NumFunctions = lists:seq(0, Row*2),
FunctionName = "a_long_function_name",
Module = lists:flatten(
["-module(long_module).\n",
"-export([",lists:join($,,[io_lib:format("~s~p/0",[FunctionName,I]) || I <- NumFunctions]),"]).\n\n",
[io_lib:format("~s~p() -> ok.\n",[FunctionName,I]) || I <- NumFunctions]]),
DoScan = fun F([]) ->
[];
F(Str) ->
{done,{ok,T,_},C} = erl_scan:tokens([],Str,0),
[ T | F(C) ]
end,
Forms = [ begin {ok,Y} = erl_parse:parse_form(X),Y end || X <- DoScan(Module) ],
{ok,_,Bin} = compile:forms(Forms, [debug_info]),
try
tmux(["resize-window -t ",tty_name(Term)," -x 80"]),
Cols = 80,
%% First check that basic completion works
send_stdin(Term, "escript:"),
send_stdin(Term, "\t"),
%% Cursor at correct place
check_location(Term, {-3, width("escript:")}),
%% Nothing after the start( completion
check_content(Term, "start\\($"),
%% Check that completion is cleared when we type
send_stdin(Term, "s"),
check_location(Term, {-3, width("escript:s")}),
check_content(Term, "escript:s$"),
%% Check that completion works when in the middle of a term
send_tty(Term, "Home"),
send_tty(Term, "["),
send_tty(Term, "End"),
send_tty(Term, ", test_after]"),
[send_tty(Term, "Left") || _ <- ", test_after]"],
send_stdin(Term, "\t"),
check_location(Term, {-3, width("[escript:s")}),
check_content(Term, "script_name\\([ ]+start\\($"),
send_tty(Term, "C-K"),
%% Check that completion works when in the middle of a long term
send_tty(Term, ", "++ lists:duplicate(80*2, $a)++"]"),
[send_tty(Term, "Left") || _ <- ", "++ lists:duplicate(80*2, $a)++"]"],
send_stdin(Term, "\t"),
check_location(Term, {-4, width("[escript:s")}),
check_content(Term, "script_name\\([ ]+start\\($"),
send_tty(Term, "End"),
send_stdin(Term, ".\n"),
%% Check that we behave as we should with very long completions
rpc(Term, fun() ->
{module, long_module} = code:load_binary(long_module, "long_module.beam", Bin)
end),
check_content(Term, "3>"),
tmux(["resize-window -t ",tty_name(Term)," -y 50"]),
timer:sleep(1000), %% Sleep to make sure window has resized
Result = 61,
Rows1 = 48,
send_stdin(Term, "long_module:" ++ FunctionName),
send_stdin(Term, "\t"),
check_content(Term, "3> long_module:" ++ FunctionName ++ "\nfunctions(\n|.)*a_long_function_name0\\("),
%% Check that correct text is printed below expansion
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[1, 7, Result])),
send_stdin(Term, "\t"),
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[1, Rows1, Result])),
send_tty(Term, "Down"),
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[2, Rows1+1, Result])),
send_tty(Term, "PgDn"),
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[7, Rows1+6, Result])),
send_tty(Term, "PgUp"),
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[2, Rows1+1, Result])),
send_tty(Term, "PgUp"),
%% Overshoot up
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[1, Rows1, Result])),
%% Overshoot down
send_tty(Term, "PgDn"),
send_tty(Term, "PgDn"),
send_tty(Term, "PgDn"),
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[14, Rows1+13, Result])),
check_content(Term, "\n.*a_long_function_name99\\("),
send_tty(Term, "Up"),
check_content(Term, io_lib:format("rows ~w to ~w of ~w",
[13, Rows1+12, Result])),
send_tty(Term, "\t"),
%% We resize the terminal to make everything fit and test that
%% expand below displays everything
tmux(["resize-window -t ", tty_name(Term), " -y ", integer_to_list(Row+10)]),
timer:sleep(1000), %% Sleep to make sure window has resized
send_tty(Term, "\t\t"),
check_content(Term, "3> long_module:" ++ FunctionName ++ "\nfunctions(\n|.)*a_long_function_name99\\($"),
%% Check that doing an expansion when cursor is in xnfix position works
send_tty(Term, "BSpace"),
check_content(Term, "3> long_module:a_long_function_nam$"),
send_tty(Term, "Home"),
send_tty(Term, lists:duplicate(Cols - Col - width(", long_module:a_long_function_name"), "a")),
send_tty(Term, ", "),
send_tty(Term, "End"),
send_tty(Term, "\t"),
check_location(Term, {-Rows + 2, -Col}),
send_tty(Term, "\t"),
check_content(Term, "3> a+, long_module:" ++ FunctionName ++ "\n\nfunctions(\n|.)*a_long_function_name0\\("),
check_location(Term, {-Rows + 2, -Col}),
send_tty(Term, "Down"),
check_location(Term, {-Rows + 2, -Col}),
send_tty(Term, "Down"),
check_location(Term, {-Rows + 2, -Col}),
send_tty(Term, "Home"),
send_tty(Term, lists:duplicate(Cols, "b")),
send_tty(Term, "End"),
send_tty(Term, "\t"),
check_content(Term, "3> b+\nb+a+, long_module:" ++ FunctionName ++ "\n\nfunctions(\n|.)*a_long_function_name0\\("),
check_location(Term, {-Rows + 3, -Col}),
send_tty(Term, "Down"),
check_location(Term, {-Rows + 3, -Col}),
send_tty(Term, "Down"),
check_location(Term, {-Rows + 3, -Col}),
ok
after
stop_tty(Term),
ok
end.
shell_expand_location_above(Config) ->
Term = start_tty([{args,["-stdlib","shell_expand_location","above"]}|Config]),
try
tmux(["resize-window -t ",tty_name(Term)," -x 80"]),
send_stdin(Term, "escript:"),
send_stdin(Term, "\t"),
check_location(Term, {0, width("escript:")}),
check_content(Term, "start\\(\n"),
check_content(Term, "escript:$"),
send_stdin(Term, "s"),
send_stdin(Term, "\t"),
check_location(Term, {0, width("escript:s")}),
check_content(Term, "\nscript_name\\([ ]+start\\(\n"),
check_content(Term, "escript:s$"),
ok
after
stop_tty(Term),
ok
end.
shell_help(Config) ->
Term = start_tty(Config),
try
send_tty(Term, "application:put_env(kernel, shell_docs_ansi, false).\n"),
send_tty(Term, "lists"),
send_tty(Term, "\^[h"),
%% Check we can see the first line
check_content(Term, "List processing functions."),
check_not_in_content(Term, "less than or equal to"),
%% Expand the help area to take up the whole buffer.
send_tty(Term, "\^[h"),
%% Check that we can see the last line (lists help should fit in the window)
check_content(Term, "less than or equal to"),
send_tty(Term, ":all"),
send_tty(Term, "\^[h"),
check_content(Term, ~S"all\(Pred, List\)"),
ok
after
stop_tty(Term),
ok
end.
%% Test the we can handle invalid ansi escape chars.
%% tmux cannot handle this... so we test this using to_erl
shell_invalid_ansi(_Config) ->
InvalidAnsiPrompt =
case proplists:get_value(encoding, io:getopts(user)) of
unicode ->
["\e]94m",54620,44397,50612,47,51312,49440,47568,"\e]0m"];
latin1 ->
["\e]94minvalid_test\e]0m"]
end,
rtnode:run(
[{eval, fun() -> application:set_env(
stdlib, shell_prompt_func_test,
fun() -> InvalidAnsiPrompt end)
end },
{putline,"a."},
{expect, "a[.]"},
{expect, ["\\Q",InvalidAnsiPrompt,"\\E"]}],
"", "",
["-pz",filename:dirname(code:which(?MODULE)),
"-connect_all","false",
"-kernel","logger_level","all",
"-kernel","shell_history","disabled",
"-kernel","prevent_overlapping_partitions","false",
"-eval","shell:prompt_func({interactive_shell_SUITE,prompt})."
]).
shell_get_password(_Config) ->
rtnode:run(
[{putline,"io:get_password()."},
{putline,"secret\r"},
{expect, "\r\n\r\n\"secret\""}]),
%% io:get_password only works when run in "newshell"
rtnode:run(
[{putline,"io:get_password()."},
{expect, "\\Q{error,enotsup}\\E"}],
"","",["-oldshell"]).
shell_ignore_pager_commands(Config) ->
Term = start_tty(Config),
case code:get_doc(file, #{sources=>[eep48]}) of
{error, _} -> {skip, "No documentation available"};
_ ->
try
send_tty(Term, "h(file).\n"),
check_content(Term,"\\Qmore (y/n)? (y)\\E"),
send_tty(Term, "n\n"),
check_content(Term,"ok"),
send_tty(Term, "C-P"),
check_content(Term,"\\Qh(file).\\E"),
ok
after
stop_tty(Term),
ok
end
end.
test_valid_keymap(Config) when is_list(Config) ->
DataDir = proplists:get_value(data_dir,Config),
Term = setup_tty([{args, ["-config", DataDir ++ "valid_keymap.config"]} | Config]),
Prompt = fun() -> ["\e[94m",54620,44397,50612,47,51312,49440,47568,"\e[0m"] end,
erpc:call(Term#tmux.node, application, set_env,
[stdlib, shell_prompt_func_test,
proplists:get_value(shell_prompt_func_test, Config, Prompt)]),
try
check_not_in_content(Term, "Invalid key"),
check_not_in_content(Term, "Invalid function"),
send_tty(Term, "asdf"),
send_tty(Term, "C-u"),
check_content(Term, ">$"),
send_tty(Term, "1.\n"),
send_tty(Term, "C-b"),
check_content(Term, "2>\\s1.$"),
ok
after
stop_tty(Term),
ok
end.
test_invalid_keymap(Config) when is_list(Config) ->
DataDir = proplists:get_value(data_dir,Config),
Term1 = setup_tty([{args, ["-config", DataDir ++ "invalid_keymap.config"]} | Config]),
try
check_content(Term1, "Invalid key"),
check_content(Term1, "Invalid function"),
send_tty(Term1, "asdf"),
send_tty(Term1, "C-u"),
check_content(Term1, ">$"),
ok
after
stop_tty(Term1),
ok
end.
external_editor(Config) ->
case os:find_executable("nano") of
false -> {skip, "nano is not installed"};
_ ->
Term = start_tty(Config),
try
tmux(["resize-window -t ",tty_name(Term)," -x 80"]),
send_tty(Term,"os:putenv(\"EDITOR\",\"nano\").\n"),
send_tty(Term, "\"some text with\nnewline in it\""),
check_content(Term,"3> \"some text with\\s*\n.+\\s*newline in it\""),
send_tty(Term, "C-O"),
check_content(Term,"GNU nano [\\d.]+"),
check_content(Term,"\"some text with\\s*\n\\s*newline in it\""),
send_tty(Term, "Right"),
send_tty(Term, "still"),
send_tty(Term, "Enter"),
send_tty(Term, "C-O"), %% save in nano
send_tty(Term, "Enter"),
send_tty(Term, "C-X"), %% quit in nano
check_content(Term,"3> \"still\\s*\n\\s*.+\\s*some text with\\s*\n.+\\s*newline in it\""),
send_tty(Term,".\n"),
check_content(Term,"\\Q\"still\\nsome text with\\nnewline in it\"\\E"),
ok
after
stop_tty(Term),
ok
end
end.
external_editor_visual(Config) ->
case os:find_executable("vim") of
false ->
{skip, "vim is not installed"};
_ ->
Term = start_tty(Config),
try
tmux(["resize-window -t ",tty_name(Term)," -x 80"]),
send_tty(Term,"os:putenv(\"EDITOR\",\"nano\").\n"),
check_content(Term, "3>"),
send_tty(Term,"os:putenv(\"VISUAL\",\"vim -u DEFAULTS -U NONE -i NONE\").\n"),
check_content(Term, "4>"),
send_tty(Term,"\"hello"),
send_tty(Term, "C-O"), %% Open vim
check_content(Term, "^\"hello"),
send_tty(Term, "$"), %% Set cursor at end
send_tty(Term, "a"), %% Enter insert mode at end
check_content(Term, "-- INSERT --"),
send_tty(Term, "\"."),
send_tty(Term,"Escape"),
send_tty(Term,":wq"),
send_tty(Term,"Enter"),
check_content(Term, "\"hello\"[.]$"),
send_tty(Term,"Enter"),
check_content(Term, "\"hello\""),
check_content(Term, "5>$")
after
stop_tty(Term),
ok
end
end.
external_editor_unicode(Config) ->
NanoPath = os:find_executable("nano"),
case NanoPath of
false -> {skip, "nano is not installed"};
_ ->
Term = start_tty(Config),
try
tmux(["resize-window -t ",tty_name(Term)," -x 80"]),
send_tty(Term,"os:putenv(\"EDITOR\",\"nano\").\n"),
send_tty(Term, hard_unicode()),
check_content(Term,"3> " ++ hard_unicode_match(Config)),
send_tty(Term, "C-O"), %% open external editor (nano)
check_content(Term,"GNU nano [\\d.]+"),
send_tty(Term, "still "),
check_content(Term,"\nstill "),
send_tty(Term, "C-O"), %% save in nano
send_tty(Term, "Enter"),
send_tty(Term, "C-X"), %% quit in nano
check_content(Term,"still "++hard_unicode_match(Config)),
ok
after
stop_tty(Term),
ok
end
end.
%% There used to be a race condition when using shell:start_interactive where
%% the newline handling of standard_error did not change correctly to compensate
%% for the tty changing to canonical mode
shell_standard_error_nlcr(Config) ->
[
begin
Term = setup_tty([{env,[{"TERM",TERM}]},{args, ["-noshell"]} | Config]),
try
rpc(Term, io, format, [standard_error,"test~ntest~ntest", []]),
check_content(Term, "test\ntest\ntest$"),
rpc(Term, fun() -> shell:start_interactive(),
io:format(standard_error,"test~ntest~ntest", [])
end),
check_content(Term, "test\ntest\ntest(\n|.)*test\ntest\ntest")
after
stop_tty(Term)
end
end || TERM <- ["dumb",os:getenv("TERM")]].
%% We test that suspending of `erl` and then resuming restores the shell
shell_suspend(Config) ->
Name = peer:random_name(proplists:get_value(tc_path,Config)),
%% In order to suspend `erl` we need it to run in a shell that has job control
%% so we start the peer within a tmux window instead of having it be the original
%% process.
os:cmd("tmux new-window -n " ++ Name ++ " -d -- bash --norc"),
Peer = #{ name => Name,
post_process_args =>
fun(["new-window","-n",_,"-d","--"|CmdAndArgs]) ->
FlatCmdAndArgs =
lists:join(
" ",[[$',A,$'] || A <- CmdAndArgs]),
["send","-t",Name,lists:flatten(FlatCmdAndArgs),"Enter"]
end
},
Term = start_tty([{peer, Peer}|Config]),
try
send_tty(Term, hard_unicode()),
check_content(Term,["2> ",hard_unicode(),"$"]),
send_tty(Term, "C-Z"),
check_content(Term,"\\Q[1]+\\E\\s*Stopped"),
send_tty(Term, "fg"),
send_tty(Term, "Enter"),
send_tty(Term, "M-l"),
check_content(Term,["2> ",hard_unicode(),"$"]),
check_location(Term,{0,width(hard_unicode())}),
ok
after
stop_tty(Term),
ok
end.
%% We test that suspending of `erl` and then resuming restores the shell
shell_full_queue(Config) ->
[throw({skip,"Need unbuffered to run"}) || os:find_executable("unbuffered") =:= false],
%% In order to fill the read buffer of the terminal we need to get a
%% bit creative. We first need to start erl in bash in order to be
%% able to get access to job control for suspended processes.
%% We then also wrap `erl` in `unbuffer -p` so that we can suspend
%% that program in order to block writing to stdout for a while.
Name = peer:random_name(proplists:get_value(tc_path,Config)),
os:cmd("tmux new-window -n " ++ Name ++ " -d -- bash --norc"),
Peer = #{ name => Name,
post_process_args =>
fun(["new-window","-n",_,"-d","--"|CmdAndArgs]) ->
FlatCmdAndArgs = ["unbuffer -p "] ++
lists:join(
" ",[[$',A,$'] || A <- CmdAndArgs]),
["send","-t",Name,lists:flatten(FlatCmdAndArgs),"Enter"]
end
},
Term = start_tty([{peer, Peer}|Config]),
UnbufferedPid = os:cmd("ps -o ppid= -p " ++ rpc(Term,os,getpid,[])),
WriteUntilStopped =
fun F(Char) ->
rpc(Term,io,format,[user,[Char],[]]),
put(bytes,get(bytes,0)+1),
receive
stop ->
rpc(Term,io,format,[user,[Char+1],[]])
after 0 -> F(Char)
end
end,
WaitUntilBlocked =
fun(Pid, Ref) ->
(fun F(Cnt) ->
receive
{'DOWN',Ref,_,_,_} = Down ->
ct:fail({io_format_did_not_block, Down})
after 1000 ->
ok
end,
case process_info(Pid,dictionary) of
{dictionary,[{bytes,Cnt}]} ->
ct:log("Bytes until blocked: ~p~n",[Cnt]),
%% Add one extra byte as for
%% the current blocking call
Cnt + 1;
{dictionary,[{bytes,NewCnt}]} ->
F(NewCnt)
end
end)(0)
end,
try
%% First test that we can suspend and then resume
os:cmd("kill -TSTP " ++ UnbufferedPid),
check_content(Term,"\\Q[1]+\\E\\s*Stopped"),
{Pid, Ref} = spawn_monitor(fun() -> WriteUntilStopped($a) end),
WaitUntilBlocked(Pid, Ref),
send_tty(Term, "fg"),
send_tty(Term, "Enter"),
Pid ! stop,
check_content(Term,"b$"),
send_tty(Term, "."),
send_tty(Term, "Enter"),
%% Then we test that all characters are written when system
%% is terminated just after writing
{ok,Cols} = rpc(Term,io,columns,[user]),
send_tty(Term, "Enter"),
os:cmd("kill -TSTP " ++ UnbufferedPid),
check_content(Term,"\\Q[1]+\\E\\s*Stopped"),
{Pid2, Ref2} = spawn_monitor(fun() -> WriteUntilStopped($c) end),
Bytes = WaitUntilBlocked(Pid2, Ref2) - 1,
stop_tty(Term),
send_tty(Term, "fg"),
send_tty(Term, "Enter"),
check_content(
fun() ->
tmux(["capture-pane -p -S - -E - -t ",tty_name(Term)])
end, lists:flatten([lists:duplicate(Cols,$c) ++ "\n" ||
_ <- lists:seq(1,(Bytes) div Cols)]
++ [lists:duplicate((Bytes) rem Cols,$c)])),
ct:log("~ts",[tmux(["capture-pane -p -S - -E - -t ",tty_name(Term)])]),
ok
after
stop_tty(Term),
ok
end.
get(Key,Default) ->
case get(Key) of
undefined ->
Default;
Value ->
Value
end.
%% A list of unicode graphemes that are notoriously hard to render
hard_unicode() ->
ZWJ =
case os:type() of
%% macOS has very good rendering of ZWJ,
%% but the cursor does not agree with it..
{unix, darwin} -> [];
_ -> [[16#1F91A,16#1F3FC]] % Hand with skintone 🤚🏼
end,
[[16#1f600], % Smilie 😀
"한", % Hangul
"Z̤͔ͧ̑̓","ä͖̭̈̇","lͮ̒ͫ","ǧ̗͚̚","o̙̔ͮ̇͐̇" %% Vertically stacked chars
%%"👩👩", % Zero width joiner
%%"👩👩👧👦" % Zero width joiner
| ZWJ].
hard_unicode_match(Config) ->
["\\Q",[unicode_to_octet(Config, U) || U <- hard_unicode()],"\\E"].
unicode_to_octet(Config, U) ->
case ?config(encoding,Config) of
unicode -> U;
latin1 -> unicode_to_octet(U)
end.
unicode_to_octet(U) ->
[if Byte >= 128 -> [$\\,integer_to_list(Byte,8)];
true -> Byte
end || <<Byte>> <= unicode:characters_to_binary(U)].
unicode_to_hex(Config, U) ->
case ?config(encoding,Config) of
unicode -> U;
latin1 -> unicode_to_hex(U)
end.
unicode_to_hex(U) when is_integer(U) ->
unicode_to_hex([U]);
unicode_to_hex(Us) ->
[if U < 128 -> U;
U < 512 -> ["\\",integer_to_list(U,8)];
true -> ["\\x{",integer_to_list(U,16),"}"]
end || U <- Us].
width(C, Str) ->
case ?config(encoding, C) of
unicode -> width(Str);
latin1 -> width(unicode_to_octet(Str))
end.
width(Str) ->
lists:sum(
[npwcwidth(CP) || CP <- lists:flatten(Str)]).
npwcwidth(CP) ->
try prim_tty:npwcwidth(CP)
catch error:undef ->
if CP =:= 16#D55C ->
2; %% 한
CP =:= 16#1f91A ->
2; %% hand
CP =:= 16#1F3Fc ->
2; %% Skintone
CP =:= 16#1f600 ->
2; %% smilie
true ->
case lists:member(CP, [775,776,780,785,786,787,788,791,793,794,
804,813,848,852,854,858,871,875,878]) of
true ->
0;
false ->
1
end
end
end.
tmux([Cmd|_] = Command) when is_list(Cmd) ->
tmux(lists:concat(Command));
tmux(Command) ->
string:trim(os:cmd(["tmux ",Command])).
rpc(#tmux{ node = N }, Fun) ->
erpc:call(N, Fun).
rpc(#tmux{ node = N }, M, F, A) ->
erpc:call(N, M, F, A).
%% Setup a TTY, but do not type anything in terminal
setup_tty(Config) ->
Name = maps:get(name,proplists:get_value(peer, Config, #{}),
peer:random_name(proplists:get_value(tc_path, Config))),
Envs = lists:flatmap(fun({Key,Value}) ->
["-env",Key,Value]
end, proplists:get_value(env,Config,[])),
ExtraArgs = proplists:get_value(args,Config,[]),
ExecArgs = case os:getenv("TMUX_DEBUG") of
"strace" ->
STraceLog = filename:join(proplists:get_value(priv_dir,Config),
Name++".strace"),
ct:log("Link to strace: file://~ts", [STraceLog]),
[os:find_executable("strace"),"-f",
"-o",STraceLog,
"-e","trace=all",
"-e","read=0,1,2",
"-e","write=0,1,2"
] ++ string:split(ct:get_progname()," ",all);
"rr" ->
[os:find_executable("cerl"),"-rr"];
_ ->
string:split(ct:get_progname()," ",all)
end,
DefaultPeerArgs = #{ name => Name,
exec =>
{os:find_executable("tmux"),
["new-window","-n",Name,"-d","--"] ++ ExecArgs },
args => ["-pz",filename:dirname(code:which(?MODULE)),
"-connect_all","false",
% "-kernel","logger_level","all",
"-kernel","shell_history","disabled",
"-kernel","prevent_overlapping_partitions","false",
"-eval","shell:prompt_func({interactive_shell_SUITE,prompt})."
] ++ Envs ++ ExtraArgs,
wait_boot => 60_000,
detached => false
},
{ok, Peer, Node} =
?CT_PEER(maps:merge(proplists:get_value(peer,Config,#{}),
DefaultPeerArgs)),
Self = self(),
%% By default peer links with the starter. For these TCs we however only
%% want the peer to die if we die, so we create a "unidirection link" using
%% monitors.
spawn(fun() ->
TCRef = erlang:monitor(process, Self),
PeerRef = erlang:monitor(process, Peer),
receive
{'DOWN',TCRef,_,_,Reason} ->
exit(Peer, Reason);
{'DOWN',PeerRef,_,_,_} ->
ok
end
end),
unlink(Peer),
"" = tmux(["set-option -t ",Name," remain-on-exit on"]),
%% We start tracing on the remote node in order to help debugging
TraceLog = filename:join(proplists:get_value(priv_dir,Config),Name++".trace"),
ct:log("Link to trace: file://~ts",[TraceLog]),
spawn(Node,
fun() ->
{ok, _} = dbg:tracer(file,TraceLog),
%% dbg:p(whereis(user_drv),[c,m]),
%% dbg:p(whereis(user_drv_writer),[c,m]),
%% dbg:p(whereis(user_drv_reader),[c,m]),
%% dbg:tp(user_drv,x),
%% dbg:tp(prim_tty,x),
%% dbg:tpl(prim_tty,write_nif,x),
%% dbg:tpl(prim_tty,read_nif,x),
monitor(process, Self),
receive _ -> ok end
end),
#tmux{ peer = Peer, node = Node, name = Name }.
%% Start a tty, setup custom prompt and set cursor at bottom
start_tty(Config) ->
Term = setup_tty(Config),
Prompt = fun() -> ["\e[94m",54620,44397,50612,47,51312,49440,47568,"\e[0m"] end,
erpc:call(Term#tmux.node, application, set_env,
[stdlib, shell_prompt_func_test,
proplists:get_value(shell_prompt_func_test, Config, Prompt)]),
{Rows, _} = get_window_size(Term),
%% We send a lot of newlines here in order for the number of rows
%% in the window to be max so that we can predict what the cursor
%% position is.
[send_tty(Term,"\n") || _ <- lists:seq(1, Rows)],
%% We enter an 'a' here so that we can get the correct orig position
%% with an alternative prompt.
send_tty(Term,"a.\n"),
check_content(Term,"2>$"),
OrigLocation = get_location(Term),
Term#tmux{ orig_location = OrigLocation }.
prompt(L) ->
N = proplists:get_value(history, L, 0),
Fun = application:get_env(stdlib, shell_prompt_func_test,
fun() -> atom_to_list(node()) end),
io_lib:format("(~ts)~w> ",[Fun(),N]).
stop_tty(Term) ->
catch peer:stop(Term#tmux.peer),
ct:log("~ts",[get_content(Term, "-e")]),
% "" = tmux("kill-window -t " ++ Term#tmux.name),
ok.
tty_name(Term) ->
Term#tmux.name.
send_tty(Term, "Home") ->
%% https://stackoverflow.com/a/55616731
send_tty(Term,"Escape"),
send_tty(Term,"OH");
send_tty(Term, "End") ->
send_tty(Term,"Escape"),
send_tty(Term,"OF");
send_tty(#tmux{ name = Name } = _Term,Value) ->
[Head | Quotes] = string:split(Value, "'", all),
"" = tmux("send -t " ++ Name ++ " '" ++ Head ++ "'"),
[begin
"" = tmux("send -t " ++ Name ++ " \"'\""),
"" = tmux("send -t " ++ Name ++ " '" ++ V ++ "'")
end || V <- Quotes].
%% We use send_stdin for testing of things that we cannot sent via
%% the tmux send command, such as invalid unicode
send_stdin(Term, Chars) when is_binary(Chars) ->
rpc(Term,erlang,display_string,[stdin,Chars]);
send_stdin(Term, Chars) ->
send_stdin(Term, iolist_to_binary(unicode:characters_to_binary(Chars))).
check_location(Term, Where) ->
check_location(Term, Where, 5).
check_location(Term, Where, Attempt) when is_tuple(Where) ->
check_location(Term, [Where], Attempt);
check_location(#tmux{ orig_location = {OrigRow, OrigCol} = Orig } = Term,
Where, Attempt) ->
NewLocation = get_location(Term),
case lists:any(fun({AdjRow, AdjCol}) ->
{OrigRow+AdjRow,OrigCol+AdjCol} =:= NewLocation
end, Where) of
true -> NewLocation;
false when Attempt =:= 0 ->
{NewRow, NewCol} = NewLocation,
ct:fail({wrong_location, {expected,Where},
{got,{NewRow - OrigRow, NewCol - OrigCol},
{NewLocation, Orig}}});
false ->
timer:sleep(50),
check_location(Term, Where, Attempt -1)
end.
get_location(Term) ->
RowAndCol = tmux("display -pF '#{cursor_y} #{cursor_x}' -t "++Term#tmux.name),
[Row, Col] = string:lexemes(string:trim(RowAndCol,both)," "),
{list_to_integer(Row), list_to_integer(Col)}.
get_window_size(Term) ->
RowAndCol = tmux("display -pF '#{window_height} #{window_width}' -t "++Term#tmux.name),
[Row, Col] = string:lexemes(string:trim(RowAndCol,both)," "),
{list_to_integer(Row), list_to_integer(Col)}.
check_not_in_content(Term, NegativeMatch) ->
check_not_in_content(Term, NegativeMatch, #{}, 5).
check_not_in_content(Term, NegativeMatch, Opts, Attempt) ->
Opts = #{},
OrigContent = case Term of
#tmux{} -> get_content(Term);
Fun when is_function(Fun,0) -> Fun()
end,
Content = case maps:find(replace, Opts) of
{ok, {RE,Repl} } ->
re:replace(OrigContent, RE, Repl, [global]);
error ->
OrigContent
end,
case re:run(string:trim(Content, both), lists:flatten(NegativeMatch), [unicode]) of
{match,_} ->
io:format("Failed, found '~ts' in ~n'~ts'~n",
[unicode:characters_to_binary(NegativeMatch), Content]),
io:format("Failed, found '~w' in ~n'~w'~n",
[unicode:characters_to_binary(NegativeMatch), Content]),
ct:fail(match);
_ when Attempt =:= 0 ->
ok;
_ ->
timer:sleep(500),
check_not_in_content(Term, NegativeMatch, Opts, Attempt - 1)
end.
check_content(Term, Match) ->
check_content(Term, Match, #{}).
check_content(Term, Match, Opts) when is_map(Opts) ->
check_content(Term, Match, Opts, 5).
check_content(Term, Match, Opts, Attempt) ->
OrigContent = case Term of
#tmux{} -> get_content(Term);
Fun when is_function(Fun,0) -> Fun()
end,
Content = case maps:find(replace, Opts) of
{ok, {RE,Repl} } ->
re:replace(OrigContent, RE, Repl, [global]);
error ->
OrigContent
end,
case re:run(string:trim(Content, both), lists:flatten(Match), [unicode]) of
{match,_} ->
ok;
_ when Attempt =:= 0 ->
io:format("Failed to find '~ts' in ~n'~ts'~n",
[unicode:characters_to_binary(Match), Content]),
io:format("Failed to find '~w' in ~n'~w'~n",
[unicode:characters_to_binary(Match), Content]),
ct:fail(nomatch);
_ ->
timer:sleep(500),
check_content(Term, Match, Opts, Attempt - 1)
end.
get_content(Term) ->
get_content(Term, "").
get_content(#tmux{ name = Name }, Args) ->
Content = unicode:characters_to_binary(tmux("capture-pane -p " ++ Args ++ " -t " ++ Name)),
case string:split(Content,"a.\na") of
[_Ignore,C] ->
C;
[C] ->
C
end.
%% Tests that exit of initial shell restarts shell.
exit_initial(Config) when is_list(Config) ->
case proplists:get_value(default_shell, Config) of
old ->
test_exit_initial(old);
new ->
test_exit_initial(old),
test_exit_initial(new)
end,
ok.
test_exit_initial(old) ->
rtnode:run(
[{putline, ""},
{putline, "2."},
{expect, "2\r\n"},
{putline, "exit()."},
{expect, "Eshell"},
{putline, ""},
{putline, "35."},
{expect, "35\r\n"}],
[], [], ["-oldshell"]);
test_exit_initial(new) ->
rtnode:run(
[{putline, ""},
{expect, "1> $"},
{putline, "2."},
{expect, "2"},
{putline,"exit()."},
{expect, "Eshell"},
{expect, "1> $"},
{putline, "35."},
{expect, "35\r\n"}]).
stop_during_init(Config) when is_list(Config) ->
{RunErl,_ToErl,[Erl|ErlArgs]} = rtnode:get_progs(),
case rtnode:create_tempdir() of
{error, Reason} ->
{skip, Reason};
Tempdir ->
XArg = " -kernel shell_history enabled -s init stop",
rtnode:start_runerl_command(RunErl, Tempdir, "\\\""++Erl++"\\\""++ErlArgs++XArg),
Logs = rtnode:read_logs(Tempdir),
rtnode:dump_logs(Logs),
nomatch = binary:match(map_get("erlang.log.1", Logs),
<<"*** ERROR: Shell process terminated! ***">>),
ok
end.
%% This testcase tests that the correct wrapping characters are added
%% When a terminal has the xn flag set, it means that wrapping may not
%% work as expected and historically the ttysl driver has always inserted
%% a " \b" (i.e. space + backspace) when an output string ends on that line
%% in order for the cursor to be at col 0 on the next line instead of col max
%% on the current line.
%%
%% This caused problems when a string was `columns` long and then ended in "\r\n"
%% as it would first wrap due to " \b" and then output "\r\n" that cause a double
%% newline to happen.
%%
%% This testcase tests that we get a " \b" when we should and we get a "\r\n" when
%% we should.
wrap(Config) when is_list(Config) ->
case proplists:get_value(default_shell, Config) of
new ->
As = lists:duplicate(20,"a"),
rtnode:run(
[{putline, "io:columns()."},
{expect, "{ok,20}\r\n"},
{putline, ["io:format(\"~s\",[lists:duplicate(20,\"a\")])."]},
{expect, As ++ " \b"},
{putline, ["io:format(\"~s~n~s\",[lists:duplicate(20,\"a\"),lists:duplicate(20,\"a\")])."]},
{expect, As ++ "\r\n" ++ As ++ " \b"}
],
[],
"stty rows 40; stty columns 20; ");
_ ->
ok
end,
ok.
%% This testcase tests that shell_history works as it should.
%% We use Ctrl + P = Cp=[$\^p] in order to navigate up
%% We use Ctrl + N = Cp=[$\^n] in order to navigate down
%% We use Ctrl + B = Cp=[$\^b] in order to navigate left
%% in the console. We also need to sleep for a while in order
%% for the system to update the display before we enter more
%% commands.
shell_history(Config) when is_list(Config) ->
Path = shell_history_path(Config, "basic"),
rtnode:run(
[{putline, "echo1."},
{expect, "echo1\r\n"},
{putline, "echo2."},
{expect, "echo2\r\n"},
{putline, "echo3."},
{expect, "echo3\r\n"},
{putline, "echo4."},
{expect, "echo4\r\n"},
{putline, "echo5."},
{expect, "echo5\r\n"}
], [], [], mk_history_param(Path)),
receive after 1000 -> ok end,
rtnode:run(
[{sleep,100},
{putline, ""},
%% the init:stop that stopped the node is dropped
{putdata, [$\^p]}, {expect, "echo5[.]$"},
{putdata, [$\n]},
{expect, "echo5\r\n"},
{putdata, [$\^p]}, {expect, "echo5[.]$"},
{putdata, [$\^p]}, {expect, "echo4[.]$"},
{putdata, [$\^p]}, {expect, "echo3[.]$"},
{putdata, [$\^p]}, {expect, "echo2[.]$"},
{putdata, [$\^n]}, {expect, "echo3[.]$"},
{putdata, [$\^n]}, {expect, "echo4[.]$"},
{putdata, [$\^b]}, {sleep,50}, %% the echo4. (cursor moved one left)
{putline, ["ECHO"]},
{expect, "echo4ECHO\r\n"}
], [], [],
mk_history_param(Path)),
receive after 1000 -> ok end,
%% ignore input given after io:get_line, and after h(module) pager
rtnode:run(
[{putdata, "io:get_line(\"getline>\").\n"},
{expect, "getline>"},
{putline, "hej"}, {expect, "hej\r\n"},
{putdata, [$\^p]}, {expect, "\\Qio:get_line(\"getline>\")\\E[.]$"}
], [], [],
mk_history_param(Path)),
ok.
shell_history_resize(Config) ->
Path = shell_history_path(Config, "resize"),
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"},
{putline, "echo2."},
{expect, "echo2\r\n"}
], [], [], ["-kernel","shell_history_file_bytes","123456"] ++
mk_history_param(Path)),
{ok, Logs} =
rtnode:run(
[{sleep,100},
{putline, ""},
{putdata, [$\^p]}, {expect, "echo2[.]$$"},
{putdata, [$\^p]}, {expect, "echo[.]$"},
{putdata, [$\n]},
{expect, "echo"}
], [], [], ["-kernel","shell_history_file_bytes","654321"] ++
mk_history_param(Path)),
rtnode:check_logs(
"erlang.log.1",
"The configured log history file size is different from the size "
"of the log file on disk", Logs),
ok.
shell_history_eaccess(Config) ->
Path = shell_history_path(Config, "eaccess"),
file:make_dir(filename:dirname(Path)),
ok = file:make_dir(Path),
{ok, Info} = file:read_file_info(Path),
try
NoExecMode = Info#file_info.mode band (bnot 8#111),
file:write_file_info(Path,Info#file_info{ mode = NoExecMode }),
%% Cannot create history log in folder
{ok, Logs1} =
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"}
], [], [], mk_history_param(Path)),
ct:log("~p",[Logs1]),
rtnode:check_logs("erlang.log.1", "Error handling file", Logs1),
%% shell_docs recursively creates the folder to store the
%% logs. This test checks that erlang still starts if we
%% cannot create the folders to the path.
{ok, Logs2} =
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"}
], [], [], mk_history_param(filename:join(Path,"logs"))),
rtnode:check_logs("erlang.log.1", "Error handling file", Logs2)
after
file:write_file_info(Path, Info)
end,
ok.
shell_history_repair(Config) ->
Path = shell_history_path(Config, "repair"),
%% We stop a node without closing the log
shell_history_halt(Path),
{ok, Logs} =
rtnode:run(
[{putline, ""},
{putdata, [$\^p]}, {expect, "echo[.]$"},
{putdata, [$\n]},
{expect, "echo\r\n"}
], [], [], mk_history_param(Path)),
%% The regexp below checks that he string is NOT part of the log
rtnode:check_logs("erlang.log.1",
"The shell history log file was corrupted and was repaired",
false,
Logs),
ok.
shell_history_repair_corrupt(Config) ->
Path = shell_history_path(Config, "repair_corrupt"),
%% We stop a node without closing the log
shell_history_halt(Path),
%% We corrupt the disklog
{ok, D} = file:open(filename:join(Path,"erlang-shell-log.1"), [read,append]),
ok = file:write(D, [10,10]),
ok = file:close(D),
{ok, Logs} =
rtnode:run(
[{putline, ""},
{putdata, [$\^p]}, {expect, "echo[.]$"},
{putdata, [$\n]},
{expect, "echo\r\n"}
], [], [], mk_history_param(Path)),
rtnode:check_logs("erlang.log.1",
"The shell history log file was corrupted and was repaired.",
Logs),
ok.
shell_history_corrupt(Config) ->
Path = shell_history_path(Config, "corrupt"),
%% We initialize the shell history log with a known value.
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"},
{putline, "echo2."},
{expect, "echo2\r\n"}
], [], [], mk_history_param(Path)),
%% We corrupt the disklog.
{ok, D} = file:open(filename:join(Path,"erlang-shell-log.1"), [read, append]),
ok = file:write(D, [10, 10]),
ok = file:close(D),
{ok, Logs} =
rtnode:run(
[{putline, ""},
{putdata, [$\^p]}, {expect, "echo2[.]$"},
{putdata, [$\^p]}, {expect, "echo[.]$"},
{putdata, [$\n]},
{expect, "echo\r\n"}
], [], [], mk_history_param(Path)),
rtnode:check_logs("erlang.log.1", "Invalid chunk in the file", Logs),
ok.
%% Stop the node without closing the log.
shell_history_halt(Path) ->
try
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"},
{sleep, 2500}, % disk_log internal cache timer is 2000 ms
{putline, "halt(0)."},
{expect, "\r\n"},
{sleep, 1000} %% wait for node to terminate
], [], [], mk_history_param(Path))
catch
_:_ ->
ok
end.
shell_history_path(Config, TestCase) ->
filename:join([proplists:get_value(priv_dir, Config),
"shell_history", TestCase]).
mk_history_param(Path) ->
["-kernel","shell_history","enabled",
"-kernel","shell_history_path","\"" ++ Path ++ "\"",
"-kernel","shell_history_drop","[\"init:stop().\"]"
].
shell_history_custom(_Config) ->
%% Up key: Ctrl + P = Cp=[$\^p]
rtnode:run(
[{expect, "1> $"},
%% {putline, ""},
{putdata, [$\^p]}, {expect, "0[.]"},
{putdata, [$\n]},
{expect, "0\r\n"},
{putline, "echo."},
{expect, "!echo\r\n"} % exclamation mark is printed by custom history module
], [], [], ["-kernel","shell_history",atom_to_list(?MODULE),
"-pz",filename:dirname(code:which(?MODULE))]),
ok.
shell_history_custom_errors(_Config) ->
%% Check that we can start with a node with an undefined
%% provider module.
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"}
], [], [], ["-kernel","shell_history","very_broken",
"-pz",filename:dirname(code:which(?MODULE))]),
%% Check that we can start with a node with a provider module
%% that crashes in load/0.
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"}
], [], [], ["-kernel","shell_history",atom_to_list(?MODULE),
"-kernel","provider_load","crash",
"-pz",filename:dirname(code:which(?MODULE))]),
%% Check that we can start with a node with a provider module
%% that return incorrect in load/0.
rtnode:run(
[{putline, "echo."},
{expect, "echo\r\n"}
], [], [], ["-kernel","shell_history",atom_to_list(?MODULE),
"-kernel","provider_load","badreturn",
"-pz",filename:dirname(code:which(?MODULE))]),
%% Check that we can start with a node with a provider module
%% that crashes in load/0.
rtnode:run(
[{putline, "echo."},
{expect, "(Disabling shell history logging.|echo)"},
{expect, "(Disabling shell history logging.|echo)"}
], [], [], ["-kernel","shell_history",atom_to_list(?MODULE),
"-kernel","provider_add","crash",
"-pz",filename:dirname(code:which(?MODULE))]),
%% Check that we can start with a node with a provider module
%% that return incorrect in load/0.
rtnode:run(
[{putline, "echo."},
{expect, "It returned {error,badreturn}.\r\n"},
{expect, "echo\r\n"}
], [], [], ["-kernel","shell_history",atom_to_list(?MODULE),
"-kernel","provider_add","badreturn",
"-pz",filename:dirname(code:which(?MODULE))]),
ok.
load() ->
case application:get_env(kernel,provider_load) of
{ok, crash} ->
error(crash);
{ok, badreturn} ->
%% Should return a list of string()
ok;
_ ->
["0.\n\n"]
end.
add(_Line) ->
case application:get_env(kernel,provider_add) of
{ok, crash} ->
error(crash);
{ok, badreturn} ->
%% Should return ok
{error, badreturn};
_ ->
io:format("!", [])
end.
%% Tests that local shell can be started by means of job control.
job_control_local(Config) when is_list(Config) ->
case proplists:get_value(default_shell, Config) of
old ->
%% Old shell tests
{skip,"No new shell found"};
new ->
%% New shell tests
rtnode:run(
[{putline, ""},
{expect, "1> $"},
{putline, "2."},
{expect, "\r\n2\r\n"},
{putline, "\^g"},
{expect, "--> $"},
{putline, "s"},
{expect, "--> $"},
{putline, "c"},
{expect, "\r\nEshell"},
{expect, "1> $"},
{putline, "35."},
{expect, "\r\n35\r\n"},
{expect, "2> $"},
{putline, "receive M -> M end.\r\n"},
{putline, "\^g"},
{expect, "--> $"},
{putline, "i 3"},
{expect, "Unknown job"},
{expect, "--> $"},
{putline, "i 2"},
{expect, "--> $"},
{putline, "c"},
{expect, "[*][*] exception exit: killed"},
{expect, "[23]>"},
{putline, "\^g"},
{expect, "--> $"},
{putline, "k 3"},
{expect, "Unknown job"},
{expect, "--> $"},
{putline, "k 2"},
{expect, "--> $"},
{putline, "k"},
{expect, "Unknown job"},
{expect, "--> $"},
{putline, "c"},
{expect, "Unknown job"},
{expect, "--> $"},
{putline, "i"},
{expect, "Unknown job"},
{expect, "--> $"},
{putline, "?"},
{expect, "this message"},
{expect, "--> $"},
{putline, "h"},
{expect, "this message"},
{expect, "--> $"},
{putdata, "\t"}, %% Test that we don't crash
{sleep, 100},
{putline, ""},
{expect, "^\r\n\r\n --> $"},
{putdata, "\^r"}, %% Test that we don't crash
{sleep, 100},
{putline, ""},
{expect, "^\r\n\r\n --> $"},
{putline, "c 1"},
{expect, "\r\n"},
{putline, "35."},
{expect, "\r\n35\r\n"},
{expect, "[23]> $"}
]),
ok
end.
%% Tests that remote shell can be started by means of job control.
job_control_remote(Config) when is_list(Config) ->
case proplists:get_value(default_shell, Config) of
old ->
{skip,"No new shell found"};
_ ->
{ok, Peer, NSNode} = ?CT_PEER(#{ args => ["-connect_all","false"],
peer_down => continue }),
try
test_remote_job_control(NSNode)
after
peer:stop(Peer)
end
end.
%% Tests that remote shell can be started by means of job control to
%% -noshell node.
job_control_remote_noshell(Config) when is_list(Config) ->
case proplists:get_value(default_shell, Config) of
old ->
{skip,"No new shell found"};
_ ->
{ok, Peer, NSNode} = ?CT_PEER(#{ name => peer:random_name(test_remote_job_control),
args => ["-connect_all","false","-noshell"],
peer_down => continue }),
try
test_remote_job_control(NSNode)
after
peer:stop(Peer)
end
end.
test_remote_job_control(Node) ->
RemNode = peer:random_name(test_remote_job_control),
Pid = spawn_link(Node, fun() ->
receive die ->
ok
end
end),
PidStr = erpc:call(Node, erlang, pid_to_list, [Pid]),
true = erpc:call(Node, erlang, register, [kalaskula,Pid]),
PrintedNode = printed_atom(Node),
CookieString = printed_atom(erlang:get_cookie()),
rtnode:run(
[{putline, ""},
{putline, "erlang:get_cookie()."},
{expect, "\r\n\\Q" ++ CookieString ++ "\\E"},
{putdata, "\^g"},
{expect, " --> $"},
{putline, "r " ++ PrintedNode},
{expect, "\r\n"},
{putline, "j"},
{expect, "1 {shell,start,\\[init]}"},
{expect, "2[*] {\\Q"++PrintedNode++"\\E,shell,start,\\[]}"},
{expect, " --> $"},
{putline, "c"},
{expect, "\r\n"},
{expect, "Eshell"},
{expect, "\\Q(" ++ atom_to_list(Node) ++")1> \\E$"},
{putline, "whereis(kalaskula)."},
{expect, PidStr},
{putline, "kalaskula ! die."},
{putline, "exit()."},
{expect, "[*][*][*] Shell process terminated!"},
{putdata, "\^g"},
{expect, " --> $"},
{putline, "j"},
{expect, "1 {shell,start,\\[init]}"},
{expect, " --> $"},
{putline, "c"},
{expect, "Unknown job"},
{expect, " --> $"},
{putline, "c 1"},
{expect, "\\Q("++RemNode++"@\\E[^)]*\\)[12]> $"},
{putdata, "\^g"},
{expect, " --> $"},
{putline, "j"},
{expect, "1[*] {shell,start,\\[init]}"},
{putline, "c"},
{expect, "\\Q("++RemNode++"@\\E[^)]*\\)[123]> $"},
{sleep, 100},
{putline, "35."},
{expect, "\\Q("++RemNode++"@\\E[^)]*\\)[123]> $"}
], RemNode),
Pid ! die,
ok.
%% Tests various control keys.
ctrl_keys(_Config) ->
Cu = [$\^u],
Cw = [$\^w],
Cy = [$\^y],
Home = [27,$O,$H],
End = [27,$O,$F],
rtnode:run(
[{putline,""},
{putline,"2."},
{expect,"2"},
{putline,"\"hello "++Cw++"world\"."}, % test <CTRL>+W
{expect,"\"world\""},
{putline,"\"hello "++Cu++"\"world\"."}, % test <CTRL>+U
{expect,"\"world\""},
{putline,"world\"."++Home++"\"hello "}, % test <HOME>
{expect,"\"hello world\""},
{putline,"world"++Home++"\"hello "++End++"\"."}, % test <END>
{expect,"\"hello world\""},
{putline,"\"hello world\""++Cu++Cy++"."},
{expect,"\"hello world\""}] ++
wordLeft() ++ wordRight()),
ok.
wordLeft() ->
L1 = "\e\e[D",
L2 = "\e[5D",
L3 = "\e[1;5D",
wordLeft(L1) ++ wordLeft(L2) ++ wordLeft(L3).
wordLeft(Chars) ->
End = "\eOF",
[{putline,"\"world\""++Chars++"hello "++End++"."},
{expect,"\"hello world\""}].
wordRight() ->
R1 = "\e\e[C",
R2 = "\e[5C",
R3 = "\e[1;5C",
wordRight(R1) ++ wordRight(R2) ++ wordRight(R3).
wordRight(Chars) ->
Home = "\eOH",
[{putline,"world"++Home++"\"hello "++Chars++"\"."},
{expect,"\"hello world\""}].
%% Test that -remsh works
remsh_basic(Config) when is_list(Config) ->
{ok, Peer, TargetNode} = ?CT_PEER(),
TargetNodeStr = printed_atom(TargetNode),
[_Name,Host] = string:split(atom_to_list(node()), "@"),
PreCmds = [{putline,""},
{putline,"node()."},
{expect, "\\Q" ++ TargetNodeStr ++ "\\E\r\n"}],
PostCmds = quit_hosting_node(),
%% Test that remsh works with explicit -sname.
HostNode = atom_to_list(?FUNCTION_NAME) ++ "_host",
HostNodeStr = printed_atom(list_to_atom(HostNode ++ "@" ++ Host)),
rtnode:run(
PreCmds ++
[{putline,"nodes()."},
{expect, "\\Q" ++ HostNodeStr ++ "\\E"}] ++
PostCmds,
HostNode, " ", "-remsh " ++ TargetNodeStr),
%% Test that remsh works without -sname.
rtnode:run(PreCmds ++ PostCmds, [], " ", "-remsh " ++ TargetNodeStr),
%% Test that if multiple remsh are given, we select the first
rtnode:run([{expect, "Multiple"}] ++ PreCmds ++ PostCmds,
[], " ",
"-remsh " ++ TargetNodeStr ++ " -remsh invalid_node"),
peer:stop(Peer),
ok.
%% Test that if we cannot connect to a node, we get a correct error
remsh_error(_Config) ->
"Could not connect to \"invalid_node\"\n" =
os:cmd(ct:get_progname() ++ " -remsh invalid_node"),
RemNode = peer:random_name(remsh_error),
rtnode:run([
{putdata, "\^g"},
{expect, " --> $"},
{putline, "r invalid_node"},
{expect, "Could not connect to node invalid_node"},
{expect, "--> $"}], RemNode),
ok.
quit_hosting_node() ->
%% Command sequence for entering a shell on the hosting node.
[{putdata, "\^g"},
{expect, "--> $"},
{putline, "s"},
{expect, "--> $"},
{putline, "c"},
{expect, ["Eshell"]},
{expect, ["1> $"]}].
remsh_dont_terminate_remote(Config) when is_list(Config) ->
{ok, Peer, TargetNode} = ?CT_PEER(),
TargetNodeStr = printed_atom(TargetNode),
[_Name,Host] = string:split(atom_to_list(node()), "@"),
%% Test that remsh works with explicit -sname.
HostNode = atom_to_list(?FUNCTION_NAME) ++ "_host",
%% Start a remote shell that will terminate because of an end of file
FullCmd = "erl -sname " ++ HostNode ++
" -remsh " ++ TargetNodeStr ++
" < /dev/null",
ct:log("~ts",[FullCmd]),
Output = os:cmd(FullCmd),
match = re:run(Output, "Shell process terminated! Read EOF", [{capture, none}]),
%% Start another remote shell, make sure the remote node has not terminated
rtnode:run([{putline, "node()."},
{expect, "\\Q" ++ TargetNodeStr ++ "\\E\r\n"}] ++
quit_hosting_node(),
HostNode, " ", "-remsh " ++ TargetNodeStr),
peer:stop(Peer),
ok.
%% Test that -remsh works with long names.
remsh_longnames(Config) when is_list(Config) ->
%% If we cannot resolve the domain, we need to add localhost to the longname
Domain =
case inet_db:res_option(domain) of
[] ->
"@127.0.0.1";
_ -> ""
end,
Name = peer:random_name(?FUNCTION_NAME),
case rtnode:start(" -name " ++ Name ++ Domain) of
{ok, _SRPid, STPid, SNode, SState} ->
try
{ok, _CRPid, CTPid, CNode, CState} =
rtnode:start("-name undefined" ++ Domain ++
" -remsh " ++ Name),
try
ok = rtnode:send_commands(
SNode,
STPid,
[{putline, ""},
{putline, "node()."},
{expect, "\\Q" ++ Name ++ "\\E"}], 1),
ok = rtnode:send_commands(
CNode,
CTPid,
[{putline, ""},
{putline, "node()."},
{expect, "\\Q" ++ Name ++ "\\E"} | quit_hosting_node()], 1)
after
rtnode:dump_logs(rtnode:stop(CState))
end
after
rtnode:dump_logs(rtnode:stop(SState))
end;
{skip, _} = Else ->
Else
end.
%% Test that -remsh works without epmd.
remsh_no_epmd(Config) when is_list(Config) ->
EPMD_ARGS = "-start_epmd false -erl_epmd_port 12346 ",
Name = ?CT_PEER_NAME(),
case rtnode:start([],"ERL_EPMD_PORT=12345 ",
EPMD_ARGS ++ " -sname " ++ Name) of
{ok, _SRPid, STPid, SNode, SState} ->
try
ok = rtnode:send_commands(
SNode,
STPid,
[{putline, ""},
{putline, "node()."},
{expect, "\\Q" ++ Name ++ "\\E"}], 1),
{ok, _CRPid, CTPid, CNode, CState} =
rtnode:start([],"ERL_EPMD_PORT=12345 ",
EPMD_ARGS ++ " -remsh "++Name),
try
ok = rtnode:send_commands(
CNode,
CTPid,
[{putline, ""},
{putline, "node()."},
{expect, "\\Q" ++ Name ++ "\\E"} | quit_hosting_node()], 1)
after
rtnode:dump_logs(rtnode:stop(CState))
end
after
rtnode:dump_logs(rtnode:stop(SState))
end;
{skip, _} = Else ->
Else
end.
remsh_expand_compatibility_25(Config) when is_list(Config) ->
{ok, _Peer, TargetNode} = ?CT_PEER(#{}), %% Create a vsn 26 node
NodeName = atom_to_list(TargetNode), %% compatibility
%% Start a node on vsn 25 but run the shell on vsn 26
case rtnode:start(peer:random_name(), "stty columns 200; ERL_AFLAGS= ", "-remsh "++NodeName, [{release, "25"}|Config]) of
{ok, _SRPid, STPid, _, SState} ->
try
ok = rtnode:send_commands(undefined,
STPid,
[{putdata, "erlang:is_atom\t"},
{expect, "\\Qerlang:is_atom(\\E"}|
quit_hosting_node()], 1)
after
Logs = rtnode:stop(SState),
rtnode:dump_logs(Logs)
end;
Else when element(1, Else) =/= ok -> Else
end.
remsh_expand_compatibility_later_version(Config) when is_list(Config) ->
PrivDir = proplists:get_value(priv_dir, Config),
case ?CT_PEER_REL([], "25", PrivDir) of
not_available -> {skip, "25 not available"};
{ok, _Peer, TargetNode} ->
NodeName = atom_to_list(TargetNode),
%% Start a node on later version but run the shell on vsn 25
case rtnode:start(peer:random_name(), "stty columns 200; ERL_AFLAGS= ", "-remsh " ++ NodeName, Config) of
{ok, _SRPid, STPid, _, SState} ->
try
ok = rtnode:send_commands(undefined,
STPid,
[{putdata, "erlang:is_atom\t"},
{expect, "\\Qerlang:is_atom(\\E"}|
quit_hosting_node()], 1)
after
Logs = rtnode:stop(SState),
rtnode:dump_logs(Logs)
end;
Else when element(1, Else) =/= ok -> Else
end
end.
printed_atom(A) ->
lists:flatten(io_lib:format("~w", [A])).
|