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 | |
| 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')
| -rwxr-xr-x | spec/integration/util/autoload.rb | 91 | ||||
| -rwxr-xr-x | spec/unit/util/autoload.rb | 17 |
2 files changed, 107 insertions, 1 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 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 |
