Skip to content

Commit

Permalink
Merge pull request #28 from austinarbor/test-kit-coverage
Browse files Browse the repository at this point in the history
Test Kit Coverage
  • Loading branch information
austinarbor authored Aug 31, 2023
2 parents d6e56f4 + 751e224 commit 54711b0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
assertj = "3.24.2"
detekt = "1.23.1"
gradle-publish = "1.2.1"
jacoco = "0.8.10"
junit = "5.10.0"
kotlin = "1.9.10"
maven = "3.9.4"
Expand All @@ -14,6 +15,7 @@ tomlj = "1.1.0"
[libraries]
assertj = { group = "org.assertj", name = "assertj-core", version.ref = "assertj" }
detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt" }
jacoco-agent = { group = "org.jacoco", name = "org.jacoco.agent", version.ref = "jacoco" }
maven-model = { group = "org.apache.maven", name = "maven-model", version.ref = "maven" }
mockito-core = { group = "org.mockito", name = "mockito-core", version.ref = "mockito" }
mockito-junit-jupiter = { group = "org.mockito", name = "mockito-junit-jupiter", version.ref = "mockito" }
Expand Down
33 changes: 33 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ val PLUGIN_DESCRIPTION: String by project
group = GROUP_ID
version = VERSION

val jacocoRuntime by configurations.creating

repositories {
mavenCentral()
}
Expand All @@ -35,6 +37,14 @@ dependencies {
testImplementation(libs.bundles.testing)
testImplementation(libs.bundles.mockito)
testImplementation(gradleTestKit())
testRuntimeOnly(files("$buildDir/testkit"))
jacocoRuntime(variantOf(libs.jacoco.agent) {
classifier("runtime")
})
}

jacoco {
toolVersion = libs.versions.jacoco.get()
}

spotless {
Expand Down Expand Up @@ -104,8 +114,31 @@ publishing {
}
}

// creates files in build/testkit that we use in
// VersionCatalogGeneratorPluginTest to instrument
// the Testkit runner with jacoco so we get test
// coverage output
val createTestkitFiles by tasks.registering {
val outputDir = file("${buildDir}/testkit")
inputs.files(sourceSets.main.get().runtimeClasspath)
inputs.files(jacocoRuntime)
outputs.dir(outputDir)
doLast {
outputDir.mkdirs()
val jacocoPath = jacocoRuntime.asPath.replace('\\', '/')
file("$outputDir/testkit-classpath.txt")
.writeText(
sourceSets.main.get().runtimeClasspath.joinToString("\n")
)
file("$outputDir/testkit-gradle.properties").writeText(
"org.gradle.jvmargs=-javaagent:${jacocoPath}=destfile=$buildDir/jacoco/test.exec"
)
}
}

tasks {
test {
dependsOn(createTestkitFiles)
finalizedBy(jacocoTestReport) // report is always generated after tests run
}
jacocoTestReport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.aga.gradle.versioncatalogs
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir

Expand All @@ -11,14 +12,34 @@ class VersionCatalogGeneratorPluginTest {

private val buildFile by lazy { projectDir.resolve("build.gradle.kts") }
private val settingsFile by lazy { projectDir.resolve("settings.gradle.kts") }
private val propertiesFile by lazy { projectDir.resolve("gradle.properties") }

private lateinit var classpathString: String

@BeforeEach
fun setup() {
// create the classpath string which we use in the buildscript in the test build file
val classpathFiles = getResourceAsText("testkit-classpath.txt").lines().map { File(it) }

classpathString =
classpathFiles
.map { it.absolutePath.replace('\\', '/') }
.joinToString(",") { "\"$it\"" }
// copy the generated properties file into the runner's directory
propertiesFile.writeText(getResourceAsText("testkit-gradle.properties"))
}

@Test
fun `plugin usage succeeds`() {
// Set up the test build
settingsFile.writeText(
"""
import dev.aga.gradle.versioncatalogs.Generator.generate
buildscript {
dependencies {
classpath(files($classpathString))
}
}
plugins {
id("dev.aga.gradle.version-catalog-generator")
}
Expand Down Expand Up @@ -56,4 +77,10 @@ class VersionCatalogGeneratorPluginTest {

assertThat(result.output).contains("BUILD SUCCESSFUL")
}

companion object {
private fun getResourceAsText(name: String): String {
return javaClass.classLoader.getResource(name).readText()
}
}
}

0 comments on commit 54711b0

Please sign in to comment.