-
Notifications
You must be signed in to change notification settings - Fork 127
Add support for coverage exclusion comments #26
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
sksamuel
merged 3 commits into
scoverage:master
from
RichardBradley:add-exclude-comments
Mar 31, 2014
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,76 @@ | ||
package scoverage | ||
|
||
/** @author Stephen Samuel */ | ||
import scala.collection.mutable | ||
import scala.reflect.internal.util.SourceFile | ||
import scala.reflect.internal.util.Position | ||
|
||
/** | ||
* Methods related to filtering the instrumentation and coverage. | ||
* | ||
* @author Stephen Samuel | ||
*/ | ||
class CoverageFilter(excludedPackages: Seq[String]) { | ||
def isIncluded(className: String): Boolean = { | ||
excludedPackages.isEmpty || !excludedPackages.exists(_.r.pattern.matcher(className).matches) | ||
|
||
val excludedClassNamePatterns = excludedPackages.map(_.r.pattern) | ||
/** | ||
* We cache the excluded ranges to avoid scanning the source code files | ||
* repeatedly. For a large project there might be a lot of source code | ||
* data, so we only hold a weak reference. | ||
*/ | ||
val linesExcludedByScoverageCommentsCache: mutable.Map[SourceFile, List[Range]] = | ||
mutable.WeakHashMap.empty | ||
|
||
final val scoverageExclusionCommentsRegex = | ||
"""(?ms)^\s*//\s*(\$COVERAGE-OFF\$).*?(^\s*//\s*\$COVERAGE-ON\$|\Z)""".r | ||
|
||
/** | ||
* True if the given className has not been excluded by the | ||
* `excludedPackages` option. | ||
*/ | ||
def isClassIncluded(className: String): Boolean = { | ||
excludedClassNamePatterns.isEmpty || | ||
!excludedClassNamePatterns.exists(_.matcher(className).matches) | ||
} | ||
|
||
/** | ||
* True if the line containing `position` has not been excluded by a magic comment. | ||
*/ | ||
def isLineIncluded(position: Position): Boolean = { | ||
if (position.isDefined) { | ||
val excludedLineNumbers = getExcludedLineNumbers(position.source) | ||
val lineNumber = position.line | ||
!excludedLineNumbers.exists(_.contains(lineNumber)) | ||
} else { | ||
true | ||
} | ||
} | ||
|
||
/** | ||
* Checks the given sourceFile for any magic comments which exclude lines | ||
* from coverage. Returns a list of Ranges of lines that should be excluded. | ||
* | ||
* The line numbers returned are conventional 1-based line numbers (i.e. the | ||
* first line is line number 1) | ||
*/ | ||
def getExcludedLineNumbers(sourceFile: SourceFile): List[Range] = { | ||
linesExcludedByScoverageCommentsCache.get(sourceFile) match { | ||
case Some(lineNumbers) => lineNumbers | ||
case None => { | ||
val lineNumbers = scoverageExclusionCommentsRegex.findAllIn(sourceFile.content).matchData.map { m => | ||
// Asking a SourceFile for the line number of the char after | ||
// the end of the file gives an exception | ||
val endChar = math.min(m.end(2), sourceFile.content.length - 1) | ||
// Most of the compiler API appears to use conventional | ||
// 1-based line numbers (e.g. "Position.line"), but it appears | ||
// that the "offsetToLine" method in SourceFile uses 0-based | ||
// line numbers | ||
Range( | ||
1 + sourceFile.offsetToLine(m.start(1)), | ||
1 + sourceFile.offsetToLine(endChar)) | ||
}.toList | ||
linesExcludedByScoverageCommentsCache.put(sourceFile, lineNumbers) | ||
lineNumbers | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a bug in the Scala compiler API? It seems like
pos.start
should be OK ifpos.isDefined
, but the old version crashes on my project codebase. Worth raising upstream?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prob not worth raising as its changed slightly in 2.11