Skip to content
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

Support nocov comment at the end of a line #1068

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions lib/simplecov/lines_classifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ class LinesClassifier
COMMENT_LINE = /^\s*#/.freeze
WHITESPACE_OR_COMMENT_LINE = Regexp.union(WHITESPACE_LINE, COMMENT_LINE)

def self.no_cov_line
def self.no_cov_block
/^(\s*)#(\s*)(:#{SimpleCov.nocov_token}:)/o
end

def self.no_cov_line
/#(\s*)(:#{SimpleCov.nocov_token}:)(\s*)$/o
end

def self.no_cov_line?(line)
no_cov_line.match?(line)
rescue ArgumentError
# E.g., line contains an invalid byte sequence in UTF-8
false
end

def self.no_cov_block?(line)
no_cov_block.match?(line)
rescue ArgumentError
# E.g., line contains an invalid byte sequence in UTF-8
false
end

def self.whitespace_line?(line)
WHITESPACE_OR_COMMENT_LINE.match?(line)
rescue ArgumentError
Expand All @@ -34,10 +45,10 @@ def classify(lines)
skipping = false

lines.map do |line|
if self.class.no_cov_line?(line)
if self.class.no_cov_block?(line)
skipping = !skipping
NOT_RELEVANT
elsif skipping || self.class.whitespace_line?(line)
elsif skipping || self.class.no_cov_line?(line) || self.class.whitespace_line?(line)
NOT_RELEVANT
else
RELEVANT
Expand Down
26 changes: 21 additions & 5 deletions spec/lines_classifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@
end
end

describe ":nocov: one liner" do
it "determines :nocov: lines are not-relevant" do
classified_lines = subject.classify [
"def hi",
"raise NotImplementedError # :nocov:",
"end",
""
]

expect(classified_lines.length).to eq 4
expect(classified_lines[1]).to be_irrelevant
end
end

describe ":nocov: blocks" do
it "determines :nocov: blocks are not-relevant" do
classified_lines = subject.classify [
Expand All @@ -80,21 +94,23 @@

it "determines all lines after a non-closing :nocov: as not-relevant" do
classified_lines = subject.classify [
"puts 'Not relevant' # :nocov:",
"# :nocov:",
"puts 'Not relevant'",
"# :nocov:",
"puts 'Relevant again'",
"puts 'Still relevant'",
"# :nocov:",
"puts 'Not relevant till the end'",
"puts 'Not relevant till the end' # :nocov:",
"puts 'Ditto'"
]

expect(classified_lines.length).to eq 8
expect(classified_lines.length).to eq 9

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
expect(classified_lines[0]).to be_irrelevant
expect(classified_lines[1..3]).to all be_irrelevant
expect(classified_lines[4..5]).to all be_relevant
expect(classified_lines[6..8]).to all be_irrelevant
end
end
end
Expand Down