From cdb7cc02e4041f28e3bd9f0f78cf862f4b63e96f Mon Sep 17 00:00:00 2001
From: Konrad Mrożek <kmrozek@comscore.com>
Date: Fri, 12 Jan 2024 13:16:08 +0000
Subject: [PATCH] Disable colors in malli pretty output

---
 clojure/src/test_runner.clj |   55 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/clojure/src/test_runner.clj b/clojure/src/test_runner.clj
index 53ee877..eb7c47e 100644
--- a/clojure/src/test_runner.clj
+++ b/clojure/src/test_runner.clj
@@ -1,4 +1,7 @@
-(ns test-runner)
+(ns test-runner
+  (:require
+   [clojure.string :as string]
+   [clojure.test :as t]))
 
 (def class-cache-dir ".cache/classes")
 (.mkdirs (java.io.File. class-cache-dir))
@@ -9,8 +12,10 @@
      ~@body))
 
 (with-class-cache
-  (require '[clojure.test :as t]))
+  (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]
@@ -29,6 +34,21 @@
     (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))
@@ -55,7 +75,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
   [_])
@@ -92,6 +114,8 @@
         (println "Malli detected. Instrument functions...")
         (require 'malli.dev)
         (require 'malli.dev.pretty)
+        (require 'malli.dev.virhe)
+        (alter-var-root (get (ns-publics 'malli.dev.virhe) '-color) (constantly (fn [_ body _] [:span body])))
         ((find-var 'malli.dev/start!) {:report ((find-var 'malli.dev.pretty/thrower))}))
       (let [test-namespaces (->> (all-ns)
                                  (mapcat ns-publics)
@@ -100,14 +124,28 @@
                                  (filter (comp test-files :file))
                                  (map :ns)
                                  (set))]
+
+        (set-validator! #'t/*testing-contexts*
+                        (let [validator (get-validator #'t/*testing-contexts*)]
+                          (fn [v]
+                            (println "Testing:" (string/join " " v))
+                            (println)
+                            (if validator
+                              (validator v)
+                              true))))
+
         (with-redefs [t/report vim-report]
           (System/exit
            (if (pos? (if (and -test-file -test-line)
 
-                       (let [results (t/run-test-var (find-closest-test (.. (java.io.File. -test-file) getAbsolutePath)
-                                                                        (parse-long -test-line)))]
-                         (+ (:fail results 0)
-                            (:error results 0)))
+                       (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
@@ -117,6 +155,3 @@
                                test-namespaces)))
              1
              0)))))))
-
-
-

--
Gitblit v1.9.3