Skip to content

Commit 357bd3f

Browse files
committed
Updated coverage filter to support file checks
1 parent d08ee17 commit 357bd3f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/main/scala/scoverage/CoverageFilter.scala

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import scala.reflect.internal.util.Position
1111
*/
1212
trait CoverageFilter {
1313
def isClassIncluded(className: String): Boolean
14+
def isFileIncluded(file: SourceFile): Boolean
1415
def isLineIncluded(position: Position): Boolean
1516
def getExcludedLineNumbers(sourceFile: SourceFile): List[Range]
1617
}
@@ -19,18 +20,21 @@ object AllCoverageFilter extends CoverageFilter {
1920
override def getExcludedLineNumbers(sourceFile: SourceFile): List[Range] = Nil
2021
override def isLineIncluded(position: Position): Boolean = true
2122
override def isClassIncluded(className: String): Boolean = true
23+
override def isFileIncluded(file: SourceFile): Boolean = true
2224
}
2325

24-
class RegexCoverageFilter(excludedPackages: Seq[String]) extends CoverageFilter {
26+
class RegexCoverageFilter(excludedPackages: Seq[String],
27+
excludedFiles: Seq[String]) extends CoverageFilter {
2528

2629
val excludedClassNamePatterns = excludedPackages.map(_.r.pattern)
30+
val excludedFilePatterns = excludedFiles.map(_.r.pattern)
31+
2732
/**
2833
* We cache the excluded ranges to avoid scanning the source code files
2934
* repeatedly. For a large project there might be a lot of source code
3035
* data, so we only hold a weak reference.
3136
*/
32-
val linesExcludedByScoverageCommentsCache: mutable.Map[SourceFile, List[Range]] =
33-
mutable.WeakHashMap.empty
37+
val linesExcludedByScoverageCommentsCache: mutable.Map[SourceFile, List[Range]] = mutable.WeakHashMap.empty
3438

3539
final val scoverageExclusionCommentsRegex =
3640
"""(?ms)^\s*//\s*(\$COVERAGE-OFF\$).*?(^\s*//\s*\$COVERAGE-ON\$|\Z)""".r
@@ -39,9 +43,13 @@ class RegexCoverageFilter(excludedPackages: Seq[String]) extends CoverageFilter
3943
* True if the given className has not been excluded by the
4044
* `excludedPackages` option.
4145
*/
42-
def isClassIncluded(className: String): Boolean = {
43-
excludedClassNamePatterns.isEmpty ||
44-
!excludedClassNamePatterns.exists(_.matcher(className).matches)
46+
override def isClassIncluded(className: String): Boolean = {
47+
excludedClassNamePatterns.isEmpty || !excludedClassNamePatterns.exists(_.matcher(className).matches)
48+
}
49+
50+
override def isFileIncluded(file: SourceFile): Boolean = {
51+
def isFileMatch(file:SourceFile) = excludedFilePatterns.exists(_.matcher(file.path).matches)
52+
excludedFilePatterns.isEmpty || !isFileMatch(file)
4553
}
4654

4755
/**

src/main/scala/scoverage/plugin.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ScoveragePlugin(val global: Global) extends Plugin {
2222
for ( opt <- opts ) {
2323
if (opt.startsWith("excludedPackages:")) {
2424
options.excludedPackages = opt.substring("excludedPackages:".length).split(";").map(_.trim).filterNot(_.isEmpty)
25+
} else if (opt.startsWith("excludedFiles:")) {
26+
options.excludedFiles = opt.substring("excludedFiles:".length).split(";").map(_.trim).filterNot(_.isEmpty)
2527
} else if (opt.startsWith("dataDir:")) {
2628
options.dataDir = opt.substring("dataDir:".length)
2729
} else {
@@ -36,13 +38,15 @@ class ScoveragePlugin(val global: Global) extends Plugin {
3638
override val optionsHelp: Option[String] = Some(Seq(
3739
"-P:scoverage:dataDir:<pathtodatadir> where the coverage files should be written\n",
3840
"-P:scoverage:excludedPackages:<regex>;<regex> semicolon separated list of regexs for packages to exclude",
41+
"-P:scoverage:excludedFiles:<regex>;<regex> semicolon separated list of regexs for paths to exclude",
3942
" Any classes whose fully qualified name matches the regex will",
4043
" be excluded from coverage."
4144
).mkString("\n"))
4245
}
4346

4447
class ScoverageOptions {
4548
var excludedPackages: Seq[String] = Nil
49+
var excludedFiles: Seq[String] = Nil
4650
var dataDir: String = File.createTempFile("scoverage_datadir_not_defined", ".tmp").getParent
4751
}
4852

0 commit comments

Comments
 (0)