Skip to content

Commit

Permalink
capture test dependencies without realizing the tasks (via #103)
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianGM authored Oct 20, 2022
1 parent 0e5f26e commit 3d50dd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -34,6 +33,7 @@ open class AllureAdapterBasePlugin : Plugin<Project> {
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 {
Expand Down Expand Up @@ -72,22 +72,5 @@ open class AllureAdapterBasePlugin : Plugin<Project> {
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 }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ open class AllureAdapterExtension @Inject constructor(
*/
val categoriesFile: Property<RegularFile> = objects.fileProperty().convention(defaultCategoriesFile(project))

private val allureResultsDir = project.layout.buildDirectory.dir("allure-results")

val frameworks = AdapterHandler(project.container {
objects.newInstance<AdapterConfig>(it, objects, this).also { adapter ->
AllureJavaAdapter.find(it)?.apply {
Expand Down Expand Up @@ -96,20 +98,23 @@ open class AllureAdapterExtension @Inject constructor(

fun gatherResultsFrom(tasks: TaskCollection<out Task>) {
project.apply<AllureAdapterBasePlugin>()
// 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<out Task>) {
// TODO: think of a better way to capture test dependencies without realizing the tasks
gatherResultsFrom(task.get())
project.apply<AllureAdapterBasePlugin>()
exposeArtifact(task)
task.configure {
internalGatherResultsFrom(this)
}
}

fun gatherResultsFrom(task: Task) {
project.apply<AllureAdapterBasePlugin>()
exposeArtifact(project.tasks.named(task.name))
internalGatherResultsFrom(task)
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 3d50dd0

Please sign in to comment.