summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/util/autoload.rb1
-rwxr-xr-xspec/integration/util/autoload.rb91
-rwxr-xr-xspec/unit/util/autoload.rb17
-rwxr-xr-xtest/util/autoload.rb116
4 files changed, 108 insertions, 117 deletions
diff --git a/lib/puppet/util/autoload.rb b/lib/puppet/util/autoload.rb
index fb15adf92..b48e3afa1 100644
--- a/lib/puppet/util/autoload.rb
+++ b/lib/puppet/util/autoload.rb
@@ -42,6 +42,7 @@ class Puppet::Util::Autoload
# we can load downloaded plugins if they've already been loaded
# into memory.
def self.loaded(file)
+ $" << file + ".rb" unless $".include?(file)
@loaded << file unless @loaded.include?(file)
end
diff --git a/spec/integration/util/autoload.rb b/spec/integration/util/autoload.rb
new file mode 100755
index 000000000..d0858abfa
--- /dev/null
+++ b/spec/integration/util/autoload.rb
@@ -0,0 +1,91 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+require 'puppet/util/autoload'
+
+class AutoloadIntegrator
+ @things = []
+ def self.newthing(name)
+ @things << name
+ end
+
+ def self.thing?(name)
+ @things.include? name
+ end
+
+ def self.clear
+ @things.clear
+ end
+end
+
+require 'puppet_spec/files'
+
+describe Puppet::Util::Autoload do
+ include PuppetSpec::Files
+
+ def mkfile(name, path)
+ # Now create a file to load
+ File.open(path, "w") do |f|
+ f.puts %{
+AutoloadIntegrator.newthing(:#{name.to_s})
+ }
+ end
+ end
+
+ def mk_loader(name, path)
+ dir = tmpfile(name + path)
+ $: << dir
+
+ Dir.mkdir(dir)
+
+ rbdir = File.join(dir, path.to_s)
+
+ Dir.mkdir(rbdir)
+
+ loader = Puppet::Util::Autoload.new(name, path)
+ return rbdir, loader
+ end
+
+ it "should make instances available by the loading class" do
+ loader = Puppet::Util::Autoload.new("foo", "bar")
+ Puppet::Util::Autoload["foo"].should == loader
+ end
+
+ it "should not fail when asked to load a missing file" do
+ Puppet::Util::Autoload.new("foo", "bar").load(:eh).should be_false
+ end
+
+ it "should load and return true when it successfully loads a file" do
+ dir, loader = mk_loader("foo", "bar")
+ path = File.join(dir, "mything.rb")
+ mkfile(:mything, path)
+ loader.load(:mything).should be_true
+ loader.should be_loaded(:mything)
+ AutoloadIntegrator.should be_thing(:mything)
+ end
+
+ it "should consider a file loaded when asked for the name without an extension" do
+ dir, loader = mk_loader("foo", "bar")
+ path = File.join(dir, "noext.rb")
+ mkfile(:noext, path)
+ loader.load(:noext)
+ loader.should be_loaded(:noext)
+ end
+
+ it "should consider a file loaded when asked for the name with an extension" do
+ dir, loader = mk_loader("foo", "bar")
+ path = File.join(dir, "withext.rb")
+ mkfile(:noext, path)
+ loader.load(:withext)
+ loader.should be_loaded("withext.rb")
+ end
+
+ it "should register the fact that the instance is loaded with the Autoload base class" do
+ dir, loader = mk_loader("foo", "bar")
+ path = File.join(dir, "baseload.rb")
+ mkfile(:baseload, path)
+ loader.load(:baseload)
+ Puppet::Util::Autoload.should be_loaded("bar/withext.rb")
+ end
+end
diff --git a/spec/unit/util/autoload.rb b/spec/unit/util/autoload.rb
index d05bc15f0..c4a8642a0 100755
--- a/spec/unit/util/autoload.rb
+++ b/spec/unit/util/autoload.rb
@@ -82,12 +82,21 @@ describe Puppet::Util::Autoload do
@autoload.should be_named_file_missing("foo")
end
+
+ it "should register loaded files with the main loaded file list so they are not reloaded by ruby" do
+ @autoload.stubs(:file_exist?).returns true
+ Kernel.stubs(:load)
+
+ @autoload.load("myfile")
+
+ $".should be_include("tmp/myfile.rb")
+ end
end
describe "when loading all files" do
before do
@autoload.stubs(:searchpath).returns %w{/a}
- Dir.stubs(:glob).returns "file.rb"
+ Dir.stubs(:glob).returns "/path/to/file.rb"
end
[RuntimeError, LoadError, SyntaxError].each do |error|
@@ -97,5 +106,11 @@ describe Puppet::Util::Autoload do
lambda { @autoload.loadall }.should_not raise_error
end
end
+
+ it "should require the full path to the file" do
+ Kernel.expects(:require).with("/path/to/file.rb")
+
+ @autoload.loadall
+ end
end
end
diff --git a/test/util/autoload.rb b/test/util/autoload.rb
deleted file mode 100755
index ae7624b62..000000000
--- a/test/util/autoload.rb
+++ /dev/null
@@ -1,116 +0,0 @@
-#!/usr/bin/env ruby
-
-require File.dirname(__FILE__) + '/../lib/puppettest'
-
-require 'puppet'
-require 'puppet/util/autoload'
-require 'puppettest'
-
-class TestAutoload < Test::Unit::TestCase
- include PuppetTest
- @things = []
- def self.newthing(name)
- @things << name
- end
-
- def self.thing?(name)
- @things.include? name
- end
-
- def self.clear
- @things.clear
- end
-
- def mkfile(name, path)
- # Now create a file to load
- File.open(path, "w") do |f|
- f.puts %{
-TestAutoload.newthing(:#{name.to_s})
- }
- end
- end
-
- def mk_loader(name)
- dir = tempfile()
- $: << dir
- cleanup do
- $:.delete(dir)
- end
-
- Dir.mkdir(dir)
-
- rbdir = File.join(dir, name.to_s)
-
- Dir.mkdir(rbdir)
-
- loader = nil
- assert_nothing_raised {
- loader = Puppet::Util::Autoload.new(self.class, name)
- }
- return rbdir, loader
- end
-
- def test_load
- dir, loader = mk_loader(:yayness)
-
- assert_equal(loader.object_id, Puppet::Util::Autoload[self.class].object_id,
- "Did not retrieve loader object by class")
-
- # Make sure we don't fail on missing files
- assert_nothing_raised {
- assert_equal(false, loader.load(:mything),
- "got incorrect return on failed load")
- }
-
- # Now create a couple of files for testing
- path = File.join(dir, "mything.rb")
- mkfile(:mything, path)
- opath = File.join(dir, "othing.rb")
- mkfile(:othing, opath)
-
- # Now try to actually load it.
- assert_nothing_raised {
- assert_equal(true, loader.load(:mything),
- "got incorrect return on load")
- }
-
- assert(loader.loaded?(:mything), "Not considered loaded")
-
- assert(self.class.thing?(:mything),
- "Did not get loaded thing")
-
- self.class.clear
-
- [:mything, :othing].each do |thing|
- loader.load(thing)
- assert(loader.loaded?(thing), "#{thing.to_s} not considered loaded")
- assert(loader.loaded?("%s.rb" % thing), "#{thing.to_s} not considered loaded with .rb")
- assert(Puppet::Util::Autoload.loaded?("yayness/%s" % thing), "%s not considered loaded by the main class" % thing)
- assert(Puppet::Util::Autoload.loaded?("yayness/%s.rb" % thing), "%s not considered loaded by the main class with .rb" % thing)
-
- assert(self.class.thing?(thing),
- "Did not get loaded #{thing.to_s}")
- end
- end
-
- # This tests #1027, which was caused by using the unqualified
- # path for requires, which was initially done so that the kernel
- # would keep track of which files got loaded.
- def test_require_uses_full_path
- loadname = "testing"
- loader = Puppet::Util::Autoload.new(self.class, loadname)
-
- basedir = "/some/dir"
- dir = File.join(basedir, loadname)
- loader.expects(:searchpath).returns(dir)
-
- subname = "instance"
-
- file = File.join(dir, subname) + ".rb"
-
- Dir.expects(:glob).with("#{dir}/*.rb").returns(file)
-
- Kernel.expects(:require).with(file)
- loader.loadall
- end
-end