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

Store and display filename in results #1090

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions lib/simplecov/coverage_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
[
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ def failing?

def report
minimum_violations.each do |violation|
filename = violation.fetch(:filename)

$stderr.printf(
"%<criterion>s coverage by file (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n",
"%<criterion>s coverage by file (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%): #{filename}\n",
covered: SimpleCov.round_coverage(violation.fetch(:actual)),
minimum_coverage: violation.fetch(:minimum_expected),
criterion: violation.fetch(:criterion).capitalize
Expand All @@ -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)
Expand Down
21 changes: 18 additions & 3 deletions lib/simplecov/file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
Loading