Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schneems/test for 141 3.x #143

Merged
merged 2 commits into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/sprockets/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ def fetch_asset_from_dependency_cache(unloaded, limit = 3)

history = cache.get(key) || []
history.each_with_index do |deps, index|
deps.map! { |path| path.start_with?("file-digest://") ? expand_from_root(path) : path }
if asset = yield(deps)
expanded_deps = deps.map do |path|
path.start_with?("file-digest://") ? expand_from_root(path) : path
end
if asset = yield(expanded_deps)
cache.set(key, history.rotate!(index)) if index > 0
return asset
end
Expand Down
35 changes: 35 additions & 0 deletions test/test_caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,41 @@ def teardown
end
end

test "history cache is not polluted" do
dependency_file = File.join(fixture_path('asset'), "dependencies", "a.js")
env1 = Sprockets::Environment.new(fixture_path('asset')) do |env|
env.append_path(".")
env.cache = @cache
end
env1['required_assets.js']

sandbox dependency_file do
write(dependency_file, "var aa = 2;")
env1['required_assets.js']

# We must use private APIs to test this behavior
# https://github.com/rails/sprockets/pull/141
cache_entries = @cache.send(:find_caches).map do |file, _|
key = file.gsub(/\.cache\z/, ''.freeze).split(@cache_dir).last
result = @cache.get(key)

if result.is_a?(Array)
result if result.first.is_a?(Set)
else
nil
end
end.compact

assert cache_entries.any?

cache_entries.each do |sets|
sets.each do |set|
refute set.any? {|uri| uri.include?(env1.root) }, "Expected entry in cache to not include absolute paths but did: #{set.inspect}"
end
end
end
end

test "no absolute paths are retuned from cache" do
env1 = Sprockets::Environment.new(fixture_path('default')) do |env|
env.append_path(".")
Expand Down