If a fact file referenced an unresolvable fact, or a fact that would be
later in the search path than the fact file, facter would attempt to
load and resolve that fact first and recurse into the same file.

The loader now stores the path of every file it has attempted to load,
and before loading a new file it will ensure that the file has not been
loaded before.

Signed-off-by: Adrien Thebo <adr...@puppetlabs.com>
---
Local-branch: ticket/master/8491-remove_multiple_fact_loading
 lib/facter/util/loader.rb                    |    7 +++++++
 spec/fixtures/unit/util/loader/nosuchfact.rb |    1 +
 spec/unit/util/loader_spec.rb                |    8 ++++++++
 3 files changed, 16 insertions(+), 0 deletions(-)
 create mode 100644 spec/fixtures/unit/util/loader/nosuchfact.rb

diff --git a/lib/facter/util/loader.rb b/lib/facter/util/loader.rb
index a52012c..83cdb6d 100644
--- a/lib/facter/util/loader.rb
+++ b/lib/facter/util/loader.rb
@@ -2,6 +2,11 @@ require 'facter'
 
 # Load facts on demand.
 class Facter::Util::Loader
+
+    def initialize
+      @loaded = []
+    end
+
     # Load all resolutions for a single fact.
     def load(fact)
         # Now load from the search path
@@ -68,8 +73,10 @@ class Facter::Util::Loader
     end
 
     def load_file(file)
+        return if @loaded.include? file
         # We have to specify Kernel.load, because we have a load method.
         begin
+            @loaded << file
             Kernel.load(file)
         rescue ScriptError => detail
             warn "Error loading fact #{file} #{detail}"
diff --git a/spec/fixtures/unit/util/loader/nosuchfact.rb 
b/spec/fixtures/unit/util/loader/nosuchfact.rb
new file mode 100644
index 0000000..f0ba60a
--- /dev/null
+++ b/spec/fixtures/unit/util/loader/nosuchfact.rb
@@ -0,0 +1 @@
+Facter.value(:nosuchfact)
diff --git a/spec/unit/util/loader_spec.rb b/spec/unit/util/loader_spec.rb
index 1bc909f..3fb1f04 100755
--- a/spec/unit/util/loader_spec.rb
+++ b/spec/unit/util/loader_spec.rb
@@ -280,4 +280,12 @@ describe Facter::Util::Loader do
             @loader.load_all
         end
     end
+
+    it "should load facts on the facter search path only once" do
+        facterlibdir = File.expand_path(File.dirname(__FILE__) + 
'../../../fixtures/unit/util/loader')
+        with_env 'FACTERLIB' => facterlibdir do
+            Facter::Util::Loader.new.load_all
+            Facter.value(:nosuchfact).should be_nil
+        end
+    end
 end
-- 
1.7.6

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to