Is it possible to test custom Detekt rules with ty...
# detekt
m
Is it possible to test custom Detekt rules with type resolution that target Android APIs? For example, I’d like to write a rule that flags when Jetpack Compose’s
setContent
function is called directly instead of a custom
setFooContent { }
which takes care of theming. However when it comes to testing the rule, how does one include the class path of the Compose environment when the custom rule project is a pure Kotlin library?
b
I don't think so. Or at least I don't know any easy way to do it. I assume that you could create the custom rule and then create an android module that uses it just to test it. Other option would be to make the module that contains the custom rule an android module but hack gradle someway to be able to get a
jar
instead of an
aar
. And be sure that you don't use any android reference in your production code.
m
Hmm… okay. Thank you for the reply. Perhaps this rule is better suited for Android Lint then. The rule itself actually works against the Android modules. It’s just that writing tests for it has its limitations since the tests cannot be configured to run with type resolution properly.
s
One workaround might be to kind of mock the APIs you need just for the sake of testing the rule even if there are slight differences. The feasibility of this approach depends of course on the complexity of the API as well as detekt custom rule itself.
m
Thanks @schalkms. I guess one approach would be to define a stub copy of the actual “banned” function under the same package as the real one, within the custom rule module.
Ultimately, I’m looking for a custom rule that flags usages of
androidx.activity.compose.setContent
and suggests that
com.foo.bar.setFooBarView
(hypothetically speaking) be used instead.