Skip to content

Commit

Permalink
Merge pull request #218 from splattael/pl-single-line-coverage
Browse files Browse the repository at this point in the history
Fix false positive for single-line code
  • Loading branch information
grodowski authored Dec 23, 2024
2 parents fe3fca2 + 48c0c29 commit 31f232a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/undercover/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ class Result
def initialize(node, file_cov, file_path)
@node = node
@coverage = file_cov.select do |ln, _|
(node.empty_def? ? ln >= first_line : ln > first_line) && ln < last_line
case
when first_line == last_line
ln == first_line
when node.empty_def?
ln >= first_line
else
ln > first_line && ln < last_line
end
end
@file_path = file_path
@flagged = false
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/single_line.lcov
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SF:./single_line.rb
DA:1,1
end_of_record
1 change: 1 addition & 0 deletions spec/fixtures/single_line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[].each {}
15 changes: 15 additions & 0 deletions spec/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,19 @@
expect(result.uncovered?(7)).to be_truthy
end
end

context 'for single-line node covered' do
let(:ast) { Imagen.from_local('spec/fixtures/single_line.rb') }
let(:lcov) do
Undercover::LcovParser.parse('spec/fixtures/single_line.lcov')
end
let(:coverage) { lcov.source_files['single_line.rb'] }

it 'uncovered gives false' do
node = ast.children[0].find_all(->(_) { true }).last
result = described_class.new(node, coverage, 'single_line.rb')

expect(result.uncovered?(1)).to be_falsy
end
end
end

0 comments on commit 31f232a

Please sign in to comment.