Skip to content

Commit

Permalink
679 Stricter coverage percentage computation to ensure 100% coverage …
Browse files Browse the repository at this point in the history
…means exactly 100%
  • Loading branch information
gleseur authored and PragTob committed Dec 3, 2019
1 parent 6dd17ee commit 91cf8c1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 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
9 changes: 4 additions & 5 deletions features/minimum_coverage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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%)."
And the output should contain "SimpleCov failed with exit 2"

Scenario:
Expand All @@ -25,13 +25,13 @@ Feature:
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%)."
And the output should contain "SimpleCov failed with exit 2"

Scenario:
Expand All @@ -40,7 +40,7 @@ Feature:
require 'simplecov'
SimpleCov.start do
add_filter 'test.rb'
minimum_coverage 88.10
minimum_coverage 88.09
end
"""

Expand All @@ -58,4 +58,3 @@ Feature:

When I run `bundle exec rake test`
Then the exit status should be 0

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 @@ -199,7 +199,7 @@ def run_exit_tasks!
def process_result(result, exit_status)
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 @@ -211,7 +211,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 @@ -191,5 +191,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 91cf8c1

Please sign in to comment.