Skip to content

Commit

Permalink
Remove: Direct dependency between rootCoverageReport and coverageRepo…
Browse files Browse the repository at this point in the history
…rt tasks

These tasks should not depend on each other as they do not depend on each others output, if some direct dependency is needed, it will be on the test tasks.
  • Loading branch information
Rolf-Smit committed Sep 2, 2024
1 parent f8f23fd commit 0adaaed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RootCoveragePlugin : Plugin<Project> {
}


private fun createSubProjectCoverageTask(subProject: Project): Task {
private fun createSubProjectCoverageTask(subProject: Project) {
val task = subProject.createJacocoReportTask(
taskName = "coverageReport",
taskGroup = "reporting",
Expand All @@ -62,7 +62,6 @@ class RootCoveragePlugin : Plugin<Project> {
)
// subProject.assertAndroidCodeCoverageVariantExists()
task.addSubProject(task.project)
return task
}

private fun createCoverageTaskForRoot(project: Project) {
Expand All @@ -84,8 +83,7 @@ class RootCoveragePlugin : Plugin<Project> {
}
rootCoverageTask.addSubProject(it)

val subProjectCoverageTask = createSubProjectCoverageTask(it)
rootCoverageTask.dependsOn(subProjectCoverageTask)
createSubProjectCoverageTask(it)
}

project.tasks.create("rootCodeCoverageReport").apply {
Expand Down Expand Up @@ -161,12 +159,7 @@ class RootCoveragePlugin : Plugin<Project> {
//
// In theory we don't need to do this if `rootProjectExtension.includeAndroidTestResults` is false, so we could check that, but
// it also does not hurt.
subProject.tasks.configureEach { task ->
if (task.path == androidTestTask.taskPath) {
dependsOn(task.path)
}
}
mustRunAfter("$path:connected${variantName}AndroidTest")
mustRunAfter(androidTestTask.taskPath)
}

sourceDirectories.from(variant.sources.java?.all)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.runners.Parameterized
import org.neotech.plugin.rootcoverage.util.SimpleTemplate
import org.neotech.plugin.rootcoverage.util.SystemOutputWriter
import org.neotech.plugin.rootcoverage.util.assertSuccessful
import org.neotech.plugin.rootcoverage.util.assertTaskNotExecuted
import org.neotech.plugin.rootcoverage.util.assertTaskSuccess
import org.neotech.plugin.rootcoverage.util.createGradlePropertiesFile
import org.neotech.plugin.rootcoverage.util.createLocalPropertiesFile
Expand Down Expand Up @@ -86,33 +87,36 @@ class IntegrationTest(
})
})

val executeAndroidTests = configuration.pluginConfiguration.getPropertyValue("executeAndroidTests", "true").toBoolean()

// Note: rootCodeCoverageReport is the old and deprecated name of the rootCoverageReport task, it is
// used to check whether the old name properly aliases to the new task name.
val gradleCommands = if (configuration.pluginConfiguration.getPropertyValue("executeAndroidTests") == "false") {
val gradleCommands = if (!executeAndroidTests) {
val runOnGradleManagedDevices = configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices") ?: "false"

// Execute Android tests completely separately (as if run on some external service,
// after which the resulting files have been imported)
if (runOnGradleManagedDevices == "false") {
listOf("clean", "connectedDebugAndroidTest", "coverageReport", "rootCodeCoverageReport", "--stacktrace")
executeGradleTasks(listOf("clean", "connectedDebugAndroidTest"))
} else {
listOf("clean", "nexusoneapi30DebugAndroidTest", "coverageReport", "rootCodeCoverageReport", "--stacktrace")
executeGradleTasks(listOf("clean", "nexusoneapi30DebugAndroidTest"))
}

listOf("coverageReport", "rootCodeCoverageReport", "--stacktrace")
} else {
listOf("clean", "coverageReport", "rootCodeCoverageReport", "--stacktrace")
}

val runner = GradleRunner.create()
.withProjectDir(projectRoot)
.withGradleVersion(gradleVersion)
.withPluginClasspath()
.forwardStdOutput(SystemOutputWriter.out())
.forwardStdError(SystemOutputWriter.err())
.withArguments(gradleCommands)

val result = runner.build()
val result = executeGradleTasks(gradleCommands)

result.assertSuccessful()

// Assert whether the correct Android Test tasks are executed
result.assertCorrectAndroidTestTasksAreExecuted()
if(executeAndroidTests) {
result.assertCorrectAndroidTestTasksAreExecuted()
} else {
result.assertCorrectAndroidTestTasksAreNotExecuted()
}

// Assert whether the combined coverage report is what we expected
result.assertRootCoverageReport()
Expand All @@ -126,18 +130,28 @@ class IntegrationTest(

private fun BuildResult.assertCorrectAndroidTestTasksAreExecuted() {
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", "false").toBoolean()) {
// Assert that the tests have been run on Gradle Managed Devices
val device = configuration.pluginConfiguration.getPropertyValue("gradleManagedDeviceName", "allDevices")
assertTaskSuccess(":app:${device}DebugAndroidTest")
assertTaskSuccess(":library_android:${device}DebugAndroidTest")

} else {
// Assert that the tests have been run on connected devices
assertTaskSuccess(":app:connectedDebugAndroidTest")
assertTaskSuccess(":library_android:connectedDebugAndroidTest")
}
}

private fun BuildResult.assertCorrectAndroidTestTasksAreNotExecuted() {
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", "false").toBoolean()) {
val device = configuration.pluginConfiguration.getPropertyValue("gradleManagedDeviceName", "allDevices")
assertTaskNotExecuted(":app:${device}DebugAndroidTest")
assertTaskNotExecuted(":library_android:${device}DebugAndroidTest")

} else {
assertTaskNotExecuted(":app:connectedDebugAndroidTest")
assertTaskNotExecuted(":library_android:connectedDebugAndroidTest")
}
}

private fun BuildResult.assertRootCoverageReport() {
assertTaskSuccess(":rootCoverageReport")

Expand Down Expand Up @@ -196,6 +210,17 @@ class IntegrationTest(
}
}

private fun executeGradleTasks(tasks: List<String>): BuildResult {
return GradleRunner.create()
.withProjectDir(projectRoot)
.withGradleVersion(gradleVersion)
.withPluginClasspath()
.forwardStdOutput(SystemOutputWriter.out())
.forwardStdError(SystemOutputWriter.err())
.withArguments(tasks)
.build()
}

companion object {

@Suppress("unused") // This method is used by the JVM (Parameterized JUnit Runner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ fun BuildResult.assertSuccessful() {

fun BuildResult.assertTaskSuccess(taskPath: String) {
assertThat(task(taskPath)!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

fun BuildResult.assertTaskNotExecuted(taskPath: String) {
assertThat(task(taskPath)).isNull()
}

0 comments on commit 0adaaed

Please sign in to comment.