Konrad Mrożek
2023-07-19 3dde4ce4104db48cc12581e3b58f5200b957f308
Implement running the closest test
1 files modified
46 ■■■■ changed files
clojure/src/test_runner.clj 46 ●●●● patch | view | raw | blame | history
clojure/src/test_runner.clj
@@ -58,8 +58,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"}}]
  (binding [*compile-files* true]
    (compile 'test-runner)
    (println "Detecting test files in" -test-file)
    (let [test-files (->> -test-file
@@ -82,13 +94,23 @@
                                 (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)
                       (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)))))))