From 8e703a7189022d36658e6c234b1773e63e43171a Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 29 Aug 2018 21:16:56 +0700 Subject: [PATCH] [gradle-plugin] Add test for cinterop --- .../test/kotlin/ExperimentalPluginTests.kt | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt b/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt index e692b1c7385..45ff28feeb8 100644 --- a/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt +++ b/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt @@ -553,4 +553,178 @@ class ExperimentalPluginTests { headerPaths.forEach { assertFileExists(it) } } } + + @Test + fun `Plugin should support cinterop dependencies`() { + // region Project set up. + val rootProject = KonanProject.createEmpty(projectDirectory).apply { + buildFile.writeText(""" + plugins { + id 'kotlin-native' + } + + group 'test' + version '1.0' + + repositories { + maven { url = 'repo' } + } + + sourceSets.main.component { + outputKinds = [ KLIBRARY ] + targets = ['host'] + } + + dependencies { + implementation project('libFoo') + } + """.trimIndent()) + + settingsFile.writeText(""" + enableFeaturePreview('GRADLE_METADATA') + + rootProject.name = 'interop-test' + + include 'libFoo' + include 'libBar' + """.trimIndent()) + + generateSrcFile("main.kt", """ + import kotlinx.cinterop.* + import mystdio.* + + fun main(args: Array) { + printf(foo()) + } + """.trimIndent()) + + generateSrcFile(listOf("src", "test", "kotlin"), "test.kt", """ + import kotlin.test.* + + @Test + fun mainTest() { + main(emptyArray()) + } + """.trimIndent()) + } + + val libFooProject = KonanProject.createEmpty(rootProject.createSubDir("libFoo")).apply { + buildFile.writeText(""" + plugins { + id 'kotlin-native' + id 'maven-publish' + } + + group 'test' + version '1.0' + + sourceSets.main { + + component { + targets = ['host'] + outputKinds = [ KLIBRARY ] + } + + dependencies { + implementation project(':libBar') + cinterop('mystdio') { + extraOpts '-nodefaultlibs' + } + } + } + + publishing { + repositories { + maven { + url = '../repo' + } + } + } + """.trimIndent()) + + generateSrcFile("lib.kt", """ + fun foo() = "Interop is here!\n${'$'}{bar()}\n" + """.trimIndent()) + + generateDefFile("mystdio.def", """ + headers = stdio.h + compilerOpts.osx = -D_ANSI_SOURCE + """.trimIndent()) + } + + val libBarProject = KonanProject.createEmpty(rootProject.createSubDir("libBar")).apply { + buildFile.writeText(""" + plugins { + id 'kotlin-native' + id 'maven-publish' + } + + group 'test' + version '1.0' + + sourceSets.main { + component { + targets = ['host'] + outputKinds = [ KLIBRARY ] + } + } + + publishing { + repositories { + maven { + url = '../repo' + } + } + } + """.trimIndent()) + + generateSrcFile("lib.kt", """ + fun bar() = "Transitive call!" + """.trimIndent()) + } + // endregion. + + rootProject.createRunner().withArguments(":build").build().apply { + output.contains("Interop is here!") + output.contains("Transitive call!") + } + + assertFileExists("libFoo/build/cinterop/mystdio/${HostManager.hostName}/mystdio.klib") + + rootProject.createRunner().withArguments(":libFoo:publish", ":libBar:publish").build() + assertFileExists("repo/test/libFoo_debug/1.0/libFoo_debug-1.0-interop-mystdio.klib") + assertFileExists("repo/test/libFoo_release/1.0/libFoo_release-1.0-interop-mystdio.klib") + + // A dependency on a published library + rootProject.buildFile.writeText(""" + plugins { + id 'kotlin-native' + } + + group 'test' + version '1.0' + + repositories { + maven { url = 'repo' } + } + + sourceSets.main.component { + outputKinds = [ KLIBRARY ] + targets = ['host'] + } + + dependencies { + implementation 'test:libFoo:1.0' + } + """.trimIndent()) + + assertTrue(projectDirectory.resolve("build").deleteRecursively()) + assertTrue(projectDirectory.resolve("libFoo/build").deleteRecursively()) + assertTrue(projectDirectory.resolve("libBar/build").deleteRecursively()) + + rootProject.createRunner().withArguments(":build").build().apply { + output.contains("Interop is here!") + output.contains("Transitive call!") + } + } } \ No newline at end of file