Skip to content

Commit

Permalink
Added compatibility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Dec 25, 2023
1 parent 0e7c06e commit cf07f13
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 75 deletions.
2 changes: 1 addition & 1 deletion demo-project-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ tasks.check {
rootProject.allprojects {
dependsOn(tasks.withType<CodeOwnersReportTask>())
}
}
}
11 changes: 5 additions & 6 deletions plugins/base-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.samWithReceiver)
alias(libs.plugins.gradle.pluginPublish)
`java-test-fixtures`
`plugin-compatibility-test`
`maven-central-publish`
jacoco
}
Expand All @@ -24,8 +26,9 @@ dependencies {
implementation(projects.matcher)
implementation(libs.apache.bcel)

testImplementation(gradleTestKit())
testImplementation(libs.kotlin.test)
testFixturesImplementation(gradleTestKit())
testFixturesApi(platform(libs.junit.bom))
testFixturesApi("org.junit.jupiter:junit-jupiter-params")
}

gradlePlugin {
Expand All @@ -41,10 +44,6 @@ gradlePlugin {
}
}

tasks.test {
workingDir(temporaryDir)
}

// makes sure to publish to mavenCentral first, before doing it to Plugins Portal
tasks.publishPlugins {
mustRunAfter(tasks.publishToSonatype)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.gmazzo.codeowners

class CodeOwnersPluginCompatTest : CodeOwnersBaseCompatTest("io.github.gmazzo.codeowners")
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ abstract class CodeOwnersReportTask : DefaultTask() {
@get:Internal
abstract val sources: ConfigurableFileCollection

@get:InputFiles
@get:IgnoreEmptyDirectories
@get:SkipWhenEmpty
@get:PathSensitive(PathSensitivity.RELATIVE)
internal val sourcesFiles = sources.asFileTree

@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val classes: ConfigurableFileCollection
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.github.gmazzo.codeowners

import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource
import java.io.File

abstract class CodeOwnersBaseCompatTest(
private val pluginId: String,
) {

@Suppress("EnumEntryName")
enum class Kind(val kotlin: Boolean, val android: Boolean) {
alone(false, false),
withKotlin(true, false),
withAndroid(false, true),
withBoth(true, true)
}

@ParameterizedTest(name = "{0}")
@EnumSource(Kind::class)
open fun `plugin can be applied with given classpath`(kind: Kind) {
val rootDir = File(kind.name)
rootDir.deleteRecursively()
rootDir.mkdirs()

val plugins = when (kind) {
Kind.alone -> "java"
Kind.withKotlin -> "id(\"org.jetbrains.kotlin.jvm\")"
Kind.withAndroid -> "id(\"com.android.application\")"
Kind.withBoth -> """
id("com.android.application")
id("org.jetbrains.kotlin.android")
""".trimIndent()
}

val buildFile = File(rootDir, "build.gradle.kts")
buildFile.writeText(
"""
plugins {
$plugins
id("$pluginId")
}
configurations.all {
exclude("io.github.gmazzo.codeowners", "jvm-core")
exclude("io.github.gmazzo.codeowners", "kotlin-core")
}
""".trimIndent()
)

if (kind.android) {
buildFile.appendText(
"""
android {
namespace = "org.test"
compileSdk = 30
defaultConfig {
minSdk = 21
targetSdk = 30
}
}
""".trimIndent()
)

File(rootDir, "src/main/AndroidManifest.xml")
.apply { parentFile.mkdirs() }
.writeText("<manifest/>")
}

File(rootDir, "settings.gradle.kts").writeText(
"""
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
google()
}
}
dependencyResolutionManagement {
repositories {
mavenCentral()
google()
}
}
rootProject.name = "test"
include(":app")
""".trimIndent()
)

File(rootDir, "CODEOWNERS").createNewFile()

val build = GradleRunner.create()
.withProjectDir(rootDir)
.withPluginClasspath("/$kind.classpath")
.withArguments("codeOwnersReport", "-s")
.build()

assertEquals(
if (kind.android) TaskOutcome.SUCCESS else TaskOutcome.NO_SOURCE,
build.task(":codeOwnersReport")?.outcome
)
}

fun GradleRunner.withPluginClasspath(fromResource: String) = apply {
withPluginClasspath(
javaClass.getResource(fromResource)!!.readText()
.splitToSequence(File.pathSeparatorChar)
.map(::File)
.toList()
)
}

}
9 changes: 9 additions & 0 deletions plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ nexusPublishing {
allprojects {
group = "io.github.gmazzo.codeowners"

plugins.withId("jvm-test-suite") {
the<TestingExtension>().suites.withType<JvmTestSuite> {
useKotlinTest(libs.versions.kotlin)
dependencies {
implementation(platform(libs.junit.bom))
}
}
}

plugins.withId("jacoco") {
val jacocoTasks = tasks.withType<JacocoReport>()

Expand Down
14 changes: 10 additions & 4 deletions plugins/buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
plugins {
`kotlin-dsl`
alias(libs.plugins.buildConfig)
}

repositories {
gradlePluginPortal()
}

dependencies {
fun plugin(dep: Provider<PluginDependency>) = with(dep.get()) {
create("$pluginId:$pluginId.gradle.plugin:$version")
}
fun plugin(dep: Provider<PluginDependency>) = dep.map {
"${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version}"
}

dependencies {
implementation(plugin(libs.plugins.buildConfig))
implementation(plugin(libs.plugins.dokka))
}

buildConfig {
buildConfigField("KOTLIN_PLUGIN", plugin(libs.plugins.kotlin.jvm))
buildConfigField("ANDROID_PLUGIN", plugin(libs.plugins.android.application))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import buildSrc.BuildConfig

plugins {
`java-gradle-plugin`
}

val compatibilityTest by testing.suites.registering(JvmTestSuite::class) {
dependencies {
implementation(testFixtures(project(":base-plugin")))
}
targets.configureEach {
testTask.configure {
javaLauncher = javaToolchains.launcherFor { languageVersion = JavaLanguageVersion.of(17) }
workingDir(temporaryDir)
}
}
}

val compatibilityTestSelfPlugin by configurations.creating
val compatibilityTestKotlinPlugin by configurations.creating
val compatibilityTestAndroidPlugin by configurations.creating

dependencies {
compatibilityTestSelfPlugin(project)
compatibilityTestKotlinPlugin(BuildConfig.KOTLIN_PLUGIN)
compatibilityTestAndroidPlugin(BuildConfig.ANDROID_PLUGIN)
}

val collectPluginMetadata by tasks.registering(CollectDependencies::class) {
aloneClasspath.from(compatibilityTestSelfPlugin)
kotlinClasspath.from(compatibilityTestSelfPlugin, compatibilityTestKotlinPlugin)
androidClasspath.from(compatibilityTestSelfPlugin, compatibilityTestAndroidPlugin)
bothClasspath.from(compatibilityTestSelfPlugin, compatibilityTestKotlinPlugin, compatibilityTestAndroidPlugin)
outputDirectory.set(temporaryDir)
}

sourceSets.named(::compatibilityTest.name) {
resources.srcDirs(collectPluginMetadata)
}

tasks.check {
dependsOn(compatibilityTest)
}

@CacheableTask
abstract class CollectDependencies : DefaultTask() {

@get:Classpath
abstract val aloneClasspath: ConfigurableFileCollection

@get:Classpath
abstract val kotlinClasspath: ConfigurableFileCollection

@get:Classpath
abstract val androidClasspath: ConfigurableFileCollection

@get:Classpath
abstract val bothClasspath: ConfigurableFileCollection

@get:OutputDirectory
abstract val outputDirectory: DirectoryProperty

@TaskAction
fun generateResources() {
val outDir = outputDirectory.get().asFile
outDir.deleteRecursively()
outDir.mkdirs()

File(outDir, "alone.classpath").writeText(aloneClasspath.asPath)
File(outDir, "withKotlin.classpath").writeText(kotlinClasspath.asPath)
File(outDir, "withAndroid.classpath").writeText(androidClasspath.asPath)
File(outDir, "withBoth.classpath").writeText(bothClasspath.asPath)
}

}
7 changes: 0 additions & 7 deletions plugins/jvm-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ description = "CodeOwners Library"

java.toolchain.languageVersion.set(JavaLanguageVersion.of(8))

testing.suites.withType<JvmTestSuite> {
useKotlinTest(libs.versions.kotlin)
dependencies {
implementation(platform(libs.junit.bom))
}
}

publishing.publications {
create<MavenPublication>("java") { from(components["java"]) }
}
Expand Down
15 changes: 5 additions & 10 deletions plugins/jvm-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
alias(libs.plugins.kotlin.samWithReceiver)
alias(libs.plugins.gradle.pluginPublish)
com.github.gmazzo.buildconfig
`plugin-compatibility-test`
`maven-central-publish`
jacoco
}
Expand All @@ -28,22 +29,16 @@ dependencies {
implementation(projects.matcher)

testImplementation(gradleKotlinDsl())
testImplementation(testFixtures(projects.basePlugin))

testRuntimeOnly(plugin(libs.plugins.kotlin.jvm))

pluginUnderTestImplementation(plugin(libs.plugins.android.application))
pluginUnderTestImplementation(plugin(libs.plugins.kotlin.jvm))
}

testing.suites.withType<JvmTestSuite> {
useKotlinTest(libs.versions.kotlin)
dependencies {
implementation(platform(libs.junit.bom))
}
targets.all {
testTask {
workingDir(provider { temporaryDir })
}
}
tasks.test {
workingDir(temporaryDir)
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.gmazzo.codeowners

class CodeOwnersJVMPluginCompatTest : CodeOwnersBaseCompatTest("io.github.gmazzo.codeowners.jvm")
Loading

0 comments on commit cf07f13

Please sign in to comment.