From 3d50dd0168e25c270f4ae7bd43cb905874175029 Mon Sep 17 00:00:00 2001 From: Cristian Garcia Date: Thu, 20 Oct 2022 08:55:47 +0200 Subject: [PATCH] capture test dependencies without realizing the tasks (via #103) --- .../gradle/adapter/AllureAdapterBasePlugin.kt | 19 +----------- .../gradle/adapter/AllureAdapterExtension.kt | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterBasePlugin.kt b/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterBasePlugin.kt index 4a8ac1d..26dd7f0 100644 --- a/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterBasePlugin.kt +++ b/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterBasePlugin.kt @@ -8,7 +8,6 @@ import io.qameta.allure.gradle.adapter.tasks.CopyCategories import io.qameta.allure.gradle.util.categoryDocumentation import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.Configuration import org.gradle.api.attributes.Usage import org.gradle.kotlin.dsl.* @@ -34,6 +33,7 @@ open class AllureAdapterBasePlugin : Plugin { val rawResultElements = configurations.create(ALLURE_RAW_RESULT_ELEMENTS_CONFIGURATION_NAME) { description = "The configuration exposes Allure raw results (simple-result.json, executor.json) for reporting" + isVisible = false isCanBeConsumed = true isCanBeResolved = false attributes { @@ -72,22 +72,5 @@ open class AllureAdapterBasePlugin : Plugin { copyCategoriesElements.outgoing.artifact(copyCategories.flatMap { it.markerFile }) { builtBy(copyCategories) } - - // Workaround for https://github.com/gradle/gradle/issues/6875 - target.afterEvaluate { - configurations.findByName("archives")?.let { archives -> - removeArtifactsFromArchives(archives, rawResultElements) - removeArtifactsFromArchives(archives, copyCategoriesElements) - } - } - } - - private fun Project.removeArtifactsFromArchives(archives: Configuration, elements: Configuration) { - val allureResultNames = elements.outgoing.artifacts.mapTo(mutableSetOf()) { it.name } - if (allureResultNames.isEmpty()) { - return - } - logger.debug("Will remove artifacts $allureResultNames (outgoing artifacts of $elements) from $archives configuration to workaround https://github.com/gradle/gradle/issues/6875") - archives.outgoing.artifacts.removeIf { it.name in allureResultNames } } } diff --git a/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterExtension.kt b/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterExtension.kt index 83d99d8..ef49a41 100644 --- a/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterExtension.kt +++ b/allure-adapter-plugin/src/main/kotlin/io/qameta/allure/gradle/adapter/AllureAdapterExtension.kt @@ -66,6 +66,8 @@ open class AllureAdapterExtension @Inject constructor( */ val categoriesFile: Property = objects.fileProperty().convention(defaultCategoriesFile(project)) + private val allureResultsDir = project.layout.buildDirectory.dir("allure-results") + val frameworks = AdapterHandler(project.container { objects.newInstance(it, objects, this).also { adapter -> AllureJavaAdapter.find(it)?.apply { @@ -96,20 +98,23 @@ open class AllureAdapterExtension @Inject constructor( fun gatherResultsFrom(tasks: TaskCollection) { project.apply() - // This causes test task realization early :-( - // TODO: think of a better way to capture test dependencies without realizing the tasks - tasks.all { + tasks.names.onEach { exposeArtifact(tasks.named(it)) } + tasks.configureEach { internalGatherResultsFrom(this) } } fun gatherResultsFrom(task: TaskProvider) { - // TODO: think of a better way to capture test dependencies without realizing the tasks - gatherResultsFrom(task.get()) + project.apply() + exposeArtifact(task) + task.configure { + internalGatherResultsFrom(this) + } } fun gatherResultsFrom(task: Task) { project.apply() + exposeArtifact(project.tasks.named(task.name)) internalGatherResultsFrom(task) } @@ -118,7 +123,7 @@ open class AllureAdapterExtension @Inject constructor( task.run { // Each task should store results in its own folder // End user should not depend on the folder name, so we do not expose it - val rawResults = project.layout.buildDirectory.dir("allure-results").get().asFile + val rawResults = allureResultsDir.get().asFile // See https://github.com/allure-framework/allure2/issues/1236 // We exclude categories.json since report task would copy categories right to the folder // of the current task @@ -150,13 +155,15 @@ open class AllureAdapterExtension @Inject constructor( // TODO: remove dependence on project at the execution time for compatibility with configuration cache generateExecutorInfo(rawResults, project, task.name) } + } + } - // Expose the gathered raw results - val allureResults = - project.configurations[AllureAdapterBasePlugin.ALLURE_RAW_RESULT_ELEMENTS_CONFIGURATION_NAME] - allureResults.outgoing.artifact(rawResults) { - builtBy(task) - } + private fun exposeArtifact(task: TaskProvider<*>) { + // Expose the gathered raw results + val allureResults = + project.configurations[AllureAdapterBasePlugin.ALLURE_RAW_RESULT_ELEMENTS_CONFIGURATION_NAME] + allureResults.outgoing.artifact(allureResultsDir) { + builtBy(task) } }