Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove Puppet::Util::Autoload::FileCache.

This cache module was complex and had obvious bugs in it, and it's not clear
that it was necessary. We'll add something back if it turns out we needed to
cache file operations in the autoloader.
  • Loading branch information...
commit a4d3df260e2cfc11dfe1db18e1dda6ac8113b743 1 parent 4e8c368
@pcarlisle pcarlisle authored
View
6 lib/puppet/util/autoload.rb
@@ -3,12 +3,6 @@
# Autoload paths, either based on names or all at once.
class Puppet::Util::Autoload
- require 'puppet/util/autoload/file_cache'
-
- include Puppet::Util
- include Puppet::Util::Warnings
- include Puppet::Util::Autoload::FileCache
-
@autoloaders = {}
@loaded = {}
View
92 lib/puppet/util/autoload/file_cache.rb
@@ -1,92 +0,0 @@
-module Puppet::Util::Autoload::FileCache
- @found_files = {}
- @missing_files = {}
- class << self
- attr_reader :found_files, :missing_files
- end
-
- # Only used for testing.
- def self.clear
- @found_files.clear
- @missing_files.clear
- end
-
- def found_files
- Puppet::Util::Autoload::FileCache.found_files
- end
-
- def missing_files
- Puppet::Util::Autoload::FileCache.missing_files
- end
-
- def directory_exist?(path)
- cache = cached_data?(path, :directory?)
- return cache unless cache.nil?
-
- protect(path) do
- stat = File.lstat(path)
- if stat.directory?
- found_file(path, stat)
- return true
- else
- missing_file(path)
- return false
- end
- end
- end
-
- def file_exist?(path)
- cache = cached_data?(path)
- return cache unless cache.nil?
-
- protect(path) do
- stat = File.lstat(path)
- found_file(path, stat)
- return true
- end
- end
-
- def found_file?(path, type = nil)
- if data = found_files[path] and ! data_expired?(data[:time])
- return(type and ! data[:stat].send(type)) ? false : true
- else
- return false
- end
- end
-
- def found_file(path, stat)
- found_files[path] = {:stat => stat, :time => Time.now}
- end
-
- def missing_file?(path)
- !!(time = missing_files[path] and ! data_expired?(time))
- end
-
- def missing_file(path)
- missing_files[path] = Time.now
- end
-
- private
-
- def cached_data?(path, type = nil)
- if found_file?(path, type)
- return true
- elsif missing_file?(path)
- return false
- else
- return nil
- end
- end
-
- def data_expired?(time)
- Time.now - time > 15
- end
-
- def protect(path)
- yield
- rescue => detail
- raise unless detail.class.to_s.include?("Errno")
- missing_file(path)
- return false
- end
-end
View
158 spec/unit/util/autoload/file_cache_spec.rb
@@ -1,158 +0,0 @@
-#!/usr/bin/env rspec
-require 'spec_helper'
-require 'puppet/util/autoload/file_cache'
-
-class FileCacheTester
- include Puppet::Util::Autoload::FileCache
-end
-
-describe Puppet::Util::Autoload::FileCache do
- before do
- @cacher = FileCacheTester.new
- end
-
- after do
- Puppet::Util::Autoload::FileCache.clear
- end
-
- describe "when checking whether files exist" do
- it "should have a method for testing whether a file exists" do
- @cacher.should respond_to(:file_exist?)
- end
-
- it "should use lstat to determine whether a file exists" do
- File.expects(:lstat).with("/my/file")
- @cacher.file_exist?("/my/file")
- end
-
- it "should consider a file as absent if its lstat fails" do
- File.expects(:lstat).with("/my/file").raises Errno::ENOENT
- @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")
- end
-
- it "should not stat a file twice in quick succession when the file is missing" do
- File.expects(:lstat).with("/my/file").once.raises Errno::ENOENT
- @cacher.should_not be_file_exist("/my/file")
- @cacher.should_not be_file_exist("/my/file")
- end
-
- it "should not stat a file twice in quick succession when the file is present" do
- File.expects(:lstat).with("/my/file").once.returns mock("stat")
- @cacher.should be_file_exist("/my/file")
- @cacher.should be_file_exist("/my/file")
- end
-
- it "should expire cached data after 15 seconds" do
- now = Time.now
-
- later = now + 16
-
- Time.expects(:now).times(3).returns(now).then.returns(later).then.returns(later)
- File.expects(:lstat).with("/my/file").times(2).returns(mock("stat")).then.raises Errno::ENOENT
- @cacher.should be_file_exist("/my/file")
- @cacher.should_not be_file_exist("/my/file")
- end
-
- it "should share cached data across autoload instances" do
- File.expects(:lstat).with("/my/file").once.returns mock("stat")
- other = Puppet::Util::Autoload.new("bar", "tmp")
-
- @cacher.should be_file_exist("/my/file")
- other.should be_file_exist("/my/file")
- end
- end
-
- describe "when checking whether files exist" do
- before do
- @stat = stub 'stat', :directory? => true
- end
-
- it "should have a method for determining whether a directory exists" do
- @cacher.should respond_to(:directory_exist?)
- end
-
- it "should use lstat to determine whether a directory exists" do
- File.expects(:lstat).with("/my/file").returns @stat
- @cacher.directory_exist?("/my/file")
- end
-
- it "should consider a directory as absent if its lstat fails" do
- File.expects(:lstat).with("/my/file").raises Errno::ENOENT
- @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
- @cacher.should be_directory_exist("/my/file")
- end
-
- it "should consider a directory as absent if its lstat succeeds and the stat is not of a directory" do
- @stat.expects(:directory?).returns false
- File.expects(:lstat).with("/my/file").returns @stat
- @cacher.should_not be_directory_exist("/my/file")
- end
-
- it "should not stat a directory twice in quick succession when the file is missing" do
- File.expects(:lstat).with("/my/file").once.raises Errno::ENOENT
- @cacher.should_not be_directory_exist("/my/file")
- @cacher.should_not be_directory_exist("/my/file")
- end
-
- it "should not stat a directory twice in quick succession when the file is present" do
- File.expects(:lstat).with("/my/file").once.returns @stat
- @cacher.should be_directory_exist("/my/file")
- @cacher.should be_directory_exist("/my/file")
- end
-
- it "should not consider a file to be a directory based on cached data" do
- @stat.stubs(:directory?).returns false
- File.stubs(:lstat).with("/my/file").returns @stat
- @cacher.file_exist?("/my/file")
- @cacher.should_not be_directory_exist("/my/file")
- end
-
- it "should share cached data across autoload instances" do
- File.expects(:lstat).with("/my/file").once.returns @stat
- other = Puppet::Util::Autoload.new("bar", "tmp")
-
- @cacher.should be_directory_exist("/my/file")
- other.should be_directory_exist("/my/file")
- end
- end
-end
View
4 spec/unit/util/autoload_spec.rb
@@ -65,10 +65,6 @@
end
end
- it "should include its FileCache module" do
- Puppet::Util::Autoload.ancestors.should be_include(Puppet::Util::Autoload::FileCache)
- end
-
describe "when loading a file" do
before do
@autoload.class.stubs(:search_directories).returns %w{/a}
Please sign in to comment.
Something went wrong with that request. Please try again.