[Pkg-puppet-devel] [facter] 238/352: (FACT-273) Resolutions should be able to reference their facts

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:49 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 c8df160008357e2ea7459a66e19fc6964ac7b041
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Tue Feb 11 14:07:44 2014 -0800

    (FACT-273) Resolutions should be able to reference their facts
    
    The fact-resolution relationship is one to many, and there is
    information in the fact that we need in the resolution. This commit
    directly couples facts and resolutions for this purpose.
    
    This commit also adds a healthy refactoring of the fact and resolution
    tests to use modern rspec expectations and use imperative phrasing in
    specifications.
---
 lib/facter/core/aggregate.rb      |   8 ++-
 lib/facter/util/fact.rb           |   4 +-
 lib/facter/util/resolution.rb     |   8 ++-
 spec/unit/core/aggregate_spec.rb  |   4 +-
 spec/unit/util/collection_spec.rb |   2 +-
 spec/unit/util/fact_spec.rb       | 102 +++++++++++++++-----------------------
 spec/unit/util/resolution_spec.rb |  60 +++++++++++-----------
 7 files changed, 91 insertions(+), 97 deletions(-)

diff --git a/lib/facter/core/aggregate.rb b/lib/facter/core/aggregate.rb
index 99f3f72..fa98dff 100644
--- a/lib/facter/core/aggregate.rb
+++ b/lib/facter/core/aggregate.rb
@@ -35,8 +35,14 @@ class Facter::Core::Aggregate
   #   @see Facter::Core::Suitable
   attr_reader :confines
 
-  def initialize(name)
+  # @!attribute [r] fact
+  # @return [Facter::Util::Fact]
+  # @api private
+  attr_reader :fact
+
+  def initialize(name, fact)
     @name = name
+    @fact = fact
 
     @confines = []
     @chunks = {}
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb
index 663c155..75f6580 100644
--- a/lib/facter/util/fact.rb
+++ b/lib/facter/util/fact.rb
@@ -45,7 +45,7 @@ class Facter::Util::Fact
   # @api private
   def add(value = nil, &block)
     begin
-      resolve = Facter::Util::Resolution.new(@name)
+      resolve = Facter::Util::Resolution.new(@name, self)
 
       resolve.instance_eval(&block) if block
       @resolves << resolve
@@ -68,7 +68,7 @@ class Facter::Util::Fact
     resolve = self.resolution(resolve_name)
 
     if resolve.nil?
-      resolve = Facter::Util::Resolution.new(resolve_name)
+      resolve = Facter::Util::Resolution.new(resolve_name, self)
       resolve.instance_eval(&block) if block
       @resolves << resolve
     else
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index 37423f4..af0c7a3 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -42,14 +42,20 @@ class Facter::Util::Resolution
   # @api public
   attr_accessor :name
 
+  # @!attribute [r] fact
+  # @return [Facter::Util::Fact]
+  # @api private
+  attr_reader :fact
+
   # Create a new resolution mechanism.
   #
   # @param name [String] The name of the resolution.
   # @return [void]
   #
   # @api private
-  def initialize(name)
+  def initialize(name, fact)
     @name = name
+    @fact = fact
     @confines = []
     @value = nil
     @timeout = 0
diff --git a/spec/unit/core/aggregate_spec.rb b/spec/unit/core/aggregate_spec.rb
index 6f85022..875734b 100644
--- a/spec/unit/core/aggregate_spec.rb
+++ b/spec/unit/core/aggregate_spec.rb
@@ -3,8 +3,10 @@ require 'facter/core/aggregate'
 
 describe Facter::Core::Aggregate do
 
+  let(:fact) { stub('stub_fact', :name => 'stub_fact') }
+
   subject do
-    obj = described_class.new('aggregated')
+    obj = described_class.new('aggregated', fact)
     obj.aggregate { |chunks| chunks.values }
     obj
   end
diff --git a/spec/unit/util/collection_spec.rb b/spec/unit/util/collection_spec.rb
index 3a80613..b11b468 100755
--- a/spec/unit/util/collection_spec.rb
+++ b/spec/unit/util/collection_spec.rb
@@ -34,7 +34,7 @@ describe Facter::Util::Collection do
       fact = Facter::Util::Fact.new(:myname)
       Facter::Util::Fact.expects(:new).with(:myname, {:timeout => 'myval'}).returns fact
 
-      resolve = Facter::Util::Resolution.new(:myname) {}
+      resolve = Facter::Util::Resolution.new(:myname, fact) {}
       fact.expects(:add).returns resolve
 
       collection.add(:myname, :timeout => "myval") {}
diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb
index 3e2f20e..e1b2ce0 100755
--- a/spec/unit/util/fact_spec.rb
+++ b/spec/unit/util/fact_spec.rb
@@ -4,47 +4,36 @@ require 'spec_helper'
 require 'facter/util/fact'
 
 describe Facter::Util::Fact do
-  it "should require a name" do
-    lambda { Facter::Util::Fact.new }.should raise_error(ArgumentError)
+
+  subject(:fact) { Facter::Util::Fact.new("yay") }
+
+  let(:resolution) { Facter::Util::Resolution.new("yay", fact) }
+
+  it "requires a name" do
+    expect { Facter::Util::Fact.new }.to raise_error(ArgumentError)
   end
 
-  it "should always downcase the name and convert it to a symbol" do
+  it "downcases the name and converts it to a symbol" do
     Facter::Util::Fact.new("YayNess").name.should == :yayness
   end
 
-  it "should issue a deprecation warning for use of ldapname" do
+  it "issues a deprecation warning for use of ldapname" do
     Facter.expects(:warnonce).with("ldapname is deprecated and will be removed in a future version")
     Facter::Util::Fact.new("YayNess", :ldapname => "fooness")
   end
 
-  it "should have a method for adding resolution mechanisms" do
-    Facter::Util::Fact.new("yay").should respond_to(:add)
-  end
-
   describe "when adding resolution mechanisms" do
-    before do
-      @fact = Facter::Util::Fact.new("yay")
+    it "can create a new resolution instance with a block" do
+      Facter::Util::Resolution.expects(:new).at_least_once.returns resolution
 
-      @resolution = Facter::Util::Resolution.new("yay")
+      fact.add { }
     end
 
-    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
-      @fact.add {
+    it "instance_evals the passed block on the new resolution" do
+      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
-      @fact.add { has_weight 1; setcode { "1" } }
-      @fact.add { has_weight 2; setcode { "2" } }
-      @fact.add { has_weight 0; setcode { "0" } }
-      @fact.value.should == "2"
+      expect(fact.value).to eq "foo"
     end
   end
 
@@ -67,7 +56,7 @@ describe Facter::Util::Fact do
 
     it "creates a new resolution if no such resolution exists" do
       res = stub 'resolution', :name => 'named'
-      Facter::Util::Resolution.expects(:new).once.with('named').returns(res)
+      Facter::Util::Resolution.expects(:new).once.with('named', fact).returns(res)
 
       fact.define_resolution('named')
 
@@ -76,7 +65,7 @@ describe Facter::Util::Fact do
 
     it "returns existing resolutions by name" do
       res = stub 'resolution', :name => 'named'
-      Facter::Util::Resolution.expects(:new).once.with('named').returns(res)
+      Facter::Util::Resolution.expects(:new).once.with('named', fact).returns(res)
 
       fact.define_resolution('named')
       fact.define_resolution('named')
@@ -85,57 +74,48 @@ describe Facter::Util::Fact do
     end
   end
 
-  it "should be able to return a value" do
+  it "can able to return a value" do
     Facter::Util::Fact.new("yay").should respond_to(:value)
   end
 
   describe "when returning a value" do
     before do
-      @fact = Facter::Util::Fact.new("yay")
+      fact = Facter::Util::Fact.new("yay")
     end
 
-    it "should return nil if there are no resolutions" do
+    it "returns nil if there are no resolutions" do
       Facter::Util::Fact.new("yay").value.should be_nil
     end
 
-    it "should return the first value returned by a resolution" do
-      r1 = stub 'r1', :weight => 2, :value => nil, :suitable? => true
-      r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
-      r3 = stub 'r3', :weight => 0, :value => "foo", :suitable? => true
-      Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
-      @fact.add { }
-      @fact.add { }
-      @fact.add { }
-
-      @fact.value.should == "yay"
+    it "prefers the highest weight resolution" do
+      fact.add { has_weight 1; setcode { "1" } }
+      fact.add { has_weight 2; setcode { "2" } }
+      fact.add { has_weight 0; setcode { "0" } }
+      expect(fact.value).to eq "2"
     end
 
-    it "should short-cut returning the value once one is found" do
-      r1 = stub 'r1', :weight => 2, :value => "foo", :suitable? => true
-      r2 = stub 'r2', :weight => 1, :suitable? => true # would fail if 'value' were asked for
-      Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
-      @fact.add { }
-      @fact.add { }
-
-      @fact.value
+    it "returns the first value returned by a resolution" do
+      fact.add { has_weight 1; setcode { "1" } }
+      fact.add { has_weight 2; setcode { nil } }
+      fact.add { has_weight 0; setcode { "0" } }
+      expect(fact.value).to eq "1"
     end
 
-    it "should skip unsuitable resolutions" do
-      r1 = stub 'r1', :weight => 2, :suitable? => false # would fail if 'value' were asked for'
-      r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
-      Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
-      @fact.add { }
-      @fact.add { }
+    it "skips unsuitable resolutions" do
+      fact.add { has_weight 1; setcode { "1" } }
+      fact.add do
+        def suitable?; false; end
+        has_weight 2
+        setcode { 2 }
+      end
 
-      @fact.value.should == "yay"
+      expect(fact.value).to eq "1"
     end
 
-    it "should return nil if the value is the empty string" do
-      r1 = stub 'r1', :suitable? => true, :value => ""
-      Facter::Util::Resolution.expects(:new).returns r1
-      @fact.add { }
+    it "returns nil if the value is the empty string" do
+      fact.add { setcode { "" } }
 
-      @fact.value.should be_nil
+      expect(fact.value).to be_nil
     end
   end
 
diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb
index 83d8cd7..bdbe032 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -6,65 +6,67 @@ require 'facter/util/resolution'
 describe Facter::Util::Resolution do
   include FacterSpec::ConfigHelper
 
+  subject(:resolution) { described_class.new(:foo, stub_fact) }
+
+  let(:stub_fact) { stub('fact', :name => :stubfact) }
+
   it "requires a name" do
     expect { Facter::Util::Resolution.new }.to raise_error(ArgumentError)
   end
 
+  it "requires a fact" do
+    expect { Facter::Util::Resolution.new('yay') }.to raise_error(ArgumentError)
+  end
+
   it "can return its name" do
-    Facter::Util::Resolution.new("yay").name.should == "yay"
+    expect(resolution.name).to eq :foo
   end
 
   it "should be able to set the value" do
-    resolve = Facter::Util::Resolution.new("yay")
-    resolve.value = "foo"
-    resolve.value.should == "foo"
+    resolution.value = "foo"
+    expect(resolution.value).to eq "foo"
   end
 
   it "should default to nil for code" do
-    Facter::Util::Resolution.new("yay").code.should be_nil
+    expect(resolution.code).to be_nil
   end
 
   describe "when setting the code" do
     before do
       Facter.stubs(:warnonce)
-      @resolve = Facter::Util::Resolution.new("yay")
     end
 
     it "should set the code to any provided string" do
-      @resolve.setcode "foo"
-      @resolve.code.should == "foo"
+      resolution.setcode "foo"
+      expect(resolution.code).to eq "foo"
     end
 
     it "should set the code to any provided block" do
       block = lambda { }
-      @resolve.setcode(&block)
-      @resolve.code.should equal(block)
+      resolution.setcode(&block)
+      resolution.code.should equal(block)
     end
 
     it "should prefer the string over a block" do
-      @resolve.setcode("foo") { }
-      @resolve.code.should == "foo"
+      resolution.setcode("foo") { }
+      expect(resolution.code).to eq "foo"
     end
 
     it "should fail if neither a string nor block has been provided" do
-      expect { @resolve.setcode }.to raise_error(ArgumentError)
+      expect { resolution.setcode }.to raise_error(ArgumentError)
     end
   end
 
   describe "when returning the value" do
-    before 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"
+      resolution.value = "foo"
+      expect(resolution.value).to eq "foo"
     end
 
     describe "and setcode has not been called" do
       it "should return nil" do
         Facter::Util::Resolution.expects(:exec).with(nil, nil).never
-        @resolve.value.should be_nil
+        resolution.value.should be_nil
       end
     end
 
@@ -75,10 +77,10 @@ describe Facter::Util::Resolution do
         end
 
         it "should return the result of executing the code" do
-          @resolve.setcode "/bin/foo"
+          resolution.setcode "/bin/foo"
           Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns "yup"
 
-          @resolve.value.should == "yup"
+          expect(resolution.value).to eq "yup"
         end
       end
 
@@ -88,31 +90,29 @@ describe Facter::Util::Resolution do
         end
 
         it "should return the result of executing the code" do
-          @resolve.setcode "/bin/foo"
+          resolution.setcode "/bin/foo"
           Facter::Util::Resolution.expects(:exec).once.with("/bin/foo").returns "yup"
 
-          @resolve.value.should == "yup"
+          expect(resolution.value).to eq "yup"
         end
       end
     end
 
     describe "and the code is a block" do
       it "should warn but not fail if the code fails" do
-        @resolve.setcode { raise "feh" }
+        resolution.setcode { raise "feh" }
         Facter.expects(:warn)
-        @resolve.value.should be_nil
+        resolution.value.should be_nil
       end
 
       it "should return the value returned by the block" do
-        @resolve.setcode { "yayness" }
-        @resolve.value.should == "yayness"
+        resolution.setcode { "yayness" }
+        expect(resolution.value).to eq "yayness"
       end
     end
   end
 
   describe "setting options" do
-    subject(:resolution) { described_class.new(:foo) }
-
     it "can set the value" do
       resolution.set_options(:value => 'something')
       expect(resolution.value).to eq 'something'

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