Skip to content

Commit

Permalink
More thorough maximum coverage drop specs + fixes
Browse files Browse the repository at this point in the history
* fixes accuracy bug in drop calculation
* fixes bug when no coverage data present from last run
  • Loading branch information
PragTob committed Jan 9, 2021
1 parent e0c5fd1 commit d4ed992
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
28 changes: 19 additions & 9 deletions lib/simplecov/exit_codes/maximum_coverage_drop_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,32 @@ def compute_coverage_drop_data
{
criterion: criterion,
max_drop: percent,
drop_percent: last_coverage(criterion) -
SimpleCov.round_coverage(
result.coverage_statistics.fetch(criterion).percent
)
drop_percent: drop_percent(criterion)
}
end
end

# if anyone says "max_coverage_drop 0.000000000000000001" I appologize. Please don't.
MAX_DROP_ACCURACY = 10
def drop_percent(criterion)
drop = last_coverage(criterion) -
SimpleCov.round_coverage(
result.coverage_statistics.fetch(criterion).percent
)

# floats, I tell ya.
# irb(main):001:0* 80.01 - 80.0
# => 0.010000000000005116
drop.floor(MAX_DROP_ACCURACY)
end

def last_coverage(criterion)
last_coverage_percent = last_run[:result][criterion]

if !last_coverage_percent && criterion == :line
last_run[:result][:covered_percent]
else
last_coverage_percent
end
# fallback for old file format
last_coverage_percent = last_run[:result][:covered_percent] if !last_coverage_percent && criterion == :line

last_coverage_percent || 0
end
end
end
Expand Down
91 changes: 91 additions & 0 deletions spec/exit_codes/maximum_coverage_drop_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

require "helper"

RSpec.describe SimpleCov::ExitCodes::MaximumCoverageDropCheck do
let(:result) do
instance_double(SimpleCov::Result, coverage_statistics: stats)
end
let(:stats) do
{
line: SimpleCov::CoverageStatistics.new(covered: 8, missed: 2),
branch: SimpleCov::CoverageStatistics.new(covered: 8, missed: 2)
}
end
let(:last_run) do
{
result: last_coverage
}
end
let(:last_coverage) { {line: 80.0, branch: 80.0} }
let(:maximum_coverage_drop) { {line: 0, branch: 0} }
subject { described_class.new(result, maximum_coverage_drop) }

before :each do
expect(SimpleCov::LastRun).to receive(:read).and_return(last_run)
end

context "we're at the same coverage" do
it { is_expected.not_to be_failing }
end

context "more coverage drop allowed" do
let(:maximum_coverage_drop) { {line: 10, branch: 10} }

it { is_expected.not_to be_failing }
end

context "last coverage lower then new coverage" do
let(:last_coverage) { {line: 70.0, branch: 70.0} }

it { is_expected.not_to be_failing }
end

context "last coverage higher than new coverage" do
let(:last_coverage) { {line: 80.01, branch: 80.01} }

it { is_expected.to be_failing }

context "but allowed drop is within range" do
let(:maximum_coverage_drop) { {line: 0.01, branch: 0.01} }

it { is_expected.not_to be_failing }
end
end

context "one coverage lower than maximum drop" do
let(:last_coverage) { {line: 80.01, branch: 70.0} }

it { is_expected.to be_failing }

context "but allowed drop is within range" do
let(:maximum_coverage_drop) { {line: 0.01} }

it { is_expected.not_to be_failing }
end
end

context "coverage expectation for a coverage that wasn't previously present" do
let(:last_coverage) { {line: 80.0} }
let(:maximum_coverage_drop) { {line: 0, branch: 0} }

it { is_expected.not_to be_failing }
end

context "no last run coverage information" do
let(:last_run) { nil }

it { is_expected.not_to be_failing }
end

context "old last_run.json format" do
let(:last_run) do
{
# this format only considers line coverage
result: {covered_percent: 80.0}
}
end

it { is_expected.not_to be_failing }
end
end
1 change: 0 additions & 1 deletion spec/exit_codes/minimum_coverage_by_file_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
let(:minimum_coverage_by_file) { {line: 90} }

it "fails" do
p minimum_coverage_by_file
expect(subject).to be_failing
end
end
Expand Down

0 comments on commit d4ed992

Please sign in to comment.