diff options
| author | luke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a> | 2006-05-23 19:45:38 +0000 |
|---|---|---|
| committer | luke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a> | 2006-05-23 19:45:38 +0000 |
| commit | 99b61e7c525400a851d1a39d36fec7f062168bc8 (patch) | |
| tree | da514476089922ce97f03670d67e167d1ef323af | |
| parent | 97f1a5eb1bdf008a60dc051d47095a4d250ba9a6 (diff) | |
| download | facter-99b61e7c525400a851d1a39d36fec7f062168bc8.tar.gz facter-99b61e7c525400a851d1a39d36fec7f062168bc8.tar.xz facter-99b61e7c525400a851d1a39d36fec7f062168bc8.zip | |
Adding final autoloading work.
git-svn-id: http://reductivelabs.com/svn/facter/trunk@106 1f5c1d6a-bddf-0310-8f58-fc49e503516a
| -rw-r--r-- | lib/facter.rb | 79 | ||||
| -rw-r--r-- | tests/tc_simple.rb | 86 |
2 files changed, 140 insertions, 25 deletions
diff --git a/lib/facter.rb b/lib/facter.rb index 14e2788..f99fefe 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -110,30 +110,29 @@ FACTERVERSION = '1.1.4' if fact = self[name] return fact.value else - p @@facts.keys super end end end - def Facter.value(name) - if @@facts.include?(name.to_s.downcase) - @@facts[name.to_s.downcase].value - else - nil + # Load a file by path + def Facter.autoload(file) + name = File.basename(file).sub(".rb",'') + begin + require file + unless @@facts.include?(name) + warn "Loaded %s but it did not define fact %s" % [file, name] + end + rescue LoadError => detail + warn "Failed to load %s: %s" % [file, detail] end end - # Flush all cached values. - def Facter.flush - @@facts.each { |fact| fact.flush } - end - - # Remove them all. - def Facter.reset - @@facts.each { |name,fact| - @@facts.delete(name) - } + # Clear all facts. Mostly used for testing. + def Facter.clear + Facter.reset + Facter.flush + @@facts.clear end # Set debugging on or off. @@ -162,11 +161,23 @@ FACTERVERSION = '1.1.4' end end + # Flush all cached values. + def Facter.flush + @@facts.each { |fact| fact.flush } + end + # Return a list of all of the facts. def Facter.list return @@facts.keys end + # Remove them all. + def Facter.reset + @@facts.each { |name,fact| + @@facts.delete(name) + } + end + # Return a hash of all of our facts. def Facter.to_hash self.inject({}) do |h, ary| @@ -175,6 +186,14 @@ FACTERVERSION = '1.1.4' end end + def Facter.value(name) + if @@facts.include?(name.to_s.downcase) + @@facts[name.to_s.downcase].value + else + nil + end + end + # Compare one value to another. def <=>(other) return self.value <=> other @@ -834,14 +853,28 @@ FACTERVERSION = '1.1.4' tag("operatingsystem","Linux") setcode "whoami" end + + locals = [] + + # Now see if we can find any other facts + $:.each do |dir| + fdir = File.join(dir, "facter") + if FileTest.exists?(fdir) + Dir.chdir(fdir) do + Dir.glob("*.rb").each do |file| + if file == "local.rb" + # Just require it normally + require File.join(fdir, file) + else + # We assume it's a new fact to be + # autoloaded, so ask Facter to load it for us + Facter.autoload(File.join(fdir, file)) + end + end + end + end + end end Facter.load end - -# try to load a local fact library, if there happens to be one -begin - require 'facter/local' -rescue LoadError - # no worries -end diff --git a/tests/tc_simple.rb b/tests/tc_simple.rb index 50ccf8a..1e1ac6c 100644 --- a/tests/tc_simple.rb +++ b/tests/tc_simple.rb @@ -23,8 +23,7 @@ class TestFacter < Test::Unit::TestCase def teardown # clear out the list of facts, so we start fresh for every test - Facter.reset - Facter.flush + Facter.clear @tmpfiles.each do |file| if FileTest.exists?(file) @@ -333,6 +332,89 @@ end assert(Facter.rubyversion, "Could not get ruby version") } end + + # Verify we autoload everything from the start. + def test_initautoloading + dir = "/tmp/facterloading" + @tmpfiles << dir + Dir.mkdir(dir) + Dir.mkdir(File.join(dir, "facter")) + $: << dir + + # Make sure we don't have a value right now. + assert_raise(NoMethodError) do + Facter.initautoloadfact + end + assert_nil(Facter["initautoloadfact"]) + + # Make our file + val = "autoloadedness" + File.open(File.join(dir, "facter", "initautoloadfact.rb"), "w") do |file| + file.puts %{ +Facter.add("InitAutoloadFact") do + setcode { "#{val}" } +end +} + end + + # Now reset and reload + Facter.reset + Facter.flush + + # And load + assert_nothing_raised { + Facter.load + } + + hash = nil + assert_nothing_raised { + hash = Facter.to_hash + } + + assert(hash.include?("initautoloadfact"), "Did not load fact at startup") + assert_equal(val, hash["initautoloadfact"], "Did not get correct value") + end + + def test_localfacts + dir = "/tmp/localloading" + @tmpfiles << dir + Dir.mkdir(dir) + Dir.mkdir(File.join(dir, "facter")) + $: << dir + + # Make sure we don't have a value right now. + assert_raise(NoMethodError) do + Facter.localfact + end + assert_nil(Facter["localfact"]) + + # Make our file + val = "localness" + File.open(File.join(dir, "facter", "local.rb"), "w") do |file| + file.puts %{ +Facter.add("LocalFact") do + setcode { "#{val}" } +end +} + end + + # Now reset and reload + Facter.reset + Facter.flush + + # And load + assert_nothing_raised { + Facter.load + } + + hash = nil + assert_nothing_raised { + hash = Facter.to_hash + } + + assert(hash.include?("localfact"), "Did not load fact at startup") + assert_equal(val, hash["localfact"], "Did not get correct value") + end end # $Id$ |
