diff options
author | Luke Kanies <luke@madstop.com> | 2009-06-16 13:25:07 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-07-16 21:23:03 +1000 |
commit | eb40966777beaff2ceca8b91e10e3cb0132ec218 (patch) | |
tree | 006e5f960ee4da5391672b972aa8ab5e5d9a4825 /spec/integration/util/autoload.rb | |
parent | a42e8788b1fde5273ba1e34f6d90c451394849b8 (diff) | |
download | puppet-eb40966777beaff2ceca8b91e10e3cb0132ec218.tar.gz puppet-eb40966777beaff2ceca8b91e10e3cb0132ec218.tar.xz puppet-eb40966777beaff2ceca8b91e10e3cb0132ec218.zip |
Ruby no longer clobbers puppet autoloading
We basically just make sure that we tell Ruby
about files we've loaded, so you can 'require' these
files and doing so will essentially no-op, rather
than clobbering the already-loaded code.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'spec/integration/util/autoload.rb')
-rwxr-xr-x | spec/integration/util/autoload.rb | 91 |
1 files changed, 91 insertions, 0 deletions
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 |