diff options
| author | Eugene Tan <jmingtan@gmail.com> | 2018-10-19 00:20:55 +0800 |
|---|---|---|
| committer | Bozhidar Batsov <bozhidar.batsov@gmail.com> | 2018-10-18 20:17:48 +0200 |
| commit | 6cb9524b67a4041def7c6d065608032d21eddf02 (patch) | |
| tree | 5e8eaa4f67ed8ccaa8bfe79f6c3e2cd33fd4d820 /test | |
| parent | ca55203e09853aa57abd5eda1c8dcce1e9816c7b (diff) | |
Migrate some ert tests
Diffstat (limited to 'test')
| -rw-r--r-- | test/projectile-legacy-test.el | 121 | ||||
| -rw-r--r-- | test/projectile-test.el | 119 |
2 files changed, 119 insertions, 121 deletions
diff --git a/test/projectile-legacy-test.el b/test/projectile-legacy-test.el index bebbcb0..635ab85 100644 --- a/test/projectile-legacy-test.el +++ b/test/projectile-legacy-test.el @@ -448,127 +448,6 @@ (projectile-file-exists-cache-cleanup) (should-not projectile-file-exists-cache-timer))))))) -(ert-deftest projectile-test-cache-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) - (noflet ((projectile-project-root () (file-truename default-directory)) - (projectile-project-vcs (project) 'none)) - (with-current-buffer (find-file-noselect "file2.el" t) - (projectile-cache-current-file) - (dolist (f '("file1.el" "file2.el")) - (should (member f (gethash (projectile-project-root) - projectile-projects-cache))))) - (with-current-buffer (find-file-noselect "file3.el" t) - (projectile-cache-current-file) - (dolist (f '("file1.el" "file2.el" "file3.el")) - (should (member f (gethash (projectile-project-root) - projectile-projects-cache))))) - (with-current-buffer (find-file-noselect "file4.el" t) - (projectile-cache-current-file) - (dolist (f '("file1.el" "file2.el" "file3.el" "file4.el")) - (should (member f (gethash (projectile-project-root) - projectile-projects-cache)))))))))) - -(ert-deftest projectile-test-cache-expiring () - "Ensure 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) - - (noflet ((projectile-project-root () (file-truename default-directory)) - (projectile-project-vcs (project) '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")) - (should (member f (gethash (projectile-project-root) - projectile-projects-cache))))))))) - -(ert-deftest projectile-test-old-project-root-gone () - "Ensure 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) - (should (string= (projectile-project-root) correct-project-root)))))) - -(ert-deftest projectile-test-git-grep-prefix () - (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))) - (noflet ((projectile-project-vcs () 'git) - (read-string (prompt initial-input history default-value &rest args) - (if (should (equal sym default-value)) default-value)) - (vc-git-grep (regexp files dir) - (progn (should (equal sym regexp)) - (should (equal (car (last test)) files)) - (should (equal (projectile-project-root) dir))))) - (with-current-buffer (find-file-noselect (car test) t) - (save-excursion - (re-search-forward sym) - (projectile-grep nil ?-))))))))) - -(ert-deftest projectile-switch-project-no-projects () - (let ((projectile-known-projects nil)) - (should-error (projectile-switch-project)))) - -(ert-deftest projectile-ignored-buffer-p-by-name () - (let ((projectile-globally-ignored-buffers '("*nrepl messages*" "*something*"))) - (should (projectile-ignored-buffer-p (get-buffer-create "*nrepl messages*"))) - (should (projectile-ignored-buffer-p (get-buffer-create "*something*"))) - (should-not (projectile-ignored-buffer-p (get-buffer-create "test"))))) - (defun projectile-test-tmp-file-path () "Return a filename suitable to save data to in the test temp directory" diff --git a/test/projectile-test.el b/test/projectile-test.el index be73388..4050894 100644 --- a/test/projectile-test.el +++ b/test/projectile-test.el @@ -100,6 +100,125 @@ test temp directory" (it "caches the project type" (expect (gethash (projectile-project-root) projectile-project-type-cache) :to-equal 'emacs-cask))) +(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 () (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-project-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" + (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-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" (let ((projectile-other-file-alist '(;; handle C/C++ extensions |
