diff options
-rw-r--r-- | lib/facter.rb | 9 | ||||
-rw-r--r-- | lib/facter/util/collection.rb (renamed from lib/facter/collection.rb) | 6 | ||||
-rw-r--r-- | lib/facter/util/confine.rb (renamed from lib/facter/confine.rb) | 2 | ||||
-rw-r--r-- | lib/facter/util/fact.rb (renamed from lib/facter/fact.rb) | 6 | ||||
-rw-r--r-- | lib/facter/util/loader.rb (renamed from lib/facter/loader.rb) | 2 | ||||
-rw-r--r-- | lib/facter/util/resolution.rb (renamed from lib/facter/resolution.rb) | 8 | ||||
-rwxr-xr-x | spec/integration/facter.rb | 9 | ||||
-rwxr-xr-x | spec/unit/util/collection.rb (renamed from spec/unit/collection.rb) | 38 | ||||
-rwxr-xr-x | spec/unit/util/confine.rb (renamed from spec/unit/confine.rb) | 22 | ||||
-rwxr-xr-x | spec/unit/util/fact.rb (renamed from spec/unit/fact.rb) | 42 | ||||
-rwxr-xr-x | spec/unit/util/loader.rb (renamed from spec/unit/loader.rb) | 18 | ||||
-rwxr-xr-x | spec/unit/util/resolution.rb (renamed from spec/unit/resolution.rb) | 50 |
12 files changed, 110 insertions, 102 deletions
diff --git a/lib/facter.rb b/lib/facter.rb index 913060e..0f00f7d 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -18,8 +18,11 @@ #-- module Facter - require 'facter/fact' - require 'facter/collection' + # This is just so the other classes have the constant. + module Util; end + + require 'facter/util/fact' + require 'facter/util/collection' include Comparable include Enumerable @@ -47,7 +50,7 @@ module Facter def self.collection unless defined?(@collection) and @collection - @collection = Facter::Collection.new + @collection = Facter::Util::Collection.new end @collection end diff --git a/lib/facter/collection.rb b/lib/facter/util/collection.rb index 5e6845a..f76b578 100644 --- a/lib/facter/collection.rb +++ b/lib/facter/util/collection.rb @@ -1,9 +1,9 @@ require 'facter' -require 'facter/resolution' +require 'facter/util/fact' # Manage which facts exist and how we access them. Largely just a wrapper # around a hash of facts. -class Facter::Collection +class Facter::Util::Collection # Return a fact object by name. If you use this, you still have to call # 'value' on it to retrieve the actual value. def [](name) @@ -16,7 +16,7 @@ class Facter::Collection name = canonize(name) unless fact = @facts[name] - fact = Facter::Fact.new(name, options) + fact = Facter::Util::Fact.new(name, options) @facts[name] = fact end diff --git a/lib/facter/confine.rb b/lib/facter/util/confine.rb index 529c968..a430bbe 100644 --- a/lib/facter/confine.rb +++ b/lib/facter/util/confine.rb @@ -1,6 +1,6 @@ # A restricting tag for fact resolution mechanisms. The tag must be true # for the resolution mechanism to be suitable. -class Facter::Confine +class Facter::Util::Confine attr_accessor :fact, :values # Add the restriction. Requires the fact name, an operator, and the value diff --git a/lib/facter/fact.rb b/lib/facter/util/fact.rb index 9514cd0..c73dfbf 100644 --- a/lib/facter/fact.rb +++ b/lib/facter/util/fact.rb @@ -1,7 +1,7 @@ require 'facter' -require 'facter/resolution' +require 'facter/util/resolution' -class Facter::Fact +class Facter::Util::Fact attr_accessor :name, :searching, :ldapname # Create a new fact, with no resolution mechanisms. @@ -31,7 +31,7 @@ class Facter::Fact def add(&block) raise ArgumentError, "You must pass a block to Fact<instance>.add" unless block_given? - resolve = Facter::Resolution.new(@name) + resolve = Facter::Util::Resolution.new(@name) resolve.instance_eval(&block) diff --git a/lib/facter/loader.rb b/lib/facter/util/loader.rb index 3284cc4..7031172 100644 --- a/lib/facter/loader.rb +++ b/lib/facter/util/loader.rb @@ -1,7 +1,7 @@ require 'facter' # Load facts on demand. -class Facter::Loader +class Facter::Util::Loader # Load all resolutions for a single fact. def load(fact) # Now load from the search path diff --git a/lib/facter/resolution.rb b/lib/facter/util/resolution.rb index 4df55b7..b6aae77 100644 --- a/lib/facter/resolution.rb +++ b/lib/facter/util/resolution.rb @@ -3,9 +3,9 @@ # specific systems. Note that the confinements are always ANDed, so any # confinements specified must all be true for the resolution to be # suitable. -require 'facter/confine' +require 'facter/util/confine' -class Facter::Resolution +class Facter::Util::Resolution attr_accessor :interpreter, :code, :name def self.have_which @@ -51,7 +51,7 @@ class Facter::Resolution # Add a new confine to the resolution mechanism. def confine(confines) confines.each do |fact, values| - @confines.push Facter::Confine.new(fact, *values) + @confines.push Facter::Util::Confine.new(fact, *values) end end @@ -98,7 +98,7 @@ class Facter::Resolution if @code.is_a?(Proc) result = @code.call() else - result = Facter::Resolution.exec(@code,@interpreter) + result = Facter::Util::Resolution.exec(@code,@interpreter) end return nil if result == "" diff --git a/spec/integration/facter.rb b/spec/integration/facter.rb index b503c9d..e1f2025 100755 --- a/spec/integration/facter.rb +++ b/spec/integration/facter.rb @@ -8,10 +8,15 @@ describe Facter do Facter.loadfacts end + after do + Facter.reset + end + it "should create a new collection if one does not exist" do Facter.reset - Facter::Collection.expects(:new).returns "coll" - Facter.collection.should == "coll" + coll = mock('coll') + Facter::Util::Collection.stubs(:new).returns coll + Facter.collection.should equal(coll) Facter.reset end diff --git a/spec/unit/collection.rb b/spec/unit/util/collection.rb index d69888e..e88de06 100755 --- a/spec/unit/collection.rb +++ b/spec/unit/util/collection.rb @@ -1,22 +1,22 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '/../../spec_helper' -require 'facter/collection' +require 'facter/util/collection' -describe Facter::Collection do +describe Facter::Util::Collection do it "should have a method for adding facts" do - Facter::Collection.new.should respond_to(:add) + Facter::Util::Collection.new.should respond_to(:add) end describe "when adding facts" do before do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new end it "should create a new fact if no fact with the same name already exists" do fact = mock 'fact' - Facter::Fact.expects(:new).with { |name, *args| name == :myname }.returns fact + Facter::Util::Fact.expects(:new).with { |name, *args| name == :myname }.returns fact @coll.add(:myname) end @@ -24,7 +24,7 @@ describe Facter::Collection do describe "and a block is provided" do it "should use the block to add a resolution to the fact" do fact = mock 'fact' - Facter::Fact.expects(:new).returns fact + Facter::Util::Fact.expects(:new).returns fact fact.expects(:add) @@ -34,12 +34,12 @@ describe Facter::Collection do end it "should have a method for retrieving facts by name" do - Facter::Collection.new.should respond_to(:fact) + Facter::Util::Collection.new.should respond_to(:fact) end describe "when retrieving facts" do before do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @fact = @coll.add("YayNess") end @@ -58,12 +58,12 @@ describe Facter::Collection do end it "should have a method for returning a fact's value" do - Facter::Collection.new.should respond_to(:value) + Facter::Util::Collection.new.should respond_to(:value) end describe "when returning a fact's value" do before do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @fact = @coll.add("YayNess") @fact.stubs(:value).returns "result" @@ -85,13 +85,13 @@ describe Facter::Collection do end it "should return the fact's value when the array index method is used" do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @coll.expects(:value).with("myfact").returns "foo" @coll["myfact"].should == "foo" end it "should have a method for flushing all facts" do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @fact = @coll.add("YayNess") @fact.expects(:flush) @@ -100,7 +100,7 @@ describe Facter::Collection do end it "should have a method that returns all fact names" do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @coll.add(:one) @coll.add(:two) @@ -108,12 +108,12 @@ describe Facter::Collection do end it "should have a method for returning a hash of fact values" do - Facter::Collection.new.should respond_to(:to_hash) + Facter::Util::Collection.new.should respond_to(:to_hash) end describe "when returning a hash of values" do before do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @fact = @coll.add(:one) @fact.stubs(:value).returns "me" end @@ -130,16 +130,16 @@ describe Facter::Collection do end it "should have a method for iterating over all facts" do - Facter::Collection.new.should respond_to(:each) + Facter::Util::Collection.new.should respond_to(:each) end it "should include Enumerable" do - Facter::Collection.ancestors.should be_include(Enumerable) + Facter::Util::Collection.ancestors.should be_include(Enumerable) end describe "when iterating over facts" do before do - @coll = Facter::Collection.new + @coll = Facter::Util::Collection.new @one = @coll.add(:one) @two = @coll.add(:two) end diff --git a/spec/unit/confine.rb b/spec/unit/util/confine.rb index ae122ea..5c1ce3b 100755 --- a/spec/unit/confine.rb +++ b/spec/unit/util/confine.rb @@ -1,41 +1,41 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '/../../spec_helper' -require 'facter/confine' +require 'facter/util/confine' -describe Facter::Confine do +describe Facter::Util::Confine do it "should require a fact name" do - Facter::Confine.new("yay", true).fact.should == "yay" + Facter::Util::Confine.new("yay", true).fact.should == "yay" end it "should accept a value specified individually" do - Facter::Confine.new("yay", "test").values.should == ["test"] + Facter::Util::Confine.new("yay", "test").values.should == ["test"] end it "should accept multiple values specified at once" do - Facter::Confine.new("yay", "test", "other").values.should == ["test", "other"] + Facter::Util::Confine.new("yay", "test", "other").values.should == ["test", "other"] end it "should convert all values to strings" do - Facter::Confine.new("yay", :test).values.should == %w{test} + Facter::Util::Confine.new("yay", :test).values.should == %w{test} end it "should fail if no fact name is provided" do - lambda { Facter::Confine.new(nil, :test) }.should raise_error(ArgumentError) + lambda { Facter::Util::Confine.new(nil, :test) }.should raise_error(ArgumentError) end it "should fail if no values were provided" do - lambda { Facter::Confine.new("yay") }.should raise_error(ArgumentError) + lambda { Facter::Util::Confine.new("yay") }.should raise_error(ArgumentError) end it "should have a method for testing whether it matches" do - Facter::Confine.new("yay", :test).should respond_to(:true?) + Facter::Util::Confine.new("yay", :test).should respond_to(:true?) end describe "when evaluating" do before do - @confine = Facter::Confine.new("yay", "one", "two") + @confine = Facter::Util::Confine.new("yay", "one", "two") @fact = mock 'fact' Facter.stubs(:[]).returns @fact end diff --git a/spec/unit/fact.rb b/spec/unit/util/fact.rb index 3772d0f..1652032 100755 --- a/spec/unit/fact.rb +++ b/spec/unit/util/fact.rb @@ -1,37 +1,37 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '/../../spec_helper' -require 'facter/fact' +require 'facter/util/fact' -describe Facter::Fact do +describe Facter::Util::Fact do it "should require a name" do - lambda { Facter::Fact.new }.should raise_error(ArgumentError) + lambda { Facter::Util::Fact.new }.should raise_error(ArgumentError) end it "should always downcase the name and convert it to a symbol" do - Facter::Fact.new("YayNess").name.should == :yayness + Facter::Util::Fact.new("YayNess").name.should == :yayness end it "should default to its name converted to a string as its ldapname" do - Facter::Fact.new("YayNess").ldapname.should == "yayness" + Facter::Util::Fact.new("YayNess").ldapname.should == "yayness" end it "should allow specifying the ldap name at initialization" do - Facter::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness" + Facter::Util::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness" end it "should fail if an unknown option is provided" do - lambda { Facter::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError) + lambda { Facter::Util::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError) end it "should have a method for adding resolution mechanisms" do - Facter::Fact.new("yay").should respond_to(:add) + Facter::Util::Fact.new("yay").should respond_to(:add) end describe "when adding resolution mechanisms" do before do - @fact = Facter::Fact.new("yay") + @fact = Facter::Util::Fact.new("yay") @resolution = mock 'resolution' @resolution.stub_everything @@ -43,7 +43,7 @@ describe Facter::Fact do end it "should create a new resolution instance" do - Facter::Resolution.expects(:new).returns @resolution + Facter::Util::Resolution.expects(:new).returns @resolution @fact.add { } end @@ -51,7 +51,7 @@ describe Facter::Fact do it "should instance_eval the passed block on the new resolution" do @resolution.expects(:instance_eval) - Facter::Resolution.stubs(:new).returns @resolution + Facter::Util::Resolution.stubs(:new).returns @resolution @fact.add { } end @@ -60,7 +60,7 @@ describe Facter::Fact do r1 = stub 'r1', :length => 1 r2 = stub 'r2', :length => 2 r3 = stub 'r3', :length => 0 - Facter::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) + Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) @fact.add { } @fact.add { } @fact.add { } @@ -70,23 +70,23 @@ describe Facter::Fact do end it "should be able to return a value" do - Facter::Fact.new("yay").should respond_to(:value) + Facter::Util::Fact.new("yay").should respond_to(:value) end describe "when returning a value" do before do - @fact = Facter::Fact.new("yay") + @fact = Facter::Util::Fact.new("yay") end it "should return nil if there are no resolutions" do - Facter::Fact.new("yay").value.should be_nil + Facter::Util::Fact.new("yay").value.should be_nil 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 - Facter::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) + Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3) @fact.add { } @fact.add { } @fact.add { } @@ -97,7 +97,7 @@ describe Facter::Fact do 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 - Facter::Resolution.expects(:new).times(2).returns(r1).returns(r2) + Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2) @fact.add { } @fact.add { } @@ -107,7 +107,7 @@ describe Facter::Fact do 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 - Facter::Resolution.expects(:new).times(2).returns(r1).returns(r2) + Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2) @fact.add { } @fact.add { } @@ -116,7 +116,7 @@ describe Facter::Fact do it "should return nil if the value is the empty string" do r1 = stub 'r1', :suitable? => true, :value => "" - Facter::Resolution.expects(:new).returns r1 + Facter::Util::Resolution.expects(:new).returns r1 @fact.add { } @fact.value.should be_nil @@ -124,6 +124,6 @@ describe Facter::Fact do end it "should have a method for flushing the cached fact" do - Facter::Fact.new(:foo).should respond_to(:flush) + Facter::Util::Fact.new(:foo).should respond_to(:flush) end end diff --git a/spec/unit/loader.rb b/spec/unit/util/loader.rb index 75146e1..4c0d777 100755 --- a/spec/unit/loader.rb +++ b/spec/unit/util/loader.rb @@ -1,8 +1,8 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '/../../spec_helper' -require 'facter/loader' +require 'facter/util/loader' # Make sure we have a Puppet constant, so we can test # loading Puppet facts. @@ -10,7 +10,7 @@ unless defined?(Puppet) class Puppet; end end -describe Facter::Loader do +describe Facter::Util::Loader do def with_env(values) old = {} values.each do |var, value| @@ -30,20 +30,20 @@ describe Facter::Loader do end it "should have a method for loading individual facts by name" do - Facter::Loader.new.should respond_to(:load) + Facter::Util::Loader.new.should respond_to(:load) end it "should have a method for loading all facts" do - Facter::Loader.new.should respond_to(:load_all) + Facter::Util::Loader.new.should respond_to(:load_all) end it "should have a method for returning directories containing facts" do - Facter::Loader.new.should respond_to(:search_path) + Facter::Util::Loader.new.should respond_to(:search_path) end describe "when determining the search path" do before do - @loader = Facter::Loader.new + @loader = Facter::Util::Loader.new @settings = mock 'settings' @settings.stubs(:value).returns "/eh" Puppet.stubs(:settings).returns @settings @@ -84,7 +84,7 @@ describe Facter::Loader do describe "when loading facts" do before do - @loader = Facter::Loader.new + @loader = Facter::Util::Loader.new @loader.stubs(:search_path).returns [] end @@ -135,7 +135,7 @@ describe Facter::Loader do describe "when loading all facts" do before do - @loader = Facter::Loader.new + @loader = Facter::Util::Loader.new @loader.stubs(:search_path).returns [] end diff --git a/spec/unit/resolution.rb b/spec/unit/util/resolution.rb index a54b872..493ee3a 100755 --- a/spec/unit/resolution.rb +++ b/spec/unit/util/resolution.rb @@ -1,25 +1,25 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '/../../spec_helper' -require 'facter/resolution' +require 'facter/util/resolution' -describe Facter::Resolution do +describe Facter::Util::Resolution do it "should require a name" do - lambda { Facter::Resolution.new }.should raise_error(ArgumentError) + lambda { Facter::Util::Resolution.new }.should raise_error(ArgumentError) end it "should have a name" do - Facter::Resolution.new("yay").name.should == "yay" + Facter::Util::Resolution.new("yay").name.should == "yay" end it "should have a method for setting the code" do - Facter::Resolution.new("yay").should respond_to(:setcode) + Facter::Util::Resolution.new("yay").should respond_to(:setcode) end describe "when setting the code" do before do - @resolve = Facter::Resolution.new("yay") + @resolve = Facter::Util::Resolution.new("yay") end it "should default to /bin/sh as the interpreter if a string is provided" do @@ -49,25 +49,25 @@ describe Facter::Resolution do end it "should be able to return a value" do - Facter::Resolution.new("yay").should respond_to(:value) + Facter::Util::Resolution.new("yay").should respond_to(:value) end describe "when returning the value" do before do - @resolve = Facter::Resolution.new("yay") + @resolve = Facter::Util::Resolution.new("yay") end describe "and the code is a string" do it "should return the result of executing the code with the interpreter" do @resolve.setcode "/bin/foo" - Facter::Resolution.expects(:exec).with("/bin/foo", "/bin/sh").returns "yup" + Facter::Util::Resolution.expects(:exec).with("/bin/foo", "/bin/sh").returns "yup" @resolve.value.should == "yup" end it "should return nil if the value is an empty string" do @resolve.setcode "/bin/foo" - Facter::Resolution.stubs(:exec).returns "" + Facter::Util::Resolution.stubs(:exec).returns "" @resolve.value.should be_nil end end @@ -86,41 +86,41 @@ describe Facter::Resolution do end it "should return its value when converted to a string" do - @resolve = Facter::Resolution.new("yay") + @resolve = Facter::Util::Resolution.new("yay") @resolve.expects(:value).returns "myval" @resolve.to_s.should == "myval" end it "should allow the adding of confines" do - Facter::Resolution.new("yay").should respond_to(:confine) + Facter::Util::Resolution.new("yay").should respond_to(:confine) end it "should provide a method for returning the number of confines" do - @resolve = Facter::Resolution.new("yay") + @resolve = Facter::Util::Resolution.new("yay") @resolve.confine "one" => "foo", "two" => "fee" @resolve.length.should == 2 end it "should return 0 confines when no confines have been added" do - Facter::Resolution.new("yay").length.should == 0 + Facter::Util::Resolution.new("yay").length.should == 0 end it "should have a method for determining if it is suitable" do - Facter::Resolution.new("yay").should respond_to(:suitable?) + Facter::Util::Resolution.new("yay").should respond_to(:suitable?) end describe "when adding confines" do before do - @resolve = Facter::Resolution.new("yay") + @resolve = Facter::Util::Resolution.new("yay") end it "should accept a hash of fact names and values" do lambda { @resolve.confine :one => "two" }.should_not raise_error end - it "should create a Confine instance for every argument in the provided hash" do - Facter::Confine.expects(:new).with("one", "foo") - Facter::Confine.expects(:new).with("two", "fee") + it "should create a Util::Confine instance for every argument in the provided hash" do + Facter::Util::Confine.expects(:new).with("one", "foo") + Facter::Util::Confine.expects(:new).with("two", "fee") @resolve.confine "one" => "foo", "two" => "fee" end @@ -129,7 +129,7 @@ describe Facter::Resolution do describe "when determining suitability" do before do - @resolve = Facter::Resolution.new("yay") + @resolve = Facter::Util::Resolution.new("yay") end it "should always be suitable if no confines have been added" do @@ -139,7 +139,7 @@ describe Facter::Resolution do it "should be unsuitable if any provided confines return false" do confine1 = mock 'confine1', :true? => true confine2 = mock 'confine2', :true? => false - Facter::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) + Facter::Util::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) @resolve.confine :one => :two, :three => :four @resolve.should_not be_suitable @@ -148,7 +148,7 @@ describe Facter::Resolution do it "should be suitable if all provided confines return true" do confine1 = mock 'confine1', :true? => true confine2 = mock 'confine2', :true? => true - Facter::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) + Facter::Util::Confine.expects(:new).times(2).returns(confine1).then.returns(confine2) @resolve.confine :one => :two, :three => :four @resolve.should be_suitable @@ -156,13 +156,13 @@ describe Facter::Resolution do end it "should have a class method for executing code" do - Facter::Resolution.should respond_to(:exec) + Facter::Util::Resolution.should respond_to(:exec) end # It's not possible, AFAICT, to mock %x{}, so I can't really test this bit. describe "when executing code" do it "should fail if any interpreter other than /bin/sh is requested" do - lambda { Facter::Resolution.exec("/something", "/bin/perl") }.should raise_error(ArgumentError) + lambda { Facter::Util::Resolution.exec("/something", "/bin/perl") }.should raise_error(ArgumentError) end end end |