|
| 1 | +import groovy.xml.XmlSlurper |
| 2 | + |
| 3 | +task coverageReportSummary { |
| 4 | + group = 'Verification' |
| 5 | + description = 'Prints Jacoco coverage summary for all subprojects' |
| 6 | + |
| 7 | + doLast { |
| 8 | + def totalCovered = 0 |
| 9 | + def totalMissed = 0 |
| 10 | + def results = [] |
| 11 | + |
| 12 | + println "" |
| 13 | + println "+---------------------------+---------------------+" |
| 14 | + println "| Project | Branch Coverage (%) |" |
| 15 | + println "+---------------------------+---------------------+" |
| 16 | + |
| 17 | + subprojects.each { subproject -> |
| 18 | + def reportFile = file("${subproject.name}/target/jacoco.xml") |
| 19 | + if (reportFile.exists()) { |
| 20 | + def xmlText = reportFile.text.replaceFirst(/<!DOCTYPE[^>]*>\s*/, '') |
| 21 | + def report = new XmlSlurper().parseText(xmlText) |
| 22 | + def counter = report.counter.find { it.@type == 'BRANCH' } |
| 23 | + |
| 24 | + if (counter) { |
| 25 | + def missed = counter.@missed.toInteger() |
| 26 | + def covered = counter.@covered.toInteger() |
| 27 | + def total = missed + covered |
| 28 | + def percent = total > 0 ? (covered * 100.0 / total) : 0 |
| 29 | + |
| 30 | + results << [project: subproject.name, percent: percent] |
| 31 | + |
| 32 | + totalCovered += covered |
| 33 | + totalMissed += missed |
| 34 | + } else { |
| 35 | + results << [project: subproject.name, percent: null] |
| 36 | + } |
| 37 | + } else { |
| 38 | + results << [project: subproject.name, percent: null] |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + results.each { |
| 43 | + def name = it.project.padRight(26) |
| 44 | + def coverage = it.percent != null ? |
| 45 | + String.format("%20.2f", it.percent) : |
| 46 | + " N/A " |
| 47 | + println "| ${name}|${coverage} |" |
| 48 | + } |
| 49 | + |
| 50 | + println "+---------------------------+---------------------+" |
| 51 | + |
| 52 | + def grandTotal = totalCovered + totalMissed |
| 53 | + if (grandTotal > 0) { |
| 54 | + def totalPercent = totalCovered * 100.0 / grandTotal |
| 55 | + println "| TOTAL |" + |
| 56 | + String.format("%20.2f", totalPercent) + " |" |
| 57 | + } else { |
| 58 | + println "| TOTAL | N/A |" |
| 59 | + } |
| 60 | + |
| 61 | + println "+---------------------------+---------------------+\n" |
| 62 | + } |
| 63 | +} |
0 commit comments