diff options
| -rw-r--r-- | lib/puppet/util/autoload/file_cache.rb | 20 | ||||
| -rwxr-xr-x | spec/unit/util/autoload/file_cache.rb | 30 |
2 files changed, 42 insertions, 8 deletions
diff --git a/lib/puppet/util/autoload/file_cache.rb b/lib/puppet/util/autoload/file_cache.rb index 12400f620..4ad0c7f39 100644 --- a/lib/puppet/util/autoload/file_cache.rb +++ b/lib/puppet/util/autoload/file_cache.rb @@ -23,7 +23,7 @@ module Puppet::Util::Autoload::FileCache cache = cached_data?(path, :directory?) return cache unless cache.nil? - begin + protect(path) do stat = File.lstat(path) if stat.directory? found_file(path, stat) @@ -32,9 +32,6 @@ module Puppet::Util::Autoload::FileCache missing_file(path) return false end - rescue Errno::ENOENT - missing_file(path) - return false end end @@ -42,13 +39,10 @@ module Puppet::Util::Autoload::FileCache cache = cached_data?(path) return cache unless cache.nil? - begin + protect(path) do stat = File.lstat(path) found_file(path, stat) return true - rescue Errno::ENOENT - missing_file(path) - return false end end @@ -108,4 +102,14 @@ module Puppet::Util::Autoload::FileCache def data_expired?(time) Time.now - time > 15 end + + def protect(path) + begin + yield + rescue => detail + raise unless detail.class.to_s.include?("Errno") + missing_file(path) + return false + end + end end diff --git a/spec/unit/util/autoload/file_cache.rb b/spec/unit/util/autoload/file_cache.rb index 333ddb545..59620ed2d 100755 --- a/spec/unit/util/autoload/file_cache.rb +++ b/spec/unit/util/autoload/file_cache.rb @@ -31,6 +31,21 @@ describe Puppet::Util::Autoload::FileCache do @cacher.should_not be_file_exist("/my/file") end + it "should consider a file as absent if the directory is absent" do + File.expects(:lstat).with("/my/file").raises Errno::ENOTDIR + @cacher.should_not be_file_exist("/my/file") + end + + it "should consider a file as absent permissions are missing" do + File.expects(:lstat).with("/my/file").raises Errno::EACCES + @cacher.should_not be_file_exist("/my/file") + end + + it "should raise non-fs exceptions" do + File.expects(:lstat).with("/my/file").raises ArgumentError + lambda { @cacher.file_exist?("/my/file") }.should raise_error(ArgumentError) + end + it "should consider a file as present if its lstat succeeds" do File.expects(:lstat).with("/my/file").returns mock("stat") @cacher.should be_file_exist("/my/file") @@ -87,6 +102,21 @@ describe Puppet::Util::Autoload::FileCache do @cacher.should_not be_directory_exist("/my/file") end + it "should consider a file as absent if the directory is absent" do + File.expects(:lstat).with("/my/file").raises Errno::ENOTDIR + @cacher.should_not be_directory_exist("/my/file") + end + + it "should consider a file as absent permissions are missing" do + File.expects(:lstat).with("/my/file").raises Errno::EACCES + @cacher.should_not be_directory_exist("/my/file") + end + + it "should raise non-fs exceptions" do + File.expects(:lstat).with("/my/file").raises ArgumentError + lambda { @cacher.directory_exist?("/my/file") }.should raise_error(ArgumentError) + end + it "should consider a directory as present if its lstat succeeds and the stat is of a directory" do @stat.expects(:directory?).returns true File.expects(:lstat).with("/my/file").returns @stat |
