Skip to content
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
13 changes: 12 additions & 1 deletion lib/simplecov/lines_classifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ def self.no_cov_line
/^(\s*)#(\s*)(:#{SimpleCov.nocov_token}:)/o
end

def self.no_cov_inline
/\S.*#\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_inline?(line)
no_cov_inline.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 @@ -37,7 +48,7 @@ def classify(lines)
if self.class.no_cov_line?(line)
skipping = !skipping
NOT_RELEVANT
elsif skipping || self.class.whitespace_line?(line)
elsif skipping || self.class.no_cov_inline?(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:",
Copy link
Copy Markdown
Collaborator

@sferik sferik Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a separate, focused test for interactions between inline markers and blocks, instead of making this (already complex) test more complex. There are also a few other edge cases worth testing (e.g. inline # :nocov: comment inside a block).

"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