Projectile: Create test file when it does not exists

Created on 2 Jul 2014  路  3Comments  路  Source: bbatsov/projectile

I'm using projectile with clojure. Before, I've used clojure-test-mode.el, and it has a clojure-jump-to-test function that works more or less like projectile-toggle-between-implementation-and-test.

However, one nice thing about clojure-jump-to-test is that, if there is no test for the current file, it visits a new file where the test file should be, whereas projectile-toggle-between-implementation-and-test returns an error No matching test file found.

I think it would be nice to have similar functionality in projectile. Right now, I've made a workaround by advising projectile-toggle-between-implementation-and-test, but that's not the best solution for sure.

For reference my workaround is:

(defun clojure-test-filename ()
  (concat (projectile-project-root) 
          "test/" 
          (mapconcat #'identity
                     (butlast (split-string (cider-current-ns) "\\.")) "/")
          "/"
          (file-name-nondirectory (file-name-sans-extension (buffer-file-name)))
          "_test.clj"))

(defadvice projectile-toggle-between-implementation-and-test (around create-clojure-test-advice)
  "Visit new file if can't find test"
  (condition-case nil
      ad-do-it
    (error (find-file (clojure-test-filename)))))

(ad-activate 'projectile-toggle-between-implementation-and-test)

Most helpful comment

I think it might be nice if instead of showing the error it prompted for a filename to create using whatever test names it tried. That would both cover the clojure case and any other languages. It might even cover the case of creating the source file if only the test was created.

All 3 comments

I think it might be nice if instead of showing the error it prompted for a filename to create using whatever test names it tried. That would both cover the clojure case and any other languages. It might even cover the case of creating the source file if only the test was created.

Thanks, @andrematheus your workaround was very helpful.

This feature has been added to Projectile, and can be activated like so:

(setq projectile-create-missing-test-files t)

Thanks @bbatsov et al.

Was this page helpful?
0 / 5 - 0 ratings