Skip to content

Commit

Permalink
Ensure cache directory exists
Browse files Browse the repository at this point in the history
Fixes rails#244
  • Loading branch information
josh committed Nov 16, 2011
1 parent 1b1d6d6 commit 4b5f0d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/sprockets/cache/file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ module Sprockets
module Cache
# A simple file system cache store.
#
# environment.cache = Sprockets::Cache::FileStore.new("tmp/sprockets")
# environment.cache = Sprockets::Cache::FileStore.new("/tmp")
#
class FileStore
def initialize(root)
@root = Pathname.new(root)

# Ensure directory exists
FileUtils.mkdir_p @root
end

# Lookup value in cache
Expand All @@ -24,6 +21,9 @@ def [](key)

# Save value to cache
def []=(key, value)
# Ensure directory exists
FileUtils.mkdir_p @root.join(key).dirname

@root.join(key).open('w') { |f| Marshal.dump(value, f)}
value
end
Expand Down
30 changes: 30 additions & 0 deletions test/test_caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,33 @@ def setup
end
end
end

require 'tmpdir'

class TestFileStore < Sprockets::TestCase
def setup
@cache = Sprockets::Cache::FileStore.new(Dir::tmpdir)

@env1 = Sprockets::Environment.new(fixture_path('default')) do |env|
env.append_path(".")
env.cache = @cache
end

@env2 = Sprockets::Environment.new(fixture_path('symlink')) do |env|
env.append_path(".")
env.cache = @cache
end
end

test "shared cache objects are eql" do
asset1 = @env1['gallery.js']
asset2 = @env2['gallery.js']

assert asset1
assert asset2

assert asset1.eql?(asset2)
assert asset2.eql?(asset1)
assert !asset1.equal?(asset2)
end
end

0 comments on commit 4b5f0d5

Please sign in to comment.