Skip to content

Commit

Permalink
simplecov-ruby#679 Stricter coverage percentage computation to ensure…
Browse files Browse the repository at this point in the history
… 100% coverage means exactly 100%
  • Loading branch information
gleseur committed May 24, 2018
1 parent 994dbff commit 4b97bb5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
6 changes: 3 additions & 3 deletions features/maximum_coverage_drop.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ Feature:

When I run `bundle exec rake test`
Then the exit status should not be 0
And the output should contain "Coverage has dropped by 3.32% since the last time (maximum allowed: 3.14%)."
And the output should contain "Coverage has dropped by 3.31% since the last time (maximum allowed: 3.14%)."
And a file named "coverage/.last_run.json" should exist
And the file "coverage/.last_run.json" should contain:
"""
{
"result": {
"covered_percent": 88.1
"covered_percent": 88.09
}
}
"""
Expand All @@ -58,7 +58,7 @@ Feature:
"""
{
"result": {
"covered_percent": 88.1
"covered_percent": 88.09
}
}
"""
Expand Down
8 changes: 4 additions & 4 deletions features/minimum_coverage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ Feature:

When I run `bundle exec rake test`
Then the exit status should not be 0
And the output should contain "Coverage (88.10%) is below the expected minimum coverage (90.00%)."
And the output should contain "Coverage (88.09%) is below the expected minimum coverage (90.00%)."

Scenario:
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
SimpleCov.start do
add_filter 'test.rb'
minimum_coverage 88.11
minimum_coverage 88.10
end
"""

When I run `bundle exec rake test`
Then the exit status should not be 0
And the output should contain "Coverage (88.10%) is below the expected minimum coverage (88.11%)."
And the output should contain "Coverage (88.09%) is below the expected minimum coverage (88.10%)."

Scenario:
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
SimpleCov.start do
add_filter 'test.rb'
minimum_coverage 88.10
minimum_coverage 88.09
end
"""

Expand Down
8 changes: 4 additions & 4 deletions features/refuse_coverage_drop.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Feature:
"""
{
"result": {
"covered_percent": 88.1
"covered_percent": 88.09
}
}
"""
Expand All @@ -39,13 +39,13 @@ Feature:

When I run `bundle exec rake test`
Then the exit status should not be 0
And the output should contain "Coverage has dropped by 3.32% since the last time (maximum allowed: 0.00%)."
And the output should contain "Coverage has dropped by 3.31% since the last time (maximum allowed: 0.00%)."
And a file named "coverage/.last_run.json" should exist
And the file "coverage/.last_run.json" should contain:
"""
{
"result": {
"covered_percent": 88.1
"covered_percent": 88.09
}
}
"""
Expand All @@ -66,7 +66,7 @@ Feature:
"""
{
"result": {
"covered_percent": 88.1
"covered_percent": 88.09
}
}
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def process_result(result, exit_status)
return exit_status unless SimpleCov.result? # Result has been computed
return exit_status if exit_status != SimpleCov::ExitCodes::SUCCESS # Existing errors

covered_percent = result.covered_percent.round(2)
covered_percent = result.covered_percent.floor(2)
result_exit_status = result_exit_status(result, covered_percent)
if result_exit_status == SimpleCov::ExitCodes::SUCCESS # No result errors
write_last_run(covered_percent)
Expand All @@ -227,7 +227,7 @@ def process_result(result, exit_status)
#
# rubocop:disable Metrics/MethodLength
def result_exit_status(result, covered_percent)
covered_percentages = result.covered_percentages.map { |percentage| percentage.round(2) }
covered_percentages = result.covered_percentages.map { |percentage| percentage.floor(2) }
if covered_percent < SimpleCov.minimum_coverage
$stderr.printf("Coverage (%.2f%%) is below the expected minimum coverage (%.2f%%).\n", covered_percent, SimpleCov.minimum_coverage)
SimpleCov::ExitCodes::MINIMUM_COVERAGE
Expand Down
37 changes: 37 additions & 0 deletions spec/simplecov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,42 @@
end
end
end

describe ".process_result" do
context "when minimum coverage is 100%" do
let(:result) { SimpleCov::Result.new({}) }

before do
allow(SimpleCov).to receive(:minimum_coverage).and_return(100)
allow(SimpleCov).to receive(:result?).and_return(true)
end

context "when actual coverage is almost 100%" do
before do
allow(result).to receive(:covered_percent).and_return(100 * 32_847.0 / 32_848)
end

it "return SimpleCov::ExitCodes::MINIMUM_COVERAGE" do
expect(
SimpleCov.process_result(result, SimpleCov::ExitCodes::SUCCESS)
).to eq(SimpleCov::ExitCodes::MINIMUM_COVERAGE)
end
end

context "when actual coverage is exactly 100%" do
before do
allow(result).to receive(:covered_percent).and_return(100.0)
allow(result).to receive(:covered_percentages).and_return([])
allow(SimpleCov::LastRun).to receive(:read).and_return(nil)
end

it "return SimpleCov::ExitCodes::SUCCESS" do
expect(
SimpleCov.process_result(result, SimpleCov::ExitCodes::SUCCESS)
).to eq(SimpleCov::ExitCodes::SUCCESS)
end
end
end
end
end
end

0 comments on commit 4b97bb5

Please sign in to comment.