diff --git a/plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt b/plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt index 6b2b972..4b72754 100644 --- a/plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt +++ b/plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt @@ -53,7 +53,7 @@ class RootCoveragePlugin : Plugin { } - private fun createSubProjectCoverageTask(subProject: Project): Task { + private fun createSubProjectCoverageTask(subProject: Project) { val task = subProject.createJacocoReportTask( taskName = "coverageReport", taskGroup = "reporting", @@ -62,7 +62,6 @@ class RootCoveragePlugin : Plugin { ) // subProject.assertAndroidCodeCoverageVariantExists() task.addSubProject(task.project) - return task } private fun createCoverageTaskForRoot(project: Project) { @@ -84,8 +83,7 @@ class RootCoveragePlugin : Plugin { } rootCoverageTask.addSubProject(it) - val subProjectCoverageTask = createSubProjectCoverageTask(it) - rootCoverageTask.dependsOn(subProjectCoverageTask) + createSubProjectCoverageTask(it) } project.tasks.create("rootCodeCoverageReport").apply { @@ -161,12 +159,7 @@ class RootCoveragePlugin : Plugin { // // 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) diff --git a/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt b/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt index c2b4042..3d623ef 100644 --- a/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt +++ b/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt @@ -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 @@ -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() @@ -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") @@ -196,6 +210,17 @@ class IntegrationTest( } } + private fun executeGradleTasks(tasks: List): 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) diff --git a/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/util/GradleBuildExtensions.kt b/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/util/GradleBuildExtensions.kt index 6ba4270..0dcc9a8 100644 --- a/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/util/GradleBuildExtensions.kt +++ b/plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/util/GradleBuildExtensions.kt @@ -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() } \ No newline at end of file