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