[Gradle, K/N] Rework environment input on test tasks

Provide method `trackEnvironment` to track environment variables. Use only tracked environment variables as task input.
#KT-44059 Fixed
This commit is contained in:
Alexander Likhachev
2021-03-04 18:42:52 +03:00
parent c5a9f20a6f
commit 78ed758704
3 changed files with 62 additions and 10 deletions
@@ -623,11 +623,47 @@ class GeneralNativeIT : BaseGradleIT() {
assertFileExists(defaultOutputFile) assertFileExists(defaultOutputFile)
} }
checkTestsUpToDate(testsToExecute, testsToSkip)
// Check simulator process leaking. // Check simulator process leaking.
val bootedSimulatorsAfter = getBootedSimulators(projectDir) val bootedSimulatorsAfter = getBootedSimulators(projectDir)
assertEquals(bootedSimulatorsBefore, bootedSimulatorsAfter) assertEquals(bootedSimulatorsBefore, bootedSimulatorsAfter)
// Check the case with failed tests. // Check the case with failed tests.
checkFailedTests(hostTestTask, testsToExecute, testsToSkip)
build("linkAnotherDebugTestHost") {
assertSuccessful()
assertFileExists(anotherOutputFile)
}
}
private fun Project.checkTestsUpToDate(testsToExecute: List<String>, testsToSkip: List<String>) {
// Check that test tasks are up-to-date on second run
build("check") {
assertSuccessful()
assertTasksUpToDate(*testsToExecute.toTypedArray())
assertTasksSkipped(*testsToSkip.toTypedArray())
}
// Check that setting new value to tracked environment variable triggers tests rerun
build("check", options = defaultBuildOptions().copy(androidHome = projectDir)) {
assertSuccessful()
assertTasksExecuted(*testsToExecute.toTypedArray())
assertTasksSkipped(*testsToSkip.toTypedArray())
}
build("check", options = defaultBuildOptions().copy(androidHome = projectDir)) {
assertSuccessful()
assertTasksUpToDate(*testsToExecute.toTypedArray())
assertTasksSkipped(*testsToSkip.toTypedArray())
}
}
private fun Project.checkFailedTests(hostTestTask: String, testsToExecute: List<String>, testsToSkip: List<String>) {
projectDir.resolve("src/commonTest/kotlin/test.kt").appendText( projectDir.resolve("src/commonTest/kotlin/test.kt").appendText(
""" """
@Test @Test
@@ -698,8 +734,8 @@ class GeneralNativeIT : BaseGradleIT() {
assertTestResults(expectedTestResults.first(), hostTestTask) assertTestResults(expectedTestResults.first(), hostTestTask)
// K/N doesn't report line numbers correctly on Linux (see KT-35408). // K/N doesn't report line numbers correctly on Linux (see KT-35408).
// TODO: Uncomment when this is fixed. // TODO: Uncomment when this is fixed.
//assertStacktrace(hostTestTask) //assertStacktrace(hostTestTask, "host")
if (hostIsMac) { if (HostManager.hostIsMac) {
assertTestResultsAnyOf( assertTestResultsAnyOf(
expectedTestResults[0], expectedTestResults[0],
expectedTestResults[1], expectedTestResults[1],
@@ -708,11 +744,6 @@ class GeneralNativeIT : BaseGradleIT() {
assertStacktrace("iosTest") assertStacktrace("iosTest")
} }
} }
build("linkAnotherDebugTestHost") {
assertSuccessful()
assertFileExists(anotherOutputFile)
}
} }
@Test @Test
@@ -42,6 +42,8 @@ kotlin {
// Check that test events are correctly reported in CLI. // Check that test events are correctly reported in CLI.
configure([hostTest, iosTest]){ configure([hostTest, iosTest]){
trackEnvironment("ANDROID_HOME")
testLogging { testLogging {
events "passed", "skipped", "failed" events "passed", "skipped", "failed"
} }
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.isAtLeast
import org.jetbrains.kotlin.gradle.targets.native.internal.parseKotlinNativeStackTraceAsJvm import org.jetbrains.kotlin.gradle.targets.native.internal.parseKotlinNativeStackTraceAsJvm
import org.jetbrains.kotlin.gradle.tasks.KotlinTest import org.jetbrains.kotlin.gradle.tasks.KotlinTest
import org.jetbrains.kotlin.gradle.utils.isConfigurationCacheAvailable import org.jetbrains.kotlin.gradle.utils.isConfigurationCacheAvailable
import org.jetbrains.kotlin.konan.CompilerVersion
import java.io.File import java.io.File
import java.util.concurrent.Callable import java.util.concurrent.Callable
@@ -65,13 +64,20 @@ abstract class KotlinNativeTest : KotlinTest() {
processOptions.workingDir = File(value) processOptions.workingDir = File(value)
} }
@get:Input @get:Internal
var environment: Map<String, Any> var environment: Map<String, Any>
get() = processOptions.environment get() = processOptions.environment
set(value) { set(value) {
processOptions.environment = value processOptions.environment = value
} }
private val trackedEnvironmentVariablesKeys = mutableSetOf<String>()
@Suppress("unused")
@get:Input
val trackedEnvironment
get() = environment.filterKeys(trackedEnvironmentVariablesKeys::contains)
private fun <T> Property<T>.set(providerLambda: () -> T) = set(project.provider { providerLambda() }) private fun <T> Property<T>.set(providerLambda: () -> T) = set(project.provider { providerLambda() })
fun executable(file: File) { fun executable(file: File) {
@@ -98,8 +104,21 @@ abstract class KotlinNativeTest : KotlinTest() {
executableProperty.set(project.provider(provider).map { project.files(it) }) executableProperty.set(project.provider(provider).map { project.files(it) })
} }
fun environment(name: String, value: Any) { @JvmOverloads
fun environment(name: String, value: Any, tracked: Boolean = true) {
processOptions.environment(name, value) processOptions.environment(name, value)
if (tracked) {
trackedEnvironmentVariablesKeys.add(name)
}
}
@JvmOverloads
fun trackEnvironment(name: String, tracked: Boolean = true) {
if (tracked) {
trackedEnvironmentVariablesKeys.add(name)
} else {
trackedEnvironmentVariablesKeys.remove(name)
}
} }
@get:Internal @get:Internal