From 4fe7dfce1c44f0f1601b49f83b01dd12b4d652dc Mon Sep 17 00:00:00 2001 From: Guillaume Leseur Date: Thu, 24 May 2018 23:54:19 +0200 Subject: [PATCH] 679 Stricter coverage percentage computation to ensure 100% coverage means exactly 100% --- features/maximum_coverage_drop.feature | 6 ++--- features/minimum_coverage.feature | 9 +++---- features/refuse_coverage_drop.feature | 8 +++--- lib/simplecov.rb | 4 +-- spec/simplecov_spec.rb | 37 ++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/features/maximum_coverage_drop.feature b/features/maximum_coverage_drop.feature index 89012b4a..7156e212 100644 --- a/features/maximum_coverage_drop.feature +++ b/features/maximum_coverage_drop.feature @@ -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 } } """ @@ -58,7 +58,7 @@ Feature: """ { "result": { - "covered_percent": 88.1 + "covered_percent": 88.09 } } """ diff --git a/features/minimum_coverage.feature b/features/minimum_coverage.feature index f51eccf7..ccc97a6d 100644 --- a/features/minimum_coverage.feature +++ b/features/minimum_coverage.feature @@ -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: @@ -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: @@ -40,7 +40,7 @@ Feature: require 'simplecov' SimpleCov.start do add_filter 'test.rb' - minimum_coverage 88.10 + minimum_coverage 88.09 end """ @@ -58,4 +58,3 @@ Feature: When I run `bundle exec rake test` Then the exit status should be 0 - diff --git a/features/refuse_coverage_drop.feature b/features/refuse_coverage_drop.feature index 0364d813..5e1073da 100644 --- a/features/refuse_coverage_drop.feature +++ b/features/refuse_coverage_drop.feature @@ -21,7 +21,7 @@ Feature: """ { "result": { - "covered_percent": 88.1 + "covered_percent": 88.09 } } """ @@ -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 } } """ @@ -66,7 +66,7 @@ Feature: """ { "result": { - "covered_percent": 88.1 + "covered_percent": 88.09 } } """ diff --git a/lib/simplecov.rb b/lib/simplecov.rb index be66c1e2..3544865a 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -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) @@ -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 diff --git a/spec/simplecov_spec.rb b/spec/simplecov_spec.rb index c1492b78..4d0b3d6f 100644 --- a/spec/simplecov_spec.rb +++ b/spec/simplecov_spec.rb @@ -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