Hi all! I’ve a doubt on using detekt (:android: )...
# detekt
a
Hi all! I’ve a doubt on using detekt (🤖 ). I’ve a rule that ?ve added a test for it, when running the test the rule complains sucesfully, but later on running
./gradlew deteket
on the real app code doesn’t throw any smell… code in 🧵
`Rule`:
Copy code
class AgreementObjectStubs : Rule() {

  override val issue = Issue(
    javaClass.simpleName,
    Severity.Performance,
    "Stubs (api mock) should be declared as object instead of class.",
    Debt(mins = 1)
  )

  override fun visitClassOrObject(classOrObject: KtClassOrObject) {
    super.visitClassOrObject(classOrObject)

    if (!classOrObject.isStub()) return
    if (classOrObject.isObject()) return

    report(
      CodeSmell(
        issue,
        Entity.from(classOrObject),
        message = "Avoid using class to define Stub. Use object instead"
      )
    )
  }

  private fun KtClassOrObject.isStub(): Boolean = getSuperNames().firstOrNull() == "Stub"
  private fun KtClassOrObject.isObject(): Boolean = getDeclarationKeyword()?.text == "object"
}
`Test`:
Copy code
class AgreementObjectStubsTest {

  private val subject = AgreementObjectStubs()
  @Test
  fun `should complain when using class`() {
    val content = """class MyStub: Stub<MyStubRequest, MyStubResponse>(
      MyStubRequest(), MyStubResponse::class, "My Stub group"
    )
          """

    val findings = subject.lint(content)

    assertThat(findings).hasSize(1)
    assertThat(findings.first().id).isEqualTo("AgreementObjectStubs")
  }

  @Test
  fun `should not complain when using object`() {
    val content = """object MyStub: Stub<MyStubRequest, MyStubResponse>(
      MyStubRequest(), MyStubResponse::class, "My Stub group"
    )
          """

    val findings = subject.lint(content)

    assertThat(findings).hasSize(0)
  }
}
Copy code
class AgreementsRuleSetProvider : RuleSetProvider {

  override val ruleSetId: String = "team-agreements"

  override fun instance(config: Config): RuleSet = RuleSet(
    ruleSetId,
    listOf(
      AgreementObjectStubs()
    )
  )
}
j
Have you added it to the yml?
a
uhm?
we use our custom set provider
not ymals
g
./gradlew deteket
on the real app code doesn’t throw any smell… code in 🧵
How are you integrating your rule? With Gradle? If so, are you using
detektPlugin
?