Skip to content

Commit d231b07

Browse files
Merge pull request #6 from pbzdyl/issues/5
Added support for JUnit XML report generation (issue #5).
2 parents 64880a2 + ca1f0e2 commit d231b07

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ compileTestClojure {
4141
testClojure {
4242
// Standard JVM execution options here for test process
4343
systemProperty 'java.awt.headless', true
44+
// Specifying junitReport will trigger JUnit XML report generation
45+
// in addition to standard console output (turned off by default)
46+
junitReport = file("$buildDir/reports/junit-report.xml")
4447
}
4548
```
4649

src/main/kotlin/cursive/ClojurePlugin.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ open class ClojureTestRunner @Inject constructor(val fileResolver: FileResolver)
411411

412412
var classpath: FileCollection = SimpleFileCollection()
413413
var namespaces: Collection<String> = emptyList()
414+
var junitReport: File? = null
414415

415416
@TaskAction
416417
fun test() {
@@ -424,11 +425,13 @@ open class ClojureTestRunner @Inject constructor(val fileResolver: FileResolver)
424425

425426
logger.info("Testing " + namespaces.joinToString(", "))
426427

427-
val namespaceList = namespaces.map { "'$it" }.joinToString(" ")
428-
val script = listOf("(require 'clojure.test $namespaceList)",
429-
"(let [summary (clojure.test/run-tests $namespaceList)]",
430-
" (System/exit (+ (:fail summary) (:error summary))))")
431-
.joinToString("\n")
428+
val namespaceVec = namespaces.joinToString(" ", "'[", "]")
429+
val runnerInvocation = if (junitReport != null)
430+
"""(run-tests $namespaceVec "${junitReport?.absolutePath}")"""
431+
else
432+
"""(run-tests $namespaceVec)"""
433+
434+
val script = "$testRunnerScript\n$runnerInvocation"
432435

433436
// println(script)
434437

@@ -453,4 +456,9 @@ open class ClojureTestRunner @Inject constructor(val fileResolver: FileResolver)
453456

454457
exec.build().start().waitForFinish().assertNormalExitValue()
455458
}
459+
460+
companion object {
461+
val testRunnerScript =
462+
ClojureTestRunner::class.java.getResourceAsStream("/cursive/test_runner.clj").bufferedReader().use { it.readText() }
463+
}
456464
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)