scoverage / scalac-scoverage-plugin

Scoverage Scala Code Coverage Core Libs
https://github.com/scoverage
Apache License 2.0
423 stars 126 forks source link

False positive 100% coverage #426

Open andreisilviudragnea opened 2 years ago

andreisilviudragnea commented 2 years ago

There are cases when coverage is mistakenly reported as 100%, even if it is not true:

package org.example

import com.typesafe.scalalogging.Logger

import java.util.concurrent.CompletableFuture

class Class(logger: Logger) {
  def brokenCoverage1(input: Option[String]): String = {
    input match {
      case Some(value) => value
      case None        => ""
    }
  }

  def brokenCoverage2(input: Option[String]): String = {
    input match {
      case Some(value) => {
        value
      }
      case None => ""
    }
  }

  def brokenCoverage3(): Unit = {
    CompletableFuture.completedFuture("").whenComplete { (_, throwable) =>
      Option(throwable) match {
        case Some(_) => logger.error("Error completing future")
        case None    => ()
      }
    }
  }

  def brokenCoverage4(): Unit = {
    CompletableFuture.completedFuture("").whenComplete { (_, throwable) =>
      Option(throwable) match {
        case Some(_) => {
          logger.error("Error completing future")
        }
        case None => ()
      }
    }
  }

  def brokenCoverage5(input: Option[String]): String = {
    input match {
      case Some(_) => s"something"
      case None    => ""
    }
  }

  def brokenCoverage6(input: Option[String]): String = {
    input match {
      case Some(value) => s"something $value"
      case None        => ""
    }
  }

  def brokenCoverage7(input: Option[String]): String = {
    input match {
      case Some(value) => {
        s"something $value"
      }
      case None => ""
    }
  }
}
package org.example

import com.typesafe.scalalogging.Logger
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should
import org.slf4j.LoggerFactory

class ScoverageSpec extends AnyFunSuite with should.Matchers {

  test("scoverage") {
    val logger: Logger = Logger(LoggerFactory.getLogger("[Gomez]"))
    new Class(logger).brokenCoverage1(None) shouldBe ""
    new Class(logger).brokenCoverage2(None) shouldBe ""
    new Class(logger).brokenCoverage3() shouldBe ()
    new Class(logger).brokenCoverage4() shouldBe ()
    new Class(logger).brokenCoverage5(None) shouldBe ""
    new Class(logger).brokenCoverage6(None) shouldBe ""
    new Class(logger).brokenCoverage7(None) shouldBe ""
  }
}

Running sbt coverage test coverageReport does not fail as it should, but reports 100% coverage. I have reproduced the bug here: https://github.com/andreisilviudragnea/scoverage-bugs

andreisilviudragnea commented 2 years ago

@ckipp01 Is there anyone checking bug reports? This seems like a major bug to me, which affects both V1 and V2.

ckipp01 commented 2 years ago

Hey @andreisilviudragnea, at the moment I'm not actively working on scoverage. The attempt to add Scala 3 support and getting the V2 out was all being done on my free time, and it was taking a pretty large chunk of it that I'd prefer to be used on the other projects I have. However I do still plan to cut releases for new Scala versions and address anything that requires an immediate response.

Potentially over break I'll get back to some of this and try to wrap it all up, look into some of the issue reports, etc, but no promises.

andreisilviudragnea commented 2 years ago

@ckipp01 Is there an active maintainer for scoverage?

I am asking because my team is checking Scala community support for the dependencies in our project, since lack of support affects our usage of Scala language in the future.

ckipp01 commented 2 years ago

@ckipp01 Is there an active maintainer for scoverage?

At the moment I'm really the only one.