Skip to content

Add verification of the generated code coverage report #6

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

Merged
merged 4 commits into from
Jan 22, 2019
Merged
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
5 changes: 3 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ ext {
minSdk : 21,
targetSdk : 28,
compileSdk : 28,
// Kotlin version is used in multiple places hence it is available as version variable
kotlin : "1.3.11"
]
projectDependency = [
Expand All @@ -20,7 +19,9 @@ ext {
junit : "junit:junit:4.12",
truth : "com.google.truth:truth:0.42",
supportTestRunner : "com.android.support.test:runner:1.0.2",
espressoCore : "com.android.support.test.espresso:espresso-core:3.0.2"
espressoCore : "com.android.support.test.espresso:espresso-core:3.0.2",
commonsCsv : "org.apache.commons:commons-csv:1.6",
kotlinTest : "org.jetbrains.kotlin:kotlin-test:${projectVersion.kotlin}"
]

// Used by the plugin-version-handler.gradle
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Tue Jan 22 16:40:52 CET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
2 changes: 2 additions & 0 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ dependencies {
implementation projectDependency.kotlinStdlibJdk7

testImplementation projectDependency.kotlinStdlibJdk7
testImplementation projectDependency.kotlinTest
testImplementation gradleTestKit()
testImplementation projectDependency.junit
testImplementation projectDependency.truth
testImplementation projectDependency.commonsCsv
}

apply from: rootProject.file('publish.gradle')
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,35 @@ class RootCoveragePlugin : Plugin<Project> {
project.afterEvaluate { createCoverageTaskForRoot(it) }
}

private fun getFileFilterPatterns(): List<String> {
return listOf(
"**/AutoValue_*.*", // Filter to remove generated files from: https://github.com/google/auto
//"**/*JavascriptBridge.class",

// Android Databinding
"**/*databinding",
"**/*binders",
"**/*layouts",
"**/BR.class", // Filter to remove generated databinding files

// Core Android generated class filters
"**/R.class",
"**/R$*.class",
"**/Manifest*.*",
"**/BuildConfig.class",
"android/**/*.*",

"**/*\$ViewBinder*.*",
"**/*\$ViewInjector*.*",
"**/Lambda$*.class",
"**/Lambda.class",
"**/*Lambda.class",
"**/*Lambda*.class",
"**/*\$InjectAdapter.class",
"**/*\$ModuleAdapter.class",
"**/*\$ViewInjector*.class") + rootProjectExtension.excludes
}

private fun getBuildVariantFor(project: Project): String {
return rootProjectExtension.buildVariantOverrides[project.path]
?: rootProjectExtension.buildVariant
}
private fun getFileFilterPatterns(): List<String> = listOf(
"**/AutoValue_*.*", // Filter to remove generated files from: https://github.com/google/auto
//"**/*JavascriptBridge.class",

// Android Databinding
"**/*databinding",
"**/*binders",
"**/*layouts",
"**/BR.class", // Filter to remove generated databinding files

// Core Android generated class filters
"**/R.class",
"**/R$*.class",
"**/Manifest*.*",
"**/BuildConfig.class",
"android/**/*.*",

"**/*\$ViewBinder*.*",
"**/*\$ViewInjector*.*",
"**/Lambda$*.class",
"**/Lambda.class",
"**/*Lambda.class",
"**/*Lambda*.class",
"**/*\$InjectAdapter.class",
"**/*\$ModuleAdapter.class",
"**/*\$ViewInjector*.class") + rootProjectExtension.excludes

private fun getBuildVariantFor(project: Project): String =
rootProjectExtension.buildVariantOverrides[project.path] ?: rootProjectExtension.buildVariant

private fun getExecutionDataFilePatterns(): List<String> {
val list = mutableListOf<String>()
Expand Down Expand Up @@ -99,10 +95,13 @@ class RootCoveragePlugin : Plugin<Project> {
task.group = "reporting"
task.description = "Generates a Jacoco report with combined results from all the subprojects."

task.reports.html.isEnabled = true
task.reports.xml.isEnabled = false
task.reports.csv.isEnabled = false
task.reports.html.isEnabled = rootProjectExtension.generateHtml
task.reports.xml.isEnabled = rootProjectExtension.generateXml
task.reports.csv.isEnabled = rootProjectExtension.generateCsv

task.reports.html.destination = project.file("${project.buildDir}/reports/jacoco")
task.reports.xml.destination = project.file("${project.buildDir}/reports/jacoco.xml")
task.reports.csv.destination = project.file("${project.buildDir}/reports/jacoco.csv")

// Add some run-time checks.
task.doFirst {
Expand Down Expand Up @@ -200,8 +199,6 @@ class RootCoveragePlugin : Plugin<Project> {
val javaClassTrees = javaClassOutputs.files.map { file ->
project.fileTree(file, excludes = getFileFilterPatterns()).excludeNonClassFiles()
}
// Hard coded alternative to get class files for Java.
//val classesTree = project.fileTree(mapOf("dir" to "${project.buildDir}/intermediates/classes/${variant.dirName}", "excludes" to getFileFilterPatterns()))

// TODO: No idea how to dynamically get the kotlin class files output folder, so for now this is hardcoded.
// TODO: For some reason the tmp/kotlin-classes folder does not use the variant.dirName property, for now we instead use the variant.name.
Expand All @@ -226,21 +223,9 @@ class RootCoveragePlugin : Plugin<Project> {
// Make the root task depend on the sub-project code coverage task
rootTask.dependsOn(subModuleTask)

// Set or add the sub-task class directories to the root task
if (rootTask.classDirectories == null) {
rootTask.classDirectories.setFrom(subModuleTask.classDirectories)
} else {
rootTask.additionalClassDirs(subModuleTask.classDirectories)
}

// Set or add the sub-task source directories to the root task
if (rootTask.sourceDirectories == null) {
rootTask.sourceDirectories.setFrom(subModuleTask.sourceDirectories)
} else {
rootTask.additionalSourceDirs(subModuleTask.sourceDirectories)
}

// Add the sub-task class directories to the root task
rootTask.executionData(subModuleTask.executionData)
// Add the sub-task class directories, source directories and executionData to the root task
rootTask.classDirectories.from(subModuleTask.classDirectories)
rootTask.sourceDirectories.from(subModuleTask.sourceDirectories)
rootTask.executionData.from(subModuleTask.executionData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.android.builder.model.TestVariantBuildOutput

open class RootCoveragePluginExtension {

var generateCsv: Boolean = false
var generateHtml: Boolean = true
var generateXml: Boolean = false
var buildVariant: String = "debug"
var buildVariantOverrides: Map<String, String> = mutableMapOf()
var excludes: List<String> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ import org.gradle.api.file.FileTree
/**
* Executes an include match on the FileTree that only includes files with the .class extension.
*/
fun FileTree.excludeNonClassFiles(): FileTree {
return matching {
it.include("**/*.class")
}
}
fun FileTree.excludeNonClassFiles(): FileTree = matching { it.include("**/*.class") }

/**
* Wrapper around Project.fileTree(Map<String, ?>) to use it easier from Kotlin code (no need to create the map every time).
* Currently only supports the dir, excludes and includes properties.
* */
fun Project.fileTree(dir: Any, excludes: List<String> = listOf(), includes: List<String> = listOf()): ConfigurableFileTree {
return fileTree(mapOf(
"dir" to dir,
"excludes" to excludes,
"includes" to includes))
}
fun Project.fileTree(dir: Any, excludes: List<String> = listOf(), includes: List<String> = listOf()): ConfigurableFileTree =
fileTree(mapOf(
"dir" to dir,
"excludes" to excludes,
"includes" to includes))
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.neotech.plugin.rootcoverage

import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord
import java.io.File
import java.nio.charset.StandardCharsets
import kotlin.test.assertEquals

class CoverageReport private constructor(
private val instructionMissedColumn: Int,
private val branchMissedColumn: Int,
private val packageColumn: Int,
private val classColumn: Int,
private val records: List<CSVRecord>) {

private fun CSVRecord?.assertFullCoverageCoverage() {
kotlin.test.assertNotNull(this)
assertEquals(this[instructionMissedColumn]?.toInt(), 0)
assertEquals(this[branchMissedColumn]?.toInt(), 0)
}

fun assertFullCoverage(packageName: String, className: String) {
find(packageName, className).assertFullCoverageCoverage()
}

fun find(packageName: String, className: String): CSVRecord? = records.find {
it[packageColumn] == packageName && it[classColumn] == className
}

companion object {

fun from(file: File): CoverageReport = CSVParser.parse(file, StandardCharsets.UTF_8, CSVFormat.DEFAULT.withHeader()).use {
CoverageReport(
it.headerMap["INSTRUCTION_MISSED"]!!,
it.headerMap["BRANCH_MISSED"]!!,
it.headerMap["PACKAGE"]!!,
it.headerMap["CLASS"]!!,
it.records)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.neotech.plugin.rootcoverage

import com.google.common.truth.Truth.assertThat
import org.gradle.testing.jacoco.tasks.JacocoReport
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
import java.io.File
import java.io.OutputStreamWriter
import kotlin.test.assertEquals

@RunWith(Parameterized::class)
class IntegrationTest(
Expand All @@ -32,13 +32,18 @@ class IntegrationTest(

assertThat(result.output).contains("BUILD SUCCESSFUL")
assertEquals(result.task(":rootCodeCoverageReport")!!.outcome, TaskOutcome.SUCCESS)

val report = CoverageReport.from(File(projectRoot, "build/reports/jacoco.csv"))

report.assertFullCoverage("org.neotech.library.android", "LibraryAndroidJava")
report.assertFullCoverage("org.neotech.library.android", "LibraryAndroidKotlin")
}

companion object {

// This method is used by the JVM (Parameterized JUnit Runner)
@Suppress("unused")
@Parameters(name = "{1}")
@Parameterized.Parameters(name = "{1}")
@JvmStatic
fun parameters(): List<Array<Any>> {
return File("src/test/test-fixtures")
Expand Down
1 change: 1 addition & 0 deletions plugin/src/test/test-fixtures/multi-module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jacoco {
import com.android.builder.model.TestVariantBuildOutput.TestType

rootCoverage {
generateCsv true
buildVariant "debug"
testTypes = [TestType.UNIT, TestType.ANDROID_TEST]
}