Skip to content

Commit

Permalink
CI
Browse files Browse the repository at this point in the history
  • Loading branch information
SalomonBrys committed Nov 21, 2023
1 parent aa0905d commit d2db04e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 70 deletions.
20 changes: 15 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ Built in collaboration with https://www.deezer.com/[Deezer].
[source,kotlin,subs="verbatim,attributes"]
----
plugins {
kotlin("multiplatform")
id("org.kodein.mock.mockmp") version "{version}"
}
kotlin {
// Your Koltin/Multiplatform configuration
}
mockmp {
usesHelper = true
installWorkaround()
}
----

Expand Down Expand Up @@ -672,11 +678,6 @@ repositories {
mavenCentral()
}
mockmp {
// OPTIONAL!
usesHelper = true //<2>
}
kotlin {
jvm()
ios()
Expand All @@ -692,9 +693,18 @@ kotlin {
}
}
}
mockmp {
// OPTIONAL!
usesHelper = true //<2>
// REQUIRED!
installWorkaround() // <3>
}
----
<1> Applying the MocKMP plugin.
<2> Requesting the *optional* `test-helper` dependency
<3> *Must* be called *after* the `kotlin` configuration and *must be the last line* of the `mockmp` block.

The plugin takes care of:

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,89 @@ class MocKMPGradlePlugin : Plugin<Project> {

@Suppress("PropertyName")
class Extension(
private val project: Project,
var usesHelper: Boolean = false,
var throwErrors: Boolean = false,
var public: Boolean = false,
var targetSourceSet: SourceSetTarget = SourceSetTarget.CommonTest_Via_JVM
) {
val CommonMain get() = SourceSetTarget.CommonMain
val CommonTest_Via_JVM get() = SourceSetTarget.CommonTest_Via_JVM
}

private val Project.kotlinExtension get() = project.extensions.findByType<KotlinMultiplatformExtension>() ?: throw GradleException("Could not find Kotlin/Multiplatform plugin")
internal var workaroundInstalled = false

private fun addKSPDependency(project: Project, kspConfiguration: String) {
project.dependencies {
kspConfiguration("org.kodein.mock:mockmp-processor:${BuildConfig.VERSION}")
fun installWorkaround() {
execute(project, this)
workaroundInstalled = true
}
}

private fun addRuntimeDependencies(project: Project, sourceSet: KotlinSourceSet, ext: Extension) {
sourceSet.dependencies {
implementation("org.kodein.mock:mockmp-runtime:${BuildConfig.VERSION}")
if (ext.usesHelper) {
implementation(project.provider {
val isJunit5 = project.tasks.withType<KotlinJvmTest>().any { it.testFramework is JUnitPlatformTestFramework }
if (isJunit5) "org.kodein.mock:mockmp-test-helper-junit5:${BuildConfig.VERSION}"
else "org.kodein.mock:mockmp-test-helper:${BuildConfig.VERSION}"
})
private companion object {
private val Project.kotlinExtension get() = project.extensions.findByType<KotlinMultiplatformExtension>() ?: throw GradleException("Could not find Kotlin/Multiplatform plugin")

private fun addKSPDependency(project: Project, kspConfiguration: String) {
project.dependencies {
kspConfiguration("org.kodein.mock:mockmp-processor:${BuildConfig.VERSION}")
}
}
}

private fun configureKsp(project: Project, ext: Extension) {
val ksp = project.extensions.getByName<com.google.devtools.ksp.gradle.KspExtension>("ksp")
if (ext.throwErrors) {
ksp.arg("org.kodein.mock.errors", "throw")
}
if (ext.public) {
ksp.arg("org.kodein.mock.visibility", "public")
private fun addRuntimeDependencies(project: Project, sourceSet: KotlinSourceSet, ext: Extension) {
sourceSet.dependencies {
implementation("org.kodein.mock:mockmp-runtime:${BuildConfig.VERSION}")
if (ext.usesHelper) {
implementation(project.provider {
val isJunit5 = project.tasks.withType<KotlinJvmTest>().any { it.testFramework is JUnitPlatformTestFramework }
if (isJunit5) "org.kodein.mock:mockmp-test-helper-junit5:${BuildConfig.VERSION}"
else "org.kodein.mock:mockmp-test-helper:${BuildConfig.VERSION}"
})
}
}
}
}

private fun executeOnTests(project: Project, ext: Extension) {
val kotlin = project.kotlinExtension

val jvmTarget = kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.jvm }
?: kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.androidJvm }
?: throw GradleException("Could not find JVM or Android target")

when (jvmTarget.platformType) {
KotlinPlatformType.jvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}Test")
KotlinPlatformType.androidJvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}TestDebug")
else -> error("Unsupported platform type ${jvmTarget.platformType}")
private fun configureKsp(project: Project, ext: Extension) {
val ksp = project.extensions.getByName<com.google.devtools.ksp.gradle.KspExtension>("ksp")
if (ext.throwErrors) {
ksp.arg("org.kodein.mock.errors", "throw")
}
if (ext.public) {
ksp.arg("org.kodein.mock.visibility", "public")
}
}

project.afterEvaluate {
val commonTest = kotlin.sourceSets.getByName("commonTest")
private fun executeOnTests(project: Project, ext: Extension) {
val kotlin = project.kotlinExtension

addRuntimeDependencies(project, commonTest, ext)
val jvmTarget = kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.jvm }
?: kotlin.targets.firstOrNull { it.platformType == KotlinPlatformType.androidJvm }
?: throw GradleException("Could not find JVM or Android target")

// Adding KSP JVM result to COMMON source set
when (jvmTarget.platformType) {
KotlinPlatformType.jvm -> commonTest.kotlin.srcDir("${project.buildDir}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}Test/kotlin")
KotlinPlatformType.androidJvm -> {
commonTest.kotlin.srcDir("${project.buildDir}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}DebugUnitTest/kotlin")
commonTest.kotlin.srcDir("${project.buildDir}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}UnitTestDebug/kotlin")
}
KotlinPlatformType.jvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}Test")
KotlinPlatformType.androidJvm -> addKSPDependency(project, "ksp${jvmTarget.name.replaceFirstChar { it.titlecase() }}TestDebug")
else -> error("Unsupported platform type ${jvmTarget.platformType}")
}

configureKsp(project, ext)
project.afterEvaluate {
val commonTest = kotlin.sourceSets.getByName("commonTest")

addRuntimeDependencies(project, commonTest, ext)

val buildDir = project.layout.buildDirectory.asFile

// Adding KSP JVM result to COMMON source set
when (jvmTarget.platformType) {
KotlinPlatformType.jvm -> {
commonTest.kotlin.srcDir(provider { "${buildDir.get()}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}Test/kotlin" })
}
KotlinPlatformType.androidJvm -> {
commonTest.kotlin.srcDir(provider { "${buildDir.get()}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}DebugUnitTest/kotlin" })
commonTest.kotlin.srcDir(provider { "${buildDir.get()}/generated/ksp/${jvmTarget.name}/${jvmTarget.name}UnitTestDebug/kotlin" })
}
else -> error("Unsupported platform type ${jvmTarget.platformType}")
}

configureKsp(project, ext)

// For some reason, the simple fact of calling `project.tasks.withType<KotlinCompile<*>>()`
// breaks KSP if not set in a deep level of afterEvaluate :(
afterEvaluate { afterEvaluate {
// Adding KSP JVM as a dependency to all Kotlin compilations
project.tasks.withType<KotlinCompile<*>>().all {
if (name.startsWith("compile") && name.contains("TestKotlin")) {
when (jvmTarget.platformType) {
Expand All @@ -108,27 +117,45 @@ class MocKMPGradlePlugin : Plugin<Project> {
}
}
}
} }
}
}

private fun executeOnMain(project: Project, ext: Extension) {
addKSPDependency(project, "kspCommonMainMetadata")
addRuntimeDependencies(project, project.kotlinExtension.sourceSets["commonMain"], ext)
configureKsp(project, ext)
}

private fun execute(project: Project, ext: Extension) {
when (ext.targetSourceSet) {
SourceSetTarget.CommonMain -> executeOnMain(project, ext)
SourceSetTarget.CommonTest_Via_JVM -> executeOnTests(project, ext)
}
}
}

private fun executeOnMain(project: Project, ext: Extension) {
addKSPDependency(project, "kspCommonMainMetadata")
addRuntimeDependencies(project, project.kotlinExtension.sourceSets["commonMain"], ext)
configureKsp(project, ext)
private class MocKMPGradlePluginInstallMissing
: Exception(
"""
Please add the following code AFTER your kotlin AND your optional mockmp configuration block:
mockmp.installWorkaround()
This is now required because of this KSP issue:
https://github.com/google/ksp/issues/1524
You can have a look at the plugin documentation for its usage information:
https://github.com/kosi-libs/MocKMP#with-the-official-plugin
""".trimIndent()) {
}

override fun apply(target: Project) {
target.plugins.apply("com.google.devtools.ksp")

val ext = Extension()
val ext = Extension(target)
target.extensions.add("mockmp", ext)

target.afterEvaluate {
when (ext.targetSourceSet) {
SourceSetTarget.CommonMain -> executeOnMain(target, ext)
SourceSetTarget.CommonTest_Via_JVM -> executeOnTests(target, ext)
}
if (!ext.workaroundInstalled) throw MocKMPGradlePluginInstallMissing()
}
}

Expand Down
11 changes: 8 additions & 3 deletions tests-projects/tests-jvm-junit4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ repositories {
}

kotlin {
applyDefaultHierarchyTemplate()

jvm()
ios()

iosSimulatorArm64()
iosX64()

sourceSets {
val commonMain by getting {
commonMain {
kotlin.srcDir("$rootDir/../../tests/tests-junit4/src/commonMain/kotlin")
dependencies {
implementation(libs.datetime)
}
}
val commonTest by getting {
commonTest {
kotlin.srcDir("$rootDir/../../tests/tests-junit4/src/commonTest/kotlin")
dependencies {
implementation(libs.coroutines.test)
Expand All @@ -36,6 +40,7 @@ kotlin {

mockmp {
usesHelper = true
installWorkaround()
}

// Showing tests in Gradle command line
Expand Down
11 changes: 8 additions & 3 deletions tests-projects/tests-jvm-junit5/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ repositories {
}

kotlin {
applyDefaultHierarchyTemplate()

jvm {
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
ios()

iosSimulatorArm64()
iosX64()

sourceSets {
val commonMain by getting {
commonMain {
kotlin.srcDir("$rootDir/../../tests/tests-junit4/src/commonMain/kotlin")
dependencies {
implementation(libs.datetime)
}
}
val commonTest by getting {
commonTest {
kotlin.srcDir("$rootDir/../../tests/tests-junit4/src/commonTest/kotlin")
dependencies {
implementation(libs.coroutines.test)
Expand All @@ -40,6 +44,7 @@ kotlin {

mockmp {
usesHelper = true
installWorkaround()
}

// Showing tests in Gradle command line
Expand Down

0 comments on commit d2db04e

Please sign in to comment.