Skip to content
Browse files

feature: Coverage threshold can be specifed in a file.

Merge branch 'store_coverage_threshold_in_file'
  • Loading branch information...
2 parents d610ea3 + 7bc67aa commit 373795ef741a106e9f06d6f2f532486578e1029d @xaviershay xaviershay committed Oct 21, 2012
Showing with 61 additions and 9 deletions.
  1. +18 −3 lib/cane/threshold_check.rb
  2. +43 −6 spec/threshold_check_spec.rb
View
21 lib/cane/threshold_check.rb
@@ -16,10 +16,18 @@ def self.options
end
def violations
- thresholds.map do |operator, file, limit|
- value = value_from_file(file)
+ thresholds.map do |operator, file, threshold|
+ value = normalized_limit(file)
+ limit = normalized_limit(threshold)
- unless value.send(operator, limit.to_f)
+ if !limit.real?
+ {
+ description: 'Quality threshold could not be read',
+ label: "%s is not a number or a file" % [
+ threshold
+ ]
+ }
+ elsif !value.send(operator, limit)
{
description: 'Quality threshold crossed',
label: "%s is %s, should be %s %s" % [
@@ -30,6 +38,12 @@ def violations
end.compact
end
+ def normalized_limit(limit)
+ Float(limit)
+ rescue ArgumentError
+ value_from_file(limit)
+ end
+
def value_from_file(file)
begin
contents = Cane::File.contents(file).chomp.to_f
@@ -49,6 +63,7 @@ def thresholds
class UnavailableValue
def >=(_); false end
def to_s; 'unavailable' end
+ def real?; false; end
end
end
View
49 spec/threshold_check_spec.rb
@@ -3,11 +3,48 @@
require 'cane/threshold_check'
describe Cane::ThresholdCheck do
- it 'returns a value of unavailable when file cannot be read' do
- check = Cane::ThresholdCheck.new(gte: [['bogus_file', 20]])
- violations = check.violations
- violations.length.should == 1
- violations[0][:label].should ==
- 'bogus_file is unavailable, should be >= 20'
+
+ context "checking violations" do
+
+ context "when the current coverage cannot be read" do
+ it 'reports a violation' do
+ check = Cane::ThresholdCheck.new(gte: [['bogus_file', '20']])
+ violations = check.violations
+ violations.length.should == 1
+ violations[0][:label].should ==
+ 'bogus_file is unavailable, should be >= 20.0'
+ end
+ end
+
+ context "when the coverage threshold is incorrectly specified" do
+ it 'reports a violation' do
+ check = Cane::ThresholdCheck.new(gte: [['20', 'bogus_file']])
+ violations = check.violations
+ violations.length.should == 1
+ violations[0][:label].should ==
+ 'bogus_file is not a number or a file'
+ end
+ end
+
+ end
+
+ context "normalizing a user supplied value to a threshold" do
+ it "normalizes an integer to itself" do
+ subject.normalized_limit(99).should == 99
+ end
+
+ it "normalizes a float to itself" do
+ subject.normalized_limit(99.6).should == 99.6
+ end
+
+ it "normalizes a valid file to its contents" do
+ subject.normalized_limit(make_file('99.5')).should == 99.5
+ end
+
+ it "normalizes an invalid file to an unavailable value" do
+ limit = subject.normalized_limit("/File.does.not.exist")
+ limit.should be_a Cane::ThresholdCheck::UnavailableValue
+ end
end
+
end

0 comments on commit 373795e

Please sign in to comment.
Something went wrong with that request. Please try again.