From b9eb2d004eba3ba78bcded9472bb2e07e23b7c62 Mon Sep 17 00:00:00 2001
From: Konrad Mrożek <konrad.mrozek@shareablee.com>
Date: Mon, 24 Jul 2023 11:27:04 +0000
Subject: [PATCH] Change class cache dir

---
 clojure/src/test_runner.clj |  129 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 109 insertions(+), 20 deletions(-)

diff --git a/clojure/src/test_runner.clj b/clojure/src/test_runner.clj
index 2868c26..07425c7 100644
--- a/clojure/src/test_runner.clj
+++ b/clojure/src/test_runner.clj
@@ -1,28 +1,117 @@
-(binding [*compile-files* true]
-  (require 'clojure.test))
+(ns test-runner)
 
-(ns test-runner
-  (:require [clojure.test :as t]))
+(def class-cache-dir ".cache/classes")
+(.mkdirs (java.io.File. class-cache-dir))
 
-(when-not (.exists (java.io.File. "classes"))
-  (.. (java.io.File. "classes") mkdir))
+(defmacro with-class-cache [& body]
+  `(binding [*compile-path*  class-cache-dir
+             *compile-files* true]
+     ~@body))
 
-(defmulti emacs-report :type)
+(with-class-cache
+  (time
+   (require '[clojure.test :as t])))
 
-(defmethod emacs-report :pass
+(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
-    (println "PASS:")))
+    (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 -main  [& [test-file]]
-  (binding  [*compile-files* true]
+(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)
-    (let  [test-namespaces (->> (or test-file  "test")
-                                (java.io.File.)
-                                (file-seq)
-                                (filter (memfn isFile))
-                                (map (memfn getPath))
-                                (map load-file)
-                                (map (comp :ns meta))
-                                (into #{}))]
-      (run! (fn [n] (t/run-tests n)) test-namespaces))))
+    (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)))))))

--
Gitblit v1.9.3