7

I'm using Emacs and projectile and trying to set the path to a test file so that I can use projectile-toggle-between-implementation-and-test.

I call M-xprojectile-find-test-file but the Helm buffer is empty. Whatever I put in the pattern field, no file shows up in the Helm buffer to select and entering the test file path relative to the project root just causes a Dired buffer to open at the project root.

Please advise how to use projectile-find-test-file

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
blokeley
  • 6,726
  • 9
  • 53
  • 75

2 Answers2

7

projectile guesses the test file name given a source file by adding suffix/prefix to your source file. For example, if you have a hello.rb then it tries to find a file called hello_test.rb in your project.

It by default has some rules that maps from project type to test suffix/prefix. Here is the actual code:

(defun projectile-test-suffix (project-type)
  "Find default test files suffix based on PROJECT-TYPE."
  (cond
   ((member project-type '(rails-rspec ruby-rspec)) "_spec")
   ((member project-type '(rails-test ruby-test lein-test go)) "_test")
   ((member project-type '(scons)) "test")
   ((member project-type '(maven symfony)) "Test")
   ((member project-type '(gradle grails)) "Spec")))

If you don't see anything in projectile-find-test-file that means most likely it cannot find anything related to your source file by adding suffix/prefix.

This can be customized via projectile-test-suffix-function. By default the variable points to the function above, but you can override it with your own rules.

Zhengyang
  • 354
  • 3
  • 5
  • For future people reading this, in the years since this was originally posted, it appears that the code specifying the projectile data structures holding the project type information has changed. For example, the data for the `rails-rspec` is stored as a property list using a call to `projectile-register-project-type`, and then the suffix information is picked off by a call to `projectile-project-type-attribute` through `projectile-test-suffix`. Just in case you were looking for the above code like I was. – dpritch May 07 '19 at 23:18
  • As well as @Yiman's answer, I also found https://www.philnewton.net/blog/exploring-emacs-projectile/ to be helpful. – dpritch May 07 '19 at 23:19
4

This isn't a great answer to your question, but projectile-find-test-file was just hanging for me before I even got a prompt. I didn't have the inclination to figure out what was going on there so I just created:

(defun my-test-prefix (project-type) "test_")

and customized projectile-test-function-prefix to point to that. That got projectile-toggle-between-implementation-and-test working for me which is all I really needed.

Turn
  • 6,656
  • 32
  • 41
  • 2
    In the years since this was originally posted, it appears that the name of the projectile variable specifying the custom test prefix function has been renamed to `projectile-test-prefix-function` – dpritch May 07 '19 at 23:05