/
cloverage.clj
170 lines (143 loc) · 6.01 KB
/
cloverage.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(ns kaocha.plugin.cloverage
(:require [clojure.string :as str]
[cloverage.coverage :as c]
[kaocha.api :as api]
[kaocha.plugin :as plugin :refer [defplugin]]
[kaocha.result :as result]
[kaocha.classpath :as cp]
[slingshot.slingshot :refer [throw+]]))
(defn accumulate [m k v]
(update m k (fnil conj []) v))
(def default-opts
{:output "target/coverage"
:text? false
:html? true
:emma-xml? false
:lcov? false
:codecov? false
:coveralls? false
:summary? true
:fail-threshold 0
:low-watermark 50
:high-watermark 80
:nop? false
:src-ns-path []
:ns-regex []
:ns-exclude-regex []})
(def cli-opts*
[["--cov-output PATH" "Cloverage output directory."]
["--[no-]cov-text" "Produce a text report."]
["--[no-]cov-html" "Produce a HTML report."]
["--[no-]emma-xml"
"Produce an EMMA XML report. [emma.sourceforge.net]"]
["--[no-]lcov"
"Produce a lcov/gcov report."]
["--[no-]codecov"
"Generate a JSON report for Codecov.io"]
["--[no-]coveralls"
"Send a JSON report to Coveralls if on a CI server"]
["--[no-]cov-summary"
"Prints a summary"]
["--cov-fail-threshold PERCENT"
"Sets the percentage threshold at which cloverage will abort the build. Default: 0%"
:parse-fn #(Integer/parseInt %)]
["--cov-low-watermark PERCENT"
"Sets the low watermark percentage (valid values 0..100). Default: 50%"
:parse-fn #(Integer/parseInt %)]
["--cov-high-watermark PERCENT"
"Sets the high watermark percentage (valid values 0..100). Default: 80%"
:parse-fn #(Integer/parseInt %)]
["--[no-]cov-nop" "Instrument with noops."]
["--cov-ns-regex REGEX"
"Regex for instrumented namespaces (can be repeated)."
:assoc-fn accumulate]
["--cov-ns-exclude-regex REGEX"
"Regex for namespaces not to be instrumented (can be repeated)."
:assoc-fn accumulate]
["--cov-src-ns-path PATH"
"Path (string) to directory containing test namespaces (can be repeated). Defaults to test suite source paths."
:assoc-fn accumulate]])
(def cli-opts (map #(into [nil] %) cli-opts*))
(defn update-config [config]
(let [opts (:kaocha/cli-options config)]
(update config
:cloverage/opts
#(cond-> (merge default-opts %)
(contains? opts :cov-output)
(assoc :output (:cov-output opts))
(contains? opts :cov-text)
(assoc :text? (:cov-text opts))
(contains? opts :cov-html)
(assoc :html? (:cov-html opts))
(contains? opts :emma-xml)
(assoc :emma-xml? (:emma-xml opts))
(contains? opts :lcov)
(assoc :lcov? (:lcov opts))
(contains? opts :codecov)
(assoc :codecov? (:codecov opts))
(contains? opts :coveralls)
(assoc :coveralls? (:coveralls opts))
(contains? opts :cov-summary)
(assoc :summary? (:cov-summary opts))
(contains? opts :cov-fail-threshold)
(assoc :fail-threshold (:cov-fail-threshold opts))
(contains? opts :cov-low-watermark)
(assoc :low-watermark (:cov-low-watermark opts))
(contains? opts :cov-high-watermark)
(assoc :high-watermark (:cov-high-watermark opts))
(contains? opts :cov-nop)
(assoc :nop? (:cov-nop opts))
(contains? opts :cov-src-ns-path)
(assoc :src-ns-path (:cov-src-ns-path opts))
(contains? opts :cov-ns-regex)
(assoc :ns-regex (:cov-ns-regex opts))
(contains? opts :cov-ns-exclude-regex)
(update :ns-exclude-regex into (:cov-ns-exclude-regex opts))
:always
(update :ns-regex (partial map re-pattern))
:always
(update :ns-exclude-regex (partial map re-pattern))))))
(defn run-cloverage [opts]
;; Compatibility with future versions
(let [arity (count (first (:arglists (meta #'c/run-main))))]
(case arity
1 (c/run-main [opts])
2 (c/run-main [opts] {}))))
(defplugin kaocha.plugin/cloverage
(cli-options [opts]
(into opts cli-opts))
;; The config hook only gets called inside api/run, which is too late for us,
;; so update-config also gets called explicitly in `main`. Calling
;; update-config here anyway so people can see the result of option merging
;; with --print-config. It's a bit of duplicated work though, we might have to
;; rethink this to make people implementing custom `main` hooks able to still
;; use `config` hooks.
(config [config]
(update-config config))
(main [config]
(binding [c/*exit-after-test* false]
(let [config (update-config config)
suites (remove :kaocha.testable/skip (:kaocha/tests config))
source-paths (mapcat :kaocha/source-paths suites)
test-paths (mapcat :kaocha/test-paths suites)
tests (map :kaocha.testable/id suites)
cloverage-opts (:cloverage/opts config)
opts (assoc cloverage-opts
:runner :kaocha
:src-ns-path (if-let [src-ns-path (seq (:src-ns-path cloverage-opts))]
src-ns-path
source-paths)
:extra-test-ns tests
;;:debug? true
::config config)]
(run! cp/add-classpath test-paths)
(throw+ {:kaocha/early-exit (run-cloverage opts)})))))
(defmethod c/runner-fn :kaocha [opts]
(fn [_test-nses]
(let [result (->> (::config opts)
api/run
(plugin/run-hook :kaocha.hooks/post-summary)
:kaocha.result/tests
result/totals)]
{:errors (+ (::result/error result)
(::result/fail result))})))