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 19, 2025
1 parent 2ed744a commit 70f4967
Show file tree
Hide file tree
Showing 33 changed files with 159 additions and 194 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
61 changes: 12 additions & 49 deletions sweetspi-tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/*
* 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")

import org.jetbrains.kotlin.gradle.*
import sweetbuild.*

plugins {
`jvm-test-suite`
id("sweetbuild.kotlin-base")
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.plugin.powerAssert)
Expand All @@ -22,9 +20,9 @@ kotlin {
}

dependencies {
api(gradleTestKit())
api(kotlin("test-junit5"))
api(libs.junit.params)
testImplementation(gradleTestKit())
testImplementation(kotlin("test-junit5"))
testImplementation(libs.junit.params)
}

// Dev artifacts for tests resolution - inspired by https://github.com/adamko-dev/dev-publish-plugin
Expand All @@ -43,53 +41,21 @@ dependencies {
devArtifacts(projects.sweetspiGradlePlugin)
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
powerAssert {
includedSourceSets.addAll(
"testRuntime",
"testProcessor",
"testPlugin"
tasks.test {
useJUnitPlatform()
jvmArgumentProviders.add(
TestsArgumentProvider(
devArtifactsDirectories = devArtifactsResolver.incoming.files,
testKitDirectory = layout.buildDirectory.dir("test-kit"),
projectVersion = provider { project.version.toString() }
)
)
}

testing.suites {
register<JvmTestSuite>("testRuntime")
register<JvmTestSuite>("testProcessor")
register<JvmTestSuite>("testPlugin")

withType<JvmTestSuite>().configureEach {
useJUnitJupiter()
dependencies {
implementation(project())
}
targets.configureEach {
this.testTask.configure {
jvmArgumentProviders.add(
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() }
)
)
}
}
}
}

tasks.check {
dependsOn(testing.suites)
}

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 +84,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()}",
)
}
44 changes: 0 additions & 44 deletions sweetspi-tests/src/main/kotlin/TestVersions.kt

This file was deleted.

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,27 +10,22 @@ import kotlin.io.path.*

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

private lateinit var temporaryDirectory: Path

@BeforeEach
fun setupTestInfo(testInfo: TestInfo) {
temporaryDirectory = if (System.getenv("CI") != null) {
Files.createTempDirectory("tests")
} else {
Path("build/test-projects")
.resolve(testInfo.testClass.get().name.substringAfter("dev.whyoleg.sweetspi.tests.").replace(".", "/"))
.resolve(testInfo.testMethod.get().name)
.also(Path::deleteRecursively)
}
temporaryDirectory = Path("build/test-projects")
.resolve(testInfo.testClass.get().name.substringAfter("dev.whyoleg.sweetspi.tests.").replace(".", "/"))
.resolve(testInfo.testMethod.get().name)
.also(Path::deleteRecursively)
}

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
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 Down
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 Down
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 Down
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 Down
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 @@ -16,7 +16,13 @@ class TestProject(
.withGradleVersion(versions.gradleVersion)
.withProjectDir(projectDirectory.toFile())
.forwardOutput()
.withArguments("--stacktrace", *arguments)
.withArguments(
"-Dorg.gradle.daemon.idletimeout=10000",
"-Dkotlin.daemon.options=autoshutdownIdleSeconds=10,autoshutdownUnusedSeconds=10",

"--stacktrace",
*arguments
)

val result = when {
expectFailure -> runner.buildAndFail()
Expand Down
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 @@ -11,7 +11,7 @@ class TestTemplate private constructor(path: String) {
val templatePath: Path = templates.resolve(path)

companion object {
private val templates = Path("src/templates")
private val templates = Path("src/test/templates")

val MULTIPLATFORM = TestTemplate("multiplatform")
val MULTIPLATFORM_MULTIMODULE = TestTemplate("multiplatform-multimodule")
Expand Down
68 changes: 68 additions & 0 deletions sweetspi-tests/src/test/kotlin/TestVersions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.whyoleg.sweetspi.tests

import org.junit.jupiter.api.extension.*
import org.junit.jupiter.params.*
import org.junit.jupiter.params.provider.*
import java.util.stream.*
import kotlin.streams.*

data class TestVersions(
val gradleVersion: String,
val kotlinVersion: String,
val kspVersion: String,
)

private const val MIN_GRADLE = "8.0.2"
private const val LATEST_GRADLE = "8.12"
private val KSP_VERSIONS = listOf(
"2.0.0-1.0.24",
"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",
)

abstract class TestVersionsProvider(private val versions: Sequence<TestVersions>) : ArgumentsProvider {
constructor(block: suspend SequenceScope<TestVersions>.() -> Unit) : this(sequence(block))

override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> = versions.map { Arguments.of(it) }.asStream()

class LatestGradle : TestVersionsProvider({
KSP_VERSIONS.forEach { kspVersion ->
yield(
TestVersions(
gradleVersion = LATEST_GRADLE,
kotlinVersion = kspVersion.substringBeforeLast("-"),
kspVersion = kspVersion
)
)
}
})

class All : TestVersionsProvider({
listOf(MIN_GRADLE, LATEST_GRADLE).forEach { gradleVersion ->
KSP_VERSIONS.forEach { kspVersion ->
yield(
TestVersions(
gradleVersion = gradleVersion,
kotlinVersion = kspVersion.substringBeforeLast("-"),
kspVersion = kspVersion
)
)
}
}
})
}

@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.LatestGradle::class)
annotation class FastVersionedTest

@ParameterizedTest
@ArgumentsSource(TestVersionsProvider.All::class)
annotation class FullVersionedTest
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
Loading

0 comments on commit 70f4967

Please sign in to comment.