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

Added ability to run instrumentation tests before unit tests and merge all tests' reports into single jacoco coverage report #21

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.dicedmelon.gradle.jacoco.android

import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.api.logging.Logger
Expand All @@ -25,14 +26,17 @@ class JacocoAndroidPlugin implements Plugin<ProjectInternal> {
project.plugins.apply(JacocoPlugin)

Plugin plugin = findAndroidPluginOrThrow(project.plugins)
Task jacocoTestReportTask = findOrCreateJacocoTestReportTask(project.tasks)
def variants = getVariants(project, plugin)

variants.all { variant ->
JacocoReport reportTask = createReportTask(project, variant)
jacocoTestReportTask.dependsOn reportTask

logTaskAdded(reportTask)
project.afterEvaluate {
Task jacocoTestReportTask = findOrCreateJacocoTestReportTask(project.tasks)
def variants = getVariants(project, plugin)

variants.all { variant ->
JacocoReport reportTask = createReportTask(project, variant)
jacocoTestReportTask.dependsOn reportTask
project.tasks{ task -> logger.info(task.name) }
logTaskAdded(reportTask)
}
}
}

Expand Down Expand Up @@ -62,16 +66,25 @@ class JacocoAndroidPlugin implements Plugin<ProjectInternal> {
private static JacocoReport createReportTask(ProjectInternal project, variant) {
def sourceDirs = sourceDirs(variant)
def classesDir = classesDir(variant)

boolean integrationTestsEnabled = project.jacocoAndroidUnitTestReport.integrationTestsEnabled
Task integrationTestTask = getTask(project, getIntegrationTaskName(variant))
def testTask = testTask(project.tasks, variant)
def executionData = executionDataFile(testTask)

def executionData = executionDataFile(testTask, integrationTestsEnabled, project)
JacocoReport reportTask = project.tasks.create("jacoco${testTask.name.capitalize()}Report",
JacocoReport)
reportTask.dependsOn testTask
if(integrationTestsEnabled){
integrationTestTask.dependsOn testTask
reportTask.dependsOn integrationTestTask
}else {
reportTask.dependsOn testTask
}
reportTask.group = "Reporting"
reportTask.description = "Generates Jacoco coverage reports for the ${variant.name} variant."
reportTask.executionData = project.files(executionData)
reportTask.executionData = executionDataFile(testTask, integrationTestsEnabled, project)
reportTask.sourceDirectories = project.files(sourceDirs)
reportTask.classDirectories =
reportTask.classDirectories =
project.fileTree(dir: classesDir, excludes: project.jacocoAndroidUnitTestReport.excludes)
reportTask.reports {
csv.enabled project.jacocoAndroidUnitTestReport.csv.enabled
Expand All @@ -89,12 +102,25 @@ class JacocoAndroidPlugin implements Plugin<ProjectInternal> {
variant.javaCompile.destinationDir
}

static def getTask(Project project, String name) {
project.getTasksByName(name, false)?.getAt(0)
}

static String getIntegrationTaskName(def variant) {
String name = "create${variant.name.capitalize()}CoverageReport"
name
}

static def testTask(TaskCollection<Task> tasks, variant) {
tasks.withType(Test).find { task -> task.name.contains(variant.name.capitalize()) }
}

static def executionDataFile(Task testTask) {
testTask.jacoco.destinationFile.path
static def executionDataFile(Task testTask, boolean integrationTestsEnabled, ProjectInternal project) {
if(integrationTestsEnabled){
project.fileTree(dir: project.buildDir, includes: ['**/*.exec', '**/*.ec'])
} else {
project.files(testTask.jacoco.destinationFile.path)
}
}

private void logTaskAdded(JacocoReport reportTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class JacocoAndroidUnitTestReportExtension {
ReportConfiguration csv
ReportConfiguration html
ReportConfiguration xml
boolean integrationTestsEnabled

JacocoAndroidUnitTestReportExtension(Collection<String> excludes) {
this.excludes = excludes
this.csv = new ReportConfiguration(false)
this.html = new ReportConfiguration(true)
this.xml = new ReportConfiguration(true)
integrationTestsEnabled = false
}
}