From 2ef7cde34b687d1426c51755648a0f47da4c0dc2 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Fri, 26 Apr 2019 20:24:20 +0700 Subject: [PATCH] Add tests for "lite" API for Kotlin/Native libraries --- konan/library-reader/build.gradle.kts | 4 +- .../lite/LiteKonanDistributionTests.kt | 30 +++++ .../lite/LiteKonanLibraryFacadeTests.kt | 123 ++++++++++++++++++ .../kotlin/konan/library/lite/utils.kt | 16 +++ .../external-libs/anotherBroken/manifest | 2 + .../testData/external-libs/correct/manifest | 2 + .../testData/external-libs/invalid | 0 .../yetAnotherBroken/not-a-manifest | 2 + .../klib/common/stdlib/manifest | 2 + .../klib/platform/macos_x64/bar/manifest | 2 + .../klib/platform/macos_x64/baz/manifest | 2 + .../klib/platform/macos_x64/broken/manifest | 2 + .../klib/platform/macos_x64/foo/manifest | 2 + .../klib/platform/macos_x64/invalid | 0 .../placeholder.kt | 1 + .../sources/kotlin-stdlib-native-sources.zip | Bin 0 -> 444 bytes 16 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanDistributionTests.kt create mode 100644 konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanLibraryFacadeTests.kt create mode 100644 konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/utils.kt create mode 100644 konan/library-reader/testData/external-libs/anotherBroken/manifest create mode 100644 konan/library-reader/testData/external-libs/correct/manifest create mode 100644 konan/library-reader/testData/external-libs/invalid create mode 100644 konan/library-reader/testData/external-libs/yetAnotherBroken/not-a-manifest create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/common/stdlib/manifest create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/bar/manifest create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/baz/manifest create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/broken/manifest create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/foo/manifest create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/invalid create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-common-sources/placeholder.kt create mode 100644 konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-native-sources.zip diff --git a/konan/library-reader/build.gradle.kts b/konan/library-reader/build.gradle.kts index ed8e78a2d5b..f25eb589d5e 100644 --- a/konan/library-reader/build.gradle.kts +++ b/konan/library-reader/build.gradle.kts @@ -25,11 +25,13 @@ dependencies { mavenCompileScope(projectRuntimeJar(":kotlin-compiler")) compile(project(":kotlin-native:kotlin-native-utils")) + + testCompile(commonDep("junit:junit")) } sourceSets { "main" { projectDefault() } - "test" { none() } + "test" { projectDefault() } } publish() diff --git a/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanDistributionTests.kt b/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanDistributionTests.kt new file mode 100644 index 00000000000..6059ef0b0f8 --- /dev/null +++ b/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanDistributionTests.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.konan.library.lite + +import org.jetbrains.kotlin.konan.KonanVersion +import org.junit.Test +import org.junit.Assert.* + +class LiteKonanDistributionTests { + + @Test + fun testDistribution() { + val distribution = LiteKonanDistributionProvider.getDistribution(konanHomeDir) + ?: error("Could not load Kotlin/Native distribution info from $konanHomeDir") + + assertEquals("Kotlin/Native distribution home", konanHomeDir, distribution.distributionHome) + assertEquals("Kotlin/Native version string", expectedVersionString, distribution.konanVersionString) + assertEquals("Kotlin/Native version", expectedVersion, distribution.konanVersion) + } + + private companion object { + const val expectedVersionString = "1.2.3-release-5678" + + val expectedVersion: KonanVersion + get() = KonanVersion.fromString(expectedVersionString) + } +} diff --git a/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanLibraryFacadeTests.kt b/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanLibraryFacadeTests.kt new file mode 100644 index 00000000000..902a05c9cb6 --- /dev/null +++ b/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/LiteKonanLibraryFacadeTests.kt @@ -0,0 +1,123 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.konan.library.lite + +import org.jetbrains.kotlin.konan.library.KLIB_DIR_NAME +import org.jetbrains.kotlin.konan.library.KONAN_COMMON_LIBS_DIR_NAME +import org.jetbrains.kotlin.konan.library.KONAN_PLATFORM_LIBS_DIR_NAME +import org.jetbrains.kotlin.konan.library.KONAN_STDLIB_NAME +import org.junit.Assert.* +import org.junit.Test +import java.io.File +import java.io.FileFilter +import java.nio.file.Files +import java.util.zip.ZipException +import java.util.zip.ZipFile + +class LiteKonanLibraryFacadeTests { + + @Test + fun allLibrariesRecognized() { + val libraryProvider = LiteKonanLibraryFacade.getLibraryProvider() + + val actualLibraries = collectLibrariesFromLocalFS(libraryProvider) + val expectedLibraries = librariesExpectedInDistribution() + librariesExpectedInExternalDir() + + compareLibraries( + expectedLibraries, + actualLibraries, + /* `LiteKonanLibraryFacade.getLibraryProvider()` is currently platform-agnostic */ true + ) + } + + @Test + fun onlyFromDistributionLibrariesRecognized() { + val libraryProvider = LiteKonanLibraryFacade.getDistributionLibraryProvider(konanHomeDir) + + val actualLibraries = collectLibrariesFromLocalFS(libraryProvider) + val expectedLibraries = librariesExpectedInDistribution() + + compareLibraries(expectedLibraries, actualLibraries, false) + } + + @Test + fun sourcesAttachedToStdlib() { + val libraryProvider = LiteKonanLibraryFacade.getDistributionLibraryProvider(konanHomeDir) + + val stdlib = collectLibrariesFromLocalFS(libraryProvider).values.firstOrNull { it.name == KONAN_STDLIB_NAME } + ?: error("Could not load Kotlin/Native $KONAN_STDLIB_NAME from $konanHomeDir") + + assertTrue("Kotlin/Native $KONAN_STDLIB_NAME sources must not be empty", stdlib.sourcePaths.isNotEmpty()) + + assertTrue( + "Each path in $KONAN_STDLIB_NAME.sourcePaths must be either non-empty directory or ZIP (JAR) file", + stdlib.sourcePaths.all { sourcePath -> + when { + sourcePath.isDirectory -> Files.newDirectoryStream(sourcePath.toPath()).use { it.any { true } } + sourcePath.isFile -> try { + ZipFile(sourcePath).use { it.name != null } + } catch (e: ZipException) { + false + } + else -> false + } + }) + + } + + private fun getPotentialLibraryPathsOnLocalFS(): List { + val roots = mutableListOf(externalLibsDir) + + roots += klibDir.resolve(KONAN_COMMON_LIBS_DIR_NAME) + roots += klibDir.resolve(KONAN_PLATFORM_LIBS_DIR_NAME).listFiles(FileFilter { it.isDirectory }).toList() + + return roots.flatMap { it.listFiles().toList() } + } + + private fun collectLibrariesFromLocalFS(libraryProvider: LiteKonanLibraryProvider): Map = + getPotentialLibraryPathsOnLocalFS().mapNotNull { libraryProvider.getLibrary(it) }.toLibraryMap() + + private fun librariesExpectedInDistribution(): Map = listOf( + FakeLibraryForTest(klibDir.resolve(KONAN_COMMON_LIBS_DIR_NAME, KONAN_STDLIB_NAME)), + FakeLibraryForTest(klibDir.resolve(KONAN_PLATFORM_LIBS_DIR_NAME, "macos_x64", "foo"), platform = "macos_x64"), + FakeLibraryForTest(klibDir.resolve(KONAN_PLATFORM_LIBS_DIR_NAME, "macos_x64", "bar"), platform = "macos_x64"), + FakeLibraryForTest(klibDir.resolve(KONAN_PLATFORM_LIBS_DIR_NAME, "macos_x64", "baz"), platform = "macos_x64") + ).toLibraryMap() + + private fun librariesExpectedInExternalDir(): Map = listOf( + FakeLibraryForTest(externalLibsDir.resolve("correct")) + ).toLibraryMap() + + private fun compareLibraries(expected: Map, actual: Map, ignorePlatform: Boolean) { + assertEquals("Libraries paths mismatch", expected.keys, actual.keys) + + for (key in expected.keys) { + val expectedLibrary = expected.getValue(key) + val actualLibrary = actual.getValue(key) + + assertEquals("Library path mismatch", expectedLibrary.path, actualLibrary.path) + + if (!ignorePlatform) { + assertEquals("Library platform mismatch, path=${expectedLibrary.path}", expectedLibrary.platform, actualLibrary.platform) + } + } + } + + private companion object { + val klibDir = konanHomeDir.resolve(KLIB_DIR_NAME) + val externalLibsDir = testDataDir.resolve("external-libs") + + fun List.toLibraryMap() = map { it.path to it }.toMap() + } +} + +private class FakeLibraryForTest( + override val path: File, + override val platform: String? = null +) : LiteKonanLibrary { + override val sourcePaths: Collection = emptyList() + override val name: String = path.name +} diff --git a/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/utils.kt b/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/utils.kt new file mode 100644 index 00000000000..336525d30be --- /dev/null +++ b/konan/library-reader/test/org/jetbrains/kotlin/konan/library/lite/utils.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.konan.library.lite + +import java.io.File + +internal val testDataDir = File(System.getProperty("user.dir") ?: ".").canonicalFile.resolve("testData") + +internal val konanHomeDir = testDataDir.resolve("kotlin-native-data-dir", "kotlin-native-PLATFORM-VERSION") + +internal fun File.resolve(relative: String, next: String) = resolve(relative).resolve(next) + +internal fun File.resolve(relative: String, next: String, another: String) = resolve(relative, next).resolve(another) diff --git a/konan/library-reader/testData/external-libs/anotherBroken/manifest b/konan/library-reader/testData/external-libs/anotherBroken/manifest new file mode 100644 index 00000000000..e03519b45fc --- /dev/null +++ b/konan/library-reader/testData/external-libs/anotherBroken/manifest @@ -0,0 +1,2 @@ +SPOILED_unique_name=anotherBroken +compiler_version=1.2.3 diff --git a/konan/library-reader/testData/external-libs/correct/manifest b/konan/library-reader/testData/external-libs/correct/manifest new file mode 100644 index 00000000000..989131422c1 --- /dev/null +++ b/konan/library-reader/testData/external-libs/correct/manifest @@ -0,0 +1,2 @@ +unique_name=correct +compiler_version=1.2.3-eap-45 diff --git a/konan/library-reader/testData/external-libs/invalid b/konan/library-reader/testData/external-libs/invalid new file mode 100644 index 00000000000..e69de29bb2d diff --git a/konan/library-reader/testData/external-libs/yetAnotherBroken/not-a-manifest b/konan/library-reader/testData/external-libs/yetAnotherBroken/not-a-manifest new file mode 100644 index 00000000000..05294b09bf5 --- /dev/null +++ b/konan/library-reader/testData/external-libs/yetAnotherBroken/not-a-manifest @@ -0,0 +1,2 @@ +unique_name=yetAnotherBroken +compiler_version=1.2-eap-8879 diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/common/stdlib/manifest b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/common/stdlib/manifest new file mode 100644 index 00000000000..e7b15ab54c9 --- /dev/null +++ b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/common/stdlib/manifest @@ -0,0 +1,2 @@ +unique_name=stdlib +compiler_version=1.2.3-release-5678 diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/bar/manifest b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/bar/manifest new file mode 100644 index 00000000000..e6b8521fcb4 --- /dev/null +++ b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/bar/manifest @@ -0,0 +1,2 @@ +unique_name=bar +compiler_version=1.2-eap-8879 diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/baz/manifest b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/baz/manifest new file mode 100644 index 00000000000..73fe57ce5d2 --- /dev/null +++ b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/baz/manifest @@ -0,0 +1,2 @@ +unique_name=baz +compiler_version=1.3.1-dev-9301 diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/broken/manifest b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/broken/manifest new file mode 100644 index 00000000000..525addbe617 --- /dev/null +++ b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/broken/manifest @@ -0,0 +1,2 @@ +unique_name=broken +SPOILED_compiler_version=1.2.3 diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/foo/manifest b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/foo/manifest new file mode 100644 index 00000000000..c99f36dbd55 --- /dev/null +++ b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/foo/manifest @@ -0,0 +1,2 @@ +unique_name=foo +compiler_version=1.3-release-1234 diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/invalid b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/klib/platform/macos_x64/invalid new file mode 100644 index 00000000000..e69de29bb2d diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-common-sources/placeholder.kt b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-common-sources/placeholder.kt new file mode 100644 index 00000000000..9c71eb811fe --- /dev/null +++ b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-common-sources/placeholder.kt @@ -0,0 +1 @@ +// this file is empty diff --git a/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-native-sources.zip b/konan/library-reader/testData/kotlin-native-data-dir/kotlin-native-PLATFORM-VERSION/sources/kotlin-stdlib-native-sources.zip new file mode 100644 index 0000000000000000000000000000000000000000..90641962441723778300b9a8729005d10cbe51f0 GIT binary patch literal 444 zcmWIWW@h1H0D;fRbN#>!C?U%r!;qa{l9QRITU?TolbNKOmspZnma1EvUs{x$TC5)$ z!pXpF)pIPy8Hh_OxEUB(zA-W|u!sN+2tYBGPgtQx3@8f1+JuZP$Vp62&B)J5NiEXL zE`eKOjcJL#zCuYxX0bwAW=^UCkWS4lD5>NM@MdI^W5(qt38?P`7~VR9m;`*t3h^O^ n?@+Bk^&i9vU`Q}5X$&W11-fr>S;)!;axD`OUIEgVKpX}Dq&{jD literal 0 HcmV?d00001