Cagdas Caglak
12/06/2024, 8:32 PMmain
source and generates some kind of runnable unit test code. The problem is, it generates the code inside the generated main source set instead of test one which makes sense because the ksp module is added as ksp
dependency and the annotation is in main
source set. I have a trick for it. I can create generated files wherever I want using basic File
class rather than CodeGenerator
in my processor but it is not the right way because ksp doesn't aware my files. If I clean and build the project, it won't generate them since ksp doesn't have any knowledge about my files.
So I need a way to generate these files into the generated test
source set.
If anyone can share their experiences, I would very much appreciate it. Thank you.glureau
12/09/2024, 8:28 AM@GenerateTestFor([MyClass1::class, MyClass2::class])
), which is better for performances (avoid scanning all classes) but may be tedious to maintain (that's why the 1st option may be handy)
• As you said "main" in your message, I guess that's JVM only, so it's likely you can do something with a bit of reflection, without even needing to generate code. The usage of reflection being in the test folder, you'll be able to scan and execute.
The choice depends on your constraints (numbers of classes to cover, if this list of classes should be manually maintained or automatically, if reflection is good enough or not for the tests you want to write, etc.)Cagdas Caglak
12/12/2024, 9:57 PMpng
).
Since ksp couldn't handle the custom files which is not generated by CodeGenerator
(this is 100% makes sense), I deal with it like that.
• My ksp generates the classes under main
source set but their classpath start from screenshot/kotlin
.
.
├── debug
│ └── kotlin
│ └── screenshot
│ └── kotlin
│ └── com
│ └── screenshot
│ └── generated
│ └── Screen_Test_1733530405871.kt
• I attach the new source set(screenshot/kotlin
) to testDebug
in build.gradle.kts
also.
getByName("testDebug").apply {
kotlin.srcDir("${layout.buildDirectory.get()}/generated/ksp/debug/kotlin/screenshot/kotlin")
}
• Luckily there is no issue for it and if I run the test command it generates class by using CodeGenerator
and run the tests.
There is no performance issue since I have a gradle plugin which hides these implementations behind the some specific command and parameters.