-
Notifications
You must be signed in to change notification settings - Fork 553
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
Fix relevant lines for unloaded files #605
Merged
Merged
Changes from all commits
Commits
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 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 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@rspec | ||
Feature: | ||
|
||
Using the setting `tracked_files` should classify whether lines | ||
are relevant or not (such as whitespace or comments). | ||
|
||
Scenario: | ||
Given SimpleCov for RSpec is configured with: | ||
""" | ||
require 'simplecov' | ||
SimpleCov.start do | ||
track_files "lib/**/*.rb" | ||
end | ||
""" | ||
Given a file named "lib/not_loaded.rb" with: | ||
""" | ||
# A comment line. Plus a whitespace line below: | ||
|
||
# :nocov: | ||
def ignore_me | ||
end | ||
# :nocov: | ||
|
||
def this_is_relevant | ||
puts "still relevant" | ||
end | ||
""" | ||
|
||
When I open the coverage report generated with `bundle exec rspec spec` | ||
Then I follow "lib/not_loaded.rb" | ||
Then I should see "3 relevant lines" within ".highlighted" | ||
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module SimpleCov | ||
# Classifies whether lines are relevant for code coverage analysis. | ||
# Comments & whitespace lines, and :nocov: token blocks, are considered not relevant. | ||
|
||
class LinesClassifier | ||
RELEVANT = 0 | ||
NOT_RELEVANT = nil | ||
|
||
WHITESPACE_LINE = /^\s*$/ | ||
COMMENT_LINE = /^\s*#/ | ||
WHITESPACE_OR_COMMENT_LINE = Regexp.union(WHITESPACE_LINE, COMMENT_LINE) | ||
|
||
def self.no_cov_line | ||
/^(\s*)#(\s*)(\:#{SimpleCov.nocov_token}\:)/ | ||
end | ||
|
||
def classify(lines) | ||
skipping = false | ||
|
||
lines.map do |line| | ||
if line =~ self.class.no_cov_line | ||
skipping = !skipping | ||
NOT_RELEVANT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 using constants! |
||
elsif skipping || line =~ WHITESPACE_OR_COMMENT_LINE | ||
NOT_RELEVANT | ||
else | ||
RELEVANT | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
require "helper" | ||
require "simplecov/lines_classifier" | ||
|
||
describe SimpleCov::LinesClassifier do | ||
describe "#classify" do | ||
describe "relevant lines" do | ||
it "determines code as relevant" do | ||
classified_lines = subject.classify [ | ||
"module Foo", | ||
" class Baz", | ||
" def Bar", | ||
" puts 'hi'", | ||
" end", | ||
" end", | ||
"end", | ||
] | ||
|
||
expect(classified_lines.length).to eq 7 | ||
expect(classified_lines).to all be_relevant | ||
end | ||
end | ||
|
||
describe "not-relevant lines" do | ||
it "determines whitespace is not-relevant" do | ||
classified_lines = subject.classify [ | ||
"", | ||
" ", | ||
"\t\t", | ||
] | ||
|
||
expect(classified_lines.length).to eq 3 | ||
expect(classified_lines).to all be_irrelevant | ||
end | ||
|
||
describe "comments" do | ||
it "determines comments are not-relevant" do | ||
classified_lines = subject.classify [ | ||
"#Comment", | ||
" # Leading space comment", | ||
"\t# Leading tab comment", | ||
] | ||
|
||
expect(classified_lines.length).to eq 3 | ||
expect(classified_lines).to all be_irrelevant | ||
end | ||
|
||
it "doesn't mistake interpolation as a comment" do | ||
classified_lines = subject.classify [ | ||
'puts "#{var}"', | ||
] | ||
|
||
expect(classified_lines.length).to eq 1 | ||
expect(classified_lines).to all be_relevant | ||
end | ||
end | ||
|
||
describe ":nocov: blocks" do | ||
it "determines :nocov: blocks are not-relevant" do | ||
classified_lines = subject.classify [ | ||
"# :nocov:", | ||
"def hi", | ||
"end", | ||
"# :nocov:", | ||
] | ||
|
||
expect(classified_lines.length).to eq 4 | ||
expect(classified_lines).to all be_irrelevant | ||
end | ||
|
||
it "determines all lines after a non-closing :nocov: as not-relevant" do | ||
classified_lines = subject.classify [ | ||
"# :nocov:", | ||
"puts 'Not relevant'", | ||
"# :nocov:", | ||
"puts 'Relevant again'", | ||
"puts 'Still relevant'", | ||
"# :nocov:", | ||
"puts 'Not relevant till the end'", | ||
"puts 'Ditto'", | ||
] | ||
|
||
expect(classified_lines.length).to eq 8 | ||
|
||
expect(classified_lines[0..2]).to all be_irrelevant | ||
expect(classified_lines[3..4]).to all be_relevant | ||
expect(classified_lines[5..7]).to all be_irrelevant | ||
end | ||
end | ||
end | ||
end | ||
|
||
RSpec::Matchers.define :be_relevant do | ||
match do |actual| | ||
actual == SimpleCov::LinesClassifier::RELEVANT | ||
end | ||
end | ||
|
||
RSpec::Matchers.define :be_irrelevant do | ||
match do |actual| | ||
actual == SimpleCov::LinesClassifier::NOT_RELEVANT | ||
end | ||
end | ||
end |
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.
Thank you for adding an integration test! 👍