-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added minimum coverage by group check
- Loading branch information
1 parent
c7102e4
commit c7835b5
Showing
9 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
65 changes: 65 additions & 0 deletions
65
lib/simplecov/exit_codes/minimum_coverage_by_group_check.rb
This file contains 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,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module SimpleCov | ||
module ExitCodes | ||
class MinimumCoverageByGroupCheck | ||
def initialize(result, minimum_coverage_by_group) | ||
@result = result | ||
@minimum_coverage_by_group = minimum_coverage_by_group | ||
end | ||
|
||
def failing? | ||
minimum_violations.any? | ||
end | ||
|
||
def report | ||
minimum_violations.each do |violation| | ||
$stderr.printf( | ||
"%<criterion>s coverage by group %<group_name>s (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n", | ||
group_name: violation.fetch(:group_name), | ||
covered: SimpleCov.round_coverage(violation.fetch(:actual)), | ||
minimum_coverage: violation.fetch(:minimum_expected), | ||
criterion: violation.fetch(:criterion).capitalize | ||
) | ||
end | ||
end | ||
|
||
def exit_code | ||
SimpleCov::ExitCodes::MINIMUM_COVERAGE | ||
end | ||
|
||
private | ||
|
||
attr_reader :result, :minimum_coverage_by_group | ||
|
||
def minimum_violations | ||
@minimum_violations ||= | ||
compute_minimum_coverage_data.select do |achieved| | ||
achieved.fetch(:actual) < achieved.fetch(:minimum_expected) | ||
end | ||
end | ||
|
||
def compute_minimum_coverage_data | ||
minimum_coverage_data = [] | ||
|
||
minimum_coverage_by_group.each do |group_name, minimum_group_coverage| | ||
minimum_group_coverage.each do |criterion, expected_percent| | ||
actual_coverage = result.groups.fetch(group_name).coverage_statistics.fetch(criterion) | ||
minimum_coverage_data << minimum_coverage_hash(group_name, criterion, expected_percent, SimpleCov.round_coverage(actual_coverage.percent)) | ||
end | ||
end | ||
|
||
minimum_coverage_data | ||
end | ||
|
||
def minimum_coverage_hash(group_name, criterion, minimum_expected, actual) | ||
{ | ||
group_name: group_name, | ||
criterion: criterion, | ||
minimum_expected: minimum_expected, | ||
actual: actual | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
RSpec.describe SimpleCov::ExitCodes::MinimumCoverageByGroupCheck do | ||
subject { described_class.new(result, minimum_coverage_by_group) } | ||
|
||
let(:coverage_statistics) { {line: SimpleCov::CoverageStatistics.new(covered: 8, missed: 2), branch: SimpleCov::CoverageStatistics.new(covered: 8, missed: 2)} } | ||
let(:result) { instance_double(SimpleCov::Result, groups: {"Test Group 1" => instance_double(SimpleCov::FileList, coverage_statistics: coverage_statistics)}) } | ||
let(:stats) { {"Test Group 1" => coverage_statistics} } | ||
|
||
context "everything exactly ok" do | ||
let(:minimum_coverage_by_group) { {"Test Group 1" => {line: 80.0}} } | ||
|
||
it { is_expected.not_to be_failing } | ||
end | ||
|
||
context "coverage violated" do | ||
let(:minimum_coverage_by_group) { {"Test Group 1" => {line: 90.0}} } | ||
|
||
it { is_expected.to be_failing } | ||
end | ||
|
||
context "coverage slightly violated" do | ||
let(:minimum_coverage_by_group) { {"Test Group 1" => {line: 80.01}} } | ||
|
||
it { is_expected.to be_failing } | ||
end | ||
|
||
context "one criterion violated" do | ||
let(:minimum_coverage_by_group) { {"Test Group 1" => {line: 80.0, branch: 90.0}} } | ||
|
||
it { is_expected.to be_failing } | ||
end | ||
end |