Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test module metadata #88

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions burst-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ dependencies {
implementation(projects.burst)
implementation(projects.burstKotlinPlugin)
implementation(libs.kotlin.gradlePlugin)
testImplementation(kotlin("compiler-embeddable"))
testImplementation(libs.assertk)
testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.kotlinx.metadata.klib)
}

buildConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ class BurstGradlePluginTest {
multiplatform(
testTaskName = "${platformName}Test",
platformName = platformName,
checkKlibMetadata = true,
)
}

private fun multiplatform(
testTaskName: String,
platformName: String,
checkKlibMetadata: Boolean = false,
) {
val projectDir = File("src/test/projects/multiplatform")

Expand All @@ -81,6 +83,20 @@ class BurstGradlePluginTest {
val sampleSpecialization = testCases.single { it.name == "test_Milk[$platformName]" }
assertThat(sampleSpecialization.skipped).isFalse()
}

if (checkKlibMetadata) {
val klib = readKlib(
projectDir.resolve("lib/build/classes/kotlin/$platformName/test/klib/lib_test"),
)
val klibMetadata = klib.moduleMetadata()
val coffeeTestMetadata = klibMetadata.classes.first { it.name == "CoffeeTest" }
// TODO: This expected output is wrong. It should have specializations like test_Milk etc.
// https://github.com/cashapp/burst/issues/87
assertThat(coffeeTestMetadata.functions.map { it.name }).containsExactlyInAnyOrder(
"setUp",
"test",
)
}
}

@Test
Expand Down
57 changes: 57 additions & 0 deletions burst-gradle-plugin/src/test/kotlin/app/cash/burst/gradle/klib.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2025 Cash App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.burst.gradle

import java.io.File
import kotlin.metadata.KmClass
import kotlinx.metadata.klib.KlibModuleFragmentReadStrategy
import kotlinx.metadata.klib.KlibModuleMetadata
import kotlinx.metadata.klib.KlibModuleMetadata.MetadataLibraryProvider
import org.jetbrains.kotlin.konan.file.File as KonanFile
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.ToolingSingleFileKlibResolveStrategy
import org.jetbrains.kotlin.util.DummyLogger

/**
* Decode a `.klib` file using APIs from the embeddable compiler.
*/
fun readKlib(klib: File): KotlinLibrary =
ToolingSingleFileKlibResolveStrategy.resolve(KonanFile(klib.toPath()), DummyLogger)

/**
* Decode the metadata from a library.
*/
fun KotlinLibrary.moduleMetadata() = KlibModuleMetadata.read(
asMetadataLibraryProvider(),
KlibModuleFragmentReadStrategy.DEFAULT,
)

val KlibModuleMetadata.classes: Sequence<KmClass>
get() = fragments.asSequence().flatMap { it.classes }

private fun KotlinLibrary.asMetadataLibraryProvider(): MetadataLibraryProvider {
val original = this
return object : MetadataLibraryProvider {
override val moduleHeaderData: ByteArray
get() = original.moduleHeaderData

override fun packageMetadata(fqName: String, partName: String) =
original.packageMetadata(fqName, partName)

override fun packageMetadataParts(fqName: String): Set<String> =
original.packageMetadataParts(fqName)
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ junit-vintage-engine = { module = "org.junit.vintage:junit-vintage-engine", vers
kotlin-compile-testing = { module = "dev.zacsweers.kctfork:core", version = "0.7.0" }
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test" }
kotlinx-metadata-klib = { module = "org.jetbrains.kotlinx:kotlinx-metadata-klib", version = "0.0.6" }
mavenPublish-gradle-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.30.0" }

[plugins]
Expand Down