From 9911d80418f2d1eb18c4b364d24aeb15a09cbc43 Mon Sep 17 00:00:00 2001 From: Konrad Mrozek <konrad.mrozek@gmail.com> Date: Tue, 05 Dec 2023 07:36:36 +0000 Subject: [PATCH] Make compatible with neovim --- clojure/src/test_runner.clj | 107 +++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 79 insertions(+), 28 deletions(-) diff --git a/clojure/src/test_runner.clj b/clojure/src/test_runner.clj index c15ce04..c617c67 100644 --- a/clojure/src/test_runner.clj +++ b/clojure/src/test_runner.clj @@ -1,27 +1,51 @@ -(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)) +(with-class-cache + (require '[clojure.test :as t] + '[lambdaisland.deep-diff2 :as ddiff])) + +(def no-colors-printer (ddiff/printer {:print-color false})) (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))))))) + (t/inc-report-counter :fail) + (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)))))) + +(defmethod t/assert-expr '= [msg form] + (let [args (rest form) + pred (first form)] + `(let [values# (list ~@args) + result# (apply ~pred values#)] + (if result# + (t/do-report {:type :pass, :message ~msg, + :expected '~form, :actual (cons '~pred values#)}) + (do + (t/do-report {:type :fail, :message ~msg, + :expected '~form, :actual (list '~'not (cons '~pred values#))}) + (-> (apply ddiff/diff values#) + (ddiff/pretty-print no-colors-printer)))) + result#))) (defn- find-line-number [source-file m] (if (instance? Throwable (:actual m)) @@ -37,6 +61,7 @@ (defmethod vim-report :error [m] + (t/inc-report-counter :error) (when-let [source-file (some-> t/*testing-vars* first meta @@ -47,7 +72,9 @@ (println (str "ERROR-CONTINUE:ACTUAL:" (if (instance? Throwable (:actual m)) (ex-message (:actual m)) - (pr-str (:actual m)))))))) + (pr-str (:actual m))))) + (when (instance? Throwable (:actual m)) + (.printStackTrace (:actual m)))))) (defmethod vim-report :default [_]) @@ -55,8 +82,20 @@ (defn- clj-file? [f] (re-matches #"^.*\.cljs?$" (.getName f))) -(defn -main [& {:strs [-test-file] :or {-test-file "test"}}] - (binding [*compile-files* true] +(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 @@ -71,7 +110,8 @@ (when (find-ns 'malli.core) (println "Malli detected. Instrument functions...") (require 'malli.dev) - ((find-var 'malli.dev/start!))) + (require 'malli.dev.pretty) + ((find-var 'malli.dev/start!) {:report ((find-var 'malli.dev.pretty/thrower))})) (let [test-namespaces (->> (all-ns) (mapcat ns-publics) (map (comp meta second)) @@ -79,13 +119,24 @@ (filter (comp test-files :file)) (map :ns) (set))] - (System/exit (if (pos? (reduce (fn [total-fails n] - (with-redefs [t/report vim-report] - (let [results (t/run-tests n)] - (+ total-fails - (:fail results 0) - (:error results 0))))) - 0 - test-namespaces)) - 1 - 0)))))) + (with-redefs [t/report vim-report] + (System/exit + (if (pos? (if (and -test-file -test-line) + + (if-let [test-var (find-closest-test (.. (java.io.File. -test-file) getAbsolutePath) + (parse-long -test-line))] + (->> (t/run-test-var test-var) + ((juxt :fail :error)) + (apply +)) + (do + (println "No test found") + 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