tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Mozilla Public License 2.0
642 stars 61 forks source link

Add type for KSP #396

Open Scogun opened 2 months ago

Scogun commented 2 months ago

Hello! First of all, thank you for such useful library! I use it to test my Kotlin Object Multiplatform Mapper. However, after I added plugin support I didn't find a way, how to add mocked plugin inctance for testing. Something like this. Could you provide any advice? Thank you in advance!

tschuchortdev commented 1 month ago

I'm not sure I understand exactly what you want to do. Your KSP SymbolProcessor is loading a KOMMPlugin class via ClassGraph, similar to ServiceLocator. And now you want to pass such a KOMMPlugin class to your test? That means you need your compiled KommPlugin to be on the classpath of the test. If you only have a single mocked KOMMPlugin that is shared between all tests, it will be very easy: just set KotlinCompilation.inheritClasspath = true. If each test should have its own mocked KOMMPlugin, things will be more difficult. Probably what you need to do is to find the .class file of the mocked KOMMPlugin class (you can look in the project output directory or use classgraph and ClassLoader.getResource) and then write your own jar file using java.util.jar.JarOutputStream (see stackoverflow). This jar can then be added to the test via KotlinCompilation.classpaths += jarFile.

Scogun commented 2 weeks ago

Hello @tschuchortdev!

Unfortunately, the second option - it's what I need. But maybe I found third way. Will update the thread if it works.