Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Function for :src-dir #1679

Merged
merged 4 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for src-dir as a function logic
Add tests for projectile--test-name-for-impl-name,
projectile--impl-file-from-src-dir-fn and add test for
projectile--find-matching-file to check precedence logic is as expected.
  • Loading branch information
LaurenceWarne committed Jun 22, 2021
commit 03efb96ebb4fa24fa8ca002483f68db4d8485d18
4 changes: 3 additions & 1 deletion projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -3300,7 +3300,9 @@ PROJECT-ROOT is the targeted directory. If nil, use
(t 'none)))

(defun projectile--test-name-for-impl-name (impl-file-path)
"Determine the name of the test file for IMPL-FILE-PATH."
"Determine the name of the test file for IMPL-FILE-PATH.

IMPL-FILE-PATH may be a absolute path, relative path or a file name."
(let* ((project-type (projectile-project-type))
(impl-file-name (file-name-sans-extension (file-name-nondirectory impl-file-path)))
(impl-file-ext (file-name-extension impl-file-path))
Expand Down
73 changes: 71 additions & 2 deletions test/projectile-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1219,15 +1219,30 @@ Just delegates OPERATION and ARGS for all operations except for`shell-command`'.
(projectile-test-with-files-using-custom-project
("src/foo/Foo.cpp"
"src/bar/Foo.cpp"
"src/foo/FooTest.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"))))))
(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"))))))

(describe "projectile--related-files"
(it "returns related files for the given file"
Expand Down Expand Up @@ -1672,6 +1687,23 @@ projectile-process-current-project-buffers-current to have similar behaviour"
(projectile-process-current-project-buffers-current (lambda () (push (current-buffer) list-b)))
(expect list-a :to-equal list-b))))))

(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 () '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 () '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)
Expand All @@ -1681,6 +1713,43 @@ projectile-process-current-project-buffers-current to have similar behaviour"
(projectile-create-missing-test-files nil))
(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 () 'foo))
((symbol-function 'projectile-project-root) (lambda () "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 () 'foo))
((symbol-function 'projectile-project-root) (lambda () "/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 () 'bar))
((symbol-function 'projectile-project-root) (lambda () "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 () 'foo))
((symbol-function 'projectile-project-root) (lambda () "/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"))
Expand Down