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
|
;;; projectile-test.el --- Projectile's test suite -*- lexical-binding: t -*-
;; Copyright © 2011-2023 Bozhidar Batsov
;; Author: Bozhidar Batsov <bozhidar@batsov.dev>
;; This file is NOT part of GNU Emacs.
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see `http://www.gnu.org/licenses/'.
;;; Commentary:
;; Projectile's Buttercup-powered test suite. You can run it either
;; interactively or via the command-line (e.g. via eldev test).
;;; Code:
;; needed for the tests to work with native compilation
(with-eval-after-load 'comp
(push 'insert-file-contents
native-comp-never-optimize-functions))
(require 'projectile)
(require 'buttercup)
(require 'subr-x)
;; Useful debug information
(message "Running tests on Emacs %s" emacs-version)
;; TODO: Revise this init logic
(defvar projectile-test-path (let* ((current-file (if load-in-progress load-file-name (buffer-file-name)))
(source-directory (locate-dominating-file current-file "Eldev"))
;; Do not load outdated byte code for tests
(load-prefer-newer t))
(expand-file-name "test" source-directory)))
;;; Test Utilities
(defmacro projectile-test-with-sandbox (&rest body)
"Evaluate BODY in an empty temporary directory."
(declare (indent 0) (debug (&rest form)))
`(let ((sandbox (expand-file-name
(convert-standard-filename "test/sandbox/")
projectile-test-path)))
(when (file-directory-p sandbox)
(delete-directory sandbox t))
(make-directory sandbox t)
(let ((default-directory sandbox))
,@body)))
(defmacro projectile-test-with-files (files &rest body)
"Evaluate BODY in the presence of FILES.
You'd normally combine this with `projectile-test-with-sandbox'."
(declare (indent 1) (debug (sexp &rest form)))
`(progn
,@(mapcar (lambda (file)
(if (string-suffix-p "/" file)
`(make-directory ,file t)
`(let ((dir (file-name-directory ,file)))
(when dir
(make-directory dir t))
(with-temp-file ,file))))
files)
,@body))
(defmacro projectile-test-with-files-using-custom-project (files project-options &rest body)
"Evaluate BODY with the custom project having PROJECT-OPTIONS with FILES."
(declare (indent 2) (debug (sexp sexp &rest form)))
`(let ((projectile-indexing-method 'native)
(projectile-projects-cache (make-hash-table :test 'equal))
(projectile-projects-cache-time (make-hash-table :test 'equal))
(projectile-enable-caching t))
,@(mapcar (lambda (file)
(let* ((path (concat "project/" file))
(dir (file-name-directory path)))
(if (string-suffix-p "/" file)
`(make-directory ,path t)
`(progn
(make-directory ,dir t)
(with-temp-file ,path)))))
files)
(projectile-register-project-type 'sample-project '("somefile") ,@project-options)
(spy-on 'projectile-project-type :and-return-value 'sample-project)
(spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/")))
,@body))
;;; To avoid a macro, we could write a single "it" and iterate over a list of
;;; functions. However, it would require manually rescuing the error and manually
;;; re-throwing the error to include the function name in the error string, to
;;; make it clear which function we're dealing with. That could be avoided if
;;; buttercup allowed us to specify a custom error message like this:
;;;
;;; (expect (funcall 'foo) :to-throw 'error nil "Custom error message here")
(defmacro assert-friendly-error-when-no-project (fn)
"Write a test that ensures FN throws a friendly error when called without a project."
(let ((description (concat "when calling " (symbol-name fn) " without a project")))
`(describe
,description
:var ((fn ',fn))
(it "throws a friendly error"
(projectile-test-with-sandbox
(projectile-test-with-files
("index.html")
(find-file-noselect "index.html" t)
;; Avoid "the current buffer is not visiting a file" error
(write-file "index.html")
(spy-on 'projectile-project-root :and-return-value nil)
(let ((projectile-require-project-root t))
(expect (call-interactively fn)
:to-throw
'error
(list (concat "Projectile cannot find a project definition in "
default-directory))))))))))
(defun projectile-test-tmp-file-path ()
"Return a filename suitable to save data to in the test temp directory."
(concat projectile-test-path
"/tmp/temporary-file-" (format "%d" (random))
".eld"))
(defun file-handler-for-tests (operation &rest args)
"Handler for # files.
Just delegates OPERATION and ARGS for all operations except for`shell-command`'."
(let ((inhibit-file-name-handlers
(cons 'file-handler-for-tests
(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(cond ((eq operation 'shell-command) (let ((default-directory ""))
(shell-command "echo magic" t)))
(t (apply operation args)))))
(add-to-list 'file-name-handler-alist (cons "^#" 'file-handler-for-tests))
;;; Tests
(describe "projectile-project-name"
(it "return projectile-project-name when present"
(let ((projectile-project-name "name"))
(expect (projectile-project-name) :to-equal "name")
(expect (projectile-project-name "other") :to-equal "name")))
(it "uses projectile-project-name-function to get the project name from the project dir"
(let ((projectile-project-name-function (lambda (dir) dir)))
(expect (projectile-project-name "some/dir") :to-equal "some/dir")))
(it "acts on the current project is not passed a project dir explicitly"
(spy-on 'projectile-project-root :and-return-value "current/project")
(let ((projectile-project-name-function (lambda (dir) dir)))
(expect (projectile-project-name) :to-equal "current/project"))))
(describe "projectile-prepend-project-name"
(it "prepends the project name to its parameter"
(spy-on 'projectile-project-name :and-return-value "project")
(expect (projectile-prepend-project-name "Test") :to-equal "[project] Test")))
(describe "projectile-expand-root"
(it "expands a relative path into an absolute path within a project"
(spy-on 'projectile-project-root :and-return-value "/path/to/project")
(expect (projectile-expand-root "foo") :to-equal "/path/to/project/foo")
(expect (projectile-expand-root "foo/bar") :to-equal "/path/to/project/foo/bar")
(expect (projectile-expand-root "./foo/bar") :to-equal "/path/to/project/foo/bar")))
(describe "projectile-expand-file-name-wildcard"
(it "expands a filename not containing wildcards"
(expect (projectile-expand-file-name-wildcard "test" "/path/to/project/")
:to-equal "/path/to/project/test"))
(it "does not try to resolve wildcards if there are none in the pattern"
(spy-on 'file-expand-wildcards)
(expect (projectile-expand-file-name-wildcard "foo" "/path/to/project/")
:to-equal "/path/to/project/foo")
(expect (spy-calls-any 'file-expand-wildcards) :to-equal nil))
(it "returns the first wildcard result if any exist"
(spy-on 'file-expand-wildcards
:and-return-value '("/path/to/project/one"
"/path/to/project/two"))
(expect (projectile-expand-file-name-wildcard "*" "/path/to/project")
:to-equal "/path/to/project/one"))
(it "returns the expanded result if the are no wildcard results"
(expect (projectile-expand-file-name-wildcard "*" "/path/to/project-b")
:to-equal "/path/to/project-b/*")))
(describe "projectile--combine-plists"
(it "Items in second plist override elements in first"
(expect (projectile--combine-plists
'(:foo "foo" :bar "bar")
'(:foo "foo" :bar "foo" :foobar "foobar"))
:to-equal
'(:foo "foo" :bar "foo" :foobar "foobar")))
(it "Nil elements in second plist override elements in first"
(expect (projectile--combine-plists
'(:foo "foo" :bar "bar")
'(:foo "foo" :bar nil :foobar "foobar"))
:to-equal
'(:foo "foo" :bar nil :foobar "foobar"))))
(describe "projectile-register-project-type"
(it "prepends new projects to projectile-project-types"
(projectile-register-project-type 'foo '("Foo"))
(expect (caar projectile-project-types) :to-equal 'foo)
(projectile-register-project-type 'bar '("Bar"))
(expect (caar projectile-project-types) :to-equal 'bar)))
(describe "projectile-update-project-type"
:var ((mock-projectile-project-types
'((foo marker-files ("marker-file")
project-file "project-file"
compilation-dir "compilation-dir"
configure-command "configure"
compile-command "compile"
test-command "test"
install-command "install"
package-command "package"
run-command "run"))))
(it "Updates existing project type in projectile-project-types"
(let ((projectile-project-types mock-projectile-project-types))
(projectile-update-project-type
'foo
:marker-files '("marker-file2")
:test-suffix "suffix")
(expect projectile-project-types :to-equal
'((foo marker-files ("marker-file2")
project-file "project-file"
compilation-dir "compilation-dir"
configure-command "configure"
compile-command "compile"
test-command "test"
install-command "install"
package-command "package"
run-command "run"
test-suffix "suffix")))))
(it "Updates existing project type with nil value"
(let ((projectile-project-types mock-projectile-project-types))
(projectile-update-project-type
'foo
:marker-files '("marker-file2")
:test-suffix nil)
(expect projectile-project-types :to-equal
'((foo marker-files ("marker-file2")
project-file "project-file"
compilation-dir "compilation-dir"
configure-command "configure"
compile-command "compile"
test-command "test"
install-command "install"
package-command "package"
run-command "run"
test-suffix nil)))))
(it "Updates existing project type using all options"
(let ((projectile-project-types mock-projectile-project-types)
(dummy-val "foo"))
(projectile-update-project-type
'foo
:marker-files (list dummy-val)
:project-file dummy-val
:compilation-dir dummy-val
:configure dummy-val
:compile dummy-val
:test dummy-val
:install dummy-val
:package dummy-val
:run dummy-val
:test-suffix dummy-val
:test-prefix dummy-val
:src-dir dummy-val
:test-dir dummy-val
:related-files-fn dummy-val)
(expect projectile-project-types :to-equal
`((foo marker-files (,dummy-val)
project-file ,dummy-val
compilation-dir ,dummy-val
configure-command ,dummy-val
compile-command ,dummy-val
test-command ,dummy-val
install-command ,dummy-val
package-command ,dummy-val
run-command ,dummy-val
test-suffix ,dummy-val
test-prefix ,dummy-val
src-dir ,dummy-val
test-dir ,dummy-val
related-files-fn ,dummy-val)))))
(it "Error when attempt to update nonexistent project type"
(let ((projectile-project-types mock-projectile-project-types))
(expect (projectile-update-project-type
'bar
:marker-files '("marker-file")
:test-suffix "suffix")
:to-throw)))
(it "changes project type precedence"
(let ((projectile-project-types
'((foo marker-files ("foo"))
(bar marker-files ("foo")))))
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/" "projectA/foo")
(spy-on 'projectile-project-root
:and-return-value
(file-truename (expand-file-name "projectA")))
(expect (projectile-project-type) :to-equal 'foo)
(projectile-update-project-type 'bar :precedence 'high)
(expect (projectile-project-type) :to-equal 'bar)
(projectile-update-project-type 'bar :precedence 'low)
(expect (projectile-project-type) :to-equal 'foo)))))
(it "errors if :precedence not valid"
(let ((projectile-project-types '((bar marker-files ("foo")))))
(expect
(projectile-update-project-type 'bar :precedence 'invalid-symbol)
:to-throw))))
(describe "projectile-project-type"
:var ((dir default-directory))
(it "detects the type of Projectile's project"
(expect (projectile-project-type) :to-equal 'emacs-eldev))
(it "caches the project type"
(expect (gethash (projectile-project-root) projectile-project-type-cache) :to-equal 'emacs-eldev))
(it "detects the type of Projectile's project when it is passed as args"
(projectile-test-with-sandbox
(let ((projectile-project-type-cache (make-hash-table :test 'equal)))
(expect (projectile-project-type dir) :to-equal 'emacs-eldev))))
(describe "override by projectile-project-type"
(it "is respected when no DIR is passed"
(let ((projectile-project-type 'python-poetry))
(expect projectile-project-type :to-equal 'python-poetry)))
(it "has no effect when DIR is passed"
(projectile-test-with-sandbox
(let ((projectile-project-type 'python-poetry))
(expect (projectile-project-type dir) :to-equal 'emacs-eldev))))))
(describe "projectile-ignored-directory-p"
(it "checks if directory should be ignored"
(spy-on 'projectile-ignored-directories :and-return-value '("/path/to/project/tmp" "/path/to/project/t\\.*"))
(expect (projectile-ignored-directory-p "/path/to/project/tmp") :to-be-truthy)
(expect (projectile-ignored-directory-p "/path/to/project/t.ignore") :to-be-truthy)
(expect (projectile-ignored-directory-p "/path/to/project/log") :not :to-be-truthy)))
(describe "projectile-ignored-file-p"
(it "checks if file should be ignored"
(spy-on 'projectile-ignored-files :and-return-value '("/path/to/project/TAGS" "/path/to/project/T.*"))
(expect (projectile-ignored-file-p "/path/to/project/TAGS") :to-be-truthy)
(expect (projectile-ignored-file-p "/path/to/project/foo.el") :not :to-be-truthy)))
(describe "projectile-ignored-files"
(it "returns list of ignored files"
(spy-on 'projectile-project-root :and-return-value "/path/to/project")
(spy-on 'projectile-project-name :and-return-value "project")
(spy-on 'projectile-project-ignored-files :and-return-value '("foo.js" "bar.rb"))
(let ((files '("/path/to/project/TAGS"
"/path/to/project/foo.js"
"/path/to/project/bar.rb"
"/path/to/project/file1.log"
"/path/to/project/file2.log"))
(projectile-ignored-files '("TAGS" "file\d+\\.log")))
(expect (projectile-ignored-files) :not :to-equal files)
(expect (projectile-ignored-files) :to-equal '("/path/to/project/TAGS"
"/path/to/project/foo.js"
"/path/to/project/bar.rb")))))
(describe "projectile-ignored-directories"
(it "returns list of ignored directories"
(spy-on 'projectile-project-ignored-directories :and-return-value '("tmp" "log"))
(spy-on 'projectile-project-root :and-return-value "/path/to/project")
(let ((paths '("/path/to/project/compiled/"
"/path/to/project/ignoreme"
"/path/to/project/ignoremetoo"
"/path/to/project/tmp"
"/path/to/project/log"))
(projectile-globally-ignored-directories '("compiled" "ignoreme")))
(expect (projectile-ignored-directories) :not :to-equal paths)
(expect (projectile-ignored-directories) :to-equal '("/path/to/project/compiled/"
"/path/to/project/ignoreme/"
"/path/to/project/tmp/"
"/path/to/project/log/")))))
(describe "projectile-project-ignored-files"
(it "returns list of project ignored files"
(let ((files '("/path/to/project/foo.el" "/path/to/project/foo.elc")))
(spy-on 'projectile-project-ignored :and-return-value files)
(spy-on 'file-directory-p :and-return-value nil)
(expect (projectile-project-ignored-files) :to-equal files)
(spy-on 'file-directory-p :and-return-value t)
(expect (projectile-project-ignored-files) :not :to-be-truthy))))
(describe "projectile-project-ignored-directories"
(it "returns list of project ignored directories"
(let ((directories '("/path/to/project/tmp" "/path/to/project/log")))
(spy-on 'projectile-project-ignored :and-return-value directories)
(spy-on 'file-directory-p :and-return-value t)
(expect (projectile-project-ignored-directories) :to-equal directories)
(spy-on 'file-directory-p :and-return-value nil)
(expect (projectile-project-ignored-directories) :not :to-be-truthy))))
(describe "projectile-project-ignored"
(it "returns list of ignored files/directories"
(spy-on 'projectile-project-root :and-return-value "/path/to/project")
(spy-on 'projectile-project-name :and-return-value "project")
(spy-on 'projectile-paths-to-ignore :and-return-value (list "log" "tmp" "compiled"))
(spy-on 'file-expand-wildcards :and-call-fake
(lambda (pattern ignored)
(cond
((string-equal pattern "log") "/path/to/project/log")
((string-equal pattern "tmp") "/path/to/project/tmp")
((string-equal pattern "compiled") "/path/to/project/compiled"))))
(let* ((file-names '("log" "tmp" "compiled"))
(files (mapcar 'projectile-expand-root file-names)))
(expect (projectile-project-ignored) :to-equal files))))
(describe "projectile-remove-ignored"
(it "removes ignored folders and files"
(spy-on 'projectile-project-root :and-return-value "/path/to/project")
(spy-on 'projectile-project-name :and-return-value "project")
(spy-on 'projectile-ignored-files-rel)
(spy-on 'projectile-ignored-directories-rel)
(let* ((file-names '("foo.c" "foo.o" "foo.so" "foo.o.gz" "foo.tar.gz" "foo.tar.GZ"))
(files (mapcar 'projectile-expand-root file-names)))
(let ((projectile-globally-ignored-file-suffixes '(".o" ".so" ".tar.gz")))
(expect (projectile-remove-ignored files) :to-equal (mapcar 'projectile-expand-root '("foo.c" "foo.o.gz")))))))
(describe "projectile-add-unignored"
(it "requires explicitly unignoring files inside ignored paths"
(spy-on 'projectile-get-repo-ignored-files :and-return-value '("unignored-file" "path/unignored-file2"))
(let ((projectile-globally-unignored-files '("unignored-file")))
(expect (projectile-add-unignored nil nil '("file")) :to-equal '("file" "unignored-file")))
(let ((projectile-globally-unignored-files '("unignored-file" "path/unignored-file2")))
(expect (projectile-add-unignored nil nil '("file")) :to-equal '("file" "unignored-file" "path/unignored-file2"))))
(it "returns the list of globally unignored files on an unsupported VCS"
(spy-on 'projectile-project-vcs :and-return-value 'none)
(let ((projectile-globally-unignored-files '("unignored-file")))
(expect (projectile-add-unignored nil nil '("file")) :to-equal '("file"))))
(it "requires explicitly unignoring ignored files inside unignored paths"
(spy-on 'projectile-project-vcs :and-return-value 'git)
(spy-on 'projectile-get-repo-ignored-files :and-return-value '("path/unignored-file"))
(spy-on 'projectile-get-repo-ignored-directory :and-call-fake
(lambda (project dir vcs)
(list (concat dir "unignored-file"))))
(let ((projectile-globally-unignored-directories '("path")))
(expect (projectile-add-unignored nil nil '("file")) :to-equal '("file" "path/unignored-file"))
(let ((projectile-globally-ignored-files '("unignored-file")))
(expect (projectile-add-unignored nil nil '("file")) :to-equal '("file"))
(let ((projectile-globally-unignored-files '("path/unignored-file")))
(expect (projectile-add-unignored nil nil '("file")) :to-equal '("file" "path/unignored-file")))))))
(describe "projectile-parse-dirconfig-file"
(it "parses dirconfig and returns directories to ignore and keep"
(spy-on 'file-exists-p :and-return-value t)
(spy-on 'file-truename :and-call-fake (lambda (filename) filename))
(spy-on 'insert-file-contents :and-call-fake
(lambda (filename)
(save-excursion (insert "\n-exclude\n+include\n#may-be-a-comment\nno-prefix\n left-wspace\nright-wspace\t\n"))))
(expect (projectile-parse-dirconfig-file) :to-equal '(("include/")
("exclude"
"#may-be-a-comment"
"no-prefix"
"left-wspace"
"right-wspace")
nil))
;; same test - but with comment lines enabled using prefix '#'
(let ((projectile-dirconfig-comment-prefix ?#))
(expect (projectile-parse-dirconfig-file) :to-equal '(("include/")
("exclude"
"no-prefix"
"left-wspace"
"right-wspace")
nil)))
))
(describe "projectile-get-project-directories"
(it "gets the list of project directories"
(spy-on 'projectile-project-root :and-return-value "/my/root/")
(spy-on 'projectile-parse-dirconfig-file :and-return-value '(nil))
(expect (projectile-get-project-directories "/my/root") :to-equal '("/my/root")))
(it "gets the list of project directories with dirs to keep"
(spy-on 'projectile-project-root :and-return-value "/my/root/")
(spy-on 'projectile-parse-dirconfig-file :and-return-value '(("foo" "bar/baz")))
(expect (projectile-get-project-directories "/my/root/") :to-equal '("/my/root/foo" "/my/root/bar/baz"))))
(describe "projectile-dir-files"
(it "fails unless directory exists"
(spy-on 'file-directory-p :and-call-fake
(lambda (filename) (equal filename "/my/root/")))
(expect (projectile-dir-files "asdf") :to-throw))
(it "lists the files in directory and sub-directories"
(spy-on 'file-directory-p :and-call-fake
(lambda (filename) (equal filename "/my/root/")))
(spy-on 'projectile-patterns-to-ignore)
(spy-on 'projectile-index-directory :and-call-fake (lambda (dir patterns progress-reporter)
(expect dir :to-equal "/my/root/")
'("/my/root/a/b/c" "/my/root/a/d/e")))
(spy-on 'projectile-dir-files-alien :and-return-value '("a/b/c" "a/d/e"))
(spy-on 'cd)
(let ((projectile-indexing-method 'native))
(expect (projectile-dir-files "/my/root/") :to-equal '("a/b/c" "a/d/e")))
(let ((projectile-indexing-method 'hybrid))
(expect (projectile-dir-files "/my/root/") :to-equal '("a/b/c" "a/d/e")))))
(describe "projectile-get-sub-projects-command"
(it "gets sub projects command for git"
(expect (string-prefix-p "git" (projectile-get-sub-projects-command 'git)) :to-be-truthy))
(it "returns empty when vcs is not supported"
(expect (string-empty-p (projectile-get-sub-projects-command 'none)) :to-be-truthy)))
(describe "projectile-files-via-ext-command"
(it "returns nil when command is nil or empty or fails"
(expect (projectile-files-via-ext-command "" "") :not :to-be-truthy)
(expect (projectile-files-via-ext-command "" nil) :not :to-be-truthy)
(expect (projectile-files-via-ext-command "" "echo Not a file name! > &2") :not :to-be-truthy)
(expect (projectile-files-via-ext-command "" "echo filename") :to-equal '("filename")))
(it "supports magic file handlers"
(expect (projectile-files-via-ext-command "#magic#" "echo filename") :to-equal '("magic"))))
(describe "projectile-mode"
(before-each
(spy-on 'projectile--cleanup-known-projects)
(spy-on 'projectile-discover-projects-in-search-path))
(it "sets up hook functions"
(projectile-mode 1)
(expect (memq 'projectile-find-file-hook-function find-file-hook) :to-be-truthy)
(projectile-mode -1)
(expect (memq 'projectile-find-file-hook-function find-file-hook) :not :to-be-truthy))
(it "respects projectile-auto-discover setting"
(unwind-protect
(progn
(let ((projectile-auto-discover nil))
(projectile-mode 1)
(expect 'projectile-discover-projects-in-search-path :not :to-have-been-called))
(let ((projectile-auto-discover t))
(projectile-mode 1)
(expect 'projectile-discover-projects-in-search-path :to-have-been-called)))
(projectile-mode -1))))
(describe "projectile-relevant-known-projects"
(it "returns a list of known projects"
(let ((projectile-known-projects '("/path/to/project1" "/path/to/project2")))
(spy-on 'projectile-project-root :and-return-value "/path/to/project1")
(expect (projectile-relevant-known-projects) :to-equal '("/path/to/project2")))))
(describe "projectile--cleanup-known-projects"
(it "removes known projects that don't exist anymore"
(let* ((projectile-known-projects-file (projectile-test-tmp-file-path))
(directories (cl-loop repeat 3 collect (make-temp-file "projectile-cleanup" t)))
(projectile-known-projects directories))
(unwind-protect
(progn
(projectile--cleanup-known-projects)
(expect projectile-known-projects :to-equal directories)
(delete-directory (car directories))
(projectile--cleanup-known-projects)
(expect projectile-known-projects :to-equal (cdr directories)))
(mapc (lambda (d) (ignore-errors (delete-directory d))) directories)
(delete-file projectile-known-projects-file nil)))))
(describe "projectile-project-root"
(it "returns the absolute root directory of a project"
(let* ((root-directory (make-temp-file "projectile-absolute" t))
(root-file (concat root-directory "/.projectile"))
(deep-directory (concat root-directory "/foo/bar/baz"))
(project-file (concat deep-directory "/tmp.txt")))
(unwind-protect
(progn
(mkdir deep-directory t)
(with-temp-file root-file)
(with-temp-file project-file)
(with-current-buffer (find-file-noselect project-file t)
(expect (file-name-absolute-p (projectile-project-root)) :to-be-truthy)))
(ignore-errors (delete-directory root-directory t))))))
(describe "projectile-tags-exclude-patterns"
(it "returns a string with exclude patterns for ctags"
(spy-on 'projectile-ignored-directories-rel :and-return-value (list ".git/" ".hg/"))
(expect (projectile-tags-exclude-patterns) :to-equal "--exclude=\".git\" --exclude=\".hg\"")))
(describe "projectile-maybe-invalidate-cache"
(it "should not invalidate cache if dirconfig is older than cache"
(spy-on 'projectile-invalidate-cache :and-return-value t)
(expect (projectile-maybe-invalidate-cache nil) :not :to-be-truthy))
(it "should invalidate cache if force is t"
(spy-on 'projectile-invalidate-cache :and-return-value t)
(expect (projectile-maybe-invalidate-cache t) :to-be-truthy))
(it "should invalidate cache if dirconfig is newer than cache"
(spy-on 'projectile-invalidate-cache :and-return-value t)
(spy-on 'file-newer-than-file-p :and-return-value t)
(expect (projectile-maybe-invalidate-cache nil) :to-be-truthy)))
(describe "projectile-root-top-down"
(it "identifies the root directory of a project by top-down search"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/.svn/"
"projectA/src/.svn/"
"projectA/src/html/.svn/"
"projectA/.git/"
"projectA/src/html/"
"projectA/src/framework/lib/"
"projectA/src/framework.conf"
"projectA/src/html/index.html")
(expect (projectile-root-top-down "projectA/src/framework/lib" '("framework.conf" ".git"))
:to-equal
(expand-file-name "projectA/src/"))
(expect (projectile-root-top-down "projectA/src/framework/lib" '(".git" "framework.conf"))
:to-equal
(expand-file-name "projectA/src/"))
(expect (projectile-root-top-down "projectA/src/html/" '(".svn"))
:to-equal
(expand-file-name "projectA/src/html/"))))))
(describe "projectile-root-top-down-recurring"
(it "identifies the root directory of a project by recurring top-down search"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/.svn/"
"projectA/src/.svn/"
"projectA/src/html/.svn/"
"projectA/.git/"
"projectA/src/html/"
"projectA/src/framework/lib/"
"projectA/src/framework/framework.conf"
"projectA/src/html/index.html"
".projectile")
(expect (projectile-root-top-down-recurring "projectA/src/html/" '("something" ".svn" ".git"))
:to-equal
(expand-file-name "projectA/"))
(expect (projectile-root-top-down-recurring "projectA/src/html/" '(".git"))
:to-equal
(expand-file-name "projectA/"))
(expect (projectile-root-top-down-recurring "projectA/src/html/" '("elusivefile"))
:not :to-be-truthy)))))
(describe "projectile-root-bottom-up"
(it "identifies the root directory of a project by bottom-up search"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/.git/"
"projectA/.projectile"
"projectA/.svn/"
"projectA/src/.svn/"
"projectA/src/framework/framework.conf"
"projectA/src/framework/lib/"
"projectA/src/html/"
"projectA/src/html/.svn/"
"projectA/src/html/index.html")
(expect (projectile-root-bottom-up "projectA/src/framework/lib" '(".git" ".svn"))
:to-equal
(expand-file-name "projectA/src/"))
(expect (projectile-root-bottom-up "projectA/src/framework/lib" '(".git"))
:to-equal
(expand-file-name "projectA/"))
(expect (projectile-root-bottom-up "projectA/src/html" '(".git" ".svn"))
:to-equal
(expand-file-name "projectA/src/html/"))
(expect (projectile-root-bottom-up "projectA/src/html" '(".svn" ".git"))
:to-equal
(expand-file-name "projectA/src/html/"))
(expect (projectile-root-bottom-up "projectA/src/html" '(".projectile" "index.html"))
:to-equal
(expand-file-name "projectA/src/html/"))
(expect (projectile-root-bottom-up "projectA/src/html" '("index.html" ".projectile"))
:to-equal
(expand-file-name "projectA/src/html/"))
(expect (projectile-root-bottom-up "projectA/src/html" '(".projectile"))
:to-equal
(expand-file-name "projectA/"))))))
(describe "projectile-project-root"
(defun projectile-test-should-root-in (root directory)
(let ((projectile-project-root-cache (make-hash-table :test 'equal)))
(expect (let ((default-directory
(expand-file-name
(file-name-as-directory directory))))
(file-truename (projectile-project-root)))
:to-equal
(file-truename (file-name-as-directory root)))))
(it "returns the root directory of a project"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/src/.svn/"
"projectA/src/html/.svn/"
"projectA/src/html/"
"projectA/src/framework/lib/"
"projectA/build/framework/lib/"
"projectA/requirements/a/b/c/d/e/f/g/"
"projectA/src/framework/framework.conf"
"projectA/requirements/a/b/c/requirements.txt"
"projectA/src/html/index.html"
"projectA/.projectile"
"override")
(let ((projectile-project-root-files-bottom-up '("somefile" ".projectile"))
(projectile-project-root-files '("otherfile" "framework.conf" "requirements.txt"))
(projectile-project-root-files-top-down-recurring '(".svn" ".foo"))
(projectile-project-root-functions '(projectile-root-bottom-up
projectile-root-top-down
projectile-root-top-down-recurring)))
(projectile-test-should-root-in "projectA" "projectA/requirements/a/b/c/d/e/f/g")
(projectile-test-should-root-in "projectA" "projectA/src/framework/lib")
(projectile-test-should-root-in "projectA" "projectA/src/html")
(setq projectile-project-root-functions '(projectile-root-top-down
projectile-root-top-down-recurring
projectile-root-bottom-up))
(projectile-test-should-root-in "projectA/requirements/a/b/c"
"projectA/requirements/a/b/c/d/e/f/g")
(projectile-test-should-root-in "projectA/src/framework"
"projectA/src/framework/lib")
(projectile-test-should-root-in "projectA/src"
"projectA/src/html"))
(let ((projectile-project-root-files-bottom-up '("somefile" ".projectile"))
(projectile-project-root-files '("otherfile" "noframework.conf"))
(projectile-project-root-files-top-down-recurring '(".svn" ".foo"))
(projectile-project-root-functions '(projectile-root-top-down-recurring
projectile-root-bottom-up
projectile-root-top-down)))
(projectile-test-should-root-in "projectA/src" "projectA/src/framework/lib")
(projectile-test-should-root-in "projectA/src" "projectA/src/html")
(projectile-test-should-root-in "projectA/" "projectA/build/framework/lib"))
(let ((projectile-project-root-files-bottom-up '("somefile" "override"))
(projectile-project-root-files '("otherfile" "anotherfile"))
(projectile-project-root-files-top-down-recurring '("someotherfile" "yetanotherfile"))
(projectile-project-root-functions '(projectile-root-bottom-up
projectile-root-top-down
projectile-root-top-down-recurring)))
(projectile-test-should-root-in default-directory "projectA/src/framework/lib")
(projectile-test-should-root-in default-directory "projectA/src/html"))
(let ((projectile-project-root-files-bottom-up '("somecoolfile"))
(projectile-project-root-files nil)
(projectile-project-root-files-top-down-recurring '(".svn"))
(projectile-project-root-functions '(projectile-root-bottom-up
projectile-root-top-down
projectile-root-top-down-recurring)))
(projectile-test-should-root-in "projectA/src/" "projectA/src/")
(projectile-test-should-root-in "projectA/src/" "projectA/src/html")))))
(it "caches permanent failure to find a project root"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/src/")
(let* ((projectile-project-root-functions '())
(dir "projectA/src")
(cache-key (format "projectilerootless-%s" dir))
(projectile-project-root-cache (make-hash-table :test 'equal)))
(expect (gethash cache-key projectile-project-root-cache) :to-be nil)
(expect (projectile-project-root dir) :to-be nil)
;; now that this has run once, the cache should be populated with 'none
(expect (gethash cache-key projectile-project-root-cache) :to-be 'none)
;; but projectile-project-root should still return nil
(expect (projectile-project-root dir) :to-be nil)))))
(it "does not cache transitory failure to find a project root"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/src/")
;; hackish, but override file-remote-p for a moment, which is called in
;; projectile-project-root with 1 argument to test if the file is remote,
;; and 3 arguments to see if the file is connected. We want to return t
;; when checking if remote, and nil when checking if connected.
(cl-letf (((symbol-function 'file-remote-p)
(lambda (&rest args) (eql 1 (length args)))))
(let* ((projectile-project-root-functions '())
(dir "projectA/src")
(cache-key (format "projectilerootless-%s" dir))
(projectile-project-root-cache (make-hash-table :test 'equal)))
(expect (gethash cache-key projectile-project-root-cache) :to-be nil)
(expect (projectile-project-root dir) :to-be nil)
;; since the failure was transitory, there should be nothing cached
(expect (gethash cache-key projectile-project-root-cache) :to-be nil)
;; and projectile-project-root should still return nil
(expect (projectile-project-root dir) :to-be nil)))))))
(describe "projectile-file-exists-p"
(it "returns t if file exists"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/dirA/dirB/"
"project/fileA")
(let ((projectile-file-exists-local-cache-expire nil)
(projectile-file-exists-remote-cache-expire nil))
(expect (projectile-file-exists-p "project/fileA") :to-be-truthy)
(expect (projectile-file-exists-p "project/dirA/dirB") :to-be-truthy)
(expect (projectile-file-exists-p "project/dirA/fileB") :not :to-be-truthy)
(with-temp-file "project/dirA/fileB")
(expect (projectile-file-exists-p "project/dirA/fileB") :to-be-truthy)
(expect (projectile-file-exists-p "project/nofile") :not :to-be-truthy)
(delete-file "project/fileA")
(expect (projectile-file-exists-p "project/fileA") :not :to-be-truthy)))))
(it "caches the results"
(projectile-test-with-sandbox
(projectile-test-with-files
("dirA/dirB/"
"fileA")
(let ((initial-time (current-time))
(projectile-file-exists-local-cache-expire 100)
(projectile-file-exists-remote-cache-expire nil))
(spy-on 'run-with-timer :and-return-value 'nooptimer)
(spy-on 'current-time :and-return-value initial-time)
(expect (projectile-file-exists-p "fileA") :to-be-truthy)
(expect (projectile-file-exists-p "dirA/dirB") :to-be-truthy)
(expect (projectile-file-exists-p "dirA/fileB") :not :to-be-truthy)
(with-temp-file "dirA/fileB")
(expect (projectile-file-exists-p "dirA/fileB") :not :to-be-truthy)
(delete-file "fileA")
(expect (projectile-file-exists-p "fileA") :to-be-truthy)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(projectile-file-exists-cache-cleanup)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(spy-on 'current-time :and-return-value (time-add initial-time (seconds-to-time 50)))
(projectile-file-exists-cache-cleanup)
(expect (projectile-file-exists-p "fileA") :to-be-truthy)
(expect (projectile-file-exists-p "dirA/fileB") :not :to-be-truthy)
(expect (projectile-file-exists-p "fileC") :not :to-be-truthy)
(with-temp-file "fileC")
(expect (projectile-file-exists-p "fileA") :to-be-truthy)
(projectile-file-exists-cache-cleanup)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(spy-on 'current-time :and-return-value (time-add initial-time (seconds-to-time 120)))
(projectile-file-exists-cache-cleanup)
(expect (projectile-file-exists-p "dirA/fileB") :to-be-truthy)
(expect (projectile-file-exists-p "fileA") :not :to-be-truthy)
(expect (projectile-file-exists-p "fileC") :not :to-be-truthy)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(projectile-file-exists-cache-cleanup)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(spy-on 'current-time :and-return-value (time-add initial-time (seconds-to-time 220)))
(projectile-file-exists-cache-cleanup)
(expect (projectile-file-exists-p "fileC") :to-be-truthy)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(projectile-file-exists-cache-cleanup)
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(spy-on 'current-time :and-return-value (time-add initial-time (seconds-to-time 1000)))
(expect projectile-file-exists-cache-timer :to-equal 'nooptimer)
(projectile-file-exists-cache-cleanup)
(expect projectile-file-exists-cache-timer :not :to-be-truthy))))))
(describe "projectile-project-root"
(it "caches the current file"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.projectile"
"project/file1.el"
"project/file2.el"
"project/file3.el"
"project/file4.el")
(cd "project")
(let ((projectile-projects-cache (make-hash-table :test #'equal))
(projectile-projects-cache-time (make-hash-table :test #'equal))
(projectile-enable-caching t))
(puthash (projectile-project-root)
'("file1.el")
projectile-projects-cache)
(spy-on 'projectile-project-root :and-call-fake (lambda (&optional _dir) (file-truename default-directory)))
(spy-on 'projectile-project-vcs :and-return-value 'none)
(with-current-buffer (find-file-noselect "file2.el" t)
(projectile-cache-current-file)
(dolist (f '("file1.el" "file2.el"))
(expect (member f (gethash (projectile-project-root) projectile-projects-cache)) :to-be-truthy)))
(with-current-buffer (find-file-noselect "file3.el" t)
(projectile-cache-current-file)
(dolist (f '("file1.el" "file2.el" "file3.el"))
(expect (member f (gethash (projectile-project-root) projectile-projects-cache)) :to-be-truthy)))
(with-current-buffer (find-file-noselect "file4.el" t)
(projectile-cache-current-file)
(dolist (f '("file1.el" "file2.el" "file3.el" "file4.el"))
(expect (member f (gethash (projectile-project-root) projectile-projects-cache)) :to-be-truthy)))))))
(it "ensures that we update the cache if it's expired"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.projectile"
"project/file1.el"
"project/file2.el")
(cd "project")
(let ((projectile-projects-cache (make-hash-table :test #'equal))
(projectile-projects-cache-time (make-hash-table :test #'equal))
(projectile-enable-caching t)
(projectile-files-cache-expire 10))
;; Create a stale cache with only one file in it.
(puthash (projectile-project-root)
'("file1.el")
projectile-projects-cache)
(puthash (projectile-project-root)
0 ;; Cached 1st of January 1970.
projectile-projects-cache-time)
(spy-on 'projectile-acquire-root :and-call-fake (lambda () (file-truename default-directory)))
(spy-on 'projectile-project-vcs :and-return-value 'none)
;; After listing all the files, the cache should have been updated.
(projectile-current-project-files)
;; find returns the leading ./ therefore the somewhat odd notation here
(dolist (f '("file1.el" "file2.el"))
(expect (member f (gethash (projectile-project-root) projectile-projects-cache)) :to-be-truthy))))))
(it "ensures that we don't cache a project root if the path has changed."
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.projectile")
(cd "project")
(let ((projectile-project-root-cache (make-hash-table :test #'equal))
(correct-project-root (projectile-project-root)))
;; If this project has been moved, then we will have stale
;; paths in the cache.
(puthash
(format "projectile-root-bottom-up-%s" correct-project-root)
"/this/path/does/not/exist"
projectile-project-root-cache)
(expect (projectile-project-root) :to-equal correct-project-root))))))
(describe "projectile-grep"
(describe "multi-root grep"
(after-each
(cl-flet ((grep-buffer-p (b) (string-prefix-p "*grep" (buffer-name b))))
(let ((grep-buffers (cl-remove-if-not #'grep-buffer-p (buffer-list))))
(dolist (grep-buffer grep-buffers)
(let ((kill-buffer-query-functions nil))
(kill-buffer grep-buffer))))))
(it "grep multi-root projects"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/bar/"
"project/baz/")
(cd "project")
(with-temp-file ".projectile" (insert (concat "+/baz\n"
"+/bar\n")))
(with-temp-file "foo.txt" (insert "hi"))
(with-temp-file "bar/bar.txt" (insert "hi"))
(with-temp-file "baz/baz.txt" (insert "hi"))
(with-current-buffer (find-file-noselect ".projectile" t)
(let ((grep-find-template "<X>")
grep-find-ignored-directories grep-find-ignored-files
projectile-globally-ignored-files
projectile-globally-ignored-file-suffixes
projectile-globally-ignored-directories)
(projectile-grep "hi")))))))
(describe "rgrep"
(before-each
(spy-on 'compilation-start))
(it "excludes global ignores"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.projectile")
(cd "project")
(with-current-buffer (find-file-noselect ".projectile" t)
(let ((grep-find-template "<X>")
(grep-find-ignored-directories '("IG_DIR"))
(grep-find-ignored-files '("IG_FILE"))
(projectile-globally-ignored-files '("GLOB_IG_FILE"))
(projectile-globally-ignored-file-suffixes '("IG_SUF"))
(projectile-globally-ignored-directories '("GLOB_IG_DIR")))
(projectile-grep "hi")))
(expect 'compilation-start :to-have-been-called-with
(concat "-type d \\( -path \\*/IG_DIR \\) -prune -o "
"\\! -type d \\( -name IG_FILE -o -name \\*IG_SUF \\) -prune -o "
"\\( -path ./GLOB_IG_DIR -o -path ./GLOB_IG_FILE \\) -prune -o ")
'grep-mode))))
(it "excludes project ignores"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/bar/"
"project/baz/")
(cd "project")
(with-temp-file ".projectile" (insert (concat "-/*.txt\n"
"-/bar/*.txt\n"
"-/baz\n"
"-*.txt\n"
"-*.text\n"
"!/abc.txt\n"
"!/bar/abc.txt\n"
"!def.txt\n")))
(with-temp-file "foo.txt")
(with-temp-file "abc.txt")
(with-temp-file "bar/foo.txt")
(with-temp-file "bar/abc.txt")
(with-current-buffer (find-file-noselect ".projectile" t)
(let ((grep-find-template "<X>")
grep-find-ignored-directories grep-find-ignored-files
projectile-globally-ignored-files
projectile-globally-ignored-file-suffixes
projectile-globally-ignored-directories)
(projectile-grep "hi")))
(expect 'compilation-start :to-have-been-called-with
(concat "\\( -path ./baz -o -path ./foo.txt -o -path ./bar/foo.txt \\) -prune -o "
"\\( "
"\\( -path \\*.txt -o -path \\*.text \\) "
"-a \\! \\( -path ./abc.txt -o -path ./bar/abc.txt -o -path \\*/def.txt \\) "
"\\) -prune -o ")
'grep-mode)))))
(it "grep a git project using default files"
(require 'vc-git)
(projectile-test-with-sandbox
(projectile-test-with-files
("project/c/src/"
"project/c/include/"
"project/go/src/package1/"
"project/.projectile")
(cd "project")
(with-temp-file "go/src/package1/x.go" (insert "foo(bar)"))
(with-temp-file "c/include/x.h" (insert "typedef struct bar_t"))
(with-temp-file "c/src/x.c" (insert "struct bar_t *x"))
(dolist (test '(("go/src/package1/x.go" "foo" "*.go")
("c/src/x.c" "bar_t" "*.[ch]")
("c/include/x.h" "bar_t" "*.[ch]")))
(let ((projectile-use-git-grep t)
(current-prefix-arg '-)
(sym (cadr test)))
(spy-on 'projectile-project-vcs :and-return-value 'git)
(spy-on 'read-string :and-call-fake
(lambda (prompt initial-input history default-value &rest args)
(if (should (equal sym default-value)) default-value)))
(spy-on 'vc-git-grep :and-call-fake
(lambda (regexp files dir)
(progn (expect regexp :to-equal sym)
(expect files :to-equal (car (last test)))
(expect (projectile-project-root) :to-equal dir))))
(with-current-buffer (find-file-noselect (car test) t)
(save-excursion
(re-search-forward sym)
(projectile-grep nil ?-)))))))))
(describe "projectile-switch-project"
(it "fails if there are no projects"
(let ((projectile-known-projects nil))
(expect (projectile-switch-project) :to-throw))))
(describe "projectile-delete-dir-local-variable"
(it "Deletes existing dir-local variables"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.dir-locals.el"
"project/.projectile")
(append-to-file
"((nil . ((foo . bar))))" nil "project/.dir-locals.el")
(with-current-buffer (find-file-noselect "project/.projectile" t)
(let ((enable-local-variables :all))
(hack-dir-local-variables-non-file-buffer)
(expect (boundp 'foo) :to-be 't)
(projectile-delete-dir-local-variable nil 'foo)
(expect (boundp 'foo) :to-be nil) ))))))
(describe "projectile-add-dir-local-variable"
(it "Adds new dir-local variables"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.projectile")
(with-current-buffer (find-file-noselect "project/.projectile" t)
(let ((enable-local-variables :all))
(expect (boundp 'fooo) :to-be nil)
(projectile-add-dir-local-variable nil 'fooo 1)
(hack-dir-local-variables-non-file-buffer)
(expect (boundp 'fooo) :to-be 't)
(expect fooo :to-be 1) ))))))
(describe "projectile-add-dir-local-variable"
(it "Fails when there is no projectile project"
(projectile-test-with-sandbox
(let ((default-directory "/"))
(expect (projectile-add-dir-local-variable nil 'fooo 1) :to-throw 'error)) )))
(describe "projectile-delete-dir-local-variable"
(it "Fails when there is no projectile project"
(projectile-test-with-sandbox
(let ((default-directory "/"))
(expect (projectile-delete-dir-local-variable nil 'fooo 1) :to-throw 'error)) )))
(describe "projectile-switch-project-by-name"
(it "calls the switch project action with project-to-switch's dir-locals loaded"
(defvar switch-project-foo)
(let ((foo 'bar)
(switch-project-foo)
(safe-local-variable-values '((foo . baz)))
(projectile-switch-project-action (lambda () (setq switch-project-foo foo))))
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.dir-locals.el"
"project/.projectile")
(append-to-file
"((nil . ((foo . baz))))" nil "project/.dir-locals.el")
(projectile-add-known-project (file-name-as-directory (expand-file-name "project")))
(projectile-switch-project-by-name (file-name-as-directory (expand-file-name "project")))
(expect switch-project-foo :to-be 'baz)))))
(it "runs hooks from the project root directory"
(defvar hook-dir)
(let ((projectile-switch-project-action
(lambda () (switch-to-buffer (find-file-noselect "file" t))))
(hook (lambda () (setq hook-dir default-directory))))
(add-hook 'projectile-after-switch-project-hook hook)
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/file")
(let ((project-dir (file-name-as-directory (expand-file-name "project"))))
(projectile-add-known-project project-dir)
(projectile-switch-project-by-name project-dir)
(remove-hook 'projectile-after-switch-project-hook hook)
(expect hook-dir :to-equal project-dir))))))
(it "ensures the buffer is switched immediately"
(let ((projectile-switch-project-action
(lambda () (switch-to-buffer (find-file-noselect "file" t)))))
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/file")
(projectile-add-known-project (file-name-as-directory (expand-file-name "project")))
(projectile-switch-project-by-name (file-name-as-directory (expand-file-name "project")))
(expect (current-buffer) :to-be (get-file-buffer "project/file")))))))
(describe "projectile-ignored-buffer-p"
(it "checks if buffer should be ignored"
(let ((projectile-globally-ignored-buffers '("*nrepl messages*" "*something*")))
(expect (projectile-ignored-buffer-p (get-buffer-create "*nrepl messages*")) :to-be-truthy)
(expect (projectile-ignored-buffer-p (get-buffer-create "*something*")) :to-be-truthy)
(expect (projectile-ignored-buffer-p (get-buffer-create "test")) :not :to-be-truthy))))
(describe "projectile-get-other-files"
(it "returns files with same names but different extensions"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/test1.c"
"src/test2.c"
"src/test+copying.m"
"src/test1.cpp"
"src/test2.cpp"
"src/Makefile"
"src/test.vert"
"src/test.frag"
"src/same_name.c"
"src/some_module/same_name.c"
"include1/same_name.h"
"include1/test1.h"
"include1/test1.h~"
"include1/test2.h"
"include1/test+copying.h"
"include1/test1.hpp"
"include2/some_module/same_name.h"
"include2/test1.h"
"include2/test2.h"
"include2/test2.hpp"
"src/test1.service.js"
"src/test2.service.spec.js"
"include1/test1.service.spec.js"
"include2/test1.service.spec.js"
"include1/test2.js"
"include2/test2.js")
()
(let ((projectile-other-file-alist '(;; handle C/C++ extensions
("cpp" . ("h" "hpp" "ipp"))
("ipp" . ("h" "hpp" "cpp"))
("hpp" . ("h" "ipp" "cpp"))
("cxx" . ("hxx" "ixx"))
("ixx" . ("cxx" "hxx"))
("hxx" . ("ixx" "cxx"))
("c" . ("h"))
("m" . ("h"))
("mm" . ("h"))
("h" . ("c" "cpp" "ipp" "hpp" "m" "mm"))
("cc" . ("hh"))
("hh" . ("cc"))
;; vertex shader and fragment shader extensions in glsl
("vert" . ("frag"))
("frag" . ("vert"))
;; handle files with no extension
(nil . ("lock" "gpg"))
("lock" . (""))
("gpg" . (""))
;; handle files with nested extensions
("service.js" . ("service.spec.js"))
("js" . ("js")))))
(expect (projectile-get-other-files "src/test1.c") :to-equal '("include1/test1.h" "include2/test1.h"))
(expect (projectile-get-other-files "src/test1.cpp") :to-equal '("include1/test1.h" "include2/test1.h" "include1/test1.hpp"))
(expect (projectile-get-other-files "test2.c") :to-equal '("include1/test2.h" "include2/test2.h"))
(expect (projectile-get-other-files "test2.cpp") :to-equal '("include1/test2.h" "include2/test2.h" "include2/test2.hpp"))
(expect (projectile-get-other-files "test1.h") :to-equal '("src/test1.c" "src/test1.cpp" "include1/test1.hpp"))
(expect (projectile-get-other-files "test2.h") :to-equal '("src/test2.c" "src/test2.cpp" "include2/test2.hpp"))
(expect (projectile-get-other-files "include1/test1.h" t) :to-equal '("src/test1.c" "src/test1.cpp" "include1/test1.hpp"))
(expect (projectile-get-other-files "Makefile.lock") :to-equal '("src/Makefile"))
(expect (projectile-get-other-files "include2/some_module/same_name.h") :to-equal '("src/some_module/same_name.c" "src/same_name.c"))
;; nested extensions
(expect (projectile-get-other-files "src/test1.service.js") :to-equal '("include1/test1.service.spec.js" "include2/test1.service.spec.js"))
;; fallback to outer extensions if no rule for nested extension defined
(expect (projectile-get-other-files "src/test2.service.spec.js") :to-equal '("include1/test2.js" "include2/test2.js"))
(expect (projectile-get-other-files "src/test+copying.m") :to-equal '("include1/test+copying.h"))))))
(it "returns files based on the paths returned by :related-files-fn option"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/test1.cpp"
"src/test1.def"
"src/test2.def"
"src/test2.cpp"
"src/test2.h"
"src/test3.cpp"
"src/test3.h")
(:related-files-fn (lambda (file)
(cond ((equal file "src/test1.def") '(:other "src/test1.cpp"))
((equal file "src/test2.def") '(:other ("src/test2.cpp" "src/test2.h" "src/test4.h")))
((equal file "src/test3.cpp") '(:other nil)))))
(expect (projectile-get-other-files "src/test1.def") :to-equal '("src/test1.cpp"))
(expect (projectile-get-other-files "src/test2.def") :to-equal '("src/test2.cpp" "src/test2.h"))
;; Make sure extension based mechanism is still working
(expect (projectile-get-other-files "src/test2.cpp") :to-equal '("src/test2.h"))
;; Make sure that related-files-fn option has priority over existing mechanism
(expect (projectile-get-other-files "src/test3.cpp") :to-equal nil))))
(it "returns files based on the predicate returned by :related-files-fn option"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/test1.cpp"
"src/test1.def"
"src/test2.def"
"src/test2.cpp"
"src/test2.h"
"src/test3.cpp"
"src/test3.h")
(:related-files-fn
(lambda (file)
(cond ((equal file "src/test1.def")
(list :other (lambda (other-file)
(equal other-file "src/test1.cpp"))))
((equal file "src/test2.def")
(list :other (lambda (other-file)
(or (equal other-file "src/test2.cpp")
(equal other-file "src/test2.h")))))
((equal file "src/test3.cpp")
(list :other (lambda (other-file) nil))))))
(expect (projectile-get-other-files "src/test1.def") :to-equal '("src/test1.cpp"))
(expect (projectile-get-other-files "src/test2.def") :to-equal '("src/test2.cpp" "src/test2.h"))
;; Make sure extension based mechanism is still working
(expect (projectile-get-other-files "src/test2.cpp") :to-equal '("src/test2.h"))
;; Make sure that related-files-fn option has priority over existing mechanism
(expect (projectile-get-other-files "src/test3.cpp") :to-equal nil)))))
(describe "projectile-compilation-dir"
(it "returns the compilation directory for a project"
(defun helper (project-root rel-dir)
(spy-on 'projectile-project-root :and-return-value project-root)
(spy-on 'projectile-project-type :and-return-value 'generic)
(let ((projectile-project-compilation-dir rel-dir))
(projectile-compilation-dir)))
(expect (helper "/root/" "build") :to-equal "/root/build/")
(expect (helper "/root/" "build/") :to-equal "/root/build/")
(expect (helper "/root/" "./build") :to-equal "/root/build/")
(expect (helper "/root/" "local/build") :to-equal "/root/local/build/"))
(it "returns the default compilation dir based on project-type"
(projectile-register-project-type 'default-dir-project '("file.txt")
:compilation-dir "build")
(defun helper (project-root &optional rel-dir)
(spy-on 'projectile-project-root :and-return-value project-root)
(spy-on 'projectile-project-type :and-return-value 'default-dir-project)
(if (null rel-dir)
(projectile-compilation-dir)
(let ((projectile-project-compilation-dir rel-dir))
(projectile-compilation-dir))))
(expect (helper "/root/") :to-equal "/root/build/")
(expect (helper "/root/" "buildings") :to-equal "/root/buildings/"))
(it "should not fail on bad compilation dir config"
(defun -compilation-test-function ()
1)
(let ((projectile-project-type 'has-command-at-point)
(projectile-project-compilation-dir nil))
(projectile-register-project-type 'has-command-at-point '("file.txt")
:compile (-compilation-test-function))
(expect (projectile-compilation-dir) :to-equal (concat (expand-file-name ".") "/")))))
(describe "projectile-default-compilation-command"
(it "returns the default compilation command for project-type"
(defun -compilation-test-function ()
(if (= (point) 1)
"my-make"
"./run-extra"))
(projectile-register-project-type 'has-command-at-point '("file.txt")
:compile '-compilation-test-function)
(expect (projectile-default-compilation-command 'has-command-at-point) :to-equal "my-make")
(with-temp-buffer
(insert "ABCDE")
(goto-char 2)
(expect (projectile-default-compilation-command 'has-command-at-point) :to-equal "./run-extra")))
(it "fails on bad project-type config"
(defun -compilation-test-function ()
1)
(projectile-register-project-type 'has-command-at-point '("file.txt")
:compile (-compilation-test-function))
(expect (projectile-default-compilation-command 'has-command-at-point) :to-throw)))
(describe "projectile-detect-project-type"
(it "detects project-type for rails-like npm tests"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/Gemfile"
"project/app/"
"project/lib/"
"project/db/"
"project/config/"
"project/spec/"
"project/package.json")
(let ((projectile-indexing-method 'native))
(spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/")))
(expect (projectile-detect-project-type) :to-equal 'rails-rspec)))))
(it "detects project-type for elisp eldev projects"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/Eldev"
"project/project.el")
(let ((projectile-indexing-method 'native))
(spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/")))
(expect (projectile-detect-project-type) :to-equal 'emacs-eldev)))))
(it "detects project-type for projects with src dir and no other marker"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/src/")
(let ((projectile-indexing-method 'native))
(spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/")))
(expect (projectile-detect-project-type) :to-equal 'dotnet-sln)))))
(it "detects project-type for Julia PkgTemplates.jl projects"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/src/"
"project/Project.toml")
(let ((projectile-indexing-method 'native))
(spy-on 'projectile-project-root :and-return-value (file-truename (expand-file-name "project/")))
(expect (projectile-detect-project-type) :to-equal 'julia))))))
(describe "projectile-dirname-matching-count"
(it "counts matching dirnames ascending file paths"
(expect (projectile-dirname-matching-count "src/food/sea.c" "src/food/cat.c") :to-equal 2)
(expect (projectile-dirname-matching-count "src/weed/sea.c" "src/food/sea.c") :to-equal 0)
(expect (projectile-dirname-matching-count "test/demo-test.el" "demo.el") :to-equal 0)))
(describe "projectile--find-matching-test"
(it "finds matching test or file"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("app/models/weed/sea.rb"
"app/models/food/sea.rb"
"spec/models/weed/sea_spec.rb"
"spec/models/food/sea_spec.rb")
(:test-suffix "_spec")
(expect (projectile--find-matching-test "app/models/food/sea.rb") :to-equal '("spec/models/food/sea_spec.rb"))
(expect (projectile--find-matching-file "spec/models/food/sea_spec.rb") :to-equal '("app/models/food/sea.rb")))))
(it "finds matching test or file with dirs"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("source/foo/foo.service.js"
"source/bar/bar.service.js"
"spec/foo/foo.service.spec.js"
"spec/bar/bar.service.spec.js")
(:test-suffix ".spec" :test-dir "spec/" :src-dir "source/")
(expect (projectile--find-matching-test
"project/source/foo/foo.service.js")
:to-equal '("spec/foo/foo.service.spec.js"))
(expect (projectile--find-matching-file
"project/spec/bar/bar.service.spec.js")
:to-equal '("source/bar/bar.service.js")))))
(it "finds matching test with dirs and inexistent test file"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("project/src/main/scala/bar/package.scala"
"project/src/main/scala/foo/package.scala"
"project/src/test/scala/foo/packageSpec.scala")
(:test-suffix "Spec" :test-dir "test" :src-dir "main")
(expect (projectile--find-matching-test
"project/src/main/scala/bar/package.scala")
:to-equal '("src/test/scala/bar/packageSpec.scala")))))
(it "finds matching test or file based on the paths returned by :related-files-fn option"
(defun -my/related-files(file)
(if (string-match (rx (group (or "src" "test")) (group "/" (1+ anything) ".cpp")) file)
(if (equal (match-string 1 file ) "test")
(list :impl (concat "src" (match-string 2 file)))
(list :test (concat "test" (match-string 2 file))))))
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/Foo.cpp"
"src/Bar.cpp"
"src/Baz.py"
"test/Bar.cpp"
"test/Foo.cpp"
"other/Test_Baz.py")
(:related-files-fn #'-my/related-files :test-prefix "Test_")
(expect (projectile-test-file-p "test/Foo.cpp") :to-equal t)
(expect (projectile-test-file-p "src/Foo.cpp") :to-equal nil)
(expect (projectile--find-matching-test "src/Foo.cpp") :to-equal '("test/Foo.cpp"))
(expect (projectile--find-matching-test "src/Foo2.cpp") :to-equal nil)
(expect (projectile--find-matching-file "test/Foo.cpp") :to-equal '("src/Foo.cpp"))
(expect (projectile--find-matching-file "test/Foo2.cpp") :to-equal nil)
;; Make sure that existing mechanism(:test-prefix) still works
(expect (projectile-test-file-p "other/Test_Baz.py") :to-equal t)
(expect (projectile-test-file-p "other/Baz.py") :to-equal nil)
(expect (projectile--find-matching-file "other/Test_Baz.py") :to-equal '("src/Baz.py"))
(expect (projectile--find-matching-test "src/Baz.py") :to-equal '("other/Test_Baz.py")))))
(it "finds matching test or file by the predicate returned by :related-files-fn option"
(defun -my/related-files(file)
(cond ((equal file "src/Foo.cpp")
(list :test (lambda (other-file)
(equal other-file "test/Foo.cpp"))))
((equal file "test/Foo.cpp")
(list :impl (lambda (other-file)
(equal other-file "src/Foo.cpp"))))))
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/Foo.cpp"
"src/Bar.cpp"
"test/Bar.cpp"
"test/Foo.cpp")
(:related-files-fn #'-my/related-files)
(expect (projectile-test-file-p "test/Foo.cpp") :to-equal t)
(expect (projectile-test-file-p "src/Foo.cpp") :to-equal nil)
(expect (projectile--find-matching-test "src/Foo.cpp") :to-equal '("test/Foo.cpp"))
(expect (projectile--find-matching-test "src/Foo.cpp") :to-equal '("test/Foo.cpp"))
(expect (projectile--find-matching-file "test/Foo.cpp") :to-equal '("src/Foo.cpp")))))
(it "defers to test-dir property when it's set to a function"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo/Foo.cpp"
"src/bar/Foo.cpp"
"test/foo/FooTest.cpp")
(:test-dir
(lambda (file-path)
(projectile-complementary-dir file-path "src" "test"))
:test-suffix "Test")
(expect (projectile--find-matching-test
(projectile-expand-root "src/bar/Foo.cpp"))
:to-equal
(list "test/bar/FooTest.cpp")))))
(it "defers to src-dir property when it's set to a function"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo/Foo.cpp"
"src/bar/Foo.cpp"
"test/foo/FooTest.cpp")
(:src-dir
(lambda (file-path)
(projectile-complementary-dir file-path "test" "src"))
:test-suffix "Test")
(expect (projectile--find-matching-file
(projectile-expand-root "test/foo/FooTest.cpp"))
:to-equal
(list "src/foo/Foo.cpp")))))
(it "defers to a fallback using \"src\" and \"test\""
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("project.clj"
"src/example/core.clj"
"test/example/core2_test.clj")
(:test-suffix "_test")
(expect (projectile--find-matching-test
(projectile-expand-root "src/example/core.clj"))
:to-equal
(list "test/example/core_test.clj"))
(expect (projectile--find-matching-file
(projectile-expand-root "test/example/core2_test.clj"))
:to-equal
(list "src/example/core2.clj"))))))
(describe "projectile--related-files"
(it "returns related files for the given file"
(defun -my/related-files(file)
(cond ((equal file "src/Foo.c")
(list :test "src/TestFoo.c" :doc "doc/Foo.txt"))
((equal file "src/TestFoo.c")
(list :impl (lambda (other-file)
(equal other-file "src/Foo.c"))))))
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/Foo.c"
"src/TestFoo.c"
"doc/Foo.txt")
(:related-files-fn #'-my/related-files)
(expect (projectile--related-files-kinds "src/Foo.c") :to-equal '(:test :doc))
(expect (projectile--related-files-kinds "src/TestFoo.c") :to-equal '(:impl))
(expect (projectile--related-files "src/TestFoo.c" :impl) :to-equal '("src/Foo.c"))
(expect (projectile--related-files "src/Foo.c" :doc) :to-equal '("doc/Foo.txt"))
;; Support abspath
(expect (projectile--related-files-kinds (concat (projectile-project-root) "src/Foo.c")) :to-equal '(:test :doc))
(expect (projectile--related-files (concat (projectile-project-root) "src/Foo.c") :doc) :to-equal '("doc/Foo.txt"))))))
(describe "projectile--merge-related-files-fns"
(it "returns a new function which returns the merged plist from each fn"
(defun -first-fn(file)
(list :foo "file1"))
(defun -second-fn(file)
(list :foo (list "file2" "file3")))
(defun -third-fn(file)
(list :bar "file4"))
(let ((fn (projectile--merge-related-files-fns '(-first-fn -second-fn))))
(expect (funcall fn "something") :to-equal '(:foo ("file1" "file2" "file3"))))
(let ((fn (projectile--merge-related-files-fns '(-first-fn -third-fn))))
(expect (funcall fn "something") :to-equal '(:foo ("file1") :bar ("file4"))))))
(describe "projectile-related-files-fn-groups"
(it "generate related files fn which relates members of each group as a specified kind"
(let ((fn (projectile-related-files-fn-groups :foo '(("a.cpp" "req/a.txt" "doc/a.uml")
("b.cpp" "req/b.txt")))))
(expect (funcall fn "a.cpp") :to-equal '(:foo ("req/a.txt" "doc/a.uml")))
(expect (funcall fn "req/a.txt") :to-equal '(:foo ("a.cpp" "doc/a.uml")))
(expect (funcall fn "b.cpp") :to-equal '(:foo ("req/b.txt")))
(expect (funcall fn "c.cpp") :to-equal nil))))
(describe "projectile-related-files-fn-extensions"
(it "generate related files fn which relates files with the given extnsions"
(let* ((fn (projectile-related-files-fn-extensions :foo '("cpp" "h" "hpp")))
(plist (funcall fn "a.cpp"))
(predicate (plist-get plist :foo)))
(expect plist :to-contain :foo)
(expect (funcall predicate "a.h") :to-equal t)
(expect (funcall predicate "a.hpp") :to-equal t)
(expect (funcall predicate "b.cpp") :to-equal nil)
(expect (funcall predicate "a.cpp") :to-equal nil))))
(describe "projectile-related-files-fn-tests-with-prefix"
(it "generate related files fn which relates tests and impl based on extension and prefix"
(let ((fn (projectile-related-files-fn-test-with-prefix "py" "test_")))
(let* ((plist (funcall fn "foo/a.py"))
(predicate (plist-get plist :test)))
(expect plist :to-contain :test)
(expect (funcall predicate "bar/test_a.py") :to-equal t)
(expect (funcall predicate "bar/test_a.cpp") :to-equal nil))
(let* ((plist (funcall fn "foo/test_a.py"))
(predicate (plist-get plist :impl)))
(expect plist :to-contain :impl)
(expect (funcall predicate "bar/a.py") :to-equal t)
(expect (funcall predicate "bar/a.cpp") :to-equal nil)
(expect (funcall predicate "bar/test_a.cpp") :to-equal nil)))))
(describe "projectile-related-files-fn-tests-with-suffix"
(it "generate related files fn which relates tests and impl based on extension and suffix"
(let ((fn (projectile-related-files-fn-test-with-suffix "py" "-test")))
(let* ((plist (funcall fn "foo/a.py"))
(predicate (plist-get plist :test)))
(expect plist :to-contain :test)
(expect (funcall predicate "bar/a-test.py") :to-equal t)
(expect (funcall predicate "bar/a-test.cpp") :to-equal nil))
(let* ((plist (funcall fn "foo/a-test.py"))
(predicate (plist-get plist :impl)))
(expect plist :to-contain :impl)
(expect (funcall predicate "bar/a.py") :to-equal t)
(expect (funcall predicate "bar/a.cpp") :to-equal nil)
(expect (funcall predicate "bar/a-test.cpp") :to-equal nil)))))
(describe "projectile--related-files-plist-by-kind"
(defun -sample-predicate (other-file)
(equal other-file "src/foo.c"))
(defun -sample-predicate2 (other-file)
(equal other-file "src/bar.c"))
(describe "when :related-files-fn returns paths"
(it "returns a plist containing :paths only with the existing files on file system without duplication"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo.c")
(:related-files-fn (lambda (_)
(list :foo '("src/foo.c" "src/bar.c" "src/foo.c"))))
(expect (projectile--related-files-plist-by-kind "something" :foo)
:to-equal '(:paths ("src/foo.c")))))))
(describe "when :related-files-fn returns one predicate"
(it "returns a plist containing :predicate with the same predicate"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo.c") ; Contents does not matter
(:related-files-fn (lambda (_)
(list :foo '-sample-predicate)))
(expect (projectile--related-files-plist-by-kind "something" :foo)
:to-equal '(:predicate -sample-predicate))))))
(describe "when :related-files-fn returns multiple predicates"
(it "returns a plist containing :predicate with a merging predicate"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo.c") ; Contents does not matter
(:related-files-fn (lambda (_)
(list :foo (list '-sample-predicate '-sample-predicate2))))
(let* ((plist (projectile--related-files-plist-by-kind "something" :foo))
(predicate (plist-get plist :predicate)))
(expect plist :to-contain :predicate)
(expect (funcall predicate "src/foo.c") :to-equal t)
(expect (funcall predicate "src/bar.c") :to-equal t))))))
(describe "when :related-files-fn returns both paths and predicates"
(it "returns a plist containing both :paths and :predicates"
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo.c")
(:related-files-fn (lambda (_)
(list :foo '("src/foo.c" -sample-predicate))))
(expect (projectile--related-files-plist-by-kind "something" :foo)
:to-equal '(:paths ("src/foo.c") :predicate -sample-predicate))))))
(describe "when :related-files-fn is a list of functions"
(it "returns a plist containing the merged results"
(defun -sample-fn(file)
(list :foo "src/foo.c"))
(defun -sample-fn2(file)
(list :foo '-sample-predicate))
(projectile-test-with-sandbox
(projectile-test-with-files-using-custom-project
("src/foo.c"
"src/bar.c")
(:related-files-fn (list '-sample-fn '-sample-fn2))
(expect (projectile--related-files-plist-by-kind "something" :foo)
:to-equal '(:paths ("src/foo.c") :predicate -sample-predicate)))))))
(describe "projectile-get-all-sub-projects"
(it "excludes out-of-project submodules"
(projectile-test-with-sandbox
(projectile-test-with-files
(;; VCS root is here
"project/"
"project/.git/"
"project/.gitmodules"
;; Current project root is here:
"project/web-ui/"
"project/web-ui/.projectile"
;; VCS git submodule will return the following submodules,
;; relative to current project root, 'project/web-ui/':
"project/web-ui/vendor/client-submodule/"
"project/server/vendor/server-submodule/")
(let ((project (file-truename (expand-file-name "project/web-ui"))))
(spy-on 'projectile-files-via-ext-command :and-call-fake
(lambda (dir vcs)
(when (string= dir project)
'("vendor/client-submodule"
"../server/vendor/server-submodule"))))
(spy-on 'projectile-project-root :and-return-value project)
;; assert that it only returns the submodule 'project/web-ui/vendor/client-submodule/'
(expect (projectile-get-all-sub-projects project) :to-equal
(list (expand-file-name "vendor/client-submodule/" project))))))))
(describe "projectile-get-all-sub-projects-files"
(it "returns relative paths to submodule files"
(spy-on 'projectile-get-all-sub-projects :and-return-value '("/a/b/x/"))
(spy-on 'projectile-files-via-ext-command :and-return-value '("1.txt" "2.txt"))
(expect (projectile-get-sub-projects-files "/a/b" 'git) :to-equal
(list "x/1.txt" "x/2.txt"))))
(describe "projectile-configure-command"
(it "configure command for generic project type"
(spy-on 'projectile-default-configure-command :and-return-value nil)
(spy-on 'projectile-project-type :and-return-value 'generic)
(expect (projectile-configure-command "fsdf") :to-equal nil)))
;;; known projects tests
(describe "projectile-add-known-project"
:var (projectile-known-projects-file)
(before-each
(setq projectile-known-projects-file (projectile-test-tmp-file-path)))
(after-each
(delete-file projectile-known-projects-file nil))
(it "an added project should be added to the list of known projects"
(let ((projectile-known-projects nil))
(projectile-add-known-project "~/my/new/project/")
(expect projectile-known-projects :to-equal '("~/my/new/project/"))))
(it "adding a project should move it to the front of the list of known projects, if it already existed."
(let ((projectile-known-projects '("~/b/" "~/a/")))
(projectile-add-known-project "~/a/")
(expect projectile-known-projects :to-equal '("~/a/" "~/b/"))))
(it "~/project and ~/project/ should not be added separately to the known projects list"
(let ((projectile-known-projects '("~/project/")))
(projectile-add-known-project "~/project")
(expect projectile-known-projects :to-equal '("~/project/"))
(projectile-add-known-project "~/b")
(projectile-add-known-project "~/b/")
(expect projectile-known-projects :to-equal '("~/b/" "~/project/")))))
(describe "projectile-load-known-projects"
(it "loads known projects through serialization functions"
(let ((projectile-known-projects-file (projectile-test-tmp-file-path)))
(spy-on 'projectile-unserialize :and-return-value '("a1" "a2"))
(projectile-load-known-projects)
(expect 'projectile-unserialize :to-have-been-called-with projectile-known-projects-file)
(expect projectile-known-projects :to-equal '("a1" "a2")))))
(describe "projectile-merge-known-projects"
(it "merges known projects"
(let ((projectile-known-projects nil)
(projectile-known-projects-file (projectile-test-tmp-file-path)))
;; initialize saved known projects and load it from disk
(projectile-serialize '("a1" "a2" "a3" "a4" "a5")
projectile-known-projects-file)
(projectile-load-known-projects)
;; simulate other emacs session changes by: remove a2 a5 and adding b1 b2
(projectile-serialize '("a3" "b1" "a1" "a4" "b2")
projectile-known-projects-file)
;; remove a4 and add a6 and merge with disk
(setq projectile-known-projects '("a6" "a1" "a2" "a3" "a5"))
(projectile-merge-known-projects)
(delete-file projectile-known-projects-file nil)
(expect projectile-known-projects :to-equal '("a6" "a1" "a3" "b1" "b2"))))
(it "merges known projects to an empty file"
(let ((projectile-known-projects nil)
(projectile-known-projects-file (projectile-test-tmp-file-path)))
;; initialize saved known projects and load it from disk
(projectile-serialize '("a1" "a2" "a3" "a4" "a5")
projectile-known-projects-file)
(projectile-load-known-projects)
;; empty the on disk known projects list
(projectile-serialize '() projectile-known-projects-file)
;; merge
(projectile-merge-known-projects)
(delete-file projectile-known-projects-file nil)
(expect projectile-known-projects :to-equal '())))
(it "merges known projects from an empty file"
(let ((projectile-known-projects nil)
(projectile-known-projects-file (projectile-test-tmp-file-path)))
;; initialize saved known projects and load it from disk
(projectile-serialize '() projectile-known-projects-file)
(projectile-load-known-projects)
;; empty the on disk known projects list
(projectile-serialize '("a" "b" "c" "d") projectile-known-projects-file)
;; merge
(projectile-merge-known-projects)
(delete-file projectile-known-projects-file nil)
(expect projectile-known-projects :to-equal '("a" "b" "c" "d"))))
(it "merges known projects while keeping their order"
(let ((projectile-known-projects nil)
(projectile-known-projects-file (projectile-test-tmp-file-path)))
;; initialize saved known projects and load it from disk
(projectile-serialize '("a" "b" "c" "d") projectile-known-projects-file)
(projectile-load-known-projects)
;; save the same list in different order
(projectile-serialize '("d" "c" "b" "a") projectile-known-projects-file)
;; merge
(projectile-merge-known-projects)
(delete-file projectile-known-projects-file nil)
(expect projectile-known-projects :to-equal '("a" "b" "c" "d")))))
(describe "projectile-save-known-projects"
(it "saves known projects through serialization functions"
(let ((projectile-known-projects-file (projectile-test-tmp-file-path))
(projectile-known-projects '(floop)))
(spy-on 'projectile-serialize)
(projectile-save-known-projects)
(expect 'projectile-serialize :to-have-been-called-with '(floop) projectile-known-projects-file))))
(describe "projectile-serialization-functions"
(it "tests that serialization functions can save/restore data to the filesystem"
(let ((this-test-file (projectile-test-tmp-file-path)))
(unwind-protect
(progn
(projectile-serialize '(some random data) this-test-file)
(expect (projectile-unserialize this-test-file) :to-equal '(some random data))
(when (file-exists-p this-test-file)
(delete-file this-test-file)))))))
(describe "projectile-clear-known-projects"
(it "clears known projects"
(let ((projectile-known-projects '("one" "two" "three"))
(projectile-known-projects-file (projectile-test-tmp-file-path)))
(projectile-clear-known-projects)
(expect projectile-known-projects :to-equal nil))))
(describe "projectile-reset-known-projects"
(it "resets known projects"
(spy-on 'projectile-clear-known-projects)
(spy-on 'projectile-discover-projects-in-search-path)
(projectile-reset-known-projects)
(expect 'projectile-clear-known-projects :to-have-been-called)
(expect 'projectile-discover-projects-in-search-path :to-have-been-called)))
(describe "projectile-test-ignored-directory-p"
(it "ignores specified literal directory values"
(spy-on 'projectile-ignored-directories :and-return-value '("/path/to/project/tmp"))
(expect (projectile-ignored-directory-p "/path/to/project/tmp") :to-be-truthy)
(expect (projectile-ignored-directory-p "/path/to/project/log") :not :to-be-truthy))
(it "ignores specified regex directory values"
(spy-on 'projectile-ignored-directories :and-return-value '("/path/to/project/t\\.*"))
(expect (projectile-ignored-directory-p "/path/to/project/tmp") :to-be-truthy)
(expect (projectile-ignored-directory-p "/path/to/project/log") :not :to-be-truthy)))
(describe "projectile-relevant-known-projects"
:var 'known-projects
(before-all
(setq known-projects '("~/foo/" "~/bar/" "~/baz/")))
(describe "when projectile-current-project-on-switch is 'remove"
(it "removes the current project"
(spy-on 'projectile-project-root :and-return-value "~/foo/")
(let ((projectile-current-project-on-switch 'remove)
(projectile-known-projects known-projects))
(expect (projectile-relevant-known-projects) :to-equal '("~/bar/" "~/baz/")))))
(describe "when projectile-current-project-on-switch is 'move-to-end"
(it "moves the current project to the end of projectile-known-projects"
(spy-on 'projectile-project-root :and-return-value "~/foo/")
(let ((projectile-current-project-on-switch 'move-to-end)
(projectile-known-projects known-projects))
(expect (projectile-relevant-known-projects) :to-equal '("~/bar/" "~/baz/" "~/foo/")))))
(describe "when projectile-current-project-on-switch is 'keep"
(it "returns projectile-known-projects"
(spy-on 'projectile-project-root :and-return-value "~/foo/")
(let ((projectile-current-project-on-switch 'keep)
(projectile-known-projects known-projects))
(expect (projectile-relevant-known-projects) :to-equal '("~/foo/" "~/bar/" "~/baz/"))))))
(describe "projectile-relevant-open-projects"
(describe "when projectile-current-project-on-switch is 'remove"
(it "removes the current project"
(spy-on 'projectile-open-projects :and-return-value '("~/foo/" "~/bar/" "~/baz/"))
(spy-on 'projectile-project-root :and-return-value "~/foo/")
(let ((projectile-current-project-on-switch 'remove))
(expect (projectile-relevant-open-projects) :to-equal '("~/bar/" "~/baz/")))))
(describe "when projectile-current-project-on-switch is 'move-to-end"
(it "moves the current project to the end of projectile-known-projects"
(spy-on 'projectile-open-projects :and-return-value '("~/foo/" "~/bar/" "~/baz/"))
(spy-on 'projectile-project-root :and-return-value "~/foo/")
(let ((projectile-current-project-on-switch 'move-to-end))
(expect (projectile-relevant-open-projects) :to-equal '("~/bar/" "~/baz/" "~/foo/")))))
(describe "when projectile-current-project-on-switch is 'keep"
(it "returns projectile-open-projects"
(spy-on 'projectile-project-root :and-return-value "~/foo/")
(spy-on 'projectile-open-projects :and-return-value '("~/foo/" "~/bar/" "~/baz/"))
(let ((projectile-current-project-on-switch 'keep))
(expect (projectile-relevant-open-projects) :to-equal '("~/foo/" "~/bar/" "~/baz/"))))))
;;; Mode line tests
(describe "projectile-default-mode-line"
(it "includes the project name and type when in a project"
(spy-on 'projectile-project-name :and-return-value "foo")
(spy-on 'projectile-project-type :and-return-value "bar")
(expect (projectile-default-mode-line) :to-equal " Projectile[foo:bar]"))
(it "returns also a - if called outside a project"
(spy-on 'projectile-project-name :and-return-value nil)
(spy-on 'projectile-project-type :and-return-value nil)
(expect (projectile-default-mode-line) :to-equal " Projectile[-]"))
(it "respects the value of projectile-mode-line-prefix"
(spy-on 'projectile-project-name :and-return-value "foo")
(spy-on 'projectile-project-type :and-return-value "bar")
(let ((projectile-mode-line-prefix " Pro"))
(expect (projectile-default-mode-line) :to-equal " Pro[foo:bar]"))))
(describe "projectile--directory-p"
(it "tests which directory exists"
(expect (projectile--directory-p nil) :to-be nil)
(expect (projectile--directory-p "asdf") :to-be nil)
(expect (projectile--directory-p "/") :to-be-truthy)))
(describe "projectile-find-file-in-directory"
(it "fails when called in a non-existing directory"
(expect (projectile-find-file-in-directory "asdf") :to-throw)))
(describe "projectile-dir-files-native"
(it "calculates ignored files and directories only once during recursion"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/"
"projectA/.svn/"
"projectA/src/.svn/"
"projectA/src/html/.svn/"
"projectA/.git/"
"projectA/src/html/"
"projectA/src/framework/lib/"
"projectA/src/framework.conf"
"projectA/src/html/index.html"
"projectA/.projectile")
;; verify that indexing only invokes these funcs once during recursion
(spy-on 'projectile-ignored-files :and-call-through)
(spy-on 'projectile-ignored-directories :and-call-through)
(spy-on 'projectile-globally-ignored-directory-names :and-call-through)
(projectile-dir-files-native "projectA/")
(expect 'projectile-ignored-files :to-have-been-called-times 1)
(expect 'projectile-globally-ignored-directory-names :to-have-been-called-times 1)
(expect 'projectile-ignored-directories :to-have-been-called-times 1))))
(it "ignores globally ignored directories when using native indexing"
(projectile-test-with-sandbox
(projectile-test-with-files
("project/"
"project/.ignoreme/"
"project/.ignoreme/should_ignore"
"project/src/"
"project/src/.ignoreme/"
"project/src/.ignoreme/should_ignore"
"project/config.conf")
(setq projectile-globally-ignored-directories '(".ignoreme"))
(expect (projectile-dir-files-native "project") :to-equal '("config.conf"))))))
(describe "projectile-process-current-project-buffers-current"
(it "expects projectile-process-current-project-buffers and
projectile-process-current-project-buffers-current to have similar behaviour"
(projectile-test-with-sandbox
(projectile-test-with-files
("projectA/"
"projectA/.projectile"
"projectA/bufferA"
"projectA/fileA"
"projectA/dirA/"
"projectA/dirA/fileC")
(let ((list-a '())
(list-b '()))
(projectile-process-current-project-buffers (lambda (b) (push b list-a)))
(projectile-process-current-project-buffers-current (lambda () (push (current-buffer) list-b)))
(expect list-a :to-equal list-b))))))
(describe "projectile-project-buffers"
(it "return project buffers"
(projectile-test-with-sandbox
(projectile-test-with-files
("project1/"
"project1/.projectile"
"project1/foo")
(cd "project1")
(with-current-buffer (find-file-noselect "foo" t))
(expect (length (projectile-project-buffers)) :to-equal 1)))))
(describe "projectile--impl-name-for-test-name"
:var ((mock-projectile-project-types
'((foo test-suffix "Test")
(bar test-prefix "Test"))))
(it "removes suffix from test file"
(cl-letf (((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-name-for-test-name "FooTest.cpp")
:to-equal
"Foo.cpp")))
(it "removes prefix from test file"
(cl-letf (((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'bar))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-name-for-test-name "TestFoo.cpp")
:to-equal
"Foo.cpp"))))
(describe "projectile-find-implementation-or-test"
(it "error when test file does not exist and projectile-create-missing-test-files is nil"
(cl-letf (((symbol-function 'projectile-test-file-p) #'ignore)
((symbol-function 'file-exists-p) #'ignore)
((symbol-function 'projectile-expand-root) #'identity)
((symbol-function 'projectile-find-matching-test) (lambda (file) "dir/foo"))
(projectile-create-missing-test-files nil)
(projectile-project-type 'foo))
(expect (projectile-find-implementation-or-test "foo") :to-throw))))
(describe "projectile--impl-file-from-src-dir-fn"
:var ((mock-projectile-project-types
'((foo src-dir (lambda (impl-file) "/outer/foo/test/dir"))
(bar src-dir "not a function"))))
(it "returns result of projectile--complementary-file when src-dir property is a function"
(cl-letf (((symbol-function 'projectile--complementary-file)
(lambda (impl-file dir-fn file-fn) (funcall dir-fn impl-file)))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
((symbol-function 'file-relative-name) (lambda (f rel) f))
((symbol-function 'file-exists-p) (lambda (file) t))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-file-from-src-dir-fn "foo") :to-equal "/outer/foo/test/dir")))
(it "returns file relative to project root"
(cl-letf (((symbol-function 'projectile--complementary-file)
(lambda (impl-file dir-fn file-fn) (funcall dir-fn impl-file)))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "/outer/foo"))
((symbol-function 'file-exists-p) (lambda (file) t))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-file-from-src-dir-fn "/outer/foo/bar")
:to-equal
"test/dir")))
(it "returns nil when src-dir property is a not function"
(cl-letf (((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'bar))
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-file-from-src-dir-fn "bar") :to-equal nil)))
(it "returns nil when src-dir function result is not an existing file"
(cl-letf (((symbol-function 'projectile--complementary-file)
(lambda (impl-file dir-fn file-fn) (funcall dir-fn impl-file)))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "/outer/foo"))
((symbol-function 'file-exists-p) #'ignore)
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-file-from-src-dir-fn "bar") :to-equal nil))))
(describe "projectile--test-file-from-test-dir-fn"
:var ((mock-projectile-project-types
'((foo test-dir (lambda (impl-file) "/outer/foo/test/dir"))
(bar test-dir "not a function"))))
(it "returns result of projectile--complementary-file when test-dir property is a function"
(cl-letf (((symbol-function 'projectile--complementary-file)
(lambda (impl-file dir-fn file-fn) (funcall dir-fn impl-file)))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
((symbol-function 'file-relative-name) (lambda (f rel) f))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--test-file-from-test-dir-fn "foo") :to-equal "/outer/foo/test/dir")))
(it "returns file relative to project root"
(cl-letf (((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "/outer/foo"))
((symbol-function 'projectile--complementary-file)
(lambda (impl-file dir-fn file-fn) (funcall dir-fn impl-file)))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--test-file-from-test-dir-fn "/outer/foo/bar")
:to-equal
"test/dir")))
(it "returns nil when test-dir property is a not function"
(cl-letf (((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'bar))
(projectile-project-types mock-projectile-project-types)
((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo")))
(expect (projectile--test-file-from-test-dir-fn "bar") :to-equal nil))))
(describe "projectile--complementary-file"
(it "dir-fn and filename-fn applied correctly"
(cl-letf (((symbol-function 'file-exists-p) (lambda (file) t))
((symbol-function 'dir-fn) (lambda (dir) "foo/test/dir"))
((symbol-function 'filename-fn) (lambda (filename) "Foo.test")))
(expect (projectile--complementary-file
"foo/src/dir/Foo.impl"
#'dir-fn
#'filename-fn)
:to-equal "foo/test/dir/Foo.test"))))
(describe "projectile--impl-to-test-dir"
:var ((mock-projectile-project-types
'((foo test-dir "test" src-dir "src")
(bar test-dir identity src-dir "src"))))
(it "replaces occurrences of src-dir with test-dir"
(cl-letf (((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-to-test-dir "/foo/src/Foo") :to-equal "/foo/test/")))
(it "nil returned when test-dir property is not a string"
(cl-letf (((symbol-function 'projectile-project-root) (lambda (&optional _dir) "bar"))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'bar))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-to-test-dir "/bar/src/bar") :to-be nil)))
(it "error when src-dir not a substring of impl file"
(cl-letf (((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--impl-to-test-dir "/bar/other/bar") :to-throw))))
(describe "projectile--test-to-impl-dir"
:var ((mock-projectile-project-types
'((foo test-dir "test" src-dir "src")
(bar test-dir "test" src-dir identity))))
(it "replaces occurrences of test-dir with src-dir"
(cl-letf (((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--test-to-impl-dir "/foo/test/Foo") :to-equal "/foo/src/")))
(it "nil returned when src-dir property is not a string"
(cl-letf (((symbol-function 'projectile-project-root) (lambda (&optional _dir) "bar"))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'bar))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--test-to-impl-dir "/bar/test/bar") :to-be nil)))
(it "error when test-dir not a substring of test file"
(cl-letf (((symbol-function 'projectile-project-root) (lambda (&optional _dir) "foo"))
((symbol-function 'projectile-project-type) (lambda (&optional _dir) 'foo))
(projectile-project-types mock-projectile-project-types))
(expect (projectile--test-to-impl-dir "/bar/other/bar") :to-throw))))
(describe "projectile-run-shell-command-in-root"
(describe "when called directly in elisp"
(before-each (spy-on 'shell-command))
(describe "when called with all three parameters"
(it "expects to call shell-command with the same parameters"
(projectile-run-shell-command-in-root "cmd" "output-buffer" "error-buffer")
(expect 'shell-command :to-have-been-called-with "cmd" "output-buffer" "error-buffer")))
(describe "when called with only one optional parameter"
(it "expects to call shell-command with the same parameters"
(projectile-run-shell-command-in-root "cmd" "output-buffer")
(expect 'shell-command :to-have-been-called-with "cmd" "output-buffer" nil)))
(describe "when called with no optional parameters"
(it "expects to call shell-command with the same parameters"
(projectile-run-shell-command-in-root "cmd")
(expect 'shell-command :to-have-been-called-with "cmd" nil nil))))
(describe "when called interactively"
(before-each (spy-on 'shell-command))
(it "expects to be interactive"
(expect (interactive-form 'projectile-run-shell-command-in-root) :not :to-be nil))
(it "expects to call shell-command with the given command"
(funcall-interactively 'projectile-run-shell-command-in-root "cmd")
(expect 'shell-command :to-have-been-called-with "cmd" nil nil))))
(describe "projectile-run-async-shell-command-in-root"
(describe "when called directly in elisp"
(before-each (spy-on 'async-shell-command))
(describe "when called with all three parameters"
(it "expects to call async-shell-command with the same parameters"
(projectile-run-async-shell-command-in-root "cmd" "output-buffer" "error-buffer")
(expect 'async-shell-command :to-have-been-called-with "cmd" "output-buffer" "error-buffer")))
(describe "when called with only one optional parameter"
(it "expects to call async-shell-command with the same parameters"
(projectile-run-async-shell-command-in-root "cmd" "output-buffer")
(expect 'async-shell-command :to-have-been-called-with "cmd" "output-buffer" nil)))
(describe "when called with no optional parameters"
(it "expects to call async-shell-command with the same parameters"
(projectile-run-async-shell-command-in-root "cmd")
(expect 'async-shell-command :to-have-been-called-with "cmd" nil nil))))
(describe "when called interactively"
(before-each (spy-on 'async-shell-command))
(it "expects to be interactive"
(expect (interactive-form 'projectile-run-async-shell-command-in-root) :not :to-be nil))
(it "expects to call async-shell-command with the given command"
(funcall-interactively 'projectile-run-async-shell-command-in-root "cmd")
(expect 'async-shell-command :to-have-been-called-with "cmd" nil nil))))
(describe "projectile--run-project-cmd"
(it "command history is not duplicated"
(spy-on 'projectile-run-compilation)
(spy-on 'projectile-maybe-read-command :and-call-fake
(lambda (arg default-cmd prompt) default-cmd))
;; Stops projectile--run-project-cmd from creating a new directory for
;; the compilation dir
(spy-on 'file-directory-p :and-return-value t)
(let ((command-map (make-hash-table :test 'equal))
;; history is based on the project root, so we set it to a random
;; path to ensure there are no existing commands in history
(projectile-project-root "/a/random/path"))
(projectile--run-project-cmd "foo" command-map)
(projectile--run-project-cmd "foo" command-map)
(projectile--run-project-cmd "foo" command-map)
(projectile--run-project-cmd "bar" command-map)
(expect 'projectile-run-compilation :to-have-been-called-times 4)
(expect (ring-elements
(projectile--get-command-history projectile-project-root))
:to-equal '("bar" "foo")))))
(describe "projectile-test-prefix"
:var ((mock-projectile-project-types
'((foo test-prefix "Test"))))
(it "gets set test-prefix"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo))
(expect (projectile-test-prefix'foo) :to-equal "Test")))
(it "uses local override"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo)
(projectile-project-test-prefix "Spec"))
(expect (projectile-test-prefix 'foo) :to-equal "Spec"))))
(describe "projectile-test-suffix"
:var ((mock-projectile-project-types
'((foo test-suffix "Test"))))
(it "gets set test-suffix"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo))
(expect (projectile-test-suffix'foo) :to-equal "Test")))
(it "uses local override"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo)
(projectile-project-test-suffix "Spec"))
(expect (projectile-test-suffix 'foo) :to-equal "Spec"))))
(describe "projectile-related-files-fn"
:var ((mock-projectile-project-types
'((foo related-files-fn ignore))))
(it "gets set related-files-fn"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo))
(expect (projectile-related-files-fn 'foo) :to-equal #'ignore)))
(it "uses local override"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo)
(projectile-project-related-files-fn #'identity))
(expect (projectile-related-files-fn 'foo) :to-equal #'identity))))
(describe "projectile-test-directory"
:var ((mock-projectile-project-types
'((foo test-dir "test"))))
(it "gets set test directory"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo))
(expect (projectile-test-directory 'foo) :to-equal "test")))
(it "uses local override"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo)
(projectile-project-test-dir "other"))
(expect (projectile-test-directory 'foo) :to-equal "other"))))
(describe "projectile-src-directory"
:var ((mock-projectile-project-types
'((foo src-dir "src"))))
(it "gets set src directory"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo))
(expect (projectile-src-directory 'foo) :to-equal "src")))
(it "uses local override"
(let ((projectile-project-types mock-projectile-project-types)
(projectile-project-type 'foo)
(projectile-project-src-dir "other"))
(expect (projectile-src-directory 'foo) :to-equal "other"))))
;; A bunch of tests that make sure Projectile commands handle
;; gracefully the case of being run outside of a project.
(assert-friendly-error-when-no-project projectile-project-info)
(assert-friendly-error-when-no-project projectile-display-buffer)
(assert-friendly-error-when-no-project projectile-find-implementation-or-test-other-frame)
(assert-friendly-error-when-no-project projectile-find-implementation-or-test-other-window)
(assert-friendly-error-when-no-project projectile-find-other-file)
(assert-friendly-error-when-no-project projectile-find-other-file-other-frame)
(assert-friendly-error-when-no-project projectile-find-other-file-other-window)
(assert-friendly-error-when-no-project projectile-find-test-file)
(assert-friendly-error-when-no-project projectile-grep)
(assert-friendly-error-when-no-project projectile-ibuffer)
(assert-friendly-error-when-no-project projectile-project-buffers-other-buffer)
(assert-friendly-error-when-no-project projectile-project-info)
(assert-friendly-error-when-no-project projectile-regenerate-tags)
(assert-friendly-error-when-no-project projectile-remove-current-project-from-known-projects)
(assert-friendly-error-when-no-project projectile-switch-to-buffer)
(assert-friendly-error-when-no-project projectile-switch-to-buffer-other-frame)
(assert-friendly-error-when-no-project projectile-switch-to-buffer-other-window)
|