Skip to content

Refresh the excluded implementation #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Search for scalac-scoverage-plugin.
#### Excluding code from coverage stats

You can exclude whole classes or packages by name. Pass a semicolon separated
list of regexes to the 'excludedPackages' option.
list of regexes to the `excludedPackages` option.

For example:

Expand All @@ -98,6 +98,15 @@ The regular expressions are matched against the fully qualified class name, and

Any matched classes will not be instrumented or included in the coverage report.

You can also exclude files from being considered for instrumentation.

-P:scoverage:excludedFiles:.*\/two\/GoodCoverage;.*\/three\/.*

Note: The `.scala` file extension needs to be omitted from the filename, if one is given.

Note: These two options only work for Scala2. Right now Scala3 does not support
a way to exclude packages or files from being instrumented.

You can also mark sections of code with comments like:

// $COVERAGE-OFF$
Expand Down
10 changes: 9 additions & 1 deletion plugin/src/main/scala/scoverage/CoverageFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scoverage
import scala.collection.mutable
import scala.reflect.internal.util.Position
import scala.reflect.internal.util.SourceFile
import scala.tools.nsc.reporters.Reporter
import scala.util.matching.Regex

/** Methods related to filtering the instrumentation and coverage.
Expand All @@ -28,8 +29,15 @@ object AllCoverageFilter extends CoverageFilter {
class RegexCoverageFilter(
excludedPackages: Seq[String],
excludedFiles: Seq[String],
excludedSymbols: Seq[String]
excludedSymbols: Seq[String],
reporter: Reporter
) extends CoverageFilter {
if (excludedPackages.nonEmpty)
reporter.echo(s"scoverage excludedPackages: ${excludedPackages}")
if (excludedFiles.nonEmpty)
reporter.echo(s"scoverage excludedFiles: ${excludedFiles}")
if (excludedSymbols.nonEmpty)
reporter.echo(s"scoverage excludedSymbols: ${excludedSymbols}")

val excludedClassNamePatterns = excludedPackages.map(_.r.pattern)
val excludedFilePatterns = excludedFiles.map(_.r.pattern)
Expand Down
10 changes: 6 additions & 4 deletions plugin/src/main/scala/scoverage/ScoveragePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class ScoverageInstrumentationComponent(
coverageFilter = new RegexCoverageFilter(
options.excludedPackages,
options.excludedFiles,
options.excludedSymbols
options.excludedSymbols,
reporter
)
new File(options.dataDir).mkdirs() // ensure data directory is created
}
Expand Down Expand Up @@ -230,8 +231,9 @@ class ScoverageInstrumentationComponent(
): Tree = {
safeSource(tree) match {
case None =>
reporter.echo(
s"[warn] Could not instrument [${tree.getClass.getSimpleName}/${tree.symbol}]. No pos."
reporter.warning(
NoPosition,
s"Could not instrument [${tree.getClass.getSimpleName}/${tree.symbol}]."
)
tree
case Some(source) =>
Expand Down Expand Up @@ -361,7 +363,7 @@ class ScoverageInstrumentationComponent(
Location.fromGlobal(global)(t) match {
case Some(loc) => this.location = loc
case _ =>
reporter.warning(t.pos, s"[warn] Cannot update location for $t")
reporter.warning(t.pos, s"Cannot update location for $t")
}
}

Expand Down
62 changes: 37 additions & 25 deletions plugin/src/test/scala/scoverage/RegexCoverageFilterTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,56 @@ import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.internal.util.NoFile
import scala.reflect.internal.util.SourceFile
import scala.reflect.io.VirtualFile
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.Settings

import munit.FunSuite

class RegexCoverageFilterTest extends FunSuite {

val reporter = new ConsoleReporter(new Settings())

test("isClassIncluded should return true for empty excludes") {
assert(new RegexCoverageFilter(Nil, Nil, Nil).isClassIncluded("x"))
assert(
new RegexCoverageFilter(Nil, Nil, Nil, reporter).isClassIncluded("x")
)
}

test("should not crash for empty input") {
assert(new RegexCoverageFilter(Nil, Nil, Nil).isClassIncluded(""))
assert(new RegexCoverageFilter(Nil, Nil, Nil, reporter).isClassIncluded(""))
}

test("should exclude scoverage -> scoverage") {
assert(
!new RegexCoverageFilter(Seq("scoverage"), Nil, Nil)
!new RegexCoverageFilter(Seq("scoverage"), Nil, Nil, reporter)
.isClassIncluded("scoverage")
)
}

test("should include scoverage -> scoverageeee") {
assert(
new RegexCoverageFilter(Seq("scoverage"), Nil, Nil)
new RegexCoverageFilter(Seq("scoverage"), Nil, Nil, reporter)
.isClassIncluded("scoverageeee")
)
}

test("should exclude scoverage* -> scoverageeee") {
assert(
!new RegexCoverageFilter(Seq("scoverage*"), Nil, Nil)
!new RegexCoverageFilter(Seq("scoverage*"), Nil, Nil, reporter)
.isClassIncluded("scoverageeee")
)
}

test("should include eee -> scoverageeee") {
assert(
new RegexCoverageFilter(Seq("eee"), Nil, Nil)
new RegexCoverageFilter(Seq("eee"), Nil, Nil, reporter)
.isClassIncluded("scoverageeee")
)
}

test("should exclude .*eee -> scoverageeee") {
assert(
!new RegexCoverageFilter(Seq(".*eee"), Nil, Nil)
!new RegexCoverageFilter(Seq(".*eee"), Nil, Nil, reporter)
.isClassIncluded("scoverageeee")
)
}
Expand All @@ -56,91 +62,97 @@ class RegexCoverageFilterTest extends FunSuite {

test("isFileIncluded should return true for empty excludes") {
val file = new BatchSourceFile(abstractFile, Array.emptyCharArray)
assert(new RegexCoverageFilter(Nil, Nil, Nil).isFileIncluded(file))
assert(
new RegexCoverageFilter(Nil, Nil, Nil, reporter).isFileIncluded(file)
)
}

test("should exclude by filename") {
val file = new BatchSourceFile(abstractFile, Array.emptyCharArray)
assert(
!new RegexCoverageFilter(Nil, Seq("sammy"), Nil)
!new RegexCoverageFilter(Nil, Seq("sammy"), Nil, reporter)
.isFileIncluded(file)
)
}

test("should exclude by regex wildcard") {
val file = new BatchSourceFile(abstractFile, Array.emptyCharArray)
assert(
!new RegexCoverageFilter(Nil, Seq("sam.*"), Nil)
!new RegexCoverageFilter(Nil, Seq("sam.*"), Nil, reporter)
.isFileIncluded(file)
)
}

test("should not exclude non matching regex") {
val file = new BatchSourceFile(abstractFile, Array.emptyCharArray)
assert(
new RegexCoverageFilter(Nil, Seq("qweqeqwe"), Nil)
new RegexCoverageFilter(Nil, Seq("qweqeqwe"), Nil, reporter)
.isFileIncluded(file)
)
}

val options = ScoverageOptions.default()

test("isSymbolIncluded should return true for empty excludes") {
assert(new RegexCoverageFilter(Nil, Nil, Nil).isSymbolIncluded("x"))
assert(
new RegexCoverageFilter(Nil, Nil, Nil, reporter).isSymbolIncluded("x")
)
}

test("should not crash for empty input") {
assert(new RegexCoverageFilter(Nil, Nil, Nil).isSymbolIncluded(""))
assert(
new RegexCoverageFilter(Nil, Nil, Nil, reporter).isSymbolIncluded("")
)
}

test("should exclude scoverage -> scoverage") {
assert(
!new RegexCoverageFilter(Nil, Nil, Seq("scoverage"))
!new RegexCoverageFilter(Nil, Nil, Seq("scoverage"), reporter)
.isSymbolIncluded("scoverage")
)
}

test("should include scoverage -> scoverageeee") {
assert(
new RegexCoverageFilter(Nil, Nil, Seq("scoverage"))
new RegexCoverageFilter(Nil, Nil, Seq("scoverage"), reporter)
.isSymbolIncluded("scoverageeee")
)
}
test("should exclude scoverage* -> scoverageeee") {
assert(
!new RegexCoverageFilter(Nil, Nil, Seq("scoverage*"))
!new RegexCoverageFilter(Nil, Nil, Seq("scoverage*"), reporter)
.isSymbolIncluded("scoverageeee")
)
}

test("should include eee -> scoverageeee") {
assert(
new RegexCoverageFilter(Nil, Nil, Seq("eee"))
new RegexCoverageFilter(Nil, Nil, Seq("eee"), reporter)
.isSymbolIncluded("scoverageeee")
)
}

test("should exclude .*eee -> scoverageeee") {
assert(
!new RegexCoverageFilter(Nil, Nil, Seq(".*eee"))
!new RegexCoverageFilter(Nil, Nil, Seq(".*eee"), reporter)
.isSymbolIncluded("scoverageeee")
)
}
test("should exclude scala.reflect.api.Exprs.Expr") {
assert(
!new RegexCoverageFilter(Nil, Nil, options.excludedSymbols)
!new RegexCoverageFilter(Nil, Nil, options.excludedSymbols, reporter)
.isSymbolIncluded("scala.reflect.api.Exprs.Expr")
)
}
test("should exclude scala.reflect.macros.Universe.Tree") {
assert(
!new RegexCoverageFilter(Nil, Nil, options.excludedSymbols)
!new RegexCoverageFilter(Nil, Nil, options.excludedSymbols, reporter)
.isSymbolIncluded("scala.reflect.macros.Universe.Tree")
)
}
test("should exclude scala.reflect.api.Trees.Tree") {
assert(
!new RegexCoverageFilter(Nil, Nil, options.excludedSymbols)
!new RegexCoverageFilter(Nil, Nil, options.excludedSymbols, reporter)
.isSymbolIncluded("scala.reflect.api.Trees.Tree")
)
}
Expand All @@ -158,7 +170,7 @@ class RegexCoverageFilterTest extends FunSuite {
|8
""".stripMargin

val numbers = new RegexCoverageFilter(Nil, Nil, Nil)
val numbers = new RegexCoverageFilter(Nil, Nil, Nil, reporter)
.getExcludedLineNumbers(mockSourceFile(file))
assertEquals(numbers, List.empty)
}
Expand All @@ -182,7 +194,7 @@ class RegexCoverageFilterTest extends FunSuite {
|16
""".stripMargin

val numbers = new RegexCoverageFilter(Nil, Nil, Nil)
val numbers = new RegexCoverageFilter(Nil, Nil, Nil, reporter)
.getExcludedLineNumbers(mockSourceFile(file))
assertEquals(numbers, List(Range(4, 9), Range(12, 14)))
}
Expand All @@ -205,7 +217,7 @@ class RegexCoverageFilterTest extends FunSuite {
|15
""".stripMargin

val numbers = new RegexCoverageFilter(Nil, Nil, Nil)
val numbers = new RegexCoverageFilter(Nil, Nil, Nil, reporter)
.getExcludedLineNumbers(mockSourceFile(file))
assertEquals(numbers, List(Range(4, 9), Range(12, 16)))
}
Expand All @@ -228,7 +240,7 @@ class RegexCoverageFilterTest extends FunSuite {
|15
""".stripMargin

val numbers = new RegexCoverageFilter(Nil, Nil, Nil)
val numbers = new RegexCoverageFilter(Nil, Nil, Nil, reporter)
.getExcludedLineNumbers(mockSourceFile(file))
assertEquals(numbers, List(Range(4, 9), Range(12, 16)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.util.UUID

import munit.FunSuite
import scoverage.domain.Constants
import scoverage.reporter.IOUtils

/** @author Stephen Samuel */
class IOUtilsTest extends FunSuite {
Expand Down