Repository: square/leakcanary
File: leakcanary-android-instrumentation/src/main/java/leakcanary/FailTestOnLeakRunListener.kt (Kotlin)

Code before:
  override fun testStarted(description: Description) {
    skipLeakDetectionReason = skipLeakDetectionReason(description)
    if (skipLeakDetectionReason != null) {
      return
    }
    val testClass = description.className
    val testName = description.methodName

    bundle = Bundle()
    bundle.putString(
        Instrumentation.REPORT_KEY_IDENTIFIER, FailTestOnLeakRunListener::class.java.name
    )
    bundle.putString(InstrumentationResultPrinter.REPORT_KEY_NAME_CLASS, testClass)
    bundle.putString(InstrumentationResultPrinter.REPORT_KEY_NAME_TEST, testName)
  }

  /**
   * Can be overridden to skip leak detection based on the description provided when a test
   * is started. Returns null to continue leak detection, or a string describing the reason for
   * skipping otherwise.
   */
  internal open fun skipLeakDetectionReason(description: Description): String? {
    return if (description.getAnnotation(LeakTest::class.java) != null)
      null
    else
      "skipped leak detection"
  }

  override fun testFailure(failure: Failure) {
    skipLeakDetectionReason = "failed"
  }

  override fun testIgnored(description: Description) {
    skipLeakDetectionReason = "was ignored"
  }

  override fun testAssumptionFailure(failure: Failure) {
    skipLeakDetectionReason = "had an assumption failure"
  }

  override fun testFinished(description: Description) {
    detectLeaks()
    LeakSentry.refWatcher.clearWatchedReferences()
  }

  override fun testRunStarted(description: Description) {
    InstrumentationLeakDetector.updateConfig()
  }

  override fun testRunFinished(result: Result) {}


Review comment (bug):
This changes the default behavior of `FailTestOnLeakRunListener`. I'd rather have `FailTestOnLeakRunListener.kt` still behave the same.

One thing you can do is add two new subclasses, e.g.`WhiteListFailTestOnLeakRunListener` (uses `@FailTestOnLeak`) and `BlackListFailTestOnLeakRunListener` (uses `@SkipLeakDetection`)

Code after:
  override fun testStarted(description: Description) {
    skipLeakDetectionReason = skipLeakDetectionReason(description)
    if (skipLeakDetectionReason != null) {
      return
    }
    val testClass = description.className
    val testName = description.methodName

    bundle = Bundle()
    bundle.putString(
        Instrumentation.REPORT_KEY_IDENTIFIER, FailTestOnLeakRunListener::class.java.name
    )
    bundle.putString(InstrumentationResultPrinter.REPORT_KEY_NAME_CLASS, testClass)
    bundle.putString(InstrumentationResultPrinter.REPORT_KEY_NAME_TEST, testName)
  }

  /**
   * Can be overridden to skip leak detection based on the description provided when a test
   * is started. Returns null to continue leak detection, or a string describing the reason for
   * skipping otherwise.
   */
  protected open fun skipLeakDetectionReason(description: Description): String? {
    return null
  }

  override fun testFailure(failure: Failure) {
    skipLeakDetectionReason = "failed"
  }

  override fun testIgnored(description: Description) {
    skipLeakDetectionReason = "was ignored"
  }

  override fun testAssumptionFailure(failure: Failure) {
    skipLeakDetectionReason = "had an assumption failure"
  }

  override fun testFinished(description: Description) {
    detectLeaks()
    LeakSentry.refWatcher.clearWatchedReferences()
  }

  override fun testRunStarted(description: Description) {
    InstrumentationLeakDetector.updateConfig()
  }

  override fun testRunFinished(result: Result) {}

  private fun detectLeaks() {
    if (skipLeakDetectionReason != null) {
      CanaryLog.d("Skipping leak detection because the test %s", skipLeakDetectionReason)