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

Add configuration options for report-path and message #16

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.<br>
One of 'INSTRUCTION', 'BRANCH', 'LINE', 'COMPLEXITY', 'METHOD' or 'CLASS'<br>
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).
Expand Down Expand Up @@ -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.
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import org.gradle.api.provider.Property
class PrintCoverageExtension {

final Property<String> coverageType
final Property<String> reportFile
final Property<String> message

PrintCoverageExtension(Project project) {
coverageType = project.objects.property(String)
coverageType.set('INSTRUCTION')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults are now handled directly in the task to allow custom task definitions with minimal configuration

reportFile = project.objects.property(String)
message = project.objects.property(String)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ class PrintCoveragePlugin implements Plugin<Project> {
}

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows to reference the PrintCoverageTask in build-scripts without having to use the fully qualified class name:
task customTask(type: PrintCoverage) { ... }
instead of
task customTask(type: de.jansauer.printcoverage.PrintCoverageTask) { ... }


task.dependsOn(target.tasks.withType(JacocoReport))
}
}
22 changes: 18 additions & 4 deletions src/main/groovy/de/jansauer/printcoverage/PrintCoverageTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional inputs with defaults handled in the task to allow custom task definitions extending this task with minimal configuration

final Property<String> coverageType = project.objects.property(String)

@Input
@Optional
final Property<String> reportFile = project.objects.property(String)

@Input
@Optional
final Property<String> message = project.objects.property(String)

PrintCoverageTask() {
setDescription('Prints code coverage for gitlab.')
setGroup('coverage')
Expand All @@ -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() }[email protected]()
double covered = report.counter.find { it.'@type' == coverageType.get() }[email protected]()

double missed = report.counter.find { it.'@type' == finalCoverageType }[email protected]()
double covered = report.counter.find { it.'@type' == finalCoverageType }[email protected]()
def coverage = (100 / (missed + covered) * covered).round(2)
println 'Coverage: ' + coverage + '%'

println String.format(finalMessage, coverage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PrintCoveragePluginTest extends Specification {

File reportFile

File customReportFile

def setup() {
temporaryFolder = new TemporaryFolder()
temporaryFolder.create()
Expand All @@ -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"() {
Expand Down Expand Up @@ -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
}

}