Skip to content

Commit bcdfbcd

Browse files
committed
fix: Avoid breaking configuration cache by using project directly in task
1 parent 076ba55 commit bcdfbcd

File tree

12 files changed

+180
-154
lines changed

12 files changed

+180
-154
lines changed

plugin/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
implementation("org.apache.avro:avro:1.12.0")
2121

2222
testImplementation(kotlin("test"))
23+
testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.0")
2324
}
2425

2526
gradlePlugin {

plugin/src/main/kotlin/io/github/androa/gradle/plugin/avro/AvroPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AvroPlugin : Plugin<Project> {
1515
project.tasks.register("generateAvro", GenerateAvroTask::class.java) {
1616
it.schemas.convention(extension.schemas)
1717
it.outputDir.convention(extension.outputDir)
18+
it.intermediateDir.convention(project.layout.buildDirectory.dir("intermediates/avro"))
1819

1920
it.encoding.convention(extension.encoding)
2021
it.stringType.convention(extension.stringType)

plugin/src/main/kotlin/io/github/androa/gradle/plugin/avro/GenerateAvroTask.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ abstract class GenerateAvroTask : DefaultTask() {
3232
@get:OutputDirectory
3333
abstract val outputDir: DirectoryProperty
3434

35+
@get:OutputDirectory
36+
abstract val intermediateDir: DirectoryProperty
37+
3538
@get:Input
3639
@get:Optional
3740
abstract val encoding: Property<String>

plugin/src/test/kotlin/io/github/androa/gradle/plugin/avro/AvroPluginFunctionalTest.kt

Lines changed: 71 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -2,137 +2,81 @@ package io.github.androa.gradle.plugin.avro
22

33
import org.gradle.testkit.runner.GradleRunner
44
import org.junit.jupiter.api.Assertions.assertTrue
5-
import org.junit.jupiter.api.Test
65
import org.junit.jupiter.api.io.TempDir
6+
import org.junit.jupiter.params.ParameterizedTest
7+
import org.junit.jupiter.params.provider.ValueSource
78
import java.io.File
9+
import java.nio.file.Files
10+
import java.nio.file.StandardCopyOption
811

912
class AvroPluginFunctionalTest {
1013
@field:TempDir
1114
lateinit var projectDir: File
1215

13-
@Test
14-
fun `plugin generates code successfully`() {
15-
// Create a minimal settings file so that Gradle recognizes the project.
16+
@ParameterizedTest
17+
@ValueSource(
18+
strings = [
19+
"zero-config.gradle.kts",
20+
"default.gradle.kts",
21+
"custom-paths.gradle.kts",
22+
],
23+
)
24+
fun `plugin generates code successfully with different build configs`(buildConfigFile: String) {
25+
// Create a minimal settings file
1626
projectDir.resolve("settings.gradle").writeText("")
1727

18-
// Create a minimal build.gradle.kts that applies the plugin
19-
projectDir.resolve("build.gradle.kts").writeText(
20-
"""
21-
import org.apache.avro.compiler.specific.SpecificCompiler
22-
23-
plugins {
24-
id("io.github.androa.gradle.plugin.avro")
25-
kotlin("jvm") version "2.1.20"
26-
}
27-
28-
generateAvro {
29-
noSetters = true
30-
// Both assign and set() is possible
31-
addNullSafeAnnotations.set(true)
32-
encoding = "UTF-8"
33-
fieldVisibility = SpecificCompiler.FieldVisibility.PRIVATE
34-
35-
//schemas.from(project.fileTree("src/main/avro"))
36-
outputDir.set(layout.buildDirectory.dir("generated-avro"))
37-
}
38-
""".trimIndent(),
39-
)
28+
// Copy the build config from resources
29+
copyResourceTo("build-configs/$buildConfigFile", "build.gradle.kts")
4030

41-
// Create a dummy Avro schema file
42-
projectDir.resolve("src/main/avro/").apply {
43-
mkdirs()
44-
resolve("schema.avsc").writeText(
45-
// language=AvroSchema
46-
"""
47-
{
48-
"namespace": "com.example",
49-
"type": "record",
50-
"name": "Dummy",
51-
"fields": [
52-
{ "name": "id", "type": "int" },
53-
{ "name": "age", "type": [ "null", "int" ] }
54-
]
55-
}
56-
""".trimIndent(),
57-
)
31+
// For the custom paths test, we need to adjust the directory structure
32+
if (buildConfigFile == "custom-paths.gradle.kts") {
33+
setupSchemaFiles("custom-avro-path")
34+
runAndVerify("build/custom-output-dir")
35+
} else {
36+
setupSchemaFiles()
37+
runAndVerify()
5838
}
39+
}
5940

60-
// Create a dummy Avro protocol file
61-
projectDir.resolve("src/main/avro/").apply {
62-
mkdirs()
63-
resolve("protocol.avpr").writeText(
64-
// language=AvroSchema
65-
"""
66-
{
67-
"protocol": "UserService",
68-
"namespace": "com.example.avro",
69-
"types": [
70-
{
71-
"type": "record",
72-
"name": "User",
73-
"fields": [
74-
{
75-
"name": "id",
76-
"type": "string"
77-
},
78-
{
79-
"name": "name",
80-
"type": "string"
81-
},
82-
{
83-
"name": "email",
84-
"type": "string",
85-
"default": ""
86-
}
87-
]
88-
}
89-
],
90-
"messages": {
91-
"getUser": {
92-
"request": [
93-
{
94-
"name": "id",
95-
"type": "string"
96-
}
97-
],
98-
"response": "User"
99-
},
100-
"createUser": {
101-
"request": [
102-
{
103-
"name": "user",
104-
"type": "User"
105-
}
106-
],
107-
"response": "string"
108-
}
109-
}
110-
}
111-
""".trimIndent(),
112-
)
113-
}
41+
/**
42+
* Helper function to copy a resource file to the test project directory
43+
*/
44+
private fun copyResourceTo(
45+
resourcePath: String,
46+
targetPath: String,
47+
) {
48+
val inputStream =
49+
javaClass.classLoader.getResourceAsStream(resourcePath)
50+
?: throw IllegalArgumentException("Resource not found: $resourcePath")
11451

115-
// Create a dummy Avro IDL file
116-
projectDir.resolve("src/main/avro/").apply {
117-
mkdirs()
118-
resolve("protocol.avdl").writeText(
119-
// language=AvroIDL
120-
"""
121-
@namespace("com.example.avro")
122-
protocol RecieptService {
123-
record Receipt {
124-
string id;
125-
string name;
126-
string email = "";
127-
}
52+
val targetFile = projectDir.resolve(targetPath)
53+
targetFile.parentFile.mkdirs()
12854

129-
Receipt getReciept(string id);
130-
string createReciept(Receipt reciept);
131-
}
132-
""".trimIndent(),
133-
)
134-
}
55+
Files.copy(
56+
inputStream,
57+
targetFile.toPath(),
58+
StandardCopyOption.REPLACE_EXISTING,
59+
)
60+
}
61+
62+
/**
63+
* Helper function to set up schema files in the project directory
64+
*/
65+
private fun setupSchemaFiles(targetDir: String = "src/main/avro") {
66+
// Copy AVSC schema
67+
copyResourceTo("schemas/avsc/schema.avsc", "$targetDir/schema.avsc")
13568

69+
// Copy AVPR protocol
70+
copyResourceTo("schemas/avpr/protocol.avpr", "$targetDir/protocol.avpr")
71+
72+
// Copy AVDL file
73+
copyResourceTo("schemas/avdl/protocol.avdl", "$targetDir/protocol.avdl")
74+
}
75+
76+
/**
77+
* Helper function to run the generateAvro task and verify outputs
78+
*/
79+
private fun runAndVerify(outputDir: String = "build/generated/sources/avro") {
13680
// Run the generateAvro task
13781
GradleRunner
13882
.create()
@@ -142,19 +86,22 @@ class AvroPluginFunctionalTest {
14286
.build()
14387

14488
// Verify that the output directory was created and contains generated files
145-
val outputDir = projectDir.resolve("build/generated-avro")
89+
val generatedDir = projectDir.resolve(outputDir)
14690
assertTrue(
147-
outputDir.exists() && outputDir.listFiles()?.isNotEmpty() == true,
148-
"Expected generated Avro code in ${outputDir.absolutePath}",
91+
generatedDir.exists() && generatedDir.listFiles()?.isNotEmpty() == true,
92+
"Expected generated Avro code in ${generatedDir.absolutePath}",
14993
)
15094

151-
with(outputDir.walk().filter { it.extension == "java" }) {
152-
assertTrue(any { it.name == "Dummy.java" }, "Expected Dummy.java in ${outputDir.absolutePath}")
153-
assertTrue(any { it.name == "User.java" }, "Expected User.java in ${outputDir.absolutePath}")
154-
assertTrue(any { it.name == "UserService.java" }, "Expected UserService.java in ${outputDir.absolutePath}")
95+
with(generatedDir.walk().filter { it.extension == "java" }) {
96+
assertTrue(any { it.name == "Dummy.java" }, "Expected Dummy.java in ${generatedDir.absolutePath}")
97+
assertTrue(any { it.name == "User.java" }, "Expected User.java in ${generatedDir.absolutePath}")
98+
assertTrue(
99+
any { it.name == "UserService.java" },
100+
"Expected UserService.java in ${generatedDir.absolutePath}",
101+
)
155102
assertTrue(
156103
any { it.name == "RecieptService.java" },
157-
"Expected RecieptService.java in ${outputDir.absolutePath}",
104+
"Expected RecieptService.java in ${generatedDir.absolutePath}",
158105
)
159106
}
160107
}

plugin/src/test/kotlin/io/github/androa/gradle/plugin/avro/IdlTransformerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class IdlTransformerTest {
1010
@Test
1111
fun `test IDL files are transformed to protocols`() {
1212
val idlTransformer = IdlTransformer()
13-
val inputFiles = File("src/test/resources/idl/idl-schema.avdl")
13+
val inputFiles = File("src/test/resources/schemas/avdl/protocol.avdl")
1414
val outputDir = File("build/intermediates/avro/")
1515

1616
idlTransformer.transformIdl(setOf(inputFiles), outputDir)
1717

1818
// Add assertions to verify the transformation
1919
assertTrue(outputDir.exists())
20-
val outputFile = File(outputDir, "idl-schema.avpr")
20+
val outputFile = File(outputDir, "protocol.avpr")
2121
assertTrue(outputFile.exists(), "Protocol file should be generated")
2222
}
2323

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.apache.avro.compiler.specific.SpecificCompiler
2+
3+
plugins {
4+
id("io.github.androa.gradle.plugin.avro")
5+
kotlin("jvm") version "2.1.20"
6+
}
7+
8+
generateAvro {
9+
noSetters = true
10+
addNullSafeAnnotations.set(false)
11+
encoding = "UTF-8"
12+
fieldVisibility = SpecificCompiler.FieldVisibility.PUBLIC
13+
14+
schemas.from(project.fileTree("custom-avro-path"))
15+
outputDir.set(layout.buildDirectory.dir("custom-output-dir"))
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import org.apache.avro.compiler.specific.SpecificCompiler
2+
3+
plugins {
4+
id("io.github.androa.gradle.plugin.avro")
5+
kotlin("jvm") version "2.1.20"
6+
}
7+
8+
generateAvro {
9+
noSetters = true
10+
// Both assign and set() is possible
11+
addNullSafeAnnotations.set(true)
12+
encoding = "UTF-8"
13+
fieldVisibility = SpecificCompiler.FieldVisibility.PRIVATE
14+
15+
// schemas.from(project.fileTree("src/main/avro"))
16+
// outputDir.set(layout.buildDirectory.dir("generated-avro"))
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugins {
2+
id("io.github.androa.gradle.plugin.avro")
3+
kotlin("jvm") version "2.1.20"
4+
}

plugin/src/test/resources/idl/idl-schema.avdl

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@namespace("com.example.avro")
2+
protocol RecieptService {
3+
record Receipt {
4+
string id;
5+
string name;
6+
string email = "";
7+
}
8+
9+
Receipt getReciept(string id);
10+
string createReciept(Receipt reciept);
11+
}

0 commit comments

Comments
 (0)