Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Jan 3, 2025
1 parent 3789a02 commit 98b1b0e
Show file tree
Hide file tree
Showing 37 changed files with 308 additions and 65 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 #v4.1.7
- uses: gradle/actions/setup-gradle@dbbdc275be76ac10734476cc723d82dfe7ec6eda #v3.4.2
- run: |
./gradlew build
./gradlew build
./gradlew -p test-plugin build
./gradlew -p test-plugin-isolated build
./gradlew -p test-app build
./gradlew -p sample-app build
./gradlew -p sample-app prepareIngredients
2 changes: 1 addition & 1 deletion build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ group = "build-logic"

/**
* Ideally would use Runtime.version().feature() but the current Gradle still ships with Kotlin 1.9
* that doesn't know about Java 22 πŸ€·β€β™‚οΈ
* that doesn't know about Java 22
*/
configureJavaCompatibility(17)
16 changes: 16 additions & 0 deletions build-logic/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import com.gradleup.librarian.gradle.librarianModule
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension

fun Project.module() {
librarianModule()

val publishing = extensions.getByName("publishing") as PublishingExtension

publishing.repositories {
it.maven {
it.name = "PluginTest"
it.url = uri(rootDir.resolve("build/m2"))
}
}
}
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ buildscript {
}
}

librarianRoot()
librarianRoot()

tasks.register("publishAllPublicationsToPluginTestRepository") {
subprojects {
dependsOn("$path:publishAllPublicationsToPluginTestRepository")
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
4 changes: 1 addition & 3 deletions gratatouille-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import com.gradleup.librarian.gradle.librarianModule

plugins {
id("org.jetbrains.kotlin.jvm")
id("org.jetbrains.kotlin.plugin.serialization")
}

librarianModule()
module()

dependencies {
implementation(libs.kotlin.test)
Expand Down
4 changes: 1 addition & 3 deletions gratatouille-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import com.gradleup.librarian.gradle.librarianModule

plugins {
id("org.jetbrains.kotlin.jvm")
id("org.jetbrains.kotlin.plugin.serialization")
id("com.github.gmazzo.buildconfig")
id("java-gradle-plugin")
}

librarianModule()
module()

dependencies {
implementation(libs.kotlin.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import javax.inject.Inject

class GratatouilleApiPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.extensions.create("gratatouille", GratatouilleExtension::class.java, target)
target.configurations.create("gratatouille") {
it.isTransitive = false
}
target.withPlugins("org.jetbrains.kotlin.jvm") {
target.withRequiredPlugins("org.jetbrains.kotlin.jvm") {
target.configure()
}
target.configureDefaultVersionsResolutionStrategy()
}

private fun Project.configure() {
Expand Down Expand Up @@ -45,4 +47,4 @@ class GratatouilleApiPlugin : Plugin<Project> {
abstract class ExtractGratatouilleSources: Copy() {
@Inject
abstract fun getArchiveOperations(): ArchiveOperations
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PluginSpec(private val id: String, private val project: Project) {
// From https://github.com/gradle/gradle/blob/master/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L253
project.tasks.named("processResources", Copy::class.java) {
val copyPluginDescriptors: CopySpec = it.rootSpec.addChild()
copyPluginDescriptors.into("META-INF/gradle-plugins")
copyPluginDescriptors.into("/")
copyPluginDescriptors.from(connection.directory)
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ class PluginSpec(private val id: String, private val project: Project) {
*
* @param mainPublication the publication to redirect to.
*/
fun marker(mainPublication: MavenPublication) {
fun mainPublication(mainPublication: MavenPublication) {
project.withRequiredPlugins("maven-publish") {
project.extensions.getByType(PublishingExtension::class.java).apply {
// https://github.com/gradle/gradle/blob/master/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/MavenPluginPublishPlugin.java#L88
Expand Down Expand Up @@ -82,9 +82,25 @@ class PluginSpec(private val id: String, private val project: Project) {
}
}
}

fun mainPublication(mainPublication: String) {
project.withRequiredPlugins("maven-publish") {
project.extensions.getByType(PublishingExtension::class.java).apply {
val publication = publications.findByName(mainPublication)
check(publication != null) {
"Gratatouille: cannot find publication named '$mainPublication'"
}
check(publication is MavenPublication) {
"Gratatouille: publication '$mainPublication' is not an instance of MavenPublication"
}
mainPublication(publication)
}
}
}
}

private fun String.displayName() = this.split(".").joinToString(separator = "") { it.replaceFirstChar { it.uppercase() } }
private fun String.displayName() =
this.split(".").joinToString(separator = "") { it.replaceFirstChar { it.uppercase() } }

abstract class GratatouilleExtension(private val project: Project) {
fun plugin(id: String, action: Action<PluginSpec>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import org.gradle.api.Project

class GratatouilleImplementationPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.withPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") {
target.withRequiredPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") {
target.configurations.getByName("ksp").dependencies.add(
target.dependencies.create("${BuildConfig.group}:gratatouille-processor:${BuildConfig.version}")
target.dependencies.create("${BuildConfig.group}:gratatouille-processor")
)
target.configurations.getByName("implementation").dependencies.add(
target.dependencies.create("${BuildConfig.group}:gratatouille-core:${BuildConfig.version}")
target.dependencies.create("${BuildConfig.group}:gratatouille-core")
)


target.afterEvaluate {
target.extensions.getByName("ksp").apply {
this as KspExtension
this.arg("implementationCoordinates", "${target.group}:${target.name}:${target.version}")
}
}
}
target.configureDefaultVersionsResolutionStrategy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import org.gradle.api.Project

class GratatouillePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.withPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") {
target.extensions.create("gratatouille", GratatouilleExtension::class.java, target)
target.withRequiredPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") {
target.configurations.getByName("ksp").dependencies.add(
target.dependencies.create("${BuildConfig.group}:gratatouille-processor:${BuildConfig.version}")
target.dependencies.create("${BuildConfig.group}:gratatouille-processor")
)
target.configurations.getByName("implementation").dependencies.add(
target.dependencies.create("${BuildConfig.group}:gratatouille-core:${BuildConfig.version}")
target.dependencies.create("${BuildConfig.group}:gratatouille-core")
)
}
target.configureDefaultVersionsResolutionStrategy()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gratatouille.gradle
import com.gradleup.gratatouille.gradle.BuildConfig
import org.gradle.api.Project
import org.gradle.api.artifacts.ExternalDependency
import org.gradle.api.artifacts.component.ModuleComponentSelector


internal fun Project.withRequiredPlugins(vararg ids: String, block: () -> Unit) {
Expand All @@ -26,7 +27,6 @@ internal fun Project.withRequiredPlugins(vararg ids: String, block: () -> Unit)

internal fun Project.configureDefaultVersionsResolutionStrategy() {
configurations.configureEach { configuration ->
// Use the API introduced in Gradle 4.4 to modify the dependencies directly before they are resolved:
configuration.withDependencies { dependencySet ->
val pluginVersion = VERSION
dependencySet.filterIsInstance<ExternalDependency>()
Expand Down
4 changes: 1 addition & 3 deletions gratatouille-processor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import com.gradleup.librarian.gradle.librarianModule

plugins {
id("org.jetbrains.kotlin.jvm")
}

librarianModule()
module()

dependencies {
implementation(libs.kotlin.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.ksp.toTypeName
import com.sun.org.apache.xpath.internal.operations.Bool

internal class GTaskAction(
val packageName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ internal fun GTaskAction.taskFile(): FileSpec {

val fileSpec = FileSpec.builder(className)
.addFunction(register())
.apply {
if (!pure) {
addBodyComment("This task is impure and not cacheable")
}
}
.addType(task())
.addType(workParameters())
.addType(workAction())
Expand Down Expand Up @@ -146,7 +141,8 @@ private fun GTaskAction.task(): TypeSpec {
.build()
)
} else {
addFunction(FunSpec.builder("init").addCode("outputs.upToDateWhen { false }").build())
addKdoc("This task is impure and not cacheable")
addInitializerBlock(CodeBlock.of("outputs.upToDateWhen { false }\n"))
}
}
.addModifiers(KModifier.ABSTRACT, KModifier.INTERNAL)
Expand Down
2 changes: 1 addition & 1 deletion librarian.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
java.compatibility=8
java.compatibility=11
kotlin.compatibility=2.0.0

kdoc.olderVersions=
Expand Down
5 changes: 3 additions & 2 deletions libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
ksp = "2.0.20-1.0.24"
kotlin = "2.0.20"
ksp = "2.0.21-1.0.26"
kotlin = "2.0.21"
coroutines = "1.8.0"
kotlinpoet = "1.14.2"
librarian = "0.0.6"
Expand All @@ -22,5 +22,6 @@ librarian = { module = "com.gradleup.librarian:librarian-gradle-plugin", version
okio = "com.squareup.okio:okio:3.9.0"
cast = "net.mbonnin.cast:cast:0.0.1"


[plugins]
librarian = { id = "com.gradleup.librarian", version.ref = "librarian"}
22 changes: 17 additions & 5 deletions test-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import testplugin.TestExtension
import testplugin.isolated.TestExtensionIsolated
import kotlin.test.assertFailsWith

plugins {
id("testplugin")
id("testplugin.isolated")
buildscript {
dependencies {
classpath("test-plugin:test-plugin")
classpath("test-plugin-isolated:api")
}
repositories {
mavenCentral()
}
}

testExtension {
plugins.apply("testplugin")
plugins.apply("testplugin.isolated")

extensions.getByName("testExtension").apply {
this as TestExtension
stringInput.set("input")
internalInput.set("internalInput")
//optionalInput.set()
Expand All @@ -26,7 +37,8 @@ testExtension {
fileOutput3.set(file("build/output"))
}

testExtensionIsolated {
extensions.getByName("testExtensionIsolated").apply {
this as TestExtensionIsolated
stringInput.set("world")
}

Expand Down
1 change: 1 addition & 0 deletions test-app/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pluginManagement {
it.apply {
mavenCentral()
google()
maven("../build/m2")
}
}
}
Expand Down
41 changes: 34 additions & 7 deletions test-plugin-isolated/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,45 @@ plugins {
id("org.jetbrains.kotlin.plugin.serialization")
id("com.google.devtools.ksp")
id("com.gradleup.gratatouille.api")
id("java-gradle-plugin")
id("maven-publish")
}

version = "0.0.0"

publishing {
publications {
create("default", MavenPublication::class.java) {
from(components["java"])
}
}
repositories {
maven {
name = "pluginTest"
url = uri(rootDir.resolve("../build/m2"))
}
}
}

dependencies {
gratatouille(project(":implementation"))
}

gradlePlugin {
plugins {
create("testplugin.isolated") {
this.implementationClass = "testplugin.isolated.TestPluginIsolated"
this.id = "testplugin.isolated"
}
dependencies {
compileOnly(libs.gradle.api)
testImplementation(gradleTestKit())
testImplementation(libs.kotlin.test)
}


gratatouille {
plugin("testplugin.isolated") {
implementationClass("testplugin.isolated.TestPluginIsolated")
mainPublication("default")
}
}

tasks.withType(Test::class.java) {
dependsOn("publishAllPublicationsToPluginTestRepository")
dependsOn(":implementation:publishAllPublicationsToPluginTestRepository")
dependsOn(gradle.includedBuild("gratatouille").task(":publishAllPublicationsToPluginTestRepository"))
}
Loading

0 comments on commit 98b1b0e

Please sign in to comment.