-
Notifications
You must be signed in to change notification settings - Fork 574
Add richer metrics to JSON formatter #1164
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b095ecc
Add richer metrics to JSON formatter
sferik db8f6d9
Add errors to JSON output for files below minimum coverage
sferik 31604ad
Add `omitted` field to JSON line stats
sferik 8cb80de
Drop dev-only entries from CHANGELOG
sferik 76d246e
Round coverage percent in minimum_coverage errors
sferik bdbe8b2
Prefer `if` guards over `next` in JSON error formatting
sferik aabfdaf
Extract CoverageViolations module for threshold checks
sferik 4916a99
Use CoverageStatistics directly in HTML coverage_summary
sferik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SimpleCov | ||
| # Computes coverage threshold violations for a given result. Shared by | ||
| # the exit-code checks and the JSON formatter's `errors` section. | ||
| # | ||
| # Each method returns an array of violation hashes. All percents are | ||
| # rounded via `SimpleCov.round_coverage` so downstream consumers don't | ||
| # need to round again. | ||
| module CoverageViolations | ||
| class << self | ||
| # @return [Array<Hash>] {:criterion, :expected, :actual} | ||
| def minimum_overall(result, thresholds) | ||
| thresholds.filter_map do |criterion, expected| | ||
| actual = round(result.coverage_statistics.fetch(criterion).percent) | ||
| {criterion: criterion, expected: expected, actual: actual} if actual < expected | ||
| end | ||
| end | ||
|
|
||
| # @return [Array<Hash>] {:criterion, :expected, :actual, :filename, :project_filename} | ||
| def minimum_by_file(result, thresholds) | ||
| thresholds.flat_map do |criterion, expected| | ||
| result.files.filter_map { |file| file_minimum_violation(file, criterion, expected) } | ||
| end | ||
| end | ||
|
|
||
| # @return [Array<Hash>] {:group_name, :criterion, :expected, :actual} | ||
| def minimum_by_group(result, thresholds) | ||
| thresholds.flat_map do |group_name, minimums| | ||
| group = lookup_group(result, group_name) | ||
| group ? group_minimum_violations(group_name, group, minimums) : [] | ||
| end | ||
| end | ||
|
|
||
| # @return [Array<Hash>] {:criterion, :maximum, :actual} where `actual` | ||
| # is the observed drop (in percentage points) vs. the last run. | ||
| def maximum_drop(result, thresholds, last_run: SimpleCov::LastRun.read) | ||
| return [] unless last_run | ||
|
|
||
| thresholds.filter_map do |criterion, maximum| | ||
| actual = compute_drop(criterion, result, last_run) | ||
| {criterion: criterion, maximum: maximum, actual: actual} if actual && actual > maximum | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def file_minimum_violation(file, criterion, expected) | ||
| actual = round(file.coverage_statistics.fetch(criterion).percent) | ||
| return unless actual < expected | ||
|
|
||
| { | ||
| criterion: criterion, | ||
| expected: expected, | ||
| actual: actual, | ||
| filename: file.filename, | ||
| project_filename: file.project_filename | ||
| } | ||
| end | ||
|
|
||
| def group_minimum_violations(group_name, group, minimums) | ||
| minimums.filter_map do |criterion, expected| | ||
| actual = round(group.coverage_statistics.fetch(criterion).percent) | ||
| {group_name: group_name, criterion: criterion, expected: expected, actual: actual} if actual < expected | ||
| end | ||
| end | ||
|
|
||
| def lookup_group(result, group_name) | ||
| group = result.groups[group_name] | ||
| warn "minimum_coverage_by_group: no group named '#{group_name}' exists. Available groups: #{result.groups.keys.join(', ')}" unless group | ||
| group | ||
| end | ||
|
|
||
| def compute_drop(criterion, result, last_run) | ||
| last_coverage_percent = last_run.dig(:result, criterion) | ||
| last_coverage_percent ||= last_run.dig(:result, :covered_percent) if criterion == :line | ||
| return unless last_coverage_percent | ||
|
|
||
| current = round(result.coverage_statistics.fetch(criterion).percent) | ||
| (last_coverage_percent - current).floor(10) | ||
| end | ||
|
|
||
| def round(percent) | ||
| SimpleCov.round_coverage(percent) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.