Skip to content

Instantly share code, notes, and snippets.

@xiongtx
Last active September 7, 2017 03:34
Show Gist options
  • Save xiongtx/e10beed9eab593752da7261bc0630a76 to your computer and use it in GitHub Desktop.
Save xiongtx/e10beed9eab593752da7261bc0630a76 to your computer and use it in GitHub Desktop.
Elisp snippet to tell you to become a carpenter if any Clojure test fails too many times in a row
(require 'cl-lib)
(require 'cider-test)
(defcustom carpenter-threshold 3
"Number of times a CIDER test can fail consecutively before
you're told to become a carpenter."
:type 'integer)
(defvar carpenter-fail-counts (make-hash-table :test #'equal))
(defun carpenter-encourage (run-test-fn)
(when-let ((ns (or (get-text-property (point) 'ns)
(clojure-find-ns)))
(var (car (or (get-text-property (point) 'var)
(cdr (clojure-find-def))))))
(let ((fails (apply #'max 0 (hash-table-values carpenter-fail-counts))))
(if (>= fails carpenter-threshold)
(message "With %s failing tests in a row, you should become a carpenter" fails)
(funcall run-test-fn)
(let* ((result-dict (car (nrepl-dict-get-in cider-test-last-results (list ns var))))
(type (nrepl-dict-get result-dict "type")))
(message type)
(cond
((equal "pass" type) (remhash (list ns var) carpenter-fail-counts))
((equal "fail" type) (cl-incf (gethash (list ns var) carpenter-fail-counts 0)))))))))
(advice-add 'cider-test-run-test :around #'carpenter-encourage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment