Skip to content

Commit

Permalink
Fix KSP processor wasm handling and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whyoleg committed Jan 14, 2025
1 parent 2ed744a commit 71b9694
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 74 deletions.
6 changes: 3 additions & 3 deletions build-logic/src/main/kotlin/sweetbuild.kotlin.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

import org.jetbrains.kotlin.gradle.dsl.*
Expand All @@ -13,7 +13,7 @@ plugins {
@Suppress("UnstableApiUsage")
plugins.withType<KotlinBasePluginWrapper>().configureEach {
extensions.configure<KotlinProjectExtension>("kotlin") {
val javaToolchains = extensions.getByName<JavaToolchainService>("javaToolchains")
val javaToolchains = project.extensions.getByName<JavaToolchainService>("javaToolchains")

val jdkToolchainVersion = 8
val jdkTestVersions = setOf(11, 17, 21)
Expand All @@ -30,7 +30,7 @@ plugins.withType<KotlinBasePluginWrapper>().configureEach {
is KotlinJvmProjectExtension -> {
jvmToolchain(jdkToolchainVersion)
plugins.apply("org.gradle.jvm-test-suite")
extensions.configure<TestingExtension>("testing") {
project.extensions.configure<TestingExtension>("testing") {
val test by suites.getting(JvmTestSuite::class) {
jdkTestVersions.forEach { jdkTestVersion ->
targets.register("test${jdkTestVersion}") {
Expand Down
7 changes: 4 additions & 3 deletions sweetspi-processor/src/main/kotlin/generate.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.processor
Expand All @@ -15,8 +15,9 @@ private val KCLASS = ClassName("kotlin.reflect", "KClass").parameterizedBy(STAR)

fun generate(codeGenerator: CodeGenerator, platform: PlatformInfo, context: SweetContext) {
// KSP doesn't support Wasm platform info
val isJs = platform.toString() == "JS"
val isWasm = platform.toString() == "Wasm"
val isJs = platform.toString().lowercase().startsWith("js")
// handles both wasm-js and wasm-wasi targets
val isWasm = platform.toString().lowercase().startsWith("wasm")
val isNative = platform is NativePlatformInfo
val isJvm = platform is JvmPlatformInfo
val moduleName = context.packageName.replace(".", "_")
Expand Down
11 changes: 1 addition & 10 deletions sweetspi-tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("UnstableApiUsage", "HasPlatformType")
Expand Down Expand Up @@ -68,8 +68,6 @@ testing.suites {
TestsArgumentProvider(
devArtifactsDirectories = devArtifactsResolver.incoming.files,
testKitDirectory = layout.buildDirectory.dir("test-kit"),
kotlinVersion = libs.versions.kotlin.asProvider(),
kspVersion = libs.versions.ksp,
projectVersion = provider { project.version.toString() }
)
)
Expand All @@ -86,10 +84,6 @@ class TestsArgumentProvider(
private val devArtifactsDirectories: FileCollection,
private val testKitDirectory: Provider<Directory>,
@get:Input
val kotlinVersion: Provider<String>,
@get:Input
val kspVersion: Provider<String>,
@get:Input
val projectVersion: Provider<String>,
) : CommandLineArgumentProvider {

Expand Down Expand Up @@ -118,8 +112,5 @@ class TestsArgumentProvider(
"-Dorg.gradle.testkit.dir=${testKitDirectoryPath.get()}",
"-Dsweettests.dev-artifacts-directories=${devArtifactsDirectoriesList.get()}",
"-Dsweettests.dev-artifacts-version=${projectVersion.get()}",
"-Dsweettests.gradle-version=${GradleVersion.current().version}",
"-Dsweettests.kotlin-version=${kotlinVersion.get()}",
"-Dsweettests.ksp-version=${kspVersion.get()}",
)
}
5 changes: 2 additions & 3 deletions sweetspi-tests/src/main/kotlin/AbstractTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests
Expand All @@ -10,7 +10,6 @@ import kotlin.io.path.*

abstract class AbstractTest {
open val defaultTemplate: TestTemplate? get() = null
open val defaultVersions: TestVersions? get() = null

private lateinit var temporaryDirectory: Path

Expand All @@ -29,8 +28,8 @@ abstract class AbstractTest {
val projectDirectory: Path get() = temporaryDirectory.resolve("project").createDirectories()

fun project(
versions: TestVersions,
template: TestTemplate = defaultTemplate ?: error("No default 'template' for $this"),
versions: TestVersions = defaultVersions ?: error("No default 'versions' for $this"),
block: TestFiles.() -> Unit,
): TestProject {
template.templatePath.copyToRecursively(projectDirectory, followLinks = false, overwrite = true)
Expand Down
14 changes: 8 additions & 6 deletions sweetspi-tests/src/main/kotlin/TestVersions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests
Expand All @@ -23,13 +23,15 @@ abstract class TestVersionsProvider(private val versions: Sequence<TestVersions>
object All : TestVersionsProvider({
listOf(
"8.0.2", // oldest supported
"8.9", // latest stable
"8.10-rc-1" // latest RC
"8.12", // latest stable
).forEach { gradleVersion ->
listOf(
"2.0.0-1.0.24", // oldest supported
"2.0.10-1.0.24", // latest stable
"2.0.20-RC-1.0.24" // latest RC
"2.0.0-1.0.24", // oldest supported
"2.0.10-1.0.24",
"2.0.21-1.0.28",
"2.1.0-1.0.29",
"2.1.10-RC-1.0.29",
"2.1.20-Beta1-1.0.29",
).forEach { kspVersion ->
yield(
TestVersions(
Expand Down
8 changes: 1 addition & 7 deletions sweetspi-tests/src/main/kotlin/TestsArguments.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests
Expand All @@ -8,12 +8,6 @@ object TestsArguments {
val devArtifactsDirectories = systemProperty("dev-artifacts-directories").split(",")
val devArtifactsVersion = systemProperty("dev-artifacts-version")

val defaultTestVersions = TestVersions(
gradleVersion = systemProperty("gradle-version"),
kotlinVersion = systemProperty("kotlin-version"),
kspVersion = systemProperty("ksp-version")
)

private fun systemProperty(name: String): String = checkNotNull(System.getProperty("sweettests.$name")) {
"'sweettests.$name' is missing in the system properties"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests.processor

import dev.whyoleg.sweetspi.tests.*
import org.gradle.testkit.runner.*
import org.junit.jupiter.params.*
import org.junit.jupiter.params.provider.*
import kotlin.io.path.*
import kotlin.test.*

class MultiplatformJvmProcessorTest : AbstractTest() {
override val defaultTemplate: TestTemplate get() = TestTemplate.MULTIPLATFORM
override val defaultVersions: TestVersions get() = TestsArguments.defaultTestVersions

@Test
fun testJvmTargetOutput() {
val project = project {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testJvmTargetOutput(versions: TestVersions) {
val project = project(versions) {
withSweetSpi()
jvmTarget()
kotlinSourceFile(
Expand All @@ -39,9 +41,10 @@ class MultiplatformJvmProcessorTest : AbstractTest() {
}
}

@Test
fun testJvmProviderNotExtendingService() {
val project = project {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testJvmProviderNotExtendingService(versions: TestVersions) {
val project = project(versions) {
withSweetSpi()
jvmTarget()
kotlinSourceFile(
Expand Down
13 changes: 7 additions & 6 deletions sweetspi-tests/src/testRuntime/kotlin/JvmRuntimeTest.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests.runtime

import dev.whyoleg.sweetspi.tests.*
import kotlin.test.*
import org.junit.jupiter.params.*
import org.junit.jupiter.params.provider.*

class JvmRuntimeTest : AbstractTest() {
override val defaultTemplate: TestTemplate get() = TestTemplate.JVM
override val defaultVersions: TestVersions get() = TestsArguments.defaultTestVersions

@Test
fun testRuntimeWorks() {
val project = project {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testRuntimeWorks(versions: TestVersions) {
val project = project(versions) {
withSweetSpi()
kotlinJvmTest()
kotlinSourceFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests.runtime

import dev.whyoleg.sweetspi.tests.*
import kotlin.test.*
import org.junit.jupiter.params.*
import org.junit.jupiter.params.provider.*

class MultiplatformCrossModuleRuntimeTest : AbstractTest() {
override val defaultTemplate: TestTemplate get() = TestTemplate.MULTIPLATFORM_MULTIMODULE
override val defaultVersions: TestVersions get() = TestsArguments.defaultTestVersions

@Test
fun test3ModulesInteraction() {
val project = project {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun test3ModulesInteraction(versions: TestVersions) {
val project = project(versions = versions) {
append(SETTINGS_GRADLE_KTS) {
"""
include(":service")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests.runtime

import dev.whyoleg.sweetspi.tests.*
import org.intellij.lang.annotations.*
import kotlin.test.*
import org.junit.jupiter.params.*
import org.junit.jupiter.params.provider.*

class MultiplatformJvmRuntimeTest : AbstractTest() {
override val defaultTemplate: TestTemplate get() = TestTemplate.MULTIPLATFORM
override val defaultVersions: TestVersions get() = TestsArguments.defaultTestVersions

private fun jvmProject(block: TestFiles.() -> Unit) = project {
private fun jvmProject(versions: TestVersions, block: TestFiles.() -> Unit) = project(versions) {
withSweetSpi()
jvmTarget()
kotlinTest(JVM_TEST)
Expand Down Expand Up @@ -50,8 +50,9 @@ class MultiplatformJvmRuntimeTest : AbstractTest() {
}
}

@Test
fun testSimple() = jvmProject {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testSimple(versions: TestVersions) = jvmProject(versions) {
jvmMainFile(
"""
@Service interface SimpleService
Expand All @@ -68,8 +69,9 @@ class MultiplatformJvmRuntimeTest : AbstractTest() {
)
}.runJvmTest()

@Test
fun testMultipleServices() = jvmProject {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testMultipleServices(versions: TestVersions) = jvmProject(versions) {
jvmMainFile(
"""
@Service interface SimpleService
Expand All @@ -87,8 +89,9 @@ class MultiplatformJvmRuntimeTest : AbstractTest() {
)
}.runJvmTest()

@Test
fun testMultipleInheritance() = jvmProject {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testMultipleInheritance(versions: TestVersions) = jvmProject(versions) {
jvmMainFile(
"""
@Service interface SimpleService1
Expand All @@ -111,8 +114,9 @@ class MultiplatformJvmRuntimeTest : AbstractTest() {
)
}.runJvmTest()

@Test
fun testFunction() = jvmProject {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testFunction(versions: TestVersions) = jvmProject(versions) {
jvmMainFile(
"""
@Service interface SimpleService
Expand All @@ -135,8 +139,9 @@ class MultiplatformJvmRuntimeTest : AbstractTest() {
)
}.runJvmTest()

@Test
fun testFunctionErrorPropagation() = jvmProject {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testFunctionErrorPropagation(versions: TestVersions) = jvmProject(versions) {
jvmMainFile(
"""
@Service interface SimpleService
Expand All @@ -155,8 +160,9 @@ class MultiplatformJvmRuntimeTest : AbstractTest() {
)
}.runJvmTest()

@Test
fun testProperty() = jvmProject {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testProperty(versions: TestVersions) = jvmProject(versions) {
jvmMainFile(
"""
@Service interface SimpleService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests.runtime

import dev.whyoleg.sweetspi.tests.*
import kotlin.test.*
import org.junit.jupiter.params.*
import org.junit.jupiter.params.provider.*

class MultiplatformRuntimeTest : AbstractTest() {
override val defaultTemplate: TestTemplate get() = TestTemplate.MULTIPLATFORM
override val defaultVersions: TestVersions get() = TestsArguments.defaultTestVersions

@Test
fun testAllTargets() {
val project = project {
@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
fun testAllTargets(versions: TestVersions) {
val project = project(versions = versions) {

withSweetSpi()
allTargets()
Expand Down

0 comments on commit 71b9694

Please sign in to comment.