Skip to content

Commit

Permalink
[Wasm][Tests] Add WasmKlibResolverTest
Browse files Browse the repository at this point in the history
^KT-70146
  • Loading branch information
vsukharev authored and Space Team committed Feb 20, 2025
1 parent 62eae07 commit 61f9441
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ abstract class WasmEnvironmentConfigurator(testServices: TestServices) : Environ

companion object : KlibBasedEnvironmentConfiguratorUtils {
fun getRuntimePathsForModule(target: WasmTarget): List<String> {
val suffix = when (target) {
WasmTarget.JS -> "-js"
WasmTarget.WASI -> "-wasi"
}
return listOf(System.getProperty("kotlin.wasm$suffix.stdlib.path")!!, System.getProperty("kotlin.wasm$suffix.kotlin.test.path")!!)
return listOf(stdlibPath(target), kotlinTestPath(target))
}

fun kotlinTestPath(target: WasmTarget): String = System.getProperty("kotlin.${target.alias}.kotlin.test.path")!!
fun stdlibPath(target: WasmTarget): String = System.getProperty("kotlin.${target.alias}.stdlib.path")!!

fun getMainModule(testServices: TestServices): TestModule {
val modules = testServices.moduleStructure.modules
val inferMainModule = INFER_MAIN_MODULE in testServices.moduleStructure.allDirectives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.cliArgument
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.config.DuplicatedUniqueNameStrategy
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
import org.jetbrains.kotlin.test.services.StandardLibrariesPathProviderForKotlinProject
import org.jetbrains.kotlin.test.util.JUnit4Assertions
import org.jetbrains.kotlin.test.util.KtTestUtil
import org.jetbrains.kotlin.test.utils.assertCompilerOutputHasKlibResolverIncompatibleAbiMessages
import org.jetbrains.kotlin.test.utils.patchManifestToBumpAbiVersion
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JsKlibResolverTest : TestCaseWithTmpdir() {
open class JsKlibResolverTest {
lateinit var tmpdir: File

@BeforeEach
fun setup() {
tmpdir = KtTestUtil.tmpDirForTest(this.javaClass.getSimpleName(), hashCode().toString())
}

@Test
fun testWarningAboutRejectedLibraryIsNotSuppressed() {
val testDataDir = File("compiler/testData/klib/resolve/mismatched-abi-version")

Expand Down Expand Up @@ -55,6 +66,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
assertCompilerOutputHasKlibResolverIncompatibleAbiMessages(JUnit4Assertions, result.output, missingLibrary = "/v2/lib1", tmpdir)
}

@Test
fun testResolvingTransitiveDependenciesRecordedInManifest() {
val moduleA = Module("a")
val moduleB = Module("b", "a")
Expand Down Expand Up @@ -93,6 +105,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
}
}

@Test
fun testWarningAboutDuplicatedUniqueNames() {
val result = compilationResultOfModulesWithDuplicatedUniqueNames(
arrayOf(DuplicatedUniqueNameStrategy.ALLOW_FIRST_WITH_WARNING.asCliArgument())
Expand All @@ -108,6 +121,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
})
}

@Test
fun testErrorAboutDuplicatedUniqueNames() {
val result = compilationResultOfModulesWithDuplicatedUniqueNames(
arrayOf(DuplicatedUniqueNameStrategy.DENY.asCliArgument())
Expand All @@ -120,6 +134,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
})
}

@Test
fun testErrorAboutDuplicatedUniqueNamesWithoutCLIParam() {
val result = compilationResultOfModulesWithDuplicatedUniqueNames(emptyArray())
result.assertFailure()
Expand All @@ -130,6 +145,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
})
}

@Test
fun testAllKlibsUsedDespiteWarningAboutDuplicatedUniqueNames() {
val result = compilationResultOfModulesWithDuplicatedUniqueNames(
arrayOf(DuplicatedUniqueNameStrategy.ALLOW_ALL_WITH_WARNING.asCliArgument())
Expand Down Expand Up @@ -171,7 +187,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
private fun createKlibDir(name: String, version: Int): File =
tmpdir.resolve("v$version").resolve(name).apply(File::mkdirs)

private fun compileKlib(
open fun compileKlib(
sourceFile: File,
dependencies: Array<File> = emptyArray(),
outputFile: File,
Expand Down Expand Up @@ -199,7 +215,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
return CompilationResult(exitCode, compilerXmlOutput.toString())
}

private fun compileToJs(entryModuleKlib: File, dependency: File?, outputFile: File): CompilationResult {
open fun compileToJs(entryModuleKlib: File, dependency: File?, outputFile: File): CompilationResult {
val libraries = listOfNotNull(
StandardLibrariesPathProviderForKotlinProject.fullJsStdlib(),
dependency
Expand All @@ -222,7 +238,7 @@ class JsKlibResolverTest : TestCaseWithTmpdir() {
return CompilationResult(exitCode, compilerXmlOutput.toString())
}

private data class CompilationResult(val exitCode: ExitCode, val output: String) {
data class CompilationResult(val exitCode: ExitCode, val output: String) {
fun assertSuccess() = JUnit4Assertions.assertTrue(exitCode == ExitCode.OK) {
buildString {
appendLine("Expected exit code: ${ExitCode.OK}, Actual: $exitCode")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2010-2025 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.wasm.test.klib

import org.jetbrains.kotlin.cli.common.arguments.cliArgument
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.js.testOld.klib.JsKlibResolverTest
import org.jetbrains.kotlin.platform.wasm.WasmTarget
import org.jetbrains.kotlin.test.services.configuration.WasmEnvironmentConfigurator
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream

class WasmJsKlibResolverTest : JsKlibResolverTest() {
// TODO: Move to helpers in compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/KotlinStandardLibrariesPathProvider.kt
private fun fullWasmJsStdlib(): File {
val stdlibPath = WasmEnvironmentConfigurator.stdlibPath(WasmTarget.JS)
return File(stdlibPath).also {
assert(it.exists()) { "stdlib is not found at $stdlibPath" }
}
}

override fun compileKlib(
sourceFile: File,
dependencies: Array<File>,
outputFile: File,
extraArgs: Array<String>
): CompilationResult {
val libraries = listOfNotNull(fullWasmJsStdlib(), *dependencies
) .joinToString(File.pathSeparator) { it.absolutePath }

val args = arrayOf(
K2JSCompilerArguments::wasm.cliArgument,
K2JSCompilerArguments::wasmTarget.cliArgument("wasm-js"),
K2JSCompilerArguments::irProduceKlibDir.cliArgument,
K2JSCompilerArguments::libraries.cliArgument, libraries,
K2JSCompilerArguments::outputDir.cliArgument, outputFile.absolutePath,
K2JSCompilerArguments::moduleName.cliArgument, outputFile.nameWithoutExtension,
*extraArgs,
sourceFile.absolutePath
)

val compilerXmlOutput = ByteArrayOutputStream()
val exitCode = PrintStream(compilerXmlOutput).use { printStream ->
K2JSCompiler().execFullPathsInMessages(printStream, args)
}

return CompilationResult(exitCode, compilerXmlOutput.toString())
}

override fun compileToJs(entryModuleKlib: File, dependency: File?, outputFile: File): CompilationResult {
val libraries = listOfNotNull(
fullWasmJsStdlib(),
dependency
).joinToString(File.pathSeparator) { it.absolutePath }

val args = arrayOf(
K2JSCompilerArguments::wasm.cliArgument,
K2JSCompilerArguments::wasmTarget.cliArgument("wasm-js"),
K2JSCompilerArguments::irProduceJs.cliArgument,
K2JSCompilerArguments::includes.cliArgument(entryModuleKlib.absolutePath),
K2JSCompilerArguments::libraries.cliArgument, libraries,
K2JSCompilerArguments::outputDir.cliArgument, outputFile.absolutePath,
K2JSCompilerArguments::moduleName.cliArgument, outputFile.nameWithoutExtension,
K2JSCompilerArguments::target.cliArgument, "es2015",
)

val compilerXmlOutput = ByteArrayOutputStream()
val exitCode = PrintStream(compilerXmlOutput).use { printStream ->
K2JSCompiler().execFullPathsInMessages(printStream, args)
}

return CompilationResult(exitCode, compilerXmlOutput.toString())
}
}

0 comments on commit 61f9441

Please sign in to comment.