Add tests for "lite" API for Kotlin/Native libraries

This commit is contained in:
Dmitriy Dolovov
2019-04-26 20:24:20 +07:00
parent 07f6059f44
commit 2ef7cde34b
16 changed files with 189 additions and 1 deletions
+3 -1
View File
@@ -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()
@@ -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)
}
}
@@ -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<File> {
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<File, LiteKonanLibrary> =
getPotentialLibraryPathsOnLocalFS().mapNotNull { libraryProvider.getLibrary(it) }.toLibraryMap()
private fun librariesExpectedInDistribution(): Map<File, FakeLibraryForTest> = 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<File, FakeLibraryForTest> = listOf(
FakeLibraryForTest(externalLibsDir.resolve("correct"))
).toLibraryMap()
private fun compareLibraries(expected: Map<File, FakeLibraryForTest>, actual: Map<File, LiteKonanLibrary>, 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 <T : LiteKonanLibrary> List<T>.toLibraryMap() = map { it.path to it }.toMap()
}
}
private class FakeLibraryForTest(
override val path: File,
override val platform: String? = null
) : LiteKonanLibrary {
override val sourcePaths: Collection<File> = emptyList()
override val name: String = path.name
}
@@ -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)
@@ -0,0 +1,2 @@
SPOILED_unique_name=anotherBroken
compiler_version=1.2.3
@@ -0,0 +1,2 @@
unique_name=correct
compiler_version=1.2.3-eap-45
@@ -0,0 +1,2 @@
unique_name=yetAnotherBroken
compiler_version=1.2-eap-8879
@@ -0,0 +1,2 @@
unique_name=stdlib
compiler_version=1.2.3-release-5678
@@ -0,0 +1,2 @@
unique_name=bar
compiler_version=1.2-eap-8879
@@ -0,0 +1,2 @@
unique_name=baz
compiler_version=1.3.1-dev-9301
@@ -0,0 +1,2 @@
unique_name=broken
SPOILED_compiler_version=1.2.3
@@ -0,0 +1,2 @@
unique_name=foo
compiler_version=1.3-release-1234