Skip to content

Commit

Permalink
Fix code coverage percentage reporting for Android projects (#7815)
Browse files Browse the repository at this point in the history
* Fix code coverage percentage reporting for Android projects

* Fix codenarc violations
  • Loading branch information
nikita-tkachenko-datadog authored Oct 23, 2024
1 parent 99b6311 commit 48a5921
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package datadog.trace.instrumentation.gradle

import datadog.trace.api.civisibility.domain.BuildModuleLayout
import datadog.trace.api.civisibility.domain.SourceSet
import org.gradle.api.Project
import org.gradle.api.file.FileTree
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.testing.Test

class AndroidGradleUtils {

private static final Logger LOGGER = Logging.getLogger(AndroidGradleUtils)

static BuildModuleLayout getAndroidModuleLayout(Project project, Test task) {
try {
def variant = getVariant(project, task)
if (variant == null) {
return null
}

def sources = getSources(variant)
def destinations = getDestinations(variant, project)
return new BuildModuleLayout(Collections.singletonList(new SourceSet(SourceSet.Type.CODE, sources, destinations)))
} catch (Exception e) {
LOGGER.error("Could not get Android module layout for ${project.name} and ${task.path}", e)
return null
}
}

private static getVariant(Project project, Test task) {
def androidPlugin = project.plugins.findPlugin('android') ?: project.plugins.findPlugin('android-library')

def variants
if (androidPlugin.class.simpleName == 'LibraryPlugin') {
variants = project.android.libraryVariants
} else {
variants = project.android.applicationVariants
}

for (def v : variants) {
if (task.path.endsWith("test${v.name.capitalize()}UnitTest")) {
return v
}
}
return null
}

static Collection<File> getSources(variant) {
List<File> sources = []
for (def srcs : variant.sourceSets.java.srcDirs) {
sources.addAll(srcs)
}
return sources
}

private static final EXCLUDES = [
'android/databinding/**/*.class',
'**/android/databinding/*Binding.class',
'**/BR.*',
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class'
]

private static Collection<File> getDestinations(variant, Project project) {
def javaDestinations = getJavaDestinations(variant)
FileTree javaTree = project.fileTree(dir: javaDestinations, excludes: EXCLUDES)

FileTree destinationsTree
if (project.plugins.findPlugin('kotlin-android') != null) {
def kotlinDestinations = "${project.buildDir}/tmp/kotlin-classes/${variant.name}"
def kotlinTree = project.fileTree(dir: kotlinDestinations, excludes: EXCLUDES)
destinationsTree = javaTree + kotlinTree
} else {
destinationsTree = javaTree
}
return destinationsTree.files
}

private static getJavaDestinations(variant) {
if (variant.hasProperty('javaCompileProvider')) {
return variant.javaCompileProvider.get().destinationDir
}
return variant.javaCompile.destinationDir
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public void beforeExecute(TaskIdentity<?> taskIdentity) {
Map<String, Object> inputProperties = task.getInputs().getProperties();
BuildModuleLayout moduleLayout =
(BuildModuleLayout) inputProperties.get(CiVisibilityPluginExtension.MODULE_LAYOUT_PROPERTY);
if (moduleLayout.getSourceSets().isEmpty()
&& project.getExtensions().findByName("android") != null) {
moduleLayout = AndroidGradleUtils.getAndroidModuleLayout(project, task);
}

JavaAgent jacocoAgent = getJacocoAgent(task);

Path jvmExecutable = CiVisibilityPluginExtension.getEffectiveExecutable(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public String[] helperClassNames() {
packageName + ".CiVisibilityService",
packageName + ".JavaCompilerPluginArgumentsProvider",
packageName + ".TracerArgumentsProvider",
packageName + ".AndroidGradleUtils",
packageName + ".CiVisibilityGradleListener",
packageName + ".CiVisibilityPluginExtension",
packageName + ".CiVisibilityPlugin"
Expand Down

0 comments on commit 48a5921

Please sign in to comment.