This is useful for defining facts that just have a
static value, rather than those that are calculated in some
way.  E.g.,

  Facter.add "foo", :value => "bar"

Signed-off-by: Luke Kanies <[email protected]>
---
Local-branch: tickets/master/2157-external_fact_support
 lib/facter/util/collection.rb     |   17 ++++++++++-------
 lib/facter/util/fact.rb           |    6 ++----
 lib/facter/util/resolution.rb     |    2 ++
 spec/unit/util/collection_spec.rb |   19 +++++++------------
 spec/unit/util/fact_spec.rb       |   33 ++++++++++-----------------------
 spec/unit/util/resolution_spec.rb |   11 +++++++++++
 6 files changed, 42 insertions(+), 46 deletions(-)

diff --git a/lib/facter/util/collection.rb b/lib/facter/util/collection.rb
index b3d3a45..00995d9 100644
--- a/lib/facter/util/collection.rb
+++ b/lib/facter/util/collection.rb
@@ -33,13 +33,16 @@ class Facter::Util::Collection
 
         if block
             resolve = fact.add(&block)
-            # Set any resolve-appropriate options
-            options.each do |opt, value|
-                method = opt.to_s + "="
-                if resolve.respond_to?(method)
-                    resolve.send(method, value)
-                    options.delete(opt)
-                end
+        else
+            resolve = fact.add
+        end
+
+        # Set any resolve-appropriate options
+        options.each do |opt, value|
+            method = opt.to_s + "="
+            if resolve.respond_to?(method)
+                resolve.send(method, value)
+                options.delete(opt)
             end
         end
 
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb
index 935b3c1..90226f1 100644
--- a/lib/facter/util/fact.rb
+++ b/lib/facter/util/fact.rb
@@ -30,12 +30,10 @@ class Facter::Util::Fact
 
     # Add a new resolution mechanism.  This requires a block, which will then
     # be evaluated in the context of the new mechanism.
-    def add(&block)
-        raise ArgumentError, "You must pass a block to Fact<instance>.add" 
unless block_given?
-
+    def add(value = nil, &block)
         resolve = Facter::Util::Resolution.new(@name)
 
-        resolve.instance_eval(&block)
+        resolve.instance_eval(&block) if block
 
         @resolves << resolve
 
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index 4388c81..92092ec 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -10,6 +10,7 @@ require 'timeout'
 
 class Facter::Util::Resolution
     attr_accessor :interpreter, :code, :name, :timeout
+    attr_writer :value, :weight
 
     INTERPRETER = Facter::Util::Config.is_windows? ? "cmd.exe" : "/bin/sh"
 
@@ -151,6 +152,7 @@ class Facter::Util::Resolution
 
     # How we get a value for our resolution mechanism.
     def value
+        return @value if @value
         result = nil
         return result if @code == nil
 
diff --git a/spec/unit/util/collection_spec.rb 
b/spec/unit/util/collection_spec.rb
index 86b602f..3988a25 100755
--- a/spec/unit/util/collection_spec.rb
+++ b/spec/unit/util/collection_spec.rb
@@ -38,10 +38,8 @@ describe Facter::Util::Collection do
         end
 
         it "should create a new fact if no fact with the same name already 
exists" do
-            fact = mock 'fact'
-            Facter::Util::Fact.expects(:new).with { |name, *args| name == 
:myname }.returns fact
-
             @coll.add(:myname)
+            @coll.fact(:myname).name.should == :myname
         end
 
         it "should accept options" do
@@ -51,10 +49,9 @@ describe Facter::Util::Collection do
         it "should set any appropriate options on the fact instances" do
             # Use a real fact instance, because we're using respond_to?
             fact = Facter::Util::Fact.new(:myname)
-            fact.expects(:ldapname=).with("testing")
-            Facter::Util::Fact.expects(:new).with(:myname).returns fact
 
             @coll.add(:myname, :ldapname => "testing")
+            @coll.fact(:myname).ldapname.should == "testing"
         end
 
         it "should set appropriate options on the resolution instance" do
@@ -84,15 +81,13 @@ describe Facter::Util::Collection do
             lambda { @coll.add(:myname, :foo => :bar) }.should 
raise_error(ArgumentError)
         end
 
-        describe "and a block is provided" do
-            it "should use the block to add a resolution to the fact" do
-                fact = mock 'fact'
-                Facter::Util::Fact.expects(:new).returns fact
+        it "should use the block to add a resolution to the fact if a block is 
provided" do
+            fact = mock 'fact'
+            Facter::Util::Fact.expects(:new).returns fact
 
-                fact.expects(:add)
+            fact.expects(:add)
 
-                @coll.add(:myname) {}
-            end
+            @coll.add(:myname) {}
         end
     end
 
diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb
index 523c855..eea4cdc 100755
--- a/spec/unit/util/fact_spec.rb
+++ b/spec/unit/util/fact_spec.rb
@@ -33,39 +33,26 @@ describe Facter::Util::Fact do
         before do
             @fact = Facter::Util::Fact.new("yay")
 
-            @resolution = mock 'resolution'
-            @resolution.stub_everything
-
-        end
-
-        it "should fail if no block is given" do
-            lambda { @fact.add }.should raise_error(ArgumentError)
+            @resolution = Facter::Util::Resolution.new("yay")
         end
 
-        it "should create a new resolution instance" do
+        it "should be to create a new resolution instance with a block" do
             Facter::Util::Resolution.expects(:new).returns @resolution
 
             @fact.add { }
         end
-
         it "should instance_eval the passed block on the new resolution" do
-            @resolution.expects(:instance_eval)
-
-            Facter::Util::Resolution.stubs(:new).returns @resolution
-
-            @fact.add { }
+            @fact.add {
+              setcode { "foo" }
+            }
+            @fact.value.should == "foo"
         end
 
         it "should re-sort the resolutions by weight, so the most restricted 
resolutions are first" do
-            r1 = stub 'r1', :weight => 1
-            r2 = stub 'r2', :weight => 2
-            r3 = stub 'r3', :weight => 0
-            
Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
-            @fact.add { }
-            @fact.add { }
-            @fact.add { }
-
-            @fact.instance_variable_get("@resolves").should == [r2, r1, r3]
+            @fact.add { self.value = "1"; self.weight = 1 }
+            @fact.add { self.value = "2"; self.weight = 2 }
+            @fact.add { self.value = "0"; self.weight = 0 }
+            @fact.value.should == "2"
         end
     end
 
diff --git a/spec/unit/util/resolution_spec.rb 
b/spec/unit/util/resolution_spec.rb
index fca56d2..a0cad10 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -13,6 +13,12 @@ describe Facter::Util::Resolution do
         Facter::Util::Resolution.new("yay").name.should == "yay"
     end
 
+    it "should be able to set the value" do
+      resolve = Facter::Util::Resolution.new("yay")
+      resolve.value = "foo"
+      resolve.value.should == "foo"
+    end
+
     it "should have a method for setting the weight" do
       Facter::Util::Resolution.new("yay").should respond_to(:has_weight)
     end
@@ -97,6 +103,11 @@ describe Facter::Util::Resolution do
             @resolve = Facter::Util::Resolution.new("yay")
         end
 
+        it "should return any value that has been provided" do
+            @resolve.value = "foo"
+            @resolve.value.should == "foo"
+        end
+
         describe "and setcode has not been called" do
             it "should return nil" do
                 Facter::Util::Resolution.expects(:exec).with(nil, nil).never
-- 
1.7.3.1

-- 
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.

Reply via email to