Skip to content

Instantly share code, notes, and snippets.

@rgm
Created January 18, 2019 04:16
Show Gist options
  • Save rgm/7cc2aaf2a131f637940ffd1d4f0bce05 to your computer and use it in GitHub Desktop.
Save rgm/7cc2aaf2a131f637940ffd1d4f0bce05 to your computer and use it in GitHub Desktop.
(ns rgm.kaocha-notifier
(:require [clojure.java.shell :refer [sh]]))
;; special thanks for terminal-notify stuff to
;; https://github.com/glittershark/midje-notifier/blob/master/src/midje/notifier.clj
(defmacro exists?
[program]
`(= 0 (:exit (sh "which" ~program))))
(defonce notification-type
(cond
(exists? "notify-send") :notify-send
(exists? "terminal-notifier") :terminal-notifier))
(def app-icon
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Clojure_logo.svg/200px-Clojure_logo.svg.png")
(defmacro notify-send
[title body]
(case notification-type
:notify-send `(sh "notify-send" ~title ~body)
:terminal-notifier `(sh "terminal-notifier"
"-message" ~body
"-title" ~title
"-appIcon" app-icon)))
(defn summarize-result
"boil down a kaocha overall result to eg.
#:kaocha.result{:pass 169, :fail 1, :error 0}"
[result]
(->> result
:kaocha.result/tests
(mapcat :kaocha.result/tests)
(mapcat :kaocha.result/tests)
(map #(select-keys % [:kaocha.result/pass :kaocha.result/fail :kaocha.result/error]))
(apply merge-with +)))
(defn passing?
[summary]
(and (= 0 (:kaocha.result/error summary))
(= 0 (:kaocha.result/fail summary))))
(defn notification-hook
"kaocha post-run hook that sends a thumbs-up or thumbs-down notification
on the whole suite, so a `kaocha --watch` terminal process can be hidden
and generally ignored until it goes red.
Requires https://github.com/julienXX/terminal-notifier on mac
or `libnotify` on linux."
[result]
(let [{:keys [kaocha.result/pass
kaocha.result/fail
kaocha.result/error] :as summary}
(summarize-result result)]
(if (passing? summary)
(let [message (str pass
(if (= 1 pass) " assertion " " assertions ")
"succeeded")]
(notify-send "✅ Passing" message))
(let [total-fails (+ fail error)
message (str total-fails
(if (= 1 total-fails) " assertion " " assertions ")
"failed, " pass " succeeded")]
(notify-send "⛔️ Failing" message))))
result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment