|
| 1 | + |
| 2 | +import buildlogic.IntegrationCoverageExt |
| 3 | +import org.gradle.api.attributes.* |
| 4 | +import org.gradle.api.model.ObjectFactory |
| 5 | +import org.gradle.api.tasks.testing.Test |
| 6 | +import org.gradle.kotlin.dsl.* |
| 7 | +import org.gradle.testing.jacoco.plugins.JacocoTaskExtension |
| 8 | +import org.gradle.testing.jacoco.tasks.JacocoReport |
| 9 | + |
| 10 | +private val FULL_REPORT_TASK_NAME = "jacocoFullCoverageReport" |
| 11 | +private val INCLUDED_BUILD_NAME = "core" |
| 12 | + |
| 13 | +val ext = extensions.create( |
| 14 | + "viaductIntegrationCoverage", |
| 15 | + IntegrationCoverageExt::class, |
| 16 | + project, |
| 17 | + FULL_REPORT_TASK_NAME, |
| 18 | + objects, |
| 19 | +) |
| 20 | + |
| 21 | +// Configurations we will resolve from the base project's unit-test exec data |
| 22 | +val incomingUnitExec by configurations.creating { |
| 23 | + description = "JaCoCo exec data from the base module's unit tests (included build)" |
| 24 | + isCanBeConsumed = false |
| 25 | + isCanBeResolved = true |
| 26 | + attributes { |
| 27 | + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.VERIFICATION)) |
| 28 | + attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME)) |
| 29 | + attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL)) |
| 30 | + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named("jacoco-exec")) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Configuration we will resolve from the base project's compiled class files |
| 35 | +// This is a bit fragile: it's critical that _only_ the classes compiled from sources |
| 36 | +// are included -- and that they come in as class files, not a JAR file. Setting |
| 37 | +// isTransitive to false is important, as is the LibraryElements.CLASSES |
| 38 | +val baseRuntimeClasses by configurations.creating { |
| 39 | + description = "Runtime class directories of the base module (for coverage attribution)" |
| 40 | + isCanBeConsumed = false |
| 41 | + isCanBeResolved = true |
| 42 | + isTransitive = false |
| 43 | + attributes { |
| 44 | + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY)) |
| 45 | + attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME)) |
| 46 | + attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL)) |
| 47 | + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.CLASSES)) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Configuration we will resolve from the base project's source files (optional - for better reports) |
| 52 | +val baseRuntimeSources by configurations.creating { |
| 53 | + description = "Optional sources jars of the base module (for HTML report browsing)" |
| 54 | + isCanBeConsumed = false |
| 55 | + isCanBeResolved = true |
| 56 | + attributes { |
| 57 | + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION)) |
| 58 | + attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME)) |
| 59 | + attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// helper to register either integration-test reports |
| 64 | +fun registerCoverageTask(name: String) = |
| 65 | + tasks.register<JacocoReport>(name) { |
| 66 | + group = "verification" |
| 67 | + |
| 68 | + // Clear anything earlier plugins may have added |
| 69 | + // More fagility: we need to be careful because we're bringing in "exec" data |
| 70 | + // from an included build - don't want any surprises |
| 71 | + classDirectories.setFrom(emptyList<Any>()) |
| 72 | + additionalClassDirs.setFrom(files()) |
| 73 | + additionalSourceDirs.setFrom(files()) |
| 74 | + sourceDirectories.setFrom(emptyList<Any>()) |
| 75 | + |
| 76 | + // Classes whose coverage is being analyzed |
| 77 | + classDirectories.setFrom( |
| 78 | + configurations.named("baseRuntimeClasses").map { cfg -> |
| 79 | + // This is a FileCollection with proper builtBy; contains only directories when using classesElements |
| 80 | + cfg.incoming.artifactView { }.files |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + // sources: only if a sources jar exists, decided at execution time |
| 85 | + val baseSrcTrees = configurations.named("baseRuntimeSources").map { cfg -> |
| 86 | + cfg.incoming.artifactView { isLenient = true /* <= because optional */ }.files.map { zipTree(it) } |
| 87 | + } |
| 88 | + sourceDirectories.setFrom(baseSrcTrees) |
| 89 | + |
| 90 | + if (name == FULL_REPORT_TASK_NAME) { |
| 91 | + // add unit-test data to report |
| 92 | + executionData.from( |
| 93 | + configurations.named("incomingUnitExec").map { it.incoming.artifactView { }.files } |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + // add integration-test data to report |
| 98 | + val itExecsProvider = providers.provider { |
| 99 | + tasks.withType<Test>().mapNotNull { t -> |
| 100 | + t.extensions.findByType(JacocoTaskExtension::class.java)?.destinationFile |
| 101 | + } |
| 102 | + } |
| 103 | + executionData.from(itExecsProvider) |
| 104 | + |
| 105 | + reports { |
| 106 | + xml.required.set(true) |
| 107 | + html.required.set(true) |
| 108 | + csv.required.set(false) |
| 109 | + } |
| 110 | + |
| 111 | + // task dependencies |
| 112 | + dependsOn(tasks.withType<Test>()) // local ITs |
| 113 | + dependsOn(providers.provider { |
| 114 | + val inc = gradle.includedBuild(INCLUDED_BUILD_NAME) |
| 115 | + inc.task("${ext.baseProjectPath.get()}:test") |
| 116 | + }) |
| 117 | + |
| 118 | + doFirst { |
| 119 | + val list = executionData.files.toList() |
| 120 | + logger.info("Full coverage exec inputs (${list.size}):") |
| 121 | + list.forEach { f -> logger.info(" $f [${if (f.exists()) f.length() else -1} bytes]") } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +registerCoverageTask(FULL_REPORT_TASK_NAME) |
| 126 | +registerCoverageTask("jacocoIntegrationOnlyReport") |
0 commit comments