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 1 commit
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
9 changes: 8 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,13 @@ 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\.scala;.*\/three\/.*

Note: This only works 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
9 changes: 7 additions & 2 deletions plugin/src/main/scala/scoverage/CoverageFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ object AllCoverageFilter extends CoverageFilter {
class RegexCoverageFilter(
excludedPackages: Seq[String],
excludedFiles: Seq[String],
excludedSymbols: Seq[String]
excludedSymbols: Seq[String],
reporter: scala.tools.nsc.reporters.Reporter

) extends CoverageFilter {
reporter.echo(s"scoverage excludedPackages: ${excludedPackages}")
reporter.echo(s"scoverage excludedFiles: ${excludedFiles}")
reporter.echo(s"scoverage excludedSymbols: ${excludedSymbols}")

val excludedClassNamePatterns = excludedPackages.map(_.r.pattern)
val excludedFilePatterns = excludedFiles.map(_.r.pattern)
Expand All @@ -56,7 +61,7 @@ class RegexCoverageFilter(

override def isFileIncluded(file: SourceFile): Boolean = {
def isFileMatch(file: SourceFile) = excludedFilePatterns.exists(
_.matcher(file.path.replace(".scala", "")).matches
_.matcher(file.path).matches
)
excludedFilePatterns.isEmpty || !isFileMatch(file)
}
Expand Down
9 changes: 5 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,8 @@ 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 +362,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
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