From 4b97bb5ca3b421e00ccb51395f572e667aa49c0a 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 | 8 +++--- features/refuse_coverage_drop.feature | 8 +++--- lib/simplecov.rb | 4 +-- spec/simplecov_spec.rb | 37 ++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 13 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 1f8dd1c8..f2bebff0 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%)." Scenario: Given SimpleCov for Test/Unit is configured with: @@ -24,13 +24,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%)." Scenario: Given SimpleCov for Test/Unit is configured with: @@ -38,7 +38,7 @@ Feature: require 'simplecov' SimpleCov.start do add_filter 'test.rb' - minimum_coverage 88.10 + minimum_coverage 88.09 end """ 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 11ab2db3..65b0526b 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -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) @@ -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 diff --git a/spec/simplecov_spec.rb b/spec/simplecov_spec.rb index 6102fcb6..4da151b6 100644 --- a/spec/simplecov_spec.rb +++ b/spec/simplecov_spec.rb @@ -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