From 319822af6d58c3e0c391e86cfd836ec31de43c67 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 11 Feb 2009 14:33:48 -0600 Subject: Fixing #1869 - autoloaded files should never leak exceptions Ruby's exception hierarchy is a bit strange, in that only exceptions that sub RuntimeError are caught by default. This patch explicitly catches the base class, Exception, which means that LoadError, SyntaxError, and any RuntimeErrors will all be caught. This is done for both load() and loadall(); load() uses Kernel.load, but loadall() uses Kernel.require. Signed-off-by: Luke Kanies --- spec/unit/util/autoload.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 spec/unit/util/autoload.rb (limited to 'spec/unit/util/autoload.rb') diff --git a/spec/unit/util/autoload.rb b/spec/unit/util/autoload.rb new file mode 100755 index 000000000..ff717d6c5 --- /dev/null +++ b/spec/unit/util/autoload.rb @@ -0,0 +1,39 @@ +#!/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' + +describe Puppet::Util::Autoload do + before do + @autoload = Puppet::Util::Autoload.new("foo", "tmp") + + @autoload.stubs(:eachdir).yields "/my/dir" + end + + describe "when loading a file" do + [RuntimeError, LoadError, SyntaxError].each do |error| + it "should not die an if a #{error.to_s} exception is thrown" do + FileTest.stubs(:exists?).returns true + + Kernel.expects(:load).raises error + + lambda { @autoload.load("foo") }.should_not raise_error + end + end + end + + describe "when loading all files" do + before do + Dir.stubs(:glob).returns "file.rb" + end + + [RuntimeError, LoadError, SyntaxError].each do |error| + it "should not die an if a #{error.to_s} exception is thrown" do + Kernel.expects(:require).raises error + + lambda { @autoload.loadall }.should_not raise_error + end + end + end +end -- cgit