From d56bca8534bd21c046fd19a7fb2f776fe3e100b4 Mon Sep 17 00:00:00 2001 From: Richard Clamp Date: Fri, 18 Mar 2011 00:10:00 +0000 Subject: refactor the mechanism for allowing for resolution ordering to be influenced renames Facter::Util::Resolution#length to weight as a more generic mechanism for allowing resolutions to state their importance --- lib/facter/util/fact.rb | 4 +--- lib/facter/util/loader.rb | 2 +- lib/facter/util/resolution.rb | 17 +++++++---------- spec/unit/util/fact_spec.rb | 22 +++++++++++----------- spec/unit/util/resolution_spec.rb | 22 ++++++++++++++++++++-- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb index e78ed97..935b3c1 100644 --- a/lib/facter/util/fact.rb +++ b/lib/facter/util/fact.rb @@ -41,9 +41,7 @@ class Facter::Util::Fact # Immediately sort the resolutions, so that we always have # a sorted list for looking up values. - # We always want to look them up in the order of number of - # confines, so the most restricted resolution always wins. - @resolves.sort! { |a, b| b.length <=> a.length } + @resolves.sort! { |a, b| b.weight <=> a.weight } return resolve end diff --git a/lib/facter/util/loader.rb b/lib/facter/util/loader.rb index b6aa8de..a52012c 100644 --- a/lib/facter/util/loader.rb +++ b/lib/facter/util/loader.rb @@ -90,7 +90,7 @@ class Facter::Util::Loader next if fact and env_name != fact Facter.add($1) do - from_environment + has_weight 1_000_000 setcode { value } end diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb index 2ca2447..d82fab2 100644 --- a/lib/facter/util/resolution.rb +++ b/lib/facter/util/resolution.rb @@ -85,9 +85,8 @@ class Facter::Util::Resolution end end - # Say this resolution came from the environment - def from_environment - @from_environment = true + def has_weight(weight) + @weight = weight end # Create a new resolution mechanism. @@ -96,15 +95,13 @@ class Facter::Util::Resolution @confines = [] @value = nil @timeout = 0 - @from_environment = false + @weight = nil end - # Return the number of confines. - def length - # If the resolution came from an environment variable - # say we're very very sure about the value of the resolution - if @from_environment - 1_000_000_000 + # Return the importance of this resolution. + def weight + if @weight + @weight else @confines.length end diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb index db08670..523c855 100755 --- a/spec/unit/util/fact_spec.rb +++ b/spec/unit/util/fact_spec.rb @@ -56,10 +56,10 @@ describe Facter::Util::Fact do @fact.add { } end - it "should re-sort the resolutions by length, so the most restricted resolutions are first" do - r1 = stub 'r1', :length => 1 - r2 = stub 'r2', :length => 2 - r3 = stub 'r3', :length => 0 + 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 { } @@ -83,9 +83,9 @@ describe Facter::Util::Fact do end it "should return the first value returned by a resolution" do - r1 = stub 'r1', :length => 2, :value => nil, :suitable? => true - r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true - r3 = stub 'r3', :length => 0, :value => "foo", :suitable? => true + 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 { } @@ -95,8 +95,8 @@ describe Facter::Util::Fact do end it "should short-cut returning the value once one is found" do - r1 = stub 'r1', :length => 2, :value => "foo", :suitable? => true - r2 = stub 'r2', :length => 1, :suitable? => true # would fail if 'value' were asked for + 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 { } @@ -105,8 +105,8 @@ describe Facter::Util::Fact do end it "should skip unsuitable resolutions" do - r1 = stub 'r1', :length => 2, :suitable? => false # would fail if 'value' were asked for' - r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true + 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 { } diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb index 581d0e1..3e13cdc 100755 --- a/spec/unit/util/resolution_spec.rb +++ b/spec/unit/util/resolution_spec.rb @@ -13,6 +13,10 @@ describe Facter::Util::Resolution do Facter::Util::Resolution.new("yay").name.should == "yay" end + it "should have a method for setting the weight" do + Facter::Util::Resolution.new("yay").should respond_to(:has_weight) + end + it "should have a method for setting the code" do Facter::Util::Resolution.new("yay").should respond_to(:setcode) end @@ -195,11 +199,25 @@ describe Facter::Util::Resolution do it "should provide a method for returning the number of confines" do @resolve = Facter::Util::Resolution.new("yay") @resolve.confine "one" => "foo", "two" => "fee" - @resolve.length.should == 2 + @resolve.weight.should == 2 end it "should return 0 confines when no confines have been added" do - Facter::Util::Resolution.new("yay").length.should == 0 + Facter::Util::Resolution.new("yay").weight.should == 0 + end + + it "should provide a way to set the weight" do + @resolve = Facter::Util::Resolution.new("yay") + @resolve.has_weight(45) + @resolve.weight.should == 45 + end + + it "should allow the weight to override the number of confines" do + @resolve = Facter::Util::Resolution.new("yay") + @resolve.confine "one" => "foo", "two" => "fee" + @resolve.weight.should == 2 + @resolve.has_weight(45) + @resolve.weight.should == 45 end it "should have a method for determining if it is suitable" do -- cgit