|
| 1 | +(ns cloverage.report.cobertura-test |
| 2 | + (:require [clojure.data.xml :as xml] |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.string :as str] |
| 5 | + [clojure.test :refer [deftest is]] |
| 6 | + [cloverage.args :as args] |
| 7 | + [cloverage.report.cobertura :as cobertura] |
| 8 | + [cloverage.source :as source])) |
| 9 | + |
| 10 | +(defn- child [node tag] |
| 11 | + (first (filter #(= tag (:tag %)) (:content node)))) |
| 12 | + |
| 13 | +(defn- parsed-report [forms] |
| 14 | + (with-redefs [source/source-file-path identity] |
| 15 | + (xml/parse-str (with-out-str (cobertura/write-cobertura-report forms))))) |
| 16 | + |
| 17 | +(defn- tracked [file lib line hits] |
| 18 | + {:file file :lib lib :line line :text "code" :tracked true :covered (pos? hits) :hits hits}) |
| 19 | + |
| 20 | +(deftest reports-real-lines-and-weighted-totals |
| 21 | + (let [forms [(tracked "alpha/core.clj" 'alpha.core 4 7) |
| 22 | + (tracked "alpha/core.clj" 'alpha.core 4 3) |
| 23 | + (tracked "alpha/core.clj" 'alpha.core 9 0) |
| 24 | + (tracked "alpha/util.clj" 'alpha.util 2 1) |
| 25 | + (tracked "beta/core.clj" 'beta.core 3 0) |
| 26 | + (tracked "beta/core.clj" 'beta.core 3 2) |
| 27 | + (tracked "beta/core.clj" 'beta.core 8 0) |
| 28 | + {:file "alpha/core.clj" :lib 'alpha.core :line 1 :text ""} |
| 29 | + {:file "alpha/core.clj" :lib 'alpha.core :line 2 :text "untracked"}] |
| 30 | + report (parsed-report forms) |
| 31 | + packages (:content (child report :packages)) |
| 32 | + classes (:content (child (first packages) :classes)) |
| 33 | + lines (:content (child (first classes) :lines))] |
| 34 | + (is (= "5" (get-in report [:attrs :lines-valid]))) |
| 35 | + (is (= "3" (get-in report [:attrs :lines-covered]))) |
| 36 | + (is (= 0.6 (Double/parseDouble (get-in report [:attrs :line-rate])))) |
| 37 | + (is (= ["alpha" "beta"] (map #(get-in % [:attrs :name]) packages))) |
| 38 | + (is (= (/ 2.0 3.0) (Double/parseDouble (get-in (first packages) [:attrs :line-rate])))) |
| 39 | + (is (= ["alpha/core.clj" "alpha/util.clj"] (map #(get-in % [:attrs :filename]) classes))) |
| 40 | + (is (= "alpha.core" (get-in (first classes) [:attrs :name]))) |
| 41 | + (is (= 0.5 (Double/parseDouble (get-in (first classes) [:attrs :line-rate])))) |
| 42 | + (is (= [{:number "4" :hits "7" :branch "false"} |
| 43 | + {:number "9" :hits "0" :branch "false"}] |
| 44 | + (map :attrs lines))) |
| 45 | + (is (= :methods (:tag (child (first classes) :methods)))) |
| 46 | + (is (= "0" (get-in report [:attrs :branches-valid]))) |
| 47 | + (is (= "0" (get-in report [:attrs :branches-covered]))) |
| 48 | + (is (= "0.0" (get-in report [:attrs :branch-rate]))) |
| 49 | + (is (pos? (Long/parseLong (get-in report [:attrs :timestamp])))))) |
| 50 | + |
| 51 | +(deftest empty-reports-and-root-namespaces |
| 52 | + (let [report (parsed-report [])] |
| 53 | + (is (= "0" (get-in report [:attrs :lines-valid]))) |
| 54 | + (is (= "0" (get-in report [:attrs :lines-covered]))) |
| 55 | + (is (= "0.0" (get-in report [:attrs :line-rate]))) |
| 56 | + (is (empty? (:content (child report :packages))))) |
| 57 | + (let [report (parsed-report [(tracked "core.clj" 'core 1 1)]) |
| 58 | + package (first (:content (child report :packages))) |
| 59 | + class (first (:content (child package :classes)))] |
| 60 | + (is (= "" (get-in package [:attrs :name]))) |
| 61 | + (is (= "core.clj" (get-in class [:attrs :filename]))))) |
| 62 | + |
| 63 | +(deftest cobertura-command-line-and-project-options |
| 64 | + (is (true? (:cobertura? (first (args/parse-args ["--cobertura"] {}))))) |
| 65 | + (is (false? (:cobertura? (first (args/parse-args ["--no-cobertura"] {}))))) |
| 66 | + (is (true? (:cobertura? (first (args/parse-args [] {:cobertura? true}))))) |
| 67 | + (is (false? (:cobertura? (first (args/parse-args [] {})))))) |
| 68 | + |
| 69 | +(deftest escapes-filenames-and-ignores-forms-without-a-source-file |
| 70 | + (let [report (parsed-report [(tracked "a&b/core.clj" 'a&b.core 1 1) |
| 71 | + {:file nil :line 2 :tracked true :covered false :hits 0}]) |
| 72 | + package (first (:content (child report :packages))) |
| 73 | + class (first (:content (child package :classes)))] |
| 74 | + (is (= "1" (get-in report [:attrs :lines-valid]))) |
| 75 | + (is (= "a&b/core.clj" (get-in class [:attrs :filename]))))) |
| 76 | + |
| 77 | +(defmacro ^:private if-jvm [& body] |
| 78 | + (when-not (System/getProperty "babashka.version") |
| 79 | + `(do ~@body))) |
| 80 | + |
| 81 | +(deftest conforms-to-the-cobertura-dtd |
| 82 | + ;; Validate against the vendored upstream DTD without accessing the network. |
| 83 | + ;; Babashka's XML writer is checked by the data tests above; its runtime does |
| 84 | + ;; not expose the JVM's validating XML parser. |
| 85 | + (if-jvm |
| 86 | + (doseq [forms [[] [(tracked "core.clj" 'core 1 0) |
| 87 | + (tracked "foo/core.clj" 'foo.core 2 3)]]] |
| 88 | + (let [factory (doto (javax.xml.parsers.DocumentBuilderFactory/newInstance) |
| 89 | + (.setValidating true)) |
| 90 | + builder (.newDocumentBuilder factory) |
| 91 | + xml-text (with-redefs [source/source-file-path identity] |
| 92 | + (with-out-str (cobertura/write-cobertura-report forms))) |
| 93 | + xml-text (str/replace xml-text "<coverage " |
| 94 | + "<!DOCTYPE coverage SYSTEM 'coverage-04.dtd'><coverage ")] |
| 95 | + (.setEntityResolver builder |
| 96 | + (reify org.xml.sax.EntityResolver |
| 97 | + (resolveEntity [_ _public-id _system-id] |
| 98 | + (org.xml.sax.InputSource. |
| 99 | + (io/reader (io/resource "cloverage/schema/coverage-04.dtd")))))) |
| 100 | + (.setErrorHandler builder |
| 101 | + (reify org.xml.sax.ErrorHandler |
| 102 | + (warning [_ e] (throw e)) |
| 103 | + (error [_ e] (throw e)) |
| 104 | + (fatalError [_ e] (throw e)))) |
| 105 | + (is (= "coverage" (-> (.parse builder (org.xml.sax.InputSource. (java.io.StringReader. xml-text))) |
| 106 | + .getDocumentElement .getTagName))))))) |
0 commit comments