Skip to content
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

Stricter coverage percentage computation to ensure 100% coverage means exactly 100% #773

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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