Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cobertura reports #325

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Available options and command-line arguments:
:emma-xml? --no-emma-xml, --emma-xml false Produce an EMMA XML report. [emma.sourceforge.net]
:lcov? --no-lcov, --lcov false Produce a lcov/gcov report.
:codecov? --no-codecov, --codecov false Generate a JSON report for Codecov.io
:cobertura? --no-cobertura, --cobertura false Generate a cobertura report.
:coveralls? --no-coveralls, --coveralls false Send a JSON report to Coveralls if on a CI server
:junit? --no-junit, --junit false Output test results as junit xml file. Supported in :clojure.test runner
:raw? --no-raw, --raw false Output raw coverage data (for debugging).
Expand Down
5 changes: 4 additions & 1 deletion cloverage/src/cloverage/args.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:junit? boolean?
:lcov? boolean?
:codecov? boolean?
:cobertura? boolean?
:coveralls? boolean?
:summary? boolean?
:colorize? boolean?
Expand Down Expand Up @@ -69,7 +70,7 @@
(def boolean-flags
(letfn [(add-? [k]
[k (keyword (str (name k) \?))])]
(->> [:text :html :raw :emma-xml :junit :lcov :codecov :coveralls :summary :colorize :debug :nop :help]
(->> [:text :html :raw :emma-xml :junit :lcov :codecov :coveralls :cobertura :summary :colorize :debug :nop :help]
(map add-?)
(into {}))))

Expand Down Expand Up @@ -112,6 +113,8 @@
"Produce a lcov/gcov report." :default false]
["--[no-]codecov"
"Generate a JSON report for Codecov.io" :default false]
["--[no-]cobertura"
"Generate a cobertura report" :default false]
["--[no-]coveralls"
"Send a JSON report to Coveralls if on a CI server" :default false]
["--[no-]junit"
Expand Down
8 changes: 5 additions & 3 deletions cloverage/src/cloverage/coverage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[cloverage.instrument :as inst]
[cloverage.report :as rep]
[cloverage.report.codecov :as codecov]
[cloverage.report.cobertura :as cobertura]
[cloverage.report.console :as console]
[cloverage.report.coveralls :as coveralls]
[cloverage.report.emma-xml :as emma-xml]
Expand Down Expand Up @@ -242,7 +243,7 @@
(throw (IllegalArgumentException.
"Runner not found. Built-in runners are `clojure.test`, `midje` and `eftest`.")))

(defn run-tests [{:keys [runner test-selectors selector junit?], :as opts} test-nses]
(defn run-tests [{:keys [runner test-selectors selector junit?] :as opts} test-nses]
;; load runner multimethod definition from other dependencies
(when-not (#{:clojure.test :midje :eftest} runner)
(try (require (symbol (format "%s.cloverage" (name runner))))
Expand All @@ -269,7 +270,7 @@
(f arg-map)))

(defn report-results
[{:keys [text? html? raw? emma-xml? lcov? codecov? coveralls? summary? colorize? low-watermark high-watermark
[{:keys [text? html? raw? emma-xml? lcov? codecov? coveralls? cobertura? summary? colorize? low-watermark high-watermark
custom-report ^String output], :as opts}
project-opts
forms]
Expand All @@ -281,6 +282,7 @@
(when lcov? (lcov/report output forms))
(when raw? (raw/report output forms @*covered*))
(when codecov? (codecov/report output forms))
(when cobertura? (cobertura/report output forms))
(when coveralls? (coveralls/report output forms))
(when summary? (console/summary forms low-watermark high-watermark colorize?)))
(when custom-report (launch-custom-report custom-report {:project project-opts
Expand All @@ -297,7 +299,7 @@
failed?)))

(defn run-main
[[{:keys [debug? junit? fail-threshold help? runner test-selectors selector], :as opts} add-nses help] project-opts]
[[{:keys [debug? fail-threshold help?], :as opts} add-nses help] project-opts]
(binding [*ns* (find-ns 'cloverage.coverage)
debug/*debug* debug?]
(if help?
Expand Down
20 changes: 10 additions & 10 deletions cloverage/src/cloverage/instrument.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
(if-let [o ((ns-map *ns*) s)]
(if (class? o)
(symbol (.getName ^Class o))
(if (var? o)
(when (var? o)
(let [^clojure.lang.Var o o]
(symbol (-> o .ns .name name) (-> o .sym name)))))
;; changed to returned unnamespaced symbol if it fails to resolve
Expand Down Expand Up @@ -193,7 +193,7 @@
"Traverse the given form and wrap all its sub-forms in a function that evals
the form and records that it was called."
{:arglists '([f line form env])}
(fn [f line form env]
(fn [_f _line form env]
(form-type form env)))

(defmacro wrapm
Expand Down Expand Up @@ -227,9 +227,9 @@
e.g. - `a (+ a b)` (let or loop)"
[f line-hint [args & body :as form]]
(d/tprnl "Wrapping overload" args body)
(let [line (or (:line (meta form)) line-hint)]
(let [wrapped (doall (map (wrapper f line) body))]
`(~args ~@wrapped))))
(let [line (or (:line (meta form)) line-hint)
wrapped (doall (map (wrapper f line) body))]
`(~args ~@wrapped)))

(defn wrap-overload
"Wrap a single function overload.
Expand Down Expand Up @@ -271,7 +271,7 @@
e)))))))

;; Don't wrap or descend into unknown forms
(defmethod do-wrap :unknown [f line form _]
(defmethod do-wrap :unknown [_f _line form _]
(log/warn (str "Unknown special form " (seq form)))
form)

Expand Down Expand Up @@ -339,7 +339,7 @@
(d/tprnl "Wrapping fn " form)
(f line (wrap-fn-body f line form)))

(defmethod do-wrap :let [f line [let-sym bindings & body :as form] _]
(defmethod do-wrap :let [f line [let-sym bindings & body] _]
(f line
`(~let-sym
[~@(mapcat (partial wrap-binding f line)
Expand All @@ -361,7 +361,7 @@
bindings)]
~@(doall (map (wrapper f line) body))))))

(defmethod do-wrap :def [f line [def-sym name & body :as form] _]
(defmethod do-wrap :def [f line [def-sym name & body] _]
(cond
(empty? body) (f line `(~def-sym ~name))
(= 1 (count body)) (let [init (first body)]
Expand All @@ -378,11 +378,11 @@
(let [[def-sym name fn-expr] (macroexpand-1 form)]
(f line `(~def-sym ~name ~(wrap-fn-body f line fn-expr)))))

(defmethod do-wrap :new [f line [new-sym class-name & args :as form] _]
(defmethod do-wrap :new [f line [new-sym class-name & args] _]
(f line `(~new-sym ~class-name ~@(doall (map (wrapper f line) args)))))

(defmethod do-wrap :dotjava
[f line [_ class-or-instance & more] env]
[f line [_ class-or-instance & more] _env]
;; form is either of the syntax
;;
;; (. class-or-instance method & args)
Expand Down
3 changes: 0 additions & 3 deletions cloverage/src/cloverage/report.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
(defn- group-by-line [forms]
(into (sorted-map) (group-by :line forms)))

(defn- group-by-file [forms]
(into (sorted-map) (group-by :file forms)))

(defn- postprocess-file [lib file forms]
(with-open [in (io/reader (resource-reader file))]
(let [forms-by-line (group-by-line forms)
Expand Down
66 changes: 66 additions & 0 deletions cloverage/src/cloverage/report/cobertura.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
(ns cloverage.report.cobertura
(:require
[clojure.java.io :as io]
[clojure.data.xml :as xml]
[clojure.string :as str]
[cloverage.report :refer [file-stats]]))

(defn line-stats [file-s]
[:line {:branch "false"
:hits 1
:number 3}])

(defn files [by-file]
[:classes
[:class {}
[:methods]
(into [:lines]
(map line-stats by-file))]])

(defn packages [by-package]
[:packages
[:package {:line-rate 0.2
:branch-rate 0.3
:name "name"
:complexity 0}
(into [:classes]
(map files by-package))]])

(defn f->pkg [filename]
(let [sp (str/split (str/replace filename "/" ".") #"\.")]
(->> sp
(take-nth (- (count sp) 2))
(str/join "."))))

(defn report
"Create '${out-dir}/cobertura.xml' in cobertura format"
[^String out-dir forms]
;; now with the stats above run the whole thing
(let [output-file (io/file out-dir "cobertura.xml")
stats (doall (file-stats forms))
with-package (map #(assoc % :package (f->pkg (:file %))) stats)

;; file-count (count (distinct (map :file stats)))
;; lib-count (count (distinct (map :lib stats)))
;; total (do-counters stats)
;; by-pkg (map do-counters (vals (group-by :lib stats)))
]

(println "Writing Cobertura report to:" (.getAbsolutePath output-file))
(with-open [wr (io/writer output-file)]
(-> [:coverage {:branch-rate 10
:branches-covered 0
:branches-valid 0
:complexity 0
:line-rate 0
:lines-covered 0
:lines-valid 3
:timestamp (System/currentTimeMillis)
:version "2.0.3"}
;; what is this `sources` for???
[:sources [:source "."]]
(into
[:packages]
(packages (group-by :package with-package)))]
xml/sexp-as-element
(xml/emit wr)))))
2 changes: 1 addition & 1 deletion cloverage/src/cloverage/report/coveralls.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
(let [lines (line-stats file-forms)]
{:name file
:source_digest (md5 (s/join "\n" (map :text lines)))
:coverage (map #(if (:instrumented? %) (:hit %)) lines)}))
:coverage (map #(when (:instrumented? %) (:hit %)) lines)}))

(defn report [^String out-dir forms]
(let [output-file (io/file out-dir "coveralls.json")
Expand Down
5 changes: 3 additions & 2 deletions cloverage/src/cloverage/report/raw.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns cloverage.report.raw
(:require
[clojure.java.io :as io]
[clojure.pprint :as pprint]
[cloverage.report :refer [with-out-writer]]))

(defn report [^String out-dir stats covered]
Expand All @@ -9,8 +10,8 @@

(println "Writing raw data to:" (.getAbsolutePath raw-data-file))
(with-out-writer raw-data-file
(clojure.pprint/pprint (zipmap (range) covered)))
(pprint/pprint (zipmap (range) covered)))

(println "Writing raw stats to:" (.getAbsolutePath raw-stats-file))
(with-out-writer raw-stats-file
(clojure.pprint/pprint stats))))
(pprint/pprint stats))))
2 changes: 1 addition & 1 deletion cloverage/test/cloverage/args_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns cloverage.args-test
(:require [clojure.test :refer :all]
(:require [clojure.test :refer [deftest testing is]]
[cloverage.args :as args]
[cloverage.coverage]))

Expand Down