Skip to content

Commit

Permalink
Merge pull request #9 from NoahTheDuke/nb/unify-output-reporters
Browse files Browse the repository at this point in the history
Unify `:output` & `:reporter`
  • Loading branch information
NoahTheDuke authored Nov 19, 2024
2 parents b7f04c8 + 7a7a1d6 commit 8abdfa9
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 54 deletions.
14 changes: 2 additions & 12 deletions src/clojure/lazytest/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
:parse-fn #(keyword (if (str/starts-with? % ":") (subs % 1) %))
:assoc-fn update-set]
[nil "--output SYMBOL" "Output format. Can be given multiple times. (Defaults to \"nested\".)"
:parse-fn read-string
:assoc-fn (fn [args k v]
(let [output (if (qualified-symbol? v)
v
(symbol "lazytest.reporters" (name v)))]
(update-vec args k output)))]
:parse-fn symbol
:assoc-fn update-vec]
[nil "--md FILE" "Run doctests for given markdown file. Can be given multiple times."
:parse-fn io/file
:assoc-fn update-vec]
Expand Down Expand Up @@ -66,11 +62,6 @@
{:exit-message (str/join \newline (cons "lazytest errors:" errors))
:ok false})

(defn prepare-output [output]
(->> output
(distinct)
(vec)))

(defn validate-opts
"Parse and validate opts.
Expand All @@ -87,5 +78,4 @@
errors (print-errors errors)
:else (-> options
(update :dir (comp not-empty vec concat) arguments)
(update :output prepare-output)
(update :delay #(or % 500))))))
16 changes: 9 additions & 7 deletions src/clojure/lazytest/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@
(defn resolve-reporter
[reporter]
(cond
(sequential? reporter) (->> (flatten reporter)
(map resolve-reporter)
(distinct)
(apply combine-reporters))
(qualified-symbol? reporter)
(if-let [r (requiring-resolve reporter)]
(resolve-reporter (var-get r))
(throw (ex-info (str "Cannot find reporter: " reporter)
{:reporter reporter})))
(symbol? reporter) (throw (ex-info (str "Cannot find reporter: " reporter)
{:reporter reporter}))
(sequential? reporter) (->> (flatten reporter)
(map resolve-reporter)
(apply combine-reporters))
(symbol? reporter) (resolve-reporter (symbol "lazytest.reporters" (name reporter)))
:else reporter))

(defn ->config [config]
(if (:lazytest.runner/depth config)
config
(let [reporter (resolve-reporter
(or (:reporter config) 'lazytest.reporters/nested))]
(let [output (or (:output config) 'lazytest.reporters/nested)
output (if (sequential? output) (distinct output) [output])
reporter (resolve-reporter output)]
(-> config
(assoc :lazytest.runner/depth 1)
(assoc :lazytest.runner/suite-history [])
(assoc :output output)
(assoc :reporter reporter)))))
8 changes: 4 additions & 4 deletions src/clojure/lazytest/filter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@
[ns-suite config]
(let [var-filter (map (comp symbol namespace) (:var-filter config))
ns-filter (not-empty (into #{} (concat var-filter (:ns-filter config))))]
(when (and ns-filter
(ns-filter (:doc ns-suite)))
(filter-suite ns-suite config))
(filter-suite ns-suite config)))
(if ns-filter
(when (ns-filter (:doc ns-suite))
(filter-suite ns-suite config))
(filter-suite ns-suite config))))

(defmethod filter-tree :lazytest/run
filter-tree--lazytest-run
Expand Down
26 changes: 9 additions & 17 deletions src/clojure/lazytest/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(:gen-class)
(:require
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.tools.namespace.file :refer [read-file-ns-decl]]
[clojure.tools.namespace.find :refer [find-sources-in-dir]]
[lazytest.cli :refer [validate-opts]]
Expand All @@ -13,18 +12,12 @@
[lazytest.runner :refer [run-tests]]
[lazytest.watch :as watch]))

(defn find-ns-decls [config dirs]
(let [var-filter-nses (->> (:var-filter config)
(map (comp symbol namespace))
(into #{}))
ns-filter (or (not-empty (set/union (:ns-filter config) var-filter-nses))
any?)]
(into []
(comp (mapcat find-sources-in-dir)
(keep read-file-ns-decl)
(keep second)
(filter ns-filter))
dirs)))
(defn find-ns-decls [dirs]
(into []
(comp (mapcat find-sources-in-dir)
(keep read-file-ns-decl)
(keep second))
dirs))

(defn add-md-tests
[config dirs]
Expand All @@ -39,16 +32,15 @@
(defn require-dirs [config dir]
(let [dirs (map io/file (or dir #{"test"}))
md-nses (add-md-tests config dirs)
nses (into (find-ns-decls config dirs)
nses (into (find-ns-decls dirs)
md-nses)]
(when (empty? nses)
(throw (ex-info "No namespaces to run" {:dir dir})))
(apply require nses)
nses))

(defn run-impl [{:keys [dir output] :as config}]
(let [output (or (not-empty output) ['lazytest.reporters/nested])
config (->config (assoc config :output output :reporter output))
(defn run-impl [{:keys [dir] :as config}]
(let [config (->config config)
nses (require-dirs config dir)]
(run-tests nses config)))

Expand Down
6 changes: 5 additions & 1 deletion src/clojure/lazytest/repl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
[lazytest.runner :as runner]))

(def repl-config
{:reporter [r/focused r/results r/summary]})
{:output [r/focused r/results r/summary]})

(defn run-tests
"Runs tests defined in the given namespaces."
{:arglists '([namespaces] [namespaces {:keys [output] :as config}])}
([namespaces] (run-tests namespaces repl-config))
([namespaces config]
(if (sequential? namespaces)
Expand All @@ -18,11 +19,14 @@

(defn run-all-tests
"Run tests defined in all namespaces."
{:arglists '([] [{:keys [output] :as config}])}
([] (run-all-tests repl-config))
([config]
(summarize (runner/run-all-tests (->config config)))))

(defn run-test-var
"Run test var."
{:arglists '([var] [var {:keys [output] :as config}])}
([v] (run-test-var v repl-config))
([v config]
(summarize (runner/run-test-var v (->config config)))))
4 changes: 2 additions & 2 deletions src/clojure/lazytest/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
run-afters run-before-eachs run-befores]]
[lazytest.filter :refer [filter-tree]]
[lazytest.find :refer [find-suite find-var-test-value]]
[lazytest.reporters :as r :refer [nested report]]
[lazytest.reporters :as r :refer [report]]
[lazytest.suite :as s :refer [suite suite-result suite?]]
[lazytest.test-case :refer [try-test-case]]))

Expand Down Expand Up @@ -109,7 +109,7 @@

(defn run-tests
"Runs tests defined in the given namespaces. Applies filters in config."
([namespaces] (run-tests namespaces (->config {:reporter nested})))
([namespaces] (run-tests namespaces (->config nil)))
([namespaces config]
(-> (apply find-suite namespaces)
(filter-and-run config))))
Expand Down
41 changes: 41 additions & 0 deletions test/clojure/lazytest/config_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(ns lazytest.config-test
(:require
[lazytest.config :as sut]
[lazytest.core :refer [defdescribe it expect]]
[lazytest.extensions.matcher-combinators :refer [match?]]))

(defn example-reporter [& _args])

(defdescribe ->config-test
(it "works"
(expect
(match? {:output [:keyword]
:reporter fn?}
(sut/->config {:output :keyword}))))
(it "resolves by default to lazytest.reporters"
(expect
(match? {:output ['dots]
:reporter fn?}
(sut/->config {:output 'dots}))))
(it "resolves other reporters correctly"
(expect
(match? {:output ['lazytest.config-test/example-reporter]
:reporter fn?}
(sut/->config {:output 'lazytest.config-test/example-reporter}))))
(it "resolves multiple reporters"
(expect
(match? {:output ['dots 'lazytest.config-test/example-reporter]
:reporter fn?}
(sut/->config {:output ['dots 'lazytest.config-test/example-reporter]}))))
(it "calls distinct on the input"
(expect
(match? {:output ['lazytest.config-test/example-reporter]
:reporter fn?}
(sut/->config {:output ['lazytest.config-test/example-reporter
'lazytest.config-test/example-reporter]})))
(expect
(match? {:output ['lazytest.config-test/example-reporter 'dots]
:reporter fn?}
(sut/->config {:output ['lazytest.config-test/example-reporter
'dots
'lazytest.config-test/example-reporter]})))))
3 changes: 1 addition & 2 deletions test/clojure/lazytest/find_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
(vreset! output (with-out-str-data-map
(main/run ["--dir" "corpus/find_tests"
"--output" "nested*"]))))
(after (remove-ns 'find-tests.examples)
(vreset! output nil))]}
(after (vreset! output nil))]}
(it "runs all suites"
(expect
(match?
Expand Down
2 changes: 1 addition & 1 deletion test/clojure/lazytest/libs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
(match? {:total 80 :pass 80 :fail 0}
(summarize
(lr/run-tests [(the-ns honeysql-ns)]
(->config {:reporter ['lazytest.reporters/quiet]})))))))))
(->config {:output ['lazytest.reporters/quiet]})))))))))
12 changes: 6 additions & 6 deletions test/clojure/lazytest/reporters_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
(defn ->passing [& {:as extra}]
(-> (tc/test-case {:doc "example test-case"
:body (fn [] true)})
(runner/run-tree (->config {:reporter [(constantly nil)]}))
(runner/run-tree (->config {:output [(constantly nil)]}))
(merge extra)))

(defn ->failing [& {:as extra}]
(-> (tc/test-case {:doc "example test-case"
:body (fn [] (expect (= 1 2) (if (contains? extra :message)
(:message extra)
"failing")))})
(runner/run-tree (->config {:reporter [(constantly nil)]}))
(runner/run-tree (->config {:output [(constantly nil)]}))
(assoc :file "example.clj")
(assoc :line 1)
(merge (dissoc extra :message))))
Expand All @@ -55,7 +55,7 @@
:line 1})]
(-> (tc/test-case {:doc "example test-case"
:body (fn [] (throw thrown))})
(runner/run-tree (->config {:reporter [(constantly nil)]})))))
(runner/run-tree (->config {:output [(constantly nil)]})))))

(defn stub-stack-trace
[_ex]
Expand Down Expand Up @@ -348,7 +348,7 @@
(suite {:type :lazytest/ns
:nses [*ns*]
:children [test-suite]})
(->config {:reporter (constantly nil)}))
(->config {:output (constantly nil)}))
suite-results (assoc test-suite :type :end-test-run :results results)]
(expect (match? (re-compile "Top 1 slowest test namespaces.*Top 1 slowest test vars" :dotall)
(-> (sut/profile nil suite-results)
Expand All @@ -361,10 +361,10 @@
(it "uses the var if given no doc string"
(expect (= " defdescribe-no-doc\n √ works\n\n"
(-> (runner/run-test-var #'defdescribe-no-doc
(->config {:reporter sut/nested*}))
(->config {:output sut/nested*}))
(with-out-str-no-color)))))
(it "uses the doc string when available"
(expect (= " cool docs\n √ works\n\n"
(-> (runner/run-test-var #'defdescribe-with-doc
(->config {:reporter sut/nested*}))
(->config {:output sut/nested*}))
(with-out-str-no-color))))))
4 changes: 2 additions & 2 deletions test/clojure/lazytest/runner_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
""
""])
(-> (sut/run-test-suite test-suite
(->config {:reporter reporters/nested*}))
(->config {:output reporters/nested*}))
(with-out-str-no-color)))))))

(defdescribe run-test-var-test
Expand All @@ -32,5 +32,5 @@
""
""])
(-> (sut/run-test-var #'run-test-suite-test
(->config {:reporter reporters/nested*}))
(->config {:output reporters/nested*}))
(with-out-str-no-color))))))

0 comments on commit 8abdfa9

Please sign in to comment.