Skip to content

Commit

Permalink
Report on changed files outside of coverage reports
Browse files Browse the repository at this point in the history
Previously, a file from Changeset but not in LcovParser#coverage would not
appear as flagged. This changes the behaviour, so that more untested files
are reported.
  • Loading branch information
grodowski committed Aug 24, 2024
1 parent 82d12e2 commit e01d298
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Metrics/BlockLength:
Exclude:
- spec/**/*

Metrics/CyclomaticComplexity:
Max: 10

Metrics/PerceivedComplexity:
Max: 10

Style/HashEachMethods:
Enabled: true

Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 3.3.0
ruby 3.3.3
5 changes: 2 additions & 3 deletions lib/undercover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(changeset, opts)
@results = {}
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def build
changeset.each_changed_line do |filepath, line_no|
dist_from_line_no = lambda do |res|
Expand All @@ -61,7 +61,7 @@ def build
end
self
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize

def build_warnings
warn('Undercover::Report#build_warnings is deprecated! ' \
Expand Down Expand Up @@ -92,7 +92,6 @@ def load_and_parse_file(filepath)
return if loaded_files[key]

coverage = lcov.coverage(filepath)
return if coverage.empty?

root_ast = Imagen::Node::Root.new.build_from_file(
File.join(code_dir, filepath)
Expand Down
8 changes: 6 additions & 2 deletions lib/undercover/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def flagged?
@flagged
end

# rubocop:disable Metrics/CyclomaticComplexity
def uncovered?(line_no)
return true if coverage.empty?

# check branch coverage for line_no
coverage.each do |ln, _block, _branch, cov|
return true if ln == line_no && cov && cov.zero?
Expand All @@ -38,13 +39,16 @@ def uncovered?(line_no)
line_cov = coverage.select { |cov| cov.size == 2 }.find { |ln, _cov| ln == line_no }
line_cov && line_cov[1].zero?
end
# rubocop:enable Metrics/CyclomaticComplexity

# Method `coverage_f` returns the total coverage of this Undercover::Result
# as a % value, taking into account missing branches. Line coverage will be counted
# as 0 if any branch is untested.
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def coverage_f
if coverage.empty?
return node.children.empty? ? 1.0 : 0.0
end

lines = {}
coverage.each do |ln, block_or_line_cov, _, branch_cov|
lines[ln] = 1 unless lines.key?(ln)
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/test_empty.lcov
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SF:./program_xyz.rb
DA:2,1
DA:3,1
DA:4,1
end_of_record
12 changes: 12 additions & 0 deletions spec/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,17 @@
expect(warnings[1].file_path).to eq('test_two_patches.rb')
expect(warnings[1].first_line).to eq(15)
end

it 'reports changed files that were not in the lcov report' do
options.lcov = 'spec/fixtures/test_empty.lcov'
report.build
warnings = report.build_warnings.to_a
expect(warnings.size).to eq(2)

expect(warnings[0].file_path).to eq('test_two_patches.rb')
expect(warnings[0].first_line).to eq(3)
expect(warnings[1].file_path).to eq('test_two_patches.rb')
expect(warnings[1].first_line).to eq(15)
end
end
end

0 comments on commit e01d298

Please sign in to comment.