diff --git a/features/maximum_coverage_drop.feature b/features/maximum_coverage_drop.feature index 84a91438..452bd9c9 100644 --- a/features/maximum_coverage_drop.feature +++ b/features/maximum_coverage_drop.feature @@ -10,7 +10,7 @@ Feature: require 'simplecov' SimpleCov.start do add_filter 'test.rb' - maximum_coverage_drop 2 + maximum_coverage_drop 3.14 end """ @@ -31,6 +31,6 @@ 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.31% since the last time (maximum allowed: 2.00%)." + And the output should contain "Coverage has dropped by 3.32% since the last time (maximum allowed: 3.14%)." And a file named "coverage/.last_run.json" should exist diff --git a/features/minimum_coverage.feature b/features/minimum_coverage.feature index 23f4db40..1f8dd1c8 100644 --- a/features/minimum_coverage.feature +++ b/features/minimum_coverage.feature @@ -18,6 +18,33 @@ Feature: Then the exit status should not be 0 And the output should contain "Coverage (88.10%) 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 + 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%)." + + Scenario: + Given SimpleCov for Test/Unit is configured with: + """ + require 'simplecov' + SimpleCov.start do + add_filter 'test.rb' + minimum_coverage 88.10 + end + """ + + When I run `bundle exec rake test` + Then the exit status should be 0 + Scenario: Given SimpleCov for Test/Unit is configured with: """ diff --git a/lib/simplecov/configuration.rb b/lib/simplecov/configuration.rb index 56eac789..c7593a37 100644 --- a/lib/simplecov/configuration.rb +++ b/lib/simplecov/configuration.rb @@ -176,8 +176,7 @@ def merge_timeout(seconds=nil) # Default is 0% (disabled) # def minimum_coverage(coverage=nil) - @minimum_coverage = coverage if coverage.kind_of?(Fixnum) - @minimum_coverage ||= 0 + @minimum_coverage ||= (coverage || 0).to_f.round(2) end # @@ -187,8 +186,7 @@ def minimum_coverage(coverage=nil) # Default is 100% (disabled) # def maximum_coverage_drop(coverage_drop=nil) - @maximum_coverage_drop = coverage_drop if coverage_drop.kind_of?(Fixnum) - @maximum_coverage_drop ||= 100 + @maximum_coverage_drop ||= (coverage_drop || 100).to_f.round(2) end # diff --git a/lib/simplecov/defaults.rb b/lib/simplecov/defaults.rb index 234b44d9..3ad9b34c 100644 --- a/lib/simplecov/defaults.rb +++ b/lib/simplecov/defaults.rb @@ -52,15 +52,17 @@ SimpleCov.at_exit.call if SimpleCov.result? # Result has been computed + covered_percent = SimpleCov.result.covered_percent.round(2) + if @exit_status.to_i == 0 # No other errors - @exit_status = if SimpleCov.result.covered_percent < SimpleCov.minimum_coverage + @exit_status = if covered_percent < SimpleCov.minimum_coverage $stderr.puts "Coverage (%.2f%%) is below the expected minimum coverage (%.2f%%)." % \ - [SimpleCov.result.covered_percent, SimpleCov.minimum_coverage] + [covered_percent, SimpleCov.minimum_coverage] SimpleCov::ExitCodes::MINIMUM_COVERAGE elsif (last_run = SimpleCov::LastRun.read) - diff = last_run['result']['covered_percent'] - SimpleCov.result.covered_percent + diff = last_run['result']['covered_percent'] - covered_percent if diff > SimpleCov.maximum_coverage_drop $stderr.puts "Coverage has dropped by %.2f%% since the last time (maximum allowed: %.2f%%)." % \ [diff, SimpleCov.maximum_coverage_drop] @@ -71,7 +73,7 @@ end metrics = { - :result => { :covered_percent => SimpleCov.result.covered_percent } + :result => { :covered_percent => covered_percent } } SimpleCov::LastRun.write(metrics) end