Skip to content

Commit e427762

Browse files
authored
Merge pull request #370 from cloverage/complete-cobertura-reports
Generate Cobertura reports from actual line coverage
2 parents 5701ed0 + 93c64cf commit e427762

8 files changed

Lines changed: 242 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ in github. Commentary on the change should appear as a nested, unordered list. -
99

1010
## Unreleased
1111

12+
- Add Cobertura XML reports with real line hit counts and coverage totals ([#325](https://github.com/cloverage/cloverage/pull/325)).
13+
1214
- Use project-relative paths in Codecov reports, preserving classpath paths for JAR resources ([#354](https://github.com/cloverage/cloverage/pull/354)).
1315

1416
- Add babashka compatibility

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Available options and command-line arguments:
6060
:emma-xml? --no-emma-xml, --emma-xml false Produce an EMMA XML report. [emma.sourceforge.net]
6161
:lcov? --no-lcov, --lcov false Produce a lcov/gcov report.
6262
:codecov? --no-codecov, --codecov false Generate a JSON report for Codecov.io
63+
:cobertura? --no-cobertura, --cobertura false Generate a Cobertura XML report (line coverage).
6364
:coveralls? --no-coveralls, --coveralls false Send a JSON report to Coveralls if on a CI server
6465
:junit? --no-junit, --junit false Output test results as junit xml file. Supported in :clojure.test runner
6566
:raw? --no-raw, --raw false Output raw coverage data (for debugging).
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!-- Portions (C) International Organization for Standardization 1986:
2+
Permission to copy in any form is granted for use with
3+
conforming SGML systems and applications as defined in
4+
ISO 8879, provided this notice is included in all copies.
5+
-->
6+
7+
<!ELEMENT coverage (sources?,packages)>
8+
<!ATTLIST coverage line-rate CDATA #REQUIRED>
9+
<!ATTLIST coverage branch-rate CDATA #REQUIRED>
10+
<!ATTLIST coverage lines-covered CDATA #REQUIRED>
11+
<!ATTLIST coverage lines-valid CDATA #REQUIRED>
12+
<!ATTLIST coverage branches-covered CDATA #REQUIRED>
13+
<!ATTLIST coverage branches-valid CDATA #REQUIRED>
14+
<!ATTLIST coverage complexity CDATA #REQUIRED>
15+
<!ATTLIST coverage version CDATA #REQUIRED>
16+
<!ATTLIST coverage timestamp CDATA #REQUIRED>
17+
18+
<!ELEMENT sources (source*)>
19+
20+
<!ELEMENT source (#PCDATA)>
21+
22+
<!ELEMENT packages (package*)>
23+
24+
<!ELEMENT package (classes)>
25+
<!ATTLIST package name CDATA #REQUIRED>
26+
<!ATTLIST package line-rate CDATA #REQUIRED>
27+
<!ATTLIST package branch-rate CDATA #REQUIRED>
28+
<!ATTLIST package complexity CDATA #REQUIRED>
29+
30+
<!ELEMENT classes (class*)>
31+
32+
<!ELEMENT class (methods,lines)>
33+
<!ATTLIST class name CDATA #REQUIRED>
34+
<!ATTLIST class filename CDATA #REQUIRED>
35+
<!ATTLIST class line-rate CDATA #REQUIRED>
36+
<!ATTLIST class branch-rate CDATA #REQUIRED>
37+
<!ATTLIST class complexity CDATA #REQUIRED>
38+
39+
<!ELEMENT methods (method*)>
40+
41+
<!ELEMENT method (lines)>
42+
<!ATTLIST method name CDATA #REQUIRED>
43+
<!ATTLIST method signature CDATA #REQUIRED>
44+
<!ATTLIST method line-rate CDATA #REQUIRED>
45+
<!ATTLIST method branch-rate CDATA #REQUIRED>
46+
<!ATTLIST method complexity CDATA #REQUIRED>
47+
48+
<!ELEMENT lines (line*)>
49+
50+
<!ELEMENT line (conditions*)>
51+
<!ATTLIST line number CDATA #REQUIRED>
52+
<!ATTLIST line hits CDATA #REQUIRED>
53+
<!ATTLIST line branch CDATA "false">
54+
<!ATTLIST line condition-coverage CDATA "100%">
55+
56+
<!ELEMENT conditions (condition*)>
57+
58+
<!ELEMENT condition EMPTY>
59+
<!ATTLIST condition number CDATA #REQUIRED>
60+
<!ATTLIST condition type CDATA #REQUIRED>
61+
<!ATTLIST condition coverage CDATA #REQUIRED>

‎cloverage/src/cloverage/args.clj‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
:junit? boolean?
2727
:lcov? boolean?
2828
:codecov? boolean?
29+
:cobertura? boolean?
2930
:coveralls? boolean?
3031
:summary? boolean?
3132
:colorize? boolean?
@@ -84,7 +85,7 @@
8485
(def boolean-flags
8586
(letfn [(add-? [k]
8687
[k (keyword (str (name k) \?))])]
87-
(->> [:text :html :raw :emma-xml :junit :lcov :codecov :coveralls :summary :colorize :debug :nop :help]
88+
(->> [:text :html :raw :emma-xml :junit :lcov :codecov :cobertura :coveralls :summary :colorize :debug :nop :help]
8889
(map add-?)
8990
(into {}))))
9091

@@ -127,6 +128,8 @@
127128
"Produce a lcov/gcov report." :default false]
128129
[nil "--[no-]codecov"
129130
"Generate a JSON report for Codecov.io" :default false]
131+
[nil "--[no-]cobertura"
132+
"Generate a Cobertura XML report." :default false]
130133
[nil "--[no-]coveralls"
131134
"Send a JSON report to Coveralls if on a CI server" :default false]
132135
[nil "--[no-]junit"

‎cloverage/src/cloverage/coverage.clj‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
[cloverage.instrument :as inst]
1212
[cloverage.report :as rep]
1313
[cloverage.report.codecov :as codecov]
14+
[cloverage.report.cobertura :as cobertura]
1415
[cloverage.report.console :as console]
1516
[cloverage.report.coveralls :as coveralls]
1617
[cloverage.report.emma-xml :as emma-xml]
@@ -271,7 +272,7 @@
271272
(f arg-map)))
272273

273274
(defn report-results
274-
[{:keys [text? html? raw? emma-xml? lcov? codecov? coveralls? summary? colorize? low-watermark high-watermark
275+
[{:keys [text? html? raw? emma-xml? lcov? codecov? cobertura? coveralls? summary? colorize? low-watermark high-watermark
275276
custom-report ^String output], :as opts}
276277
project-opts
277278
forms]
@@ -283,6 +284,7 @@
283284
(when lcov? (lcov/report output forms))
284285
(when raw? (raw/report output forms @*covered*))
285286
(when codecov? (codecov/report output forms))
287+
(when cobertura? (cobertura/report output forms))
286288
(when coveralls? (coveralls/report output forms))
287289
(when summary? (console/summary forms low-watermark high-watermark colorize?)))
288290
(when custom-report (launch-custom-report custom-report {:project project-opts
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
(ns cloverage.report.cobertura
2+
(:require [clojure.data.xml :as xml]
3+
[clojure.java.io :as io]
4+
[cloverage.report :refer [line-stats with-out-writer]]
5+
[cloverage.source :refer [source-file-path]]))
6+
7+
(defn- line-rate [covered total]
8+
(if (pos? total) (/ (double covered) total) 0.0))
9+
10+
(defn- file-coverage [[file forms]]
11+
(let [lib (str (:lib (first forms)))
12+
last-dot (.lastIndexOf ^String lib ".")
13+
lines (vec (filter :instrumented? (line-stats forms)))]
14+
{:name lib
15+
:filename (source-file-path file)
16+
:package (if (neg? last-dot) "" (subs lib 0 last-dot))
17+
:lines lines
18+
:valid (count lines)
19+
:covered (count (filter #(pos? (:times-hit %)) lines))}))
20+
21+
(defn- totals [files]
22+
(let [covered (reduce + (map :covered files))
23+
valid (reduce + (map :valid files))]
24+
{:covered covered :valid valid :rate (line-rate covered valid)}))
25+
26+
(defn- class-element [{:keys [name filename lines valid covered]}]
27+
[:class {:name name :filename filename
28+
:line-rate (line-rate covered valid) :branch-rate 0.0 :complexity 0}
29+
[:methods]
30+
(into [:lines]
31+
(map (fn [line]
32+
[:line {:number (:line line) :hits (:times-hit line) :branch "false"}])
33+
lines))])
34+
35+
(defn- package-element [[package files]]
36+
[:package {:name package :line-rate (:rate (totals files)) :branch-rate 0.0 :complexity 0}
37+
(into [:classes] (map class-element files))])
38+
39+
(defn write-cobertura-report
40+
"Write a Cobertura XML report to *out* from gather-stats records, which include :lib.
41+
Branch and method coverage are not tracked by Cloverage."
42+
[forms]
43+
(let [files (mapv file-coverage (sort-by first (filter first (group-by :file forms))))
44+
{:keys [covered valid rate]} (totals files)]
45+
(xml/emit
46+
(xml/sexp-as-element
47+
[:coverage {:line-rate rate :lines-covered covered :lines-valid valid
48+
:branch-rate 0.0 :branches-covered 0 :branches-valid 0
49+
:complexity 0 :version "cloverage"
50+
:timestamp (quot (System/currentTimeMillis) 1000)}
51+
[:sources [:source "."]]
52+
(into [:packages]
53+
(map package-element (sort-by first (group-by :package files))))])
54+
*out*)))
55+
56+
(defn report
57+
"Write a Cobertura XML report to '${out-dir}/cobertura.xml'."
58+
[^String out-dir forms]
59+
(let [output-file (io/file out-dir "cobertura.xml")]
60+
(println "Writing Cobertura report to:" (.getAbsolutePath output-file))
61+
(with-out-writer output-file
62+
(write-cobertura-report forms))))
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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)))))))

‎scripts/test-plugin-integration.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ repo_root=$(cd "$(dirname "$0")/.." && pwd)
99

1010
cd "$repo_root/lein-cloverage/test-resources/integration"
1111
rm -rf target/integration
12-
lein cloverage --output target/integration/explicit --text --no-html
12+
lein cloverage --output target/integration/explicit --text --cobertura --no-html
1313
test -s target/integration/explicit/coverage.txt
14+
test -s target/integration/explicit/cobertura.xml
15+
grep -q '<coverage ' target/integration/explicit/cobertura.xml
1416

1517
CLOVERAGE_VERSION=1.3.0-SNAPSHOT lein with-profile +auto-version cloverage \
1618
--output target/integration/auto --text --no-html

0 commit comments

Comments
 (0)