Skip to content

Commit

Permalink
Merge pull request #162 from infertux/fractional_thresholds
Browse files Browse the repository at this point in the history
Allow to specify fractional coverage thresholds
  • Loading branch information
colszowka committed Sep 11, 2012
2 parents 548d212 + 7c1f96b commit f4aee37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions features/maximum_coverage_drop.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature:
require 'simplecov'
SimpleCov.start do
add_filter 'test.rb'
maximum_coverage_drop 2
maximum_coverage_drop 3.14
end
"""

Expand All @@ -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

27 changes: 27 additions & 0 deletions features/minimum_coverage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
6 changes: 2 additions & 4 deletions lib/simplecov/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand All @@ -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

#
Expand Down
10 changes: 6 additions & 4 deletions lib/simplecov/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -71,7 +73,7 @@
end

metrics = {
:result => { :covered_percent => SimpleCov.result.covered_percent }
:result => { :covered_percent => covered_percent }
}
SimpleCov::LastRun.write(metrics)
end
Expand Down

0 comments on commit f4aee37

Please sign in to comment.