Skip to content

Commit a4076fa

Browse files
committed
Fix JS tests
1 parent 80130ed commit a4076fa

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

core/build.gradle.kts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
12
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
23

34
plugins {
@@ -18,6 +19,35 @@ buildConfig {
1819
}
1920
}
2021

22+
/* Multiple variants are offered of the dependencies kotlin-dom-api-compat and kotlin-stdlib-js. Usually, Gradle picks
23+
* them automatically based on what kind of build it is, i.e. it would look for a platform JVM variant for this JVM build.
24+
* Naturally, there is no JVM version for JS libraries. We need to fix the variant attributes manually so the right variant
25+
* will be picked for runtime use in the JS compile tests. */
26+
configurations.all {
27+
resolutionStrategy.dependencySubstitution {
28+
substitute(module(libs.kotlin.domApiCompat.get().module.toString()))
29+
.using(variant(module(libs.kotlin.domApiCompat.get().toString())) {
30+
attributes {
31+
attribute(Attribute.of("org.jetbrains.kotlin.platform.type", KotlinPlatformType::class.java), KotlinPlatformType.js)
32+
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "kotlin-runtime"))
33+
}
34+
})
35+
}
36+
}
37+
38+
configurations.all {
39+
resolutionStrategy.dependencySubstitution {
40+
substitute(module(libs.kotlin.stdlibJs.get().module.toString()))
41+
.using(variant(module(libs.kotlin.stdlibJs.get().toString())) {
42+
attributes {
43+
attribute(Attribute.of("org.jetbrains.kotlin.platform.type", KotlinPlatformType::class.java), KotlinPlatformType.js)
44+
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "kotlin-runtime"))
45+
attribute(Attribute.of("org.jetbrains.kotlin.js.compiler", String::class.java), "ir")
46+
}
47+
})
48+
}
49+
}
50+
2151
dependencies {
2252
implementation(libs.autoService)
2353
ksp(libs.autoService.ksp)
@@ -31,6 +61,9 @@ dependencies {
3161
// These dependencies are only needed as a "sample" compiler plugin to test that
3262
// running compiler plugins passed via the pluginClasspath CLI option works
3363
testRuntimeOnly(libs.kotlin.scriptingCompiler)
64+
// Include Kotlin/JS standard library in test classpath for auto loading
65+
testRuntimeOnly(libs.kotlin.stdlibJs)
66+
testRuntimeOnly(libs.kotlin.domApiCompat)
3467
testRuntimeOnly(libs.intellij.core)
3568
testRuntimeOnly(libs.intellij.util)
3669

core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ internal object HostEnvironment {
2727
findInClasspath(kotlinDependencyRegex("kotlin-stdlib-js"))
2828
}
2929

30+
val kotlinDomApiCompatKlib: File? by lazy {
31+
findInClasspath(kotlinDependencyRegex("kotlin-dom-api-compat"))
32+
}
33+
34+
3035
val kotlinReflectJar: File? by lazy {
3136
findInClasspath(kotlinDependencyRegex("kotlin-reflect"))
3237
}
@@ -40,7 +45,7 @@ internal object HostEnvironment {
4045
}
4146

4247
private fun kotlinDependencyRegex(prefix: String): Regex {
43-
return Regex("$prefix(-[0-9]+\\.[0-9]+(\\.[0-9]+)?)([-0-9a-zA-Z]+)?\\.jar")
48+
return Regex("$prefix(-[0-9]+\\.[0-9]+(\\.[0-9]+)?)([-0-9a-zA-Z]+)?(\\.jar|\\.klib)")
4449
}
4550

4651
/** Tries to find a file matching the given [regex] in the host process' classpath */
@@ -60,7 +65,11 @@ internal object HostEnvironment {
6065

6166
val classpaths = classGraph.classpathFiles
6267
val modules = classGraph.modules.mapNotNull { it.locationFile }
68+
val klibs = System.getProperty("java.class.path")
69+
.split(File.pathSeparator)
70+
.filter { it.endsWith(".klib") }
71+
.map(::File)
6372

64-
return (classpaths + modules).distinctBy(File::getAbsolutePath)
73+
return (classpaths + modules + klibs).distinctBy(File::getAbsolutePath)
6574
}
6675
}

core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ class KotlinJsCompilation : AbstractKotlinCompilation<K2JSCompilerArguments>() {
4747
HostEnvironment.kotlinStdLibJsJar
4848
}
4949

50+
/**
51+
* Path to the kotlin-dom-api-compat.klib
52+
* If none is given, it will be searched for in the host
53+
* process' classpaths
54+
*/
55+
var kotlinStdLibDomApi: File? by default {
56+
HostEnvironment.kotlinDomApiCompatKlib
57+
}
58+
5059
/**
5160
* Generate TypeScript declarations .d.ts file alongside JS file. Available in IR backend only
5261
*/
@@ -67,7 +76,7 @@ class KotlinJsCompilation : AbstractKotlinCompilation<K2JSCompilerArguments>() {
6776
args.outputFile = File(outputDir, outputFileName).absolutePath
6877
args.outputDir = outputDir.absolutePath
6978
args.sourceMapBaseDirs = jsClasspath().joinToString(separator = File.pathSeparator)
70-
args.libraries = listOfNotNull(kotlinStdLibJsJar).joinToString(separator = ":")
79+
args.libraries = listOfNotNull(kotlinStdLibJsJar, kotlinStdLibDomApi).joinToString(separator = File.pathSeparator)
7180

7281
args.irProduceKlibDir = irProduceKlibDir
7382
args.irProduceKlibFile = irProduceKlibFile

core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import org.junit.Test
1313
import org.junit.rules.TemporaryFolder
1414
import java.io.File
1515

16-
@Ignore("These no longer work with Kotlin JS IR")
1716
@Suppress("MemberVisibilityCanBePrivate")
1817
class KotlinJsCompilationTests {
1918
@Rule @JvmField val temporaryFolder = TemporaryFolder()

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ javapoet = "com.squareup:javapoet:1.13.0"
2424
kotlin-compilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" }
2525
kotlin-annotationProcessingEmbeddable = { module = "org.jetbrains.kotlin:kotlin-annotation-processing-embeddable", version.ref = "kotlin" }
2626
kotlin-scriptingCompiler = { module = "org.jetbrains.kotlin:kotlin-scripting-compiler", version.ref = "kotlin" }
27+
kotlin-stdlibJs = { module = "org.jetbrains.kotlin:kotlin-stdlib-js", version.ref = "kotlin" }
28+
kotlin-domApiCompat = { module = "org.jetbrains.kotlin:kotlin-dom-api-compat", version.ref = "kotlin" }
2729
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
2830
kotlin-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
2931

0 commit comments

Comments
 (0)