diff --git a/src/clojure/lazytest/cli.clj b/src/clojure/lazytest/cli.clj index 9202453..9656c93 100644 --- a/src/clojure/lazytest/cli.clj +++ b/src/clojure/lazytest/cli.clj @@ -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] @@ -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. @@ -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)))))) diff --git a/src/clojure/lazytest/config.clj b/src/clojure/lazytest/config.clj index 1e9eb69..34bde8c 100644 --- a/src/clojure/lazytest/config.clj +++ b/src/clojure/lazytest/config.clj @@ -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))))) diff --git a/src/clojure/lazytest/filter.clj b/src/clojure/lazytest/filter.clj index cb09f1d..ba725b0 100644 --- a/src/clojure/lazytest/filter.clj +++ b/src/clojure/lazytest/filter.clj @@ -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 diff --git a/src/clojure/lazytest/main.clj b/src/clojure/lazytest/main.clj index b9fdc4d..41db606 100644 --- a/src/clojure/lazytest/main.clj +++ b/src/clojure/lazytest/main.clj @@ -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]] @@ -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] @@ -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))) diff --git a/src/clojure/lazytest/repl.clj b/src/clojure/lazytest/repl.clj index 4d5007e..c475501 100644 --- a/src/clojure/lazytest/repl.clj +++ b/src/clojure/lazytest/repl.clj @@ -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) @@ -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))))) diff --git a/src/clojure/lazytest/runner.clj b/src/clojure/lazytest/runner.clj index cab6652..a82efe5 100644 --- a/src/clojure/lazytest/runner.clj +++ b/src/clojure/lazytest/runner.clj @@ -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]])) @@ -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)))) diff --git a/test/clojure/lazytest/config_test.clj b/test/clojure/lazytest/config_test.clj new file mode 100644 index 0000000..a455d48 --- /dev/null +++ b/test/clojure/lazytest/config_test.clj @@ -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]}))))) diff --git a/test/clojure/lazytest/find_test.clj b/test/clojure/lazytest/find_test.clj index ab73e07..9ddc523 100644 --- a/test/clojure/lazytest/find_test.clj +++ b/test/clojure/lazytest/find_test.clj @@ -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? diff --git a/test/clojure/lazytest/libs_test.clj b/test/clojure/lazytest/libs_test.clj index 46c94ac..b61e41c 100644 --- a/test/clojure/lazytest/libs_test.clj +++ b/test/clojure/lazytest/libs_test.clj @@ -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]}))))))))) diff --git a/test/clojure/lazytest/reporters_test.clj b/test/clojure/lazytest/reporters_test.clj index 79e7950..c22ae19 100644 --- a/test/clojure/lazytest/reporters_test.clj +++ b/test/clojure/lazytest/reporters_test.clj @@ -35,7 +35,7 @@ (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}] @@ -43,7 +43,7 @@ :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)))) @@ -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] @@ -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) @@ -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)))))) diff --git a/test/clojure/lazytest/runner_test.clj b/test/clojure/lazytest/runner_test.clj index 40edb9f..fbfa8c6 100644 --- a/test/clojure/lazytest/runner_test.clj +++ b/test/clojure/lazytest/runner_test.clj @@ -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 @@ -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))))))