From e44082417f4ee5dc1e313adb0b80a1467049a5bc Mon Sep 17 00:00:00 2001 From: Curtis Hawthorne Date: Sun, 21 Feb 2010 22:35:56 -0500 Subject: [PATCH] delete_matched shouldn't throw on missing dir If the cache dir is missing, delete_matched should not throw an error. The cache dir is created as needed by #write, so having a missing cache dir could just be an indication that nothing has been cached yet and should not be treated as an error condition. --- .../lib/active_support/cache/file_store.rb | 22 +++++++++++-------- activesupport/test/caching_test.rb | 8 +++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 7521efe..b05beb2 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -75,15 +75,19 @@ module ActiveSupport end def search_dir(dir, &callback) - Dir.foreach(dir) do |d| - next if d == "." || d == ".." - name = File.join(dir, d) - if File.directory?(name) - search_dir(name, &callback) - else - callback.call name - end - end + begin + Dir.foreach(dir) do |d| + next if d == "." || d == ".." + name = File.join(dir, d) + if File.directory?(name) + search_dir(name, &callback) + else + callback.call name + end + end + rescue SystemCallError + # If the dir doesn't exist, that's fine + end end end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 00e05f7..fe6446e 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -167,6 +167,14 @@ class FileStoreTest < ActiveSupport::TestCase end end +class FileStoreNotExistTest < ActiveSupport::TestCase + def test_expire_not_exist + cache = ActiveSupport::Cache.lookup_store(:file_store, File.join(Dir.pwd, "dir_that_does_not_exist")) + # The dir doesn't exist, but it shouldn't throw an exception + cache.delete_matched(/some.pattern/) + end +end + class MemoryStoreTest < ActiveSupport::TestCase def setup @cache = ActiveSupport::Cache.lookup_store(:memory_store) -- 1.6.0.4