googlesamples / android-custom-lint-rules

This sample demonstrates how to create a custom lint checks and corresponding lint tests
Apache License 2.0
968 stars 174 forks source link

constructor detection for `com.google.gson.Gson` is working but tests unable to detect #96

Open theapache64 opened 2 months ago

theapache64 commented 2 months ago

Code: Modified avoid AvoidDateDetector.kt

override fun getApplicableConstructorTypes(): List<String> = listOf("com.google.gson.Gson")

  override fun visitConstructor(
    context: JavaContext,
    node: UCallExpression,
    constructor: PsiMethod,
  ) {
    context.report(
      ISSUE,
      node,
      context.getLocation(node),
      "Don't use `Gson`;",
    )
  }

Test

fun testDocumentationExample() {
    lint()
      .files(
        kotlin(
            """
              package test.pkg
              import com.google.gson.Gson
              fun test() {
                val gson = Gson()
              }
              """
          )
          .indented(),
      )
        .allowCompilationErrors()
      .run()
      .expectErrorCount(1)
  }

Result

tnorbye commented 2 months ago

It's likely this: https://googlesamples.github.io/android-custom-lint-rules/api-guide.md.html#frequentlyaskedquestions//mydetectorcallbacksaren'tinvoked

See https://googlesamples.github.io/android-custom-lint-rules/api-guide/unit-testing.md.html#lintcheckunittesting/librarydependenciesandstubs