Skip to content

Commit

Permalink
fix allureReport, allureServe --depends-on-tests (fixes #73, via #74)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi authored Oct 1, 2021
1 parent 135345d commit 1d1b058
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ open class AllureAdapterExtension @Inject constructor(

fun gatherResultsFrom(tasks: TaskCollection<out Task>) {
project.apply<AllureAdapterBasePlugin>()
tasks.configureEach {
// This causes test task realization early :-(
// TODO: think of a better way to capture test dependencies without realizing the tasks
tasks.all {
internalGatherResultsFrom(this)
}
}

fun gatherResultsFrom(task: TaskProvider<out Task>) {
project.apply<AllureAdapterBasePlugin>()
task {
internalGatherResultsFrom(this)
}
// TODO: think of a better way to capture test dependencies without realizing the tasks
gatherResultsFrom(task.get())
}

fun gatherResultsFrom(task: Task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ abstract class AllureExecTask constructor(objects: ObjectFactory) : DefaultTask(
protected val inputFiles = project.files(resultsDirs.map { dirs -> dirs.map { project.fileTree(it) } })

init {
dependsOn(dependsOnTests.map { if (it) emptyList<Any>() else resultsDirs })
dependsOn(dependsOnTests.map { if (it) resultsDirs else emptyList<Any>() })
// In any case, if user launches "./gradlew test allureReport" the report generation
// should wait for test execution
mustRunAfter(resultsDirs)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.qameta.allure.gradle.report

import io.qameta.allure.gradle.rule.GradleRunnerRule
import org.assertj.core.api.Assertions
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
class TestAndAllureReportTest {
@Rule
@JvmField
val gradleRunner = GradleRunnerRule()
.version { version }
.project { project }
.tasks {
if (dependsOnTests) {
arrayOf("allureReport", "--depends-on-tests")
} else {
arrayOf("allureReport")
}
}

@Parameterized.Parameter(0)
lateinit var version: String

@Parameterized.Parameter(1)
lateinit var project: String

@JvmField
@Parameterized.Parameter(2)
var dependsOnTests: Boolean = false

companion object {
@JvmStatic
@Parameterized.Parameters(name = "{1}, [{0}]")
fun getFrameworks(): Array<Any> {
val res = mutableListOf<Array<Any>>()
for (gradleVersion in listOf("7.2", "7.0", "6.8.3")) {
for (project in listOf("src/it/junit5-5.8.1")) {
for (dependOnTests in listOf(true, false)) {
res.add(arrayOf(gradleVersion, project, dependOnTests))
}
}
}
return res.toTypedArray()
}
}

@Test
fun `check allureReport outcome`() {
if (dependsOnTests) {
Assertions.assertThat(gradleRunner.buildResult.tasks)
.`as`("allureReport --depends-on-tests should trigger test execution")
.filteredOn { task -> task.path == ":test" }
.extracting("outcome")
.containsExactly(TaskOutcome.SUCCESS)

Assertions.assertThat(gradleRunner.buildResult.tasks)
.`as`("allureReport --depends-on-tests should result in SUCCESS for :allureReport")
.filteredOn { task -> task.path == ":allureReport" }
.extracting("outcome")
.containsExactly(TaskOutcome.SUCCESS)
} else {
Assertions.assertThat(gradleRunner.buildResult.tasks)
.`as`("allureReport without --depends-on-tests should not trigger test execution")
.filteredOn { task -> task.path == ":test" }
.isEmpty()

Assertions.assertThat(gradleRunner.buildResult.tasks)
.`as`("allureReport without --depends-on-tests should result in NO-SOURCE for :allureReport")
.filteredOn { task -> task.path == ":allureReport" }
.extracting("outcome")
.containsExactly(TaskOutcome.NO_SOURCE)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import io.qameta.allure.gradle.base.AllureExtension
import io.qameta.allure.gradle.base.tasks.AllureExecTask
import io.qameta.allure.gradle.util.conv
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.*
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.the
import report
import java.io.File
import javax.inject.Inject

open class AllureReport @Inject constructor(objects: ObjectFactory) : AllureExecTask(objects) {
@OutputDirectory
@Option(option = "report-dir", description = "The directory to generate Allure report into")
val reportDir = objects.directoryProperty().conv(
project.the<AllureExtension>().report.reportDir.map { it.dir(this@AllureReport.name) }
)

@Option(option = "report-dir", description = "The directory to generate Allure report into")
fun setReportDir(directory: String) {
reportDir.set(project.layout.dir(project.provider { File(directory) }))
}

@Input
@Option(option = "clean", description = "Clean Allure report directory before generating a new one")
val clean = objects.property<Boolean>().conv(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.qameta.allure.gradle.report.tasks
import io.qameta.allure.gradle.base.tasks.AllureExecTask
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.property
Expand All @@ -24,9 +23,13 @@ open class AllureServe @Inject constructor(objects: ObjectFactory) : AllureExecT
val host = objects.property<String>()

@Internal
@Option(option = "port", description = "This port will be used to start web server for the report")
val port = objects.property<Int>()

@Option(option = "port", description = "This port will be used to start web server for the report")
fun setPort(port: String) {
this.port.set(port.toInt())
}

@TaskAction
fun serveAllureReport() {
val rawResults = rawResults.map { it.absolutePath }
Expand Down

0 comments on commit 1d1b058

Please sign in to comment.