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 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
|
import asyncio
import errno
import fcntl
import inspect
import logging
import os
import platform
import pty
import resource
import signal
import stat
import sys
import tempfile
import time
import unittest
import unittest.mock
import warnings
from asyncio.queues import Queue as AQueue
from contextlib import contextmanager
from functools import partial, wraps
from hashlib import md5
from io import BytesIO, StringIO
from os.path import dirname, exists, join, realpath, split
from pathlib import Path
import sh
THIS_DIR = Path(__file__).resolve().parent
RAND_BYTES = os.urandom(10)
# we have to use the real path because on osx, /tmp is a symlink to
# /private/tmp, and so assertions that gettempdir() == sh.pwd() will fail
tempdir = Path(tempfile.gettempdir()).resolve()
IS_MACOS = platform.system() in ("AIX", "Darwin")
def hash(a: str):
h = md5(a.encode("utf8") + RAND_BYTES)
return h.hexdigest()
def randomize_order(a, b):
h1 = hash(a)
h2 = hash(b)
if h1 == h2:
return 0
elif h1 < h2:
return -1
else:
return 1
unittest.TestLoader.sortTestMethodsUsing = staticmethod(randomize_order)
# these 3 functions are helpers for modifying PYTHONPATH with a module's main
# directory
def append_pythonpath(env, path):
key = "PYTHONPATH"
pypath = [p for p in env.get(key, "").split(":") if p]
pypath.insert(0, path)
pypath = ":".join(pypath)
env[key] = pypath
def get_module_import_dir(m):
mod_file = inspect.getsourcefile(m)
is_package = mod_file.endswith("__init__.py")
mod_dir = dirname(mod_file)
if is_package:
mod_dir, _ = split(mod_dir)
return mod_dir
def append_module_path(env, m):
append_pythonpath(env, get_module_import_dir(m))
system_python = sh.Command(sys.executable)
# this is to ensure that our `python` helper here is able to import our local sh
# module, and not the system one
baked_env = os.environ.copy()
append_module_path(baked_env, sh)
python = system_python.bake(_env=baked_env, _return_cmd=True)
pythons = python.bake(_return_cmd=False)
def requires_progs(*progs):
missing = []
for prog in progs:
try:
sh.Command(prog)
except sh.CommandNotFound:
missing.append(prog)
friendly_missing = ", ".join(missing)
return unittest.skipUnless(
len(missing) == 0, f"Missing required system programs: {friendly_missing}"
)
requires_posix = unittest.skipUnless(os.name == "posix", "Requires POSIX")
requires_utf8 = unittest.skipUnless(
sh.DEFAULT_ENCODING == "UTF-8", "System encoding must be UTF-8"
)
not_macos = unittest.skipUnless(not IS_MACOS, "Doesn't work on MacOS")
def requires_poller(poller):
use_select = bool(int(os.environ.get("SH_TESTS_USE_SELECT", "0")))
cur_poller = "select" if use_select else "poll"
return unittest.skipUnless(
cur_poller == poller, f"Only enabled for select.{cur_poller}"
)
@contextmanager
def ulimit(key, new_soft):
soft, hard = resource.getrlimit(key)
resource.setrlimit(key, (new_soft, hard))
try:
yield
finally:
resource.setrlimit(key, (soft, hard))
def create_tmp_test(code, prefix="tmp", delete=True, **kwargs):
"""creates a temporary test file that lives on disk, on which we can run
python with sh"""
py = tempfile.NamedTemporaryFile(prefix=prefix, delete=delete)
code = code.format(**kwargs)
code = code.encode("UTF-8")
py.write(code)
py.flush()
# make the file executable
st = os.stat(py.name)
os.chmod(py.name, st.st_mode | stat.S_IEXEC)
# we don't explicitly close, because close will remove the file, and we
# don't want that until the test case is done. so we let the gc close it
# when it goes out of scope
return py
class BaseTests(unittest.TestCase):
def setUp(self):
warnings.simplefilter("ignore", ResourceWarning)
def tearDown(self):
warnings.simplefilter("default", ResourceWarning)
def assert_oserror(self, num, fn, *args, **kwargs):
try:
fn(*args, **kwargs)
except OSError as e:
self.assertEqual(e.errno, num)
def assert_deprecated(self, fn, *args, **kwargs):
with warnings.catch_warnings(record=True) as w:
fn(*args, **kwargs)
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
class ArgTests(BaseTests):
def test_list_args(self):
processed = sh._aggregate_keywords({"arg": [1, 2, 3]}, "=", "--")
self.assertListEqual(processed, ["--arg=1", "--arg=2", "--arg=3"])
def test_bool_values(self):
processed = sh._aggregate_keywords({"truthy": True, "falsey": False}, "=", "--")
self.assertListEqual(processed, ["--truthy"])
def test_space_sep(self):
processed = sh._aggregate_keywords({"arg": "123"}, " ", "--")
self.assertListEqual(processed, ["--arg", "123"])
@requires_posix
class FunctionalTests(BaseTests):
def setUp(self):
self._environ = os.environ.copy()
super().setUp()
def tearDown(self):
os.environ = self._environ
super().tearDown()
def test_print_command(self):
from sh import ls, which
actual_location = which("ls").strip()
out = str(ls)
self.assertEqual(out, actual_location)
def test_unicode_arg(self):
from sh import echo
test = "漢字"
p = echo(test, _encoding="utf8")
output = p.strip()
self.assertEqual(test, output)
def test_unicode_exception(self):
from sh import ErrorReturnCode
py = create_tmp_test("exit(1)")
arg = "漢字"
native_arg = arg
try:
python(py.name, arg, _encoding="utf8")
except ErrorReturnCode as e:
self.assertIn(native_arg, str(e))
else:
self.fail("exception wasn't raised")
def test_pipe_fd(self):
py = create_tmp_test("""print("hi world")""")
read_fd, write_fd = os.pipe()
python(py.name, _out=write_fd)
out = os.read(read_fd, 10)
self.assertEqual(out, b"hi world\n")
def test_trunc_exc(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write("a" * 1000)
sys.stderr.write("b" * 1000)
exit(1)
"""
)
self.assertRaises(sh.ErrorReturnCode_1, python, py.name)
def test_number_arg(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
options, args = parser.parse_args()
print(args[0])
"""
)
out = python(py.name, 3).strip()
self.assertEqual(out, "3")
def test_arg_string_coercion(self):
py = create_tmp_test(
"""
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-n", type=int)
parser.add_argument("--number", type=int)
ns = parser.parse_args()
print(ns.n + ns.number)
"""
)
out = python(py.name, n=3, number=4, _long_sep=None).strip()
self.assertEqual(out, "7")
def test_empty_stdin_no_hang(self):
py = create_tmp_test(
"""
import sys
data = sys.stdin.read()
sys.stdout.write("no hang")
"""
)
out = pythons(py.name, _in="", _timeout=2)
self.assertEqual(out, "no hang")
out = pythons(py.name, _in=None, _timeout=2)
self.assertEqual(out, "no hang")
def test_exit_code(self):
from sh import ErrorReturnCode_3
py = create_tmp_test(
"""
exit(3)
"""
)
self.assertRaises(ErrorReturnCode_3, python, py.name)
def test_patched_glob(self):
from glob import glob
py = create_tmp_test(
"""
import sys
print(sys.argv[1:])
"""
)
files = glob("*.faowjefoajweofj")
out = python(py.name, files).strip()
self.assertEqual(out, "['*.faowjefoajweofj']")
def test_exit_code_with_hasattr(self):
from sh import ErrorReturnCode_3
py = create_tmp_test(
"""
exit(3)
"""
)
try:
out = python(py.name, _iter=True)
# hasattr can swallow exceptions
hasattr(out, "something_not_there")
list(out)
self.assertEqual(out.exit_code, 3)
self.fail("Command exited with error, but no exception thrown")
except ErrorReturnCode_3:
pass
def test_exit_code_from_exception(self):
from sh import ErrorReturnCode_3
py = create_tmp_test(
"""
exit(3)
"""
)
self.assertRaises(ErrorReturnCode_3, python, py.name)
try:
python(py.name)
except Exception as e:
self.assertEqual(e.exit_code, 3)
def test_stdin_from_string(self):
from sh import sed
self.assertEqual(
sed(_in="one test three", e="s/test/two/").strip(), "one two three"
)
def test_ok_code(self):
from sh import ErrorReturnCode_1, ErrorReturnCode_2, ls
exc_to_test = ErrorReturnCode_2
code_to_pass = 2
if IS_MACOS:
exc_to_test = ErrorReturnCode_1
code_to_pass = 1
self.assertRaises(exc_to_test, ls, "/aofwje/garogjao4a/eoan3on")
ls("/aofwje/garogjao4a/eoan3on", _ok_code=code_to_pass)
ls("/aofwje/garogjao4a/eoan3on", _ok_code=[code_to_pass])
ls("/aofwje/garogjao4a/eoan3on", _ok_code=range(code_to_pass + 1))
def test_ok_code_none(self):
py = create_tmp_test("exit(0)")
python(py.name, _ok_code=None)
def test_ok_code_exception(self):
from sh import ErrorReturnCode_0
py = create_tmp_test("exit(0)")
self.assertRaises(ErrorReturnCode_0, python, py.name, _ok_code=2)
def test_none_arg(self):
py = create_tmp_test(
"""
import sys
print(sys.argv[1:])
"""
)
maybe_arg = "some"
out = python(py.name, maybe_arg).strip()
self.assertEqual(out, "['some']")
maybe_arg = None
out = python(py.name, maybe_arg).strip()
self.assertEqual(out, "[]")
def test_quote_escaping(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
options, args = parser.parse_args()
print(args)
"""
)
out = python(py.name, "one two three").strip()
self.assertEqual(out, "['one two three']")
out = python(py.name, 'one "two three').strip()
self.assertEqual(out, "['one \"two three']")
out = python(py.name, "one", "two three").strip()
self.assertEqual(out, "['one', 'two three']")
out = python(py.name, "one", 'two "haha" three').strip()
self.assertEqual(out, "['one', 'two \"haha\" three']")
out = python(py.name, "one two's three").strip()
self.assertEqual(out, '["one two\'s three"]')
out = python(py.name, "one two's three").strip()
self.assertEqual(out, '["one two\'s three"]')
def test_multiple_pipes(self):
import time
py = create_tmp_test(
"""
import sys
import os
import time
for l in "andrew":
sys.stdout.write(l)
time.sleep(.2)
"""
)
inc_py = create_tmp_test(
"""
import sys
while True:
letter = sys.stdin.read(1)
if not letter:
break
sys.stdout.write(chr(ord(letter)+1))
"""
)
def inc(*args, **kwargs):
return python("-u", inc_py.name, *args, **kwargs)
class Derp:
def __init__(self):
self.times = []
self.stdout = []
self.last_received = None
def agg(self, line):
self.stdout.append(line.strip())
now = time.time()
if self.last_received:
self.times.append(now - self.last_received)
self.last_received = now
derp = Derp()
p = inc(
_in=inc(
_in=inc(_in=python("-u", py.name, _piped=True), _piped=True),
_piped=True,
),
_out=derp.agg,
)
p.wait()
self.assertEqual("".join(derp.stdout), "dqguhz")
self.assertTrue(all([t > 0.15 for t in derp.times]))
def test_manual_stdin_string(self):
from sh import tr
out = tr("[:lower:]", "[:upper:]", _in="andrew").strip()
self.assertEqual(out, "ANDREW")
def test_manual_stdin_iterable(self):
from sh import tr
test = ["testing\n", "herp\n", "derp\n"]
out = tr("[:lower:]", "[:upper:]", _in=test)
match = "".join([t.upper() for t in test])
self.assertEqual(out, match)
def test_manual_stdin_file(self):
import tempfile
from sh import tr
test_string = "testing\nherp\nderp\n"
stdin = tempfile.NamedTemporaryFile()
stdin.write(test_string.encode())
stdin.flush()
stdin.seek(0)
out = tr("[:lower:]", "[:upper:]", _in=stdin)
self.assertEqual(out, test_string.upper())
def test_manual_stdin_queue(self):
from sh import tr
try:
from Queue import Queue
except ImportError:
from queue import Queue
test = ["testing\n", "herp\n", "derp\n"]
q = Queue()
for t in test:
q.put(t)
q.put(None) # EOF
out = tr("[:lower:]", "[:upper:]", _in=q)
match = "".join([t.upper() for t in test])
self.assertEqual(out, match)
def test_environment(self):
"""tests that environments variables that we pass into sh commands
exist in the environment, and on the sh module"""
import os
# this is the environment we'll pass into our commands
env = {"HERP": "DERP"}
# first we test that the environment exists in our child process as
# we've set it
py = create_tmp_test(
"""
import os
for key in list(os.environ.keys()):
if key != "HERP":
del os.environ[key]
print(dict(os.environ))
"""
)
out = python(py.name, _env=env).strip()
self.assertEqual(out, "{'HERP': 'DERP'}")
py = create_tmp_test(
"""
import os, sys
sys.path.insert(0, os.getcwd())
import sh
for key in list(os.environ.keys()):
if key != "HERP":
del os.environ[key]
print(dict(HERP=sh.HERP))
"""
)
out = python(py.name, _env=env, _cwd=THIS_DIR).strip()
self.assertEqual(out, "{'HERP': 'DERP'}")
# Test that _env also accepts os.environ which is a mpping but not a dict.
os.environ["HERP"] = "DERP"
out = python(py.name, _env=os.environ, _cwd=THIS_DIR).strip()
self.assertEqual(out, "{'HERP': 'DERP'}")
def test_which(self):
# Test 'which' as built-in function
from sh import ls
which = sh._SelfWrapper__env.b_which
self.assertEqual(which("fjoawjefojawe"), None)
self.assertEqual(which("ls"), str(ls))
def test_which_paths(self):
# Test 'which' as built-in function
which = sh._SelfWrapper__env.b_which
py = create_tmp_test(
"""
print("hi")
"""
)
test_path = dirname(py.name)
_, test_name = os.path.split(py.name)
found_path = which(test_name)
self.assertEqual(found_path, None)
found_path = which(test_name, [test_path])
self.assertEqual(found_path, py.name)
def test_no_close_fds(self):
# guarantee some extra fds in our parent process that don't close on exec. we
# have to explicitly do this because at some point (I believe python 3.4),
# python started being more stringent with closing fds to prevent security
# vulnerabilities. python 2.7, for example, doesn't set CLOEXEC on
# tempfile.TemporaryFile()s
#
# https://www.python.org/dev/peps/pep-0446/
tmp = [tempfile.TemporaryFile() for i in range(10)]
for t in tmp:
flags = fcntl.fcntl(t.fileno(), fcntl.F_GETFD)
flags &= ~fcntl.FD_CLOEXEC
fcntl.fcntl(t.fileno(), fcntl.F_SETFD, flags)
py = create_tmp_test(
"""
import os
print(len(os.listdir("/dev/fd")))
"""
)
out = python(py.name, _close_fds=False).strip()
# pick some number greater than 4, since it's hard to know exactly how many fds
# will be open/inherted in the child
self.assertGreater(int(out), 7)
for t in tmp:
t.close()
def test_close_fds(self):
# guarantee some extra fds in our parent process that don't close on exec.
# we have to explicitly do this because at some point (I believe python 3.4),
# python started being more stringent with closing fds to prevent security
# vulnerabilities. python 2.7, for example, doesn't set CLOEXEC on
# tempfile.TemporaryFile()s
#
# https://www.python.org/dev/peps/pep-0446/
tmp = [tempfile.TemporaryFile() for i in range(10)]
for t in tmp:
flags = fcntl.fcntl(t.fileno(), fcntl.F_GETFD)
flags &= ~fcntl.FD_CLOEXEC
fcntl.fcntl(t.fileno(), fcntl.F_SETFD, flags)
py = create_tmp_test(
"""
import os
print(os.listdir("/dev/fd"))
"""
)
out = python(py.name).strip()
self.assertEqual(out, "['0', '1', '2', '3']")
for t in tmp:
t.close()
def test_pass_fds(self):
# guarantee some extra fds in our parent process that don't close on exec.
# we have to explicitly do this because at some point (I believe python 3.4),
# python started being more stringent with closing fds to prevent security
# vulnerabilities. python 2.7, for example, doesn't set CLOEXEC on
# tempfile.TemporaryFile()s
#
# https://www.python.org/dev/peps/pep-0446/
tmp = [tempfile.TemporaryFile() for i in range(10)]
for t in tmp:
flags = fcntl.fcntl(t.fileno(), fcntl.F_GETFD)
flags &= ~fcntl.FD_CLOEXEC
fcntl.fcntl(t.fileno(), fcntl.F_SETFD, flags)
last_fd = tmp[-1].fileno()
py = create_tmp_test(
"""
import os
print(os.listdir("/dev/fd"))
"""
)
out = python(py.name, _pass_fds=[last_fd]).strip()
inherited = [0, 1, 2, 3, last_fd]
inherited_str = [str(i) for i in inherited]
self.assertEqual(out, str(inherited_str))
for t in tmp:
t.close()
def test_no_arg(self):
import pwd
from sh import whoami
u1 = whoami().strip()
u2 = pwd.getpwuid(os.geteuid())[0]
self.assertEqual(u1, u2)
def test_incompatible_special_args(self):
from sh import ls
self.assertRaises(TypeError, ls, _iter=True, _piped=True)
def test_invalid_env(self):
from sh import ls
exc = TypeError
self.assertRaises(exc, ls, _env="XXX")
self.assertRaises(exc, ls, _env={"foo": 123})
self.assertRaises(exc, ls, _env={123: "bar"})
def test_exception(self):
from sh import ErrorReturnCode_2
py = create_tmp_test(
"""
exit(2)
"""
)
self.assertRaises(ErrorReturnCode_2, python, py.name)
def test_piped_exception1(self):
from sh import ErrorReturnCode_2
py = create_tmp_test(
"""
import sys
sys.stdout.write("line1\\n")
sys.stdout.write("line2\\n")
sys.stdout.flush()
exit(2)
"""
)
py2 = create_tmp_test("")
def fn():
list(python(python(py.name, _piped=True), "-u", py2.name, _iter=True))
self.assertRaises(ErrorReturnCode_2, fn)
def test_piped_exception2(self):
from sh import ErrorReturnCode_2
py = create_tmp_test(
"""
import sys
sys.stdout.write("line1\\n")
sys.stdout.write("line2\\n")
sys.stdout.flush()
exit(2)
"""
)
py2 = create_tmp_test("")
def fn():
python(python(py.name, _piped=True), "-u", py2.name)
self.assertRaises(ErrorReturnCode_2, fn)
def test_command_not_found(self):
from sh import CommandNotFound
def do_import():
from sh import aowjgoawjoeijaowjellll # noqa: F401
self.assertRaises(ImportError, do_import)
def do_import():
import sh
sh.awoefaowejfw
self.assertRaises(CommandNotFound, do_import)
def do_import():
import sh
sh.Command("ofajweofjawoe")
self.assertRaises(CommandNotFound, do_import)
def test_command_wrapper_equivalence(self):
from sh import Command, ls, which
self.assertEqual(Command(str(which("ls")).strip()), ls)
def test_doesnt_execute_directories(self):
save_path = os.environ["PATH"]
bin_dir1 = tempfile.mkdtemp()
bin_dir2 = tempfile.mkdtemp()
gcc_dir1 = os.path.join(bin_dir1, "gcc")
gcc_file2 = os.path.join(bin_dir2, "gcc")
try:
os.environ["PATH"] = os.pathsep.join((bin_dir1, bin_dir2))
# a folder named 'gcc', its executable, but should not be
# discovered by internal which(1)-clone
os.makedirs(gcc_dir1)
# an executable named gcc -- only this should be executed
bunk_header = "#!/bin/sh\necho $*"
with open(gcc_file2, "w") as h:
h.write(bunk_header)
os.chmod(gcc_file2, int(0o755))
from sh import gcc
self.assertEqual(gcc._path, gcc_file2)
self.assertEqual(
gcc("no-error", _return_cmd=True).stdout.strip(),
b"no-error",
)
finally:
os.environ["PATH"] = save_path
if exists(gcc_file2):
os.unlink(gcc_file2)
if exists(gcc_dir1):
os.rmdir(gcc_dir1)
if exists(bin_dir1):
os.rmdir(bin_dir1)
if exists(bin_dir1):
os.rmdir(bin_dir2)
def test_multiple_args_short_option(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", dest="long_option")
options, args = parser.parse_args()
print(len(options.long_option.split()))
"""
)
num_args = int(python(py.name, l="one two three")) # noqa: E741
self.assertEqual(num_args, 3)
num_args = int(python(py.name, "-l", "one's two's three's"))
self.assertEqual(num_args, 3)
def test_multiple_args_long_option(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--long-option", dest="long_option")
options, args = parser.parse_args()
print(len(options.long_option.split()))
"""
)
num_args = int(python(py.name, long_option="one two three", nothing=False))
self.assertEqual(num_args, 3)
num_args = int(python(py.name, "--long-option", "one's two's three's"))
self.assertEqual(num_args, 3)
def test_short_bool_option(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-s", action="store_true", default=False, dest="short_option")
options, args = parser.parse_args()
print(options.short_option)
"""
)
self.assertTrue(python(py.name, s=True).strip() == "True")
self.assertTrue(python(py.name, s=False).strip() == "False")
self.assertTrue(python(py.name).strip() == "False")
def test_long_bool_option(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--long-option", action="store_true", default=False, \
dest="long_option")
options, args = parser.parse_args()
print(options.long_option)
"""
)
self.assertTrue(python(py.name, long_option=True).strip() == "True")
self.assertTrue(python(py.name).strip() == "False")
def test_false_bool_ignore(self):
py = create_tmp_test(
"""
import sys
print(sys.argv[1:])
"""
)
test = True
self.assertEqual(python(py.name, test and "-n").strip(), "['-n']")
test = False
self.assertEqual(python(py.name, test and "-n").strip(), "[]")
def test_composition(self):
py1 = create_tmp_test(
"""
import sys
print(int(sys.argv[1]) * 2)
"""
)
py2 = create_tmp_test(
"""
import sys
print(int(sys.argv[1]) + 1)
"""
)
res = python(py2.name, python(py1.name, 8)).strip()
self.assertEqual("17", res)
def test_incremental_composition(self):
py1 = create_tmp_test(
"""
import sys
print(int(sys.argv[1]) * 2)
"""
)
py2 = create_tmp_test(
"""
import sys
print(int(sys.stdin.read()) + 1)
"""
)
res = python(py2.name, _in=python(py1.name, 8, _piped=True)).strip()
self.assertEqual("17", res)
def test_short_option(self):
from sh import sh
s1 = sh(c="echo test").strip()
s2 = "test"
self.assertEqual(s1, s2)
def test_long_option(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-l", "--long-option", action="store", default="", dest="long_option")
options, args = parser.parse_args()
print(options.long_option.upper())
"""
)
self.assertTrue(python(py.name, long_option="testing").strip() == "TESTING")
self.assertTrue(python(py.name).strip() == "")
def test_raw_args(self):
py = create_tmp_test(
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--long_option", action="store", default=None,
dest="long_option1")
parser.add_option("--long-option", action="store", default=None,
dest="long_option2")
options, args = parser.parse_args()
if options.long_option1:
print(options.long_option1.upper())
else:
print(options.long_option2.upper())
"""
)
self.assertEqual(
python(py.name, {"long_option": "underscore"}).strip(), "UNDERSCORE"
)
self.assertEqual(python(py.name, long_option="hyphen").strip(), "HYPHEN")
def test_custom_separator(self):
py = create_tmp_test(
"""
import sys
print(sys.argv[1])
"""
)
opt = {"long-option": "underscore"}
correct = "--long-option=custom=underscore"
out = python(py.name, opt, _long_sep="=custom=").strip()
self.assertEqual(out, correct)
# test baking too
correct = "--long-option=baked=underscore"
python_baked = python.bake(py.name, opt, _long_sep="=baked=")
out = python_baked().strip()
self.assertEqual(out, correct)
def test_custom_separator_space(self):
py = create_tmp_test(
"""
import sys
print(str(sys.argv[1:]))
"""
)
opt = {"long-option": "space"}
correct = ["--long-option", "space"]
out = python(py.name, opt, _long_sep=" ").strip()
self.assertEqual(out, str(correct))
def test_custom_long_prefix(self):
py = create_tmp_test(
"""
import sys
print(sys.argv[1])
"""
)
out = python(
py.name, {"long-option": "underscore"}, _long_prefix="-custom-"
).strip()
self.assertEqual(out, "-custom-long-option=underscore")
out = python(py.name, {"long-option": True}, _long_prefix="-custom-").strip()
self.assertEqual(out, "-custom-long-option")
# test baking too
out = python.bake(
py.name, {"long-option": "underscore"}, _long_prefix="-baked-"
)().strip()
self.assertEqual(out, "-baked-long-option=underscore")
out = python.bake(
py.name, {"long-option": True}, _long_prefix="-baked-"
)().strip()
self.assertEqual(out, "-baked-long-option")
def test_command_wrapper(self):
from sh import Command, which
ls = Command(str(which("ls")).strip())
wc = Command(str(which("wc")).strip())
c1 = int(wc(l=True, _in=ls("-A1", THIS_DIR, _return_cmd=True))) # noqa: E741
c2 = len(os.listdir(THIS_DIR))
self.assertEqual(c1, c2)
def test_background(self):
import time
from sh import sleep
start = time.time()
sleep_time = 0.5
p = sleep(sleep_time, _bg=True)
now = time.time()
self.assertLess(now - start, sleep_time)
p.wait()
now = time.time()
self.assertGreater(now - start, sleep_time)
def test_background_exception(self):
py = create_tmp_test("exit(1)")
p = python(py.name, _bg=True, _bg_exc=False) # should not raise
self.assertRaises(sh.ErrorReturnCode_1, p.wait) # should raise
def test_with_context(self):
import getpass
from sh import whoami
py = create_tmp_test(
"""
import sys
import os
import subprocess
print("with_context")
subprocess.Popen(sys.argv[1:], shell=False).wait()
"""
)
cmd1 = python.bake(py.name, _with=True)
with cmd1:
out = whoami()
self.assertIn("with_context", out)
self.assertIn(getpass.getuser(), out)
def test_with_context_args(self):
import getpass
from sh import whoami
py = create_tmp_test(
"""
import sys
import os
import subprocess
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-o", "--opt", action="store_true", default=False, dest="opt")
options, args = parser.parse_args()
if options.opt:
subprocess.Popen(args[0], shell=False).wait()
"""
)
with python(py.name, opt=True, _with=True):
out = whoami()
self.assertEqual(getpass.getuser(), out.strip())
with python(py.name, _with=True):
out = whoami()
self.assertEqual(out.strip(), "")
def test_with_context_nested(self):
echo_path = sh.echo._path
with sh.echo.bake("test1", _with=True):
with sh.echo.bake("test2", _with=True):
out = sh.echo("test3")
self.assertEqual(out.strip(), f"test1 {echo_path} test2 {echo_path} test3")
def test_binary_input(self):
py = create_tmp_test(
"""
import sys
data = sys.stdin.read()
sys.stdout.write(data)
"""
)
data = b"1234"
out = pythons(py.name, _in=data)
self.assertEqual(out, "1234")
def test_err_to_out(self):
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stdout.flush()
sys.stderr.write("stderr")
sys.stderr.flush()
"""
)
stdout = pythons(py.name, _err_to_out=True)
self.assertEqual(stdout, "stdoutstderr")
def test_err_to_out_and_sys_stdout(self):
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stdout.flush()
sys.stderr.write("stderr")
sys.stderr.flush()
"""
)
master, slave = os.pipe()
stdout = pythons(py.name, _err_to_out=True, _out=slave)
self.assertEqual(stdout, "")
self.assertEqual(os.read(master, 12), b"stdoutstderr")
def test_err_piped(self):
py = create_tmp_test(
"""
import sys
sys.stderr.write("stderr")
"""
)
py2 = create_tmp_test(
"""
import sys
while True:
line = sys.stdin.read()
if not line:
break
sys.stdout.write(line)
"""
)
out = pythons("-u", py2.name, _in=python("-u", py.name, _piped="err"))
self.assertEqual(out, "stderr")
def test_out_redirection(self):
import tempfile
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
file_obj = tempfile.NamedTemporaryFile()
out = python(py.name, _out=file_obj)
self.assertEqual(len(out), 0)
file_obj.seek(0)
actual_out = file_obj.read()
file_obj.close()
self.assertNotEqual(len(actual_out), 0)
# test with tee
file_obj = tempfile.NamedTemporaryFile()
out = python(py.name, _out=file_obj, _tee=True)
self.assertGreater(len(out), 0)
file_obj.seek(0)
actual_out = file_obj.read()
file_obj.close()
self.assertGreater(len(actual_out), 0)
def test_err_redirection(self):
import tempfile
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
file_obj = tempfile.NamedTemporaryFile()
p = python("-u", py.name, _err=file_obj)
file_obj.seek(0)
stderr = file_obj.read().decode()
file_obj.close()
self.assertEqual(p.stdout, b"stdout")
self.assertEqual(stderr, "stderr")
self.assertEqual(len(p.stderr), 0)
# now with tee
file_obj = tempfile.NamedTemporaryFile()
p = python(py.name, _err=file_obj, _tee="err")
file_obj.seek(0)
stderr = file_obj.read().decode()
file_obj.close()
self.assertEqual(p.stdout, b"stdout")
self.assertEqual(stderr, "stderr")
self.assertGreater(len(p.stderr), 0)
def test_out_and_err_redirection(self):
import tempfile
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
err_file_obj = tempfile.NamedTemporaryFile()
out_file_obj = tempfile.NamedTemporaryFile()
p = python(py.name, _out=out_file_obj, _err=err_file_obj, _tee=("err", "out"))
out_file_obj.seek(0)
stdout = out_file_obj.read().decode()
out_file_obj.close()
err_file_obj.seek(0)
stderr = err_file_obj.read().decode()
err_file_obj.close()
self.assertEqual(stdout, "stdout")
self.assertEqual(p.stdout, b"stdout")
self.assertEqual(stderr, "stderr")
self.assertEqual(p.stderr, b"stderr")
def test_tty_tee(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write("stdout")
"""
)
read, write = pty.openpty()
out = python("-u", py.name, _out=write).stdout
tee = os.read(read, 6)
self.assertEqual(out, b"")
self.assertEqual(tee, b"stdout")
os.close(write)
os.close(read)
read, write = pty.openpty()
out = python("-u", py.name, _out=write, _tee=True).stdout
tee = os.read(read, 6)
self.assertEqual(out, b"stdout")
self.assertEqual(tee, b"stdout")
os.close(write)
os.close(read)
def test_err_redirection_actual_file(self):
import tempfile
file_obj = tempfile.NamedTemporaryFile()
py = create_tmp_test(
"""
import sys
import os
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
stdout = pythons("-u", py.name, _err=file_obj.name)
file_obj.seek(0)
stderr = file_obj.read().decode()
file_obj.close()
self.assertEqual(stdout, "stdout")
self.assertEqual(stderr, "stderr")
def test_subcommand_and_bake(self):
import getpass
py = create_tmp_test(
"""
import sys
import os
import subprocess
print("subcommand")
subprocess.Popen(sys.argv[1:], shell=False).wait()
"""
)
cmd1 = python.bake(py.name)
out = cmd1.whoami()
self.assertIn("subcommand", out)
self.assertIn(getpass.getuser(), out)
def test_multiple_bakes(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write(str(sys.argv[1:]))
"""
)
out = python.bake(py.name).bake("bake1").bake("bake2")()
self.assertEqual("['bake1', 'bake2']", str(out))
def test_arg_preprocessor(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write(str(sys.argv[1:]))
"""
)
def arg_preprocess(args, kwargs):
args.insert(0, "preprocessed")
kwargs["a-kwarg"] = 123
return args, kwargs
cmd = pythons.bake(py.name, _arg_preprocess=arg_preprocess)
out = cmd("arg")
self.assertEqual("['preprocessed', 'arg', '--a-kwarg=123']", out)
def test_bake_args_come_first(self):
from sh import ls
ls = ls.bake(h=True)
ran = ls("-la", _return_cmd=True).ran
ft = ran.index("-h")
self.assertIn("-la", ran[ft:])
def test_output_equivalence(self):
from sh import whoami
iam1 = whoami()
iam2 = whoami()
self.assertEqual(iam1, iam2)
# https://github.com/amoffat/sh/pull/252
def test_stdout_pipe(self):
py = create_tmp_test(
r"""
import sys
sys.stdout.write("foobar\n")
"""
)
read_fd, write_fd = os.pipe()
python(py.name, _out=write_fd, u=True)
def alarm(sig, action):
self.fail("Timeout while reading from pipe")
import signal
signal.signal(signal.SIGALRM, alarm)
signal.alarm(3)
data = os.read(read_fd, 100)
self.assertEqual(b"foobar\n", data)
signal.alarm(0)
signal.signal(signal.SIGALRM, signal.SIG_DFL)
def test_stdout_callback(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(5): print(i)
"""
)
stdout = []
def agg(line):
stdout.append(line)
p = python("-u", py.name, _out=agg)
p.wait()
self.assertEqual(len(stdout), 5)
def test_stdout_callback_no_wait(self):
import time
py = create_tmp_test(
"""
import sys
import os
import time
for i in range(5):
print(i)
time.sleep(.5)
"""
)
stdout = []
def agg(line):
stdout.append(line)
python("-u", py.name, _out=agg, _bg=True)
# we give a little pause to make sure that the NamedTemporaryFile
# exists when the python process actually starts
time.sleep(0.5)
self.assertNotEqual(len(stdout), 5)
def test_stdout_callback_line_buffered(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(5): print("herpderp")
"""
)
stdout = []
def agg(line):
stdout.append(line)
p = python("-u", py.name, _out=agg, _out_bufsize=1)
p.wait()
self.assertEqual(len(stdout), 5)
def test_stdout_callback_line_unbuffered(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(5): print("herpderp")
"""
)
stdout = []
def agg(char):
stdout.append(char)
p = python("-u", py.name, _out=agg, _out_bufsize=0)
p.wait()
# + 5 newlines
self.assertEqual(len(stdout), len("herpderp") * 5 + 5)
def test_stdout_callback_buffered(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(5): sys.stdout.write("herpderp")
"""
)
stdout = []
def agg(chunk):
stdout.append(chunk)
p = python("-u", py.name, _out=agg, _out_bufsize=4)
p.wait()
self.assertEqual(len(stdout), len("herp") / 2 * 5)
def test_stdout_callback_with_input(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(5): print(str(i))
derp = input("herp? ")
print(derp)
"""
)
def agg(line, stdin):
if line.strip() == "4":
stdin.put("derp\n")
p = python("-u", py.name, _out=agg, _tee=True)
p.wait()
self.assertIn("derp", p)
def test_stdout_callback_exit(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(5): print(i)
"""
)
stdout = []
def agg(line):
line = line.strip()
stdout.append(line)
if line == "2":
return True
p = python("-u", py.name, _out=agg, _tee=True)
p.wait()
self.assertIn("4", p)
self.assertNotIn("4", stdout)
def test_stdout_callback_terminate(self):
import signal
py = create_tmp_test(
"""
import sys
import os
import time
for i in range(5):
print(i)
time.sleep(.5)
"""
)
stdout = []
def agg(line, stdin, process):
line = line.strip()
stdout.append(line)
if line == "3":
process.terminate()
return True
import sh
caught_signal = False
try:
p = python("-u", py.name, _out=agg, _bg=True)
p.wait()
except sh.SignalException_SIGTERM:
caught_signal = True
self.assertTrue(caught_signal)
self.assertEqual(p.process.exit_code, -signal.SIGTERM)
self.assertNotIn("4", p)
self.assertNotIn("4", stdout)
def test_stdout_callback_kill(self):
import signal
py = create_tmp_test(
"""
import sys
import os
import time
for i in range(5):
print(i)
time.sleep(.5)
"""
)
stdout = []
def agg(line, stdin, process):
line = line.strip()
stdout.append(line)
if line == "3":
process.kill()
return True
import sh
caught_signal = False
try:
p = python("-u", py.name, _out=agg, _bg=True)
p.wait()
except sh.SignalException_SIGKILL:
caught_signal = True
self.assertTrue(caught_signal)
self.assertEqual(p.process.exit_code, -signal.SIGKILL)
self.assertNotIn("4", p)
self.assertNotIn("4", stdout)
def test_general_signal(self):
from signal import SIGINT
py = create_tmp_test(
"""
import sys
import os
import time
import signal
i = 0
def sig_handler(sig, frame):
global i
i = 42
signal.signal(signal.SIGINT, sig_handler)
for _ in range(6):
print(i)
i += 1
sys.stdout.flush()
time.sleep(2)
"""
)
stdout = []
def agg(line, stdin, process):
line = line.strip()
stdout.append(line)
if line == "3":
process.signal(SIGINT)
return True
p = python(py.name, _out=agg, _tee=True)
p.wait()
self.assertEqual(p.process.exit_code, 0)
self.assertEqual(str(p), "0\n1\n2\n3\n42\n43\n")
def test_iter_generator(self):
py = create_tmp_test(
"""
import sys
import os
import time
for i in range(42):
print(i)
sys.stdout.flush()
"""
)
out = []
for line in python(py.name, _iter=True):
out.append(int(line.strip()))
self.assertEqual(len(out), 42)
self.assertEqual(sum(out), 861)
def test_async(self):
py = create_tmp_test(
"""
import os
import time
time.sleep(0.5)
print("hello")
"""
)
alternating = []
async def producer(q):
alternating.append(1)
msg = await python(py.name, _async=True)
alternating.append(1)
await q.put(msg.strip())
async def consumer(q):
await asyncio.sleep(0.1)
alternating.append(2)
msg = await q.get()
self.assertEqual(msg, "hello")
alternating.append(2)
async def main():
q = AQueue()
await asyncio.gather(producer(q), consumer(q))
asyncio.run(main())
self.assertListEqual(alternating, [1, 2, 1, 2])
def test_async_exc(self):
py = create_tmp_test("""exit(34)""")
async def producer():
await python(py.name, _async=True, _return_cmd=False)
self.assertRaises(sh.ErrorReturnCode_34, asyncio.run, producer())
def test_async_iter(self):
py = create_tmp_test(
"""
for i in range(5):
print(i)
"""
)
# this list will prove that our coroutines are yielding to eachother as each
# line is produced
alternating = []
async def producer(q):
async for line in python(py.name, _iter=True):
alternating.append(1)
await q.put(int(line.strip()))
await q.put(None)
async def consumer(q):
while True:
line = await q.get()
if line is None:
return
alternating.append(2)
async def main():
q = AQueue()
await asyncio.gather(producer(q), consumer(q))
asyncio.run(main())
self.assertListEqual(alternating, [1, 2, 1, 2, 1, 2, 1, 2, 1, 2])
def test_async_iter_exc(self):
py = create_tmp_test(
"""
for i in range(5):
print(i)
exit(34)
"""
)
lines = []
async def producer():
async for line in python(py.name, _async=True):
lines.append(int(line.strip()))
self.assertRaises(sh.ErrorReturnCode_34, asyncio.run, producer())
def test_async_return_cmd(self):
py = create_tmp_test(
"""
import sys
sys.exit(0)
"""
)
async def main():
result = await python(py.name, _async=True, _return_cmd=True)
self.assertIsInstance(result, sh.RunningCommand)
result_str = await python(py.name, _async=True, _return_cmd=False)
self.assertIsInstance(result_str, str)
asyncio.run(main())
def test_async_return_cmd_exc(self):
py = create_tmp_test(
"""
import sys
sys.exit(1)
"""
)
async def main():
await python(py.name, _async=True, _return_cmd=True)
self.assertRaises(sh.ErrorReturnCode_1, asyncio.run, main())
def test_handle_both_out_and_err(self):
py = create_tmp_test(
"""
import sys
import os
import time
for i in range(42):
sys.stdout.write(str(i) + "\\n")
sys.stdout.flush()
if i % 2 == 0:
sys.stderr.write(str(i) + "\\n")
sys.stderr.flush()
"""
)
out = []
def handle_out(line):
out.append(int(line.strip()))
err = []
def handle_err(line):
err.append(int(line.strip()))
p = python(py.name, _err=handle_err, _out=handle_out, _bg=True)
p.wait()
self.assertEqual(sum(out), 861)
self.assertEqual(sum(err), 420)
def test_iter_unicode(self):
# issue https://github.com/amoffat/sh/issues/224
test_string = "\xe4\xbd\x95\xe4\xbd\x95\n" * 150 # len > buffer_s
txt = create_tmp_test(test_string)
for line in sh.cat(txt.name, _iter=True):
break
self.assertLess(len(line), 1024)
def test_nonblocking_iter(self):
from errno import EWOULDBLOCK
py = create_tmp_test(
"""
import time
import sys
time.sleep(1)
sys.stdout.write("stdout")
"""
)
count = 0
value = None
for line in python(py.name, _iter_noblock=True):
if line == EWOULDBLOCK:
count += 1
else:
value = line
self.assertGreater(count, 0)
self.assertEqual(value, "stdout")
py = create_tmp_test(
"""
import time
import sys
time.sleep(1)
sys.stderr.write("stderr")
"""
)
count = 0
value = None
for line in python(py.name, _iter_noblock="err"):
if line == EWOULDBLOCK:
count += 1
else:
value = line
self.assertGreater(count, 0)
self.assertEqual(value, "stderr")
def test_for_generator_to_err(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(42):
sys.stderr.write(str(i)+"\\n")
"""
)
out = []
for line in python("-u", py.name, _iter="err"):
out.append(line)
self.assertEqual(len(out), 42)
# verify that nothing is going to stdout
out = []
for line in python("-u", py.name, _iter="out"):
out.append(line)
self.assertEqual(len(out), 0)
def test_sigpipe(self):
py1 = create_tmp_test(
"""
import sys
import os
import time
import signal
# by default, python disables SIGPIPE, in favor of using IOError exceptions, so
# let's put that back to the system default where we terminate with a signal
# exit code
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
for letter in "andrew":
time.sleep(0.6)
print(letter)
"""
)
py2 = create_tmp_test(
"""
import sys
import os
import time
while True:
line = sys.stdin.readline()
if not line:
break
print(line.strip().upper())
exit(0)
"""
)
p1 = python("-u", py1.name, _piped="out")
p2 = python(
"-u",
py2.name,
_in=p1,
)
# SIGPIPE should happen, but it shouldn't be an error, since _piped is
# truthful
self.assertEqual(-p1.exit_code, signal.SIGPIPE)
self.assertEqual(p2.exit_code, 0)
def test_piped_generator(self):
import time
py1 = create_tmp_test(
"""
import sys
import os
import time
for letter in "andrew":
time.sleep(0.6)
print(letter)
"""
)
py2 = create_tmp_test(
"""
import sys
import os
import time
while True:
line = sys.stdin.readline()
if not line:
break
print(line.strip().upper())
"""
)
times = []
last_received = None
letters = ""
for line in python(
"-u", py2.name, _iter=True, _in=python("-u", py1.name, _piped="out")
):
letters += line.strip()
now = time.time()
if last_received:
times.append(now - last_received)
last_received = now
self.assertEqual("ANDREW", letters)
self.assertTrue(all([t > 0.3 for t in times]))
def test_no_out_iter_err(self):
py = create_tmp_test(
"""
import sys
sys.stderr.write("1\\n")
sys.stderr.write("2\\n")
sys.stderr.write("3\\n")
sys.stderr.flush()
"""
)
nums = [int(num.strip()) for num in python(py.name, _iter="err", _no_out=True)]
assert nums == [1, 2, 3]
def test_generator_and_callback(self):
py = create_tmp_test(
"""
import sys
import os
for i in range(42):
sys.stderr.write(str(i * 2)+"\\n")
print(i)
"""
)
stderr = []
def agg(line):
stderr.append(int(line.strip()))
out = []
for line in python("-u", py.name, _iter=True, _err=agg):
out.append(line)
self.assertEqual(len(out), 42)
self.assertEqual(sum(stderr), 1722)
def test_cast_bg(self):
py = create_tmp_test(
"""
import sys
import time
time.sleep(0.5)
sys.stdout.write(sys.argv[1])
"""
)
self.assertEqual(int(python(py.name, "123", _bg=True)), 123)
self.assertEqual(float(python(py.name, "789", _bg=True)), 789.0)
def test_cmd_eq(self):
py = create_tmp_test("")
cmd1 = python.bake(py.name, "-u")
cmd2 = python.bake(py.name, "-u")
cmd3 = python.bake(py.name)
self.assertEqual(cmd1, cmd2)
self.assertNotEqual(cmd1, cmd3)
def test_fg(self):
py = create_tmp_test("exit(0)")
# notice we're using `system_python`, and not `python`. this is because
# `python` has an env baked into it, and we want `_env` to be None for
# coverage
system_python(py.name, _fg=True)
def test_fg_false(self):
"""https://github.com/amoffat/sh/issues/520"""
py = create_tmp_test("print('hello')")
buf = StringIO()
python(py.name, _fg=False, _out=buf)
self.assertEqual(buf.getvalue(), "hello\n")
def test_fg_true(self):
"""https://github.com/amoffat/sh/issues/520"""
py = create_tmp_test("print('hello')")
buf = StringIO()
self.assertRaises(TypeError, python, py.name, _fg=True, _out=buf)
def test_fg_env(self):
py = create_tmp_test(
"""
import os
code = int(os.environ.get("EXIT", "0"))
exit(code)
"""
)
env = os.environ.copy()
env["EXIT"] = "3"
self.assertRaises(sh.ErrorReturnCode_3, python, py.name, _fg=True, _env=env)
def test_fg_alternative(self):
py = create_tmp_test("exit(0)")
python(py.name, _in=sys.stdin, _out=sys.stdout, _err=sys.stderr)
def test_fg_exc(self):
py = create_tmp_test("exit(1)")
self.assertRaises(sh.ErrorReturnCode_1, python, py.name, _fg=True)
def test_out_filename(self):
outfile = tempfile.NamedTemporaryFile()
py = create_tmp_test("print('output')")
python(py.name, _out=outfile.name)
outfile.seek(0)
self.assertEqual(b"output\n", outfile.read())
def test_out_pathlike(self):
from pathlib import Path
outfile = tempfile.NamedTemporaryFile()
py = create_tmp_test("print('output')")
python(py.name, _out=Path(outfile.name))
outfile.seek(0)
self.assertEqual(b"output\n", outfile.read())
def test_bg_exit_code(self):
py = create_tmp_test(
"""
import time
time.sleep(1)
exit(49)
"""
)
p = python(py.name, _ok_code=49, _bg=True)
self.assertEqual(49, p.exit_code)
def test_cwd(self):
from os.path import realpath
from sh import pwd
self.assertEqual(str(pwd(_cwd="/tmp")), realpath("/tmp") + "\n")
self.assertEqual(str(pwd(_cwd="/etc")), realpath("/etc") + "\n")
def test_cwd_fg(self):
td = realpath(tempfile.mkdtemp())
py = create_tmp_test(
f"""
import sh
import os
from os.path import realpath
orig = realpath(os.getcwd())
print(orig)
sh.pwd(_cwd="{td}", _fg=True)
print(realpath(os.getcwd()))
"""
)
orig, newdir, restored = python(py.name).strip().split("\n")
newdir = realpath(newdir)
self.assertEqual(newdir, td)
self.assertEqual(orig, restored)
self.assertNotEqual(orig, newdir)
os.rmdir(td)
def test_huge_piped_data(self):
from sh import tr
stdin = tempfile.NamedTemporaryFile()
data = "herpderp" * 4000 + "\n"
stdin.write(data.encode())
stdin.flush()
stdin.seek(0)
out = tr("[:upper:]", "[:lower:]", _in=tr("[:lower:]", "[:upper:]", _in=data))
self.assertTrue(out == data)
def test_tty_input(self):
py = create_tmp_test(
"""
import sys
import os
if os.isatty(sys.stdin.fileno()):
sys.stdout.write("password?\\n")
sys.stdout.flush()
pw = sys.stdin.readline().strip()
sys.stdout.write("%s\\n" % ("*" * len(pw)))
sys.stdout.flush()
else:
sys.stdout.write("no tty attached!\\n")
sys.stdout.flush()
"""
)
test_pw = "test123"
expected_stars = "*" * len(test_pw)
d = {}
def password_enterer(line, stdin):
line = line.strip()
if not line:
return
if line == "password?":
stdin.put(test_pw + "\n")
elif line.startswith("*"):
d["stars"] = line
return True
pw_stars = python(py.name, _tty_in=True, _out=password_enterer)
pw_stars.wait()
self.assertEqual(d["stars"], expected_stars)
response = python(py.name)
self.assertEqual(str(response), "no tty attached!\n")
def test_tty_output(self):
py = create_tmp_test(
"""
import sys
import os
if os.isatty(sys.stdout.fileno()):
sys.stdout.write("tty attached")
sys.stdout.flush()
else:
sys.stdout.write("no tty attached")
sys.stdout.flush()
"""
)
out = pythons(py.name, _tty_out=True)
self.assertEqual(out, "tty attached")
out = pythons(py.name, _tty_out=False)
self.assertEqual(out, "no tty attached")
def test_stringio_output(self):
import sh
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.argv[1])
"""
)
out = StringIO()
sh.python(py.name, "testing 123", _out=out)
self.assertEqual(out.getvalue(), "testing 123")
out = BytesIO()
sh.python(py.name, "testing 123", _out=out)
self.assertEqual(out.getvalue().decode(), "testing 123")
def test_stringio_input(self):
from sh import cat
input = StringIO()
input.write("herpderp")
input.seek(0)
out = cat(_in=input)
self.assertEqual(out, "herpderp")
def test_internal_bufsize(self):
from sh import cat
output = cat(_in="a" * 1000, _internal_bufsize=100, _out_bufsize=0)
self.assertEqual(len(output), 100)
output = cat(_in="a" * 1000, _internal_bufsize=50, _out_bufsize=2)
self.assertEqual(len(output), 100)
def test_change_stdout_buffering(self):
py = create_tmp_test(
"""
import sys
import os
# this proves that we won't get the output into our callback until we send
# a newline
sys.stdout.write("switch ")
sys.stdout.flush()
sys.stdout.write("buffering\\n")
sys.stdout.flush()
sys.stdin.read(1)
sys.stdout.write("unbuffered")
sys.stdout.flush()
# this is to keep the output from being flushed by the process ending, which
# would ruin our test. we want to make sure we get the string "unbuffered"
# before the process ends, without writing a newline
sys.stdin.read(1)
"""
)
d = {
"newline_buffer_success": False,
"unbuffered_success": False,
}
def interact(line, stdin, process):
line = line.strip()
if not line:
return
if line == "switch buffering":
d["newline_buffer_success"] = True
process.change_out_bufsize(0)
stdin.put("a")
elif line == "unbuffered":
stdin.put("b")
d["unbuffered_success"] = True
return True
# start with line buffered stdout
pw_stars = python("-u", py.name, _out=interact, _out_bufsize=1)
pw_stars.wait()
self.assertTrue(d["newline_buffer_success"])
self.assertTrue(d["unbuffered_success"])
def test_callable_interact(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write("line1")
"""
)
class Callable:
def __init__(self):
self.line = None
def __call__(self, line):
self.line = line
cb = Callable()
python(py.name, _out=cb)
self.assertEqual(cb.line, "line1")
def test_encoding(self):
return self.skipTest(
"what's the best way to test a different '_encoding' special keyword"
"argument?"
)
def test_timeout(self):
from time import time
import sh
sleep_for = 3
timeout = 1
started = time()
try:
sh.sleep(sleep_for, _timeout=timeout).wait()
except sh.TimeoutException as e:
assert "sleep 3" in e.full_cmd
else:
self.fail("no timeout exception")
elapsed = time() - started
self.assertLess(abs(elapsed - timeout), 0.5)
def test_timeout_overstep(self):
started = time.time()
sh.sleep(1, _timeout=5)
elapsed = time.time() - started
self.assertLess(abs(elapsed - 1), 0.5)
def test_timeout_wait(self):
p = sh.sleep(3, _bg=True)
self.assertRaises(sh.TimeoutException, p.wait, timeout=1)
def test_timeout_wait_overstep(self):
p = sh.sleep(1, _bg=True)
p.wait(timeout=5)
def test_timeout_wait_negative(self):
p = sh.sleep(3, _bg=True)
self.assertRaises(RuntimeError, p.wait, timeout=-3)
def test_binary_pipe(self):
binary = b"\xec;\xedr\xdbF"
py1 = create_tmp_test(
"""
import sys
import os
sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0)
sys.stdout.write(b'\\xec;\\xedr\\xdbF')
"""
)
py2 = create_tmp_test(
"""
import sys
import os
sys.stdin = os.fdopen(sys.stdin.fileno(), "rb", 0)
sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0)
sys.stdout.write(sys.stdin.read())
"""
)
out = python(py2.name, _in=python(py1.name))
self.assertEqual(out.stdout, binary)
# designed to trigger the "... (%d more, please see e.stdout)" output
# of the ErrorReturnCode class
def test_failure_with_large_output(self):
from sh import ErrorReturnCode_1
py = create_tmp_test(
"""
print("andrewmoffat" * 1000)
exit(1)
"""
)
self.assertRaises(ErrorReturnCode_1, python, py.name)
# designed to check if the ErrorReturnCode constructor does not raise
# an UnicodeDecodeError
def test_non_ascii_error(self):
from sh import ErrorReturnCode, ls
test = "/á"
self.assertRaises(ErrorReturnCode, ls, test, _encoding="utf8")
def test_no_out(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
p = python(py.name, _no_out=True)
self.assertEqual(p.stdout, b"")
self.assertEqual(p.stderr, b"stderr")
self.assertTrue(p.process._pipe_queue.empty())
def callback(line):
pass
p = python(py.name, _out=callback)
self.assertEqual(p.stdout, b"")
self.assertEqual(p.stderr, b"stderr")
self.assertTrue(p.process._pipe_queue.empty())
p = python(py.name)
self.assertEqual(p.stdout, b"stdout")
self.assertEqual(p.stderr, b"stderr")
self.assertFalse(p.process._pipe_queue.empty())
def test_tty_stdin(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.stdin.read())
sys.stdout.flush()
"""
)
out = pythons(py.name, _in="test\n", _tty_in=True)
self.assertEqual("test\n", out)
def test_no_err(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write("stdout")
sys.stderr.write("stderr")
"""
)
p = python(py.name, _no_err=True)
self.assertEqual(p.stderr, b"")
self.assertEqual(p.stdout, b"stdout")
self.assertFalse(p.process._pipe_queue.empty())
def callback(line):
pass
p = python(py.name, _err=callback)
self.assertEqual(p.stderr, b"")
self.assertEqual(p.stdout, b"stdout")
self.assertFalse(p.process._pipe_queue.empty())
p = python(py.name)
self.assertEqual(p.stderr, b"stderr")
self.assertEqual(p.stdout, b"stdout")
self.assertFalse(p.process._pipe_queue.empty())
def test_no_pipe(self):
from sh import ls
# calling a command regular should fill up the pipe_queue
p = ls(_return_cmd=True)
self.assertFalse(p.process._pipe_queue.empty())
# calling a command with a callback should not
def callback(line):
pass
p = ls(_out=callback, _return_cmd=True)
self.assertTrue(p.process._pipe_queue.empty())
# calling a command regular with no_pipe also should not
p = ls(_no_pipe=True, _return_cmd=True)
self.assertTrue(p.process._pipe_queue.empty())
def test_decode_error_handling(self):
from functools import partial
py = create_tmp_test(
"""
# -*- coding: utf8 -*-
import sys
import os
sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb')
sys.stdout.write(bytes("te漢字st", "utf8") + "äåéë".encode("latin_1"))
"""
)
fn = partial(pythons, py.name, _encoding="ascii")
self.assertRaises(UnicodeDecodeError, fn)
p = pythons(py.name, _encoding="ascii", _decode_errors="ignore")
self.assertEqual(p, "test")
p = pythons(
py.name,
_encoding="ascii",
_decode_errors="ignore",
_out=sys.stdout,
_tee=True,
)
self.assertEqual(p, "test")
def test_signal_exception(self):
from sh import SignalException_15
def throw_terminate_signal():
py = create_tmp_test(
"""
import time
while True: time.sleep(1)
"""
)
to_kill = python(py.name, _bg=True)
to_kill.terminate()
to_kill.wait()
self.assertRaises(SignalException_15, throw_terminate_signal)
def test_signal_group(self):
child = create_tmp_test(
"""
import time
time.sleep(3)
"""
)
parent = create_tmp_test(
"""
import sys
import sh
python = sh.Command(sys.executable)
p = python("{child_file}", _bg=True, _new_session=False)
print(p.pid)
print(p.process.pgid)
p.wait()
""",
child_file=child.name,
)
def launch():
p = python(parent.name, _bg=True, _iter=True, _new_group=True)
child_pid = int(next(p).strip())
child_pgid = int(next(p).strip())
parent_pid = p.pid
parent_pgid = p.process.pgid
return p, child_pid, child_pgid, parent_pid, parent_pgid
def assert_alive(pid):
os.kill(pid, 0)
def assert_dead(pid):
self.assert_oserror(errno.ESRCH, os.kill, pid, 0)
# first let's prove that calling regular SIGKILL on the parent does
# nothing to the child, since the child was launched in the same process
# group (_new_session=False) and the parent is not a controlling process
p, child_pid, child_pgid, parent_pid, parent_pgid = launch()
assert_alive(parent_pid)
assert_alive(child_pid)
p.kill()
time.sleep(0.1)
assert_dead(parent_pid)
assert_alive(child_pid)
self.assertRaises(sh.SignalException_SIGKILL, p.wait)
assert_dead(child_pid)
# now let's prove that killing the process group kills both the parent
# and the child
p, child_pid, child_pgid, parent_pid, parent_pgid = launch()
assert_alive(parent_pid)
assert_alive(child_pid)
p.kill_group()
time.sleep(0.1)
assert_dead(parent_pid)
assert_dead(child_pid)
def test_pushd(self):
"""test basic pushd functionality"""
child = realpath(tempfile.mkdtemp())
old_wd1 = sh.pwd().strip()
old_wd2 = os.getcwd()
self.assertEqual(old_wd1, old_wd2)
self.assertNotEqual(old_wd1, child)
with sh.pushd(child):
new_wd1 = sh.pwd().strip()
new_wd2 = os.getcwd()
old_wd3 = sh.pwd().strip()
old_wd4 = os.getcwd()
self.assertEqual(old_wd3, old_wd4)
self.assertEqual(old_wd1, old_wd3)
self.assertEqual(new_wd1, child)
self.assertEqual(new_wd2, child)
def test_pushd_cd(self):
"""test that pushd works like pushd/popd"""
child = realpath(tempfile.mkdtemp())
try:
old_wd = os.getcwd()
with sh.pushd(tempdir):
self.assertEqual(str(tempdir), os.getcwd())
self.assertEqual(old_wd, os.getcwd())
finally:
os.rmdir(child)
def test_non_existant_cwd(self):
from sh import ls
# sanity check
non_exist_dir = join(tempdir, "aowjgoahewro")
self.assertFalse(exists(non_exist_dir))
self.assertRaises(sh.ForkException, ls, _cwd=non_exist_dir)
# https://github.com/amoffat/sh/issues/176
def test_baked_command_can_be_printed(self):
from sh import ls
ll = ls.bake("-l")
self.assertTrue(str(ll).endswith("/ls -l"))
# https://github.com/amoffat/sh/issues/185
def test_done_callback(self):
import time
class Callback:
def __init__(self):
self.called = False
self.exit_code = None
self.success = None
def __call__(self, p, success, exit_code):
self.called = True
self.exit_code = exit_code
self.success = success
py = create_tmp_test(
"""
from time import time, sleep
sleep(1)
print(time())
"""
)
callback = Callback()
p = python(py.name, _done=callback, _bg=True)
# do a little setup to prove that a command with a _done callback is run
# in the background
wait_start = time.time()
p.wait()
wait_elapsed = time.time() - wait_start
self.assertTrue(callback.called)
self.assertLess(abs(wait_elapsed - 1.0), 1.0)
self.assertEqual(callback.exit_code, 0)
self.assertTrue(callback.success)
# https://github.com/amoffat/sh/issues/564
def test_done_callback_no_deadlock(self):
import time
py = create_tmp_test(
"""
from sh import sleep
def done(cmd, success, exit_code):
print(cmd, success, exit_code)
sleep('1', _done=done)
"""
)
p = python(py.name, _bg=True, _timeout=2)
# do a little setup to prove that a command with a _done callback is run
# in the background
wait_start = time.time()
p.wait()
wait_elapsed = time.time() - wait_start
self.assertLess(abs(wait_elapsed - 1.0), 1.0)
def test_fork_exc(self):
from sh import ForkException
py = create_tmp_test("")
def fail():
raise RuntimeError("nooo")
self.assertRaises(ForkException, python, py.name, _preexec_fn=fail)
def test_new_session_new_group(self):
from threading import Event
py = create_tmp_test(
"""
import os
import time
pid = os.getpid()
pgid = os.getpgid(pid)
sid = os.getsid(pid)
stuff = [pid, pgid, sid]
print(",".join([str(el) for el in stuff]))
time.sleep(0.5)
"""
)
event = Event()
def handle(run_asserts, line, stdin, p):
pid, pgid, sid = line.strip().split(",")
pid = int(pid)
pgid = int(pgid)
sid = int(sid)
test_pid = os.getpgid(os.getpid())
self.assertEqual(p.pid, pid)
self.assertEqual(p.pgid, pgid)
self.assertEqual(pgid, p.get_pgid())
self.assertEqual(p.sid, sid)
self.assertEqual(sid, p.get_sid())
run_asserts(pid, pgid, sid, test_pid)
event.set()
def session_true_group_false(pid, pgid, sid, test_pid):
self.assertEqual(pid, sid)
self.assertEqual(pid, pgid)
p = python(
py.name, _out=partial(handle, session_true_group_false), _new_session=True
)
p.wait()
self.assertTrue(event.is_set())
event.clear()
def session_false_group_false(pid, pgid, sid, test_pid):
self.assertEqual(test_pid, pgid)
self.assertNotEqual(pid, sid)
p = python(
py.name, _out=partial(handle, session_false_group_false), _new_session=False
)
p.wait()
self.assertTrue(event.is_set())
event.clear()
def session_false_group_true(pid, pgid, sid, test_pid):
self.assertEqual(pid, pgid)
self.assertNotEqual(pid, sid)
p = python(
py.name,
_out=partial(handle, session_false_group_true),
_new_session=False,
_new_group=True,
)
p.wait()
self.assertTrue(event.is_set())
event.clear()
def test_done_cb_exc(self):
from sh import ErrorReturnCode
class Callback:
def __init__(self):
self.called = False
self.success = None
def __call__(self, p, success, exit_code):
self.success = success
self.called = True
py = create_tmp_test("exit(1)")
callback = Callback()
try:
p = python(py.name, _done=callback, _bg=True)
p.wait()
except ErrorReturnCode:
self.assertTrue(callback.called)
self.assertFalse(callback.success)
else:
self.fail("command should've thrown an exception")
def test_callable_stdin(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.stdin.read())
"""
)
def create_stdin():
state = {"count": 0}
def stdin():
count = state["count"]
if count == 4:
return None
state["count"] += 1
return str(count)
return stdin
out = pythons(py.name, _in=create_stdin())
self.assertEqual("0123", out)
def test_stdin_unbuffered_bufsize(self):
from time import sleep
# this tries to receive some known data and measures the time it takes
# to receive it. since we're flushing by newline, we should only be
# able to receive the data when a newline is fed in
py = create_tmp_test(
"""
import sys
from time import time
started = time()
data = sys.stdin.read(len("testing"))
waited = time() - started
sys.stdout.write(data + "\\n")
sys.stdout.write(str(waited) + "\\n")
started = time()
data = sys.stdin.read(len("done"))
waited = time() - started
sys.stdout.write(data + "\\n")
sys.stdout.write(str(waited) + "\\n")
sys.stdout.flush()
"""
)
def create_stdin():
yield "test"
sleep(1)
yield "ing"
sleep(1)
yield "done"
out = python(py.name, _in=create_stdin(), _in_bufsize=0)
word1, time1, word2, time2, _ = out.split("\n")
time1 = float(time1)
time2 = float(time2)
self.assertEqual(word1, "testing")
self.assertLess(abs(1 - time1), 0.5)
self.assertEqual(word2, "done")
self.assertLess(abs(1 - time2), 0.5)
def test_stdin_newline_bufsize(self):
from time import sleep
# this tries to receive some known data and measures the time it takes
# to receive it. since we're flushing by newline, we should only be
# able to receive the data when a newline is fed in
py = create_tmp_test(
"""
import sys
from time import time
started = time()
data = sys.stdin.read(len("testing\\n"))
waited = time() - started
sys.stdout.write(data)
sys.stdout.write(str(waited) + "\\n")
started = time()
data = sys.stdin.read(len("done\\n"))
waited = time() - started
sys.stdout.write(data)
sys.stdout.write(str(waited) + "\\n")
sys.stdout.flush()
"""
)
# we'll feed in text incrementally, sleeping strategically before
# sending a newline. we then measure the amount that we slept
# indirectly in the child process
def create_stdin():
yield "test"
sleep(1)
yield "ing\n"
sleep(1)
yield "done\n"
out = python(py.name, _in=create_stdin(), _in_bufsize=1)
word1, time1, word2, time2, _ = out.split("\n")
time1 = float(time1)
time2 = float(time2)
self.assertEqual(word1, "testing")
self.assertLess(abs(1 - time1), 0.5)
self.assertEqual(word2, "done")
self.assertLess(abs(1 - time2), 0.5)
def test_custom_timeout_signal(self):
import signal
from sh import TimeoutException
py = create_tmp_test(
"""
import time
time.sleep(3)
"""
)
try:
python(py.name, _timeout=1, _timeout_signal=signal.SIGHUP)
except TimeoutException as e:
self.assertEqual(e.exit_code, signal.SIGHUP)
else:
self.fail("we should have handled a TimeoutException")
def test_append_stdout(self):
py = create_tmp_test(
"""
import sys
num = sys.stdin.read()
sys.stdout.write(num)
"""
)
append_file = tempfile.NamedTemporaryFile(mode="a+b")
python(py.name, _in="1", _out=append_file)
python(py.name, _in="2", _out=append_file)
append_file.seek(0)
output = append_file.read()
self.assertEqual(b"12", output)
def test_shadowed_subcommand(self):
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.argv[1])
"""
)
out = pythons.bake(py.name).bake_()
self.assertEqual("bake", out)
def test_no_proc_no_attr(self):
py = create_tmp_test("")
with python(py.name) as p:
self.assertRaises(AttributeError, getattr, p, "exit_code")
def test_partially_applied_callback(self):
from functools import partial
py = create_tmp_test(
"""
for i in range(10):
print(i)
"""
)
output = []
def fn(foo, line):
output.append((foo, int(line.strip())))
log_line = partial(fn, "hello")
python(py.name, _out=log_line)
self.assertEqual(output, [("hello", i) for i in range(10)])
output = []
def fn(foo, line, stdin, proc):
output.append((foo, int(line.strip())))
log_line = partial(fn, "hello")
python(py.name, _out=log_line)
self.assertEqual(output, [("hello", i) for i in range(10)])
# https://github.com/amoffat/sh/issues/266
def test_grandchild_no_sighup(self):
import time
# child process that will write to a file if it receives a SIGHUP
child = create_tmp_test(
"""
import signal
import sys
import time
output_file = sys.argv[1]
with open(output_file, "w") as f:
def handle_sighup(signum, frame):
f.write("got signal %d" % signum)
sys.exit(signum)
signal.signal(signal.SIGHUP, handle_sighup)
time.sleep(2)
f.write("made it!\\n")
"""
)
# the parent that will terminate before the child writes to the output
# file, potentially causing a SIGHUP
parent = create_tmp_test(
"""
import os
import time
import sys
child_file = sys.argv[1]
output_file = sys.argv[2]
python_name = os.path.basename(sys.executable)
os.spawnlp(os.P_NOWAIT, python_name, python_name, child_file, output_file)
time.sleep(1) # give child a chance to set up
"""
)
output_file = tempfile.NamedTemporaryFile(delete=True)
python(parent.name, child.name, output_file.name)
time.sleep(3)
out = output_file.readlines()[0]
self.assertEqual(out, b"made it!\n")
def test_unchecked_producer_failure(self):
from sh import ErrorReturnCode_2
producer = create_tmp_test(
"""
import sys
for i in range(10):
print(i)
sys.exit(2)
"""
)
consumer = create_tmp_test(
"""
import sys
for line in sys.stdin:
pass
"""
)
direct_pipe = python(producer.name, _piped=True)
self.assertRaises(ErrorReturnCode_2, python, direct_pipe, consumer.name)
def test_unchecked_pipeline_failure(self):
# similar to test_unchecked_producer_failure, but this
# tests a multi-stage pipeline
from sh import ErrorReturnCode_2
producer = create_tmp_test(
"""
import sys
for i in range(10):
print(i)
sys.exit(2)
"""
)
middleman = create_tmp_test(
"""
import sys
for line in sys.stdin:
print("> " + line)
"""
)
consumer = create_tmp_test(
"""
import sys
for line in sys.stdin:
pass
"""
)
producer_normal_pipe = python(producer.name, _piped=True)
middleman_normal_pipe = python(
middleman.name, _piped=True, _in=producer_normal_pipe
)
self.assertRaises(
ErrorReturnCode_2, python, middleman_normal_pipe, consumer.name
)
class MockTests(BaseTests):
def test_patch_command_cls(self):
def fn():
cmd = sh.Command("afowejfow")
return cmd()
@unittest.mock.patch("sh.Command")
def test(Command):
Command().return_value = "some output"
return fn()
self.assertEqual(test(), "some output")
self.assertRaises(sh.CommandNotFound, fn)
def test_patch_command(self):
def fn():
return sh.afowejfow()
@unittest.mock.patch("sh.afowejfow", create=True)
def test(cmd):
cmd.return_value = "some output"
return fn()
self.assertEqual(test(), "some output")
self.assertRaises(sh.CommandNotFound, fn)
class MiscTests(BaseTests):
def test_pickling(self):
import pickle
py = create_tmp_test(
"""
import sys
sys.stdout.write("some output")
sys.stderr.write("some error")
exit(1)
"""
)
try:
python(py.name)
except sh.ErrorReturnCode as e:
restored = pickle.loads(pickle.dumps(e))
self.assertEqual(restored.stdout, b"some output")
self.assertEqual(restored.stderr, b"some error")
self.assertEqual(restored.exit_code, 1)
else:
self.fail("Didn't get an exception")
@requires_poller("poll")
def test_fd_over_1024(self):
py = create_tmp_test("""print("hi world")""")
with ulimit(resource.RLIMIT_NOFILE, 2048):
cutoff_fd = 1024
pipes = []
for i in range(cutoff_fd):
master, slave = os.pipe()
pipes.append((master, slave))
if slave >= cutoff_fd:
break
python(py.name)
for master, slave in pipes:
os.close(master)
os.close(slave)
def test_args_deprecated(self):
self.assertRaises(DeprecationWarning, sh.args, _env={})
def test_percent_doesnt_fail_logging(self):
"""test that a command name doesn't interfere with string formatting in
the internal loggers"""
py = create_tmp_test(
"""
print("cool")
"""
)
python(py.name, "%")
python(py.name, "%%")
python(py.name, "%%%")
def test_pushd_thread_safety(self):
import threading
import time
temp1 = realpath(tempfile.mkdtemp())
temp2 = realpath(tempfile.mkdtemp())
try:
results = [None, None]
def fn1():
with sh.pushd(temp1):
time.sleep(0.2)
results[0] = realpath(os.getcwd())
def fn2():
time.sleep(0.1)
with sh.pushd(temp2):
results[1] = realpath(os.getcwd())
time.sleep(0.3)
t1 = threading.Thread(name="t1", target=fn1)
t2 = threading.Thread(name="t2", target=fn2)
t1.start()
t2.start()
t1.join()
t2.join()
self.assertEqual(results, [temp1, temp2])
finally:
os.rmdir(temp1)
os.rmdir(temp2)
def test_stdin_nohang(self):
py = create_tmp_test(
"""
print("hi")
"""
)
read, write = os.pipe()
stdin = os.fdopen(read, "r")
python(py.name, _in=stdin)
@requires_utf8
def test_unicode_path(self):
from sh import Command
python_name = os.path.basename(sys.executable)
py = create_tmp_test(
f"""#!/usr/bin/env {python_name}
# -*- coding: utf8 -*-
print("字")
""",
prefix="字",
delete=False,
)
try:
py.close()
os.chmod(py.name, int(0o755))
cmd = Command(py.name)
# all of these should behave just fine
str(cmd)
repr(cmd)
running = cmd(_return_cmd=True)
str(running)
repr(running)
str(running.process)
repr(running.process)
finally:
os.unlink(py.name)
# https://github.com/amoffat/sh/issues/121
def test_wraps(self):
from sh import ls
wraps(ls)(lambda f: True)
def test_signal_exception_aliases(self):
"""proves that signal exceptions with numbers and names are equivalent"""
import signal
import sh
sig_name = f"SignalException_{signal.SIGQUIT}"
sig = getattr(sh, sig_name)
from sh import SignalException_SIGQUIT
self.assertEqual(sig, SignalException_SIGQUIT)
def test_change_log_message(self):
py = create_tmp_test(
"""
print("cool")
"""
)
def log_msg(cmd, call_args, pid=None):
return "Hi! I ran something"
buf = StringIO()
handler = logging.StreamHandler(buf)
logger = logging.getLogger("sh")
logger.setLevel(logging.INFO)
try:
logger.addHandler(handler)
python(py.name, "meow", "bark", _log_msg=log_msg)
finally:
logger.removeHandler(handler)
loglines = buf.getvalue().split("\n")
self.assertTrue(loglines, "Log handler captured no messages?")
self.assertTrue(loglines[0].startswith("Hi! I ran something"))
# https://github.com/amoffat/sh/issues/273
def test_stop_iteration_doesnt_block(self):
"""proves that calling calling next() on a stopped iterator doesn't
hang."""
py = create_tmp_test(
"""
print("cool")
"""
)
p = python(py.name, _iter=True)
for i in range(100):
try:
next(p)
except StopIteration:
pass
# https://github.com/amoffat/sh/issues/195
def test_threaded_with_contexts(self):
import threading
import time
py = create_tmp_test(
"""
import sys
a = sys.argv
res = (a[1], a[3])
sys.stdout.write(repr(res))
"""
)
p1 = python.bake("-u", py.name, 1)
p2 = python.bake("-u", py.name, 2)
results = [None, None]
def f1():
with p1:
time.sleep(1)
results[0] = str(system_python("one"))
def f2():
with p2:
results[1] = str(system_python("two"))
t1 = threading.Thread(target=f1)
t1.start()
t2 = threading.Thread(target=f2)
t2.start()
t1.join()
t2.join()
correct = [
"('1', 'one')",
"('2', 'two')",
]
self.assertEqual(results, correct)
# https://github.com/amoffat/sh/pull/292
def test_eintr(self):
import signal
def handler(num, frame):
pass
signal.signal(signal.SIGALRM, handler)
py = create_tmp_test(
"""
import time
time.sleep(2)
"""
)
p = python(py.name, _bg=True)
signal.alarm(1)
p.wait()
class StreamBuffererTests(unittest.TestCase):
def test_unbuffered(self):
from sh import StreamBufferer
b = StreamBufferer(0)
self.assertEqual(b.process(b"test"), [b"test"])
self.assertEqual(b.process(b"one"), [b"one"])
self.assertEqual(b.process(b""), [b""])
self.assertEqual(b.flush(), b"")
def test_newline_buffered(self):
from sh import StreamBufferer
b = StreamBufferer(1)
self.assertEqual(b.process(b"testing\none\ntwo"), [b"testing\n", b"one\n"])
self.assertEqual(b.process(b"\nthree\nfour"), [b"two\n", b"three\n"])
self.assertEqual(b.flush(), b"four")
def test_chunk_buffered(self):
from sh import StreamBufferer
b = StreamBufferer(10)
self.assertEqual(b.process(b"testing\none\ntwo"), [b"testing\non"])
self.assertEqual(b.process(b"\nthree\n"), [b"e\ntwo\nthre"])
self.assertEqual(b.flush(), b"e\n")
@requires_posix
class ExecutionContextTests(unittest.TestCase):
def test_basic(self):
import sh
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.argv[1])
"""
)
out = StringIO()
sh2 = sh.bake(_out=out)
sh2.python(py.name, "TEST")
self.assertEqual("TEST", out.getvalue())
def test_multiline_defaults(self):
py = create_tmp_test(
"""
import os
print(os.environ["ABC"])
"""
)
sh2 = sh.bake(
_env={
"ABC": "123",
}
)
output = sh2.python(py.name).strip()
assert output == "123"
def test_no_interfere1(self):
import sh
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.argv[1])
"""
)
out = StringIO()
_sh = sh.bake(_out=out) # noqa: F841
_sh.python(py.name, "TEST")
self.assertEqual("TEST", out.getvalue())
# Emptying the StringIO
out.seek(0)
out.truncate(0)
sh.python(py.name, "KO")
self.assertEqual("", out.getvalue())
def test_no_interfere2(self):
import sh
out = StringIO()
from sh import echo
_sh = sh.bake(_out=out) # noqa: F841
echo("-n", "TEST")
self.assertEqual("", out.getvalue())
def test_set_in_parent_function(self):
import sh
py = create_tmp_test(
"""
import sys
sys.stdout.write(sys.argv[1])
"""
)
out = StringIO()
_sh = sh.bake(_out=out)
def nested1():
_sh.python(py.name, "TEST1")
def nested2():
import sh
sh.python(py.name, "TEST2")
nested1()
nested2()
self.assertEqual("TEST1", out.getvalue())
def test_command_with_baked_call_args(self):
# Test that sh.Command() knows about baked call args
import sh
_sh = sh.bake(_ok_code=1)
self.assertEqual(sh.Command._call_args["ok_code"], 0)
self.assertEqual(_sh.Command._call_args["ok_code"], 1)
if __name__ == "__main__":
root = logging.getLogger()
root.setLevel(logging.DEBUG)
root.addHandler(logging.NullHandler())
test_kwargs = {"warnings": "ignore"}
# if we're running a specific test, we can let unittest framework figure out
# that test and run it itself. it will also handle setting the return code
# of the process if any tests error or fail
if len(sys.argv) > 1:
unittest.main(**test_kwargs)
# otherwise, it looks like we want to run all the tests
else:
suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
test_kwargs["verbosity"] = 2
result = unittest.TextTestRunner(**test_kwargs).run(suite)
if not result.wasSuccessful():
exit(1)
|