[Pkg-puppet-devel] [facter] 253/352: (FACT-322) Remove special casing of resolution command

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:50 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit bae6c15ea801c13cc1a028d25bcb14f8b27dc803
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Thu Feb 13 13:38:24 2014 -0800

    (FACT-322) Remove special casing of resolution command
    
    Resolutions could accept either a command to execute or a block to run
    to evaluate when resolving. However this behavior meant that special
    behavior had to be inserted into the execution command in case a command
    returned nil. This commit converts the #setcode(String) invocation to
    create a block that will evaluate a command and normalize the output so
    that Facter::Core::Execution doesn't have to be responsible for this.
---
 lib/facter/util/resolution.rb     | 27 ++++++++++-------
 spec/unit/util/resolution_spec.rb | 62 ++++++++++++---------------------------
 2 files changed, 36 insertions(+), 53 deletions(-)

diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index d7564c2..ae9ccb2 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -127,25 +127,32 @@ class Facter::Util::Resolution
   # @api public
   def setcode(string = nil, &block)
     if string
-      @code = string
-    else
-      unless block_given?
-        raise ArgumentError, "You must pass either code or a block"
+      @code = Proc.new do
+        output = Facter::Core::Execution.exec(string)
+        if output.nil?
+          nil
+        elsif output.empty?
+          nil
+        else
+          output
+        end
       end
+    elsif block_given?
       @code = block
+    else
+      raise ArgumentError, "You must pass either code or a block"
     end
   end
 
   private
 
   def resolve_value
-    return @value if @value
-    return nil if @code.nil?
-
-    if @code.is_a? Proc
+    if @value
+      @value
+    elsif @code.nil?
+      nil
+    elsif @code
       @code.call()
-    else
-      Facter::Core::Execution.exec(@code)
     end
   end
 end
diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb
index 71bf168..fbfdaec 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -22,12 +22,12 @@ describe Facter::Util::Resolution do
     expect(resolution.name).to eq :foo
   end
 
-  it "should be able to set the value" do
+  it "can explicitly set a value" do
     resolution.value = "foo"
     expect(resolution.value).to eq "foo"
   end
 
-  it "should default to nil for code" do
+  it "defaults to nil for code" do
     expect(resolution.code).to be_nil
   end
 
@@ -36,76 +36,52 @@ describe Facter::Util::Resolution do
       Facter.stubs(:warnonce)
     end
 
-    it "should set the code to any provided string" do
+    it "creates a block when given a command" do
       resolution.setcode "foo"
-      expect(resolution.code).to eq "foo"
+      expect(resolution.code).to be_a_kind_of Proc
     end
 
-    it "should set the code to any provided block" do
+    it "stores the provided block when given a block" do
       block = lambda { }
       resolution.setcode(&block)
       resolution.code.should equal(block)
     end
 
-    it "should prefer the string over a block" do
-      resolution.setcode("foo") { }
-      expect(resolution.code).to eq "foo"
+
+    it "prefers a command over a block" do
+      block = lambda { }
+      resolution.setcode("foo", &block)
+      expect(resolution.code).to_not eq block
     end
 
-    it "should fail if neither a string nor block has been provided" do
+    it "fails if neither a string nor block has been provided" do
       expect { resolution.setcode }.to raise_error(ArgumentError)
     end
   end
 
   describe "when returning the value" do
-    it "should return any value that has been provided" do
+    it "returns any value that has been provided" do
       resolution.value = "foo"
       expect(resolution.value).to eq "foo"
     end
 
     describe "and setcode has not been called" do
-      it "should return nil" do
-        Facter::Core::Execution.expects(:exec).with(nil, nil).never
-        resolution.value.should be_nil
+      it "returns nil" do
+        expect(resolution.value).to be_nil
       end
     end
 
     describe "and the code is a string" do
-      describe "on windows" do
-        before do
-          given_a_configuration_of(:is_windows => true)
-        end
-
-        it "should return the result of executing the code" do
-          resolution.setcode "/bin/foo"
-          Facter::Core::Execution.expects(:exec).once.with("/bin/foo").returns "yup"
+      it "returns the result of executing the code" do
+        resolution.setcode "/bin/foo"
+        Facter::Core::Execution.expects(:exec).once.with("/bin/foo").returns "yup"
 
-          expect(resolution.value).to eq "yup"
-        end
-      end
-
-      describe "on non-windows systems" do
-        before do
-          given_a_configuration_of(:is_windows => false)
-        end
-
-        it "should return the result of executing the code" do
-          resolution.setcode "/bin/foo"
-          Facter::Core::Execution.expects(:exec).once.with("/bin/foo").returns "yup"
-
-          expect(resolution.value).to eq "yup"
-        end
+        expect(resolution.value).to eq "yup"
       end
     end
 
     describe "and the code is a block" do
-      it "should warn but not fail if the code fails" do
-        resolution.setcode { raise "feh" }
-        Facter.expects(:warn)
-        resolution.value.should be_nil
-      end
-
-      it "should return the value returned by the block" do
+      it "returns the value returned by the block" do
         resolution.setcode { "yayness" }
         expect(resolution.value).to eq "yayness"
       end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list