Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Patrick Carlisle authored February 14, 2012
6  lib/puppet/util/autoload.rb
@@ -3,12 +3,6 @@
3 3
 
4 4
 # Autoload paths, either based on names or all at once.
5 5
 class Puppet::Util::Autoload
6  
-  require 'puppet/util/autoload/file_cache'
7  
-
8  
-  include Puppet::Util
9  
-  include Puppet::Util::Warnings
10  
-  include Puppet::Util::Autoload::FileCache
11  
-
12 6
   @autoloaders = {}
13 7
   @loaded = {}
14 8
 
92  lib/puppet/util/autoload/file_cache.rb
... ...
@@ -1,92 +0,0 @@
1  
-module Puppet::Util::Autoload::FileCache
2  
-  @found_files = {}
3  
-  @missing_files = {}
4  
-  class << self
5  
-    attr_reader :found_files, :missing_files
6  
-  end
7  
-
8  
-  # Only used for testing.
9  
-  def self.clear
10  
-    @found_files.clear
11  
-    @missing_files.clear
12  
-  end
13  
-
14  
-  def found_files
15  
-    Puppet::Util::Autoload::FileCache.found_files
16  
-  end
17  
-
18  
-  def missing_files
19  
-    Puppet::Util::Autoload::FileCache.missing_files
20  
-  end
21  
-
22  
-  def directory_exist?(path)
23  
-    cache = cached_data?(path, :directory?)
24  
-    return cache unless cache.nil?
25  
-
26  
-    protect(path) do
27  
-      stat = File.lstat(path)
28  
-      if stat.directory?
29  
-        found_file(path, stat)
30  
-        return true
31  
-      else
32  
-        missing_file(path)
33  
-        return false
34  
-      end
35  
-    end
36  
-  end
37  
-
38  
-  def file_exist?(path)
39  
-    cache = cached_data?(path)
40  
-    return cache unless cache.nil?
41  
-
42  
-    protect(path) do
43  
-      stat = File.lstat(path)
44  
-      found_file(path, stat)
45  
-      return true
46  
-    end
47  
-  end
48  
-
49  
-  def found_file?(path, type = nil)
50  
-    if data = found_files[path] and ! data_expired?(data[:time])
51  
-      return(type and ! data[:stat].send(type)) ? false : true
52  
-    else
53  
-      return false
54  
-    end
55  
-  end
56  
-
57  
-  def found_file(path, stat)
58  
-    found_files[path] = {:stat => stat, :time => Time.now}
59  
-  end
60  
-
61  
-  def missing_file?(path)
62  
-    !!(time = missing_files[path] and ! data_expired?(time))
63  
-  end
64  
-
65  
-  def missing_file(path)
66  
-    missing_files[path] = Time.now
67  
-  end
68  
-
69  
-  private
70  
-
71  
-  def cached_data?(path, type = nil)
72  
-    if found_file?(path, type)
73  
-      return true
74  
-    elsif missing_file?(path)
75  
-      return false
76  
-    else
77  
-      return nil
78  
-    end
79  
-  end
80  
-
81  
-  def data_expired?(time)
82  
-    Time.now - time > 15
83  
-  end
84  
-
85  
-  def protect(path)
86  
-      yield
87  
-  rescue => detail
88  
-      raise unless detail.class.to_s.include?("Errno")
89  
-      missing_file(path)
90  
-      return false
91  
-  end
92  
-end
158  spec/unit/util/autoload/file_cache_spec.rb
... ...
@@ -1,158 +0,0 @@
1  
-#!/usr/bin/env rspec
2  
-require 'spec_helper'
3  
-require 'puppet/util/autoload/file_cache'
4  
-
5  
-class FileCacheTester
6  
-  include Puppet::Util::Autoload::FileCache
7  
-end
8  
-
9  
-describe Puppet::Util::Autoload::FileCache do
10  
-  before do
11  
-    @cacher = FileCacheTester.new
12  
-  end
13  
-
14  
-  after do
15  
-    Puppet::Util::Autoload::FileCache.clear
16  
-  end
17  
-
18  
-  describe "when checking whether files exist" do
19  
-    it "should have a method for testing whether a file exists" do
20  
-      @cacher.should respond_to(:file_exist?)
21  
-    end
22  
-
23  
-    it "should use lstat to determine whether a file exists" do
24  
-      File.expects(:lstat).with("/my/file")
25  
-      @cacher.file_exist?("/my/file")
26  
-    end
27  
-
28  
-    it "should consider a file as absent if its lstat fails" do
29  
-      File.expects(:lstat).with("/my/file").raises Errno::ENOENT
30  
-      @cacher.should_not be_file_exist("/my/file")
31  
-    end
32  
-
33  
-    it "should consider a file as absent if the directory is absent" do
34  
-      File.expects(:lstat).with("/my/file").raises Errno::ENOTDIR
35  
-      @cacher.should_not be_file_exist("/my/file")
36  
-    end
37  
-
38  
-    it "should consider a file as absent permissions are missing" do
39  
-      File.expects(:lstat).with("/my/file").raises Errno::EACCES
40  
-      @cacher.should_not be_file_exist("/my/file")
41  
-    end
42  
-
43  
-    it "should raise non-fs exceptions" do
44  
-      File.expects(:lstat).with("/my/file").raises ArgumentError
45  
-      lambda { @cacher.file_exist?("/my/file") }.should raise_error(ArgumentError)
46  
-    end
47  
-
48  
-    it "should consider a file as present if its lstat succeeds" do
49  
-      File.expects(:lstat).with("/my/file").returns mock("stat")
50  
-      @cacher.should be_file_exist("/my/file")
51  
-    end
52  
-
53  
-    it "should not stat a file twice in quick succession when the file is missing" do
54  
-      File.expects(:lstat).with("/my/file").once.raises Errno::ENOENT
55  
-      @cacher.should_not be_file_exist("/my/file")
56  
-      @cacher.should_not be_file_exist("/my/file")
57  
-    end
58  
-
59  
-    it "should not stat a file twice in quick succession when the file is present" do
60  
-      File.expects(:lstat).with("/my/file").once.returns mock("stat")
61  
-      @cacher.should be_file_exist("/my/file")
62  
-      @cacher.should be_file_exist("/my/file")
63  
-    end
64  
-
65  
-    it "should expire cached data after 15 seconds" do
66  
-      now = Time.now
67  
-
68  
-      later = now + 16
69  
-
70  
-      Time.expects(:now).times(3).returns(now).then.returns(later).then.returns(later)
71  
-      File.expects(:lstat).with("/my/file").times(2).returns(mock("stat")).then.raises Errno::ENOENT
72  
-      @cacher.should be_file_exist("/my/file")
73  
-      @cacher.should_not be_file_exist("/my/file")
74  
-    end
75  
-
76  
-    it "should share cached data across autoload instances" do
77  
-      File.expects(:lstat).with("/my/file").once.returns mock("stat")
78  
-      other = Puppet::Util::Autoload.new("bar", "tmp")
79  
-
80  
-      @cacher.should be_file_exist("/my/file")
81  
-      other.should be_file_exist("/my/file")
82  
-    end
83  
-  end
84  
-
85  
-  describe "when checking whether files exist" do
86  
-    before do
87  
-      @stat = stub 'stat', :directory? => true
88  
-    end
89  
-
90  
-    it "should have a method for determining whether a directory exists" do
91  
-      @cacher.should respond_to(:directory_exist?)
92  
-    end
93  
-
94  
-    it "should use lstat to determine whether a directory exists" do
95  
-      File.expects(:lstat).with("/my/file").returns @stat
96  
-      @cacher.directory_exist?("/my/file")
97  
-    end
98  
-
99  
-    it "should consider a directory as absent if its lstat fails" do
100  
-      File.expects(:lstat).with("/my/file").raises Errno::ENOENT
101  
-      @cacher.should_not be_directory_exist("/my/file")
102  
-    end
103  
-
104  
-    it "should consider a file as absent if the directory is absent" do
105  
-      File.expects(:lstat).with("/my/file").raises Errno::ENOTDIR
106  
-      @cacher.should_not be_directory_exist("/my/file")
107  
-    end
108  
-
109  
-    it "should consider a file as absent permissions are missing" do
110  
-      File.expects(:lstat).with("/my/file").raises Errno::EACCES
111  
-      @cacher.should_not be_directory_exist("/my/file")
112  
-    end
113  
-
114  
-    it "should raise non-fs exceptions" do
115  
-      File.expects(:lstat).with("/my/file").raises ArgumentError
116  
-      lambda { @cacher.directory_exist?("/my/file") }.should raise_error(ArgumentError)
117  
-    end
118  
-
119  
-    it "should consider a directory as present if its lstat succeeds and the stat is of a directory" do
120  
-      @stat.expects(:directory?).returns true
121  
-      File.expects(:lstat).with("/my/file").returns @stat
122  
-      @cacher.should be_directory_exist("/my/file")
123  
-    end
124  
-
125  
-    it "should consider a directory as absent if its lstat succeeds and the stat is not of a directory" do
126  
-      @stat.expects(:directory?).returns false
127  
-      File.expects(:lstat).with("/my/file").returns @stat
128  
-      @cacher.should_not be_directory_exist("/my/file")
129  
-    end
130  
-
131  
-    it "should not stat a directory twice in quick succession when the file is missing" do
132  
-      File.expects(:lstat).with("/my/file").once.raises Errno::ENOENT
133  
-      @cacher.should_not be_directory_exist("/my/file")
134  
-      @cacher.should_not be_directory_exist("/my/file")
135  
-    end
136  
-
137  
-    it "should not stat a directory twice in quick succession when the file is present" do
138  
-      File.expects(:lstat).with("/my/file").once.returns @stat
139  
-      @cacher.should be_directory_exist("/my/file")
140  
-      @cacher.should be_directory_exist("/my/file")
141  
-    end
142  
-
143  
-    it "should not consider a file to be a directory based on cached data" do
144  
-      @stat.stubs(:directory?).returns false
145  
-      File.stubs(:lstat).with("/my/file").returns @stat
146  
-      @cacher.file_exist?("/my/file")
147  
-      @cacher.should_not be_directory_exist("/my/file")
148  
-    end
149  
-
150  
-    it "should share cached data across autoload instances" do
151  
-      File.expects(:lstat).with("/my/file").once.returns @stat
152  
-      other = Puppet::Util::Autoload.new("bar", "tmp")
153  
-
154  
-      @cacher.should be_directory_exist("/my/file")
155  
-      other.should be_directory_exist("/my/file")
156  
-    end
157  
-  end
158  
-end
4  spec/unit/util/autoload_spec.rb
@@ -65,10 +65,6 @@
65 65
     end
66 66
   end
67 67
 
68  
-  it "should include its FileCache module" do
69  
-    Puppet::Util::Autoload.ancestors.should be_include(Puppet::Util::Autoload::FileCache)
70  
-  end
71  
-
72 68
   describe "when loading a file" do
73 69
     before do
74 70
       @autoload.class.stubs(:search_directories).returns %w{/a}

0 notes on commit a4d3df2

Please sign in to comment.
Something went wrong with that request. Please try again.