Skip to content

Commit

Permalink
Implement test with ensure config (#2)
Browse files Browse the repository at this point in the history
* Implement test with ensure config
 is generated successfully

* Add file content comparison to tests

* Move stdlib dependency to libs.versions.toml

* Revert jvmToolchain version

* Revert jvmToolchain version. Update versions
  • Loading branch information
LimeBeck authored Aug 27, 2023
1 parent f7fd062 commit 12147b0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 10 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.mavenPublish)
alias(libs.plugins.pluginPublish)
alias(libs.plugins.versions)
`java-gradle-plugin`
Expand All @@ -17,7 +16,9 @@ dependencies {
compileOnly(gradleApi())
implementation(libs.kotlin.plugin)
implementation(libs.kotlinpoet)
testImplementation(libs.kotlin.stdlib)
testImplementation(kotlin("test"))
testImplementation(gradleTestKit())
}

tasks.test {
Expand Down
15 changes: 6 additions & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
[versions]
kotlin = "1.7.20"
dokka = "1.7.20"

jvmTarget = "1.8"
kotlin = "1.9.10"
dokka = "1.8.20"

[libraries]
kotlinpoet = { module = "com.squareup:kotlinpoet", version = "1.13.0" }
junit = { module = "junit:junit", version = "4.13.2" }
kotlinpoet = { module = "com.squareup:kotlinpoet", version = "1.14.2" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }

kotlin-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
android-plugin = { module = "com.android.tools.build:gradle", version = "7.2.2" }
Expand All @@ -15,6 +13,5 @@ android-plugin = { module = "com.android.tools.build:gradle", version = "7.2.2"
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
versions = { id = "com.github.ben-manes.versions", version = "0.46.0" }
pluginPublish = { id = "com.gradle.plugin-publish", version = "1.2.0" }
mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.25.1" }
versions = { id = "com.github.ben-manes.versions", version = "0.47.0" }
pluginPublish = { id = "com.gradle.plugin-publish", version = "1.2.1" }
94 changes: 94 additions & 0 deletions src/test/kotlin/PluginTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
import kotlin.io.path.createFile
import kotlin.io.path.writeText
import kotlin.io.path.createDirectories
import kotlin.io.path.readText
import kotlin.test.assertEquals

class PluginTest {

@TempDir
lateinit var testProjectDir: Path

private lateinit var gradleRunner: GradleRunner

@BeforeEach
fun setup() {
gradleRunner = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(testProjectDir.toFile())
.withTestKitDir(testProjectDir.resolve("./testKit").createDirectories().toFile())
}

@Test
fun `Generate build time config`() {
val buildGradleContent = """
plugins {
kotlin("jvm") version "1.8.0"
id("dev.limebeck.build-time-config")
}
buildTimeConfig {
config {
packageName.set("dev.limebeck.config")
objectName.set("MyConfig")
destination.set(project.buildDir)
configProperties {
property<String>("someProp") set "SomeValue"
property<Int>("someProp2") set 123
property<Double>("someProp3") set 123.0
property<Long>("someProp4") set 123L
property<Boolean>("someProp5") set true
obj("nested") set {
property<String>("someProp") set "SomeValue"
}
}
}
}
""".trimIndent()
testProjectDir
.resolve("build.gradle.kts")
.createFile()
.writeText(buildGradleContent)
testProjectDir
.resolve("settings.gradle.kts")
.createFile()
.writeText("rootProject.name = \"build-time-config-test\"")

val codeGenerationResult = gradleRunner.withArguments("generateConfig").build()
assertEquals(TaskOutcome.SUCCESS, codeGenerationResult.task(":generateConfig")!!.outcome)
val resultFile = testProjectDir.resolve("./build/unnamed/MyConfig.kt").readText().trim()
@Language("kotlin") val expectedGeneratedFileContent = """
package dev.limebeck.config
import kotlin.Boolean
import kotlin.Double
import kotlin.Int
import kotlin.Long
import kotlin.String
public object MyConfig {
public val someProp: String = "SomeValue"
public val someProp2: Int = 123
public val someProp3: Double = 123.0
public val someProp4: Long = 123
public val someProp5: Boolean = true
public object nested {
public val someProp: String = "SomeValue"
}
}
""".trimIndent()
assertEquals(expectedGeneratedFileContent, resultFile)
}
}

0 comments on commit 12147b0

Please sign in to comment.