Konrad Mrożek
2023-08-31 d937545d69736a906fdb549b75bdf2b8e005e287
commit | author | age
b9eb2d 1 (ns test-runner)
919581 2
b9eb2d 3 (def class-cache-dir ".cache/classes")
KM 4 (.mkdirs (java.io.File. class-cache-dir))
919581 5
b9eb2d 6 (defmacro with-class-cache [& body]
KM 7   `(binding [*compile-path*  class-cache-dir
8              *compile-files* true]
9      ~@body))
10
11 (with-class-cache
11f9a1 12   (require '[clojure.test :as t]))
919581 13
55439f 14 (defmulti vim-report :type)
919581 15
55439f 16 (defmethod vim-report :begin-test-ns [m]
KM 17   (println "\nTesting" (ns-name (:ns m))))
18
2f0051 19 (defmethod vim-report :begin-test-var [m]
KM 20   (println "\nExecuting" (:name (meta (:var m)))))
21
55439f 22 (defmethod vim-report :fail
919581 23   [m]
2f8117 24   (t/inc-report-counter :fail)
KM 25   (when-let [source-file (some-> t/*testing-vars*
26                                  first
27                                  meta
28                                  :file)]
29     (println (str "FAIL:" source-file ":" (:line m) ":" (t/testing-vars-str m) ":" (t/testing-contexts-str) ":" (:message m "FAIL")))
30     (println (str "FAIL-CONTINUE:EXPECTED:" (pr-str (:expected m))))
31     (println (str "FAIL-CONTINUE:ACTUAL:" (pr-str (:actual m))))))
04e874 32
22b826 33 (defn- find-line-number [source-file m]
KM 34   (if (instance? Throwable (:actual m))
35     (let [fname (-> source-file (java.io.File.) (.getName))]
36       (->> m
37            :actual
38            Throwable->map
39            :trace
40            (some (fn [[_ _ e-file e-line]]
41                    (when (= e-file fname)
42                      e-line)))))
43     (:line m)))
44
55439f 45 (defmethod vim-report :error
04e874 46   [m]
2f8117 47   (t/inc-report-counter :error)
04e874 48   (when-let [source-file (some-> t/*testing-vars*
KM 49                                  first
50                                  meta
51                                  :file)]
22b826 52     (let [line (find-line-number source-file m)]
KM 53       (println (str "ERROR:" source-file ":" line ":" (t/testing-vars-str m) ":" (t/testing-contexts-str) ":" (:message m "FAIL")))
54       (println (str "ERROR-CONTINUE:EXPECTED:" (pr-str (:expected m))))
55       (println (str "ERROR-CONTINUE:ACTUAL:"
56                     (if (instance? Throwable (:actual m))
57                       (ex-message (:actual m))
58                       (pr-str (:actual m))))))))
04e874 59
55439f 60 (defmethod vim-report :default
04e874 61   [_])
919581 62
9ae31a 63 (defn- clj-file? [f]
KM 64   (re-matches #"^.*\.cljs?$" (.getName f)))
65
3dde4c 66 (defn find-closest-test [test-file test-line]
KM 67   (->> (all-ns)
68        (mapcat ns-publics)
69        (map second)
70        (filter (comp :test meta))
71        (filter (comp #{test-file} :file meta))
72        (map #(vector % (- test-line (-> % meta :line))))
73        (filterv (comp pos? second))
74        (sort-by second)
75        first
76        first))
77
78 (defn -main [& {:strs [-test-file -test-line] :or {-test-file "test"}}]
b9eb2d 79   (with-class-cache
919581 80     (compile 'test-runner)
9ae31a 81     (println "Detecting test files in" -test-file)
KM 82     (let [test-files (->> -test-file
f5c518 83                           (java.io.File.)
KM 84                           (file-seq)
85                           (filter (memfn isFile))
9ae31a 86                           (filter clj-file?)
f5c518 87                           (map (memfn getAbsolutePath))
KM 88                           (set))]
89       (println "Loading test files...")
90       (run! load-file test-files)
55439f 91       (when (find-ns 'malli.core)
KM 92         (println "Malli detected. Instrument functions...")
93         (require 'malli.dev)
88cddc 94         (require 'malli.dev.pretty)
KM 95         ((find-var 'malli.dev/start!) {:report ((find-var 'malli.dev.pretty/thrower))}))
f5c518 96       (let [test-namespaces (->> (all-ns)
KM 97                                  (mapcat ns-publics)
98                                  (map (comp meta second))
99                                  (filter :test)
100                                  (filter (comp test-files :file))
101                                  (map :ns)
102                                  (set))]
3dde4c 103         (with-redefs [t/report vim-report]
KM 104           (System/exit
105            (if (pos? (if (and -test-file -test-line)
2f8117 106
d93754 107                        (if-let [test-var (find-closest-test (.. (java.io.File. -test-file) getAbsolutePath)
KM 108                                                             (parse-long -test-line))]
109                          (->> (t/run-test-var test-var)
110                               ((juxt :fail :error))
111                               (apply +))
112                          (do
113                            (println "No test found")
114                            0))
3dde4c 115                        (reduce (fn [total-fails n]
KM 116                                  (let [results (t/run-tests n)]
117                                    (+ total-fails
118                                       (:fail results 0)
119                                       (:error results 0))))
120                                0
121                                test-namespaces)))
122              1
123              0)))))))