Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4632017
commit 22946a4
Showing
2 changed files
with
46 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,55 @@ | ||
require 'cane/file' | ||
|
||
# Configurable check that allows the contents of a file to be compared against | ||
# a given value. | ||
class ThresholdCheck < Struct.new(:opts) | ||
|
||
def self.key; :threshold; end | ||
def self.options | ||
{ | ||
gte: ["If FILE contains a number, verify it is >= to THRESHOLD", | ||
variable: "FILE,THRESHOLD", | ||
type: Array] | ||
} | ||
end | ||
module Cane | ||
|
||
# Configurable check that allows the contents of a file to be compared against | ||
# a given value. | ||
class ThresholdCheck < Struct.new(:opts) | ||
|
||
def self.key; :threshold; end | ||
def self.options | ||
{ | ||
gte: ["If FILE contains a number, verify it is >= to THRESHOLD", | ||
variable: "FILE,THRESHOLD", | ||
type: Array] | ||
} | ||
end | ||
|
||
def violations | ||
thresholds.map do |operator, file, limit| | ||
value = value_from_file(file) | ||
|
||
unless value.send(operator, limit.to_f) | ||
{ | ||
description: 'Quality threshold crossed', | ||
label: "%s is %s, should be %s %s" % [ | ||
file, operator, value, limit | ||
] | ||
} | ||
end | ||
end.compact | ||
end | ||
|
||
def violations | ||
thresholds.map do |operator, file, limit| | ||
value = value_from_file(file) | ||
|
||
unless value.send(operator, limit.to_f) | ||
{ | ||
description: 'Quality threshold crossed', | ||
label: "%s is %s, should be %s %s" % [ | ||
file, operator, value, limit | ||
] | ||
} | ||
def value_from_file(file) | ||
begin | ||
contents = Cane::File.contents(file).chomp.to_f | ||
rescue Errno::ENOENT | ||
UnavailableValue.new | ||
end | ||
end.compact | ||
end | ||
end | ||
|
||
def value_from_file(file) | ||
begin | ||
contents = Cane::File.contents(file).chomp.to_f | ||
rescue Errno::ENOENT | ||
UnavailableValue.new | ||
def thresholds | ||
(opts[:gte] || []).map do |x| | ||
x.unshift(:>=) | ||
end | ||
end | ||
end | ||
|
||
def thresholds | ||
(opts[:gte] || []).map do |x| | ||
x.unshift(:>=) | ||
# Null object for all cases when the value to be compared against cannot be | ||
# read. | ||
class UnavailableValue | ||
def >=(_); false end | ||
def to_s; 'unavailable' end | ||
end | ||
end | ||
|
||
# Null object for all cases when the value to be compared against cannot be | ||
# read. | ||
class UnavailableValue | ||
def >=(_); false end | ||
def to_s; 'unavailable' end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters