[Native][tests] Allow overriding test properties via Gradle properties

This commit is contained in:
Dmitriy Dolovov
2021-11-13 16:50:55 +03:00
parent a029b50755
commit 61b8fb8fc2
3 changed files with 38 additions and 23 deletions
+24 -11
View File
@@ -39,14 +39,24 @@ if (kotlinBuildProperties.isInJpsBuildIdeaSync) {
} }
} }
if (kotlinBuildProperties.isKotlinNativeEnabled) { enum class TestProperty(shortName: String) {
val kotlinNativeHome = project(":kotlin-native").projectDir.resolve("dist") // Use a separate Gradle property to pass Kotlin/Native home to tests: "kotlin.internal.native.test.nativeHome".
// Don't use "kotlin.native.home" and similar properties for this purpose, as these properties may have undesired
// effect on other Gradle tasks (ex: :kotlin-native:dist) that might be executed along with test task.
KOTLIN_NATIVE_HOME("nativeHome"),
COMPILER_CLASSPATH("compilerClasspath"),
TEST_MODE("mode"),
USE_CACHE("useCache");
val kotlinNativeCompilerClassPath: Configuration by configurations.creating private val propertyName = "kotlin.internal.native.test.$shortName"
dependencies {
kotlinNativeCompilerClassPath(project(":kotlin-native-compiler-embeddable")) fun setUpFromGradleProperty(task: Test, defaultValue: () -> Any? = { null }) {
val propertyValue = task.project.findProperty(propertyName) ?: defaultValue()
if (propertyValue != null) task.systemProperty(propertyName, propertyValue)
} }
}
if (kotlinBuildProperties.isKotlinNativeEnabled) {
projectTest(taskName = "test", jUnitMode = JUnitMode.JUnit5) { projectTest(taskName = "test", jUnitMode = JUnitMode.JUnit5) {
dependsOn(":kotlin-native:dist" /*, ":kotlin-native:distPlatformLibs"*/) dependsOn(":kotlin-native:dist" /*, ":kotlin-native:distPlatformLibs"*/)
workingDir = rootDir workingDir = rootDir
@@ -60,14 +70,17 @@ if (kotlinBuildProperties.isKotlinNativeEnabled) {
// additional stack frames more compared to the old one because of another launcher, etc. and it turns out this is not enough. // additional stack frames more compared to the old one because of another launcher, etc. and it turns out this is not enough.
jvmArgs("-Xss2m") jvmArgs("-Xss2m")
systemProperty("kotlin.native.home", kotlinNativeHome.absolutePath) TestProperty.KOTLIN_NATIVE_HOME.setUpFromGradleProperty(this) {
systemProperty("kotlin.internal.native.classpath", kotlinNativeCompilerClassPath.files.joinToString(";")) project(":kotlin-native").projectDir.resolve("dist").absolutePath
}
TestProperty.COMPILER_CLASSPATH.setUpFromGradleProperty(this) {
configurations.detachedConfiguration(dependencies.project(":kotlin-native-compiler-embeddable")).files.joinToString(";")
}
// Pass Gradle properties as JVM properties so test process can read them. // Pass Gradle properties as JVM properties so test process can read them.
listOf( TestProperty.TEST_MODE.setUpFromGradleProperty(this)
"kotlin.internal.native.test.mode", TestProperty.USE_CACHE.setUpFromGradleProperty(this)
"kotlin.internal.native.test.useCache"
).forEach { propertyName -> findProperty(propertyName)?.let { systemProperty(propertyName, it) } }
useJUnitPlatform() useJUnitPlatform()
} }
@@ -27,8 +27,11 @@ class NativeBlackBoxTestSupport : BeforeEachCallback {
* Also, [TestInstancePostProcessor.postProcessTestInstance] allows accessing only the currently created test instance and does * Also, [TestInstancePostProcessor.postProcessTestInstance] allows accessing only the currently created test instance and does
* not allow accessing its parent test instance in case there are inner test classes in the generated test suite. * not allow accessing its parent test instance in case there are inner test classes in the generated test suite.
*/ */
override fun beforeEach(extensionContext: ExtensionContext) = with(extensionContext) { override fun beforeEach(extensionContext: ExtensionContext): Unit = with(extensionContext) {
enclosingTestInstance.testRunProvider = getOrCreateTestRunProvider() enclosingTestInstance.testRunProvider = getOrCreateTestRunProvider()
// Set the essential compiler property.
System.setProperty("kotlin.native.home", getOrCreateGlobalEnvironment().kotlinNativeHome.path)
} }
companion object { companion object {
@@ -38,22 +38,22 @@ internal class GlobalTestEnvironment(
// Use isolated cached class loader. // Use isolated cached class loader.
private val defaultKotlinNativeClassLoader: Lazy<ClassLoader> = lazy { private val defaultKotlinNativeClassLoader: Lazy<ClassLoader> = lazy {
val nativeClassPath = System.getProperty(KOTLIN_NATIVE_CLASSPATH) val nativeClassPath = System.getProperty(COMPILER_CLASSPATH)
?.split(':', ';') ?.split(':', ';')
?.map { File(it).toURI().toURL() } ?.map { File(it).toURI().toURL() }
?.toTypedArray() ?.toTypedArray()
?: fail { "Non-specified $KOTLIN_NATIVE_CLASSPATH system property" } ?: fail { "Non-specified $COMPILER_CLASSPATH system property" }
URLClassLoader(nativeClassPath, /* no parent class loader */ null).apply { setDefaultAssertionStatus(true) } URLClassLoader(nativeClassPath, /* no parent class loader */ null).apply { setDefaultAssertionStatus(true) }
} }
private val defaultTestMode: TestMode = run { private val defaultTestMode: TestMode = run {
val testModeName = System.getProperty(KOTLIN_NATIVE_TEST_MODE) ?: return@run TestMode.WITH_MODULES val testModeName = System.getProperty(TEST_MODE) ?: return@run TestMode.WITH_MODULES
TestMode.values().firstOrNull { it.name == testModeName } ?: fail { TestMode.values().firstOrNull { it.name == testModeName } ?: fail {
buildString { buildString {
appendLine("Unknown test mode name $testModeName.") appendLine("Unknown test mode name $testModeName.")
appendLine("One of the following test modes should be passed through $KOTLIN_NATIVE_TEST_MODE system property:") appendLine("One of the following test modes should be passed through $TEST_MODE system property:")
TestMode.values().forEach { testMode -> TestMode.values().forEach { testMode ->
appendLine("- ${testMode.name}: ${testMode.description}") appendLine("- ${testMode.name}: ${testMode.description}")
} }
@@ -62,10 +62,9 @@ internal class GlobalTestEnvironment(
} }
private val defaultCacheSettings: TestCacheSettings = run { private val defaultCacheSettings: TestCacheSettings = run {
val useCacheValue = System.getProperty(KOTLIN_NATIVE_TEST_USE_CACHE) val useCacheValue = System.getProperty(USE_CACHE)
val useCache = if (useCacheValue != null) { val useCache = if (useCacheValue != null) {
useCacheValue.toBooleanStrictOrNull() useCacheValue.toBooleanStrictOrNull() ?: fail { "Invalid value for $USE_CACHE system property: $useCacheValue" }
?: fail { "Invalid value for $KOTLIN_NATIVE_TEST_USE_CACHE system property: $useCacheValue" }
} else } else
true true
@@ -75,10 +74,10 @@ internal class GlobalTestEnvironment(
private val projectBuildDir: File private val projectBuildDir: File
get() = System.getenv(PROJECT_BUILD_DIR)?.let(::File) ?: fail { "Non-specified $PROJECT_BUILD_DIR environment variable" } get() = System.getenv(PROJECT_BUILD_DIR)?.let(::File) ?: fail { "Non-specified $PROJECT_BUILD_DIR environment variable" }
private const val KOTLIN_NATIVE_HOME = "kotlin.native.home" private const val KOTLIN_NATIVE_HOME = "kotlin.internal.native.test.nativeHome"
private const val KOTLIN_NATIVE_CLASSPATH = "kotlin.internal.native.classpath" private const val COMPILER_CLASSPATH = "kotlin.internal.native.test.compilerClasspath"
private const val KOTLIN_NATIVE_TEST_MODE = "kotlin.internal.native.test.mode" private const val TEST_MODE = "kotlin.internal.native.test.mode"
private const val KOTLIN_NATIVE_TEST_USE_CACHE = "kotlin.internal.native.test.useCache" private const val USE_CACHE = "kotlin.internal.native.test.useCache"
private const val PROJECT_BUILD_DIR = "PROJECT_BUILD_DIR" private const val PROJECT_BUILD_DIR = "PROJECT_BUILD_DIR"
} }
} }