diff --git a/lib/simplecov/coverage_statistics.rb b/lib/simplecov/coverage_statistics.rb index 14ec02e8c..3e0e78671 100644 --- a/lib/simplecov/coverage_statistics.rb +++ b/lib/simplecov/coverage_statistics.rb @@ -5,15 +5,16 @@ module SimpleCov # # This is uniform across coverage criteria as they all have: # + # * filename - the full name of the file # * total - how many things to cover there are (total relevant loc/branches) # * covered - how many of the coverables are hit # * missed - how many of the coverables are missed # * percent - percentage as covered/missed # * strength - average hits per/coverable (will not exist for one shot lines format) class CoverageStatistics - attr_reader :total, :covered, :missed, :strength, :percent + attr_reader :filename, :total, :covered, :missed, :strength, :percent - def self.from(coverage_statistics) + def self.from(filename:, coverage_statistics:) sum_covered, sum_missed, sum_total_strength = coverage_statistics.reduce([0, 0, 0.0]) do |(covered, missed, total_strength), file_coverage_statistics| [ @@ -25,13 +26,14 @@ def self.from(coverage_statistics) ] end - new(covered: sum_covered, missed: sum_missed, total_strength: sum_total_strength) + new(filename: filename, covered: sum_covered, missed: sum_missed, total_strength: sum_total_strength) end # Requires only covered, missed and strength to be initialized. # # Other values are computed by this class. - def initialize(covered:, missed:, total_strength: 0.0) + def initialize(filename:, covered:, missed:, total_strength: 0.0) + @filename = filename @covered = covered @missed = missed @total = covered + missed diff --git a/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb b/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb index a276d2756..211569d8b 100644 --- a/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +++ b/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb @@ -14,8 +14,10 @@ def failing? def report minimum_violations.each do |violation| + filename = violation.fetch(:filename) + $stderr.printf( - "%s coverage by file (%.2f%%) is below the expected minimum coverage (%.2f%%).\n", + "%s coverage by file (%.2f%%) is below the expected minimum coverage (%.2f%%): #{filename}\n", covered: SimpleCov.round_coverage(violation.fetch(:actual)), minimum_coverage: violation.fetch(:minimum_expected), criterion: violation.fetch(:criterion).capitalize @@ -42,6 +44,7 @@ def compute_minimum_coverage_data minimum_coverage_by_file.flat_map do |criterion, expected_percent| result.coverage_statistics_by_file.fetch(criterion).map do |actual_coverage| { + filename: actual_coverage.filename, criterion: criterion, minimum_expected: expected_percent, actual: SimpleCov.round_coverage(actual_coverage.percent) diff --git a/lib/simplecov/file_list.rb b/lib/simplecov/file_list.rb index 756233b89..d7777a6eb 100644 --- a/lib/simplecov/file_list.rb +++ b/lib/simplecov/file_list.rb @@ -105,16 +105,31 @@ def branch_covered_percent private def compute_coverage_statistics_by_file - @files.each_with_object(line: [], branch: []) do |file, together| + @files.each_with_object(filename: [], line: [], branch: []) do |file, together| + together[:filename] << file.filename together[:line] << file.coverage_statistics.fetch(:line) together[:branch] << file.coverage_statistics.fetch(:branch) if SimpleCov.branch_coverage? end end def compute_coverage_statistics - coverage_statistics = {line: CoverageStatistics.from(coverage_statistics_by_file[:line])} - coverage_statistics[:branch] = CoverageStatistics.from(coverage_statistics_by_file[:branch]) if SimpleCov.branch_coverage? + coverage_statistics = {line: line_coverage_statistics} + coverage_statistics[:branch] = branch_coverage_statistics if SimpleCov.branch_coverage? coverage_statistics end + + def line_coverage_statistics + CoverageStatistics.from( + filename: coverage_statistics_by_file[:filename], + coverage_statistics: coverage_statistics_by_file[:line] + ) + end + + def branch_coverage_statistics + CoverageStatistics.from( + filename: coverage_statistics_by_file[:filename], + coverage_statistics: coverage_statistics_by_file[:branch] + ) + end end end diff --git a/lib/simplecov/source_file.rb b/lib/simplecov/source_file.rb index 3d9efcd60..a3832bd79 100644 --- a/lib/simplecov/source_file.rb +++ b/lib/simplecov/source_file.rb @@ -336,6 +336,7 @@ def build_branch(branch_data, hit_count, condition_start_line) def line_coverage_statistics { line: CoverageStatistics.new( + filename: filename, total_strength: lines_strength, covered: covered_lines.size, missed: missed_lines.size @@ -346,6 +347,7 @@ def line_coverage_statistics def branch_coverage_statistics { branch: CoverageStatistics.new( + filename: filename, covered: covered_branches.size, missed: missed_branches.size )