|
| 1 | +; |
| 2 | +; Copyright 2016 Colin Fleming |
| 3 | +; |
| 4 | +; Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | +; you may not use this file except in compliance with the License. |
| 6 | +; You may obtain a copy of the License at |
| 7 | +; |
| 8 | +; http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +; |
| 10 | +; Unless required by applicable law or agreed to in writing, software |
| 11 | +; distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +; See the License for the specific language governing permissions and |
| 14 | +; limitations under the License. |
| 15 | +; |
| 16 | + |
| 17 | +(ns cursive.test-runner |
| 18 | + (:require |
| 19 | + [clojure.java.io] |
| 20 | + [clojure.test] |
| 21 | + [clojure.test.junit]) |
| 22 | + (:import (java.io File))) |
| 23 | + |
| 24 | +(defn- exit-code |
| 25 | + [summary] |
| 26 | + (if (clojure.test/successful? summary) |
| 27 | + 0 |
| 28 | + 1)) |
| 29 | + |
| 30 | +(let [plain-report clojure.test/report |
| 31 | + junit-report clojure.test.junit/junit-report] |
| 32 | + (defn- junit-logging-report-fn [plain-test-out] |
| 33 | + (fn [m] |
| 34 | + (junit-report m) |
| 35 | + (binding [clojure.test/*test-out* plain-test-out] |
| 36 | + (plain-report m))))) |
| 37 | + |
| 38 | +(defn- run-tests-with-junit-report |
| 39 | + [^String junit-report-filename & ns-syms] |
| 40 | + |
| 41 | + (when-let [report-dir (.getParentFile (File. junit-report-filename))] |
| 42 | + (.mkdirs report-dir)) |
| 43 | + |
| 44 | + (let [plain-test-out clojure.test/*test-out*] |
| 45 | + (with-open [xml-test-out (clojure.java.io/writer junit-report-filename)] |
| 46 | + (binding [clojure.test/*test-out* xml-test-out |
| 47 | + clojure.test.junit/junit-report (junit-logging-report-fn plain-test-out)] |
| 48 | + (clojure.test.junit/with-junit-output |
| 49 | + (apply clojure.test/run-tests ns-syms)))))) |
| 50 | + |
| 51 | +(defn run-tests* |
| 52 | + [ns-syms runner-fn] |
| 53 | + (apply require ns-syms) |
| 54 | + (->> ns-syms |
| 55 | + (apply runner-fn) |
| 56 | + (exit-code) |
| 57 | + (System/exit))) |
| 58 | + |
| 59 | +(defn run-tests |
| 60 | + ([ns-syms] |
| 61 | + (run-tests* ns-syms clojure.test/run-tests)) |
| 62 | + |
| 63 | + ([ns-syms ^String junit-report-filename] |
| 64 | + (run-tests* ns-syms (partial run-tests-with-junit-report junit-report-filename)))) |
0 commit comments