Improve framework test configuration: use DSL with Closure

This commit is contained in:
Pavel Punegov
2020-06-01 21:37:57 +03:00
committed by Pavel Punegov
parent 21862ef847
commit d2f8b3b142
3 changed files with 58 additions and 59 deletions
+35 -23
View File
@@ -4018,7 +4018,8 @@ standaloneTest("fake_override_0") {
Task frameworkTest(String name, Closure<FrameworkTest> configurator) { Task frameworkTest(String name, Closure<FrameworkTest> configurator) {
return KotlinNativeTestKt.createTest(project, name, FrameworkTest) { task -> return KotlinNativeTestKt.createTest(project, name, FrameworkTest) { task ->
task.configure(configurator) configurator.delegate = task
configurator()
if (task.enabled) { if (task.enabled) {
konanArtifacts { konanArtifacts {
task.frameworks.forEach { fr -> task.frameworks.forEach { fr ->
@@ -4026,7 +4027,7 @@ Task frameworkTest(String name, Closure<FrameworkTest> configurator) {
fr.sources.forEach { src -> fr.sources.forEach { src ->
srcFiles src srcFiles src
} }
baseDir "$testOutputFramework/${task.testName}" baseDir "$testOutputFramework/${task.name}"
if (fr.library != null) { if (fr.library != null) {
libraries { libraries {
@@ -4055,8 +4056,7 @@ Task frameworkTest(String name, Closure<FrameworkTest> configurator) {
if (isAppleTarget(project)) { if (isAppleTarget(project)) {
frameworkTest('testObjCExport') { frameworkTest('testObjCExport') {
final String frameworkName = 'Kt' final String frameworkName = 'Kt'
testName = "ObjCExport" final String dir = "$testOutputFramework/$name"
final String dir = "$testOutputFramework/$testName"
final File lazyHeader = file("$dir/$target-lazy.h") final File lazyHeader = file("$dir/$target-lazy.h")
doLast { doLast {
@@ -4095,8 +4095,11 @@ if (isAppleTarget(project)) {
extraOpts "-module-name", "org.jetbrains.kotlin.native.test-library" extraOpts "-module-name", "org.jetbrains.kotlin.native.test-library"
} }
} }
it.createFramework(frameworkName, ['objcexport'], false, framework(frameworkName) {
frameworkName, libraryName, ["-Xemit-lazy-objc-header=$lazyHeader"]) sources = ['objcexport']
library = libraryName
opts = ["-Xemit-lazy-objc-header=$lazyHeader"]
}
swiftSources = ['objcexport'] swiftSources = ['objcexport']
} }
@@ -4119,21 +4122,29 @@ if (isAppleTarget(project)) {
} }
} }
testName = "ObjCExportStatic"
codesign = false codesign = false
it.createFramework(frameworkName, ['objcexport'], false, frameworkArtifactName, libraryName, ['-Xstatic-framework']) framework(frameworkName) {
sources = ['objcexport']
bitcode = false
artifact = frameworkArtifactName
library = libraryName
opts = ['-Xstatic-framework']
}
swiftSources = ['objcexport'] swiftSources = ['objcexport']
} }
frameworkTest('testValuesGenericsFramework') { frameworkTest('testValuesGenericsFramework') {
testName = 'ValuesGenerics' framework('ValuesGenerics') {
it.createFramework('ValuesGenerics', ['objcexport/values.kt', 'framework/values_generics']) sources = ['objcexport/values.kt', 'framework/values_generics']
}
swiftSources = ['framework/values_generics/'] swiftSources = ['framework/values_generics/']
} }
frameworkTest("testStdlibFramework") { frameworkTest("testStdlibFramework") {
testName = 'Stdlib' framework('Stdlib') {
it.createFramework('Stdlib', ['framework/stdlib'], true) sources = ['framework/stdlib']
bitcode = true
}
if (cacheTesting == null) fullBitcode = true if (cacheTesting == null) fullBitcode = true
swiftSources = ['framework/stdlib/'] swiftSources = ['framework/stdlib/']
} }
@@ -4141,21 +4152,22 @@ if (isAppleTarget(project)) {
if (cacheTesting != null && cacheTesting.isDynamic) { if (cacheTesting != null && cacheTesting.isDynamic) {
// testMultipleFrameworks disabled until https://youtrack.jetbrains.com/issue/KT-34262 is fixed. // testMultipleFrameworks disabled until https://youtrack.jetbrains.com/issue/KT-34262 is fixed.
} else frameworkTest("testMultipleFrameworks") { } else frameworkTest("testMultipleFrameworks") {
testName = "MultipleFrameworks" framework('First') {
it.createFramework('First', sources = ['framework/multiple/framework1', 'framework/multiple/shared']
['framework/multiple/framework1', 'framework/multiple/shared'], bitcode = true
true // bitcode }
) framework('Second') {
it.createFramework('Second', sources = ['framework/multiple/framework2', 'framework/multiple/shared']
['framework/multiple/framework2', 'framework/multiple/shared'], bitcode = true
true // bitcode }
)
swiftSources = ['framework/multiple'] swiftSources = ['framework/multiple']
} }
frameworkTest("testGh3343Framework") { frameworkTest("testGh3343Framework") {
testName = 'Gh3343' framework('Gh3343') {
it.createFramework('Gh3343', ['framework/gh3343'], false, 'Gh3343', 'objcGh3343') sources = ['framework/gh3343']
library = 'objcGh3343'
}
swiftSources = ['framework/gh3343/'] swiftSources = ['framework/gh3343/']
} }
} }
@@ -503,7 +503,7 @@ fun KonanTestExecutable.configureXcodeBuild() {
// Copy each framework to the Frameworks dir. // Copy each framework to the Frameworks dir.
it += frameworks.map { framework -> it += frameworks.map { framework ->
val name = framework.artifact val name = framework.artifact
"cp -r \"$testOutput/$testName/${project.testTarget.name}/$name.framework\" " + "cp -r \"$testOutput/$name/${project.testTarget.name}/$name.framework\" " +
"\"\$TARGET_BUILD_DIR/\$FRAMEWORKS_FOLDER_PATH/$name.framework\"" "\"\$TARGET_BUILD_DIR/\$FRAMEWORKS_FOLDER_PATH/$name.framework\""
} }
} }
@@ -4,7 +4,6 @@ import groovy.lang.Closure
import org.gradle.api.Action import org.gradle.api.Action
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.Task import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree import org.gradle.api.file.FileTree
import org.gradle.api.tasks.Input import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.TaskAction
@@ -24,16 +23,12 @@ import java.nio.file.Paths
* according to a pattern "compileKonan${frameworkName}". * according to a pattern "compileKonan${frameworkName}".
* *
* @property swiftSources Swift-language test sources that use a given framework * @property swiftSources Swift-language test sources that use a given framework
* @property testName test name
* @property frameworks names of frameworks * @property frameworks names of frameworks
*/ */
open class FrameworkTest : DefaultTask(), KonanTestExecutable { open class FrameworkTest : DefaultTask(), KonanTestExecutable {
@Input @Input
lateinit var swiftSources: List<String> lateinit var swiftSources: List<String>
@Input
lateinit var testName: String
@Input @Input
lateinit var frameworks: MutableList<Framework> lateinit var frameworks: MutableList<Framework>
@@ -54,32 +49,29 @@ open class FrameworkTest : DefaultTask(), KonanTestExecutable {
* @param library library dependency name, * @param library library dependency name,
* @param opts additional options for the compiler. * @param opts additional options for the compiler.
*/ */
data class Framework( class Framework(
val name: String, val name: String,
val sources: List<String>, var sources: List<String> = emptyList(),
val bitcode: Boolean = false, var bitcode: Boolean = false,
val artifact: String = name, var artifact: String = name,
val library: String? = null, var library: String? = null,
val opts: List<String> = emptyList() var opts: List<String> = emptyList()
) )
@JvmOverloads fun framework(name: String, closure: Closure<Framework>): Framework {
fun createFramework(name: String, val f = Framework(name).apply {
sources: List<String>, closure.delegate = this
bitcode: Boolean = false, closure.resolveStrategy = Closure.DELEGATE_FIRST
artifact: String = name, closure.call()
library: String? = null, // map to file paths
opts: List<String> = emptyList() sources = sources.toFiles(Language.Kotlin).map { it.path }
) = Framework(
name,
sources.toFiles(Language.Kotlin).map { it.path },
bitcode, artifact, library, opts
).also {
if (!::frameworks.isInitialized) {
frameworks = mutableListOf(it)
} else {
frameworks.add(it)
} }
if (!::frameworks.isInitialized) {
frameworks = mutableListOf(f)
} else {
frameworks.add(f)
}
return f
} }
enum class Language(val extension: String) { enum class Language(val extension: String) {
@@ -96,10 +88,7 @@ open class FrameworkTest : DefaultTask(), KonanTestExecutable {
.flatMap { it.files } .flatMap { it.files }
override val executable: String override val executable: String
get() { get() = Paths.get(testOutput, name, "swiftTestExecutable").toString()
check(::testName.isInitialized) { "Test name should be set" }
return Paths.get(testOutput, testName, "swiftTestExecutable").toString()
}
override var doBeforeRun: Action<in Task>? = null override var doBeforeRun: Action<in Task>? = null
@@ -118,14 +107,12 @@ open class FrameworkTest : DefaultTask(), KonanTestExecutable {
group = LifecycleBasePlugin.VERIFICATION_GROUP group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Kotlin/Native test infrastructure task" description = "Kotlin/Native test infrastructure task"
check(::testName.isInitialized) { "Test name should be set" }
check(::frameworks.isInitialized) { "Frameworks should be set" } check(::frameworks.isInitialized) { "Frameworks should be set" }
return this return this
} }
private fun buildTestExecutable() { private fun buildTestExecutable() {
val frameworkParentDirPath = "$testOutput/$testName/${project.testTarget.name}" val frameworkParentDirPath = "$testOutput/$name/${project.testTarget.name}"
frameworks.forEach { framework -> frameworks.forEach { framework ->
val frameworkArtifact = framework.artifact val frameworkArtifact = framework.artifact
val frameworkPath = "$frameworkParentDirPath/$frameworkArtifact.framework" val frameworkPath = "$frameworkParentDirPath/$frameworkArtifact.framework"
@@ -135,7 +122,7 @@ open class FrameworkTest : DefaultTask(), KonanTestExecutable {
} }
// create a test provider and get main entry point // create a test provider and get main entry point
val provider = Paths.get(testOutput, testName, "provider.swift") val provider = Paths.get(testOutput, name, "provider.swift")
FileWriter(provider.toFile()).use { writer -> FileWriter(provider.toFile()).use { writer ->
val providers = swiftSources.toFiles(Language.Swift) val providers = swiftSources.toFiles(Language.Swift)
.map { it.name.toString().removeSuffix(".swift").capitalize() } .map { it.name.toString().removeSuffix(".swift").capitalize() }