Question to jacoco experts - we have custom compos...
# code-coverage
e
Question to jacoco experts - we have custom compose preview annotation, but looks like jacoco looks for
Generated
substring in the annotation name. Is it possible to overcome?
m
So you have a custom annotation for Compose that contains "Generated" and you want to avoid Jacoco ignoring those classes, right? According to their wiki "annotation-based-filtering", the annotation used for filtering must have a retention policy of RUNTIME or CLASS. If your compose annotation has SOURCE it won't be interpreted by Jacoco. However, what is the purpose of your custom annotation? If it has to do with generated code, it really should be excluded from coverage.
e
@Matteo Mirk thanks! Actually, name of our annotation doesn't have
Generated
substring.
👍 1
Can you point in jacoco code where it filters based on the annotation? I wonder if annotation should have it name or extending other annotation with that name would be enough.
m
I don't have the exact location in the source where that happens, but if you read the wiki section I've linked, you'll find all the rules that apply to this filtering.
e
Yup, even found the PR with diff
So there is just annotation name operating
I have to add
Generated
to our annotation
That I really don't want
m
Let me warn you about this method: the annotation-based filtering should be applied only to actual generated code. For all the other cases you should use the "excludes" property on the gradle task configuration.
So there is just annotation name operating
not only, the annotation declaration must have retention RUNTIME or CLASS
e
Yeah, but our Preview annotated code is really code that should be removed/not used in production. It is specifics for the Compose usage.
🆗 1
m
ok so I may have misinterpreted your original message: do you want to exclude those compose-related classes? Instead of worrying about the annotation, could you exclude them using an
excludes
glob path, like
com/foo/bar/preview/**
? If you can locate such code under the same package you can exclude it easier without annotations
Alternatively, if the generated classes follow a pattern, that could be intercepted, for example:
**/*Preview.class
(remember Jacoco analyzes bytecode not source)
e
It is not classes but high order functions
m
Ok got it, you could try to exclude the whole class-file anyway and see if it helps. Anyway, a fix for this has been merged to master but it's not released yet. You can try using a snapshot version and see if it works for you.
e
The fix you posted is not about what I am asking 🙂 The fix filter some bytecode generated by compose. I write this preview functions by hand. It is not generated code. But purpose of this code is just nice preview in IDE. And I don't want to write tests for it.
m
So, anything annotated with
@Composable
generates additional bytecode upon compilation, that includes your regular and preview functions. The above fix indeed excludes that generated bytecode from coverage. Again, Jacoco doesn't analyze your source code, only the bytecode, that's why it reports code that you can't (or won't) reach with a test as not covered. You can try it for yourself, either by employing the "excludes" settings I propsed or by using a SNAPSHOT version of Jacoco and check if it works on your coverage. Or you can not trust me and argue here all day, it's your choice.