Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporter refactor #17

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clj-kondo/com.github.metabase/hawk/config.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:linters
{:unresolved-symbol
{:exclude
[(clojure.test/is [partial= query= re= schema= sql= =?])]}}}
[(clojure.test/is [partial= re= schema= =?])]}}}
17 changes: 2 additions & 15 deletions src/mb/hawk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
[environ.core :as env]
[mb.hawk.assert-exprs]
[mb.hawk.init :as hawk.init]
[mb.hawk.junit :as hawk.junit]
[mb.hawk.parallel :as hawk.parallel]
[mb.hawk.speak :as hawk.speak]
[mb.hawk.reporter.interface :as hawk.reporter]
[mb.hawk.util :as u]))

(set! *warn-on-reflection* true)
Expand Down Expand Up @@ -137,18 +136,6 @@

(alter-var-root #'t/test-var (constantly run-test))

(defn- reporter
"Create a new test reporter/event handler, a function with the signature `(handle-event event)` that gets called once
for every [[clojure.test]] event, including stuff like `:begin-test-run`, `:end-test-var`, and `:fail`."
[options]
(let [stdout-reporter (case (:mode options)
(:cli/ci :repl) eftest.report.pretty/report
:cli/local eftest.report.progress/report)]
(fn handle-event [event]
(hawk.junit/handle-event! event)
(hawk.speak/handle-event! event)
(stdout-reporter event))))

(def ^:private env-mode
(cond
(env/env :hawk-mode)
Expand Down Expand Up @@ -184,7 +171,7 @@
(merge
{:capture-output? false
:multithread? :vars
:report (reporter options)}
:report (hawk.reporter/reporter options)}
options))
@*parallel-test-counter*))))))

Expand Down
109 changes: 0 additions & 109 deletions src/mb/hawk/junit.clj

This file was deleted.

34 changes: 34 additions & 0 deletions src/mb/hawk/reporter/interface.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(ns mb.hawk.reporter.interface
(:require
[eftest.report.pretty]
[eftest.report.progress]))

(defmulti handle-event
"a"
{:arglists '([reporter event])}
(fn [reporter event]
[reporter (:type event)]))

(defmethod handle-event :default
[_ _])

(defn reporter
"Create a new test reporter/event handler, a function with the signature `(handle-event reporter event)`
that gets called once for every [[clojure.test]] event, including stuff like `:begin-test-run`,
`:end-test-var`, and `:fail`."
[options]
(let [stdout-reporter (case (:mode options)
(:cli/ci :repl) eftest.report.pretty/report
:cli/local eftest.report.progress/report)
reporters (descendants :hawk/reporter)]
(fn [event]
(doseq [reporter' reporters]
(handle-event reporter' event))
(stdout-reporter event))))

(defn register-reporter!
"Register a reporter.

`name` should be a namespaced keyword."
[name]
(derive name :hawk/reporter))
145 changes: 145 additions & 0 deletions src/mb/hawk/reporter/junit.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
(ns mb.hawk.reporter.junit
(:require
[clojure.test :as t]
[mb.hawk.reporter.interface :as hawk.reporter]
[mb.hawk.reporter.junit.write :as write]))

(hawk.reporter/register-reporter! :hawk/junit)

(defmacro ^:private with-error-handling
[event & body]
`(try
~@body
(catch Throwable e#
(throw (ex-info (str "Error handling event: " (ex-message e#))
{:event ~event}
e#)))))

(defn- with-var-and-ns
[{test-var :var, :as event}]
(let [test-var (or test-var
(when (seq t/*testing-vars*)
(last t/*testing-vars*)))]
(merge
{:var test-var}
event
(when test-var
{:ns (:ns (meta test-var))}))))

(defn handle-event!
"Write JUnit output for a `clojure.test` event such as success or failure."
[thunk {test-var :var, :as event}]
(let [test-var (or test-var
(when (seq t/*testing-vars*)
(last t/*testing-vars*)))
event (merge
{:var test-var}
event
(when test-var
{:ns (:ns (meta test-var))}))]
(try
(thunk event)
(catch Throwable e
(throw (ex-info (str "Error handling event: " (ex-message e))
{:event event}
e))))))

;; for unknown event types (e.g. `:clojure.test.check.clojure-test/trial`) just ignore them.
(defmethod hawk.reporter/handle-event [:hawk/junit :default]
[_ _])

(defmethod hawk.reporter/handle-event [:hawk/junit :begin-test-run]
[_ event]
(with-error-handling event
(write/clean-output-dir!)
(write/create-thread-pool!)))

(defmethod hawk.reporter/handle-event [:hawk/junit :summary]
[_ event]
(with-error-handling event
(write/wait-for-writes-to-finish)))

(defmethod hawk.reporter/handle-event [:hawk/junit :begin-test-ns]
[_reporter event]
(let [event (with-var-and-ns event)
{test-ns :ns} event]
(with-error-handling event
(alter-meta!
test-ns assoc ::context
{:start-time-ms (System/currentTimeMillis)
:timestamp (java.time.OffsetDateTime/now)
:test-count 0
:error-count 0
:failure-count 0
:results []}))))

(defmethod hawk.reporter/handle-event [:hawk/junit :end-test-ns]
[_ event]
(let [event (with-var-and-ns event)
{test-ns :ns} event
context (::context (meta test-ns))]
(with-error-handling event
(write/write-ns-result! (merge
event
context
{:duration-ms (- (System/currentTimeMillis) (:start-time-ms context))})))))

(defmethod hawk.reporter/handle-event [:hawk/junit :begin-test-var]
[_ event]
(let [event (with-var-and-ns event)
{test-var :var} event]
(alter-meta!
test-var assoc ::context
{:start-time-ms (System/currentTimeMillis)
:assertion-count 0
:results []})))

(defmethod hawk.reporter/handle-event [:hawk/junit :end-test-var]
[_ {test-var :var :as event}]
(let [event (with-var-and-ns event)
{test-ns :ns} event
context (::context (meta test-var))
result (merge
event
context
{:duration-ms (- (System/currentTimeMillis) (:start-time-ms context))})]
(alter-meta! test-ns update-in [::context :results] conj result)))

(defn- inc-ns-test-counts! [{test-var :var :as _event} & ks]
(alter-meta! (:ns (meta test-var)) update ::context (fn [context]
(reduce
(fn [context k]
(update context k inc))
context
ks))))

(defn- record-assertion-result! [{test-var :var :as event}]
(let [event (assoc event :testing-contexts (vec t/*testing-contexts*))]
(alter-meta! test-var update ::context
(fn [context]
(-> context
(update :assertion-count inc)
(update :results conj event))))))

(defmethod hawk.reporter/handle-event [:hawk/junit :pass]
[_ event]
(let [event (with-var-and-ns event)]
(with-error-handling event
(inc-ns-test-counts! event :test-count)
(record-assertion-result! event))))

(defmethod hawk.reporter/handle-event [:hawk/junit :fail]
[_ event]
(let [event (with-var-and-ns event)]
(with-error-handling event
(inc-ns-test-counts! event :test-count :failure-count)
(record-assertion-result! event))))

(defmethod hawk.reporter/handle-event [:hawk/junit :error]
[_ event]
(let [{test-var :var :as event} (with-var-and-ns event)]
;; some `:error` events happen because of errors in fixture initialization and don't have associated vars/namespaces
(when test-var
(with-error-handling event
(inc-ns-test-counts! event :test-count :error-count)
(record-assertion-result! event)))))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns mb.hawk.junit.write
(ns mb.hawk.reporter.junit.write
"Logic related to writing test results for a namespace to a JUnit XML file. See
https://stackoverflow.com/a/9410271/1198455 for the JUnit output spec."
(:require
Expand Down
16 changes: 7 additions & 9 deletions src/mb/hawk/speak.clj → src/mb/hawk/reporter/speak.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
(ns mb.hawk.speak
(:require [clojure.java.shell :as sh]))
(ns mb.hawk.reporter.speak
(:require
[clojure.java.shell :as sh]
[mb.hawk.reporter.interface :as hawk.reporter]))

(defmulti handle-event!
"Handles a test event by speaking(!?) it if appropriate"
:type)
(hawk.reporter/register-reporter! :hawk/speak)

(defn- enabled? [] (some? (System/getenv "SPEAK_TEST_RESULTS")))

(defmethod handle-event! :default [_] nil)

(defmethod handle-event! :summary
[{:keys [error fail]}]
(defmethod hawk.reporter/handle-event [:hawk/speak :summary]
[_ {:keys [error fail]}]
(when (enabled?)
(apply sh/sh "say"
(if (zero? (+ error fail))
Expand Down
18 changes: 18 additions & 0 deletions test/mb/hawk/reporter/speak_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns mb.hawk.reporter.speak-test
(:require
[clojure.java.shell :as sh]
[clojure.test :refer :all]
[mb.hawk.reporter.interface :as hawk.reporter]
[mb.hawk.reporter.speak :as hawk.reporter.speak]))

(deftest speak-results-test
(are [error fail expected] (let [sh-args (atom nil)]
(with-redefs [hawk.reporter.speak/enabled? (constantly true)
sh/sh (fn [& args] (reset! sh-args (vec args)))]
(hawk.reporter/handle-event :hawk/speak {:type :summary :error error :fail fail})
(= (into ["say"] expected) @sh-args)))
0 0 ["all tests passed"]
1 0 ["tests failed" "1 error"]
2 0 ["tests failed" "2 errors"]
2 1 ["tests failed" "2 errors" "1 failure"]
0 2 ["tests failed" "2 failures"]))
Loading