diff --git a/README.md b/README.md index ff51d83..d2cf081 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ For more information see [Gitalb test coverage parsing](https://docs.gitlab.com/ ## Getting Started -Add this snippet to yout build script. +Add this snippet to your build script. ``` plugins { @@ -33,16 +33,32 @@ build-gradle: ## Configuration +Configuration for the default 'printCoverage' task: ``` printcoverage { coverageType = 'INSTRUCTION' + reportFile = "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml" + message = 'Coverage: %s%%' } ``` * `coverageType`: Type of [coverage metric](http://www.eclemma.org/jacoco/trunk/doc/counters.html) to be printed.
One of 'INSTRUCTION', 'BRANCH', 'LINE', 'COMPLEXITY', 'METHOD' or 'CLASS'
Default: 'INSTRUCTION' +* `reportFile`: Path to the Jacoco xml-report to be used + Default: "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml" +* `message`: Format string to be used for printing the coverage information + Default: 'Coverage: %s%%' +To print coverage information for different reports, define custom tasks: +``` +task printCombinedCoverage(type: PrintCoverage) { + reportFile = "$buildDir/reports/jacoco/combinedCoverageReport/combinedCoverageReport.xml" + message = 'Total Coverage: %s%%' +} +``` + + ## Publishing Workflow Every commit on this repository gets tested via [circleci](https://circleci.com/gh/jansauer/gradle-print-coverage-plugin). @@ -78,4 +94,4 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -``` \ No newline at end of file +``` diff --git a/src/main/groovy/de/jansauer/printcoverage/PrintCoverageExtension.groovy b/src/main/groovy/de/jansauer/printcoverage/PrintCoverageExtension.groovy index 1ddc02d..72b8c80 100644 --- a/src/main/groovy/de/jansauer/printcoverage/PrintCoverageExtension.groovy +++ b/src/main/groovy/de/jansauer/printcoverage/PrintCoverageExtension.groovy @@ -6,9 +6,12 @@ import org.gradle.api.provider.Property class PrintCoverageExtension { final Property coverageType + final Property reportFile + final Property message PrintCoverageExtension(Project project) { coverageType = project.objects.property(String) - coverageType.set('INSTRUCTION') + reportFile = project.objects.property(String) + message = project.objects.property(String) } } diff --git a/src/main/groovy/de/jansauer/printcoverage/PrintCoveragePlugin.groovy b/src/main/groovy/de/jansauer/printcoverage/PrintCoveragePlugin.groovy index b94b1ea..33a7b09 100644 --- a/src/main/groovy/de/jansauer/printcoverage/PrintCoveragePlugin.groovy +++ b/src/main/groovy/de/jansauer/printcoverage/PrintCoveragePlugin.groovy @@ -15,9 +15,15 @@ class PrintCoveragePlugin implements Plugin { } def extension = target.extensions.create('printcoverage', PrintCoverageExtension, target) + Task task = target.tasks.create('printCoverage', PrintCoverageTask) { coverageType = extension.coverageType + reportFile = extension.reportFile + message = extension.message } + + target.ext.PrintCoverage = PrintCoverageTask + task.dependsOn(target.tasks.withType(JacocoReport)) } } diff --git a/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy b/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy index e62ded2..18612ad 100644 --- a/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy +++ b/src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy @@ -4,13 +4,23 @@ import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.provider.Property import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.TaskAction class PrintCoverageTask extends DefaultTask { @Input + @Optional final Property coverageType = project.objects.property(String) + @Input + @Optional + final Property reportFile = project.objects.property(String) + + @Input + @Optional + final Property message = project.objects.property(String) + PrintCoverageTask() { setDescription('Prints code coverage for gitlab.') setGroup('coverage') @@ -22,16 +32,20 @@ class PrintCoverageTask extends DefaultTask { slurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false) slurper.setFeature('http://apache.org/xml/features/nonvalidating/load-external-dtd', false) - File jacocoTestReport = new File("${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml") + File jacocoTestReport = new File(reportFile.getOrElse("${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml")) if (!jacocoTestReport.exists()) { logger.error('Jacoco test report is missing.') throw new GradleException('Jacoco test report is missing.') } + def finalCoverageType = coverageType.getOrElse('INSTRUCTION') + def finalMessage = message.getOrElse('Coverage: %s%%') def report = slurper.parse(jacocoTestReport) - double missed = report.counter.find { it.'@type' == coverageType.get() }.@missed.toDouble() - double covered = report.counter.find { it.'@type' == coverageType.get() }.@covered.toDouble() + + double missed = report.counter.find { it.'@type' == finalCoverageType }.@missed.toDouble() + double covered = report.counter.find { it.'@type' == finalCoverageType }.@covered.toDouble() def coverage = (100 / (missed + covered) * covered).round(2) - println 'Coverage: ' + coverage + '%' + + println String.format(finalMessage, coverage) } } diff --git a/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy b/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy index d60a71f..0894f9a 100644 --- a/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy +++ b/src/test/groovy/de/jansauer/printcoverage/PrintCoveragePluginTest.groovy @@ -21,6 +21,8 @@ class PrintCoveragePluginTest extends Specification { File reportFile + File customReportFile + def setup() { temporaryFolder = new TemporaryFolder() temporaryFolder.create() @@ -30,11 +32,16 @@ class PrintCoveragePluginTest extends Specification { org.gradle.jvmargs=-Xmx512m -Xms256m """ buildFile = temporaryFolder.newFile('build.gradle') - reportFile = temporaryFolder - .newFolder('build', 'reports', 'jacoco', 'test') + def buildDir = temporaryFolder + .newFolder('build', 'reports', 'jacoco', 'test') + reportFile = buildDir .toPath() .resolve('jacocoTestReport.xml') .toFile() + customReportFile = buildDir + .toPath() + .resolve('customTestReport.xml') + .toFile() } def "should fail if the jacoco plugin is missing"() { @@ -246,4 +253,96 @@ class PrintCoveragePluginTest extends Specification { 'METHOD' | '66.67%' 'CLASS' | '100.0%' } + + def "should use configured message" () { + given: + buildFile << """ + plugins { + id 'jacoco' + id 'de.jansauer.printcoverage' + } + + printcoverage { + message = 'Custom message: %s%%' + } + """ + reportFile << new File("src/test/resources/jacocoTestReport.xml").text + + when: + def result = GradleRunner.create() + .withGradleVersion(gradleVersion) + .withProjectDir(temporaryFolder.root) + .withArguments('printCoverage') + .withPluginClasspath() + .build() + + then: + result.output.contains('Custom message: 3.13%') + result.task(":printCoverage").outcome == SUCCESS + + where: + gradleVersion << SUPPORTED_GRADLE_VERSIONS + } + + def "should use configured report-path" () { + given: + buildFile << """ + plugins { + id 'jacoco' + id 'de.jansauer.printcoverage' + } + + printcoverage { + reportFile = "\$buildDir/reports/jacoco/test/customTestReport.xml".toString() + } + """ + customReportFile << new File("src/test/resources/jacocoTestReport.xml").text + + when: + def result = GradleRunner.create() + .withGradleVersion(gradleVersion) + .withProjectDir(temporaryFolder.root) + .withArguments('printCoverage') + .withPluginClasspath() + .build() + + then: + result.output.contains('Coverage: 3.13%') + result.task(":printCoverage").outcome == SUCCESS + + where: + gradleVersion << SUPPORTED_GRADLE_VERSIONS + } + + def "should allow custom task definitions" () { + given: + buildFile << """ + plugins { + id 'jacoco' + id 'de.jansauer.printcoverage' + } + + task printCustomCoverage(type: PrintCoverage) { + reportFile = "\$buildDir/reports/jacoco/test/customTestReport.xml".toString() + message = 'Custom coverage: %s%%' + } + """ + customReportFile << new File("src/test/resources/jacocoTestReport.xml").text + + when: + def result = GradleRunner.create() + .withGradleVersion(gradleVersion) + .withProjectDir(temporaryFolder.root) + .withArguments('printCustomCoverage') + .withPluginClasspath() + .build() + + then: + result.output.contains('Custom coverage: 3.13%') + result.task(":printCustomCoverage").outcome == SUCCESS + + where: + gradleVersion << SUPPORTED_GRADLE_VERSIONS + } + }