Endless Parentheses

Concise ramblings on Emacs productivity.

Test-Driven-Development in CIDER and Emacs

As I was catching up on a few Parens of the Dead episodes this weekend, I was amused at how Magnar set up his Emacs to run tests whenever the file is saved. At first I thought it wasn’t for me (I’m one of those who obsessively saves every few seconds), but I’ve been trying it out lately and it’s starting to grow on me.

At its core, all you really need is an hook. But I walked the extra yard and wrote a minor mode for it, so I can easily call M-x tdd-mode to disable it if it ever gets on my nerves.

(defun tdd-test ()
  "Thin wrapper around `cider-test-run-tests'."
  (when (cider-connected-p)
    (let ((cider-auto-select-test-report-buffer nil)
          (cider-test-show-report-on-success nil))
      (cider-test-run-ns-tests nil))))

(define-minor-mode tdd-mode
  "Run all tests whenever a file is saved."
  t nil nil
  :global t
  (if tdd-mode
      (add-hook 'cider-file-loaded-hook #'tdd-test)
    (remove-hook 'cider-file-loaded-hook #'tdd-test)))

I also had to change the cider-test-success-face to something a little less “screamy”.

(custom-set-faces
 '(cider-test-success-face
   ((t (:foreground "green" :background nil)))))

Update 21 Nov 2015

Made it a global mode, so it’s easy to disable everywhere, and set the initial value to t.

Update 04 Mar 2016

Fixed a function call to the new name.

Tags: CIDER, clojure, programming, init.el, emacs

Say thanks on Gratipay
comments powered by Disqus