this ensures we can compare all kind of objects and
not only instances of strings.
It compares strings in a case-insensitive manner and converts
symbols to strings.
introducing this behavior required that we introduce
a convert util method, to ensure that we convert the value
correctly. Introduced this method in other places as well.
This behavior change requires that we drop one test, which have
become anyway deprecated.
---
lib/facter/util/confine.rb | 24 +++++++-------
lib/facter/util/values.rb | 14 ++++++++
spec/unit/util/confine.rb | 75 +++++++++++++++++++++++++++++++++++++++++---
3 files changed, 96 insertions(+), 17 deletions(-)
create mode 100644 lib/facter/util/values.rb
diff --git a/lib/facter/util/confine.rb b/lib/facter/util/confine.rb
index a430bbe..4cbb32c 100644
--- a/lib/facter/util/confine.rb
+++ b/lib/facter/util/confine.rb
@@ -1,22 +1,20 @@
# A restricting tag for fact resolution mechanisms. The tag must be true
# for the resolution mechanism to be suitable.
+
+require 'facter/util/values'
+
class Facter::Util::Confine
attr_accessor :fact, :values
+ include Facter::Util::Values
+
# Add the restriction. Requires the fact name, an operator, and the value
# we're comparing to.
def initialize(fact, *values)
raise ArgumentError, "The fact name must be provided" unless fact
raise ArgumentError, "One or more values must be provided" if
values.empty?
- fact = fact.to_s if fact.is_a? Symbol
@fact = fact
- @values = values.collect do |value|
- if value.is_a? String
- value
- else
- value.to_s
- end
- end
+ @values = values
end
def to_s
@@ -29,13 +27,15 @@ class Facter::Util::Confine
Facter.debug "No fact for %s" % @fact
return false
end
- value = fact.value
+ value = convert(fact.value)
return false if value.nil?
- @values.each { |v|
- return true if value.downcase == v.downcase
- }
+ @values.each do |v|
+ v = convert(v)
+ next unless v.class == value.class
+ return true if value == v
+ end
return false
end
end
diff --git a/lib/facter/util/values.rb b/lib/facter/util/values.rb
new file mode 100644
index 0000000..ebc7614
--- /dev/null
+++ b/lib/facter/util/values.rb
@@ -0,0 +1,14 @@
+# A util module for facter containing helper methods
+module Facter
+ module Util
+ module Values
+ module_function
+
+ def convert(value)
+ value = value.to_s if value.is_a?(Symbol)
+ value = value.downcase if value.is_a?(String)
+ value
+ end
+ end
+ end
+end
diff --git a/spec/unit/util/confine.rb b/spec/unit/util/confine.rb
index 5c1ce3b..757ca26 100755
--- a/spec/unit/util/confine.rb
+++ b/spec/unit/util/confine.rb
@@ -3,6 +3,9 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'facter/util/confine'
+require 'facter/util/values'
+
+include Facter::Util::Values
describe Facter::Util::Confine do
it "should require a fact name" do
@@ -17,10 +20,6 @@ describe Facter::Util::Confine do
Facter::Util::Confine.new("yay", "test", "other").values.should ==
["test", "other"]
end
- it "should convert all values to strings" do
- Facter::Util::Confine.new("yay", :test).values.should == %w{test}
- end
-
it "should fail if no fact name is provided" do
lambda { Facter::Util::Confine.new(nil, :test) }.should
raise_error(ArgumentError)
end
@@ -35,7 +34,7 @@ describe Facter::Util::Confine do
describe "when evaluating" do
before do
- @confine = Facter::Util::Confine.new("yay", "one", "two")
+ @confine = Facter::Util::Confine.new("yay", "one", "two", "Four",
:xy, true, 1, [3,4])
@fact = mock 'fact'
Facter.stubs(:[]).returns @fact
end
@@ -66,10 +65,76 @@ describe Facter::Util::Confine do
@confine.true?.should be_true
end
+ it "should return true if any of the provided symbol values matches
the fact's value" do
+ @fact.stubs(:value).returns :xy
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided integer values matches
the fact's value" do
+ @fact.stubs(:value).returns 1
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided boolan values matches
the fact's value" do
+ @fact.stubs(:value).returns true
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided array values matches the
fact's value" do
+ @fact.stubs(:value).returns [3,4]
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided symbol values matches
the fact's string value" do
+ @fact.stubs(:value).returns :one
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided string values matches
case-insensitive the fact's value" do
+ @fact.stubs(:value).returns "four"
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided symbol values matches
case-insensitive the fact's string value" do
+ @fact.stubs(:value).returns :four
+
+ @confine.true?.should be_true
+ end
+
+ it "should return true if any of the provided symbol values matches
the fact's string value" do
+ @fact.stubs(:value).returns :Xy
+
+ @confine.true?.should be_true
+ end
+
it "should return false if none of the provided values matches the
fact's value" do
@fact.stubs(:value).returns "three"
@confine.true?.should be_false
end
+
+ it "should return false if none of the provided integer values matches
the fact's value" do
+ @fact.stubs(:value).returns 2
+
+ @confine.true?.should be_false
+ end
+
+ it "should return false if none of the provided boolan values matches
the fact's value" do
+ @fact.stubs(:value).returns false
+
+ @confine.true?.should be_false
+ end
+
+ it "should return false if none of the provided array values matches
the fact's value" do
+ @fact.stubs(:value).returns [1,2]
+
+ @confine.true?.should be_false
+ end
end
end
--
1.6.0.6
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---