-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
projectile-test.el
2279 lines (2117 loc) · 115 KB
/
projectile-test.el
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
;;; projectile-test.el --- Projectile's test suite -*- lexical-binding: t -*-
;; Copyright © 2011-2024 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)
;; 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))
;; Load the file under test
(load (expand-file-name "projectile" source-directory))
(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/")
(file-name-directory (locate-library "projectile.el" t)))))
(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")))