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 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
|
# frozen_string_literal: true
require_relative 'test_helper'
require 'csv'
# FakeFS tests
class FakeFSTest < Minitest::Test
def setup
act_on_real_fs do
File.umask(0o006)
FileUtils.rm_rf(real_file_sandbox)
FileUtils.mkdir_p(real_file_sandbox)
FileUtils.chmod(0o777, real_file_sandbox)
end
FakeFS.activate!
FakeFS::FileSystem.clear
# Create /tmp so that Minitest can create files for diffing when an
# assertion fails. See https://github.com/defunkt/fakefs/issues/143
FileUtils.mkdir_p('/tmp')
end
def teardown
FakeFS.deactivate!
FakeFS::FileSystem.clear
act_on_real_fs do
FileUtils.rm_rf(real_file_sandbox)
end
end
# See https://github.com/fakefs/fakefs/issues/391
# Helper method to simplify running tests with either string paths as arguments
# or Pathname arguments. Make sure to wrap paths with string_or_pathname inside
# the given block.
def perform_with_both_string_paths_and_pathnames(&_block)
@use_pathnames = false
yield
@use_pathnames = true
yield
end
# See above and https://github.com/fakefs/fakefs/issues/391
# Returns the given string_path or a Pathname object, depending on whether
# @use_pathnames is set.
def string_or_pathname(string_path)
@use_pathnames ? Pathname.new(string_path) : string_path
end
def test_can_be_initialized_empty
FakeFS::FileSystem.clear
fs = FakeFS::FileSystem
assert_equal 0, fs.files.size
end
def xtest_can_be_initialized_with_an_existing_directory
fs = FakeFS::FileSystem
fs.clone(__dir__).inspect
assert_equal 1, fs.files.size
end
def test_can_create_directories_with_file_utils_mkdir_p
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/dir'))
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
end
end
def test_can_cd_to_directory_with_block
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/dir'))
new_path = nil
FileUtils.cd(string_or_pathname('/path/to')) do
new_path = Dir.getwd
end
assert_equal new_path, '/path/to'
end
end
def test_can_create_a_list_of_directories_with_file_utils_mkdir_p
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p([string_or_pathname('/path/to/dir1'), string_or_pathname('/path/to/dir2')])
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir1']
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir2']
end
end
def test_can_create_directories_with_options
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/dir'), mode: 0o755)
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
end
end
def test_can_create_directories_with_file_utils_mkdir
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/dir'))
FileUtils.mkdir(path = string_or_pathname('/path/to/dir/subdir'))
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']['subdir']
FileUtils.rm(path)
end
end
def test_can_create_a_list_of_directories_with_file_utils_mkdir
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/dir'))
FileUtils.mkdir([string_or_pathname('/path/to/dir/subdir1'), string_or_pathname('/path/to/dir/subdir2')])
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']['subdir1']
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']['subdir2']
FileUtils.rmdir([string_or_pathname('/path/to/dir/subdir1'), string_or_pathname('/path/to/dir/subdir2')])
end
end
def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
FileUtils.mkdir(string_or_pathname('/this/path/does/not/exists/newdir'))
end
end
end
def test_raises_error_when_creating_a_new_dir_over_existing_file
perform_with_both_string_paths_and_pathnames do
File.open(string_or_pathname('file'), 'w') { |f| f << 'This is a file, not a directory.' }
assert_raises Errno::EEXIST do
FileUtils.mkdir_p(string_or_pathname('file/subdir'))
end
FileUtils.mkdir(dir = string_or_pathname('dir'))
File.open(path = string_or_pathname('dir/subfile'), 'w') { |f| f << 'This is a file inside a directory.' }
assert_raises Errno::EEXIST do
FileUtils.mkdir_p(string_or_pathname('file/subdir'))
end
FileUtils.rm(path)
FileUtils.rmdir(dir)
end
end
def test_can_create_directories_with_mkpath
perform_with_both_string_paths_and_pathnames do
FileUtils.mkpath(string_or_pathname('/path/to/dir'))
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
end
end
def test_can_create_directories_with_mkpath_and_options
perform_with_both_string_paths_and_pathnames do
FileUtils.mkpath(string_or_pathname('/path/to/dir'), mode: 0o755)
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
end
end
def test_can_create_directories_with_mkdirs
perform_with_both_string_paths_and_pathnames do
FileUtils.makedirs(string_or_pathname('/path/to/dir'))
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
end
end
def test_can_create_directories_with_mkdirs_and_options
perform_with_both_string_paths_and_pathnames do
FileUtils.makedirs(string_or_pathname('/path/to/dir'), mode: 0o755)
assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
end
end
def test_unlink_errors_on_file_not_found
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
FileUtils.rm(string_or_pathname('/foo'))
end
end
end
def test_unlink_doesnt_error_on_file_not_found_when_forced
perform_with_both_string_paths_and_pathnames do
FileUtils.rm(string_or_pathname('/foo'), force: true)
end
end
def test_unlink_doesnt_error_on_file_not_found_with_rm_rf
perform_with_both_string_paths_and_pathnames do
FileUtils.rm_rf(string_or_pathname('/foo'))
end
end
def test_unlink_doesnt_error_on_file_not_found_with_rm_f
perform_with_both_string_paths_and_pathnames do
FileUtils.rm_f(string_or_pathname('/foo'))
end
end
def test_can_delete_directories
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/dir'))
FileUtils.rmdir(string_or_pathname('/path/to/dir'))
assert File.exist?(string_or_pathname('/path/to/'))
assert File.exist?(string_or_pathname('/path/to/dir')) == false
end
end
def test_can_delete_multiple_files
perform_with_both_string_paths_and_pathnames do
FileUtils.touch([string_or_pathname('foo'), string_or_pathname('bar')])
FileUtils.rm([string_or_pathname('foo'), string_or_pathname('bar')])
assert File.exist?(string_or_pathname('foo')) == false
assert File.exist?(string_or_pathname('bar')) == false
end
end
def test_aliases_exist
assert File.respond_to?(:unlink)
assert FileUtils.respond_to?(:rm_f)
assert FileUtils.respond_to?(:rm_r)
assert FileUtils.respond_to?(:rm)
assert FileUtils.respond_to?(:symlink)
assert FileUtils.respond_to?(:move)
assert FileUtils.respond_to?(:copy)
assert FileUtils.respond_to?(:remove)
assert FileUtils.respond_to?(:rmtree)
assert FileUtils.respond_to?(:safe_unlink)
assert FileUtils.respond_to?(:cmp)
assert FileUtils.respond_to?(:identical?)
end
def test_knows_directories_exist
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/path/to/dir'))
assert File.exist?(path)
end
end
def test_handles_pathnames
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('/path/to/dir')
FileUtils.mkdir_p(path)
path_name = RealPathname.new(path)
assert File.directory?(path_name)
path_name = Pathname.new(path)
assert File.directory?(path_name)
end
end
def test_knows_directories_are_directories
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/path/to/dir'))
assert File.directory?(path)
end
end
def test_knows_directories_are_directories_with_periods
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(period_path = string_or_pathname('/path/to/periodfiles/one.one'))
FileUtils.mkdir(path = string_or_pathname('/path/to/periodfiles/one-one'))
assert File.directory?(period_path)
FileUtils.rmdir(path)
end
end
def test_knows_symlink_directories_are_directories
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/path/to/dir'))
FileUtils.ln_s path, sympath = string_or_pathname('/sympath')
assert File.directory?(sympath)
FileUtils.rm(sympath)
end
end
def test_knows_non_existent_directories_arent_directories
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('does/not/exist/')
assert_equal RealFile.directory?(path), File.directory?(path)
end
end
def test_doesnt_overwrite_existing_directories
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/path/to/dir'))
assert File.exist?(path)
FileUtils.mkdir_p(string_or_pathname('/path/to'))
assert File.exist?(path)
assert_raises Errno::EEXIST do
FileUtils.mkdir(string_or_pathname('/path/to'))
end
assert File.exist?(path)
end
end
def test_file_utils_mkdir_takes_options
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir(path = string_or_pathname('/foo'), some: :option)
assert File.exist?(path)
FileUtils.rmdir(path)
end
end
def test_symlink_with_missing_refferent_does_not_exist
perform_with_both_string_paths_and_pathnames do
File.symlink(string_or_pathname('/foo'), sympath = string_or_pathname('/bar'))
refute File.exist?(string_or_pathname('/bar'))
FileUtils.rm(sympath)
end
end
def test_can_create_symlinks
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/target'))
FileUtils.ln_s(target, sympath = string_or_pathname('/path/to/link'))
assert_kind_of FakeFS::FakeSymlink, FakeFS::FileSystem.fs['path']['to']['link']
assert_raises(Errno::EEXIST) do
FileUtils.ln_s(target, sympath)
end
FileUtils.rm(sympath)
end
end
def test_can_force_creation_of_symlinks
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/first/target'))
FileUtils.ln_s(target, sympath = string_or_pathname('/path/to/link'))
assert_kind_of FakeFS::FakeSymlink, FakeFS::FileSystem.fs['path']['to']['link']
FileUtils.ln_s(target, sympath, force: true)
FileUtils.rm(sympath)
end
end
def test_create_symlink_using_ln_sf
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/first/target'))
FileUtils.ln_s(target, sympath = string_or_pathname('/path/to/link'))
assert_kind_of FakeFS::FakeSymlink, FakeFS::FileSystem.fs['path']['to']['link']
FileUtils.ln_sf(target, sympath)
FileUtils.rm(sympath)
end
end
def test_can_follow_symlinks
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/target'))
FileUtils.ln_s(target, link = string_or_pathname('/path/to/symlink'))
assert_equal target, File.readlink(link)
FileUtils.rm(link)
end
end
def test_symlinks_in_different_directories
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to/bar'))
FileUtils.mkdir_p(target = string_or_pathname('/path/to/foo/target'))
FileUtils.ln_s(target, link = string_or_pathname('/path/to/bar/symlink'))
assert_equal target, File.readlink(link)
FileUtils.rm(link)
end
end
def test_symlink_with_relative_path_exists
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('/file'))
FileUtils.mkdir_p(string_or_pathname('/a/b'))
FileUtils.ln_s(string_or_pathname('../../file'), sympath = string_or_pathname('/a/b/symlink'))
assert File.exist?(string_or_pathname('/a/b/symlink'))
FileUtils.rm(sympath)
end
end
def test_symlink_with_relative_path_and_nonexistant_file_does_not_exist
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('/file'))
FileUtils.mkdir_p(string_or_pathname('/a/b'))
FileUtils.ln_s(string_or_pathname('../../file_foo'), sympath = string_or_pathname('/a/b/symlink'))
refute File.exist?(sympath)
FileUtils.rm(sympath)
end
end
def test_symlink_with_relative_path_has_correct_target
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('/file'))
FileUtils.mkdir_p(string_or_pathname('/a/b'))
FileUtils.ln_s(path = string_or_pathname('../../file'), link = string_or_pathname('/a/b/symlink'))
assert_equal path, File.readlink(link)
FileUtils.rm(link)
end
end
def test_symlinks_to_symlinks
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/foo/target'))
FileUtils.mkdir_p(string_or_pathname('/path/to/bar'))
FileUtils.mkdir_p(string_or_pathname('/path/to/bar2'))
FileUtils.ln_s(target, link1 = string_or_pathname('/path/to/bar/symlink'))
FileUtils.ln_s(link1, link2 = string_or_pathname('/path/to/bar2/symlink'))
assert_equal link1, File.readlink(link2)
FileUtils.rm(link1)
FileUtils.rm(link2)
end
end
def test_symlink_to_symlinks_should_raise_error_if_dir_doesnt_exist
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/foo/target'))
refute Dir.exist?(string_or_pathname('/path/to/bar'))
assert_raises Errno::ENOENT do
FileUtils.ln_s(target, string_or_pathname('/path/to/bar/symlink'))
end
end
end
def test_knows_symlinks_are_symlinks
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(target = string_or_pathname('/path/to/target'))
FileUtils.ln_s(target, link = string_or_pathname('/path/to/symlink'))
assert File.symlink?(link)
FileUtils.rm(link)
end
end
def test_can_create_files_in_current_dir
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert File.exist?(path)
assert File.readable?(path)
assert File.writable?(path)
FileUtils.rm(path)
end
end
def test_can_create_files_with_brackets
perform_with_both_string_paths_and_pathnames do
# test various combinations of files with brackets
file = string_or_pathname('[file')
File.open(file, 'w') { |f| f << 'some content' }
assert File.exist?(file)
FileUtils.rm(file)
file = string_or_pathname(']file')
File.open(file, 'w') { |f| f << 'some content' }
assert File.exist?(file)
FileUtils.rm(file)
file = string_or_pathname('fi][le')
File.open(file, 'w') { |f| f << 'some content' }
assert File.exist?(file)
FileUtils.rm(file)
file = string_or_pathname('[file]')
File.open(file, 'w') { |f| f << 'some content' }
assert File.exist?(file)
FileUtils.rm(file)
file = string_or_pathname('[[[[]][[]][]][[[[[[[[]]]')
File.open(file, 'w') { |f| f << 'some content' }
assert File.exist?(file)
FileUtils.rm(file)
end
end
def test_nothing_is_sticky
perform_with_both_string_paths_and_pathnames do
refute File.sticky?(string_or_pathname('/'))
end
end
def test_can_create_files_in_existing_dir
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p string_or_pathname('/path/to')
path = string_or_pathname('/path/to/file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert File.exist?(path)
assert File.readable?(path)
assert File.writable?(path)
FileUtils.rm(path)
end
end
def test_write_returns_length
assert_equal 2, File.write('filename', 'hi')
assert_equal 2, File.write('filename', 'hi', 8)
assert_equal 8, File.write('filename', 'hi', encoding: Encoding::UTF_32LE)
skip 'raises "ASCII incompatible encoding needs binmode"'
# IO.open validates encoding, but IO.write seems to bypass this check
# not possible to do that with StringIO
assert_equal 8, File.write('filename', 'hi', 2, encoding: Encoding::UTF_32LE)
end
# Taken from ruby specs
def test_uses_an_open_args_option
assert_equal 2, File.write('filename', 'hi', open_args: ['w'])
assert_equal 8, File.write('filename', 'hi', open_args: ['w', { encoding: Encoding::UTF_32LE }])
assert_equal 8, File.write('filename', 'hi', open_args: ['w', nil, { encoding: Encoding::UTF_32LE }])
assert_equal 8, File.write('filename', 'hi', open_args: ['w', nil, { encoding: Encoding::UTF_32LE }])
end
def test_disregards_other_options_if_open_args_is_given
# first, test regular call
assert_equal 8, File.write('filename', 'hi', 2, mode: "w", encoding: Encoding::UTF_32LE)
assert_equal "\u0000\u0000h\u0000\u0000\u0000i\u0000\u0000\u0000", File.read('filename')
# encoding and mode are ignored
assert_equal 2, File.write('filename', 'hi', 2, mode: "r", encoding: Encoding::UTF_32LE, open_args: ["w"])
assert_equal "\0\0hi", File.read('filename')
end
def test_raises_ENOENT_trying_to_create_files_in_nonexistent_dir
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('/path/to/file.txt')
assert_raises(Errno::ENOENT) do
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
end
end
end
def test_raises_ENOENT_trying_to_create_files_in_relative_nonexistent_dir
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p string_or_pathname('/some/path')
Dir.chdir(string_or_pathname('/some/path')) do
assert_raises(Errno::ENOENT) do
File.open(string_or_pathname('../foo')) { |f| f.write 'moo' }
end
end
end
end
def test_raises_ENOENT_trying_to_create_files_in_obscured_nonexistent_dir
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p string_or_pathname('/some/path')
assert_raises(Errno::ENOENT) do
File.open(string_or_pathname('/some/path/../foo')) { |f| f.write 'moo' }
end
end
end
def test_raises_ENOENT_trying_to_create_tilde_referenced_nonexistent_dir
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname("~/fakefs_test_#{$$}_0000")
path = path.succ while File.exist? path
assert_raises(Errno::ENOENT) do
File.open(string_or_pathname("#{path}/foo")) { |f| f.write 'moo' }
end
end
end
def test_raises_EISDIR_if_trying_to_open_existing_directory_name
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('/path/to')
FileUtils.mkdir_p path
assert_raises(Errno::EISDIR) do
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
end
end
end
def test_can_create_files_with_bitmasks
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/path/to'))
path = string_or_pathname('/path/to/file.txt')
File.open(path, File::RDWR | File::CREAT) do |f|
f.write 'Yatta!'
end
assert File.exist?(path)
assert File.readable?(path)
assert File.writable?(path)
FileUtils.rm(path)
end
end
def test_file_opens_in_read_only_mode
perform_with_both_string_paths_and_pathnames do
File.open(string_or_pathname('foo'), 'w') { |f| f << 'foo' }
f = File.open(string_or_pathname('foo'))
assert_raises(IOError) do
f << 'bar'
end
end
end
def test_file_opens_in_read_only_mode_with_bitmasks
perform_with_both_string_paths_and_pathnames do
File.open(string_or_pathname('foo'), 'w') { |f| f << 'foo' }
f = File.open(string_or_pathname('foo'), File::RDONLY)
assert_raises(IOError) do
f << 'bar'
end
end
end
def test_file_opens_in_invalid_mode
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('foo'))
assert_raises(ArgumentError) do
File.open(string_or_pathname('foo'), 'an_illegal_mode')
end
end
end
def test_raises_error_when_cannot_find_file_in_read_mode
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
File.open(string_or_pathname('does_not_exist'), 'r')
end
end
end
def test_raises_error_when_cannot_find_file_in_read_write_mode
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
File.open(string_or_pathname('does_not_exist'), 'r+')
end
end
end
def test_globs_in_exist
perform_with_both_string_paths_and_pathnames do
Dir.mkdir(string_or_pathname("a"))
File.write(string_or_pathname("a/1.txt"), "a")
assert_equal File.exist?("**/*.txt"), false
assert_equal File.exist?("a/1.txt"), true
# otherwise the second pass of perform_with_both_string_paths_and_pathnames fails with
# Errno::EEXIST: File exists - a
FakeFS::FileSystem.clear
end
end
def test_special_chars_in_paths
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname("{}.txt")
File.write(path, "a")
assert_equal File.exist?(path), true
end
end
def test_creates_files_in_write_only_mode
perform_with_both_string_paths_and_pathnames do
File.open(string_or_pathname('foo'), 'w')
assert File.exist?(string_or_pathname('foo'))
end
end
def test_creates_files_in_write_only_mode_with_bitmasks
perform_with_both_string_paths_and_pathnames do
File.open(string_or_pathname('foo'), File::WRONLY | File::CREAT)
assert File.exist?(string_or_pathname('foo'))
end
end
def test_raises_in_write_only_mode_without_create_bitmask
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
File.open(string_or_pathname('foo'), File::WRONLY)
end
end
end
def test_creates_files_in_read_write_truncate_mode
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w+')
assert File.exist?(path)
FileUtils.rm(path)
end
end
def test_creates_files_in_append_write_only
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'a')
assert File.exist?(path)
FileUtils.rm(path)
end
end
def test_creates_files_in_append_read_write
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'a+')
assert File.exist?(path)
FileUtils.rm(path)
end
end
def test_file_in_write_only_raises_error_when_reading
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('foo'))
f = File.open(string_or_pathname('foo'), 'w')
assert_raises(IOError) do
f.read
end
end
end
def test_file_in_write_mode_truncates_existing_file
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'contents' }
File.open(path, 'w')
assert_equal '', File.read(path)
FileUtils.rm(path)
end
end
def test_file_in_read_write_truncation_mode_truncates_file
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'foo' }
File.open(path, 'w+')
assert_equal '', File.read(path)
FileUtils.rm(path)
end
end
def test_can_read_file_including_dollar
perform_with_both_string_paths_and_pathnames do
File.write(path = string_or_pathname('$foo'), 'foo')
assert_equal 'foo', File.read(path)
FileUtils.rm(path)
end
end
def test_file_in_append_write_only_raises_error_when_reading
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(path = string_or_pathname('foo'))
f = File.open(path, 'a')
assert_raises(IOError) do
f.read
end
end
end
def test_can_read_files_once_written
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert_equal 'Yatta!', File.read(path)
FileUtils.rm(path)
end
end
def test_file_read_accepts_hashes
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
# nothing raised
File.read(path, mode: 'r:UTF-8:-')
FileUtils.rm(path)
end
end
def test_file_read_respects_args
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert_equal 'Ya', File.read(path, 2)
assert_equal 'at', File.read(path, 2, 1)
assert_equal 'atta!', File.read(path, nil, 1)
FileUtils.rm(path)
end
end
def test_can_write_to_files
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 'Yada Yada', File.read(path)
FileUtils.rm(path)
end
end
def test_raises_error_when_opening_with_binary_mode_only
perform_with_both_string_paths_and_pathnames do
assert_raises ArgumentError do
File.open(string_or_pathname('/foo'), 'b')
end
end
end
def test_can_open_file_in_binary_mode
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'wb') { |x| x << 'a' }
assert_equal 'a', File.read(path)
FileUtils.rm(path)
end
end
def test_can_chunk_io_when_reading
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p string_or_pathname('/path/to')
path = string_or_pathname('/path/to/file.txt')
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
file = File.new(path, 'r')
assert_equal 'Yada', file.read(4)
assert_equal ' Yada', file.read(5)
assert_equal '', file.read
file.rewind
assert_equal 'Yada Yada', file.read
FileUtils.rm(path)
end
end
def test_can_get_size_of_files
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 9, File.size(path)
FileUtils.rm(path)
end
end
def test_can_get_correct_size_for_files_with_multibyte_characters
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'wb') do |f|
f << "Y\xC3\xA1da"
end
assert_equal 5, File.size(path)
FileUtils.rm(path)
end
end
def test_can_get_correct_size_for_empty_directory
perform_with_both_string_paths_and_pathnames do
Dir.mkdir(dir = string_or_pathname('/foo'))
assert_equal 64, File.size?(dir)
FileUtils.rmdir(dir)
end
end
def test_can_get_correct_size_for_parent_directory
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('/foo/bar'))
assert_equal 96, File.size?(dir = string_or_pathname('/foo'))
FileUtils.rmtree(dir)
end
end
def test_can_get_correct_size_for_grandparent_directory
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p string_or_pathname('/foo/bar/baz')
assert_equal 96, File.size?(dir = string_or_pathname('/foo'))
FileUtils.rmtree(dir)
end
end
def test_can_get_correct_size_for_grandparent_directory_with_files
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p string_or_pathname('/foo/bar/baz')
File.open('/foo/a.txt', 'w')
File.open('/foo/bar/b.txt', 'w')
assert_equal 128, File.size?(dir = string_or_pathname('/foo'))
FileUtils.rmtree(dir)
end
end
def test_can_check_if_file_has_size?
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 9, File.size?(path)
assert_nil File.size?(string_or_pathname('other.txt'))
FileUtils.rm(path)
end
end
def test_can_check_size_of_empty_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << ''
end
assert_nil File.size?(string_or_pathname('file.txt'))
FileUtils.rm(path)
end
end
def test_can_check_size_of_directory
perform_with_both_string_paths_and_pathnames do
Dir.mkdir(path = string_or_pathname('/foo'))
assert_equal 64, File.size?(path)
FileUtils.rm(path)
end
end
def test_zero_on_empty_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << ''
end
assert_equal true, File.zero?(path)
FileUtils.rm(path)
end
end
def test_zero_on_non_empty_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << 'Not empty'
end
assert_equal false, File.zero?(path)
FileUtils.rm(path)
end
end
def test_zero_on_non_existent_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file_does_not_exist.txt')
assert_equal false, File.zero?(path)
end
end
if RUBY_VERSION >= '2.4'
def test_empty_on_empty_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << ''
end
assert_equal true, File.empty?(path)
FileUtils.rm(path)
end
end
def test_empty_on_non_empty_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << 'Not empty'
end
assert_equal false, File.empty?(path)
FileUtils.rm(path)
end
end
def test_empty_on_non_existent_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file_does_not_exist.txt')
assert_equal false, File.empty?(path)
end
end
else
def test_file_empty_not_implemented
assert_equal false, File.respond_to?(:empty?)
end
end
def test_raises_error_on_mtime_if_file_does_not_exist
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.mtime(string_or_pathname('/path/to/file.txt'))
end
end
end
def test_can_set_mtime_on_new_file_touch_with_param
perform_with_both_string_paths_and_pathnames do
time = Time.new(2002, 10, 31, 2, 2, 2, '+02:00')
FileUtils.touch(path = string_or_pathname('foo.txt'), mtime: time)
assert_equal File.mtime(path), time
FileUtils.rm(path)
end
end
def test_can_set_mtime_on_existing_file_touch_with_param
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(path = string_or_pathname('foo.txt'))
time = Time.new(2002, 10, 31, 2, 2, 2, '+02:00')
FileUtils.touch(path, mtime: time)
assert_equal File.mtime(path), time
end
end
def test_can_return_mtime_on_existing_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << ''
end
assert File.mtime(path).is_a?(Time)
FileUtils.rm(path)
end
end
def test_raises_error_on_ctime_if_file_does_not_exist
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.ctime(string_or_pathname('file.txt'))
end
end
end
def test_can_return_ctime_on_existing_file
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'some content' }
assert File.ctime(path).is_a?(Time)
FileUtils.rm(path)
end
end
def test_raises_error_on_atime_if_file_does_not_exist
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.atime(string_or_pathname('file.txt'))
end
end
end
def test_can_return_atime_on_existing_file
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'some content' }
assert File.atime(path).is_a?(Time)
FileUtils.rm(path)
end
end
def test_ctime_mtime_and_atime_are_equal_for_new_files
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('foo')
FileUtils.touch(path)
ctime = File.ctime(path)
mtime = File.mtime(path)
atime = File.atime(path)
assert ctime.is_a?(Time)
assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime
assert_equal ctime, atime
File.open(path, 'r') do |f|
assert_equal ctime, f.ctime
assert_equal mtime, f.mtime
assert_equal atime, f.atime
end
FileUtils.rm(path)
end
end
def test_ctime_mtime_and_atime_are_equal_for_new_directories
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('foo')
FileUtils.mkdir_p(path)
ctime = File.ctime(path)
mtime = File.mtime(path)
atime = File.atime(path)
assert ctime.is_a?(Time)
assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime
assert_equal ctime, atime
FileUtils.rmdir(path)
end
end
def test_file_ctime_is_equal_to_file_stat_ctime
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'some content' }
assert_equal File.stat(path).ctime, File.ctime(path)
FileUtils.rm(path)
end
end
def test_directory_ctime_is_equal_to_directory_stat_ctime
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('foo'))
assert_equal File.stat(path).ctime, File.ctime(path)
FileUtils.rmdir(path)
end
end
def test_file_mtime_is_equal_to_file_stat_mtime
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'some content' }
assert_equal File.stat(path).mtime, File.mtime(path)
FileUtils.rm(path)
end
end
def test_directory_mtime_is_equal_to_directory_stat_mtime
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('foo'))
assert_equal File.stat(path).mtime, File.mtime(path)
FileUtils.rmdir(path)
end
end
def test_file_atime_is_equal_to_file_stat_atime
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'some content' }
assert_equal File.stat(path).atime, File.atime(path)
FileUtils.rm(path)
end
end
def test_directory_atime_is_equal_to_directory_stat_atime
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('foo'))
assert_equal File.stat(path).atime, File.atime(path)
FileUtils.rmdir(path)
end
end
def test_utime_raises_error_if_path_does_not_exist
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.utime(Time.now, Time.now, string_or_pathname('/path/to/file.txt'))
end
end
end
def test_can_call_utime_on_an_existing_file
perform_with_both_string_paths_and_pathnames do
time = Time.now - 300 # Not now
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << ''
end
File.utime(time, time, path)
assert_equal time, File.mtime(path)
assert_equal time, File.atime(path)
FileUtils.rm(path)
end
end
def test_utime_returns_number_of_paths
perform_with_both_string_paths_and_pathnames do
path1, path2 = string_or_pathname('file.txt'), string_or_pathname('another_file.txt')
[path1, path2].each do |path|
File.open(path, 'w') do |f|
f << ''
end
end
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
FileUtils.rm(path1, path2)
end
end
def test_file_a_time_updated_when_file_is_read
perform_with_both_string_paths_and_pathnames do
old_atime = Time.now - 300
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f << 'Hello'
end
File.utime(old_atime, File.mtime(path), path)
assert_equal File.atime(path), old_atime
File.read(path)
assert File.atime(path) != old_atime
FileUtils.rm(path)
end
end
def test_can_read_with_File_readlines
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.puts 'Yatta!', 'Gatta!'
f.puts ['woot', 'toot']
end
assert_equal ["Yatta!\n", "Gatta!\n", "woot\n", "toot\n"], File.readlines(path)
FileUtils.rm(path)
end
end
def test_can_read_with_File_readlines_and_only_empty_lines
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write "\n"
end
assert_equal ["\n"], File.readlines(path)
FileUtils.rm(path)
end
end
def test_can_read_with_File_readlines_and_new_lines
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write "this\nis\na\ntest\n"
end
assert_equal ["this\n", "is\n", "a\n", "test\n"], File.readlines(path)
FileUtils.rm(path)
end
end
def test_can_read_with_File_readlines_with_chomp
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write "this\nis\na\ntest\n"
end
assert_equal ['this', 'is', 'a', 'test'], File.readlines(path, chomp: true)
FileUtils.rm(path)
end
end
def test_can_read_with_File_foreach
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.puts ['flub', 'dub', 'crub']
end
read_lines = []
File.foreach(path) { |line| read_lines << line }
assert_equal ["flub\n", "dub\n", "crub\n"], read_lines
FileUtils.rm(path)
end
end
def test_File_foreach_returns_iterator
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.puts ['flub', 'dub', 'shrub']
end
read_lines = File.foreach(path).to_a
assert_equal ["flub\n", "dub\n", "shrub\n"], read_lines
FileUtils.rm(path)
end
end
def test_file_ftype_is_equal_to_file_lstat_ftype
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'some content' }
FileUtils.ln_s(path, link = string_or_pathname('bar'))
assert_equal File.stat(link).ftype, File.ftype(link)
FileUtils.rm(link)
FileUtils.rm(path)
end
end
def test_File_close_disallows_further_access
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
file = File.open(path, 'w')
file.write 'Yada'
file.close
assert_raises IOError do
file.read
end
FileUtils.rm(path)
end
end
def test_File_close_disallows_further_writes
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
file = File.open(path, 'w')
file.write 'Yada'
file.close
assert_raises IOError do
file << 'foo'
end
FileUtils.rm(path)
end
end
def test_can_read_from_file_objects
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert_equal 'Yatta!', File.new(path).read
FileUtils.rm(path)
end
end
def test_can_read_nil_from_binary
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
f = File.new(path, 'rb')
assert_equal 'Yatta!', f.read(1000)
assert_nil f.read(1000)
FileUtils.rm(path)
end
end
def test_file_object_has_default_external_encoding
old_verbose = $VERBOSE
$VERBOSE = nil
old_encoding = Encoding.default_external
Encoding.default_external = 'UTF-8'
path = 'file.txt'
File.open(path, 'w') { |f| f.write 'Yatta!' }
assert_equal 'UTF-8', File.new(path).read.encoding.name
ensure
Encoding.default_external = old_encoding
$VERBOSE = old_verbose
end
def test_file_object_initialization_with_mode_in_hash_parameter
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('file.txt'), mode: 'w') { |f| f.write 'Yatta!' }
FileUtils.rm(path)
end
end
def test_file_object_initialization_with_brackets_in_filename
filename = 'bracket[1](2).txt'
expected_contents = 'Yokudekimashita'
# nothing raised
File.open(filename, mode: 'w') { |f| f.write expected_contents.to_s }
# /tmp is explicitly created
the_file = Dir['/*'] - ['/tmp']
assert_equal the_file.length, 1
assert_equal the_file[0], "/#{filename}"
contents = File.open("/#{filename}").read
assert_equal contents, expected_contents
end
def test_file_object_initialization_with_utf_chars
filename = "\u65e5\u672c\u8a9e.txt"
expected_contents = 'Yokudekimashita'
# nothing raised
File.open(filename, mode: 'w') { |f| f.write expected_contents.to_s }
contents = File.open("/#{filename}").read
assert_equal contents, expected_contents
end
def test_file_read_errors_appropriately
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.read(string_or_pathname('anything'))
end
end
end
def test_file_read_errors_on_directory
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('a_directory'))
assert_raises Errno::EISDIR do
File.read(path)
end
end
end
def test_knows_files_are_files
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert File.file?(path)
FileUtils.rm(path)
end
end
def test_size_returns_size
perform_with_both_string_paths_and_pathnames do
first_file = string_or_pathname('first.txt')
File.open(first_file, 'w') do |f|
f.write '12345678'
end
assert_equal File.size?(first_file), 8
File.open(first_file, 'w') do |f|
f.write 'abcd'
end
assert_equal File.size?(first_file), 4
second_file = string_or_pathname('second.txt')
File.open(second_file, 'w') do |f|
f.write '1'
end
assert_equal File.size?(second_file), 1
FileUtils.rm(first_file)
FileUtils.rm(second_file)
end
end
def test_File_io_returns_self
f = File.open('foo', 'w')
assert_equal f, f.to_io
end
def test_File_to_i_is_alias_for_filno
f = File.open('foo', 'w')
assert_equal f.method(:to_i), f.method(:fileno)
end
def test_knows_symlink_files_are_files
path = 'file.txt'
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
FileUtils.ln_s path, sympath = '/sympath'
assert File.file?(sympath)
end
def test_knows_non_existent_files_arent_files
perform_with_both_string_paths_and_pathnames do
assert_equal RealFile.file?(string_or_pathname('does/not/exist.txt')), File.file?(string_or_pathname('does/not/exist.txt'))
end
end
def test_executable_returns_false_for_non_existent_files
perform_with_both_string_paths_and_pathnames do
refute File.executable?(string_or_pathname('/does/not/exist'))
end
end
def groupname_of_id(gid)
Etc.getgrgid(gid).name
rescue ArgumentError # probably OSX, fall back on GID
gid
end
def test_can_chown_files
perform_with_both_string_paths_and_pathnames do
good = string_or_pathname('file.txt')
bad = string_or_pathname('nofile.txt')
File.open(good, 'w') { |f| f.write 'foo' }
username = Etc.getpwuid(Process.uid).name
groupname = groupname_of_id(Process.gid)
out = FileUtils.chown(1337, 1338, good, verbose: true)
assert_equal [good], out
assert_equal File.stat(good).uid, 1337
assert_equal File.stat(good).gid, 1338
assert_raises(Errno::ENOENT) do
FileUtils.chown(username, groupname, bad, verbose: true)
end
assert_equal [good], FileUtils.chown(username, groupname, good)
assert_equal File.stat(good).uid, Process.uid
assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
FileUtils.chown(username, groupname, bad)
end
assert_equal [good], FileUtils.chown(username, groupname, [good])
assert_equal File.stat(good).uid, Process.uid
assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
FileUtils.chown(username, groupname, [good, bad])
end
# FileUtils.chown with nil user and nil group should not change anything
FileUtils.chown(username, groupname, good)
assert_equal File.stat(good).uid, Process.uid
assert_equal File.stat(good).gid, Process.gid
assert_equal [good], FileUtils.chown(nil, nil, [good])
assert_equal File.stat(good).uid, Process.uid
assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
FileUtils.chown(nil, nil, [good, bad])
end
FileUtils.rm(good)
end
end
def test_can_chown_R_files
perform_with_both_string_paths_and_pathnames do
username = Etc.getpwuid(Process.uid).name
groupname = groupname_of_id(Process.gid)
FileUtils.mkdir_p(dir = string_or_pathname('/path/'))
File.open(foo = string_or_pathname('/path/foo'), 'w') { |f| f.write 'foo' }
File.open(foobar = string_or_pathname('/path/foobar'), 'w') { |f| f.write 'foo' }
assert_equal [dir], FileUtils.chown_R(username, groupname, dir)
[dir, foo, foobar].each do |f|
assert_equal File.stat(f).uid, Process.uid
assert_equal File.stat(f).gid, Process.gid
end
FileUtils.rmtree(dir)
end
end
def test_can_chmod_files
perform_with_both_string_paths_and_pathnames do
good = string_or_pathname('file.txt')
bad = string_or_pathname('nofile.txt')
FileUtils.touch(good)
assert_equal [good], FileUtils.chmod(0o600, good, verbose: true)
assert_equal File.stat(good).mode, 0o100600
assert_equal File.executable?(good), false
assert_raises(Errno::ENOENT) do
FileUtils.chmod(0o600, bad)
end
assert_equal [good], FileUtils.chmod(0o666, good)
assert_equal File.stat(good).mode, 0o100666
assert_raises(Errno::ENOENT) do
FileUtils.chmod(0o666, bad)
end
assert_equal [good], FileUtils.chmod(0o644, [good])
assert_equal File.stat(good).mode, 0o100644
assert_raises(Errno::ENOENT) do
FileUtils.chmod(0o644, bad)
end
assert_equal [good], FileUtils.chmod(0o744, [good])
assert_equal File.executable?(good), true
# This behaviour is unimplemented, the spec below is only to show that it
# is a deliberate YAGNI omission.
assert_equal [good], FileUtils.chmod(0o477, [good])
assert_equal File.executable?(good), false
FileUtils.rm(good)
end
end
def test_symbolic_chmod_mode
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('ugo=rwx', file_name)
assert_equal File.stat(file_name).mode, 0o100777
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('ug=x,o=rwx', directory_name)
assert_equal File.stat(directory_name).mode, 0o100117
end
def test_symbolic_chmod_mode_ignores_duplicate_groups
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('ugggooouuuggoo=rwx', file_name)
assert_equal File.stat(file_name).mode, 0o100777
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('o=', directory_name)
FileUtils.chmod('uggguuugguu=x', directory_name)
assert_equal File.stat(directory_name).mode, 0o100110
end
def test_symbolic_chmod_mode_ignores_duplicate_modes
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('o=', file_name)
FileUtils.chmod('ug=xxxrxxxrrrrx', file_name)
assert_equal File.stat(file_name).mode, 0o100550
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('ugo=xxxwwrrwwxxrxxww', directory_name)
assert_equal File.stat(directory_name).mode, 0o100777
end
def test_symbolic_chmod_mode_interprets_no_modes_as_zero_for_group
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('ugo=wrx', file_name)
FileUtils.chmod('u=', file_name)
assert_equal File.stat(file_name).mode, 0o100077
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('ugo=', directory_name)
assert_equal File.stat(directory_name).mode, 0o100000
end
def test_symbolic_chmod_mode_interprets_no_groups_as_all_groups
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('=w', file_name)
assert_equal File.stat(file_name).mode, 0o100222
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('=rw', directory_name)
assert_equal File.stat(directory_name).mode, 0o100666
end
def test_symbolic_chmod_mode_applies_only_rightmost_permissions_for_group
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('ugo=w,go=rx,u=', file_name)
assert_equal File.stat(file_name).mode, 0o100055
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('u=,go=r,=rwx', directory_name)
assert_equal File.stat(directory_name).mode, 0o100777
end
def test_symbolic_chmod_mode_raises_argument_error_when_flags_are_not_rwx
perform_with_both_string_paths_and_pathnames do
file_name = string_or_pathname('test.txt')
FileUtils.touch file_name
assert_raises ArgumentError do
FileUtils.chmod('=rwxt', file_name)
end
directory_name = string_or_pathname('dir/')
Dir.mkdir directory_name
assert_raises ArgumentError do
FileUtils.chmod('g=tru', directory_name)
end
FileUtils.rm(file_name)
FileUtils.rmdir(directory_name)
end
end
def test_symbolic_chmod_mode_raises_argument_error_when_groups_are_not_ugo
perform_with_both_string_paths_and_pathnames do
file_name = string_or_pathname('test.txt')
FileUtils.touch file_name
assert_raises ArgumentError do
FileUtils.chmod('ugto=rwx', file_name)
end
directory_name = string_or_pathname('dir/')
Dir.mkdir directory_name
assert_raises ArgumentError do
FileUtils.chmod('ugobt=r', directory_name)
end
FileUtils.rm(file_name)
FileUtils.rmdir(directory_name)
end
end
def test_symbolic_chmod_mode_handles_plus_sign
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod(0o234, file_name)
FileUtils.chmod('u+wrx', file_name)
assert_equal File.stat(file_name).mode, 0o100734
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('ugo=', directory_name)
FileUtils.chmod('go+rw', directory_name)
assert_equal File.stat(directory_name).mode, 0o100066
end
def test_symbolic_chmod_mode_handles_minus_sign
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod(0o777, file_name)
FileUtils.chmod('u-r', file_name)
assert_equal File.stat(file_name).mode, 0o100377
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod(0o567, directory_name)
FileUtils.chmod('go-r', directory_name)
assert_equal File.stat(directory_name).mode, 0o100523
end
def test_symbolic_chmod_mode_can_mix_minus_plus_assignment_ops
file_name = 'test.txt'
FileUtils.touch file_name
FileUtils.chmod('ugo=wrx,u-rx,go-wrx,u+x,g+wrx,o+wrx,o=rx,o-x', file_name)
assert_equal File.stat(file_name).mode, 0o100374
directory_name = 'dir/'
Dir.mkdir directory_name
FileUtils.chmod('=,u+x,g+r,o+w,ou-r,o=wrx,ug=,u+rw,o-wrx,g+rwx,o-rww', directory_name)
assert_equal File.stat(directory_name).mode, 0o100670
end
def test_can_chmod_R_files
FileUtils.mkdir_p '/path/sub'
FileUtils.touch '/path/file1'
FileUtils.touch '/path/sub/file2'
assert_equal ['/path'], FileUtils.chmod_R(0o600, '/path')
assert_equal File.stat('/path').mode, 0o100600
assert_equal File.stat('/path/file1').mode, 0o100600
assert_equal File.stat('/path/sub').mode, 0o100600
assert_equal File.stat('/path/sub/file2').mode, 0o100600
FileUtils.mkdir_p '/path2'
FileUtils.touch '/path2/hej'
assert_equal ['/path2'], FileUtils.chmod_R(0o600, '/path2')
end
def test_copy_entry
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(dir = string_or_pathname('/path'))
File.open(string_or_pathname('/path/foo'), 'w') { |f| f.write 'foo' }
File.open(string_or_pathname('/path/foobar'), 'w') { |f| f.write 'foo' }
FileUtils.mkdir_p(string_or_pathname('/path/bar'))
File.open(string_or_pathname('/path/bar/baz'), 'w') { |f| f.write 'foo' }
FileUtils.copy_entry(dir, copied_path = string_or_pathname('/copied_path'))
assert_equal ['/copied_path/bar',
'/copied_path/bar/baz',
'/copied_path/foo',
'/copied_path/foobar'], Dir.glob('/copied_path/**/*')
FileUtils.rmtree(copied_path)
FileUtils.rmtree(dir)
end
end
def test_dir_globs_paths
FileUtils.mkdir_p '/path'
File.open('/path/foo', 'w') { |f| f.write 'foo' }
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
File.open('/path/.bar', 'w') { |f| f.write 'foo' }
FileUtils.mkdir_p '/path/bar'
File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
FileUtils.cp_r '/path/bar', '/path/bar2'
assert_equal ['/path'], Dir['/path']
assert_equal ['/path/.bar'], Dir['**/{.*}']
assert_equal ['/path/.bar'], Dir['/path**/{.*}']
assert_equal ['/path/.bar'], Dir['/path/{.*}']
assert_equal ['/path/bar', '/path/bar2', '/path/foo', '/path/foobar'], Dir['/path/*']
assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
assert_equal ['/path/foo'], Dir['/path/foo']
assert_equal ['/path'], Dir['/path*']
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar', '/tmp'], Dir['/**/*']
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
assert_equal ['/path/bar/baz', '/path/bar2/baz'], Dir['/path/bar/**/*', '/path/bar2/**/*']
assert_equal ['/path/bar/baz', '/path/bar2/baz', '/path/bar/baz'], Dir['/path/ba*/**/*', '/path/bar/**/*']
FileUtils.cp_r '/path', '/otherpath'
assert_equal ['/otherpath/foo', '/otherpath/foobar', '/path/foo', '/path/foobar'], Dir['/*/foo*']
assert_equal ['/path/bar', '/path/foo'], Dir['/path/{foo,bar}']
assert_equal ['/path/bar', '/path/bar2'], Dir['/path/bar{2,}']
assert_equal ['/path/bar', '/path/foo'], Dir['{/nowhere,/path/{foo,bar}}']
assert_equal ['/path/bar', '/path/foo'], Dir['{/nowhere,/path/{foo,{bar,bar2/baz}}}']
Dir.chdir '/path' do
assert_equal ['foo'], Dir['foo']
end
end
def test_file_utils_cp_allows_verbose_option
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'TEST' }
assert_equal("cp foo bar\n", capture_stderr { FileUtils.cp path, string_or_pathname('bar'), verbose: true })
end
end
def test_file_utils_cp_allows_noop_option
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f << 'TEST' }
FileUtils.cp(path, string_or_pathname('bar'), noop: true)
refute File.exist?(string_or_pathname('bar')), 'does not actually copy'
FileUtils.rm(path)
end
end
def test_file_utils_cp_raises_on_invalid_option
perform_with_both_string_paths_and_pathnames do
assert_raises ArgumentError do
FileUtils.cp(string_or_pathname('foo'), string_or_pathname('bar'), whatisthis: "I don't know")
end
end
end
def test_file_utils_cp_r_allows_verbose_option
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(path = string_or_pathname('/foo'))
assert_equal("cp -r /foo /bar\n", capture_stderr { FileUtils.cp_r(path, string_or_pathname('/bar'), verbose: true) })
FileUtils.rm('/bar')
end
end
def test_file_utils_cp_r_allows_noop_option
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(path = string_or_pathname('/foo'))
FileUtils.cp_r(path, string_or_pathname('/bar'), noop: true)
refute File.exist?(string_or_pathname('/bar')), 'does not actually copy'
FileUtils.rm(path)
end
end
def test_dir_glob_handles_root
FileUtils.mkdir_p '/path'
# this fails. the root dir should be named '/' but it is '.'
assert_equal ['/'], Dir['/']
end
def test_dir_glob_takes_optional_flags
FileUtils.touch '/.foo'
assert_equal ['/.foo', '/tmp'], Dir.glob('/*', File::FNM_DOTMATCH)
end
def test_dir_glob_takes_optional_flags_as_keyword
FileUtils.touch '/.foo'
assert_equal ['/.foo', '/tmp'], Dir.glob('/*', flags: File::FNM_DOTMATCH)
end
def test_dir_glob_takes_base
FileUtils.mkdir_p '/foo/bar'
FileUtils.touch '/foo/bar/one'
FileUtils.touch '/foo/bar/two'
Dir.chdir('/foo') do
assert_equal ['one', 'two'], Dir.glob('*', base: 'bar')
end
end
def test_dir_glob_handles_recursive_globs
FileUtils.mkdir_p '/one/two/three'
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
assert_equal ['/one/two'], Dir['/one/**/two']
assert_equal ['/one/two/three'], Dir['/one/**/three']
end
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
FileUtils.mkdir_p '/one/two/three'
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**']
end
def test_dir_glob_ending_in_group_and_wildcard
FileUtils.mkdir_p '/tmp/python-3.4.1'
FileUtils.mkdir_p '/tmp/python-2.7.8'
assert_equal ['/tmp/python-2.7.8', '/tmp/python-3.4.1'], Dir.glob('/tmp/python-[0-9]*')
end
def test_dir_glob_plus_sign_in_dir_name
FileUtils.mkdir_p '/abc+cde/python-3.4.1'
FileUtils.mkdir_p '/abc+cde/python-2.7.8'
Dir.chdir('/abc+cde') do
assert_equal ['python-2.7.8', 'python-3.4.1'], Dir.glob('**/*')
end
end
def test_dir_glob_plus_signs_in_dir_name
FileUtils.mkdir_p '/abc+cde+123/python-3.4.1'
FileUtils.mkdir_p '/abc+cde+123/python-2.7.8'
Dir.chdir('/abc+cde+123') do
assert_equal ['python-2.7.8', 'python-3.4.1'], Dir.glob('**/*')
end
end
def test_dir_glob_respects_fnm_dotmatch
File.open('/file', 'w') { |f| f << 'content' }
File.open('/.file_hidden', 'w') { |f| f << 'content' }
Dir.mkdir('/subdir')
Dir.mkdir('/.subdir_hidden')
# add in /tmp since it's made by the test suite
assert_equal ['/.file_hidden', '/.subdir_hidden', '/file', '/subdir', '/tmp'], Dir.glob('*', File::FNM_DOTMATCH)
end
def test_dir_glob_with_block
FileUtils.touch('foo')
FileUtils.touch('bar')
yielded = []
Dir.glob('*') { |file| yielded << file }
assert_equal 3, yielded.size
end
def test_dir_glob_with_sort_false
FileUtils.touch('foo')
FileUtils.touch('bar')
assert_equal Dir.glob('*', sort: false), ['/tmp', '/foo', '/bar']
end
def test_copy_with_subdirectory
FileUtils.mkdir_p '/one/two/three/'
FileUtils.mkdir_p '/onebis/two/three/'
FileUtils.touch '/one/two/three/foo'
Dir.glob('/one/two/three/*') do |hook|
FileUtils.cp(hook, '/onebis/two/three/')
end
assert_equal ['/onebis/two/three/foo'], Dir['/onebis/two/three/*']
end
def test_dir_home
skip "only broken in jruby, needs a fix" if RUBY_PLATFORM == "java"
assert_equal RealDir.home, Dir.home
end
if RUBY_VERSION >= '2.4'
def test_dir_empty_on_empty_directory
perform_with_both_string_paths_and_pathnames do
dir_path = string_or_pathname('an-empty-dir')
FileUtils.mkdir dir_path
assert_equal true, Dir.empty?(dir_path)
FileUtils.rmdir(dir_path)
end
end
def test_dir_empty_on_directory_with_subdirectory
perform_with_both_string_paths_and_pathnames do
parent = string_or_pathname('parent')
child = string_or_pathname('child')
path = File.join(parent, child)
FileUtils.mkdir_p path
assert_equal false, Dir.empty?(parent)
FileUtils.rmtree(parent)
end
end
def test_dir_empty_on_directory_with_file
perform_with_both_string_paths_and_pathnames do
dir_path = string_or_pathname('a-non-empty-dir')
FileUtils.mkdir dir_path
file_path = File.join(dir_path, string_or_pathname('file.txt'))
FileUtils.touch(file_path)
assert_equal false, Dir.empty?(dir_path)
FileUtils.rmtree(dir_path)
end
end
def test_dir_empty_on_nonexistent_path
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) { Dir.empty?(string_or_pathname('/not/a/real/dir/')) }
end
end
def test_dir_empty_on_file
perform_with_both_string_paths_and_pathnames do
path = string_or_pathname('file.txt')
FileUtils.touch(path)
assert_equal false, Dir.empty?(path)
FileUtils.rm(path)
end
end
else
def test_dir_empty_not_implemented
assert_equal false, Dir.respond_to?(:empty?)
end
end
def test_should_report_pos_as_0_when_opening
File.open('foo', 'w') do |f|
f << 'foobar'
f.rewind
assert_equal 0, f.pos
end
end
def test_should_report_pos_as_1_when_seeking_one_char
File.open('foo', 'w') do |f|
f << 'foobar'
f.rewind
f.seek(1)
assert_equal 1, f.pos
end
end
def test_should_set_pos
File.open('foo', 'w') do |f|
f << 'foo'
end
fp = File.open('foo', 'r')
fp.pos = 1
assert_equal 1, fp.pos
end
def test_should_set_pos_with_tell_method
File.open('foo', 'w') do |f|
f << 'foo'
end
fp = File.open('foo', 'r')
fp.tell = 1
assert_equal 1, fp.pos
end
OMITTED_MRI_FILE_METHODS = [
# omit methods from etc
:pathconf,
# omit methods from io/console
:beep,
:cooked, :cooked!,
:cursor, :cursor=,
:echo?, :echo=, :noecho,
:goto,
:iflush, :ioflush, :oflush,
:pressed?,
:raw, :raw!,
:winsize, :winsize=,
# omit methods from io/nonblock
:nonblock?, :nonblock=, :nonblock,
# omit methods from io/wait
:nread, :pwrite, :pread,
:ready?,
:wait, :wait_readable, :wait_writable, :wait_priority,
:timeout, :timeout=,
:ttyname,
# omit methods from io/console required by irb
:check_winsize_changed,
:clear_screen,
:console_mode, :console_mode=,
:cursor_down, :cursor_left, :cursor_right, :cursor_up,
:erase_line, :erase_screen,
:goto_column, :scroll_backward, :scroll_forward
].freeze
OMITTED_JRUBY_FILE_METHODS = [
# omit public methods re https://github.com/jruby/jruby/issues/4275
:ttymode,
:ttymode_yield,
# omit Java-oriented conversion methods
:to_channel,
:to_outputstream,
:to_inputstream
].freeze
OMITTED_JRUBY_92_FILE_METHODS = [:to_output_stream, :to_input_stream].freeze
def self.omitted_file_methods
if defined?(JRUBY_VERSION)
if JRUBY_VERSION < '9.2'
OMITTED_MRI_FILE_METHODS + OMITTED_JRUBY_FILE_METHODS
else
OMITTED_MRI_FILE_METHODS + OMITTED_JRUBY_FILE_METHODS + OMITTED_JRUBY_92_FILE_METHODS
end
else
OMITTED_MRI_FILE_METHODS
end
end
(RealFile.instance_methods - omitted_file_methods).each do |method_name|
define_method("test_#{method_name}_method_in_file_is_in_fake_fs_file") do
skip "only broken in jruby, needs a fix" if RUBY_PLATFORM == "java"
assert File.instance_methods.include?(method_name), "#{method_name} method is not available in File :("
end
end
def test_file_should_not_respond_to_string_io_unique_methods_except_string
uniq_string_io_methods = (StringIO.instance_methods - RealFile.instance_methods)
# Remove `:string` because we implement a `#string` method
uniq_string_io_methods.delete(:string)
uniq_string_io_methods.each do |method_name|
refute File.instance_methods.include?(method_name), "File responds to #{method_name}"
end
end
def test_does_not_remove_methods_from_stringio
stringio = StringIO.new('foo')
assert stringio.respond_to?(:size)
end
def test_is_not_a_stringio
File.open('foo', 'w') do |f|
refute f.is_a?(StringIO), 'File is not a StringIO'
end
end
def test_can_be_read_via_csv_library
# Changes to how CSV's are read were introduced in Ruby 2.6.x.
# When parsing the CSV, `CSV::Parser` will break any non `File`
# object into chunks of 1024 bytes. If there's more than one chunk
# (i.e. file > 1024 bytes), each chunk will have `#string` invoked.
#
# In this spec, we generate a `FakeFS::File` with 1025 bytes of data,
# which pushes us just over the threshold of 1024 bytes. This provides
# an adequate test setup.
#
# We could make this number much higher than 1025. The only thing
# that matters is that the file is larger than 1024 bytes.
#
csv_rows = [
Array.new(171) { '1' }.join(','), # 341 bytes
Array.new(171) { '2' }.join(','), # 341 bytes
Array.new(171) { '3' }.join(',') # 341 bytes
]
csv_string = csv_rows.join("\n") # 1025 total bytes = (341 characters * 3) + (2 newline characters)
File.write('test.csv', csv_string)
result = begin
CSV.read('test.csv')
rescue ArgumentError # "unknown encoding name - bom|utf-8"
defined?(JRUBY_VERSION) ? skip : raise
end
assert result.is_a?(Array)
assert_equal csv_rows.length, result.length
end
def test_chdir_changes_directories_like_a_boss
perform_with_both_string_paths_and_pathnames do
# I know memes!
FileUtils.mkdir_p(path = string_or_pathname('/path'))
assert_equal '/', FakeFS::FileSystem.fs.name
assert_equal [], Dir.glob('/path/*')
Dir.chdir path do
File.open(string_or_pathname('foo'), 'w') { |f| f.write 'foo' }
File.open(string_or_pathname('foobar'), 'w') { |f| f.write 'foo' }
end
assert_equal '/', FakeFS::FileSystem.fs.name
assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
c = nil
Dir.chdir path do
c = File.open(string_or_pathname('foo'), 'r') { |f| f.read }
end
assert_equal 'foo', c
FileUtils.rmtree(path)
end
end
def test_chdir_shouldnt_keep_us_from_absolute_paths
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/path'))
Dir.chdir path do
File.open(string_or_pathname('foo'), 'w') { |f| f.write 'foo' }
File.open(string_or_pathname('/foobar'), 'w') { |f| f.write 'foo' }
end
assert_equal ['/path/foo'], Dir.glob('/path/*').sort
assert_equal ['/foobar', '/path', '/tmp'], Dir.glob('/*').sort
Dir.chdir path do
FileUtils.rm(string_or_pathname('foo'))
FileUtils.rm(string_or_pathname('/foobar'))
end
assert_equal [], Dir.glob('/path/*').sort
assert_equal ['/path', '/tmp'], Dir.glob('/*').sort
end
end
def test_chdir_should_be_nestable
FileUtils.mkdir_p '/path/me'
Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo' }
Dir.chdir 'me' do
File.open('foobar', 'w') { |f| f.write 'foo' }
end
end
assert_equal ['/path/foo', '/path/me'], Dir.glob('/path/*').sort
assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
end
def test_chdir_should_be_nestable_with_absolute_paths
FileUtils.mkdir_p '/path/me'
Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo' }
Dir.chdir '/path/me' do
File.open('foobar', 'w') { |f| f.write 'foo' }
end
end
assert_equal ['/path/foo', '/path/me'], Dir.glob('/path/*').sort
assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
end
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
Dir.chdir(string_or_pathname('/nope')) do
1
end
end
end
end
def test_chdir_raises_error_when_attempting_to_cd_into_a_file
perform_with_both_string_paths_and_pathnames do
File.open(file1 = string_or_pathname('file1'), 'w') { |f| f << 'content' }
assert_raises(Errno::ENOTDIR) do
Dir.chdir(file1)
end
assert_raises(Errno::ENOTDIR) do
Dir.chdir(file1) do
File.open(string_or_pathname('file2'), 'w') { |f| f << 'content' }
end
end
FileUtils.rm(file1)
end
end
def test_chdir_shouldnt_lose_state_because_of_errors
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/path'))
Dir.chdir path do
File.open(string_or_pathname('foo'), 'w') { |f| f.write 'foo' }
File.open(string_or_pathname('foobar'), 'w') { |f| f.write 'foo' }
end
begin
Dir.chdir(path) do
raise Errno::ENOENT
end
rescue Errno::ENOENT # hardcore
'Nothing to do'
end
Dir.chdir(path) do
begin
Dir.chdir(string_or_pathname('nope')) {}
rescue Errno::ENOENT
'Nothing to do'
end
assert_equal ['/', '/path'], FakeFS::FileSystem.dir_levels
end
assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
FileUtils.rmtree(path)
end
end
def test_chdir_with_no_block_is_awesome
FileUtils.mkdir_p '/path'
Dir.chdir('/path')
FileUtils.mkdir_p 'subdir'
assert_equal ['subdir'], Dir.glob('*')
Dir.chdir('subdir')
File.open('foo', 'w') { |f| f.write 'foo' }
assert_equal ['foo'], Dir.glob('*')
assert_raises(Errno::ENOENT) do
Dir.chdir('subsubdir')
end
assert_equal ['foo'], Dir.glob('*')
end
def test_chdir_with_a_block_passes_in_the_path
FileUtils.mkdir_p('/path')
Dir.chdir('/path') do |path|
assert_equal '/path', path
end
Dir.chdir('/path')
FileUtils.mkdir('subdir')
Dir.chdir('subdir') do |path|
assert_equal 'subdir', path
end
end
def test_current_dir_reflected_by_pwd
FileUtils.mkdir_p '/path'
Dir.chdir('/path')
assert_equal '/path', Dir.pwd
assert_equal '/path', Dir.getwd
FileUtils.mkdir_p 'subdir'
Dir.chdir('subdir')
assert_equal '/path/subdir', Dir.pwd
assert_equal '/path/subdir', Dir.getwd
end
def test_current_dir_reflected_by_expand_path_with_relative_paths
FileUtils.mkdir_p '/path'
Dir.chdir '/path'
assert_equal '/path', File.expand_path('.')
assert_equal '/path/foo', File.expand_path('foo')
FileUtils.mkdir_p 'subdir'
Dir.chdir 'subdir'
assert_equal '/path/subdir', File.expand_path('.')
assert_equal '/path/subdir/foo', File.expand_path('foo')
end
def test_expand_path_with_parent_dir
perform_with_both_string_paths_and_pathnames do
FakeFS.deactivate!
real = File.expand_path(string_or_pathname('other.file'), __dir__)
FakeFS.activate!
fake = File.expand_path(string_or_pathname('other.file'), __dir__)
assert_equal real, fake
end
end
def test_expand_path_works_with_absolute_paths
FakeFS.deactivate!
home = File.expand_path('~')
FakeFS.activate!
assert_equal "#{home}/dir/subdir", File.expand_path('subdir', '~/dir')
assert_equal '/somewhere/else', File.expand_path('else', '/somewhere')
end
def test_file_open_defaults_to_read
File.open('foo', 'w') { |f| f.write 'bar' }
assert_equal 'bar', File.open('foo') { |f| f.read }
end
def test_flush_exists_on_file
r = File.open('foo', 'w') do |f|
f.write 'bar'
f.flush
end
assert_equal 'foo', r.path
end
def test_mv_should_raise_error_on_missing_file
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
FileUtils.mv string_or_pathname('blafgag'), string_or_pathname('foo')
end
exception = assert_raises(Errno::ENOENT) do
FileUtils.mv [string_or_pathname('foo'), string_or_pathname('bar')], string_or_pathname('destdir')
end
assert_equal 'No such file or directory - foo', exception.message
end
end
def test_mv_actually_works
perform_with_both_string_paths_and_pathnames do
File.open(path = string_or_pathname('foo'), 'w') { |f| f.write 'bar' }
FileUtils.mv(path, baz = string_or_pathname('baz'))
assert_equal 'bar', File.open(baz) { |f| f.read }
FileUtils.rm(baz)
end
end
def test_mv_overwrites_existing_files
File.open('foo', 'w') { |f| f.write 'bar' }
File.open('baz', 'w') { |f| f.write 'qux' }
FileUtils.mv 'foo', 'baz'
assert_equal 'bar', File.read('baz')
end
def test_mv_works_with_options
File.open('foo', 'w') { |f| f.write 'bar' }
FileUtils.mv 'foo', 'baz', force: true
assert_equal('bar', File.open('baz') { |f| f.read })
end
def test_mv_to_directory
File.open('foo', 'w') { |f| f.write 'bar' }
FileUtils.mkdir_p 'destdir'
FileUtils.mv 'foo', 'destdir'
assert_equal('bar', File.open('destdir/foo') { |f| f.read })
assert File.directory?('destdir')
end
def test_mv_array
File.open('foo', 'w') { |f| f.write 'bar' }
File.open('baz', 'w') { |f| f.write 'binky' }
FileUtils.mkdir_p 'destdir'
FileUtils.mv ['foo', 'baz'], 'destdir'
assert_equal('bar', File.open('destdir/foo') { |f| f.read })
assert_equal('binky', File.open('destdir/baz') { |f| f.read })
end
def test_mv_accepts_verbose_option
FileUtils.touch 'foo'
assert_equal("mv foo bar\n", capture_stderr { FileUtils.mv 'foo', 'bar', verbose: true })
end
def test_mv_accepts_noop_option
FileUtils.touch 'foo'
FileUtils.mv 'foo', 'bar', noop: true
assert File.exist?('foo'), 'does not remove src'
refute File.exist?('bar'), 'does not create target'
end
def test_mv_raises_when_moving_file_onto_directory
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(string_or_pathname('dir/stuff'))
FileUtils.touch(stuff = string_or_pathname('stuff'))
assert_raises Errno::EEXIST do
FileUtils.mv(stuff, string_or_pathname('dir'))
end
FileUtils.rmtree('dir')
FileUtils.rm('stuff')
end
end
def test_mv_raises_when_moving_to_non_existent_directory
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(path = string_or_pathname('stuff'))
assert_raises Errno::ENOENT do
FileUtils.mv path, string_or_pathname('/this/path/is/not/here')
end
FileUtils.rm(path)
end
end
def test_mv_ignores_failures_when_using_force
FileUtils.mkdir_p 'dir/stuff'
FileUtils.touch ['stuff', 'other']
FileUtils.mv ['stuff', 'other'], 'dir', force: true
assert File.exist?('stuff'), 'failed move remains where it was'
assert File.exist?('dir/other'), 'successful one is moved'
refute File.exist?('other'), 'successful one is moved'
FileUtils.mv 'stuff', '/this/path/is/not/here', force: true
assert File.exist?('stuff'), 'failed move remains where it was'
refute File.exist?('/this/path/is/not/here'), 'nothing is created for a failed move'
end
def test_cp_actually_works
perform_with_both_string_paths_and_pathnames do
File.open(foo = string_or_pathname('foo'), 'w') { |f| f.write 'bar' }
FileUtils.cp(foo, baz = string_or_pathname('baz'))
assert_equal 'bar', File.read(baz)
FileUtils.rm(foo)
FileUtils.rm(baz)
end
end
def test_cp_file_into_dir
File.open('foo', 'w') { |f| f.write 'bar' }
FileUtils.mkdir_p 'baz'
FileUtils.cp('foo', 'baz')
assert_equal 'bar', File.read('baz/foo')
end
def test_cp_array_of_files_into_directory
File.open('foo', 'w') { |f| f.write 'footext' }
File.open('bar', 'w') { |f| f.write 'bartext' }
FileUtils.mkdir_p 'destdir'
FileUtils.cp(['foo', 'bar'], 'destdir')
assert_equal 'footext', File.read('destdir/foo')
assert_equal 'bartext', File.read('destdir/bar')
end
def test_cp_fails_on_array_of_files_into_non_directory
perform_with_both_string_paths_and_pathnames do
File.open(foo = string_or_pathname('foo'), 'w') { |f| f.write 'footext' }
exception = assert_raises(Errno::ENOTDIR) do
FileUtils.cp([foo], string_or_pathname('baz'))
end
assert_equal 'Not a directory - baz', exception.to_s
end
end
def test_cp_overwrites_dest_file
File.open('foo', 'w') { |f| f.write 'FOO' }
File.open('bar', 'w') { |f| f.write 'BAR' }
FileUtils.cp('foo', 'bar')
assert_equal 'FOO', File.read('bar')
end
def test_cp_fails_on_no_source
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
FileUtils.cp(string_or_pathname('foo'), string_or_pathname('baz'))
end
end
end
def test_file_utils_cp_allows_source_directories
Dir.mkdir 'foo'
FileUtils.cp 'foo', 'bar'
assert Dir.exist? 'bar'
end
def test_file_utils_cp_raises_error_on_nonexisting_target
FileUtils.touch('file.txt')
FileUtils.mkdir('bar')
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
FileUtils.cp(string_or_pathname('file.txt'), string_or_pathname('bar/nonexistent/file.txt'))
end
end
end
def test_copy_file_works
File.open('foo', 'w') { |f| f.write 'bar' }
FileUtils.copy_file('foo', 'baz', :ignore_param_1, :ignore_param_2)
assert_equal 'bar', File.read('baz')
end
def test_cp_r_doesnt_tangle_files_together
File.open('foo', 'w') { |f| f.write 'bar' }
FileUtils.cp_r('foo', 'baz')
File.open('baz', 'w') { |f| f.write 'quux' }
assert_equal 'bar', File.open('foo') { |f| f.read }
end
def test_cp_r_should_raise_error_on_missing_file
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
FileUtils.cp_r string_or_pathname('blafgag'), string_or_pathname('foo')
end
end
end
def test_cp_r_handles_copying_directories
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(subdir = string_or_pathname('subdir'))
Dir.chdir(subdir) { File.open(string_or_pathname('foo'), 'w') { |f| f.write 'footext' } }
FileUtils.mkdir_p(baz = string_or_pathname('baz'))
# To a previously uncreated directory
FileUtils.cp_r(subdir, string_or_pathname('quux'))
assert_equal 'footext', File.open(string_or_pathname('quux/foo')) { |f| f.read }
# To a directory that already exists
FileUtils.cp_r(subdir, baz)
assert_equal 'footext', File.open(string_or_pathname('baz/subdir/foo')) { |f| f.read }
# To a subdirectory of a directory that does not exist
assert_raises(Errno::ENOENT) do
FileUtils.cp_r(subdir, string_or_pathname('nope/something'))
end
FileUtils.rmtree(subdir)
FileUtils.rmtree(baz)
end
end
def test_cp_r_array_of_files
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(subdir = string_or_pathname('subdir'))
File.open(foo = string_or_pathname('foo'), 'w') { |f| f.write 'footext' }
File.open(bar = string_or_pathname('bar'), 'w') { |f| f.write 'bartext' }
FileUtils.cp_r([foo, bar], subdir)
assert_equal 'footext', File.open(string_or_pathname('subdir/foo')) { |f| f.read }
assert_equal 'bartext', File.open(string_or_pathname('subdir/bar')) { |f| f.read }
FileUtils.rmtree(subdir)
FileUtils.rm(foo)
FileUtils.rm(bar)
end
end
def test_cp_r_array_of_directories
perform_with_both_string_paths_and_pathnames do
[foo = string_or_pathname('foo'),
bar = string_or_pathname('bar'),
subdir = string_or_pathname('subdir')].each { |d| FileUtils.mkdir_p d }
File.open(string_or_pathname('foo/baz'), 'w') { |f| f.write 'baztext' }
File.open(string_or_pathname('bar/quux'), 'w') { |f| f.write 'quuxtext' }
FileUtils.cp_r([foo, bar], subdir)
assert_equal 'baztext', File.open(string_or_pathname('subdir/foo/baz')) { |f| f.read }
assert_equal 'quuxtext', File.open(string_or_pathname('subdir/bar/quux')) { |f| f.read }
FileUtils.rmtree(foo)
FileUtils.rmtree(bar)
FileUtils.rmtree(subdir)
end
end
def test_cp_r_only_copies_into_directories
FileUtils.mkdir_p 'subdir'
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
File.open('bar', 'w') { |f| f.write 'bartext' }
assert_raises(Errno::EEXIST) do
FileUtils.cp_r 'subdir', 'bar'
end
FileUtils.mkdir_p 'otherdir'
FileUtils.ln_s 'otherdir', 'symdir'
FileUtils.cp_r 'subdir', 'symdir'
assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
end
def test_cp_r_sets_parent_correctly
FileUtils.mkdir_p '/path/foo'
File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
FileUtils.cp_r '/path/foo', '/path/bar'
assert File.exist?('/path/bar/baz')
FileUtils.rm_rf '/path/bar/baz'
assert_equal ['/path/bar/bar'], Dir['/path/bar/*']
end
def test_cp_r_updates_time_unless_preserve_is_false
[
{ preserve: nil, assertion: method(:refute_equal) },
{ preserve: true, assertion: method(:assert_equal) },
{ preserve: false, assertion: method(:refute_equal) }
].each do |test|
test => { preserve:, assertion: }
FakeFS.with_fresh do
FileUtils.mkdir_p '/source/subdir'
File.write('/source/foo', 'foo')
File.write('/source/subdir/subfile', 'subfile')
FileUtils.mkdir '/dest'
File.write('/dest/baz', 'baz')
atime = File.atime('/dest/baz')
mtime = File.mtime('/dest/baz')
sleep 0.001
FileUtils.cp_r '/source/.', '/dest', { preserve: preserve }
['foo', 'subdir/subfile'].each do |filename|
source_name = File.join '/source', filename
dest_name = File.join '/dest', filename
assertion.call File.atime(source_name), File.atime(dest_name), "atime for #{filename} with preserve=#{test[:preserve]}"
assertion.call File.mtime(source_name), File.mtime(dest_name), "mtime for #{filename} with preserve=#{test[:preserve]}"
end
# Check that pre-existing files are not changed:
assert_equal atime, File.atime('/dest/baz')
assert_equal mtime, File.mtime('/dest/baz')
end
end
end
def test_clone_clones_normal_files
act_on_real_fs do
File.open(real_file_sandbox('foo'), 'w') do |f|
f.write 'bar'
end
assert RealFile.file?(real_file_sandbox('foo'))
assert File.file?(real_file_sandbox('foo'))
end
assert RealFile.file?(real_file_sandbox('foo'))
refute File.exist?(real_file_sandbox('foo'))
FakeFS::FileSystem.clone(real_file_sandbox('foo'))
assert_equal 'bar', File.open(real_file_sandbox('foo')) { |f| f.read }
end
def test_clone_clones_directories
act_on_real_fs { FileUtils.mkdir_p(real_file_sandbox('subdir')) }
FakeFS::FileSystem.clone(real_file_sandbox('subdir'))
assert File.exist?(real_file_sandbox('subdir')), 'subdir was cloned'
assert File.directory?(real_file_sandbox('subdir')), 'subdir is a directory'
end
def test_clone_clones_dot_files_even_hard_to_find_ones
act_on_real_fs { FileUtils.mkdir_p(real_file_sandbox('subdir/.bar/baz/.quux/foo')) }
refute File.exist?(real_file_sandbox('subdir'))
FakeFS::FileSystem.clone(real_file_sandbox('subdir'))
assert_equal ['.', '..', '.bar'], Dir.entries(real_file_sandbox('subdir'))
assert_equal ['.', '..', 'foo'], Dir.entries(real_file_sandbox('subdir/.bar/baz/.quux'))
end
def test_dir_glob_on_clone_with_absolute_path
act_on_real_fs { FileUtils.mkdir_p(real_file_sandbox('subdir/.bar/baz/.quux/foo')) }
FileUtils.mkdir_p '/path'
Dir.chdir('/path')
FakeFS::FileSystem.clone(real_file_sandbox('subdir'), '/foo')
assert Dir.glob '/foo/*'
end
def test_clone_with_target_specified
act_on_real_fs do
assert FileUtils == RealFileUtils, 'using the real FileUtils in act_on_real_fs'
FileUtils.mkdir_p(real_file_sandbox('subdir/.bar/baz/.quux/foo'))
end
refute File.exist?(real_file_sandbox('subdir'))
FakeFS::FileSystem.clone(real_file_sandbox('subdir'), real_file_sandbox('subdir2'))
refute File.exist?(real_file_sandbox('subdir'))
assert_equal ['.', '..', '.bar'], Dir.entries(real_file_sandbox('subdir2'))
assert_equal ['.', '..', 'foo'], Dir.entries(real_file_sandbox('subdir2/.bar/baz/.quux'))
end
def test_clone_with_file_symlinks
original = real_file_sandbox('subdir/test-file')
symlink = real_file_sandbox('subdir/test-file.txt')
act_on_real_fs do
FileUtils.mkdir_p(File.dirname(original))
File.open(original, 'w') { |f| f << 'stuff' }
FileUtils.ln_s original, symlink
assert File.symlink?(symlink), 'real symlink is in place'
end
refute File.exist?(original), 'file does not already exist'
FakeFS::FileSystem.clone(File.dirname(original))
assert File.symlink?(symlink), 'symlinks are cloned as symlinks'
assert_equal 'stuff', File.read(symlink)
end
def test_clone_with_dir_symlinks
original = real_file_sandbox('subdir/dir')
symlink = real_file_sandbox('subdir/dir.link')
original_file = File.join(original, 'test-file')
symlink_file = File.join(symlink, 'test-file')
act_on_real_fs do
FileUtils.mkdir_p(original)
File.open(original_file, 'w') { |f| f << 'stuff' }
FileUtils.ln_s original, symlink
assert File.symlink?(symlink), 'real symlink is in place'
end
refute File.exist?(original_file), 'file does not already exist'
FakeFS::FileSystem.clone(File.dirname(original))
assert File.symlink?(symlink), 'symlinks are cloned as symlinks'
assert_equal 'stuff', File.read(symlink_file)
end
def test_putting_a_dot_at_end_copies_the_contents
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(subdir = string_or_pathname('subdir'))
Dir.chdir(subdir) { File.open(string_or_pathname('foo'), 'w') { |f| f.write 'footext' } }
FileUtils.mkdir_p(newdir = string_or_pathname('newdir'))
FileUtils.cp_r(string_or_pathname('subdir/.'), newdir)
assert_equal 'footext', File.open(string_or_pathname('newdir/foo')) { |f| f.read }
FileUtils.rmtree subdir
FileUtils.rmtree newdir
end
end
def test_file_can_read_from_symlinks
perform_with_both_string_paths_and_pathnames do
File.open(first = string_or_pathname('first'), 'w') { |f| f.write '1' }
FileUtils.ln_s(first, one = string_or_pathname('one'))
assert_equal '1', File.open(one) { |f| f.read }
FileUtils.mkdir_p(subdir = string_or_pathname('subdir'))
File.open(string_or_pathname('subdir/nother'), 'w') { |f| f.write 'works' }
FileUtils.ln_s(subdir, link = string_or_pathname('new'))
assert_equal 'works', File.open(string_or_pathname('new/nother')) { |f| f.read }
FileUtils.rm(link)
FileUtils.rmtree(subdir)
FileUtils.rm(one)
FileUtils.rm(first)
end
end
def test_can_symlink_through_file
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(foo = string_or_pathname('/foo'))
File.symlink(foo, bar = string_or_pathname('/bar'))
assert File.symlink?(bar)
FileUtils.rm(foo)
FileUtils.rm(bar)
end
end
def test_files_can_be_touched
FileUtils.touch('touched_file')
assert File.exist?('touched_file')
list = ['newfile', 'another']
FileUtils.touch(list)
list.each { |fp| assert(File.exist?(fp)) }
end
def test_touch_does_not_work_if_the_dir_path_cannot_be_found
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
FileUtils.touch(string_or_pathname('this/path/should/not/be/here'))
end
FileUtils.mkdir_p(string_or_pathname('subdir'))
list = [string_or_pathname('subdir/foo'), string_or_pathname('nosubdir/bar')]
assert_raises(Errno::ENOENT) do
FileUtils.touch(list)
end
end
end
def test_extname
perform_with_both_string_paths_and_pathnames do
assert File.extname(string_or_pathname('test.doc')) == '.doc'
end
end
# Directory tests
def test_new_directory
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/this/path/should/be/here'))
# nothing raised
Dir.new(path)
FileUtils.rmtree(path)
end
end
def test_new_directory_does_not_work_if_dir_path_cannot_be_found
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
Dir.new(string_or_pathname('/this/path/should/not/be/here'))
end
end
end
def test_directory_close
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/this/path/should/be/here'))
dir = Dir.new(path)
assert dir.close.nil?
assert_raises(IOError) do
dir.each { |_| }
end
end
end
def test_directory_each_with_block
files = ['.', '..', 'file_1', 'file_2', 'file_3']
dir_path = '/this/path/should/be/here'
FileUtils.mkdir_p(dir_path)
files.each { |f| FileUtils.touch("#{dir_path}/#{f}") }
dir = Dir.new(dir_path)
yielded = []
dir.each { |d| yielded << d }
assert yielded.sort == files.sort
end
def test_directory_each_without_block
files = ['.', '..', 'file_1', 'file_2', 'file_3']
dir_path = '/this/path/should/be/here'
FileUtils.mkdir_p(dir_path)
files.each { |f| FileUtils.touch("#{dir_path}/#{f}") }
dir = Dir.new(dir_path)
each = dir.each
assert_kind_of Enumerator, each
assert each.to_a.sort == files.sort
end
def test_directory_path
FileUtils.mkdir_p('/this/path/should/be/here')
good_path = '/this/path/should/be/here'
assert_equal good_path, Dir.new('/this/path/should/be/here').path
end
def test_directory_pos
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.new('/this/path/should/be/here')
assert dir.pos == 0
dir.read
assert dir.pos == 1
dir.read
assert dir.pos == 2
dir.read
assert dir.pos == 3
dir.read
assert dir.pos == 4
dir.read
assert dir.pos == 5
end
def test_directory_pos_assign
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.new('/this/path/should/be/here')
assert dir.pos == 0
dir.pos = 2
assert dir.pos == 2
end
def test_directory_read
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.new('/this/path/should/be/here')
assert dir.pos == 0
d = dir.read
assert dir.pos == 1
assert d == '.'
d = dir.read
assert dir.pos == 2
assert d == '..'
end
def test_directory_read_past_length
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.new('/this/path/should/be/here')
d = dir.read
refute_nil d
d = dir.read
refute_nil d
d = dir.read
refute_nil d
d = dir.read
refute_nil d
d = dir.read
refute_nil d
d = dir.read
refute_nil d
d = dir.read
refute_nil d
d = dir.read
assert_nil d
end
def test_directory_rewind
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.new('/this/path/should/be/here')
dir.read
dir.read
assert dir.pos == 2
dir.rewind
assert dir.pos == 0
end
def test_directory_seek
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.new('/this/path/should/be/here')
d = dir.seek 1
assert d == '..'
assert dir.pos == 1
end
def test_directory_class_delete
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/this/path/should/be/here'))
Dir.delete(path)
assert File.exist?(path) == false
end
end
def test_directory_class_delete_does_not_act_on_non_empty_directory
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
perform_with_both_string_paths_and_pathnames do
FileUtils.mkdir_p(path = string_or_pathname('/this/path/should/be/here'))
test.each do |f|
FileUtils.touch(string_or_pathname("/this/path/should/be/here/#{f}"))
end
assert_raises(Errno::ENOTEMPTY) do
Dir.delete(path)
end
FileUtils.rmtree(path)
end
end
def test_directory_class_delete_does_not_work_if_dir_path_cannot_be_found
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
Dir.delete(string_or_pathname('/this/path/should/not/be/here'))
end
end
end
def test_directory_entries
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
yielded = Dir.entries('/this/path/should/be/here')
assert yielded.size == test.size
test.each { |t| assert yielded.include?(t) }
end
def test_directory_children
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
test_with_files_only = test - ['.', '..']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
yielded = Dir.children('/this/path/should/be/here')
assert yielded.size == test_with_files_only.size
test_with_files_only.each { |t| assert yielded.include?(t) }
end
def test_directory_entries_works_with_trailing_slash
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
yielded = Dir.entries('/this/path/should/be/here/')
assert yielded.size == test.size
test.each { |t| assert yielded.include?(t) }
end
def test_directory_entries_does_not_work_if_dir_path_cannot_be_found
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
Dir.delete(string_or_pathname('/this/path/should/not/be/here'))
end
end
end
def test_directory_foreach
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
test_with_files_only = test - ['.', '..']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
yielded = []
Dir.each_child('/this/path/should/be/here') do |dir|
yielded << dir
end
assert yielded.size == test_with_files_only.size
test_with_files_only.each { |t| assert yielded.include?(t) }
end
def test_directory_each_child
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
yielded = []
Dir.foreach('/this/path/should/be/here') do |dir|
yielded << dir
end
assert yielded.size == test.size
test.each { |t| assert yielded.include?(t) }
end
def test_directory_foreach_relative_paths
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
yielded = []
Dir.chdir '/this/path/should/be' do
Dir.foreach('here') do |dir|
yielded << dir
end
end
assert yielded.size == test.size, 'wrong number of files yielded'
test.each { |t| assert yielded.include?(t), "#{t} was not included in #{yielded.inspect}" }
end
def test_directory_mkdir
Dir.mkdir('/path')
assert File.exist?('/path')
end
def test_directory_mkdir_nested
Dir.mkdir('/tmp/stream20120103-11847-xc8pb.lock')
assert File.exist?('/tmp/stream20120103-11847-xc8pb.lock')
end
def test_can_create_subdirectories_with_dir_mkdir
Dir.mkdir 'foo'
Dir.mkdir 'foo/bar'
assert Dir.exist?('foo/bar')
end
def test_can_create_absolute_subdirectories_with_dir_mkdir
Dir.mkdir '/foo'
Dir.mkdir '/foo/bar'
assert Dir.exist?('/foo/bar')
end
def test_can_create_directories_starting_with_dot
Dir.mkdir './path'
assert File.exist? './path'
end
def test_can_create_directories_with_brackets
# test various combinations of directories with brackets
dir = '/[dir'
Dir.mkdir dir
assert Dir.exist?(dir)
dir = '/]dir'
Dir.mkdir dir
assert Dir.exist?(dir)
dir = '/[dir]'
Dir.mkdir dir
assert Dir.exist?(dir)
dir = '/di][r'
Dir.mkdir dir
assert Dir.exist?(dir)
dir = '/[[][][][][[[[]][[[[[[]]]'
Dir.mkdir dir
assert Dir.exist?(dir)
end
def test_directory_mkdir_relative
FileUtils.mkdir_p('/new/root')
FakeFS::FileSystem.chdir('/new/root')
Dir.mkdir('path')
assert File.exist?('/new/root/path')
end
def test_directory_mkdir_not_recursive
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
Dir.mkdir(string_or_pathname('/path/does/not/exist'))
end
end
end
def test_mkdir_raises_error_if_already_created
perform_with_both_string_paths_and_pathnames do
Dir.mkdir(foo = string_or_pathname('foo'))
assert_raises(Errno::EEXIST) do
Dir.mkdir foo
end
FileUtils.rmdir(foo)
end
end
def test_mkdir_p_of_root_does_not_cause_faulty_entries
FileUtils.mkdir_p '/'
refute_includes FakeFS::FileSystem.fs.entries.map(&:name), nil
end
def test_directory_open
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
dir = Dir.open('/this/path/should/be/here')
assert dir.path == '/this/path/should/be/here'
end
def test_directory_open_block
test = ['file_1', 'file_2', 'file_3', 'file_4', 'file_5']
FileUtils.mkdir_p('/this/path/should/be/here')
test.each do |f|
FileUtils.touch("/this/path/should/be/here/#{f}")
end
children = Dir.open('/this/path/should/be/here') do |dir|
dir.children
end
assert children.size == test.size
test.each { |t| assert children.include?(t) }
end
def test_directory_exists
assert Dir.exist?('/this/path/should/be/here') == false
assert Dir.exist?('/this/path/should/be/here') == false
FileUtils.mkdir_p('/this/path/should/be/here')
assert Dir.exist?('/this/path/should/be/here') == true
assert Dir.exist?('/this/path/should/be/here') == true
end
def test_tmpdir
assert Dir.tmpdir == '/tmp'
end
def test_rename_renames_a_file
perform_with_both_string_paths_and_pathnames do
File.write(string_or_pathname('/foo'), 'content')
File.rename(string_or_pathname('/foo'), string_or_pathname('/bar'))
assert File.file?(string_or_pathname('/bar'))
refute File.file?(string_or_pathname('/foo'))
assert_equal 'content', File.read(string_or_pathname('/bar'))
FileUtils.rm('/bar')
end
end
def test_rename_renames_a_symlink
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(path = string_or_pathname('/file'))
File.symlink(path, link = string_or_pathname('/symlink'))
assert_equal File.readlink(link), path
File.rename(link, link2 = string_or_pathname('/symlink2'))
assert_equal File.readlink(link2), path
FileUtils.rm(link2)
FileUtils.rm(path)
end
end
def test_rename_returns
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('/foo'))
assert_equal 0, File.rename(string_or_pathname('/foo'), string_or_pathname('/bar'))
FileUtils.rm('/bar')
end
end
def test_rename_renames_two_files
File.write('/foo', "foo")
File.write('/bar', "bar")
File.rename('/foo', '/bar')
refute File.exist?('/foo')
assert_equal "foo", File.read('/bar')
assert File.file?('/bar')
end
def test_rename_file_to_itself
FileUtils.touch('/foo')
File.rename('/foo', '/foo')
assert File.exist?('/foo')
assert File.file?('/foo')
end
def test_rename_non_existing_file_to_itself_raises_error
assert_raises(Errno::ENOENT) do
File.rename('/foo', '/foo')
end
assert !File.exist?('/foo')
end
def test_readlink_non_existent_raises_ENOENT
assert_raises(Errno::ENOENT) do
File.readlink('does_not_exist')
end
end
def test_rename_renames_a_directories
Dir.mkdir('/foo')
File.write '/foo/file', 'content'
File.rename('/foo', '/bar')
assert File.directory?('/bar')
assert_equal 'content', File.read('/bar/file')
end
def test_rename_renames_two_directories
Dir.mkdir('/foo')
Dir.mkdir('/bar')
File.rename('/foo', '/bar')
assert File.directory?('/bar')
end
def test_rename_file_to_directory_raises_error
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(foo = string_or_pathname('/foo'))
Dir.mkdir(bar = string_or_pathname('/bar'))
assert_raises(Errno::EISDIR) do
File.rename(foo, bar)
end
FileUtils.rm(foo)
FileUtils.rmdir(bar)
end
end
def test_rename_directory_to_file_raises_error
perform_with_both_string_paths_and_pathnames do
Dir.mkdir(foo = string_or_pathname('/foo'))
FileUtils.touch(bar = string_or_pathname('/bar'))
assert_raises(Errno::ENOTDIR) do
File.rename(foo, bar)
end
FileUtils.rmdir(foo)
end
end
def test_rename_with_missing_source_raises_error
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
File.rename(string_or_pathname('/no_such_file'), string_or_pathname('/bar'))
end
end
end
def test_rename_with_missing_dest_directory_raises_error
perform_with_both_string_paths_and_pathnames do
FileUtils.touch(string_or_pathname('/foo'))
assert_raises(Errno::ENOENT) do
File.rename(string_or_pathname('/foo'), string_or_pathname('/bar/foo'))
end
end
end
def test_hard_link_creates_file
FileUtils.touch('/foo')
perform_with_both_string_paths_and_pathnames do
File.link(string_or_pathname('/foo'), string_or_pathname('/bar'))
assert File.exist?(bar = string_or_pathname('/bar'))
FileUtils.rm(bar)
end
end
def test_hard_link_with_missing_file_raises_error
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::ENOENT) do
File.link(string_or_pathname('/foo'), string_or_pathname('/bar'))
end
end
end
def test_hard_link_with_existing_destination_file
FileUtils.touch('/foo')
FileUtils.touch('/bar')
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::EEXIST) do
File.link(string_or_pathname('/foo'), string_or_pathname('/bar'))
end
end
end
def test_hard_link_returns_0_when_successful
FileUtils.touch('/foo')
assert_equal 0, File.link('/foo', '/bar')
end
def test_hard_link_returns_duplicate_file
File.open('/foo', 'w') { |x| x << 'some content' }
File.link('/foo', '/bar')
assert_equal 'some content', File.read('/bar')
end
def test_hard_link_with_directory_raises_error
Dir.mkdir '/foo'
perform_with_both_string_paths_and_pathnames do
assert_raises(Errno::EPERM) do
File.link(string_or_pathname('/foo'), string_or_pathname('/bar'))
end
end
end
def test_file_stat_returns_file_stat_object
FileUtils.touch('/foo')
assert_equal File::Stat, File.stat('/foo').class
end
def test_can_delete_file_with_delete
FileUtils.touch('/foo')
File.delete('/foo')
refute File.exist?('/foo')
end
def test_can_delete_multiple_files_with_delete
FileUtils.touch('/foo')
FileUtils.touch('/bar')
File.delete('/foo', '/bar')
refute File.exist?('/foo')
refute File.exist?('/bar')
end
def test_delete_returns_zero_when_no_filename_given
assert_equal 0, File.delete
end
def test_delete_returns_number_one_when_given_one_arg
FileUtils.touch('/foo')
assert_equal 1, File.delete('/foo')
end
def test_delete_returns_number_two_when_given_two_args
FileUtils.touch('/foo')
FileUtils.touch('/bar')
assert_equal 2, File.delete('/foo', '/bar')
end
def test_delete_raises_error_when_first_file_does_not_exist
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.delete(string_or_pathname('/foo'))
end
end
end
def test_unlink_removes_only_one_file_content
File.open('/foo', 'w') { |f| f << 'some_content' }
perform_with_both_string_paths_and_pathnames do
File.link(foo = string_or_pathname('/foo'), bar = string_or_pathname('/bar'))
File.unlink(bar)
assert_equal 'some_content', File.read(foo)
end
end
def test_link_reports_correct_stat_info_after_unlinking
File.open('/foo', 'w') { |f| f << 'some_content' }
File.link('/foo', '/bar')
File.unlink('/bar')
assert_equal 1, File.stat('/foo').nlink
end
def test_delete_works_with_symlink
FileUtils.touch('/foo')
File.symlink('/foo', '/bar')
File.unlink('/bar')
assert File.exist?('/foo')
refute File.exist?('/bar')
end
def test_delete_works_with_symlink_source
FileUtils.touch('/foo')
File.symlink('/foo', '/bar')
File.unlink('/foo')
refute File.exist?('/foo')
end
def test_file_seek_returns_0
File.open('/foo', 'w') do |f|
f << "one\ntwo\nthree"
end
file = File.open('/foo', 'r')
assert_equal 0, file.seek(1)
end
def test_file_seek_seeks_to_location
File.open('/foo', 'w') do |f|
f << '123'
end
file = File.open('/foo', 'r')
file.seek(1)
assert_equal '23', file.read
end
def test_file_seek_seeks_to_correct_location
File.open('/foo', 'w') do |f|
f << '123'
end
file = File.open('/foo', 'r')
file.seek(2)
assert_equal '3', file.read
end
def test_file_seek_can_take_negative_offset
File.open('/foo', 'w') do |f|
f << '123456789'
end
file = File.open('/foo', 'r')
file.seek(-1, IO::SEEK_END)
assert_equal '9', file.read
file.seek(-2, IO::SEEK_END)
assert_equal '89', file.read
file.seek(-3, IO::SEEK_END)
assert_equal '789', file.read
end
def test_should_have_constants_inherited_from_descending_from_io
assert_equal IO::SEEK_CUR, File::SEEK_CUR
assert_equal IO::SEEK_END, File::SEEK_END
assert_equal IO::SEEK_SET, File::SEEK_SET
end
def test_filetest_exists_return_correct_values
FileUtils.mkdir_p('/path/to/dir')
assert FileTest.exist?('/path/to/')
FileUtils.rmdir('/path/to/dir')
refute FileTest.exist?('/path/to/dir')
end
def test_filetest_executable_returns_correct_values
FileUtils.mkdir_p('/path/to')
path = '/path/to/file.txt'
File.open(path, 'w') { |f| f.write 'Yatta!' }
refute FileTest.executable?(path)
end
def test_filetest_directory_returns_correct_values
FileUtils.mkdir_p '/path/to/somedir'
assert FileTest.directory?('/path/to/somedir')
FileUtils.rm_r '/path/to/somedir'
refute FileTest.directory?('/path/to/somedir')
end
def test_filetest_file_returns_correct_values
FileUtils.mkdir_p('/path/to')
path = '/path/to/file.txt'
File.open(path, 'w') { |f| f.write 'Yatta!' }
assert FileTest.file?(path)
FileUtils.rm path
refute FileTest.file?(path)
FileUtils.mkdir_p '/path/to/somedir'
refute FileTest.file?('/path/to/somedir')
end
def test_filetest_sticky_returns_correct_values
FileUtils.mkdir_p('/path/to')
path = '/path/to/file.txt'
File.open(path, 'w') { |f| f.write 'Yatta!' }
refute FileTest.sticky?(path)
end
def test_filetest_symlink_returns_correct_values
src = '/path/to/dir'
dst = '/path/to/sym'
FileUtils.mkdir_p(src)
FileUtils.symlink src, dst
assert FileTest.symlink?(dst)
FileUtils.rm_r dst
FileUtils.mkdir_p dst
refute FileTest.symlink?(dst)
end
def test_filetest_world_readable_returns_correct_values
FileUtils.mkdir_p('/path/to')
path = '/path/to/file.txt'
File.open(path, 'w') { |f| f.write 'Yatta!' }
assert FileTest.world_readable?(path) == 0o777
end
def test_filetest_world_writable_returns_correct_values
FileUtils.mkdir_p('/path/to')
path = '/path/to/file.txt'
File.open(path, 'w') { |f| f.write 'Yatta!' }
assert FileTest.world_writable?(path) == 0o777
end
# NOTE: FileTest.readable? and FileTest.writable? are wrappers around File.readable? and
# File.writable? respectively. Thus, testing the FileTest versions of these functions will
# also test the File versions of these functions.
def test_filetest_readable_can_read_user_made_files
FileUtils.touch 'here.txt'
perform_with_both_string_paths_and_pathnames do
assert FileTest.readable?(string_or_pathname('here.txt')), 'files are readable'
FileUtils.mkdir(dir = string_or_pathname('dir'))
assert FileTest.readable?(dir), 'directories are readable'
FileUtils.rmdir(dir)
end
end
def test_filetest_properly_reports_readable_for_files_chmoded_000
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o000, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 000, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o000, file_name)
File.chown(nil, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 000, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o000, file_name)
File.chown(1234, nil, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 000, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o000, file_name)
File.chown(1234, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 000, different user, different group'
end
def test_filetest_properly_reports_readable_for_files_chmoded_400
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o400, file_name)
assert FileTest.readable?(file_name), 'files are readable with user read bit, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o400, file_name)
File.chown(nil, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with user read bit, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o400, file_name)
File.chown(1234, nil, file_name)
refute FileTest.readable?(file_name), 'files are readable with user read bit, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o400, file_name)
File.chown(1234, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with user read bit, different user, different group'
end
def test_filetest_properly_reports_readable_for_files_chmoded_440
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o440, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 440, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o440, file_name)
File.chown(nil, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 440, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o440, file_name)
File.chown(1234, nil, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 440, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o440, file_name)
File.chown(1234, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 440, different user, different group'
end
def test_filetest_properly_reports_readable_for_files_chmoded_444
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o444, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 444, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o444, file_name)
File.chown(nil, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 444, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o444, file_name)
File.chown(1234, nil, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 444, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o444, file_name)
File.chown(1234, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 444, different user, different group'
end
def test_filetest_properly_reports_readable_for_files_chmoded_040
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o040, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 040, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o040, file_name)
File.chown(nil, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 040, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o040, file_name)
File.chown(1234, nil, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 040, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o040, file_name)
File.chown(1234, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 040, different user, different group'
end
def test_filetest_properly_reports_readable_for_files_chmoded_044
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o044, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 044, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o044, file_name)
File.chown(nil, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 044, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o044, file_name)
File.chown(1234, nil, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 044, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o044, file_name)
File.chown(1234, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 044, different user, different group'
end
def test_filetest_properly_reports_readable_for_files_chmoded_004
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o004, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 004, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o004, file_name)
File.chown(nil, 1234, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 004, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o004, file_name)
File.chown(1234, nil, file_name)
refute FileTest.readable?(file_name), 'files are readable with chmod 004, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o004, file_name)
File.chown(1234, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 004, different user, different group'
end
def test_filetest_readable_returns_false_for_missing_files
refute FileTest.readable?('not-here.txt'), 'missing files are not readable'
refute FileTest.readable?('/no/such/dir'), 'missing directories are not readable'
end
# test a 'random' chmod value
def test_filetest_properly_reports_readable_for_files_chmoded_567
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o567, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 567, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o567, file_name)
File.chown(nil, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 567, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o567, file_name)
File.chown(1234, nil, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 567, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o567, file_name)
File.chown(1234, 1234, file_name)
assert FileTest.readable?(file_name), 'files are readable with chmod 567, different user, different group'
end
def test_filetest_writable_for_user_made_directories
FileUtils.touch 'file.txt'
perform_with_both_string_paths_and_pathnames do
assert FileTest.writable?(string_or_pathname('file.txt')), 'files are writable'
FileUtils.mkdir(dir = string_or_pathname('dir'))
assert FileTest.writable?(dir), 'directories are writable'
FileUtils.rmdir(dir)
end
end
# Since we've tested every possible chmod combination on files already,
# just test to make sure the bit is correct for write
def test_filetest_properly_reports_writable_for_files_chmoded_200
file_name = 'file1.txt'
FileUtils.touch file_name
File.chmod(0o200, file_name)
assert FileTest.writable?(file_name), 'files are writable with chmod 200, same user, same group'
file_name = 'file2.txt'
FileUtils.touch file_name
File.chmod(0o200, file_name)
File.chown(nil, 1234, file_name)
assert FileTest.writable?(file_name), 'files are writable with chmod 200, same user, different group'
file_name = 'file3.txt'
FileUtils.touch file_name
File.chmod(0o200, file_name)
File.chown(1234, nil, file_name)
refute FileTest.writable?(file_name), 'files are readable with chmod 200, different user, same group'
file_name = 'file4.txt'
FileUtils.touch file_name
File.chmod(0o200, file_name)
File.chown(1234, 1234, file_name)
refute FileTest.writable?(file_name), 'files are readable with chmod 200, different user, different group'
end
def test_filetest_writable_returns_false_for_missing_files
perform_with_both_string_paths_and_pathnames do
refute FileTest.writable?(string_or_pathname('not-here.txt')), 'missing files are not writable'
refute FileTest.writable?(string_or_pathname('/no/such/dir')), 'missing directories are not writable'
end
end
def test_filetest_zero_returns_correct_values
perform_with_both_string_paths_and_pathnames do
refute FileTest.zero?(string_or_pathname('/not/a/real/directory'))
filepath = string_or_pathname('here.txt')
FileUtils.touch filepath
assert FileTest.zero?(filepath)
File.write(filepath, 'content')
refute FileTest.zero?(filepath)
FileUtils.rm(filepath)
end
end
if RUBY_VERSION > '2.4'
def test_filetest_empty_returns_correct_values
refute FileTest.empty?('/not/a/real/directory')
filepath = 'here.txt'
FileUtils.touch filepath
assert FileTest.empty?(filepath)
File.write(filepath, 'content')
refute FileTest.empty?(filepath)
end
else
def test_filetest_empty_not_implemented
refute FileTest.respond_to?(:empty?)
end
end
def test_dir_mktmpdir
# FileUtils.mkdir '/tmpdir'
tmpdir = Dir.mktmpdir
assert File.directory?(tmpdir)
FileUtils.rm_r tmpdir
Dir.mktmpdir do |t|
tmpdir = t
assert File.directory?(t)
end
refute File.directory?(tmpdir)
end
def test_activating_returns_true
FakeFS.deactivate!
assert_equal true, FakeFS.activate!
end
def test_deactivating_returns_true
assert_equal true, FakeFS.deactivate!
end
def test_split
assert File.respond_to? :split
filename = '/this/is/what/we/expect.txt'
perform_with_both_string_paths_and_pathnames do
path, name = File.split(string_or_pathname(filename))
assert_equal path, '/this/is/what/we'
assert_equal name, 'expect.txt'
end
end
#########################
def test_file_default_mode
FileUtils.touch 'foo'
perform_with_both_string_paths_and_pathnames do
assert_equal File.stat(string_or_pathname('foo')).mode, (0o100000 + 0o666 - File.umask)
end
end
def test_dir_default_mode
Dir.mkdir 'bar'
assert_equal File.stat('bar').mode, (0o100000 + 0o777 - File.umask)
end
def test_file_default_uid_and_gid
FileUtils.touch 'foo'
assert_equal File.stat('foo').uid, Process.uid
assert_equal File.stat('foo').gid, Process.gid
end
def test_file_chmod_of_file
FileUtils.touch 'foo'
perform_with_both_string_paths_and_pathnames do
foo = string_or_pathname('foo')
File.chmod 0o600, foo
assert_equal File.stat(foo).mode, 0o100600
File.new(foo).chmod 0o644
assert_equal File.stat(foo).mode, 0o100644
end
end
def test_file_chmod_of_dir
Dir.mkdir 'bar'
perform_with_both_string_paths_and_pathnames do
bar = string_or_pathname('bar')
File.chmod 0o777, bar
assert_equal File.stat(bar).mode, 0o100777
File.new(bar).chmod 0o1700
assert_equal File.stat(bar).mode, 0o101700
end
end
def test_file_chown_of_file
FileUtils.touch 'foo'
perform_with_both_string_paths_and_pathnames do
foo = string_or_pathname('foo')
File.chown 1337, 1338, foo
assert_equal File.stat(foo).uid, 1337
assert_equal File.stat(foo).gid, 1338
end
end
def test_file_chown_of_dir
Dir.mkdir 'bar'
perform_with_both_string_paths_and_pathnames do
bar = string_or_pathname('bar')
File.chown 1337, 1338, bar
assert_equal File.stat(bar).uid, 1337
assert_equal File.stat(bar).gid, 1338
end
end
def test_file_chown_of_file_nil_user_group
FileUtils.touch 'foo'
File.chown 1337, 1338, 'foo'
File.chown nil, nil, 'foo'
assert_equal File.stat('foo').uid, 1337
assert_equal File.stat('foo').gid, 1338
end
def test_file_chown_of_file_negative_user_group
FileUtils.touch 'foo'
File.chown 1337, 1338, 'foo'
File.chown(-1, -1, 'foo')
assert_equal File.stat('foo').uid, 1337
assert_equal File.stat('foo').gid, 1338
end
def test_file_instance_chown_nil_user_group
FileUtils.touch('foo')
File.chown(1337, 1338, 'foo')
assert_equal File.stat('foo').uid, 1337
assert_equal File.stat('foo').gid, 1338
file = File.open('foo')
file.chown nil, nil
assert_equal File.stat('foo').uid, 1337
assert_equal File.stat('foo').gid, 1338
end
def test_file_instance_chown_negative_user_group
FileUtils.touch('foo')
File.chown(1337, 1338, 'foo')
assert_equal File.stat('foo').uid, 1337
assert_equal File.stat('foo').gid, 1338
file = File.new('foo')
file.chown(-1, -1)
file.close
assert_equal File.stat('foo').uid, 1337
assert_equal File.stat('foo').gid, 1338
end
def test_file_umask
assert_equal File.umask, RealFile.umask
File.umask(0o740)
assert_equal File.umask, RealFile.umask
assert_equal File.umask, 0o740
end
def test_file_stat_comparable
a_time = Time.new
same1 = File.new('s1', 'w')
same2 = File.new('s2', 'w')
different1 = File.new('d1', 'w')
different2 = File.new('d2', 'w')
FakeFS::FileSystem.find('s1').mtime = a_time
FakeFS::FileSystem.find('s2').mtime = a_time
FakeFS::FileSystem.find('d1').mtime = a_time
FakeFS::FileSystem.find('d2').mtime = a_time + 1
assert same1.mtime == same2.mtime
assert different1.mtime != different2.mtime
assert same1.stat == same2.stat
assert((same1.stat <=> same2.stat) == 0)
assert different1.stat != different2.stat
assert((different1.stat <=> different2.stat) == -1)
end
def test_file_binread_works
File.open('testfile', 'w') do |f|
f << "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
end
perform_with_both_string_paths_and_pathnames do
assert_equal File.binread(string_or_pathname('testfile')), "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
assert_equal File.binread(string_or_pathname('testfile'), 20), "This is line one\nThi"
assert_equal File.binread(string_or_pathname('testfile'), 20, 10), "ne one\nThis is line "
end
end
def test_file_binwrite_write_content_in_file
perform_with_both_string_paths_and_pathnames do
target = string_or_pathname('testfile')
::File.binwrite(target, "This is line one\nThis is line two\nThis is line three\nAnd so on...\n")
assert_equal File.binread(target), "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
end
end
def test_file_binwrite_returns_the_length_written
assert_equal File.binwrite('testfile', '0123456789'), 10
end
def test_file_binwrite_truncate_if_offset_is_not_given
perform_with_both_string_paths_and_pathnames do
target = string_or_pathname('testfile')
File.write(target, 'foobar')
File.binwrite(target, 'baz')
assert_equal File.binread(target), 'baz'
end
end
def test_file_binwrite_truncate_if_offset_is_nil
perform_with_both_string_paths_and_pathnames do
target = string_or_pathname('testfile')
File.write(target, 'foo')
File.binwrite(target, 'bar', nil)
assert_equal File.binread(target), 'bar'
end
end
def test_file_binwrite_writes_at_offset_and_does_not_truncate
perform_with_both_string_paths_and_pathnames do
target = string_or_pathname('testfile')
File.write(target, 'foo')
File.binwrite(target, 'bar', 3)
assert_equal File.binread(target), 'foobar'
File.binwrite(target, 'baz', 3)
assert_equal File.binread(target), 'foobaz'
end
end
def test_file_utils_compare_file
file1 = 'file1.txt'
file2 = 'file2.txt'
file3 = 'file3.txt'
content = "This is my \n file\content\n"
File.open(file1, 'w') do |f|
f.write content
end
File.open(file3, 'w') do |f|
f.write "#{content} with additional content"
end
FileUtils.cp file1, file2
perform_with_both_string_paths_and_pathnames do
path1 = string_or_pathname(file1)
path2 = string_or_pathname(file2)
path3 = string_or_pathname(file3)
assert_equal FileUtils.compare_file(path1, path2), true
assert_equal FileUtils.compare_file(path1, path3), false
assert_raises Errno::ENOENT do
FileUtils.compare_file(path1, string_or_pathname('file4.txt'))
end
end
end
def test_file_utils_uptodate
perform_with_both_string_paths_and_pathnames do
old_file = string_or_pathname('old.txt')
new_file = string_or_pathname('new.txt')
newer_file = string_or_pathname('newer.txt')
FileUtils.touch(old_file)
assert_equal FileUtils.uptodate?(new_file, [old_file]), false
FileUtils.touch(new_file)
assert_equal FileUtils.uptodate?(new_file, [old_file]), true
FileUtils.touch(newer_file)
assert_equal FileUtils.uptodate?(new_file, [old_file, newer_file]), false
assert_equal FileUtils.uptodate?(newer_file, [new_file, old_file]), true
FileUtils.rm(old_file)
FileUtils.rm(new_file)
FileUtils.rm(newer_file)
end
end
def test_fnmatch
assert_equal File.fnmatch?('test', 'test'), true
assert_equal File.fnmatch('nope', 'blargh'), false
assert_equal File.fnmatch?('nope', 'blargh'), File.fnmatch('nope', 'blargh')
end
def test_absolute_path_with_absolute_path
perform_with_both_string_paths_and_pathnames do
assert_equal '/foo/bar', File.absolute_path(string_or_pathname('/foo/bar'))
end
end
def test_absolute_path_with_absolute_path_with_dir_name
assert_equal '/foo/bar', File.absolute_path('/foo/bar', '/dir')
end
def test_absolute_path_with_relative_path
assert_equal "#{Dir.getwd}foo/bar", File.absolute_path('foo/bar')
end
def test_absolute_path_with_relative_path_with_dir_name
assert_equal '/dir/foo/bar', File.absolute_path('foo/bar', '/dir')
end
def test_file_size
File.open('foo', 'w') do |f|
f << 'Yada Yada'
assert_equal 9, f.size
end
end
def test_fdatasync
File.open('foo', 'w') do |f|
f << 'Yada Yada'
# nothing raised
f.fdatasync
end
end
def test_autoclose
File.open('foo', 'w') do |f|
assert_equal true, f.autoclose?
f.autoclose = false
assert_equal false, f.autoclose?
end
end
def test_binmode
File.open('foo', 'w') do |f|
assert_equal false, f.binmode?
end
File.open('foo', 'wb') do |f|
assert_equal true, f.binmode?
end
File.open('foo', 'w:binary') do |f|
assert_equal false, f.binmode?
end
File.open('foo', 'w', binmode: true) do |f|
assert_equal true, f.binmode?
end
File.open('foo', File::WRONLY | File::CREAT | File::TRUNC, binmode: true) do |f|
assert_equal true, f.binmode?
end
end
def test_exclusive_works
File.open('exclusive', 'wx')
FileUtils.rm('exclusive')
File.open('exclusive', File::EXCL | File::WRONLY | File::CREAT)
FileUtils.rm('exclusive')
File.open('exclusive', File::EXCL | File::CREAT)
FileUtils.rm('exclusive')
# Reading newly created file is nonsense
assert_raises(ArgumentError) do
File.open('exclusive', 'rx', flags: File::CREAT)
end
# Same nonsense, but no validation here
File.open('exclusive', File::EXCL | File::RDONLY | File::CREAT)
FileUtils.rm('exclusive')
# Truncating is nonsense too, but it's allowed
File.open('exclusive', File::EXCL | File::WRONLY | File::CREAT | File::TRUNC)
FileUtils.rm('exclusive')
# Same as appending
File.open('exclusive', File::EXCL | File::WRONLY | File::CREAT | File::APPEND)
FileUtils.rm('exclusive')
assert_raises(ArgumentError) do
File.open('exclusive', 'x')
end
assert_raises(ArgumentError) do
File.open('exclusive', 'rx')
end
end
def test_exclusive_does_not_do_sanity_check
assert_raises(Errno::ENOENT) do
File.open('exclusive', flags: File::EXCL)
end
assert_raises(Errno::ENOENT) do
# Nonsence without File::CREAT
File.open('exclusive', File::EXCL)
end
assert_raises(Errno::ENOENT) do
File.open('exclusive', File::RDONLY, flags: File::EXCL)
end
assert_raises(Errno::ENOENT) do
File.open('exclusive', 'r', flags: File::EXCL)
end
end
def test_exclusive_checks_for_file_existence
FileUtils.touch('exclusive')
assert_raises(Errno::EEXIST) do
File.open('exclusive', 'w', flags: File::EXCL)
end
assert_raises(Errno::EEXIST) do
File.open('exclusive', 'wx')
end
assert_raises(Errno::EEXIST) do
File.open('exclusive', File::WRONLY | File::EXCL | File::CREAT)
end
end
def test_open_encoding
File.open('foo', 'w:UTF-32LE') do |f|
assert_equal 8, f.write('hi')
end
File.open('foo') do |f|
assert_equal "h\u0000\u0000\u0000i\u0000\u0000\u0000", f.read
end
File.open('foo', 'rb:UTF-32LE') do |f|
assert_equal 'hi'.encode('UTF-32LE'), f.read
end
File.open('foo', 'r:UTF-32LE', binmode: true) do |f|
assert_equal 'hi'.encode('UTF-32LE'), f.read
end
skip 'Encoding is not supported natively by StringIO' if RUBY_PLATFORM == "java"
assert_raises ArgumentError do
# ASCII incompatible encoding needs binmode
File.open('foo', 'r:UTF-32LE')
end
end
def test_open_encoding_kw
File.open('foo', 'w', encoding: Encoding::UTF_32LE) do |f|
assert_equal 8, f.write('hi')
end
File.open('foo') do |f|
assert_equal "h\u0000\u0000\u0000i\u0000\u0000\u0000", f.read
end
File.open('foo', 'rb', encoding: Encoding::UTF_32LE) do |f|
assert_equal 'hi'.encode(Encoding::UTF_32LE), f.read
end
File.open('foo', binmode: true, encoding: Encoding::UTF_32LE) do |f|
assert_equal 'hi'.encode(Encoding::UTF_32LE), f.read
end
skip 'Encoding is not supported natively by StringIO' if RUBY_PLATFORM == "java"
assert_raises ArgumentError do
# ASCII incompatible encoding needs binmode
File.open('foo', 'r', encoding: Encoding::UTF_32LE)
end
end
def test_read_write_encoding
assert_equal 8, File.write('foo', 'hi', mode: 'w:UTF-32LE')
assert_equal "h\u0000\u0000\u0000i\u0000\u0000\u0000", File.read('foo')
assert_equal 'hi'.encode('UTF-32LE'), File.read('foo', mode: 'rb:UTF-32LE')
assert_equal 'hi'.encode('UTF-32LE'), File.read('foo', binmode: true, mode: 'r:UTF-32LE')
skip 'Encoding is not supported natively by StringIO' if RUBY_PLATFORM == "java"
assert_raises ArgumentError do
File.read('foo', mode: 'r:UTF-32LE')
end
end
def test_read_write_encoding_kw
assert_equal 8, File.write('foo', 'hi', mode: 'w', encoding: Encoding::UTF_32LE)
assert_equal "h\u0000\u0000\u0000i\u0000\u0000\u0000", File.read('foo')
assert_equal 'hi'.encode('UTF-32LE'), File.read('foo', mode: 'rb', encoding: Encoding::UTF_32LE)
assert_equal 'hi'.encode('UTF-32LE'), File.read('foo', binmode: true, encoding: Encoding::UTF_32LE)
skip 'Encoding is not supported natively by StringIO' if RUBY_PLATFORM == "java"
assert_raises ArgumentError do
File.read('foo', encoding: Encoding::UTF_32LE)
end
end
def test_to_path
File.open('foo', 'w') do |f|
assert_equal 'foo', f.to_path
end
end
def test_advise
File.open('foo', 'w') do |f|
# nothing raised
f.advise(:normal, 0, 0)
end
end
def test_file_read_respects_hashes
path = 'file.txt'
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
assert_equal 'ASCII-8BIT', File.read(path, mode: 'rb').encoding.to_s
end
def test_file_read_respects_args_and_hashes
path = 'file.txt'
File.open(path, 'w') do |f|
f.write 'Yatta!'
end
result = File.read(path, 2, 1, mode: 'rb')
assert_equal 'at', result
assert_equal 'ASCII-8BIT', result.encoding.to_s
end
def test_file_write_can_write_a_file
File.write('testfile', '0123456789')
assert_equal File.read('testfile'), '0123456789'
end
def test_file_write_returns_the_length_written
assert_equal File.write('testfile', '0123456789'), 10
end
def test_file_write_truncates_file_if_offset_not_given
File.open('foo', 'w') do |f|
f << 'foo'
end
File.write('foo', 'bar')
assert_equal File.read('foo'), 'bar'
end
def test_file_write_writes_at_offset_and_does_not_truncate
File.open('foo', 'w') do |f|
f << 'foo'
end
File.write('foo', 'bar', 3)
assert_equal File.read('foo'), 'foobar'
File.write('foo', 'baz', 3)
assert_equal File.read('foo'), 'foobaz'
end
def test_can_read_binary_data_in_binary_mode
File.open('foo', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00".dup.force_encoding('ASCII-8BIT'), File.open('foo', 'rb').read
end
def test_can_read_binary_data_in_non_binary_mode
File.open('foo_non_bin', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00".dup.force_encoding('UTF-8'), File.open('foo_non_bin', 'r').read
end
def test_can_read_binary_data_using_binread
File.open('foo', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00".dup.force_encoding('ASCII-8BIT'), File.binread('foo')
end
def test_can_use_readpartial
::File.open('foo', 'wb+') do |f|
assert_equal '', f.readpartial(0)
f << 'abcde'
assert_equal '', f.readpartial(0)
assert_raises(::EOFError) { f.readpartial(1) }
f.seek(0)
assert_equal '', f.readpartial(0)
assert_equal 'a', f.readpartial(1)
assert_equal 'bc', f.readpartial(2)
assert_equal 'de', f.readpartial(3)
assert_raises(::EOFError) { f.readpartial(1) }
end
end
def test_raises_error_on_birthtime_if_file_does_not_exist
perform_with_both_string_paths_and_pathnames do
assert_raises Errno::ENOENT do
File.birthtime(string_or_pathname('file.txt'))
end
end
end
def test_can_return_birthtime_on_existing_file
File.open('foo', 'w') { |f| f << 'some content' }
perform_with_both_string_paths_and_pathnames do
assert File.birthtime(string_or_pathname('foo')).is_a?(Time)
end
end
def test_file_birthtime_is_equal_to_file_stat_birthtime
File.open('foo', 'w') { |f| f << 'some content' }
assert_equal File.stat('foo').birthtime, File.birthtime('foo')
end
def test_remove_entry_secure_removes_files
perform_with_both_string_paths_and_pathnames do
foo = string_or_pathname('foo')
File.open(foo, 'w') { |f| f << 'some content' }
FileUtils.remove_entry_secure(foo, false)
assert File.exist?(foo) == false
File.open(foo, 'w') { |f| f << 'some content' }
FileUtils.remove_entry_secure(foo, true)
assert File.exist?(foo) == false
end
end
def test_properly_calculates_ino_for_files
# sanitize our testing environment
FakeFS::FakeInode.clear_inode_info_for_tests
# make sure that inodes are assigned starting from 0 in ascending order
file_name = 'file1'
File.open(file_name, 'w') { |f| f << 'some content' }
assert File.stat(file_name).ino == 0
file_name = 'file2'
File.open(file_name, 'w') { |f| f << 'some content' }
assert File.stat(file_name).ino == 1
# make sure any deleted inodes are reused
file_name = 'file1'
deleted_ino = File.stat(file_name).ino
File.delete(file_name)
file_name = 'file3'
File.open(file_name, 'w') { |f| f << 'some content' }
assert File.stat(file_name).ino == deleted_ino
# and that the next largest inode number is picked if we are out of
# unused, deleted inodes
file_name = 'file4'
File.open(file_name, 'w') { |f| f << 'some content' }
assert File.stat(file_name).ino == 2
# make sure moved files retain their existing inodes
file_name = 'file3'
move_file_name = 'file3_mv'
old_ino = File.stat(file_name).ino
FileUtils.mv(file_name, move_file_name)
assert File.stat(move_file_name).ino == old_ino
# but that copied ones do not
file_name = 'file2'
copy_file = 'file2_cp'
FileUtils.cp(file_name, copy_file)
assert File.stat(copy_file).ino == 3
# and finally that symlinks have the same inode as what they link to
# NOTE: viewing files with `ls -il` will show that a symlink has a different
# inode value than what it is pointing to. However, testing on a file made via
# ruby's `ln_s` method will show that the inode of a symlink and what it is
# pointing to is identical, hence I am testing for equality
file_name = 'file4'
sym_link = 'file4_symlink'
FileUtils.ln_s(file_name, sym_link)
assert File.stat(sym_link).ino == File.stat(file_name).ino
end
def test_properly_calculates_ino_for_directories
# sanitize our testing environment
FakeFS::FakeInode.clear_inode_info_for_tests
# make sure that inodes are assigned starting from 0 in ascending order
dir_name = 'dir1'
Dir.mkdir dir_name
assert File.stat(dir_name).ino == 0
dir_name = 'dir2'
FileUtils.mkdir(dir_name)
assert File.stat(dir_name).ino == 1
# make sure any deleted inodes are reused
dir_name = 'dir1'
deleted_ino = File.stat(dir_name).ino
FileUtils.rm_rf(dir_name)
dir_name = 'dir3'
Dir.mkdir dir_name
assert File.stat(dir_name).ino == deleted_ino
# and that the next largest inode number is picked if we are out of
# unused, deleted inodes
dir_name = 'dir4'
Dir.mkdir dir_name
assert File.stat(dir_name).ino == 2
# make sure moved directories retain their existing inodes
dir_name = 'dir3'
move_dir_name = 'dir3_mv'
old_ino = File.stat(dir_name).ino
FileUtils.mv(dir_name, move_dir_name)
assert File.stat(move_dir_name).ino == old_ino
# but that copied ones do not
dir_name = 'dir2'
copy_dir = 'dir2_cp'
FileUtils.cp_r(dir_name, copy_dir)
assert File.stat(copy_dir).ino == 3
# and finally that symlinks have the same inode as what they link to
# NOTE: viewing files with `ls -il` will show that a symlink has a different
# inode value than what it is pointing to. However, testing on a directory made via
# ruby's `ln_s` method will show that the inode of a symlink and what it is
# pointing to is identical, hence I am testing for equality
dir_name = 'dir4'
sym_link = 'dir4_symlink'
FileUtils.ln_s(dir_name, sym_link)
assert File.stat(sym_link).ino == File.stat(dir_name).ino
end
def test_files_and_dirs_share_the_same_inode_pool
# make sure that inodes are assigned starting from 0 in ascending order
# and that files and directories share the same pool of inode numbers
# case where the directory is first
dir_name = 'dir1'
Dir.mkdir dir_name
dir_ino = File.stat(dir_name).ino
file_name = 'file1'
File.open(file_name, 'w') { |f| f << 'some content' }
file_ino = File.stat(file_name).ino
assert file_ino == dir_ino + 1
# case where the file is first
file_name = 'file2'
File.open(file_name, 'w') { |f| f << 'some content' }
file_ino = File.stat(file_name).ino
dir_name = 'dir2'
Dir.mkdir dir_name
dir_ino = File.stat(dir_name).ino
assert dir_ino == file_ino + 1
# make sure that inodes are reused for both files and dirs when either a file
# or a dir is deleted
# case where dir is deleted
dir_name = 'dir3'
Dir.mkdir dir_name
deleted_dir_ino = File.stat(dir_name).ino
FileUtils.rm_rf(dir_name)
file_name = 'file3'
File.open(file_name, 'w') { |f| f << 'some content' }
file_ino = File.stat(file_name).ino
assert file_ino == deleted_dir_ino
# case where file is deleted
file_name = 'file4'
File.open(file_name, 'w') { |f| f << 'some content' }
deleted_file_ino = File.stat(file_name).ino
File.delete(file_name)
dir_name = 'dir4'
Dir.mkdir dir_name
dir_ino = File.stat(dir_name).ino
assert deleted_file_ino == dir_ino
end
def test_file_is_a_io
assert FakeFS::File.open('/foo', 'a').is_a?(IO)
end
def test_file_is_not_something_else
refute FakeFS::File.open('/foo', 'a').is_a?(Integer)
end
end
|